X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fz80%2Fgen.c;h=4d9d0ffbd471438b67ae36a3f074a399e85eb99d;hb=6ee20dae1541e6e9edf127b58f2986bad8fd81c8;hp=9d60d5db109db5d932afbb5b02fc1aa78527b5b6;hpb=a408d9da29a7cd633e158f65770f223a83210a90;p=fw%2Fsdcc diff --git a/src/z80/gen.c b/src/z80/gen.c index 9d60d5db..4d9d0ffb 100644 --- a/src/z80/gen.c +++ b/src/z80/gen.c @@ -432,6 +432,8 @@ emit2 (const char *szFormat,...) static void emitDebug (const char *szFormat,...) { + if (!options.verboseAsm) + return; if (!DISABLE_DEBUG) { va_list ap; @@ -1508,8 +1510,18 @@ fetchPairLong (PAIR_ID pairId, asmop * aop, iCode *ic, int offset) } else { - emit2 ("ld %s,%s", _pairs[pairId].l, aopGet (aop, offset, FALSE)); - emit2 ("ld %s,%s", _pairs[pairId].h, aopGet (aop, offset + 1, FALSE)); + /* Swapping register contents within register pair */ + if(!strcmp(aopGet (aop, offset, FALSE), _pairs[pairId].h)) + { + emit2 ("ld a,%s",aopGet (aop, offset + 1, FALSE)); + emit2 ("ld %s,%s", _pairs[pairId].l, aopGet (aop, offset, FALSE)); + emit2 ("ld %s,a", _pairs[pairId].h); + } + else + { + emit2 ("ld %s,%s", _pairs[pairId].l, aopGet (aop, offset, FALSE)); + emit2 ("ld %s,%s", _pairs[pairId].h, aopGet (aop, offset + 1, FALSE)); + } } /* PENDING: check? */ if (pairId == PAIR_HL) @@ -2523,7 +2535,7 @@ assignResultValue (operand * oper) aopPut (AOP (oper), _fReturn[size], size-1); size--; } - while (size--) + while(size--) { aopPut (AOP (oper), _fReturn[size], size); } @@ -2946,19 +2958,6 @@ emitCall (iCode * ic, bool ispcall) /* Mark the registers as restored. */ _G.saves.saved = FALSE; - /* if we need assign a result value */ - if ((IS_ITEMP (IC_RESULT (ic)) && - (OP_SYMBOL (IC_RESULT (ic))->nRegs || - OP_SYMBOL (IC_RESULT (ic))->spildir)) || - IS_TRUE_SYMOP (IC_RESULT (ic))) - { - aopOp (IC_RESULT (ic), ic, FALSE, FALSE); - - assignResultValue (IC_RESULT (ic)); - - freeAsmop (IC_RESULT (ic), NULL, ic); - } - /* adjust the stack for parameters if required */ if (ic->parmBytes) { @@ -2993,6 +2992,19 @@ emitCall (iCode * ic, bool ispcall) } } + /* if we need assign a result value */ + if ((IS_ITEMP (IC_RESULT (ic)) && + (OP_SYMBOL (IC_RESULT (ic))->nRegs || + OP_SYMBOL (IC_RESULT (ic))->spildir)) || + IS_TRUE_SYMOP (IC_RESULT (ic))) + { + aopOp (IC_RESULT (ic), ic, FALSE, FALSE); + + assignResultValue (IC_RESULT (ic)); + + freeAsmop (IC_RESULT (ic), NULL, ic); + } + spillCached (); if (IC_RESULT (ic)) { @@ -3264,7 +3276,22 @@ genFunction (iCode * ic) else if (sym->stack && IS_GB && sym->stack > -INT8MIN) emit2 ("!enterxl", sym->stack); else if (sym->stack) - emit2 ("!enterx", sym->stack); + { + if ((optimize.codeSize && sym->stack <= 8) || sym->stack <= 4) + { + int stack = sym->stack; + emit2 ("!enter"); + while (stack > 1) + { + emit2 ("push af"); + stack -= 2; + } + if(stack > 0) + emit2 ("dec sp"); + } + else + emit2 ("!enterx", sym->stack); + } else emit2 ("!enter"); @@ -3834,7 +3861,16 @@ genPlus (iCode * ic) { fetchPair (PAIR_HL, AOP (IC_LEFT (ic))); emit2 ("add hl,%s", getPairName (AOP (IC_RIGHT (ic)))); - spillCached(); + spillPair (PAIR_HL); + commitPair ( AOP (IC_RESULT (ic)), PAIR_HL); + goto release; + } + + if (isPair (AOP (IC_LEFT (ic))) && AOP_TYPE (IC_RIGHT (ic)) == AOP_LIT && getPairId (AOP (IC_LEFT (ic))) != PAIR_HL) + { + fetchPair (PAIR_HL, AOP (IC_RIGHT (ic))); + emit2 ("add hl,%s", getPairName (AOP (IC_LEFT (ic)))); + spillPair (PAIR_HL); commitPair ( AOP (IC_RESULT (ic)), PAIR_HL); goto release; } @@ -3912,28 +3948,57 @@ genPlus (iCode * ic) setupToPreserveCarry (ic); - while (size--) + /* This is ugly, but it fixes the worst code generation bug on Z80. */ + /* Probably something similar has to be done for addition of larger numbers, too. */ + if(size == 2) { - if (AOP_TYPE (IC_LEFT (ic)) == AOP_ACC) + _moveA (aopGet (AOP (IC_LEFT (ic)), 0, FALSE)); + emit2 ("add a,%s", aopGet (AOP (IC_RIGHT (ic)), 0, FALSE)); + if(strcmp (aopGet (AOP (IC_RESULT (ic)), 0, FALSE), aopGet (AOP (IC_LEFT (ic)), 1, FALSE))) { - _moveA (aopGet (AOP (IC_LEFT (ic)), offset, FALSE)); - if (offset == 0) - emit2 ("add a,%s", - aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); - else - emit2 ("adc a,%s", - aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); + aopPut (AOP (IC_RESULT (ic)), "a", 0); + _moveA (aopGet (AOP (IC_LEFT (ic)), 1, FALSE)); } else { - _moveA (aopGet (AOP (IC_LEFT (ic)), offset, FALSE)); - if (offset == 0) - emit2 ("add a,%s", - aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); + emitDebug ("; Addition result is in same register as operand of next addition."); + if(strchr (aopGet (AOP (IC_RESULT (ic)), 0, FALSE), 'c') || + strchr (aopGet (AOP (IC_RESULT (ic)), 0, FALSE), 'b') ) + { + emit2 ("push de"); + emit2 ("ld e, a"); + emit2 ("ld a, %s", aopGet (AOP (IC_LEFT (ic)), 1, FALSE)); + emit2 ("ld d, a"); + emit2 ("ld a, e"); + emit2 ("ld %s, a", aopGet (AOP (IC_RESULT (ic)), 0, FALSE)); + emit2 ("ld a, d"); + emit2 ("pop de"); + } else - emit2 ("adc a,%s", - aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); + { + emit2 ("push bc"); + emit2 ("ld c, a"); + emit2 ("ld a, %s", aopGet (AOP (IC_LEFT (ic)), 1, FALSE)); + emit2 ("ld b, a"); + emit2 ("ld a, c"); + emit2 ("ld %s, a", aopGet (AOP (IC_RESULT (ic)), 0, FALSE)); + emit2 ("ld a, b"); + emit2 ("pop bc"); + } + } + emit2 ("adc a,%s", aopGet (AOP (IC_RIGHT (ic)), 1, FALSE)); + aopPut (AOP (IC_RESULT (ic)), "a", 1); + goto release; + } + + while (size--) + { + _moveA (aopGet (AOP (IC_LEFT (ic)), offset, FALSE)); + if (offset == 0) + emit2 ("add a,%s", aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); + else + emit2 ("adc a,%s", aopGet (AOP (IC_RIGHT (ic)), offset, FALSE)); aopPut (AOP (IC_RESULT (ic)), "a", offset++); } @@ -4126,7 +4191,12 @@ genMinus (iCode * ic) { /* first add without previous c */ if (!offset) - emit2 ("add a,!immedbyte", (unsigned int) (lit & 0x0FFL)); + { + if (size == 0 && (unsigned int) (lit & 0x0FFL) == 0xFF) + emit2 ("dec a"); + else + emit2 ("add a,!immedbyte", (unsigned int) (lit & 0x0FFL)); + } else emit2 ("adc a,!immedbyte", (unsigned int) ((lit >> (offset * 8)) & 0x0FFL)); } @@ -4192,7 +4262,9 @@ genMult (iCode * ic) _G.stack.pushedDE = TRUE; } - if ( AOP_SIZE (IC_LEFT (ic)) == 1 && !SPEC_USIGN (getSpec (operandType ( IC_LEFT (ic))))) + if (byteResult) + emit2 ("ld a,%s", aopGet (AOP (IC_LEFT (ic)), LSB, FALSE)); + else if ( AOP_SIZE (IC_LEFT (ic)) == 1 && !SPEC_USIGN (getSpec (operandType ( IC_LEFT (ic))))) { emit2 ("ld e,%s", aopGet (AOP (IC_LEFT (ic)), LSB, FALSE)); if (!byteResult) @@ -4210,32 +4282,40 @@ genMult (iCode * ic) i = val; - /* Fully unroled version of mul.s. Not the most efficient. - */ for (count = 0; count < 16; count++) { if (count != 0 && active) { - emit2 ("add hl,hl"); + if (byteResult) + emit2 ("add a,a"); + else + emit2 ("add hl,hl"); } if (i & 0x8000U) { if (active == FALSE) { - emit2 ("ld l,e"); - if (!byteResult) - emit2 ("ld h,d"); + if (byteResult) + emit2("ld e,a"); + else + { + emit2 ("ld l,e"); + emit2 ("ld h,d"); + } } else { - emit2 ("add hl,de"); + if (byteResult) + emit2 ("add a,e"); + else + emit2 ("add hl,de"); } active = TRUE; } i <<= 1; } - spillCached(); + spillPair(PAIR_HL); if (IS_Z80 && _G.stack.pushedDE) { @@ -4244,7 +4324,7 @@ genMult (iCode * ic) } if (byteResult) - aopPut (AOP (IC_RESULT (ic)), _pairs[PAIR_HL].l, 0); + aopPut (AOP (IC_RESULT (ic)), "a", 0); else commitPair ( AOP (IC_RESULT (ic)), PAIR_HL); @@ -6010,13 +6090,25 @@ shiftL1Left2Result (operand * left, int offl, operand * result, int offr, int shCount) { const char *l; - l = aopGet (AOP (left), offl, FALSE); - _moveA (l); - /* shift left accumulator */ - AccLsh (shCount); - aopPut (AOP (result), "a", offr); -} + /* If operand and result are the same we can shift in place. + However shifting in acc using add is cheaper than shifting + in place using sla; when shifting by more than 2 shifting in + acc is worth the additional effort for loading from/to acc. */ + if (sameRegs (AOP (left), AOP (result)) && shCount <= 2 && offr == offl) + { + while (shCount--) + emit2 ("sla %s", aopGet (AOP (result), 0, FALSE)); + } + else + { + l = aopGet (AOP (left), offl, FALSE); + _moveA (l); + /* shift left accumulator */ + AccLsh (shCount); + aopPut (AOP (result), "a", offr); + } +} /*-----------------------------------------------------------------*/ /* genlshTwo - left shift two bytes by known amount */ @@ -7119,7 +7211,7 @@ genAddrOf (iCode * ic) { if (sym->onStack) { - spillCached (); + spillPair (PAIR_HL); if (sym->stack <= 0) { setupPairFromSP (PAIR_HL, sym->stack + _G.stack.pushed + _G.stack.offset); @@ -7138,7 +7230,7 @@ genAddrOf (iCode * ic) } else { - spillCached (); + spillPair (PAIR_HL); if (sym->onStack) { /* if it has an offset then we need to compute it */ @@ -7318,7 +7410,7 @@ genJumpTab (iCode * ic) emit2 ("ld e,%s", l); emit2 ("ld d,!zero"); jtab = newiTempLabel (NULL); - spillCached (); + spillPair (PAIR_HL); emit2 ("ld hl,!immed!tlabel", jtab->key + 100); emit2 ("add hl,de"); emit2 ("add hl,de"); @@ -8138,7 +8230,7 @@ genZ80Code (iCode * lic) } if (options.iCodeInAsm) { - char *iLine = printILine(ic); + const char *iLine = printILine(ic); emit2 (";ic:%d: %s", ic->key, iLine); dbuf_free(iLine); }