xa51, work in progress
[fw/sdcc] / src / xa51 / gen.c
index 1c4e3f882d345502620ff573a529ad9e1eca4faf..a2762237d495fca97a6c5b911a454e96b4ca474b 100755 (executable)
@@ -1,11 +1,3 @@
-/* The only ops for now are:
-   genAssign
-   genIfx
-   genAddrOf
-   genPointerSet
-   genPointerGet
-*/
-
 /*-------------------------------------------------------------------------
   SDCCgen51.c - source file for code generation for 8051
 
@@ -31,9 +23,6 @@
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!
 
-  Notes:
-  000123 mlh  Moved aopLiteral to SDCCglue.c to help the split
-      Made everything static
 -------------------------------------------------------------------------*/
 
 //#define D(x)
@@ -85,6 +74,7 @@ static struct
   {
     short inLine;
     short debugLine;
+    short stackExtend;
     short nRegsSaved;
     set *sendSet;
   }
@@ -102,10 +92,13 @@ static lineNode *lineCurr = NULL;
 #define MSB24   2
 #define MSB32   3
 
+static char *MOV="mov";
 static char *MOVB="mov.b";
 static char *MOVW="mov.w";
-static char *MOVCB="movc.b";
-static char *MOVCW="movc.w";
+static char *MOVC="movc";
+
+static char *R1L="r1l";
+static char *R1="r1";
 
 void bailOut (char *mesg) {
   fprintf (stderr, "%s: bailing out\n", mesg);
@@ -147,11 +140,10 @@ static void emitcode (char *inst, char *fmt,...) {
 
 char *getStackOffset(int stack) {
   static char gsoBuf[1024];
-  // dit slaat natuurlijk nergens op
-  sprintf (gsoBuf, "r7+(%+d+0%+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;
 }
 
@@ -176,9 +168,10 @@ char *aopTypeName(asmop *aop) {
     case AOP_DIR: return "dir";
     case AOP_FAR: return "far";
     case AOP_CODE: return "code";
+    case AOP_GPTR: return "gptr";
     case AOP_STK: return "stack";
     case AOP_IMMD: return "imm";
-    case AOP_CRY: return "bit";
+    case AOP_BIT: return "bit";
     }
   return "unknown";
 }
@@ -186,7 +179,8 @@ char *aopTypeName(asmop *aop) {
 /*-----------------------------------------------------------------*/
 /* aopForSym - for a true symbol                                   */
 /*-----------------------------------------------------------------*/
-static asmop *aopForSym(symbol *sym, bool result) {
+static asmop *aopForSym(symbol *sym, 
+                       bool canUsePointer, bool canUseOffset) {
   int size;
   asmop *aop;
 
@@ -197,7 +191,7 @@ static asmop *aopForSym(symbol *sym, bool result) {
   if (sym->nRegs && sym->regs[0]) {
     aop->type=AOP_REG;
     sprintf (aop->name[0], sym->regs[0]->name);
-    if (size>2) {
+    if (size > 2) {
       sprintf (aop->name[1], sym->regs[1]->name);
     }
     return aop;
@@ -205,6 +199,32 @@ static asmop *aopForSym(symbol *sym, bool result) {
 
   // if it is on stack
   if (sym->onStack) {
+    if (!canUsePointer || !canUseOffset) {
+      aop->type=AOP_REG;
+      switch (size) 
+       {
+       case 1:
+         emitcode ("mov.b", "r0l,[%s]", getStackOffset(sym->stack));
+         sprintf (aop->name[0], "r0l");
+         return aop;
+       case 2:
+         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         sprintf (aop->name[0], "r0");
+         return aop;
+       case 3:
+         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         sprintf (aop->name[0], "r0");
+         emitcode ("mov.b", "r1l,[%s]", getStackOffset(sym->stack+2));
+         sprintf (aop->name[1], "r1l");
+         return aop;
+       case 4:
+         emitcode ("mov.w", "r0,[%s]", getStackOffset(sym->stack));
+         sprintf (aop->name[0], "r0");
+         emitcode ("mov.w", "r1,[%s]", getStackOffset(sym->stack+2));
+         sprintf (aop->name[1], "r1");
+         return aop;
+       }
+    }
     aop->type=AOP_STK;
     sprintf (aop->name[0], "[%s]", getStackOffset(sym->stack));
     if (size > 2) {
@@ -215,12 +235,12 @@ static asmop *aopForSym(symbol *sym, bool result) {
 
   // if it has a spillLoc
   if (sym->usl.spillLoc) {
-    return aopForSym (sym->usl.spillLoc, result);
+    return aopForSym (sym->usl.spillLoc, canUsePointer, canUseOffset);
   }
 
   // if in bit space
   if (IN_BITSPACE(SPEC_OCLS(sym->etype))) {
-    aop->type=AOP_CRY;
+    aop->type=AOP_BIT;
     sprintf (aop->name[0], sym->rname);
     return aop;
   }
@@ -230,36 +250,96 @@ static asmop *aopForSym(symbol *sym, bool result) {
     aop->type=AOP_DIR;
     sprintf (aop->name[0], sym->rname);
     if (size>2) {
-      sprintf (aop->name[0], "%s+2", sym->rname);
+      sprintf (aop->name[1], "%s+2", sym->rname);
     }
     return aop;
   }
 
   // if in code space
   if (IN_CODESPACE(SPEC_OCLS(sym->etype))) {
-    if (result) {
-      bailOut("aopForSym: result can not be in code space");
-    }
-    aop->type=AOP_CODE;
-    emitcode ("mov", "r0,#%s", sym->rname);
-    sprintf (aop->name[0], "[r0]");
-    if (size>2) {
-      sprintf (aop->name[1], "[r0+1]");
+    if (!canUsePointer) {
+      aop->type=AOP_REG;
+      switch (size) 
+       {
+       case 1:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOVC, "r0l,[r0]");
+         sprintf (aop->name[0], "r0l");
+         return aop;
+       case 2:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOVC, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       case 3:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOVC, "r1l,[r0+2]");
+         sprintf (aop->name[1], "r1l");
+         emitcode (MOV, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       case 4:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOVC, "r1,[r0+2]");
+         sprintf (aop->name[1], "r1");
+         emitcode (MOVC, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       }
+      
+    } else {
+      aop->type=AOP_CODE;
+      emitcode ("mov", "r0,#%s ; aopForSym:code", sym->rname);
+      sprintf (aop->name[0], "[r0]");
+      if (size>2) {
+       sprintf (aop->name[1], "[r0+2]");
+      }
     }
     return aop;
   }
 
   // if in far space
   if (IN_FARSPACE(SPEC_OCLS(sym->etype))) {
-    aop->type=AOP_FAR;
-    emitcode ("mov", "r0,#%s", sym->rname);
-    sprintf (aop->name[0], "[r0]");
-    if (size>2) {
-      sprintf (aop->name[1], "[r0+1]");
+    if (!canUsePointer) {
+      aop->type=AOP_REG;
+      switch (size) 
+       {
+       case 1:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOV, "r0l,[r0]");
+         sprintf (aop->name[0], "r0l");
+         return aop;
+       case 2:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOV, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       case 3:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOV, "r1l,[r0+2]");
+         sprintf (aop->name[1], "r1l");
+         emitcode (MOV, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       case 4:
+         emitcode (MOV, "r0,#%s", sym->rname);
+         emitcode (MOV, "r1,[r0+2]");
+         sprintf (aop->name[1], "r1");
+         emitcode (MOV, "r0,[r0]");
+         sprintf (aop->name[0], "r0");
+         return aop;
+       }
+    } else {
+      aop->type=AOP_FAR;
+      emitcode ("mov", "r0,#%s ; aopForSym:far", sym->rname);
+      sprintf (aop->name[0], "[r0]");
+      if (size>2) {
+       sprintf (aop->name[1], "[r0+2]");
+      }
+      return aop;
     }
-    return aop;
   }
-
+  
   bailOut("aopForSym");
   return NULL;
 }
@@ -269,21 +349,33 @@ static asmop *aopForSym(symbol *sym, bool result) {
 /*-----------------------------------------------------------------*/
 static asmop *aopForVal(operand *op) {
   asmop *aop;
-  long v=(long long)floatFromVal(OP_VALUE(op));
 
   if (IS_OP_LITERAL(op)) {
     op->aop = aop = newAsmop (AOP_LIT);
     switch ((aop->size=getSize(operandType(op))))
       {
       case 1:
-       sprintf (aop->name[0], "#0x%02lx", v);
+       sprintf (aop->name[0], "#0x%02x", 
+                SPEC_CVAL(operandType(op)).v_int & 0xff);
+       sprintf (aop->name[1], "#0");
        break;
       case 2:
-       sprintf (aop->name[0], "#0x%04lx", v);
+       sprintf (aop->name[0], "#0x%04x", 
+                SPEC_CVAL(operandType(op)).v_int & 0xffff);
+       sprintf (aop->name[1], "#0");
+       break;
+      case 3:
+       // must be a generic pointer, can only be zero
+       // ?? if (v!=0) fprintf (stderr, "invalid val op for gptr\n"); exit(1);
+       sprintf (aop->name[0], "#0x%04x", 
+                SPEC_CVAL(operandType(op)).v_uint & 0xffff);
+       sprintf (aop->name[1], "#0");
        break;
       case 4:
-       sprintf (aop->name[0], "#(0x%08lx >> 16)", v);
-       sprintf (aop->name[1], "#(0x%08lx & 0xffff)", v);
+       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);
        break;
       default:
        bailOut("aopForVal");
@@ -306,27 +398,52 @@ static asmop *aopForVal(operand *op) {
        return aop;
       }
   }
+
   bailOut ("aopForVal: unknown type");
   return NULL;
 }
 
-static void aopOp(operand *op, bool result) {
+static int aopOp(operand *op, 
+                 bool canUsePointer, bool canUseOffset) {
 
   if (IS_SYMOP(op)) {
-    op->aop=aopForSym (OP_SYMBOL(op), result);
-    return;
+    op->aop=aopForSym (OP_SYMBOL(op), canUsePointer, canUseOffset);
+    return AOP_SIZE(op);
   }
   if (IS_VALOP(op)) {
-    if (result) {
-      bailOut("aopOp: result can not be a value");
-    }
-    aopForVal (op);
-    return;
+    op->aop=aopForVal (op);
+    return AOP_SIZE(op);
   }
 
   bailOut("aopOp: unexpected operand");
+  return 0;
+}
+
+bool aopEqual(asmop *aop1, asmop *aop2, int offset) {
+  if (strcmp(aop1->name[offset], aop2->name[offset])) {
+    return FALSE;
+  }
+  return TRUE;
+}
+
+bool aopIsDir(operand *op) {
+  return AOP_TYPE(op)==AOP_DIR;
+}
+
+bool aopIsBit(operand *op) {
+  return AOP_TYPE(op)==AOP_BIT;
 }
 
+bool aopIsPtr(operand *op) {
+  if (AOP_TYPE(op)==AOP_STK ||
+      AOP_TYPE(op)==AOP_CODE ||
+      AOP_TYPE(op)==AOP_FAR) {
+    return TRUE;
+  } else {
+    return FALSE;
+  }
+}
+      
 char *opRegName(operand *op, int offset, char *opName) {
 
   if (IS_SYMOP(op)) {
@@ -415,6 +532,10 @@ char * printOp (operand *op) {
       sprintf (line+strlen(line), "stack%+d", sym->stack);
       return line;
     }
+    if (IN_CODESPACE(SPEC_OCLS(sym->etype))) {
+      strcat (line, "code");
+      return line;
+    }
     if (IN_FARSPACE(SPEC_OCLS(sym->etype))) {
       strcat (line, "far");
       return line;
@@ -479,12 +600,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";
     }
 
@@ -604,10 +729,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))
@@ -616,10 +742,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);
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -630,13 +756,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", "");
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -646,7 +782,7 @@ static void genRet (iCode * ic) {
 
   printIc ("genRet", ic, 0,1,0);
 
-  aopOp(IC_LEFT(ic),FALSE);
+  aopOp(IC_LEFT(ic), TRUE, TRUE);
 
   switch (AOP_SIZE(IC_LEFT(ic)))
     {
@@ -691,7 +827,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;
 }
 
 /*-----------------------------------------------------------------*/
@@ -733,19 +919,19 @@ static void genCmpGt (iCode * ic) {
 /* genCmpGt :- greater than comparison                             */
 /*-----------------------------------------------------------------*/
 static void genCmpLe (iCode * ic) {
-  printIc ("genCmpGt", ic, 1,1,1);
+  printIc ("genCmpLe", ic, 1,1,1);
 }
 /*-----------------------------------------------------------------*/
 /* genCmpGt :- greater than comparison                             */
 /*-----------------------------------------------------------------*/
 static void genCmpGe (iCode * ic) {
-  printIc ("genCmpGt", ic, 1,1,1);
+  printIc ("genCmpGe", ic, 1,1,1);
 }
 /*-----------------------------------------------------------------*/
 /* genCmpGt :- greater than comparison                             */
 /*-----------------------------------------------------------------*/
 static void genCmpNe (iCode * ic) {
-  printIc ("genCmpGt", ic, 1,1,1);
+  printIc ("genCmpNe", ic, 1,1,1);
 }
 /*-----------------------------------------------------------------*/
 /* genCmpLt - less than comparisons                                */
@@ -840,7 +1026,52 @@ static void genAnd (iCode * ic, iCode * ifx) {
 /* genOr  - code for or                                            */
 /*-----------------------------------------------------------------*/
 static void genOr (iCode * ic, iCode * ifx) {
+  operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
+  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;
+    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;
+    }
+  }
+  
+  aopOp(left, !aopIsPtr(result), !aopIsDir(result));
+  aopOp(right, !aopIsPtr(result), !aopIsDir(result));
+
+  if (size>1) {
+    instr="or.w";
+  } else {
+    instr="or.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]);
+    }
+    emitcode (instr, "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+  }
+  return;
 }
 
 /*-----------------------------------------------------------------*/
@@ -854,7 +1085,10 @@ static void genXor (iCode * ic, iCode * ifx) {
 /* genInline - write the inline code out                           */
 /*-----------------------------------------------------------------*/
 static void genInline (iCode * ic) {
+
   printIc ("genInline", ic, 0,0,0);
+  
+  emitcode ("", IC_INLINE(ic));
 }
 
 /*-----------------------------------------------------------------*/
@@ -896,9 +1130,9 @@ static void genRightShift (iCode * ic) {
 /* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
 static void genPointerGet (iCode * ic, iCode *pi) {
-  char *instr;
-
+  char *instr, *scratchReg;
   operand *result=IC_RESULT(ic), *left=IC_LEFT(ic);
+  int size;
 
   printIc ("genPointerGet", ic, 1,1,0);
 
@@ -906,99 +1140,73 @@ static void genPointerGet (iCode * ic, iCode *pi) {
     bailOut ("genPointerGet: pointer required");
   }
 
-  aopOp(left,FALSE);
-  aopOp(result,TRUE);
+  aopOp(left,FALSE,FALSE);
+  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
+    emitcode ("mov", "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+    if (size>2) {
+      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
+    emitcode ("mov", "r0,%s", AOP_NAME(left)[0]);
+    emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[0]);
+    if (size>2) {
+      emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[1], AOP_NAME(left)[1]);
+    }
+    emitcode ("", "%05d$:", tlbl2->key+100);
     return;
   }
 
   switch (AOP_TYPE(left)) 
     {
-    case AOP_CODE:
-      if (AOP_SIZE(result)==1) {
-       instr=MOVCB;
+    case AOP_REG:
+      if (size>1) {
+       instr=MOVW;
+       scratchReg=R1;
       } else {
-       instr=MOVCW;
-      }
-      emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
-      if (AOP_SIZE(result) > 2) {
-       if (AOP_SIZE(result)==3) {
-         instr=MOVCB;
-       } else {
-         instr=MOVCW;
-       }
-       emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
-      }
-      return;
-    case AOP_FAR:
-      if (AOP_SIZE(result)==1) {
        instr=MOVB;
-      } else {
-       instr=MOVW;
-      }
-      emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
-      if (AOP_SIZE(result) > 2) {
-       if (AOP_SIZE(result)==3) {
-         instr=MOVB;
-       } else {
-         instr=MOVW;
-       }
-       emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
-      }
-      return;
-    case AOP_DIR:
-      emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
-      if (AOP_SIZE(result) > 2) {
-       emitcode (instr, "%s,%s+2", AOP_NAME(result)[1], AOP_NAME(left)[0]);
-      }
-      return;
-    case AOP_REG:
-      emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
-      if (AOP_SIZE(result) > 2) {
-       // result is generic pointer
-       sym_link *optype=operandType(left);
-       sym_link *opetype=getSpec(optype);
-       if (IS_PTR(optype) && !IS_GENPTR(optype)) {
-         emitcode ("mov.b", "%s,#0x%02x", AOP_NAME(result)[1], 
-                   PTR_TYPE(SPEC_OCLS(opetype)));
-       } else {
-         emitcode (instr, "%s,[%s]", AOP_NAME(result)[1], AOP_NAME(left)[1]);
-       }
+       scratchReg=R1L;
       }
-      return;
-    case AOP_STK:
-      // if both on stack
       if (AOP_TYPE(result)==AOP_STK) {
-       if (AOP_SIZE(result)==1) {
-         emitcode ("mov", "r0l,%s", AOP_NAME(left)[0]);
-         emitcode ("mov", "%s,r0l", AOP_NAME(result)[0]);
+       emitcode (MOV, "%s,[%s]", scratchReg, AOP_NAME(left)[0]);
+       emitcode (MOV, "%s,%s", AOP_NAME(result)[0], scratchReg);
+      } else {
+       if (pi) {
+         emitcode (instr, "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+         pi->generated=1;
        } else {
-         emitcode ("mov", "r0,%s", AOP_NAME(left)[0]);
-         emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+         emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
        }
-      } else {
-       emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
       }
-      if (AOP_SIZE(result) > 2) {
+      if (size > 2) {
+       if (size==3) {
+         instr=MOVB;
+         scratchReg=R1L;
+       }
        if (AOP_TYPE(result)==AOP_STK) {
-         if (AOP_SIZE(result)==3) {
-           emitcode ("mov", "r0l,%s", AOP_NAME(left)[1]);
-           emitcode ("mov", "%s,r0l", AOP_NAME(result)[1]);
+         emitcode (MOV, "%s,[%s+2]", scratchReg, AOP_NAME(left)[0]);
+         emitcode (MOV, "%s,%s", AOP_NAME(result)[1], scratchReg);
+       } else {
+         if (pi) {
+           emitcode (instr, "%s,[%s+]", AOP_NAME(result)[1], 
+                     AOP_NAME(left)[0]);
          } else {
-           emitcode ("mov", "r0,%s", AOP_NAME(left)[1]);
-           emitcode ("mov", "%s,r0", AOP_NAME(result)[1]);
+           emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], 
+                     AOP_NAME(left)[0]);
          }
-       } else {
-         emitcode (instr, "%s,%s", AOP_NAME(result)[1], AOP_NAME(left)[1]);
        }
       }
       return;
     }
-  bailOut ("genPointerGet: unknown pointer");
+  bailOut ("genPointerGet");
 }
 
 /*-----------------------------------------------------------------*/
@@ -1006,8 +1214,8 @@ static void genPointerGet (iCode * ic, iCode *pi) {
 /*-----------------------------------------------------------------*/
 static void genPointerSet (iCode * ic, iCode *pi) {
   char *instr;
-
   operand *result=IC_RESULT(ic), *right=IC_RIGHT(ic);
+  int size;
 
   printIc ("genPointerSet", ic, 1,0,1);
 
@@ -1015,45 +1223,53 @@ static void genPointerSet (iCode * ic, iCode *pi) {
     bailOut ("genPointerSet: pointer required");
   }
 
-  aopOp(right,FALSE);
-  aopOp(result,TRUE);
+  aopOp(result,FALSE,FALSE);
+  size=aopOp(right,FALSE, FALSE);
 
   if (IS_GENPTR(operandType(result))) {
-    emitcode (";", "INLINE _gptrset ; [%s %s]=  %s %s", 
+    emitcode (";", "INLINE\t_gptrset ; [%s %s] = %s %s", 
              AOP_NAME(result)[0], AOP_NAME(result)[1],
              AOP_NAME(right)[0], AOP_NAME(right)[1]);
     return;
   }
 
-  switch (AOP_TYPE(result)) 
+  switch (AOP_TYPE(right)) 
     {
+    case AOP_LIT:
     case AOP_REG:
-    case AOP_DIR:
-    case AOP_FAR:
-    case AOP_STK:
-      if (AOP_SIZE(result)==1) {
+      if (size>1) {
+       instr=MOVW;
+      } else {
        instr=MOVB;
+      }
+      if (pi) {
+       emitcode (instr, "[%s+],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+       pi->generated=1;
       } else {
-       instr=MOVW;
+       emitcode (instr, "[%s],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
       }
-      emitcode (instr, "[%s],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
-      if (AOP_SIZE(result) > 2) {
-       if (AOP_SIZE(result)==3) {
+      if (size > 2) {
+       if (size==3) {
          instr=MOVB;
+       }
+       if (pi) {
+         emitcode (instr, "[%s+],%s", AOP_NAME(result)[0], 
+                   AOP_NAME(right)[1]);
        } else {
-         instr=MOVW;
+         emitcode (instr, "[%s+2],%s", AOP_NAME(result)[0], 
+                   AOP_NAME(right)[1]);
        }
-       emitcode (instr, "[%s],%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
       }
       return;
     }
-  bailOut ("genPointerSet: unknown pointer");
+  bailOut ("genPointerSet");
 }
 
 /*-----------------------------------------------------------------*/
 /* genIfx - generate code for Ifx statement                        */
 /*-----------------------------------------------------------------*/
 static void genIfx (iCode * ic, iCode * popIc) {
+  int size;
   char *instr;
   bool trueOrFalse;
   symbol *jlbl, *tlbl;
@@ -1064,7 +1280,7 @@ static void genIfx (iCode * ic, iCode * popIc) {
            IC_TRUE(ic) ? IC_TRUE(ic)->name : "NULL",
            IC_FALSE(ic) ? IC_FALSE(ic)->name : "NULL");
 
-  aopOp(cond,FALSE);
+  size=aopOp(cond,TRUE,TRUE);
 
   if (IC_TRUE(ic)) {
     trueOrFalse=TRUE;
@@ -1076,7 +1292,7 @@ static void genIfx (iCode * ic, iCode * popIc) {
 
   switch (AOP_TYPE(cond) )
     {
-    case AOP_CRY:
+    case AOP_BIT:
       emitcode (trueOrFalse ? "jb" : "jbc", "%s,%05d$", 
                AOP_NAME(cond)[0], jlbl->key+100);
       return;
@@ -1085,21 +1301,20 @@ static void genIfx (iCode * ic, iCode * popIc) {
     case AOP_FAR:
     case AOP_STK:
       tlbl=newiTempLabel(NULL);
-      if (AOP_SIZE(cond)==1) {
-       instr="cmp.b";
-      } else {
+      if (size>1) {
        instr="cmp.w";
+      } else {
+       instr="cmp.b";
       }
       emitcode (instr, "%s,#0", AOP_NAME(cond)[0]);
       emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
-      if (AOP_SIZE(cond) > 2) {
-       if (AOP_SIZE(cond)==3) {
-         instr="cmp.b";
+      if (size > 2) {
+       if (size==3) {
+         // generic pointer, forget the generic part
        } else {
-         instr="cmp.w";
+         emitcode (instr, "%s,#0", AOP_NAME(cond)[1]);
+         emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
        }
-       emitcode (instr, "%s,#0", AOP_NAME(cond)[1]);
-       emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
       }
       emitcode ("jmp", "%05d$", jlbl->key+100);
       emitcode ("", "%05d$:", tlbl->key+100);
@@ -1112,18 +1327,19 @@ static void genIfx (iCode * ic, iCode * popIc) {
 /* genAddrOf - generates code for address of                       */
 /*-----------------------------------------------------------------*/
 static void genAddrOf (iCode * ic) {
+  int size;
   operand *left=IC_LEFT(ic);
 
   printIc ("genAddrOf", ic, 1,1,0);
 
-  aopOp (IC_RESULT(ic),TRUE);
+  size=aopOp (IC_RESULT(ic), FALSE, TRUE);
 
   if (isOperandOnStack(left)) {
     emitcode ("lea", "%s,%s", AOP_NAME(IC_RESULT(ic))[0],
              getStackOffset(OP_SYMBOL(left)->stack));
-    if (AOP_SIZE(IC_RESULT(ic)) > 2) {
+    if (size > 2) {
       // this must be a generic pointer
-      emitcode ("mov", "%s,#0x01", AOP_NAME(IC_RESULT(ic))[1]);
+      emitcode ("mov", "%s,#0x%02x", AOP_NAME(IC_RESULT(ic))[1], FPOINTER);
     }
     return;
   }
@@ -1131,9 +1347,9 @@ static void genAddrOf (iCode * ic) {
   if (isOperandInDirSpace(left) ||
       isOperandInFarSpace(left) ||
       isOperandInCodeSpace(left)) {
-    emitcode ("mov", "%s,#%s", AOP_NAME(IC_RESULT(ic))[0],
-             OP_SYMBOL(left));
-    if (AOP_SIZE(IC_RESULT(ic)) > 2) {
+    emitcode ("mov.w", "%s,#%s", AOP_NAME(IC_RESULT(ic))[0],
+             OP_SYMBOL(left)->rname);
+    if (size > 2) {
       // this must be a generic pointer
       int space=0; // dir space
       if (isOperandInFarSpace(left)) {
@@ -1141,7 +1357,7 @@ static void genAddrOf (iCode * ic) {
       } else if (isOperandInCodeSpace(left)) {
        space=2;
       }
-      emitcode ("mov", "%s,#0x%02x", AOP_NAME(IC_RESULT(ic))[1], space);
+      emitcode ("mov.b", "%s,#0x%02x", AOP_NAME(IC_RESULT(ic))[1], space);
     }
     return;
   }
@@ -1163,27 +1379,12 @@ static void genAssign (iCode * ic) {
     bailOut("genAssign: result is not a symbol");
   }
   
-  aopOp(right, FALSE);
-  aopOp(result, TRUE);
+  aopOp(result, TRUE, TRUE);
+  aopOp(right, !aopIsPtr(result), AOP_TYPE(result)!=AOP_DIR);
   size=AOP_SIZE(result);
 
-  if (result->aop->type==AOP_REG || 
-      right->aop->type==AOP_REG ||
-      right->aop->type==AOP_LIT ||
-      right->aop->type==AOP_STK ||
-      right->aop->type==AOP_IMMD) {
-    // everything will do
-  } else {
-    // they have to match
-    if (result->aop->type != right->aop->type) {
-      fprintf (stderr, "genAssign: types don't match (%s!=%s)\n",
-              aopTypeName(result->aop), aopTypeName(right->aop));
-      exit (1);
-    }
-  }
-
   /* if result is a bit */
-  if (AOP_TYPE(result) == AOP_CRY) {
+  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)) {
@@ -1194,34 +1395,32 @@ static void genAssign (iCode * ic) {
       return;
     }
     /* if right is also a bit */
-    if (AOP_TYPE(right) == AOP_CRY) {
+    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", AOP_NAME(result), toBoolean(right));
+    emitcode ("mov", "%s,%s; toBoolean", AOP_NAME(result), toBoolean(right));
     return;
   }
 
   /* general case */
-  if (size==1) {
-    instr=MOVB;
-  } else {
+  if (size>1) {
     instr=MOVW;
+  } else {
+    instr=MOVB;
   }
-  emitcode (instr, "%s,%s",
-           result->aop->name[0], right->aop->name[0]);
-  if (AOP_SIZE(result) > 2) {
+  emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+
+  if (size > 2) {
     if (size==3) {
+      // generic pointer
       instr=MOVB;
-    } else {
-      instr=MOVW;
     }
-    emitcode (instr, "%s,%s",
-             result->aop->name[1], right->aop->name[1]);
+    emitcode (instr, "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+    return;
   }
-  
 }
 
 /*-----------------------------------------------------------------*/
@@ -1272,18 +1471,19 @@ static bool genDjnz (iCode * ic, iCode * ifx) {
   lbl = newiTempLabel (NULL);
   lbl1 = newiTempLabel (NULL);
 
-  aopOp (IC_RESULT (ic), TRUE);
+  aopOp (IC_RESULT (ic), FALSE, TRUE);
 
   if (AOP_TYPE(IC_RESULT(ic))==AOP_REG || AOP_TYPE(IC_RESULT(ic))==AOP_DIR) {
     emitcode ("djnz", "%s,%05d$", AOP_NAME(IC_RESULT(ic)), lbl->key+100);
-    emitcode ("bra", "%05d$", lbl1->key + 100);
+    emitcode ("br", "%05d$", lbl1->key + 100);
     emitcode ("", "%05d$:", lbl->key + 100);
     emitcode ("jmp", "%05d$", IC_TRUE (ifx)->key + 100);
     emitcode ("", "%05d$:", lbl1->key + 100);
     return TRUE;
   }
-    bailOut("genDjnz: aop type");
-    return FALSE;
+
+  bailOut("genDjnz: aop type");
+  return FALSE;
 }
 
 /*-----------------------------------------------------------------*/
@@ -1300,13 +1500,8 @@ void genXA51Code (iCode * lic) {
   iCode *ic;
   int cln = 0;
   
-  fprintf (stderr, "genXA51Code\n");
   lineHead = lineCurr = NULL;
   
-  /* print the allocation information */
-  if (allocInfo)
-    printAllocInfo (currFunc, codeOutFile);
-
   /* if debug information required */
   if (options.debug && currFunc)
     {
@@ -1320,7 +1515,7 @@ void genXA51Code (iCode * lic) {
     }
   
   for (ic = lic; ic; ic = ic->next) {
-    if (cln != ic->lineno) {
+    if (ic->lineno && cln != ic->lineno) {
       if (options.debug) {
        _G.debugLine = 1;
        emitcode ("", "C$%s$%d$%d$%d ==.",