X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fmcs51%2Fgen.c;h=85405e07018adae484e6d4a6afd7ffb9d6f37c17;hb=5a2f57901a5aba5079b22ebd93c1abdbf3acfa27;hp=e2252b7e5bd8d835668edfdce20c9c7a1ed86a1d;hpb=4db4740164fed3cb25145cfdaadb986fc0690507;p=fw%2Fsdcc diff --git a/src/mcs51/gen.c b/src/mcs51/gen.c index e2252b7e..85405e07 100644 --- a/src/mcs51/gen.c +++ b/src/mcs51/gen.c @@ -44,6 +44,7 @@ #include "gen.h" char *aopLiteral (value * val, int offset); +char *aopLiteralLong (value * val, int offset, int size); extern int allocInfo; /* this is the down and dirty file with all kinds of @@ -80,10 +81,10 @@ static unsigned short rbank = -1; #define SYM_BP(sym) (SPEC_OCLS (sym->etype)->paged ? "_bpx" : "_bp") -#define R0INB _G.bu.bs.r0InB -#define R1INB _G.bu.bs.r1InB -#define OPINB _G.bu.bs.OpInB -#define BINUSE _G.bu.BInUse +#define R0INB _G.bu.bs.r0InB +#define R1INB _G.bu.bs.r1InB +#define OPINB _G.bu.bs.OpInB +#define BINUSE _G.bu.BInUse static struct { @@ -110,7 +111,8 @@ static struct _G; static char *rb1regs[] = { - "b1_0","b1_1","b1_2","b1_3","b1_4","b1_5","b1_6","b1_7" + "b1_0","b1_1","b1_2","b1_3","b1_4","b1_5","b1_6","b1_7", + "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7" }; extern int mcs51_ptrRegReq; @@ -164,7 +166,7 @@ emitcode (char *inst, const char *fmt,...) else tvsprintf (lb, sizeof(lb), fmt, ap); - while (isspace (*lbp)) + while (isspace ((unsigned char)*lbp)) lbp++; if (lbp && *lbp) @@ -203,6 +205,24 @@ mova (const char *x) emitcode("mov","a,%s", x); } +/*-----------------------------------------------------------------*/ +/* movc - moves specified value into the carry */ +/*-----------------------------------------------------------------*/ +static void +movc (const char *s) +{ + if (s == zero) + CLRC; + else if (s == one) + SETC; + else if (strcmp (s, "c")) + {/* it's not in carry already */ + MOVA (s); + /* set C, if a >= 1 */ + emitcode ("add", "a,#0xff"); + } +} + /*-----------------------------------------------------------------*/ /* pushB - saves register B if necessary */ /*-----------------------------------------------------------------*/ @@ -240,6 +260,42 @@ popB (bool pushedB) } } +/*-----------------------------------------------------------------*/ +/* pushReg - saves register */ +/*-----------------------------------------------------------------*/ +static bool +pushReg (int index, bool bits_pushed) +{ + regs * reg = mcs51_regWithIdx (index); + if (reg->type == REG_BIT) + { + if (!bits_pushed) + emitcode ("push", "%s", reg->base); + return TRUE; + } + else + emitcode ("push", "%s", reg->dname); + return bits_pushed; +} + +/*-----------------------------------------------------------------*/ +/* popReg - restores register */ +/*-----------------------------------------------------------------*/ +static bool +popReg (int index, bool bits_popped) +{ + regs * reg = mcs51_regWithIdx (index); + if (reg->type == REG_BIT) + { + if (!bits_popped) + emitcode ("pop", "%s", reg->base); + return TRUE; + } + else + emitcode ("pop", "%s", reg->dname); + return bits_popped; +} + /*-----------------------------------------------------------------*/ /* getFreePtr - returns r0 or r1 whichever is free or can be pushed */ /*-----------------------------------------------------------------*/ @@ -402,7 +458,7 @@ getTempRegs(regs **tempRegs, int size, iCode *ic) } freeBitVect(freeRegs); - return 1; + return 0; } @@ -416,6 +472,7 @@ newAsmop (short type) aop = Safe_calloc (1, sizeof (asmop)); aop->type = type; + aop->allocated = 1; return aop; } @@ -512,7 +569,10 @@ aopForSym (iCode * ic, symbol * sym, bool result) /* if already has one */ if (sym->aop) - return sym->aop; + { + sym->aop->allocated++; + return sym->aop; + } /* assign depending on the storage class */ /* if it is on the stack or indirectly addressable */ @@ -598,7 +658,7 @@ aopForSym (iCode * ic, symbol * sym, bool result) sym->aop = aop = newAsmop (AOP_IMMD); aop->aopu.aop_immd.aop_immd1 = Safe_calloc (1, strlen (sym->rname) + 1); strcpy (aop->aopu.aop_immd.aop_immd1, sym->rname); - aop->size = FPTRSIZE; + aop->size = getSize (sym->type); return aop; } @@ -637,7 +697,7 @@ aopForRemat (symbol * sym) aop->aopu.aop_immd.from_cast_remat = 1; ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode; ptr_type = pointerTypeToGPByte (DCL_TYPE(from_type), NULL, NULL); - continue ; + continue; } else break; ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode; @@ -727,8 +787,9 @@ operandsEqu (operand * op1, operand * op2) return TRUE; /* if they have the same rname */ - if (sym1->rname[0] && sym2->rname[0] - && strcmp (sym1->rname, sym2->rname) == 0) + if (sym1->rname[0] && sym2->rname[0] && + strcmp (sym1->rname, sym2->rname) == 0 && + !(IS_PARM (op2) && IS_ITEMP (op1))) return TRUE; /* if left is a tmp & right is not */ @@ -748,6 +809,24 @@ operandsEqu (operand * op1, operand * op2) return FALSE; } +/*-----------------------------------------------------------------*/ +/* sameReg - two asmops have the same register at given offsets */ +/*-----------------------------------------------------------------*/ +static bool +sameReg (asmop * aop1, int off1, asmop * aop2, int off2) +{ + if (aop1->type != AOP_REG && aop1->type != AOP_CRY) + return FALSE; + + if (aop1->type != aop2->type) + return FALSE; + + if (aop1->aopu.aop_reg[off1] != aop2->aopu.aop_reg[off2]) + return FALSE; + + return TRUE; +} + /*-----------------------------------------------------------------*/ /* sameRegs - two asmops have the same registers */ /*-----------------------------------------------------------------*/ @@ -759,16 +838,17 @@ sameRegs (asmop * aop1, asmop * aop2) if (aop1 == aop2) return TRUE; - if (aop1->type != AOP_REG || - aop2->type != AOP_REG) + if (aop1->type != AOP_REG && aop1->type != AOP_CRY) + return FALSE; + + if (aop1->type != aop2->type) return FALSE; if (aop1->size != aop2->size) return FALSE; for (i = 0; i < aop1->size; i++) - if (aop1->aopu.aop_reg[i] != - aop2->aopu.aop_reg[i]) + if (aop1->aopu.aop_reg[i] != aop2->aopu.aop_reg[i]) return FALSE; return TRUE; @@ -797,13 +877,17 @@ aopOp (operand * op, iCode * ic, bool result) } /* if already has a asmop then continue */ - if (op->aop ) - return; + if (op->aop) + { + op->aop->allocated++; + return; + } /* if the underlying symbol has a aop */ if (IS_SYMOP (op) && OP_SYMBOL (op)->aop) { op->aop = OP_SYMBOL (op)->aop; + op->aop->allocated++; return; } @@ -887,6 +971,16 @@ aopOp (operand * op, iCode * ic, bool result) return; } + /* if the type is a bit register */ + if (sym->regType == REG_BIT) + { + sym->aop = op->aop = aop = newAsmop (AOP_CRY); + aop->size = sym->nRegs;//1??? + aop->aopu.aop_reg[0] = sym->regs[0]; + aop->aopu.aop_dir = sym->regs[0]->name; + return; + } + /* must be in a register */ sym->aop = op->aop = aop = newAsmop (AOP_REG); aop->size = sym->nRegs; @@ -910,13 +1004,13 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop) if (!aop) return; - if (aop->freed) - goto dealloc; + aop->allocated--; - aop->freed = 1; + if (aop->allocated) + goto dealloc; - /* depending on the asmop type only three cases need work AOP_RO - , AOP_R1 && AOP_STK */ + /* depending on the asmop type only three cases need work + AOP_R0, AOP_R1 & AOP_STK */ switch (aop->type) { case AOP_R0: @@ -1029,7 +1123,7 @@ freeForBranchAsmop (operand * op) if (!aop) return; - if (aop->freed) + if (!aop->allocated) return; switch (aop->type) @@ -1281,6 +1375,50 @@ aopGet (operand * oper, int offset, bool bit16, bool dname) "aopget got unsupported aop->type"); exit (1); } + +/*-----------------------------------------------------------------*/ +/* aopPutUsesAcc - indicates ahead of time whether aopPut() will */ +/* clobber the accumulator */ +/*-----------------------------------------------------------------*/ +static bool +aopPutUsesAcc (operand * oper, const char *s, int offset) +{ + asmop * aop = AOP (oper); + + if (offset > (aop->size - 1)) + return FALSE; + + switch (aop->type) + { + case AOP_DUMMY: + return TRUE; + case AOP_DIR: + return FALSE; + case AOP_REG: + wassert(strcmp(aop->aopu.aop_reg[offset]->name, "a")); + return FALSE; + case AOP_DPTR: + return TRUE; + case AOP_R0: + case AOP_R1: + return ((aop->paged) || (*s == '@')); + case AOP_STK: + return (*s == '@'); + case AOP_CRY: + return (!aop->aopu.aop_dir || strcmp(s, aop->aopu.aop_dir)); + case AOP_STR: + return FALSE; + case AOP_IMMD: + return FALSE; + case AOP_ACC: + return FALSE; + default: + /* Error case --- will have been caught already */ + wassert(0); + return FALSE; + } +} + /*-----------------------------------------------------------------*/ /* aopPut - puts a string for a aop and indicates if acc is in use */ /*-----------------------------------------------------------------*/ @@ -1309,16 +1447,13 @@ aopPut (operand * result, const char *s, int offset, bool bvolatile) case AOP_DIR: if (SPEC_SCLS (getSpec (operandType (result))) == S_SFR && offset) - sprintf (d, "(%s >> %d)", - aop->aopu.aop_dir, offset * 8); + sprintf (d, "(%s >> %d)", aop->aopu.aop_dir, offset * 8); else if (offset) - sprintf (d, "(%s + %d)", - aop->aopu.aop_dir, offset); + sprintf (d, "(%s + %d)", aop->aopu.aop_dir, offset); else sprintf (d, "%s", aop->aopu.aop_dir); - if (strcmp (d, s) || - bvolatile) + if (strcmp (d, s) || bvolatile) emitcode ("mov", "%s,%s", d, s); if (!strcmp (d, "acc")) accuse = TRUE; @@ -1392,7 +1527,6 @@ aopPut (operand * result, const char *s, int offset, bool bvolatile) { MOVA (s); emitcode ("movx", "@%s,a", aop->aopu.aop_ptr->name); - } else if (*s == '@') { @@ -1448,7 +1582,7 @@ aopPut (operand * result, const char *s, int offset, bool bvolatile) emitcode ("setb", "%s", aop->aopu.aop_dir); else if (!strcmp (s, "c")) emitcode ("mov", "%s,c", aop->aopu.aop_dir); - else + else if (strcmp (s, aop->aopu.aop_dir)) { MOVA (s); /* set C, if a >= 1 */ @@ -1676,14 +1810,23 @@ genNot (iCode * ic) /* if in bit space then a special case */ if (AOP_TYPE (IC_LEFT (ic)) == AOP_CRY) { - emitcode ("mov", "c,%s", IC_LEFT (ic)->aop->aopu.aop_dir); - emitcode ("cpl", "c"); - outBitC (IC_RESULT (ic)); + /* if left==result then cpl bit */ + if (sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic)))) + { + emitcode ("cpl", "%s", IC_LEFT (ic)->aop->aopu.aop_dir); + } + else + { + emitcode ("mov", "c,%s", IC_LEFT (ic)->aop->aopu.aop_dir); + emitcode ("cpl", "c"); + outBitC (IC_RESULT (ic)); + } goto release; } toBoolean (IC_LEFT (ic)); + /* set C, if a == 0 */ tlbl = newiTempLabel (NULL); emitcode ("cjne", "a,#0x01,%05d$", tlbl->key + 100); emitcode ("", "%05d$:", tlbl->key + 100); @@ -1691,8 +1834,8 @@ genNot (iCode * ic) release: /* release the aops */ - freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); } @@ -1725,7 +1868,6 @@ genCpl (iCode * ic) bit -> int -> ~int -> bit uchar -> int -> ~int -> bit */ - werror(W_COMPLEMENT); emitcode ("setb", "%s", IC_RESULT (ic)->aop->aopu.aop_dir); goto release; } @@ -1760,8 +1902,8 @@ genCpl (iCode * ic) release: /* release the aops */ - freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); } /*-----------------------------------------------------------------*/ @@ -1870,8 +2012,8 @@ genUminus (iCode * ic) release: /* release the aops */ - freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? 0 : 1)); } /*-----------------------------------------------------------------*/ @@ -1916,8 +2058,15 @@ saveRegisters (iCode * lic) if (count == 1) { - i = bitVectFirstBit (rsave); - emitcode ("mov", "a,%s", mcs51_regWithIdx (i)->name); + regs * reg = mcs51_regWithIdx (bitVectFirstBit (rsave)); + if (reg->type == REG_BIT) + { + emitcode ("mov", "a,%s", reg->base); + } + else + { + emitcode ("mov", "a,%s", reg->name); + } emitcode ("mov", "r0,%s", spname); emitcode ("inc", "%s", spname);// allocate before use emitcode ("movx", "@r0,a"); @@ -1926,6 +2075,17 @@ saveRegisters (iCode * lic) } else if (count != 0) { + bitVect *rsavebits = bitVectIntersect (bitVectCopy (mcs51_allBitregs ()), rsave); + int nBits = bitVectnBitsOn (rsavebits); + + if (nBits != 0) + { + count = count - nBits + 1; + /* remove all but the first bits as they are pushed all at once */ + rsave = bitVectCplAnd (rsave, rsavebits); + rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits)); + } + if (bitVectBitValue (rsave, R0_IDX)) { emitcode ("push", "%s", mcs51_regWithIdx (R0_IDX)->dname); @@ -1938,14 +2098,19 @@ saveRegisters (iCode * lic) { if (bitVectBitValue (rsave, i)) { + regs * reg = mcs51_regWithIdx (i); if (i == R0_IDX) { emitcode ("pop", "acc"); emitcode ("push", "acc"); } + else if (reg->type == REG_BIT) + { + emitcode ("mov", "a,%s", reg->base); + } else { - emitcode ("mov", "a,%s", mcs51_regWithIdx (i)->name); + emitcode ("mov", "a,%s", reg->name); } emitcode ("movx", "@r0,a"); if (--count) @@ -1961,11 +2126,16 @@ saveRegisters (iCode * lic) } } else - for (i = 0; i < mcs51_nRegs; i++) - { - if (bitVectBitValue (rsave, i)) - emitcode ("push", "%s", mcs51_regWithIdx (i)->dname); - } + { + bool bits_pushed = FALSE; + for (i = 0; i < mcs51_nRegs; i++) + { + if (bitVectBitValue (rsave, i)) + { + bits_pushed = pushReg (i, bits_pushed); + } + } + } } /*-----------------------------------------------------------------*/ @@ -1988,26 +2158,53 @@ unsaveRegisters (iCode * ic) if (count == 1) { + regs * reg = mcs51_regWithIdx (bitVectFirstBit (rsave)); emitcode ("mov", "r0,%s", spname); emitcode ("dec", "r0"); emitcode ("movx", "a,@r0"); - i = bitVectFirstBit (rsave); - emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name); + if (reg->type == REG_BIT) + { + emitcode ("mov", "%s,a", reg->base); + } + else + { + emitcode ("mov", "%s,a", reg->name); + } emitcode ("dec", "%s", spname); } else if (count != 0) { + bitVect *rsavebits = bitVectIntersect (bitVectCopy (mcs51_allBitregs ()), rsave); + int nBits = bitVectnBitsOn (rsavebits); + + if (nBits != 0) + { + count = count - nBits + 1; + /* remove all but the first bits as they are popped all at once */ + rsave = bitVectCplAnd (rsave, rsavebits); + rsave = bitVectSetBit (rsave, bitVectFirstBit (rsavebits)); + } + emitcode ("mov", "r0,%s", spname); for (i = mcs51_nRegs; i >= 0; i--) { if (bitVectBitValue (rsave, i)) { + regs * reg = mcs51_regWithIdx (i); emitcode ("dec", "r0"); emitcode ("movx", "a,@r0"); - if (i != R0_IDX) - emitcode ("mov", "%s,a", mcs51_regWithIdx (i)->name); + if (i == R0_IDX) + { + emitcode ("push", "acc"); + } + else if (reg->type == REG_BIT) + { + emitcode ("mov", "%s,a", reg->base); + } else - emitcode ("push", "acc"); + { + emitcode ("mov", "%s,a", reg->name); + } } } emitcode ("mov", "%s,r0", spname); @@ -2018,16 +2215,21 @@ unsaveRegisters (iCode * ic) } } else - for (i = mcs51_nRegs; i >= 0; i--) - { - if (bitVectBitValue (rsave, i)) - emitcode ("pop", "%s", mcs51_regWithIdx (i)->dname); - } + { + bool bits_popped = FALSE; + for (i = mcs51_nRegs; i >= 0; i--) + { + if (bitVectBitValue (rsave, i)) + { + bits_popped = popReg (i, bits_popped); + } + } + } } /*-----------------------------------------------------------------*/ -/* pushSide - */ +/* pushSide - */ /*-----------------------------------------------------------------*/ static void pushSide (operand * oper, int size) @@ -2044,22 +2246,38 @@ pushSide (operand * oper, int size) emitcode ("push", "acc"); } else + { emitcode ("push", "%s", l); } } +} /*-----------------------------------------------------------------*/ /* assignResultValue - also indicates if acc is in use afterwards */ /*-----------------------------------------------------------------*/ static bool -assignResultValue (operand * oper) +assignResultValue (operand * oper, operand * func) { int offset = 0; int size = AOP_SIZE (oper); bool accuse = FALSE; + bool pushedA = FALSE; + if (func && IS_BIT (OP_SYM_ETYPE (func))) + { + outBitC (oper); + return FALSE; + } + + if ((size > 3) && aopPutUsesAcc (oper, fReturn[offset], offset)) + { + emitcode ("push", "acc"); + pushedA = TRUE; + } while (size--) { + if ((offset == 3) && pushedA) + emitcode ("pop", "acc"); accuse |= aopPut (oper, fReturn[offset], offset, isOperandVolatile (oper, FALSE)); offset++; } @@ -2119,6 +2337,7 @@ genIpush (iCode * ic) { int size, offset = 0; char *l; + char *prev = ""; D(emitcode ("; genIpush","")); @@ -2147,7 +2366,7 @@ genIpush (iCode * ic) return; } - /* this is a paramter push: in this case we call + /* this is a parameter push: in this case we call the routine to find the call and save those registers that need to be saved */ saveRegisters (ic); @@ -2173,11 +2392,15 @@ genIpush (iCode * ic) AOP_TYPE (IC_LEFT (ic)) != AOP_DIR && strcmp (l, "a")) { - MOVA (l); + if (strcmp (l, prev) || *l == '@') + MOVA (l); emitcode ("push", "acc"); } else + { emitcode ("push", "%s", l); + } + prev = l; } freeAsmop (IC_LEFT (ic), NULL, ic, TRUE); @@ -2214,7 +2437,7 @@ static void saveRBank (int bank, iCode * ic, bool pushPsw) { int i; - int count = mcs51_nRegs + (pushPsw ? 1 : 0); + int count = 8 + ((mcs51_nRegs > 8) ? 1 : 0) + (pushPsw ? 1 : 0); asmop *aop = NULL; regs *r = NULL; @@ -2237,7 +2460,7 @@ saveRBank (int bank, iCode * ic, bool pushPsw) emitcode ("mov", "%s,a", spname); } - for (i = 0; i < mcs51_nRegs; i++) + for (i = 0; i < 8; i++) { if (options.useXstack) { @@ -2252,6 +2475,22 @@ saveRBank (int bank, iCode * ic, bool pushPsw) regs8051[i].base, 8 * bank + regs8051[i].offset); } + if (mcs51_nRegs > 8) + { + if (options.useXstack) + { + emitcode ("mov", "a,bits"); + emitcode ("movx", "@%s,a", r->name); + if (--count) + emitcode ("inc", "%s", r->name); + } + else + { + emitcode ("push", "bits"); + } + BitBankUsed = 1; + } + if (pushPsw) { if (options.useXstack) @@ -2318,7 +2557,21 @@ unsaveRBank (int bank, iCode * ic, bool popPsw) } } - for (i = (mcs51_nRegs - 1); i >= 0; i--) + if (mcs51_nRegs > 8) + { + if (options.useXstack) + { + emitcode ("dec", "%s", r->name); + emitcode ("movx", "a,@%s", r->name); + emitcode ("mov", "bits,a"); + } + else + { + emitcode ("pop", "bits"); + } + } + + for (i = 7; i >= 0; i--) { if (options.useXstack) { @@ -2350,31 +2603,106 @@ unsaveRBank (int bank, iCode * ic, bool popPsw) /*-----------------------------------------------------------------*/ static void genSend(set *sendSet) { - iCode *sic; - int rb1_count = 0 ; + iCode *sic; + int bit_count = 0; - for (sic = setFirstItem (sendSet); sic; - sic = setNextItem (sendSet)) { + /* first we do all bit parameters */ + for (sic = setFirstItem (sendSet); sic; + sic = setNextItem (sendSet)) + { + if (sic->argreg > 12) + { + int bit = sic->argreg-13; + + aopOp (IC_LEFT (sic), sic, FALSE); + + /* if left is a literal then + we know what the value is */ + if (AOP_TYPE (IC_LEFT (sic)) == AOP_LIT) + { + if (((int) operandLitValue (IC_LEFT (sic)))) + emitcode ("setb", "b[%d]", bit); + else + emitcode ("clr", "b[%d]", bit); + } + else if (AOP_TYPE (IC_LEFT (sic)) == AOP_CRY) + { + char *l = AOP (IC_LEFT (sic))->aopu.aop_dir; + if (strcmp (l, "c")) + emitcode ("mov", "c,%s", l); + emitcode ("mov", "b[%d],c", bit); + } + else + { + /* we need to or */ + toBoolean (IC_LEFT (sic)); + /* set C, if a >= 1 */ + emitcode ("add", "a,#0xff"); + emitcode ("mov", "b[%d],c", bit); + } + bit_count++; + BitBankUsed = 1; + + freeAsmop (IC_LEFT (sic), NULL, sic, TRUE); + } + } + + if (bit_count) + { + saveRegisters (setFirstItem (sendSet)); + emitcode ("mov", "bits,b"); + } + + /* then we do all other parameters */ + for (sic = setFirstItem (sendSet); sic; + sic = setNextItem (sendSet)) + { + if (sic->argreg <= 12) + { int size, offset = 0; aopOp (IC_LEFT (sic), sic, FALSE); size = AOP_SIZE (IC_LEFT (sic)); - if (sic->argreg == 1) { - while (size--) { - char *l = aopGet (IC_LEFT (sic), offset, - FALSE, FALSE); + if (sic->argreg == 1) + { + while (size--) + { + char *l = aopGet (IC_LEFT (sic), offset, FALSE, FALSE); if (strcmp (l, fReturn[offset])) emitcode ("mov", "%s,%s", fReturn[offset], l); offset++; - } - rb1_count = 0; - } else { - while (size--) { - emitcode ("mov","b1_%d,%s",rb1_count++, - aopGet (IC_LEFT (sic), offset++,FALSE, FALSE)); - } - } + } + } + else + { + while (size--) + { + emitcode ("mov","%s,%s", rb1regs[sic->argreg+offset-5], + aopGet (IC_LEFT (sic), offset,FALSE, FALSE)); + offset++; + } + } freeAsmop (IC_LEFT (sic), NULL, sic, TRUE); + } + } +} + +/*-----------------------------------------------------------------*/ +/* selectRegBank - emit code to select the register bank */ +/*-----------------------------------------------------------------*/ +static void +selectRegBank (short bank, bool keepFlags) +{ + /* if f.e. result is in carry */ + if (keepFlags) + { + emitcode ("anl", "psw,#0xE7"); + if (bank) + emitcode ("orl", "psw,#0x%02x", (bank << 3) & 0xff); + } + else + { + emitcode ("mov", "psw,#0x%02x", (bank << 3) & 0xff); } } @@ -2385,14 +2713,18 @@ static void genCall (iCode * ic) { sym_link *dtype; + sym_link *etype; // bool restoreBank = FALSE; bool swapBanks = FALSE; bool accuse = FALSE; bool accPushed = FALSE; + bool resultInF0 = FALSE; + bool assignResultGenerated = FALSE; D(emitcode("; genCall","")); dtype = operandType (IC_LEFT (ic)); + etype = getSpec(dtype); /* if send set is not empty then assign */ if (_G.sendSet) { @@ -2408,37 +2740,57 @@ genCall (iCode * ic) /* if we are calling a not _naked function that is not using the same register bank then we need to save the destination registers on the stack */ - dtype = operandType (IC_LEFT (ic)); if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) && (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) && !IFFUNC_ISISR (dtype)) - { + { swapBanks = TRUE; - } + } /* if caller saves & we have not saved then */ if (!ic->regsSaved) saveRegisters (ic); if (swapBanks) - { + { emitcode ("mov", "psw,#0x%02x", ((FUNC_REGBANK(dtype)) << 3) & 0xff); - } + } /* make the call */ - emitcode ("lcall", "%s", (OP_SYMBOL (IC_LEFT (ic))->rname[0] ? - OP_SYMBOL (IC_LEFT (ic))->rname : - OP_SYMBOL (IC_LEFT (ic))->name)); + if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype))) + { + if (IFFUNC_CALLEESAVES(dtype)) + { + werror (E_BANKED_WITH_CALLEESAVES); + } + else + { + char *l = (OP_SYMBOL (IC_LEFT (ic))->rname[0] ? + OP_SYMBOL (IC_LEFT (ic))->rname : + OP_SYMBOL (IC_LEFT (ic))->name); + + emitcode ("mov", "r0,#%s", l); + emitcode ("mov", "r1,#(%s >> 8)", l); + emitcode ("mov", "r2,#(%s >> 16)", l); + emitcode ("lcall", "__sdcc_banked_call"); + } + } + else + { + emitcode ("lcall", "%s", (OP_SYMBOL (IC_LEFT (ic))->rname[0] ? + OP_SYMBOL (IC_LEFT (ic))->rname : + OP_SYMBOL (IC_LEFT (ic))->name)); + } if (swapBanks) - { - emitcode ("mov", "psw,#0x%02x", - ((FUNC_REGBANK(currFunc->type)) << 3) & 0xff); - } + { + selectRegBank (FUNC_REGBANK(currFunc->type), IS_BIT (etype)); + } /* if we need assign a result value */ if ((IS_ITEMP (IC_RESULT (ic)) && + !IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) && (OP_SYMBOL (IC_RESULT (ic))->nRegs || OP_SYMBOL (IC_RESULT (ic))->accuse || OP_SYMBOL (IC_RESULT (ic))->spildir)) || @@ -2449,7 +2801,8 @@ genCall (iCode * ic) aopOp (IC_RESULT (ic), ic, FALSE); _G.accInUse--; - accuse = assignResultValue (IC_RESULT (ic)); + accuse = assignResultValue (IC_RESULT (ic), IC_LEFT (ic)); + assignResultGenerated = TRUE; freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); } @@ -2465,6 +2818,13 @@ genCall (iCode * ic) emitcode ("push", "acc"); accPushed = TRUE; } + if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic))) && + IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) && + !assignResultGenerated) + { + emitcode ("mov", "F0,c"); + resultInF0 = TRUE; + } emitcode ("mov", "a,%s", spname); emitcode ("add", "a,#0x%02x", (-ic->parmBytes) & 0xff); @@ -2483,7 +2843,7 @@ genCall (iCode * ic) emitcode ("dec", "%s", spname); } - /* if we hade saved some registers then unsave them */ + /* if we had saved some registers then unsave them */ if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype)) { if (accuse && !accPushed && options.useXstack) @@ -2499,6 +2859,16 @@ genCall (iCode * ic) // if (restoreBank) // unsaveRBank (FUNC_REGBANK (dtype), ic, FALSE); + if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) && !assignResultGenerated) + { + if (resultInF0) + emitcode ("mov", "c,F0"); + + aopOp (IC_RESULT (ic), ic, FALSE); + assignResultValue (IC_RESULT (ic), IC_LEFT (ic)); + freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + } + if (accPushed) emitcode ("pop", "acc"); } @@ -2510,69 +2880,155 @@ static void genPcall (iCode * ic) { sym_link *dtype; + sym_link *etype; symbol *rlbl = newiTempLabel (NULL); // bool restoreBank=FALSE; bool swapBanks = FALSE; + bool resultInF0 = FALSE; D(emitcode("; genPCall","")); - /* if caller saves & we have not saved then */ - if (!ic->regsSaved) - saveRegisters (ic); + dtype = operandType (IC_LEFT (ic))->next; + etype = getSpec(dtype); + /* if caller saves & we have not saved then */ + if (!ic->regsSaved) + saveRegisters (ic); + + /* if we are calling a not _naked function that is not using + the same register bank then we need to save the + destination registers on the stack */ + if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) && + (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) && + !IFFUNC_ISISR (dtype)) + { +// saveRBank (FUNC_REGBANK (dtype), ic, TRUE); +// restoreBank=TRUE; + swapBanks = TRUE; + // need caution message to user here + } + + if (IS_LITERAL(etype)) + { + /* if send set is not empty then assign */ + if (_G.sendSet) + { + genSend(reverseSet(_G.sendSet)); + _G.sendSet = NULL; + } + + if (swapBanks) + { + emitcode ("mov", "psw,#0x%02x", + ((FUNC_REGBANK(dtype)) << 3) & 0xff); + } + + if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype))) + { + if (IFFUNC_CALLEESAVES(dtype)) + { + werror (E_BANKED_WITH_CALLEESAVES); + } + else + { + char *l = aopLiteralLong (OP_VALUE (IC_LEFT (ic)), 0, 2); + + emitcode ("mov", "r0,#%s", l); + emitcode ("mov", "r1,#(%s >> 8)", l); + emitcode ("mov", "r2,#(%s >> 16)", l); + emitcode ("lcall", "__sdcc_banked_call"); + } + } + else + { + emitcode ("lcall", "%s", aopLiteralLong (OP_VALUE (IC_LEFT (ic)), 0, 2)); + } + } + else + { + if (IFFUNC_ISBANKEDCALL (dtype) && !SPEC_STAT(getSpec(dtype))) + { + if (IFFUNC_CALLEESAVES(dtype)) + { + werror (E_BANKED_WITH_CALLEESAVES); + } + else + { + aopOp (IC_LEFT (ic), ic, FALSE); + + if (!swapBanks) + { + emitcode ("mov", "ar0,%s", aopGet(IC_LEFT (ic), 0, FALSE, FALSE)); + emitcode ("mov", "ar1,%s", aopGet(IC_LEFT (ic), 1, FALSE, FALSE)); + emitcode ("mov", "ar2,%s", aopGet(IC_LEFT (ic), 2, FALSE, FALSE)); + } + else + { + int reg = ((FUNC_REGBANK(dtype)) << 3) & 0xff; + emitcode ("mov", "0x%02x,%s", reg++, aopGet(IC_LEFT (ic), 0, FALSE, FALSE)); + emitcode ("mov", "0x%02x,%s", reg++, aopGet(IC_LEFT (ic), 1, FALSE, FALSE)); + emitcode ("mov", "0x%02x,%s", reg, aopGet(IC_LEFT (ic), 2, FALSE, FALSE)); + } - /* if we are calling a not _naked function that is not using - the same register bank then we need to save the - destination registers on the stack */ - dtype = operandType (IC_LEFT (ic))->next; - if (currFunc && dtype && !IFFUNC_ISNAKED(dtype) && - (FUNC_REGBANK (currFunc->type) != FUNC_REGBANK (dtype)) && - !IFFUNC_ISISR (dtype)) - { -// saveRBank (FUNC_REGBANK (dtype), ic, TRUE); -// restoreBank=TRUE; - swapBanks = TRUE; - // need caution message to user here - } + freeAsmop (IC_LEFT (ic), NULL, ic, TRUE); - /* push the return address on to the stack */ - emitcode ("mov", "a,#%05d$", (rlbl->key + 100)); - emitcode ("push", "acc"); - emitcode ("mov", "a,#(%05d$ >> 8)", (rlbl->key + 100)); - emitcode ("push", "acc"); + /* if send set is not empty then assign */ + if (_G.sendSet) + { + genSend(reverseSet(_G.sendSet)); + _G.sendSet = NULL; + } - /* now push the calling address */ - aopOp (IC_LEFT (ic), ic, FALSE); + if (swapBanks) + { + emitcode ("mov", "psw,#0x%02x", + ((FUNC_REGBANK(dtype)) << 3) & 0xff); + } - pushSide (IC_LEFT (ic), FPTRSIZE); + /* make the call */ + emitcode ("lcall", "__sdcc_banked_call"); + } + } + else + { + /* push the return address on to the stack */ + emitcode ("mov", "a,#%05d$", (rlbl->key + 100)); + emitcode ("push", "acc"); + emitcode ("mov", "a,#(%05d$ >> 8)", (rlbl->key + 100)); + emitcode ("push", "acc"); - freeAsmop (IC_LEFT (ic), NULL, ic, TRUE); + /* now push the calling address */ + aopOp (IC_LEFT (ic), ic, FALSE); - /* if send set is not empty the assign */ - if (_G.sendSet) - { - genSend(reverseSet(_G.sendSet)); - _G.sendSet = NULL; - } + pushSide (IC_LEFT (ic), FPTRSIZE); - if (swapBanks) - { - emitcode ("mov", "psw,#0x%02x", - ((FUNC_REGBANK(dtype)) << 3) & 0xff); - } + freeAsmop (IC_LEFT (ic), NULL, ic, TRUE); - /* make the call */ - emitcode ("ret", ""); - emitcode ("", "%05d$:", (rlbl->key + 100)); + /* if send set is not empty the assign */ + if (_G.sendSet) + { + genSend(reverseSet(_G.sendSet)); + _G.sendSet = NULL; + } + if (swapBanks) + { + emitcode ("mov", "psw,#0x%02x", + ((FUNC_REGBANK(dtype)) << 3) & 0xff); + } + /* make the call */ + emitcode ("ret", ""); + emitcode ("", "%05d$:", (rlbl->key + 100)); + } + } if (swapBanks) - { - emitcode ("mov", "psw,#0x%02x", - ((FUNC_REGBANK(currFunc->type)) << 3) & 0xff); - } + { + selectRegBank (FUNC_REGBANK(currFunc->type), IS_BIT (etype)); + } /* if we need assign a result value */ if ((IS_ITEMP (IC_RESULT (ic)) && + !IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic))) && (OP_SYMBOL (IC_RESULT (ic))->nRegs || OP_SYMBOL (IC_RESULT (ic))->spildir)) || IS_TRUE_SYMOP (IC_RESULT (ic))) @@ -2582,18 +3038,24 @@ genPcall (iCode * ic) aopOp (IC_RESULT (ic), ic, FALSE); _G.accInUse--; - assignResultValue (IC_RESULT (ic)); + assignResultValue (IC_RESULT (ic), IC_LEFT (ic)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); } - /* adjust the stack for parameters if - required */ + /* adjust the stack for parameters if required */ if (ic->parmBytes) { int i; if (ic->parmBytes > 3) { + if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic))) && + IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic)))) + { + emitcode ("mov", "F0,c"); + resultInF0 = TRUE; + } + emitcode ("mov", "a,%s", spname); emitcode ("add", "a,#0x%02x", (-ic->parmBytes) & 0xff); emitcode ("mov", "%s,a", spname); @@ -2608,10 +3070,19 @@ genPcall (iCode * ic) // if (restoreBank) // unsaveRBank (FUNC_REGBANK (dtype), ic, TRUE); - /* if we hade saved some registers then - unsave them */ + /* if we had saved some registers then unsave them */ if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype)) unsaveRegisters (ic); + + if (IS_BIT (OP_SYM_ETYPE (IC_RESULT (ic)))) + { + if (resultInF0) + emitcode ("mov", "c,F0"); + + aopOp (IC_RESULT (ic), ic, FALSE); + assignResultValue (IC_RESULT (ic), IC_LEFT (ic)); + freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + } } /*-----------------------------------------------------------------*/ @@ -2700,15 +3171,18 @@ genFunction (iCode * ic) rbank = FUNC_REGBANK (ftype); for (i = 0; i < mcs51_nRegs; i++) { - if (strcmp (regs8051[i].base, "0") == 0) - emitcode ("", "%s = 0x%02x", - regs8051[i].dname, - 8 * rbank + regs8051[i].offset); - else - emitcode ("", "%s = %s + 0x%02x", - regs8051[i].dname, - regs8051[i].base, - 8 * rbank + regs8051[i].offset); + if (regs8051[i].type != REG_BIT) + { + if (strcmp (regs8051[i].base, "0") == 0) + emitcode ("", "%s = 0x%02x", + regs8051[i].dname, + 8 * rbank + regs8051[i].offset); + else + emitcode ("", "%s = %s + 0x%02x", + regs8051[i].dname, + regs8051[i].base, + 8 * rbank + regs8051[i].offset); + } } } @@ -2741,11 +3215,12 @@ genFunction (iCode * ic) /* if any registers used */ if (sym->regsUsed) { + bool bits_pushed = FALSE; /* save the registers used */ for (i = 0; i < sym->regsUsed->size; i++) { if (bitVectBitValue (sym->regsUsed, i)) - emitcode ("push", "%s", mcs51_regWithIdx (i)->dname); + bits_pushed = pushReg (i, bits_pushed); } } } @@ -2877,6 +3352,7 @@ genFunction (iCode * ic) /* if any registers used */ if (sym->regsUsed) { + bool bits_pushed = FALSE; /* save the registers used */ for (i = 0; i < sym->regsUsed->size; i++) { @@ -2885,7 +3361,7 @@ genFunction (iCode * ic) /* remember one saved register for later usage */ if (calleesaves_saved_register < 0) calleesaves_saved_register = i; - emitcode ("push", "%s", mcs51_regWithIdx (i)->dname); + bits_pushed = pushReg (i, bits_pushed); _G.nRegsSaved++; } } @@ -3097,8 +3573,18 @@ genEndFunction (iCode * ic) if (IFFUNC_ISCRITICAL (sym->type)) { - emitcode ("pop", "psw"); /* restore ea via c in psw */ - emitcode ("mov", "ea,c"); + if (IS_BIT (OP_SYM_ETYPE (IC_LEFT (ic)))) + { + emitcode ("rlc", "a"); /* save c in a */ + emitcode ("pop", "psw"); /* restore ea via c in psw */ + emitcode ("mov", "ea,c"); + emitcode ("rrc", "a"); /* restore c from a */ + } + else + { + emitcode ("pop", "psw"); /* restore ea via c in psw */ + emitcode ("mov", "ea,c"); + } } if ((IFFUNC_ISREENT (sym->type) || options.stackAuto)) @@ -3130,7 +3616,7 @@ genEndFunction (iCode * ic) /* restore the register bank */ if ( /* FUNC_REGBANK (sym->type) || */ IFFUNC_ISISR (sym->type)) { - if (/* !FUNC_REGBANK (sym->type) || */ !IFFUNC_ISISR (sym->type) + if (!FUNC_REGBANK (sym->type) || !IFFUNC_ISISR (sym->type) || !options.useXstack) { /* Special case of ISR using non-zero bank with useXstack @@ -3159,11 +3645,12 @@ genEndFunction (iCode * ic) /* if any registers used */ if (sym->regsUsed) { + bool bits_popped = FALSE; /* save the registers used */ for (i = sym->regsUsed->size; i >= 0; i--) { if (bitVectBitValue (sym->regsUsed, i)) - emitcode ("pop", "%s", mcs51_regWithIdx (i)->dname); + bits_popped = popReg (i, bits_popped); } } } @@ -3256,7 +3743,14 @@ genEndFunction (iCode * ic) debugFile->writeEndFunction (currFunc, ic, 1); } - emitcode ("ret", ""); + if (IFFUNC_ISBANKEDCALL (sym->type) && !SPEC_STAT(getSpec(sym->type))) + { + emitcode ("ljmp", "__sdcc_banked_ret"); + } + else + { + emitcode ("ret", ""); + } } if (!port->peep.getRegsRead || !port->peep.getRegsWritten || options.nopeep) @@ -3374,6 +3868,13 @@ genRet (iCode * ic) aopOp (IC_LEFT (ic), ic, FALSE); size = AOP_SIZE (IC_LEFT (ic)); + + if (IS_BIT(_G.currentFunc->etype)) + { + movc (aopGet (IC_LEFT (ic), 0, FALSE, FALSE)); + size = 0; + } + while (size--) { char *l; @@ -3394,16 +3895,13 @@ genRet (iCode * ic) } } - if (pushed) + while (pushed) { - while (pushed) - { - pushed--; - if (strcmp (fReturn[pushed], "a")) - emitcode ("pop", fReturn[pushed]); - else - emitcode ("pop", "acc"); - } + pushed--; + if (strcmp (fReturn[pushed], "a")) + emitcode ("pop", fReturn[pushed]); + else + emitcode ("pop", "acc"); } freeAsmop (IC_LEFT (ic), NULL, ic, TRUE); @@ -3486,16 +3984,14 @@ genPlusIncr (iCode * ic) if (AOP_TYPE (IC_RIGHT (ic)) != AOP_LIT) return FALSE; - /* if the literal value of the right hand side - is greater than 4 then it is not worth it */ - if ((icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit)) > 4) - return FALSE; + icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit); D(emitcode ("; genPlusIncr","")); /* if increment >=16 bits in register or direct space */ if ((AOP_TYPE(IC_LEFT(ic)) == AOP_REG || AOP_TYPE(IC_LEFT(ic)) == AOP_DIR ) && sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))) && + !isOperandVolatile (IC_RESULT (ic), FALSE) && (size > 1) && (icount == 1)) { @@ -3572,6 +4068,34 @@ genPlusIncr (iCode * ic) return TRUE; } + /* if result is dptr */ + if ((AOP_TYPE (IC_RESULT (ic)) == AOP_STR) && + (AOP_SIZE (IC_RESULT (ic)) == 2) && + !strncmp(AOP (IC_RESULT (ic))->aopu.aop_str[0], "dpl", 4) && + !strncmp(AOP (IC_RESULT (ic))->aopu.aop_str[1], "dph", 4)) + { + if (aopGetUsesAcc (IC_LEFT (ic), 0)) + return FALSE; + + if (icount > 9) + return FALSE; + + if ((AOP_TYPE (IC_LEFT (ic)) != AOP_DIR) && (icount > 5)) + return FALSE; + + aopPut (IC_RESULT (ic), aopGet (IC_LEFT (ic), 0, FALSE, FALSE), 0, FALSE); + aopPut (IC_RESULT (ic), aopGet (IC_LEFT (ic), 1, FALSE, FALSE), 1, FALSE); + while (icount--) + emitcode ("inc", "dptr"); + + return TRUE; + } + + /* if the literal value of the right hand side + is greater than 4 then it is not worth it */ + if (icount > 4) + return FALSE; + /* if the sizes are greater than 1 then we cannot */ if (AOP_SIZE (IC_RESULT (ic)) > 1 || AOP_SIZE (IC_LEFT (ic)) > 1) @@ -3737,6 +4261,7 @@ genPlus (iCode * ic) int size, offset = 0; int skip_bytes = 0; char *add = "add"; + bool swappedLR = FALSE; operand *leftOp, *rightOp; operand * op; @@ -3758,6 +4283,7 @@ genPlus (iCode * ic) operand *t = IC_RIGHT (ic); IC_RIGHT (ic) = IC_LEFT (ic); IC_LEFT (ic) = t; + swappedLR = TRUE; } /* if both left & right are in bit @@ -3802,7 +4328,7 @@ genPlus (iCode * ic) size = getDataSize (IC_RESULT (ic)); leftOp = IC_LEFT(ic); rightOp = IC_RIGHT(ic); - op=IC_LEFT(ic); + op = IC_LEFT(ic); /* if this is an add for an array access at a 256 byte boundary */ @@ -3893,13 +4419,21 @@ genPlus (iCode * ic) adjustArithmeticResult (ic); release: - freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + if (!swappedLR) + { + freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + } + else + { + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + } } /*-----------------------------------------------------------------*/ -/* genMinusDec :- does subtraction with deccrement if possible */ +/* genMinusDec :- does subtraction with decrement if possible */ /*-----------------------------------------------------------------*/ static bool genMinusDec (iCode * ic) @@ -4009,9 +4543,23 @@ genMinusDec (iCode * ic) same */ if (sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic)))) { + char *l; + + if (aopGetUsesAcc (IC_LEFT (ic), 0)) + { + MOVA (aopGet (IC_RESULT (ic), 0, FALSE, FALSE)); + l = "a"; + } + else + { + l = aopGet (IC_RESULT (ic), 0, FALSE, FALSE); + } while (icount--) - emitcode ("dec", "%s", aopGet (IC_RESULT (ic), 0, FALSE, FALSE)); + emitcode ("dec", "%s", l); + + if (AOP_NEEDSACC (IC_RESULT (ic))) + aopPut (IC_RESULT (ic), "a", 0, isOperandVolatile (IC_RESULT (ic), FALSE)); return TRUE; } @@ -4112,22 +4660,35 @@ genMinus (iCode * ic) while (size--) { - if (useCarry || ((lit >> (offset * 8)) & 0x0FFL)) { + if (useCarry || ((lit >> (offset * 8)) & 0x0FFL)) + { MOVA (aopGet (IC_LEFT (ic), offset, FALSE, FALSE)); - if (!offset && !size && lit== (unsigned long) -1) { - emitcode ("dec", "a"); - } else if (!useCarry) { - /* first add without previous c */ - emitcode ("add", "a,#0x%02x", - (unsigned int) ((lit >> (offset * 8)) & 0x0FFL)); - useCarry = TRUE; - } else { - emitcode ("addc", "a,#0x%02x", - (unsigned int) ((lit >> (offset * 8)) & 0x0FFL)); - } - aopPut (IC_RESULT (ic), "a", offset++, isOperandVolatile (IC_RESULT (ic), FALSE)); - } else { + if (!offset && !size && lit== (unsigned long) -1) + { + emitcode ("dec", "a"); + } + else if (!useCarry) + { + /* first add without previous c */ + emitcode ("add", "a,#0x%02x", + (unsigned int) ((lit >> (offset * 8)) & 0x0FFL)); + useCarry = TRUE; + } + else + { + emitcode ("addc", "a,#0x%02x", + (unsigned int) ((lit >> (offset * 8)) & 0x0FFL)); + } + aopPut (IC_RESULT (ic), "a", offset++, isOperandVolatile (IC_RESULT (ic), FALSE)); + } + else + { /* no need to add zeroes */ + if (!sameRegs (AOP (IC_RESULT (ic)), AOP (IC_LEFT (ic)))) + { + aopPut (IC_RESULT (ic), aopGet (IC_LEFT (ic), offset, FALSE, FALSE), + offset, isOperandVolatile (IC_RESULT (ic), FALSE)); + } offset++; } } @@ -4154,13 +4715,17 @@ genMinus (iCode * ic) emitcode ("subb", "a,b"); popB (pushedB); } else { + /* reverse subtraction with 2's complement */ + if (offset == 0) + emitcode( "setb", "c"); + else + emitcode( "cpl", "c"); wassertl(!aopGetUsesAcc(leftOp, offset), "accumulator clash"); MOVA (aopGet(rightOp, offset, FALSE, TRUE)); - if (offset == 0) { - emitcode( "setb", "c"); - } emitcode("subb", "a,%s", aopGet(leftOp, offset, FALSE, TRUE)); emitcode("cpl", "a"); + if (size) /* skip if last byte */ + emitcode( "cpl", "c"); } } else { MOVA (aopGet (leftOp, offset, FALSE, FALSE)); @@ -4178,9 +4743,9 @@ genMinus (iCode * ic) adjustArithmeticResult (ic); release: - freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); + freeAsmop (IC_RIGHT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + freeAsmop (IC_LEFT (ic), NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); } @@ -4414,7 +4979,7 @@ genMult (iCode * ic) D(emitcode ("; genMult","")); - /* assign the amsops */ + /* assign the asmops */ aopOp (left, ic, FALSE); aopOp (right, ic, FALSE); aopOp (result, ic, TRUE); @@ -4490,6 +5055,8 @@ genDivOneByte (operand * left, { bool lUnsigned, rUnsigned, pushedB; bool runtimeSign, compiletimeSign; + bool accuse = FALSE; + bool pushedA = FALSE; symbol *lbl; int size, offset; @@ -4653,20 +5220,32 @@ genDivOneByte (operand * left, emitcode ("inc", "a"); emitcode ("", "%05d$:", (lbl->key + 100)); - aopPut (result, "a", 0, isOperandVolatile (result, FALSE)); + accuse = aopPut (result, "a", 0, isOperandVolatile (result, FALSE)); if (size > 0) { /* msb is 0x00 or 0xff depending on the sign */ if (runtimeSign) { + if (accuse) + { + emitcode ("push", "acc"); + pushedA = TRUE; + } emitcode ("mov", "c,F0"); emitcode ("subb", "a,acc"); while (size--) aopPut (result, "a", offset++, isOperandVolatile (result, FALSE)); } else /* compiletimeSign */ - while (size--) - aopPut (result, "#0xff", offset++, isOperandVolatile (result, FALSE)); + { + if (aopPutUsesAcc (result, "#0xFF", offset)) + { + emitcode ("push", "acc"); + pushedA = TRUE; + } + while (size--) + aopPut (result, "#0xff", offset++, isOperandVolatile (result, FALSE)); + } } } else @@ -4676,6 +5255,8 @@ genDivOneByte (operand * left, aopPut (result, zero, offset++, isOperandVolatile (result, FALSE)); } + if (pushedA) + emitcode ("pop", "acc"); popB (pushedB); } @@ -4716,9 +5297,9 @@ genDiv (iCode * ic) /* should have been converted to function call */ assert (0); release: + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -4987,9 +5568,9 @@ genMod (iCode * ic) assert (0); release: + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -5188,9 +5769,9 @@ genCmpGt (iCode * ic, iCode * ifx) sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) || (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype)))); /* assign the amsops */ + aopOp (result, ic, TRUE); aopOp (left, ic, FALSE); aopOp (right, ic, FALSE); - aopOp (result, ic, TRUE); genCmp (right, left, result, ifx, sign, ic); @@ -5218,11 +5799,11 @@ genCmpLt (iCode * ic, iCode * ifx) sign = !((SPEC_USIGN (letype) && !(IS_CHAR (letype) && IS_LITERAL (letype))) || (SPEC_USIGN (retype) && !(IS_CHAR (retype) && IS_LITERAL (retype)))); /* assign the amsops */ + aopOp (result, ic, TRUE); aopOp (left, ic, FALSE); aopOp (right, ic, FALSE); - aopOp (result, ic, TRUE); - genCmp (left, right, result, ifx, sign,ic); + genCmp (left, right, result, ifx, sign, ic); freeAsmop (result, NULL, ic, TRUE); } @@ -5330,6 +5911,7 @@ gencjne (operand * left, operand * right, symbol * lbl) static void genCmpEq (iCode * ic, iCode * ifx) { + bool swappedLR = FALSE; operand *left, *right, *result; D(emitcode ("; genCmpEq","")); @@ -5347,6 +5929,7 @@ genCmpEq (iCode * ic, iCode * ifx) operand *t = IC_RIGHT (ic); IC_RIGHT (ic) = IC_LEFT (ic); IC_LEFT (ic) = t; + swappedLR = TRUE; } if (ifx && !AOP_SIZE (result)) @@ -5498,9 +6081,17 @@ genCmpEq (iCode * ic, iCode * ifx) } release: - freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (result, NULL, ic, TRUE); + if (!swappedLR) + { + freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + } + else + { + freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); + } } /*-----------------------------------------------------------------*/ @@ -5598,9 +6189,9 @@ genAndOp (iCode * ic) outBitAcc (result); } + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } @@ -5640,9 +6231,9 @@ genOrOp (iCode * ic) outBitAcc (result); } + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -5950,9 +6541,24 @@ genAnd (iCode * ic, iCode * ifx) } else { - if (AOP_TYPE (left) == AOP_ACC && offset == 0) + if (AOP_TYPE (left) == AOP_ACC) + { + if (offset) + emitcode("mov", "a,b"); + emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("anl", "a,b"); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); + } + else if (aopGetUsesAcc (left, offset)) { + MOVA (aopGet (left, offset, FALSE, FALSE)); emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); } else { @@ -6006,12 +6612,12 @@ genAnd (iCode * ic, iCode * ifx) MOVA (aopGet (right, offset, FALSE, FALSE)); emitcode("anl", "a,b"); } - } else { + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); - emitcode ("anl", "a,%s", + emitcode ("anl", "a,%s", aopGet (left, offset, FALSE, FALSE)); + } } - } emitcode ("jnz", "%05d$", tlbl->key + 100); offset++; } @@ -6075,11 +6681,21 @@ genAnd (iCode * ic, iCode * ifx) emitcode("mov", "a,b"); emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE)); } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("anl", "a,b"); + } + else if (aopGetUsesAcc (left, offset)) + { + MOVA (aopGet (left, offset, FALSE, FALSE)); + emitcode ("anl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); - emitcode ("anl", "a,%s", - aopGet (left, offset, FALSE, FALSE)); + emitcode ("anl", "a,%s", aopGet (left, offset, FALSE, FALSE)); } aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); } @@ -6087,9 +6703,9 @@ genAnd (iCode * ic, iCode * ifx) } release: + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6295,6 +6911,19 @@ genOr (iCode * ic, iCode * ifx) emitcode("mov", "a,b"); emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE)); } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("orl", "a,b"); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); + } + else if (aopGetUsesAcc (left, offset)) + { + MOVA (aopGet (left, offset, FALSE, FALSE)); + emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); @@ -6384,11 +7013,21 @@ genOr (iCode * ic, iCode * ifx) emitcode("mov", "a,b"); emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE)); } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("orl", "a,b"); + } + else if (aopGetUsesAcc (left, offset)) + { + MOVA (aopGet (left, offset, FALSE, FALSE)); + emitcode ("orl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); - emitcode ("orl", "a,%s", - aopGet (left, offset, FALSE, FALSE)); + emitcode ("orl", "a,%s", aopGet (left, offset, FALSE, FALSE)); } aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); } @@ -6396,9 +7035,9 @@ genOr (iCode * ic, iCode * ifx) } release: + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6583,6 +7222,19 @@ genXor (iCode * ic, iCode * ifx) emitcode("mov", "a,b"); emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE)); } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("xrl", "a,b"); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); + } + else if (aopGetUsesAcc (left, offset)) + { + MOVA (aopGet (left, offset, FALSE, FALSE)); + emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); @@ -6668,11 +7320,21 @@ genXor (iCode * ic, iCode * ifx) emitcode("mov", "a,b"); emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE)); } + else if (aopGetUsesAcc (left, offset) && aopGetUsesAcc (right, offset)) + { + emitcode ("mov", "b,%s", aopGet (left, offset, FALSE, FALSE)); + MOVA (aopGet (right, offset, FALSE, FALSE)); + emitcode ("xrl", "a,b"); + } + else if (aopGetUsesAcc (left, offset)) + { + MOVA (aopGet (left, offset, FALSE, FALSE)); + emitcode ("xrl", "a,%s", aopGet (right, offset, FALSE, FALSE)); + } else { MOVA (aopGet (right, offset, FALSE, FALSE)); - emitcode ("xrl", "a,%s", - aopGet (left, offset, FALSE, TRUE)); + emitcode ("xrl", "a,%s", aopGet (left, offset, FALSE, TRUE)); } aopPut (result, "a", offset, isOperandVolatile (result, FALSE)); } @@ -6680,9 +7342,9 @@ genXor (iCode * ic, iCode * ifx) } release: + freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE)); - freeAsmop (result, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6712,7 +7374,7 @@ genInline (iCode * ic) else { /* Add \n for labels, not dirs such as c:\mydir */ - if ( (*bp == ':') && (isspace(bp[1])) ) + if ( (*bp == ':') && (isspace((unsigned char)bp[1])) ) { bp++; *bp = '\0'; @@ -6776,8 +7438,8 @@ genRRC (iCode * ic) emitcode ("mov", "acc.7,c"); release: aopPut (result, "a", AOP_SIZE (result) - 1, isOperandVolatile (result, FALSE)); - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6831,8 +7493,8 @@ genRLC (iCode * ic) emitcode ("mov", "acc.0,c"); release: aopPut (result, "a", 0, isOperandVolatile (result, FALSE)); - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6864,9 +7526,142 @@ genGetHbit (iCode * ic) outAcc (result); } + freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); +} + +/*-----------------------------------------------------------------*/ +/* genGetAbit - generates code get a single bit */ +/*-----------------------------------------------------------------*/ +static void +genGetAbit (iCode * ic) +{ + operand *left, *right, *result; + int shCount; + + D(emitcode ("; genGetAbit","")); + + left = IC_LEFT (ic); + right = IC_RIGHT (ic); + result = IC_RESULT (ic); + aopOp (left, ic, FALSE); + aopOp (right, ic, FALSE); + aopOp (result, ic, FALSE); + + shCount = (int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit); + + /* get the needed byte into a */ + MOVA (aopGet (left, shCount / 8, FALSE, FALSE)); + shCount %= 8; + if (AOP_TYPE (result) == AOP_CRY) + { + if ((shCount) == 7) + emitcode ("rlc", "a"); + else if ((shCount) == 0) + emitcode ("rrc", "a"); + else + emitcode ("mov", "c,acc[%d]", shCount); + outBitC (result); + } + else + { + switch (shCount) + { + case 2: + emitcode ("rr", "a"); + //fallthrough + case 1: + emitcode ("rr", "a"); + //fallthrough + case 0: + emitcode ("anl", "a,#0x01"); + break; + case 3: + case 5: + emitcode ("mov", "c,acc[%d]", shCount); + emitcode ("clr", "a"); + emitcode ("rlc", "a"); + break; + case 4: + emitcode ("swap", "a"); + emitcode ("anl", "a,#0x01"); + break; + case 6: + emitcode ("rl", "a"); + //fallthrough + case 7: + emitcode ("rl", "a"); + emitcode ("anl", "a,#0x01"); + break; + } + outAcc (result); + } + + freeAsmop (result, NULL, ic, TRUE); + freeAsmop (right, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); +} + +/*-----------------------------------------------------------------*/ +/* genGetByte - generates code get a single byte */ +/*-----------------------------------------------------------------*/ +static void +genGetByte (iCode * ic) +{ + operand *left, *right, *result; + int offset; + + D(emitcode ("; genGetByte","")); + + left = IC_LEFT (ic); + right = IC_RIGHT (ic); + result = IC_RESULT (ic); + aopOp (left, ic, FALSE); + aopOp (right, ic, FALSE); + aopOp (result, ic, FALSE); + + offset = (int)floatFromVal (AOP (right)->aopu.aop_lit) / 8; + aopPut (result, + aopGet (left, offset, FALSE, FALSE), + 0, + isOperandVolatile (result, FALSE)); + freeAsmop (result, NULL, ic, TRUE); + freeAsmop (right, NULL, ic, TRUE); freeAsmop (left, NULL, ic, TRUE); +} + +/*-----------------------------------------------------------------*/ +/* genGetWord - generates code get two bytes */ +/*-----------------------------------------------------------------*/ +static void +genGetWord (iCode * ic) +{ + operand *left, *right, *result; + int offset; + + D(emitcode ("; genGetWord","")); + + left = IC_LEFT (ic); + right = IC_RIGHT (ic); + result = IC_RESULT (ic); + aopOp (left, ic, FALSE); + aopOp (right, ic, FALSE); + aopOp (result, ic, FALSE); + + offset = (int)floatFromVal (AOP (right)->aopu.aop_lit) / 8; + aopPut (result, + aopGet (left, offset, FALSE, FALSE), + 0, + isOperandVolatile (result, FALSE)); + aopPut (result, + aopGet (left, offset+1, FALSE, FALSE), + 1, + isOperandVolatile (result, FALSE)); + freeAsmop (result, NULL, ic, TRUE); + freeAsmop (right, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -6931,8 +7726,8 @@ genSwap (iCode * ic) wassertl(FALSE, "unsupported SWAP operand size"); } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } @@ -7118,7 +7913,7 @@ movLeft2Result (operand * left, int offl, /* MSB sign in acc.7 ! */ if (getDataSize (left) == offl + 1) { - emitcode ("mov", "a,%s", l); + MOVA (l); aopPut (result, "a", offr, isOperandVolatile (result, FALSE)); } } @@ -7433,21 +8228,47 @@ static void shiftL2Left2Result (operand * left, int offl, operand * result, int offr, int shCount) { + char * x; + bool pushedB = FALSE; + bool usedB = FALSE; + if (sameRegs (AOP (result), AOP (left)) && ((offl + MSB16) == offr)) { /* don't crash result[offr] */ MOVA (aopGet (left, offl, FALSE, FALSE)); emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE)); + x = aopGet (result, offr, FALSE, FALSE); + } + else if (aopGetUsesAcc (result, offr)) + { + movLeft2Result (left, offl, result, offr, 0); + pushedB = pushB (); + usedB = TRUE; + emitcode ("mov", "b,%s", aopGet (left, offl + MSB16, FALSE, FALSE)); + MOVA (aopGet (result, offr, FALSE, FALSE)); + emitcode ("xch", "a,b"); + x = "b"; } else { movLeft2Result (left, offl, result, offr, 0); MOVA (aopGet (left, offl + MSB16, FALSE, FALSE)); + x = aopGet (result, offr, FALSE, FALSE); } /* ax << shCount (x = lsb(result)) */ - AccAXLsh (aopGet (result, offr, FALSE, FALSE), shCount); - aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE)); + AccAXLsh (x, shCount); + if (usedB) + { + emitcode ("xch", "a,b"); + aopPut (result, "a", offr, isOperandVolatile (result, FALSE)); + aopPut (result, "b", offr + MSB16, isOperandVolatile (result, FALSE)); + popB (pushedB); + } + else + { + aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE)); + } } @@ -7459,23 +8280,45 @@ shiftR2Left2Result (operand * left, int offl, operand * result, int offr, int shCount, int sign) { + char * x; + bool pushedB = FALSE; + bool usedB = FALSE; + if (sameRegs (AOP (result), AOP (left)) && ((offl + MSB16) == offr)) { /* don't crash result[offr] */ MOVA (aopGet (left, offl, FALSE, FALSE)); emitcode ("xch", "a,%s", aopGet (left, offl + MSB16, FALSE, FALSE)); + x = aopGet (result, offr, FALSE, FALSE); + } + else if (aopGetUsesAcc (result, offr)) + { + movLeft2Result (left, offl, result, offr, 0); + pushedB = pushB (); + usedB = TRUE; + emitcode ("mov", "b,%s", aopGet (result, offr, FALSE, FALSE)); + MOVA (aopGet (left, offl + MSB16, FALSE, FALSE)); + x = "b"; } else { movLeft2Result (left, offl, result, offr, 0); MOVA (aopGet (left, offl + MSB16, FALSE, FALSE)); + x = aopGet (result, offr, FALSE, FALSE); } /* a:x >> shCount (x = lsb(result)) */ if (sign) - AccAXRshS (aopGet (result, offr, FALSE, FALSE), shCount); + AccAXRshS (x, shCount); else - AccAXRsh (aopGet (result, offr, FALSE, FALSE), shCount); + AccAXRsh (x, shCount); + if (usedB) + { + emitcode ("xch", "a,b"); + aopPut (result, "a", offr, isOperandVolatile (result, FALSE)); + emitcode ("xch", "a,b"); + popB (pushedB); + } if (getDataSize (result) > 1) aopPut (result, "a", offr + MSB16, isOperandVolatile (result, FALSE)); } @@ -7491,7 +8334,16 @@ shiftLLeftOrResult (operand * left, int offl, /* shift left accumulator */ AccLsh (shCount); /* or with result */ - emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE)); + if (aopGetUsesAcc (result, offr)) + { + emitcode ("xch", "a,b"); + MOVA (aopGet (result, offr, FALSE, FALSE)); + emitcode ("orl", "a,b"); + } + else + { + emitcode ("orl", "a,%s", aopGet (result, offr, FALSE, FALSE)); + } /* back to result */ aopPut (result, "a", offr, isOperandVolatile (result, FALSE)); } @@ -7783,8 +8635,8 @@ genLeftShiftLiteral (operand * left, break; } } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -7893,8 +8745,8 @@ genLeftShift (iCode * ic) emitcode ("djnz", "b,%05d$", tlbl->key + 100); popB (pushedB); release: - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -7943,59 +8795,89 @@ static void shiftRLong (operand * left, int offl, operand * result, int sign) { - int isSameRegs=sameRegs(AOP(left),AOP(result)); + bool useSameRegs = regsInCommon (left, result); - if (isSameRegs && offl>1) { - // we are in big trouble, but this shouldn't happen - werror(E_INTERNAL_ERROR, __FILE__, __LINE__); - } + if (useSameRegs && offl>1) + { + // we are in big trouble, but this shouldn't happen + werror(E_INTERNAL_ERROR, __FILE__, __LINE__); + } MOVA (aopGet (left, MSB32, FALSE, FALSE)); - if (offl==MSB16) { - // shift is > 8 - if (sign) { - emitcode ("rlc", "a"); - emitcode ("subb", "a,acc"); - if (isSameRegs) - emitcode ("xch", "a,%s", aopGet (left, MSB32, FALSE, FALSE)); - else { - aopPut (result, "a", MSB32, isOperandVolatile (result, FALSE)); - MOVA (aopGet (left, MSB32, FALSE, FALSE)); - } - } else { - aopPut (result, zero, MSB32, isOperandVolatile (result, FALSE)); + if (offl==MSB16) + { + // shift is > 8 + if (sign) + { + emitcode ("rlc", "a"); + emitcode ("subb", "a,acc"); + if (useSameRegs && sameReg (AOP (left), MSB32, AOP (result), MSB32)) + { + emitcode ("xch", "a,%s", aopGet (left, MSB32, FALSE, FALSE)); + } + else + { + aopPut (result, "a", MSB32, isOperandVolatile (result, FALSE)); + MOVA (aopGet (left, MSB32, FALSE, FALSE)); + } + } + else + { + aopPut (result, zero, MSB32, isOperandVolatile (result, FALSE)); + } } - } - if (!sign) { - emitcode ("clr", "c"); - } else { - emitcode ("mov", "c,acc.7"); - } + if (!sign) + { + emitcode ("clr", "c"); + } + else + { + emitcode ("mov", "c,acc.7"); + } emitcode ("rrc", "a"); - if (isSameRegs && offl==MSB16) { - emitcode ("xch", "a,%s",aopGet (left, MSB24, FALSE, FALSE)); - } else { - aopPut (result, "a", MSB32-offl, isOperandVolatile (result, FALSE)); - MOVA (aopGet (left, MSB24, FALSE, FALSE)); - } + if (useSameRegs && offl==MSB16 && + sameReg (AOP (left), MSB24, AOP (result), MSB32-offl)) + { + emitcode ("xch", "a,%s",aopGet (left, MSB24, FALSE, FALSE)); + } + else + { + aopPut (result, "a", MSB32-offl, isOperandVolatile (result, FALSE)); + MOVA (aopGet (left, MSB24, FALSE, FALSE)); + } emitcode ("rrc", "a"); - if (isSameRegs && offl==1) { - emitcode ("xch", "a,%s",aopGet (left, MSB16, FALSE, FALSE)); - } else { - aopPut (result, "a", MSB24-offl, isOperandVolatile (result, FALSE)); - MOVA (aopGet (left, MSB16, FALSE, FALSE)); - } + if (useSameRegs && offl==1 && + sameReg (AOP (left), MSB16, AOP (result), MSB24-offl)) + { + emitcode ("xch", "a,%s",aopGet (left, MSB16, FALSE, FALSE)); + } + else + { + aopPut (result, "a", MSB24-offl, isOperandVolatile (result, FALSE)); + MOVA (aopGet (left, MSB16, FALSE, FALSE)); + } emitcode ("rrc", "a"); - aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE)); - - if (offl == LSB) + if (offl != LSB) + { + aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE)); + } + else { - MOVA (aopGet (left, LSB, FALSE, FALSE)); + if (useSameRegs && + sameReg (AOP (left), LSB, AOP (result), MSB16-offl)) + { + emitcode ("xch", "a,%s",aopGet (left, LSB, FALSE, FALSE)); + } + else + { + aopPut (result, "a", MSB16 - offl, isOperandVolatile (result, FALSE)); + MOVA (aopGet (left, LSB, FALSE, FALSE)); + } emitcode ("rrc", "a"); aopPut (result, "a", LSB, isOperandVolatile (result, FALSE)); } @@ -8133,8 +9015,8 @@ genRightShiftLiteral (operand * left, break; } } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -8244,8 +9126,8 @@ genSignedRightShift (iCode * ic) popB (pushedB); release: - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -8369,8 +9251,8 @@ genRightShift (iCode * ic) popB (pushedB); release: - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -8504,8 +9386,17 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx) if (blen < 8) { emitPtrByteGet (rname, ptype, FALSE); - AccRsh (bstr); + AccRol (8 - bstr); emitcode ("anl", "a,#0x%02x", ((unsigned char) -1) >> (8 - blen)); + if (!SPEC_USIGN (etype)) + { + /* signed bitfield */ + symbol *tlbl = newiTempLabel (NULL); + + emitcode ("jnb", "acc.%d,%05d$", blen - 1, tlbl->key + 100); + emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << blen)); + emitcode ("", "%05d$:", tlbl->key + 100); + } aopPut (result, "a", offset++, isOperandVolatile (result, FALSE)); goto finish; } @@ -8525,15 +9416,36 @@ genUnpackBits (operand * result, char *rname, int ptype, iCode *ifx) { emitPtrByteGet (rname, ptype, FALSE); emitcode ("anl", "a,#0x%02x", ((unsigned char) -1) >> (8-rlen)); + if (!SPEC_USIGN (etype)) + { + /* signed bitfield */ + symbol *tlbl = newiTempLabel (NULL); + + emitcode ("jnb", "acc.%d,%05d$", rlen - 1, tlbl->key + 100); + emitcode ("orl", "a,#0x%02x", (unsigned char) (0xff << rlen)); + emitcode ("", "%05d$:", tlbl->key + 100); + } aopPut (result, "a", offset++, isOperandVolatile (result, FALSE)); } finish: if (offset < rsize) { + char *source; + + if (SPEC_USIGN (etype)) + source = zero; + else + { + /* signed bitfield: sign extension with 0x00 or 0xff */ + emitcode ("rlc", "a"); + emitcode ("subb", "a,acc"); + + source = "a"; + } rsize -= offset; while (rsize--) - aopPut (result, zero, offset++, isOperandVolatile (result, FALSE)); + aopPut (result, source, offset++, isOperandVolatile (result, FALSE)); } } @@ -8566,8 +9478,8 @@ genDataPointerGet (operand * left, aopPut (result, buffer, offset++, isOperandVolatile (result, FALSE)); } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -8803,10 +9715,9 @@ genPagedPointerGet (operand * left, } /* done */ - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); if (pi) pi->generated = 1; - } /*--------------------------------------------------------------------*/ @@ -8917,8 +9828,8 @@ genFarPointerGet (operand * left, genIfxJump (ifx, "a", left, NULL, result); } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -8979,8 +9890,8 @@ genCodePointerGet (operand * left, genIfxJump (ifx, "a", left, NULL, result); } - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -9031,9 +9942,8 @@ genGenPointerGet (operand * left, genIfxJump (ifx, "a", left, NULL, result); } - - freeAsmop (left, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (left, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -9267,8 +10177,8 @@ genDataPointerSet (operand * right, aopGet (right, offset++, FALSE, FALSE)); } - freeAsmop (right, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (right, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -9491,8 +10401,6 @@ genPagedPointerSet (operand * right, if (pi) pi->generated = 1; freeAsmop (result, NULL, ic, TRUE); freeAsmop (right, NULL, ic, TRUE); - - } /*-----------------------------------------------------------------*/ @@ -9660,6 +10568,7 @@ genIfx (iCode * ic, iCode * popIc) { operand *cond = IC_COND (ic); int isbit = 0; + char *dup = NULL; D(emitcode ("; genIfx","")); @@ -9669,8 +10578,12 @@ genIfx (iCode * ic, iCode * popIc) if (AOP_TYPE (cond) != AOP_CRY) toBoolean (cond); else - isbit = 1; - /* the result is now in the accumulator */ + { + isbit = 1; + if (AOP(cond)->aopu.aop_dir) + dup = Safe_strdup(AOP(cond)->aopu.aop_dir); + } + /* the result is now in the accumulator or a directly addressable bit */ freeAsmop (cond, NULL, ic, TRUE); /* if there was something to be popped then do it */ @@ -9678,7 +10591,9 @@ genIfx (iCode * ic, iCode * popIc) genIpop (popIc); /* if the condition is a bit variable */ - if (isbit && IS_ITEMP (cond) && SPIL_LOC (cond)) + if (isbit && dup) + genIfxJump(ic, dup, NULL, NULL, NULL); + else if (isbit && IS_ITEMP (cond) && SPIL_LOC (cond)) genIfxJump (ic, SPIL_LOC (cond)->rname, NULL, NULL, NULL); else if (isbit && !IS_ITEMP (cond)) genIfxJump (ic, OP_SYMBOL (cond)->rname, NULL, NULL, NULL); @@ -9784,7 +10699,6 @@ genFarFarAssign (operand * result, operand * right, iCode * ic) aopPut (result, "a", --offset, isOperandVolatile (result, FALSE)); } freeAsmop (result, NULL, ic, FALSE); - } /*-----------------------------------------------------------------*/ @@ -9899,8 +10813,8 @@ genAssign (iCode * ic) } release: - freeAsmop (right, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); + freeAsmop (right, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -10028,8 +10942,8 @@ genCast (iCode * ic) /* if the result is a bit (and not a bitfield) */ // if (AOP_TYPE (result) == AOP_CRY) - if (IS_BITVAR (OP_SYMBOL (result)->type) - && !IS_BITFIELD (OP_SYMBOL (result)->type) ) + if (IS_BIT (OP_SYMBOL (result)->type)) + /* not for bitfields */ { /* if the right size is a literal then we know what the value is */ @@ -10184,9 +11098,8 @@ genCast (iCode * ic) /* we are done hurray !!!! */ release: - freeAsmop (right, NULL, ic, TRUE); freeAsmop (result, NULL, ic, TRUE); - + freeAsmop (right, NULL, ic, TRUE); } /*-----------------------------------------------------------------*/ @@ -10277,15 +11190,18 @@ genDjnz (iCode * ic, iCode * ifx) static void genReceive (iCode * ic) { - int size = getSize (operandType (IC_RESULT (ic))); - int offset = 0; + int size = getSize (operandType (IC_RESULT (ic))); + int offset = 0; + D(emitcode ("; genReceive","")); - if (ic->argreg == 1) { /* first parameter */ - if (isOperandInFarSpace (IC_RESULT (ic)) && + if (ic->argreg == 1) + { /* first parameter */ + if ((isOperandInFarSpace (IC_RESULT (ic)) || + isOperandInPagedSpace (IC_RESULT (ic))) && (OP_SYMBOL (IC_RESULT (ic))->isspilt || - IS_TRUE_SYMOP (IC_RESULT (ic)))) { - + IS_TRUE_SYMOP (IC_RESULT (ic)))) + { regs *tempRegs[4]; int receivingA = 0; int roffset = 0; @@ -10327,33 +11243,48 @@ genReceive (iCode * ic) } offset = fReturnSizeMCS51 - size; - while (size--) { + while (size--) + { emitcode ("push", "%s", (strcmp (fReturn[fReturnSizeMCS51 - offset - 1], "a") ? fReturn[fReturnSizeMCS51 - offset - 1] : "acc")); offset++; - } + } aopOp (IC_RESULT (ic), ic, FALSE); size = AOP_SIZE (IC_RESULT (ic)); offset = 0; - while (size--) { + while (size--) + { emitcode ("pop", "acc"); aopPut (IC_RESULT (ic), "a", offset++, isOperandVolatile (IC_RESULT (ic), FALSE)); - } - - } else { + } + } + else + { _G.accInUse++; aopOp (IC_RESULT (ic), ic, FALSE); _G.accInUse--; - assignResultValue (IC_RESULT (ic)); - } - } else { /* second receive onwards */ + assignResultValue (IC_RESULT (ic), NULL); + } + } + else if (ic->argreg > 12) + { /* bit parameters */ + if (OP_SYMBOL (IC_RESULT (ic))->regs[0]->rIdx != ic->argreg-5) + { + aopOp (IC_RESULT (ic), ic, FALSE); + emitcode ("mov", "c,%s", rb1regs[ic->argreg-5]); + outBitC(IC_RESULT (ic)); + } + } + else + { /* other parameters */ int rb1off ; aopOp (IC_RESULT (ic), ic, FALSE); rb1off = ic->argreg; - while (size--) { + while (size--) + { aopPut (IC_RESULT (ic), rb1regs[rb1off++ -5], offset++, isOperandVolatile (IC_RESULT (ic), FALSE)); - } - } + } + } release: freeAsmop (IC_RESULT (ic), NULL, ic, TRUE); @@ -10535,11 +11466,21 @@ gen51Code (iCode * lic) char regsInUse[80]; int i; + #if 0 for (i=0; i<8; i++) { sprintf (®sInUse[i], - "%c", ic->riu & (1<riu & (1<rMask, i)) + { + int offset = regs8051[i].offset; + regsInUse[offset] = offset + '0'; /* show rMask */ + } + #endif + } emitcode("", "; [%s] ic:%d: %s", regsInUse, ic->seq, printILine(ic)); } /* if the result is marked as @@ -10690,6 +11631,18 @@ gen51Code (iCode * lic) genGetHbit (ic); break; + case GETABIT: + genGetAbit (ic); + break; + + case GETBYTE: + genGetByte (ic); + break; + + case GETWORD: + genGetWord (ic); + break; + case LEFT_OP: genLeftShift (ic); break;