]> git.gag.com Git - fw/sdcc/blobdiff - src/xa51/gen.c
xa51, work in progress
[fw/sdcc] / src / xa51 / gen.c
index 67690d5ee86d0c4029fa76abe98c9e682c57f969..74f0947db8789e2193907fd299c81439e7fe1dc3 100755 (executable)
@@ -74,6 +74,7 @@ static struct
   {
     short inLine;
     short debugLine;
+    short stackExtend;
     short nRegsSaved;
     set *sendSet;
   }
@@ -95,6 +96,8 @@ static char *MOV="mov";
 static char *MOVB="mov.b";
 static char *MOVW="mov.w";
 static char *MOVC="movc";
+static char *MOVCB="movc.b";
+static char *MOVCW="movc.w";
 
 static char *R1L="r1l";
 static char *R1="r1";
@@ -139,10 +142,10 @@ static void emitcode (char *inst, char *fmt,...) {
 
 char *getStackOffset(int stack) {
   static char gsoBuf[1024];
-  sprintf (gsoBuf, "r7+(%d%+d%+d)", stack,
+  sprintf (gsoBuf, "r7+(%d%+d%+d%+d)", stack,
           FUNC_ISISR(currFunc->type) ? 
             port->stack.isr_overhead : port->stack.call_overhead,
-          _G.nRegsSaved);
+          currFunc->stack, _G.nRegsSaved);
   return gsoBuf;
 }
 
@@ -372,9 +375,9 @@ static asmop *aopForVal(operand *op) {
        break;
       case 4:
        sprintf (aop->name[0], "#0x%04lx",
+                SPEC_CVAL(operandType(op)).v_ulong & 0xffff);
+       sprintf (aop->name[1], "#0x%04lx", 
                 SPEC_CVAL(operandType(op)).v_ulong >> 16);
-       sprintf (aop->name[1], "#0x%04x", 
-                SPEC_CVAL(operandType(op)).v_ulong && 0xffff);
        break;
       default:
        bailOut("aopForVal");
@@ -443,7 +446,7 @@ bool aopIsPtr(operand *op) {
   }
 }
       
-char *opRegName(operand *op, int offset, char *opName) {
+char *opRegName(operand *op, int offset, char *opName, bool decorate) {
 
   if (IS_SYMOP(op)) {
     if (OP_SYMBOL(op)->onStack) {
@@ -468,17 +471,21 @@ char *opRegName(operand *op, int offset, char *opName) {
       }
       // fall through
     case V_CHAR:
-      sprintf (opName, "#0x%02x", SPEC_CVAL(OP_VALUE(op)->type).v_int);
+      sprintf (opName, "#%s0x%02x", decorate?"(char)":"", 
+              SPEC_CVAL(OP_VALUE(op)->type).v_int);
       break;
     case V_INT:
       if (SPEC_LONG(OP_VALUE(op)->type)) {
-       sprintf (opName, "#0x%02lx", SPEC_CVAL(OP_VALUE(op)->type).v_long);
+       sprintf (opName, "#%s0x%02lx", decorate?"(long)":"",
+                SPEC_CVAL(OP_VALUE(op)->type).v_long);
       } else {
-       sprintf (opName, "#0x%02x", SPEC_CVAL(OP_VALUE(op)->type).v_int);
+       sprintf (opName, "#%s0x%02x", decorate?"(int)":"",
+                SPEC_CVAL(OP_VALUE(op)->type).v_int);
       }
       break;
     case V_FLOAT:
-      sprintf (opName, "#%f", SPEC_CVAL(OP_VALUE(op)->type).v_float);
+      sprintf (opName, "#%s%f", decorate?"(float)":"",
+              SPEC_CVAL(OP_VALUE(op)->type).v_float);
       break;
     default: 
       bailOut("opRegName: unexpected noun");
@@ -550,7 +557,7 @@ char * printOp (operand *op) {
     strcat (line, "unknown");
     return line;
   } else if (IS_VALOP(op)) {
-    opRegName(op, 0, line);
+    opRegName(op, 0, line, 1);
   } else if (IS_TYPOP(op)) {
     sprintf (line, "[");
     if (isPtr) {
@@ -599,12 +606,16 @@ void printIc (char *op, iCode * ic, bool result, bool left, bool right) {
 /* toBoolean - return carry for operand!=0                           */
 /*-----------------------------------------------------------------*/
 static char *toBoolean (operand * op) {
+  symbol *tlbl=newiTempLabel(NULL);
+
   switch (AOP_SIZE(op)) 
     {
     case 1:
     case 2:
-      emitcode ("cmp", "%s,#0", AOP_NAME(op));
-      emitcode ("mov", "c,z");
+      emitcode ("cjne", "%s,#1,%05d$; %s", AOP_NAME(op), tlbl->key+100,
+               "This needs a second thought");
+      
+      emitcode ("", "%05d$:", tlbl->key+100);
       return "c";
     }
 
@@ -687,10 +698,43 @@ static void genUminus (iCode * ic) {
 }
 
 /*-----------------------------------------------------------------*/
-/* genIpush - genrate code for pushing this gets a little complex  */
+/* genIpush - generate code for pushing                            */
 /*-----------------------------------------------------------------*/
 static void genIpush (iCode * ic) {
+  operand *left=IC_LEFT(ic);
+
   printIc ("genIpush", ic, 0,1,0);
+  aopOp(left,FALSE,FALSE);
+  if (AOP_TYPE(left)==AOP_LIT) {
+    switch (AOP_SIZE(left)) 
+      {
+      case 1:
+       emitcode ("mov", "r1l,%s", AOP_NAME(left)[0]);
+       emitcode ("push", "r1l");
+       return;
+      case 2:
+       emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+       emitcode ("push", "r1");
+       return;
+      case 3:
+       emitcode ("mov", "r1l,%s", AOP_NAME(left)[1]);
+       emitcode ("push", "r1l");
+       emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+       emitcode ("push", "r1");
+       return;
+      case 4:
+       emitcode ("mov", "r1,%s", AOP_NAME(left)[1]);
+       emitcode ("push", "r1");
+       emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+       emitcode ("push", "r1");
+       return;
+      }
+  } else {
+    if (AOP_SIZE(left)>2) {
+      emitcode ("push", "%s", AOP_NAME(left)[1]);
+    }
+    emitcode ("push", "%s", AOP_NAME(left)[0]);
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -704,8 +748,37 @@ static void genIpop (iCode * ic) {
 /* genCall - generates a call statement                            */
 /*-----------------------------------------------------------------*/
 static void genCall (iCode * ic) {
-  emitcode (";", "genCall %s result=%s", OP_SYMBOL(IC_LEFT(ic))->name,
+  operand *result=IC_RESULT(ic);
+
+  emitcode (";", "genCall(%d) %s result=%s", ic->lineno,
+           OP_SYMBOL(IC_LEFT(ic))->name,
            printOp (IC_RESULT(ic)));
+  emitcode ("call", "%s", OP_SYMBOL(IC_LEFT(ic))->rname);
+
+  /* if we need to assign a result value */
+  if (IS_ITEMP (IC_RESULT(ic)) &&
+      OP_SYMBOL (IC_RESULT (ic))->nRegs) {
+    aopOp(result,FALSE,FALSE);
+    switch (AOP_SIZE(result))
+      {
+      case 1:
+       emitcode ("mov", "%s,r0l", AOP_NAME(result)[0]);
+       return;
+      case 2:
+       emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+       return;
+      case 3:
+       // generic pointer
+       emitcode ("mov", "%s,r1l", AOP_NAME(result)[1]);
+       emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+       return;
+      case 4:
+       emitcode ("mov", "%s,r1", AOP_NAME(result)[1]);
+       emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+       return;
+      }
+    bailOut("genCall");
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -724,10 +797,11 @@ static void genFunction (iCode * ic) {
   symbol *sym=OP_SYMBOL(IC_LEFT(ic));
   sym_link *type=sym->type;
 
-  emitcode (";", "-----------------------------------------");
-  emitcode (";", " function %s", sym->name);
-  emitcode (";", "-----------------------------------------");
-  
+  emitcode (";", "genFunction %s", sym->rname);
+
+  /* print the allocation information */
+  printAllocInfo (currFunc, codeOutFile);
+
   emitcode ("", "%s:", sym->rname);
 
   if (IFFUNC_ISNAKED(type))
@@ -736,10 +810,10 @@ static void genFunction (iCode * ic) {
       return;
   }
 
-  /* if critical function then turn interrupts off */
-  if (IFFUNC_ISCRITICAL (type))
-    emitcode ("clr", "ea");
-
+  /* adjust the stack for locals used in this function */
+  if (sym->stack) {
+    emitcode ("sub", "r7,#%d\t; create stack space for locals", sym->stack);
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -750,13 +824,23 @@ genEndFunction (iCode * ic)
 {
   symbol *sym = OP_SYMBOL (IC_LEFT (ic));
 
-  if (IFFUNC_ISNAKED(sym->type))
-  {
+  printIc ("genEndFunction", ic, 0,0,0);
+
+  if (IFFUNC_ISNAKED(sym->type)) {
       emitcode(";", "naked function: no epilogue.");
       return;
   }
 
-  printIc ("genEndFunction", ic, 0,0,0);
+  /* readjust the stock for locals used in this function */
+  if (sym->stack) {
+    emitcode ("add", "r7,#%d\t; release stack space for locals", sym->stack);
+  }
+
+  if (IFFUNC_ISISR(sym->type)) {
+    emitcode ("reti", "");
+  } else {
+    emitcode ("ret", "");
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -811,7 +895,57 @@ static void genGoto (iCode * ic) {
 /* genPlus - generates code for addition                           */
 /*-----------------------------------------------------------------*/
 static void genPlus (iCode * ic) {
+  operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
+  int size;
+  char *instr;
+
   printIc ("genPlus", 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 ("setb", 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");
+  }
+  
+  aopOp(left, !aopIsPtr(result), !aopIsDir(result));
+  aopOp(right, !aopIsPtr(result), !aopIsDir(result));
+
+  if (size>1) {
+    instr="add.w";
+  } else {
+    instr="add.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 ("addc.w", "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+    }
+  }
+  return;
 }
 
 /*-----------------------------------------------------------------*/
@@ -918,11 +1052,14 @@ static iCode *hasInc (operand *op, iCode *ic, int osize) {
 
   while (lic) {
     /* if operand of the form op = op + <sizeof *op> */
-    if (lic->op == '+' && isOperandEqual(IC_LEFT(lic),op) &&
-       isOperandEqual(IC_RESULT(lic),op) && 
-       isOperandLiteral(IC_RIGHT(lic)) &&
-       operandLitValue(IC_RIGHT(lic)) == isize) {
-      return lic;
+    if (lic->op == '+') {
+      if (isOperandEqual(IC_LEFT(lic),op) &&
+         //isOperandEqual(IC_RESULT(lic),op) && 
+         isOperandLiteral(IC_RIGHT(lic)) &&
+         operandLitValue(IC_RIGHT(lic)) == isize) {
+       emitcode (";", "Found hasInc");
+       return lic;
+      }
     }
     /* if the operand used or deffed */
     if (bitVectBitValue(OP_USES(op),lic->key) || (unsigned) lic->defKey == op->key) {
@@ -961,11 +1098,13 @@ static void genAnd (iCode * ic, iCode * ifx) {
 /*-----------------------------------------------------------------*/
 static void genOr (iCode * ic, iCode * ifx) {
   operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
-  int size=aopOp(result, TRUE, TRUE);
+  int size;
   char *instr;
 
   printIc ("genOr", 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;
@@ -1064,6 +1203,7 @@ static void genRightShift (iCode * ic) {
 static void genPointerGet (iCode * ic, iCode *pi) {
   char *instr, *scratchReg;
   operand *result=IC_RESULT(ic), *left=IC_LEFT(ic);
+  bool codePointer=IS_CODEPTR(operandType(left));
   int size;
 
   printIc ("genPointerGet", ic, 1,1,0);
@@ -1076,9 +1216,42 @@ static void genPointerGet (iCode * ic, iCode *pi) {
   size=aopOp(result,TRUE,aopIsDir(left));
 
   if (IS_GENPTR(operandType(left))) {
-    emitcode (";", "INLINE\t_gptrget ; %s %s = [%s %s]", 
-             AOP_NAME(result)[0], AOP_NAME(result)[1],
-             AOP_NAME(left)[0], AOP_NAME(left)[1]);
+    symbol *tlbl1=newiTempLabel(NULL);
+    symbol *tlbl2=newiTempLabel(NULL);
+    emitcode ("cmp", "%s,#0x%02x", AOP_NAME(left)[1], CPOINTER);
+    emitcode ("bne", "%05d$", tlbl1->key+100);
+    // far/near pointer
+    if (pi) {
+      emitcode ("mov", "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+      pi->generated=1;
+    } else {
+      emitcode ("mov", "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+    }
+    if (size>2) {
+      if (pi) {
+       emitcode ("mov", "%s,[%s+]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+      } else {
+       emitcode ("mov", "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+      }
+    }
+    emitcode ("br", "%05d$", tlbl2->key+100);
+    emitcode ("", "%05d$:", tlbl1->key+100);
+    // code pointer
+    if (pi) {
+      emitcode ("movc", "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+      pi->generated=1;
+    } else {
+      emitcode ("mov", "r0,%s", AOP_NAME(left)[0]);
+      emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[0]);
+    }
+    if (size>2) {
+      if (pi) {
+       emitcode ("movc", "%s,[%s+]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+      } else {
+       emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[1]);
+      }
+    }
+    emitcode ("", "%05d$:", tlbl2->key+100);
     return;
   }
 
@@ -1086,29 +1259,62 @@ static void genPointerGet (iCode * ic, iCode *pi) {
     {
     case AOP_REG:
       if (size>1) {
-       instr=MOVW;
+       if (codePointer) {
+         instr=MOVCW;
+       } else {
+         instr=MOVW;
+       }
        scratchReg=R1;
       } else {
-       instr=MOVB;
+       if (codePointer) {
+         instr=MOVCB;
+       } else {
+         instr=MOVB;
+       }
        scratchReg=R1L;
       }
       if (AOP_TYPE(result)==AOP_STK) {
        emitcode (MOV, "%s,[%s]", scratchReg, AOP_NAME(left)[0]);
        emitcode (MOV, "%s,%s", AOP_NAME(result)[0], scratchReg);
       } else {
-       emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+       if (pi) {
+         emitcode (instr, "%s,[%s+]", AOP_NAME(result)[0], 
+                   AOP_NAME(left)[0]);
+         pi->generated=1;
+       } else {
+         if (codePointer) {
+           emitcode (MOV, "r1,%s", AOP_NAME(left)[0]);
+           emitcode (instr, "%s,[r1+]", AOP_NAME(result)[0]);
+         } else {
+           emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], 
+                     AOP_NAME(left)[0]);
+         }
+       }
       }
       if (size > 2) {
        if (size==3) {
-         instr=MOVB;
+         if (codePointer) {
+           instr=MOVCB;
+         } else {
+           instr=MOVB;
+         }
          scratchReg=R1L;
        }
        if (AOP_TYPE(result)==AOP_STK) {
          emitcode (MOV, "%s,[%s+2]", scratchReg, AOP_NAME(left)[0]);
          emitcode (MOV, "%s,%s", AOP_NAME(result)[1], scratchReg);
        } else {
-         emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], 
-                   AOP_NAME(left)[0]);
+         if (pi) {
+           emitcode (instr, "%s,[%s+]", AOP_NAME(result)[1], 
+                     AOP_NAME(left)[0]);
+         } else {
+           if (codePointer) {
+             emitcode (instr, "%s,[r1]", AOP_NAME(result)[1]);
+           } else {
+             emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], 
+                       AOP_NAME(left)[0]);
+           }
+         }
        }
       }
       return;
@@ -1149,12 +1355,23 @@ static void genPointerSet (iCode * ic, iCode *pi) {
       } else {
        instr=MOVB;
       }
-      emitcode (instr, "[%s],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+      if (pi) {
+       emitcode (instr, "[%s+],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+       pi->generated=1;
+      } else {
+       emitcode (instr, "[%s],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+      }
       if (size > 2) {
        if (size==3) {
          instr=MOVB;
        }
-       emitcode (instr, "[%s+2],%s", AOP_NAME(result)[0], AOP_NAME(right)[1]);
+       if (pi) {
+         emitcode (instr, "[%s+],%s", AOP_NAME(result)[0], 
+                   AOP_NAME(right)[1]);
+       } else {
+         emitcode (instr, "[%s+2],%s", AOP_NAME(result)[0], 
+                   AOP_NAME(right)[1]);
+       }
       }
       return;
     }
@@ -1206,7 +1423,7 @@ static void genIfx (iCode * ic, iCode * popIc) {
       emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
       if (size > 2) {
        if (size==3) {
-         // generic pointer, just consider the pointer part
+         // generic pointer, forget the generic part
        } else {
          emitcode (instr, "%s,#0", AOP_NAME(cond)[1]);
          emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
@@ -1297,7 +1514,7 @@ static void genAssign (iCode * ic) {
       return;
     }
     /* we need to or */
-    emitcode ("mov", "%s,%s", AOP_NAME(result), toBoolean(right));
+    emitcode ("mov", "%s,%s; toBoolean", AOP_NAME(result), toBoolean(right));
     return;
   }
 
@@ -1398,10 +1615,6 @@ void genXA51Code (iCode * lic) {
   
   lineHead = lineCurr = NULL;
   
-  /* print the allocation information */
-  if (allocInfo)
-    printAllocInfo (currFunc, codeOutFile);
-
   /* if debug information required */
   if (options.debug && currFunc)
     {
@@ -1586,7 +1799,7 @@ void genXA51Code (iCode * lic) {
        break;
        
       case GET_VALUE_AT_ADDRESS:
-       genPointerGet (ic, hasInc(IC_LEFT(ic), ic, getSize(operandType(IC_LEFT(ic)))));
+       genPointerGet (ic, hasInc(IC_LEFT(ic), ic, getSize(operandType(IC_RESULT(ic)))));
        break;
        
       case '=':