Fixed up z80 port so that it works again
[fw/sdcc] / src / z80 / gen.c
index 431eb1bc9b643334cdefa1e41b88e5e54505be9c..2bdd7f38d866f20ce06015c7cacb9dfdeb19362d 100644 (file)
@@ -17,6 +17,7 @@
   Better reg packing, first peephole           20038   163     1873
   With assign packing                          19281   165     1849
   5/3/00                                       17741   185     17B6
+  With reg params for mul and div              16234   202     162D
 
   Michael Hope <michaelh@earthling.net>        2000
   Based on the mcs51 generator - 
@@ -148,6 +149,8 @@ static struct {
        int pushed_de;
     } stack;
     int frameId;
+    bool flush_statics;
+    bool in_home;
 } _G;
 
 static char *aopGet(asmop *aop, int offset, bool bit16);
@@ -721,8 +724,10 @@ char *aopGetLitWordLong(asmop *aop, int offset, bool with_hash)
        /* otherwise it is fairly simple */
        if (!IS_FLOAT(val->type)) {
            unsigned long v = floatFromVal(val);
+
            if (offset == 2)
                v >>= 16;
+
            if (with_hash)
                tsprintf(buffer, "!immedword", v);
            else
@@ -800,11 +805,31 @@ static bool requiresHL(asmop *aop)
     }
 }
 
+static char *fetchLitSpecial(asmop *aop, bool negate, bool xor)
+{
+    unsigned long v;
+    value * val = aop->aopu.aop_lit;
+
+    wassert(aop->type == AOP_LIT);
+    wassert(!IS_FLOAT(val->type));
+    
+    v = floatFromVal(val);
+
+    if (xor)
+       v ^= 0x8000;
+    if (negate)
+       v = -v;
+    v &= 0xFFFF;
+
+    tsprintf(buffer, "!immedword", v);
+    return gc_strdup(buffer);
+}
+
 static void fetchLitPair(PAIR_ID pairId, asmop *left, int offset)
 {
     const char *l;
     const char *pair = _pairs[pairId].name;
-    l = aopGetLitWordLong(left, offset, FALSE);
+    l = aopGetLitWordLong(left, 0, FALSE);
     wassert(l && pair);
 
     if (isPtr(pair)) {
@@ -825,13 +850,21 @@ static void fetchLitPair(PAIR_ID pairId, asmop *left, int offset)
        _G.pairs[pairId].lit = gc_strdup(l);
        _G.pairs[pairId].offset = offset;
     }
+    if (IS_GB && pairId == PAIR_DE && 0) {
+       if (_G.pairs[pairId].lit && !strcmp(_G.pairs[pairId].lit, l)) {
+           if (abs(_G.pairs[pairId].offset - offset) < 3) {
+               adjustPair(pair, &_G.pairs[pairId].offset, offset);
+               return;
+           }
+       }
+       _G.pairs[pairId].last_type = left->type;
+       _G.pairs[pairId].lit = gc_strdup(l);
+       _G.pairs[pairId].offset = offset;
+    }
     /* Both a lit on the right and a true symbol on the left */
-    /* PENDING: for re-target */
-#if 0
     if (offset)
-       emit2("ld %s,!hashedstr + %d", pair, l, offset);
-    else 
-#endif
+       emit2("ld %s,!hashedstr + %u", pair, l, offset);
+    else
        emit2("ld %s,!hashedstr", pair, l);
 }
 
