* src/SDCCsymt.h: use appropriate data-types on host for SDCC's int and long
[fw/sdcc] / src / xa51 / gen.c
index 2ce675329ec7837f960900daa215d9692ad2c4c1..fdd1a3c249f958324b2c6fdc22d3361dca1802d6 100755 (executable)
@@ -205,23 +205,23 @@ static asmop *aopForSym(symbol *sym,
       switch (size) 
        {
        case 1:
-         emitcode ("mov.b", "r0l,[%s]", getStackOffset(sym->stack));
+         emitcode ("mov.b", "r0l,[%s] ;aopForSym:stack:1", getStackOffset(sym->stack));
          sprintf (aop->name[0], "r0l");
          return aop;
        case 2:
-         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         emitcode ("mov.w", "r0,[%s] ;aopForSym:stack:2", getStackOffset(sym->stack));
          sprintf (aop->name[0], "r0");
          return aop;
        case 3:
-         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         emitcode ("mov.w", "r0,[%s] ;aopForSym:stack:3.w", getStackOffset(sym->stack));
          sprintf (aop->name[0], "r0");
-         emitcode ("mov.b", "r1l,[%s]", getStackOffset(sym->stack+2));
+         emitcode ("mov.b", "r1l,[%s] ;aopForSym:stack:3.b", getStackOffset(sym->stack+2));
          sprintf (aop->name[1], "r1l");
          return aop;
        case 4:
-         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         emitcode ("mov.w", "r0,[%s] ;aopForSym:stack:4", getStackOffset(sym->stack));
          sprintf (aop->name[0], "r0");
-         emitcode ("mov.w", "r1,[%s]", getStackOffset(sym->stack+2));
+         emitcode ("mov.w", "r1,[%s] ;aopForSym:stack:4", getStackOffset(sym->stack+2));
          sprintf (aop->name[1], "r1");
          return aop;
        }
@@ -374,9 +374,9 @@ static asmop *aopForVal(operand *op) {
        sprintf (aop->name[1], "#0");
        break;
       case 4:
-       sprintf (aop->name[0], "#0x%04lx",
+       sprintf (aop->name[0], "#0x%04x",
                 SPEC_CVAL(operandType(op)).v_ulong & 0xffff);
-       sprintf (aop->name[1], "#0x%04lx", 
+       sprintf (aop->name[1], "#0x%04x", 
                 SPEC_CVAL(operandType(op)).v_ulong >> 16);
        break;
       default:
@@ -476,7 +476,7 @@ char *opRegName(operand *op, int offset, char *opName, bool decorate) {
       break;
     case V_INT:
       if (SPEC_LONG(OP_VALUE(op)->type)) {
-       sprintf (opName, "#%s0x%02lx", decorate?"(long)":"",
+       sprintf (opName, "#%s0x%02x", decorate?"(long)":"",
                 SPEC_CVAL(OP_VALUE(op)->type).v_long);
       } else {
        sprintf (opName, "#%s0x%02x", decorate?"(int)":"",
@@ -949,7 +949,7 @@ static void genPlus (iCode * ic) {
   aopOp(left, !aopIsPtr(result), !aopIsDir(result));
   aopOp(right, !aopIsPtr(result), !aopIsDir(result));
 
-  // special case for * = * + char, needs a closer look
+  // special case for "* = * + char", needs a closer look
   // heck, this shouldn't have come here but bug-223113 does
   if (size==3 && AOP_SIZE(right)==1) {
     emitcode ("mov", "r1l,%s", AOP_NAME(right)[0]);
@@ -960,7 +960,7 @@ static void genPlus (iCode * ic) {
     return;
   }
 
-  // special case for (whatever)* = (whatever)** + char, needs a closer look
+  // special case for "xdata * = xdata * + char", needs a closer look
   // heck, this shouldn't have come here but bug-441448 does
   if (size==2 && AOP_SIZE(right)==1) {
     emitcode ("mov", "r1l,%s", AOP_NAME(right)[0]);
@@ -996,9 +996,62 @@ static void genPlus (iCode * ic) {
 /* genMinus - generates code for subtraction                       */
 /*-----------------------------------------------------------------*/
 static void genMinus (iCode * ic) {
+  operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
+  int size;
+  char *instr;
+
   printIc (0, "genMinus", ic, 1,1,1);
-}
 
+  size=aopOp(result, TRUE, TRUE);
+
+  /* if left is a literal, then exchange them */
+  if (IS_LITERAL(operandType(left))) {
+    operand *tmp = right;
+    right = left;
+    left = tmp;
+  }
+    
+  if (aopIsBit(result)) {
+    if (IS_LITERAL(operandType(right))) {
+      if (operandLitValue(right)) {
+       emitcode ("clr", AOP_NAME(result)[0]);
+       return;
+      }
+      aopOp(left, TRUE, TRUE);
+      emitcode ("mov", "%s,%s", AOP_NAME(result)[0], toBoolean(left));
+      return;
+    }
+    bailOut("genPlus: unfinished genPlus bit");
+  }
+  
+  if (isOperandEqual(result,left)) {
+    left->aop=result->aop;
+  } else {
+    aopOp(left, !aopIsPtr(result), !aopIsDir(result));
+  }
+  aopOp(right, !aopIsPtr(result), !aopIsDir(result));
+
+  if (size>1) {
+    instr="sub.w";
+  } else {
+    instr="sub.b";
+  }
+  if (!aopEqual(result->aop, left->aop, 0)) {
+    emitcode ("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+  }
+  emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+  if (size>2) {
+    if (!aopEqual(result->aop, left->aop, 1)) {
+      emitcode ("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(left)[1]);
+    }
+    if (size==3) {
+      // generic pointer
+    } else {
+      emitcode ("subb.w", "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+    }
+  }
+  return;
+}
 
 /*-----------------------------------------------------------------*/
 /* genMult - generates code for multiplication                     */
@@ -1052,15 +1105,16 @@ static void genCmp (iCode * ic, char *trueInstr, char *falseInstr) {
   char *instr;
   int jlbl;
 
-  if (!ifx) {
-    bailOut("genCmp: no ifx");
-  } else {
-    ifx->generated=1;
-  }
 
   size=aopOp(left, TRUE, TRUE);
   aopOp(right, !aopIsPtr(left), TRUE);
 
+  if (size==1) {
+    instr="cmp.b";
+  } else {
+    instr="cmp.w";
+  }
+
   if (IC_TRUE(ifx)) {
     isTrue=TRUE;
     jlbl=IC_TRUE(ifx)->key+100;
@@ -1068,17 +1122,25 @@ static void genCmp (iCode * ic, char *trueInstr, char *falseInstr) {
     isTrue=FALSE;
     jlbl=IC_FALSE(ifx)->key+100;
   }
-
-  if (size==1) {
-    instr="cmp.b";
+  
+  if (!ifx) {
+    aopOp(IC_RESULT(ic), !aopIsPtr(left), TRUE);
+    jlbl=newiTempLabel(NULL)->key+100;
+    emitcode("mov", "%s,#-1", AOP_NAME(IC_RESULT(ic))[0]);
+    emitcode(instr, "%s,%s", AOP_NAME(left)[0], AOP_NAME(right)[0]);
+    emitcode(isTrue ? trueInstr : falseInstr, "%05d$", jlbl);
+    emitcode("cpl", "%s", AOP_NAME(IC_RESULT(ic))[0]);
+    emitcode("", "%05d$:", jlbl);
   } else {
-    instr="cmp.w";
+    emitcode(instr, "%s,%s", AOP_NAME(left)[0], AOP_NAME(right)[0]);
+    emitcode(isTrue ? trueInstr : falseInstr, "%05d$", jlbl);
+    ifx->generated=1;
   }
-  emitcode(instr, "%s,%s", AOP_NAME(left)[0], AOP_NAME(right)[0]);
-  emitcode(isTrue ? trueInstr : falseInstr, "%05d$", jlbl);
+
   if (size>2) {
     bailOut("genCmp: size > 2");
   }
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -1874,10 +1936,16 @@ void genXA51Code (iCode * lic) {
                  ic->level, ic->block);
        _G.debugLine = 0;
       }
-      emitcode ("", ";\t%s:%d: %s", ic->filename, ic->lineno, 
-               printCLine(ic->filename, ic->lineno));
+      if (!options.noCcodeInAsm) {
+       emitcode ("", ";\t%s:%d: %s", ic->filename, ic->lineno, 
+                 printCLine(ic->filename, ic->lineno));
+      }
       cln = ic->lineno;
     }
+    if (options.iCodeInAsm) {
+      emitcode("", ";ic:%d: %s", ic->key, printILine(ic));
+    }
+    
     /* if the result is marked as
        spilt and rematerializable or code for
        this has already been generated then