*** empty log message ***
[fw/sdcc] / src / xa51 / gen.c
index b466d5dd544f402412cf65cf97c68b495dc4b415..7395180170b3b44a818178a9dd5b3e55c115767e 100755 (executable)
@@ -143,9 +143,7 @@ static void emitcode (char *inst, char *fmt,...) {
 
 char *getStackOffset(int stack) {
   static char gsoBuf[1024];
-  sprintf (gsoBuf, "r7+(%d%+d%+d%+d)", stack,
-          FUNC_ISISR(currFunc->type) ? 
-            port->stack.isr_overhead : port->stack.call_overhead,
+  sprintf (gsoBuf, "r7+(%d%+d%+d)", stack,
           currFunc->stack, _G.nRegsSaved);
   return gsoBuf;
 }
@@ -334,7 +332,7 @@ static asmop *aopForSym(symbol *sym,
        }
     } else {
       aop->type=AOP_FAR;
-      emitcode ("mov", "r0,#%s ; aopForSym:far", sym->rname);
+      emitcode ("mov.w", "r0,#%s ; aopForSym:far", sym->rname);
       sprintf (aop->name[0], "[r0]");
       if (size>2) {
        sprintf (aop->name[1], "[r0+2]");
@@ -560,7 +558,7 @@ char * printOp (operand *op) {
   } else if (IS_VALOP(op)) {
     opRegName(op, 0, line, 1);
   } else if (IS_TYPOP(op)) {
-    sprintf (line, "[");
+    sprintf (line, "(");
     if (isPtr) {
       if (DCL_TYPE(optype)==FPOINTER)
        strcat (line, "far * ");
@@ -577,7 +575,7 @@ char * printOp (operand *op) {
     if (SPEC_USIGN(operandType(op))) strcat (line, "unsigned ");
     if (SPEC_LONG(operandType(op))) strcat (line, "long ");
     strcat (line, nounName(operandType(op)));
-    strcat (line, "]");
+    strcat (line, ")");
   } else {
     bailOut("printOp: unexpected operand type");
   }
@@ -1223,7 +1221,11 @@ static void genPointerGet (iCode * ic, iCode *pi) {
   bool codePointer=IS_CODEPTR(operandType(left));
   int size;
 
-  printIc ("genPointerGet", ic, 1,1,0);
+  if (pi) {
+    printIc ("genPointerGet pi", ic, 1,1,0);
+  } else {
+    printIc ("genPointerGet", ic, 1,1,0);
+  }
 
   if (!IS_PTR(operandType(left))) {
     bailOut ("genPointerGet: pointer required");
@@ -1236,7 +1238,7 @@ static void genPointerGet (iCode * ic, iCode *pi) {
     symbol *tlbl1=newiTempLabel(NULL);
     symbol *tlbl2=newiTempLabel(NULL);
     emitcode ("cmp", "%s,#0x%02x", AOP_NAME(left)[1], CPOINTER);
-    emitcode ("bne", "%05d$", tlbl1->key+100);
+    emitcode ("beq", "%05d$", tlbl1->key+100);
     // far/near pointer
     if (pi) {
       emitcode ("mov", "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
@@ -1402,7 +1404,7 @@ static void genIfx (iCode * ic, iCode * popIc) {
   int size;
   char *instr;
   bool trueOrFalse;
-  symbol *jlbl, *tlbl;
+  symbol *jlbl, *tlbl=newiTempLabel(NULL);
   operand *cond=IC_COND(ic);
 
   emitcode (";", "genIfx(%d) cond=%s trueLabel:%s falseLabel:%s", 
@@ -1423,14 +1425,15 @@ static void genIfx (iCode * ic, iCode * popIc) {
   switch (AOP_TYPE(cond) )
     {
     case AOP_BIT:
-      emitcode (trueOrFalse ? "jb" : "jnb", "%s,%05d$", 
-               AOP_NAME(cond)[0], jlbl->key+100);
+      emitcode (trueOrFalse ? "jnb" : "jb", "%s,%05d$", 
+               AOP_NAME(cond)[0], tlbl->key+100);
+      emitcode ("jmp", "%05d$", jlbl->key+100);
+      emitcode ("", "%05d$:", tlbl->key+100);
       return;
     case AOP_REG:
     case AOP_DIR:
     case AOP_FAR:
     case AOP_STK:
-      tlbl=newiTempLabel(NULL);
       if (size>1) {
        instr="cmp.w";
       } else {
@@ -1564,9 +1567,156 @@ static void genJumpTab (iCode * ic) {
 /* genCast - gen code for casting                                  */
 /*-----------------------------------------------------------------*/
 static void genCast (iCode * ic) {
+  int size;
+  operand *result=IC_RESULT(ic);
+  operand *right=IC_RIGHT(ic);
+  sym_link *ctype=operandType(IC_LEFT(ic));
+  sym_link *rtype=operandType(IC_RIGHT(ic));
+  sym_link *etype=getSpec(rtype);
+  short ptrType, signedness;
+
   printIc ("genCast", ic, 1,1,1);
+
+  aopOp(result, TRUE, TRUE);
+  aopOp(right, !aopIsPtr(result), AOP_TYPE(result)!=AOP_DIR);
+  size=AOP_SIZE(result);
+  
+  /* if result is a bit */
+  if (AOP_TYPE(result) == AOP_BIT) {
+    /* if right is literal, we know what the value is */
+    if (AOP_TYPE(right) == AOP_LIT) {
+      if (operandLitValue(right)) {
+       emitcode ("setb", AOP_NAME(result)[0]);
+      } else {
+       emitcode ("clr", AOP_NAME(result)[0]);
+      }
+      return;
+    }
+    /* if right is also a bit */
+    if (AOP_TYPE(right) == AOP_BIT) {
+      emitcode ("mov", "c,%s", AOP_NAME(right));
+      emitcode ("mov", "%s,c", AOP_NAME(result));
+      return;
+    }
+    /* we need to or */
+    emitcode ("mov", "%s,%s; toBoolean", AOP_NAME(result), toBoolean(right));
+    return;
+  }
+
+  /* if right is a bit */
+  if (AOP_TYPE(right)==AOP_BIT) {
+    emitcode ("mov", "c,%s", AOP_NAME(right));
+    emitcode ("mov", "%s,#0", AOP_NAME(result)[0]);
+    emitcode ("rlc", "%s,#1", AOP_NAME(result)[0]);
+    if (size>2) {
+      emitcode ("mov", "%s,#0", AOP_NAME(result)[1]);
+    }
+    return;
+  }
+
+  /* if the result is of type pointer */
+  if (IS_PTR (ctype)) {
+
+    if (AOP_SIZE(right)>1) {
+      emitcode ("mov", "%s,%s",  AOP_NAME(result)[0], AOP_NAME(right)[0]);
+    } else {
+      emitcode ("mov", "r1l,%s", AOP_NAME(right)[0]);
+      emitcode ("sext", "r1h");
+      emitcode ("mov", "%s,r1",  AOP_NAME(result)[0]);
+    }
+    
+    /* if pointer to generic pointer */
+    if (IS_GENPTR (ctype)) {
+            
+      if (IS_GENPTR (rtype)) {
+       bailOut("genCast: gptr -> gptr");
+      }
+
+      if (IS_PTR (rtype)) {
+       ptrType = DCL_TYPE (rtype);
+      } else {
+       /* we have to go by the storage class */
+       if (!SPEC_OCLS(etype)) {
+         ptrType=0; // hush the compiler
+         bailOut("genCast: unknown storage class");
+       } else {
+         ptrType = PTR_TYPE (SPEC_OCLS (etype));
+       }
+      }
+      
+      /* the generic part depends on the type */
+      switch (ptrType)
+       {
+       case POINTER:
+         emitcode ("mov", "%s,#0x00", AOP_NAME(result)[1]);
+         break;
+       case FPOINTER:
+         emitcode ("mov", "%s,#0x01", AOP_NAME(result)[1]);
+         break;
+       case CPOINTER:
+         emitcode ("mov", "%s,#0x02", AOP_NAME(result)[1]);
+         break;
+       default:
+         bailOut("genCast: got unknown storage class");
+       }
+    }
+    return;
+  }
+
+  /* do we have to sign extend? */
+  signedness = SPEC_USIGN(rtype);
+
+  /* now depending on the size */
+  switch ((AOP_SIZE(result)<<4) + AOP_SIZE(right))
+    {
+    case 0x44:
+    case 0x33:
+      emitcode("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+      // fall through
+    case 0x24:
+    case 0x22:
+    case 0x11:
+      emitcode("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+      return;
+    case 0x42:
+      emitcode("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+      if (signedness) {
+       emitcode("sext", "%s", AOP_NAME(result)[1]);
+      } else {
+       emitcode("mov", "%s,#0", AOP_NAME(result)[1]);
+      }
+      return;
+    case 0x41:
+    case 0x21:
+      emitcode("mov", "r1l,%s", AOP_NAME(right)[0]);
+      if (signedness) {
+       emitcode("sext", "r1h");
+      } else {
+       emitcode("mov", "r1h,#0");
+      }
+      emitcode("mov", "%s,r1", AOP_NAME(result)[0]);
+      if (size==2)
+       return;
+      // fall through: case 0x41
+      if (signedness) {
+       emitcode("sext", "r1");
+      } else {
+       emitcode("mov", "r1,#0");
+      }
+      emitcode("mov", "%s,r1", AOP_NAME(result)[1]);
+      return;
+    case 0x14:
+    case 0x12:
+      emitcode("mov", "r1,%s", AOP_NAME(right)[0]);
+      emitcode("mov", "%s,r1l", AOP_NAME(result)[0]);
+      return;
+    }
+  fprintf(stderr, "genCast: unknown size: %d:%d\n",
+         AOP_SIZE(result), AOP_SIZE(right));
+  bailOut("genCast: unknown size");
 }
 
+
 /*-----------------------------------------------------------------*/
 /* genDjnz - generate decrement & jump if not zero instrucion      */
 /*-----------------------------------------------------------------*/