@@ -844,9 +877,19 @@ static void fetchPairLong(PAIR_ID pairId, asmop *aop, int offset)
     else { /* we need to get it byte by byte */
        if (pairId == PAIR_HL && IS_GB && requiresHL(aop)) {
            aopGet(aop, offset, FALSE);
-           emit2("!ldahli");
-           emit2("ld h,!*hl");
-           emit2("ld l,a");
+           switch (aop->size) {
+           case 1:
+               emit2("ld l,!*hl");
+               emit2("ld h,!immedbyte", 0);
+               break;
+           case 2:
+               emit2("!ldahli");
+               emit2("ld h,!*hl");
+               emit2("ld l,a");
+               break;
+           default:
+               emit2("; WARNING: mlh woosed out.  This code is invalid.");
+           }
        }
        else if (IS_Z80 && aop->type == AOP_IY) {
            /* Instead of fetching relative to IY, just grab directly
@@ -993,7 +1036,9 @@ static char *aopGet(asmop *aop, int offset, bool bit16)
            tsprintf(s, "!*hl");
        }
        else {
-           tsprintf(s,"!*ixx", aop->aopu.aop_stk+offset);
+           if (aop->aopu.aop_stk >= 0)
+               offset += _G.stack.param_offset;
+           tsprintf(s,"!*ixx ; x", aop->aopu.aop_stk+offset);
        }
        ALLOC_ATOMIC(rs,strlen(s)+1);
        strcpy(rs,s);   
@@ -1129,6 +1174,8 @@ static void aopPut (asmop *aop, const char *s, int offset)
                emit2("ld !*hl,%s", s);
        }
        else {
+           if (aop->aopu.aop_stk >= 0)
+               offset += _G.stack.param_offset;
            if (!canAssignToPtr(s)) {
                emit2("ld a,%s", s);
                emit2("ld !*ixx,a", aop->aopu.aop_stk+offset);
@@ -1254,7 +1301,7 @@ void outAcc(operand *result)
 
 /** Take the value in carry and put it into a register
  */
-void outBitC(operand *result)
+void outBitCLong(operand *result, bool swap_sense)
 {
     /* if the result is bit */
     if (AOP_TYPE(result) == AOP_CRY) {
@@ -1264,10 +1311,17 @@ void outBitC(operand *result)
     else {
        emit2("ld a,!zero");
        emit2("rla");
+       if (swap_sense)
+           emit2("xor a,!immedbyte", 1);
         outAcc(result);
     }
 }
 
+void outBitC(operand *result)
+{
+    outBitCLong(result, FALSE);
+}
+
 /*-----------------------------------------------------------------*/
 /* toBoolean - emit code for orl a,operator(sizeop)                */
 /*-----------------------------------------------------------------*/
@@ -1629,6 +1683,30 @@ static int _opUsesPair(operand *op, iCode *ic, PAIR_ID pairId)
     return ret;
 }
 
+/* This is quite unfortunate */
+static void setArea(int inHome)
+{
+    static int lastArea = 0;
+
+    /*    
+    if (_G.in_home != inHome) {
+       if (inHome) {
+           const char *sz = port->mem.code_name;
+           port->mem.code_name = "HOME";
+           emit2("!area", CODE_NAME);
+           port->mem.code_name = sz;
+       }
+       else
+       emit2("!area", CODE_NAME);*/
+       _G.in_home = inHome;
+       //    }
+}
+
+static bool isInHome(void)
+{
+    return _G.in_home;
+}
+
 /** Emit the code for a call statement 
  */
 static void emitCall(iCode *ic, bool ispcall)
@@ -1636,9 +1714,6 @@ static void emitCall(iCode *ic, bool ispcall)
     int pushed_de = 0;
     link *detype = getSpec(operandType(IC_LEFT(ic)));
 
-    if (IS_BANKED(detype)) 
-       emit2("; call to a banked function");
-
     /* if caller saves & we have not saved then */
     if (!ic->regsSaved) {
        /* PENDING */
@@ -1709,7 +1784,7 @@ static void emitCall(iCode *ic, bool ispcall)
     }
 
     if (ispcall) {
-       if (IS_BANKED(detype)) {
+       if (IS_BANKEDCALL(detype)) {
            werror(W_INDIR_BANKED);
        }
        aopOp(IC_LEFT(ic),ic,FALSE, FALSE);
@@ -1736,7 +1811,7 @@ static void emitCall(iCode *ic, bool ispcall)
        char *name = OP_SYMBOL(IC_LEFT(ic))->rname[0] ?
            OP_SYMBOL(IC_LEFT(ic))->rname :
            OP_SYMBOL(IC_LEFT(ic))->name;
-       if (IS_BANKED(detype)) {
+       if (IS_BANKEDCALL(detype)) {
            emit2("call banked_call");
            emit2("!dws", name);
            emit2("!dw !bankimmeds", name);
@@ -1798,7 +1873,6 @@ static void emitCall(iCode *ic, bool ispcall)
 static void genCall (iCode *ic)
 {
     link *detype = getSpec(operandType(IC_LEFT(ic)));
-    if (IS_BANKED(detype)) emit2("; call to a banked function");
     emitCall(ic, FALSE);
 }
 
@@ -1827,6 +1901,8 @@ static int resultRemat (iCode *ic)
     return 0;
 }
 
+extern set *publics;
+
 /*-----------------------------------------------------------------*/
 /* genFunction - generated code for function entry                 */
 /*-----------------------------------------------------------------*/
@@ -1836,6 +1912,13 @@ static void genFunction (iCode *ic)
     link *fetype;
 
     nregssaved = 0;
+    setArea(IS_NONBANKED(sym->etype));
+
+    /* PENDING: hack */
+    if (!IS_STATIC(sym->etype)) {
+       addSetIfnotP(&publics, sym);
+    }
+
     /* create the function header */
     emit2("!functionheader", sym->name);
     /* PENDING: portability. */
@@ -1847,7 +1930,6 @@ static void genFunction (iCode *ic)
     /* if critical function then turn interrupts off */
     if (SPEC_CRTCL(fetype))
        emit2("!di");
-    if (SPEC_BANKED(fetype)) emit2("; Iam banked");
 
     /* if this is an interrupt service routine then
     save acc, b, dpl, dph  */
@@ -1940,6 +2022,7 @@ static void genEndFunction (iCode *ic)
        /* PENDING: portability. */
        emit2("__%s_end:", sym->rname);
     }
+    _G.flush_statics = 1;
     _G.stack.pushed = 0;
     _G.stack.offset = 0;
 }
@@ -2227,7 +2310,10 @@ static void genPlus (iCode *ic)
        if (AOP_TYPE(IC_LEFT(ic)) == AOP_STK ||
            AOP_TYPE(IC_RIGHT(ic)) == AOP_STK ||
            AOP_TYPE(IC_RESULT(ic)) == AOP_STK) {
-           if (size == 2) {
+           if ((AOP_SIZE(IC_LEFT(ic)) == 2 ||
+               AOP_SIZE(IC_RIGHT(ic)) == 2) &&
+               (AOP_SIZE(IC_LEFT(ic)) <= 2 &&
+                AOP_SIZE(IC_RIGHT(ic)) <= 2)) {
                if (getPairId(AOP(IC_RIGHT(ic))) == PAIR_BC) {
                    /* Swap left and right */
                    operand *t = IC_RIGHT(ic);
@@ -2389,7 +2475,10 @@ static void genMinus (iCode *ic)
        if (AOP_TYPE(IC_LEFT(ic)) == AOP_STK ||
            AOP_TYPE(IC_RIGHT(ic)) == AOP_STK ||
            AOP_TYPE(IC_RESULT(ic)) == AOP_STK) {
-           if (size == 2) {
+           if ((AOP_SIZE(IC_LEFT(ic)) == 2 ||
+               AOP_SIZE(IC_RIGHT(ic)) == 2) &&
+               (AOP_SIZE(IC_LEFT(ic)) <= 2 &&
+                AOP_SIZE(IC_RIGHT(ic)) <= 2)) {
                PAIR_ID left = getPairId(AOP(IC_LEFT(ic)));
                PAIR_ID right = getPairId(AOP(IC_RIGHT(ic)));
 
@@ -2491,7 +2580,7 @@ static void genIfxJump (iCode *ic, char *jval)
 
     /* if true label then we jump if condition
     supplied is true */
-    if ( IC_TRUE(ic) ) {
+    if (IC_TRUE(ic)) {
         jlbl = IC_TRUE(ic);
        if (!strcmp(jval, "a")) {
            inst = "nz";
@@ -2499,6 +2588,9 @@ static void genIfxJump (iCode *ic, char *jval)
        else if (!strcmp(jval, "c")) {
            inst = "c";
        }
+       else if (!strcmp(jval, "nc")) {
+           inst = "nc";
+       }
        else {
            /* The buffer contains the bit on A that we should test */
            inst = "nz";
@@ -2513,6 +2605,9 @@ static void genIfxJump (iCode *ic, char *jval)
        else if (!strcmp(jval, "c")) {
            inst = "nc";
        }
+       else if (!strcmp(jval, "nc")) {
+           inst = "c";
+       }
        else {
            /* The buffer contains the bit on A that we should test */
            inst = "z";
@@ -2524,6 +2619,8 @@ static void genIfxJump (iCode *ic, char *jval)
     }
     else if (!strcmp(jval, "c")) {
     }
+    else if (!strcmp(jval, "nc")) {
+    }
     else {
        emitcode("bit", "%s,a", jval);
     }
@@ -2533,6 +2630,11 @@ static void genIfxJump (iCode *ic, char *jval)
     ic->generated = 1;
 }
 
+static const char *_getPairIdName(PAIR_ID id)
+{
+    return _pairs[id].name;
+}
+
 /** Generic compare for > or <
  */
 static void genCmp (operand *left,operand *right,
@@ -2540,6 +2642,7 @@ static void genCmp (operand *left,operand *right,
 {
     int size, offset = 0 ;
     unsigned long lit = 0L;
+    bool swap_sense = FALSE;
 
     /* if left & right are bit variables */
     if (AOP_TYPE(left) == AOP_CRY &&
@@ -2564,6 +2667,40 @@ static void genCmp (operand *left,operand *right,
                emitcode("cp", "%s", aopGet(AOP(right), offset, FALSE));
         } 
        else {
+           /* Special cases:
+              On the GB:
+                 If the left or the right is a lit:
+                       Load -lit into HL, add to right via, check sense.
+           */
+           if (size == 2 && (AOP_TYPE(right) == AOP_LIT || AOP_TYPE(left) == AOP_LIT)) {
+               PAIR_ID id = PAIR_DE;
+               asmop *lit = AOP(right);
+               asmop *op = AOP(left);
+               swap_sense = TRUE;
+
+               if (AOP_TYPE(left) == AOP_LIT) {
+                   swap_sense = FALSE;
+                   lit = AOP(left);
+                   op = AOP(right);
+               }
+               if (sign) {
+                   emit2("ld e,%s", aopGet(op, 0, 0));
+                   emit2("ld a,%s", aopGet(op, 1, 0));
+                   emit2("xor a,!immedbyte", 0x80);
+                   emit2("ld d,a");
+               }
+               else {
+                   id = getPairId(op);
+                   if (id == PAIR_INVALID) {
+                       fetchPair(PAIR_DE, op);
+                       id = PAIR_DE;
+                   }
+               }
+               spillPair(PAIR_HL);
+               emit2("ld hl,%s", fetchLitSpecial(lit, TRUE, sign));
+               emit2("add hl,%s", _getPairIdName(id));
+               goto release;
+           }
             if(AOP_TYPE(right) == AOP_LIT) {
                 lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
                 /* optimize if(x < 0) or if(x >= 0) */
@@ -2637,15 +2774,15 @@ static void genCmp (operand *left,operand *right,
 
 release:
     if (AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) {
-        outBitC(result);
+        outBitCLong(result, swap_sense);
     } else {
         /* if the result is used in the next
         ifx conditional branch then generate
         code a little differently */
-        if (ifx )
-            genIfxJump (ifx,"c");
+        if (ifx)
+            genIfxJump(ifx, swap_sense ? "nc" : "c");
         else
-            outBitC(result);
+            outBitCLong(result, swap_sense);
         /* leave the result in acc */
     }
 }
@@ -4092,6 +4229,7 @@ static void genGenPointerGet (operand *left,
            }
            if (size) {
                emit2("inc %s", _pairs[pair].name);
+               _G.pairs[pair].offset++;
            }
         }
     }
@@ -4184,6 +4322,7 @@ static void genGenPointerSet (operand *right,
            }
            if (size) {
                emitcode("inc", _pairs[pairId].name);
+               _G.pairs[pairId].offset++;
            }
            offset++;
         }
@@ -4280,7 +4419,10 @@ static void genAddrOf (iCode *ic)
        spillCached();
        if (sym->onStack) {
            /* if it has an offset  then we need to compute it */
-           emitcode("ld", "hl,#%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset);
+           if (sym->stack > 0) 
+               emitcode("ld", "hl,#%d+%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset, _G.stack.param_offset);
+           else
+               emitcode("ld", "hl,#%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset);
            emitcode("add", "hl,sp");
        }
        else {
@@ -4821,7 +4963,17 @@ void genZ80Code (iCode *lic)
     if (!options.nopeep)
        peepHole (&lineHead);
 
+    /* This is unfortunate */
     /* now do the actual printing */
-    printLine (lineHead,codeOutFile);
-    return;
+    {
+       FILE *fp = codeOutFile;
+       if (isInHome() && codeOutFile == code->oFile)
+           codeOutFile = home->oFile;
+       printLine (lineHead, codeOutFile);
+       if (_G.flush_statics) {
+           flushStatics();
+           _G.flush_statics = 0;
+       }
+       codeOutFile = fp;
+    }
 }