Removed some more ds390 leftovers in mcs51
[fw/sdcc] / src / mcs51 / gen.c
index 0b99a0242956dc098f62d09111bfa06f5a983acf..446618b4059582a0673ad1e908b438f5fcb2a230 100644 (file)
@@ -233,7 +233,6 @@ endOfWorld:
       return NULL;
     }
 
-  piCode (ic, stdout);
   /* other wise this is true end of the world */
   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
          "getFreePtr should never reach here");
@@ -253,21 +252,6 @@ newAsmop (short type)
   return aop;
 }
 
-static void
-genSetDPTR (int n)
-{
-  if (!n)
-    {
-      emitcode (";", "Select standard DPTR");
-      emitcode ("mov", "dps, #0x00");
-    }
-  else
-    {
-      emitcode (";", "Select alternate DPTR");
-      emitcode ("mov", "dps, #0x01");
-    }
-}
-
 /*-----------------------------------------------------------------*/
 /* pointerCode - returns the code for a pointer type               */
 /*-----------------------------------------------------------------*/
@@ -386,9 +370,9 @@ aopForRemat (symbol * sym)
   for (;;)
     {
       if (ic->op == '+')
-       val += operandLitValue (IC_RIGHT (ic));
+       val += (int) operandLitValue (IC_RIGHT (ic));
       else if (ic->op == '-')
-       val -= operandLitValue (IC_RIGHT (ic));
+       val -= (int) operandLitValue (IC_RIGHT (ic));
       else
        break;
 
@@ -604,7 +588,7 @@ aopOp (operand * op, iCode * ic, bool result)
 
       if (sym->ruonly)
        {
-         int i;
+         unsigned i;
          aop = op->aop = sym->aop = newAsmop (AOP_STR);
          aop->size = getSize (sym->type);
          for (i = 0; i < fReturnSizeMCS51; i++)
@@ -780,13 +764,6 @@ aopGet (asmop * aop, int offset, bool bit16, bool dname)
       return rs;
 
     case AOP_DPTR:
-    case AOP_DPTR2:
-
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (1);
-       }
-
       while (offset > aop->coff)
        {
          emitcode ("inc", "dptr");
@@ -809,12 +786,6 @@ aopGet (asmop * aop, int offset, bool bit16, bool dname)
        {
          emitcode ("movx", "a,@dptr");
        }
-
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (0);
-       }
-
       return (dname ? "acc" : "a");
 
 
@@ -884,7 +855,6 @@ static void
 aopPut (asmop * aop, char *s, int offset)
 {
   char *d = buffer;
-  symbol *lbl;
 
   if (aop->size && offset > (aop->size - 1))
     {
@@ -931,13 +901,6 @@ aopPut (asmop * aop, char *s, int offset)
       break;
 
     case AOP_DPTR:
-    case AOP_DPTR2:
-
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (1);
-       }
-
       if (aop->code)
        {
          werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
@@ -963,11 +926,6 @@ aopPut (asmop * aop, char *s, int offset)
       MOVA (s);
 
       emitcode ("movx", "@dptr,a");
-
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (0);
-       }
       break;
 
     case AOP_R0:
@@ -1039,17 +997,18 @@ aopPut (asmop * aop, char *s, int offset)
            emitcode ("mov", "%s,c", aop->aopu.aop_dir);
          else
            {
-             lbl = newiTempLabel (NULL);
-
              if (strcmp (s, "a"))
                {
                  MOVA (s);
                }
-             emitcode ("clr", "c");
-             emitcode ("jz", "%05d$", lbl->key + 100);
-             emitcode ("cpl", "c");
-             emitcode ("", "%05d$:", lbl->key + 100);
-             emitcode ("mov", "%s,c", aop->aopu.aop_dir);
+             {
+               symbol *lbl = newiTempLabel (NULL);
+               emitcode ("clr", "c");
+               emitcode ("jz", "%05d$", lbl->key + 100);
+               emitcode ("cpl", "c");
+               emitcode ("", "%05d$:", lbl->key + 100);
+               emitcode ("mov", "%s,c", aop->aopu.aop_dir);
+             }
            }
        }
       break;
@@ -1112,38 +1071,24 @@ pointToEnd (asmop * aop)
 static void
 reAdjustPreg (asmop * aop)
 {
-  int size;
-
-  aop->coff = 0;
-  if ((size = aop->size) <= 1)
+  if ((aop->coff==0) || aop->size <= 1)
     return;
-  size--;
+
   switch (aop->type)
     {
     case AOP_R0:
     case AOP_R1:
-      while (size--)
+      while (aop->coff--)
        emitcode ("dec", "%s", aop->aopu.aop_ptr->name);
       break;
     case AOP_DPTR:
-    case AOP_DPTR2:
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (1);
-       }
-      while (size--)
+      while (aop->coff--)
        {
          emitcode ("lcall", "__decdptr");
        }
-
-      if (aop->type == AOP_DPTR2)
-       {
-         genSetDPTR (0);
-       }
       break;
-
     }
-
+  aop->coff = 0;
 }
 
 #define AOP(op) op->aop
@@ -1153,8 +1098,7 @@ reAdjustPreg (asmop * aop)
                        AOP_TYPE(x) == AOP_R0))
 
 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY ||  \
-                        AOP_TYPE(x) == AOP_DPTR || AOP_TYPE(x) == AOP_DPTR2 || \
-                         AOP(x)->paged))
+                        AOP_TYPE(x) == AOP_DPTR || AOP(x)->paged))
 
 #define AOP_INPREG(x) (x && (x->type == AOP_REG &&                        \
                       (x->aopu.aop_reg[0] == mcs51_regWithIdx(R0_IDX) || \
@@ -1185,7 +1129,6 @@ genNotFloat (operand * op, operand * res)
                aopGet (op->aop,
                        offset++, FALSE, FALSE));
     }
-  tlbl = newiTempLabel (NULL);
 
   tlbl = newiTempLabel (NULL);
   aopPut (res->aop, one, 1);
@@ -2571,7 +2514,7 @@ genPlusIncr (iCode * ic)
 
   /* if the literal value of the right hand side
      is greater than 4 then it is not worth it */
-  if ((icount = floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit)) > 4)
+  if ((icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit)) > 4)
     return FALSE;
 
   /* if increment 16 bits in register */
@@ -3152,7 +3095,7 @@ genMultbits (operand * left,
 
 
 /*-----------------------------------------------------------------*/
-/* genMultOneByte : 8 bit multiplication & division                */
+/* genMultOneByte : 8*8=8/16 bit multiplication                    */
 /*-----------------------------------------------------------------*/
 static void
 genMultOneByte (operand * left,
@@ -3160,89 +3103,99 @@ genMultOneByte (operand * left,
                operand * result)
 {
   sym_link *opetype = operandType (result);
-  char *l;
   symbol *lbl;
-  int size, offset;
+  int size=AOP_SIZE(result);
+
+  //emitcode (";",__FUNCTION__);
+  if (size<1 || size>2) {
+    // this should never happen
+      fprintf (stderr, "size!=1||2 (%d) in %s at line:%d \n", 
+              AOP_SIZE(result), __FUNCTION__, lineno);
+      exit (1);
+  }
 
-  /* (if two literals, the value is computed before) */
+  /* (if two literals: the value is computed before) */
   /* if one literal, literal on the right */
   if (AOP_TYPE (left) == AOP_LIT)
     {
       operand *t = right;
       right = left;
       left = t;
+      //emitcode (";", "swapped left and right");
+    }
+
+  if (SPEC_USIGN(opetype)
+      // ignore the sign of left and right, what else can we do?
+      || (SPEC_USIGN(operandType(left)) && 
+         SPEC_USIGN(operandType(right)))) {
+    // just an unsigned 8*8=8/16 multiply
+    //emitcode (";","unsigned");
+    emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
+    MOVA (aopGet (AOP (left), 0, FALSE, FALSE));
+    emitcode ("mul", "ab");
+    aopPut (AOP (result), "a", 0);
+    if (size==2) {
+      aopPut (AOP (result), "b", 1);
     }
+    return;
+  }
 
-  size = AOP_SIZE (result);
-  /* signed or unsigned */
-  emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
-  l = aopGet (AOP (left), 0, FALSE, FALSE);
-  MOVA (l);
-  emitcode ("mul", "ab");
-  /* if result size = 1, mul signed = mul unsigned */
-  aopPut (AOP (result), "a", 0);
-  if (size > 1)
-    {
-      if (SPEC_USIGN (opetype))
-       {
-         aopPut (AOP (result), "b", 1);
-         if (size > 2)
-           /* for filling the MSBs */
-           emitcode ("clr", "a");
-       }
-      else
-       {
-         emitcode ("mov", "a,b");
+  // we have to do a signed multiply
 
-         /* adjust the MSB if left or right neg */
+  //emitcode (";", "signed");
+  emitcode ("clr", "F0"); // reset sign flag
+  emitcode ("mov", "b,%s", aopGet (AOP (right), 0, FALSE, FALSE));
+  MOVA (aopGet (AOP (left), 0, FALSE, FALSE));
 
-         /* if one literal */
-         if (AOP_TYPE (right) == AOP_LIT)
-           {
-             /* AND literal negative */
-             if ((int) floatFromVal (AOP (right)->aopu.aop_lit) < 0)
-               {
-                 /* adjust MSB (c==0 after mul) */
-                 emitcode ("subb", "a,%s", aopGet (AOP (left), 0, FALSE, FALSE));
-               }
-           }
-         else
-           {
-             lbl = newiTempLabel (NULL);
-             emitcode ("xch", "a,%s", aopGet (AOP (right), 0, FALSE, FALSE));
-             emitcode ("cjne", "a,#0x80,%05d$", (lbl->key + 100));
-             emitcode ("", "%05d$:", (lbl->key + 100));
-             emitcode ("xch", "a,%s", aopGet (AOP (right), 0, FALSE, FALSE));
-             lbl = newiTempLabel (NULL);
-             emitcode ("jc", "%05d$", (lbl->key + 100));
-             emitcode ("subb", "a,%s", aopGet (AOP (left), 0, FALSE, FALSE));
-             emitcode ("", "%05d$:", (lbl->key + 100));
-           }
+  lbl=newiTempLabel(NULL);
+  emitcode ("jnb", "acc.7,%05d$",  lbl->key+100);
+  // left side is negative, 8-bit two's complement, this fails for -128
+  emitcode ("setb", "F0"); // set sign flag
+  emitcode ("cpl", "a");
+  emitcode ("inc", "a");
 
-         lbl = newiTempLabel (NULL);
-         emitcode ("xch", "a,%s", aopGet (AOP (left), 0, FALSE, FALSE));
-         emitcode ("cjne", "a,#0x80,%05d$", (lbl->key + 100));
-         emitcode ("", "%05d$:", (lbl->key + 100));
-         emitcode ("xch", "a,%s", aopGet (AOP (left), 0, FALSE, FALSE));
-         lbl = newiTempLabel (NULL);
-         emitcode ("jc", "%05d$", (lbl->key + 100));
-         emitcode ("subb", "a,%s", aopGet (AOP (right), 0, FALSE, FALSE));
-         emitcode ("", "%05d$:", (lbl->key + 100));
+  emitcode ("", "%05d$:", lbl->key+100);
+  emitcode ("xch", "a,b");
 
-         aopPut (AOP (result), "a", 1);
-         if (size > 2)
-           {
-             /* get the sign */
-             emitcode ("rlc", "a");
-             emitcode ("subb", "a,acc");
-           }
-       }
-      size -= 2;
-      offset = 2;
-      if (size > 0)
-       while (size--)
-         aopPut (AOP (result), "a", offset++);
+  /* if literal */
+  if (AOP_TYPE(right)==AOP_LIT) {
+    /* AND literal negative */
+    if ((int) floatFromVal (AOP (right)->aopu.aop_lit) < 0) {
+      // two's complement for literal<0
+      emitcode ("xrl", "PSW,#0x20"); // xrl sign flag
+      emitcode ("cpl", "a");
+      emitcode ("inc", "a");
     }
+  } else {
+    lbl=newiTempLabel(NULL);
+    emitcode ("jnb", "acc.7,%05d$", lbl->key+100);
+    // right side is negative, 8-bit two's complement
+    emitcode ("xrl", "PSW,#0x20"); // xrl sign flag
+    emitcode ("cpl", "a");
+    emitcode ("inc", "a");
+    emitcode ("", "%05d$:", lbl->key+100);
+  }
+  emitcode ("mul", "ab");
+    
+  lbl=newiTempLabel(NULL);
+  emitcode ("jnb", "F0,%05d$", lbl->key+100);
+  // only ONE op was negative, we have to do a 8/16-bit two's complement
+  emitcode ("cpl", "a"); // lsb
+  if (size==1) {
+    emitcode ("inc", "a");
+  } else {
+    emitcode ("add", "a,#1");
+    emitcode ("xch", "a,b");
+    emitcode ("cpl", "a"); // msb
+    emitcode ("addc", "a,#0");
+    emitcode ("xch", "a,b");
+  }
+
+  emitcode ("", "%05d$:", lbl->key+100);
+  aopPut (AOP (result), "a", 0);
+  if (size==2) {
+    aopPut (AOP (result), "b", 1);
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -3614,7 +3567,7 @@ genIfxJump (iCode * ic, char *jval)
 /*-----------------------------------------------------------------*/
 static void
 genCmp (operand * left, operand * right,
-       operand * result, iCode * ifx, int sign)
+       operand * result, iCode * ifx, int sign, iCode *ic)
 {
   int size, offset = 0;
   unsigned long lit = 0L;
@@ -3698,6 +3651,8 @@ genCmp (operand * left, operand * right,
     }
 
 release:
+  freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
+  freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   if (AOP_TYPE (result) == AOP_CRY && AOP_SIZE (result))
     {
       outBitC (result);
@@ -3737,10 +3692,8 @@ genCmpGt (iCode * ic, iCode * ifx)
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
 
-  genCmp (right, left, result, ifx, sign);
+  genCmp (right, left, result, ifx, sign,ic);
 
-  freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -3767,10 +3720,8 @@ genCmpLt (iCode * ic, iCode * ifx)
   aopOp (right, ic, FALSE);
   aopOp (result, ic, TRUE);
 
-  genCmp (left, right, result, ifx, sign);
+  genCmp (left, right, result, ifx, sign,ic);
 
-  freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
-  freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -4049,6 +4000,37 @@ ifxForOp (operand * op, iCode * ic)
 
   return NULL;
 }
+
+/*-----------------------------------------------------------------*/
+/* hasInc - operand is incremented before any other use            */
+/*-----------------------------------------------------------------*/
+static iCode *
+hasInc (operand *op, iCode *ic)
+{
+  sym_link *type = operandType(op);
+  sym_link *retype = getSpec (type);
+  iCode *lic = ic->next;
+  int isize ;
+  
+  if (IS_BITVAR(retype)||!IS_PTR(type)) return NULL;
+  isize = getSize(type->next);
+  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 the operand used or deffed */
+    if (bitVectBitValue(ic->uses,op->key) || (unsigned) ic->defKey == op->key) {
+      return NULL;
+    }
+    lic = lic->next;
+  }
+  return NULL;
+}
+
 /*-----------------------------------------------------------------*/
 /* genAndOp - for && operation                                     */
 /*-----------------------------------------------------------------*/
@@ -4207,6 +4189,7 @@ genAnd (iCode * ic, iCode * ifx)
   int bytelit = 0;
   char buffer[10];
 
+  catchMe();
   aopOp ((left = IC_LEFT (ic)), ic, FALSE);
   aopOp ((right = IC_RIGHT (ic)), ic, FALSE);
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
@@ -5436,18 +5419,25 @@ AccAXLsh (char *x, int shCount)
 
       break;
     case 6:                    // AAAAAABB:CCCCCCDD
-
       emitcode ("anl", "a,#0x%02x",
                SRMask[shCount]);       // 000000BB:CCCCCCDD
-
       emitcode ("mov", "c,acc.0");     // c = B
-
       emitcode ("xch", "a,%s", x);     // CCCCCCDD:000000BB
-
+#if 0 // REMOVE ME
       AccAXRrl1 (x);           // BCCCCCCD:D000000B
-
       AccAXRrl1 (x);           // BBCCCCCC:DD000000
-
+#else
+      emitcode("rrc","a"); 
+      emitcode("xch","a,%s", x); 
+      emitcode("rrc","a"); 
+      emitcode("mov","c,acc.0"); //<< get correct bit 
+      emitcode("xch","a,%s", x); 
+
+      emitcode("rrc","a"); 
+      emitcode("xch","a,%s", x); 
+      emitcode("rrc","a"); 
+      emitcode("xch","a,%s", x); 
+#endif
       break;
     case 7:                    // a:x <<= 7
 
@@ -5867,7 +5857,7 @@ genlshFour (operand * result, operand * left, int shCount)
        movLeft2Result (left, LSB, result, MSB32, 0);
       aopPut (AOP (result), zero, LSB);
       aopPut (AOP (result), zero, MSB16);
-      aopPut (AOP (result), zero, MSB32);
+      aopPut (AOP (result), zero, MSB24);
       return;
     }
 
@@ -6683,7 +6673,8 @@ genDataPointerGet (operand * left,
 static void
 genNearPointerGet (operand * left,
                   operand * result,
-                  iCode * ic)
+                  iCode * ic,
+                  iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -6724,7 +6715,6 @@ genNearPointerGet (operand * left,
   else
     rname = aopGet (AOP (left), 0, FALSE, FALSE);
 
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -6750,15 +6740,17 @@ genNearPointerGet (operand * left,
              aopPut (AOP (result), buffer, offset);
            }
          offset++;
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop)       /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) { /* post increment present */
+       aopPut(AOP ( left ),rname,0);
+      }
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -6768,10 +6760,11 @@ genNearPointerGet (operand * left,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (result) > 1 &&
-         !OP_SYMBOL (left)->remat &&
-         (OP_SYMBOL (left)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (result) > 1 &&
+          !OP_SYMBOL (left)->remat &&
+          (OP_SYMBOL (left)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (result) - 1;
          while (size--)
@@ -6780,8 +6773,9 @@ genNearPointerGet (operand * left,
     }
 
   /* done */
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  if (pi) pi->generated = 1;
 }
 
 /*-----------------------------------------------------------------*/
@@ -6790,7 +6784,8 @@ genNearPointerGet (operand * left,
 static void
 genPagedPointerGet (operand * left,
                    operand * result,
-                   iCode * ic)
+                   iCode * ic,
+                   iCode *pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -6817,7 +6812,6 @@ genPagedPointerGet (operand * left,
   else
     rname = aopGet (AOP (left), 0, FALSE, FALSE);
 
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -6837,15 +6831,15 @@ genPagedPointerGet (operand * left,
 
          offset++;
 
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut ( AOP (left), rname, 0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -6855,10 +6849,11 @@ genPagedPointerGet (operand * left,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (result) > 1 &&
-         !OP_SYMBOL (left)->remat &&
-         (OP_SYMBOL (left)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (result) > 1 &&
+          !OP_SYMBOL (left)->remat &&
+          (OP_SYMBOL (left)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (result) - 1;
          while (size--)
@@ -6867,8 +6862,9 @@ genPagedPointerGet (operand * left,
     }
 
   /* done */
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
-
+  if (pi) pi->generated = 1;
 
 }
 
@@ -6877,7 +6873,7 @@ genPagedPointerGet (operand * left,
 /*-----------------------------------------------------------------*/
 static void
 genFarPointerGet (operand * left,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -6898,7 +6894,6 @@ genFarPointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -6913,20 +6908,26 @@ genFarPointerGet (operand * left,
        {
          emitcode ("movx", "a,@dptr");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
-
+  
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
-/* emitcodePointerGet - gget value from code space                  */
+/* genCodePointerGet - gget value from code space                  */
 /*-----------------------------------------------------------------*/
 static void
-emitcodePointerGet (operand * left,
-                   operand * result, iCode * ic)
+genCodePointerGet (operand * left,
+                   operand * result, iCode * ic, iCode *pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -6947,7 +6948,6 @@ emitcodePointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -6963,11 +6963,17 @@ emitcodePointerGet (operand * left,
          emitcode ("clr", "a");
          emitcode ("movc", "a,@a+dptr");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -6976,7 +6982,7 @@ emitcodePointerGet (operand * left,
 /*-----------------------------------------------------------------*/
 static void
 genGenPointerGet (operand * left,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode *pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
@@ -7001,7 +7007,6 @@ genGenPointerGet (operand * left,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (left, NULL, ic, TRUE);
   aopOp (result, ic, FALSE);
 
   /* if bit then unpack */
@@ -7016,11 +7021,17 @@ genGenPointerGet (operand * left,
        {
          emitcode ("lcall", "__gptrget");
          aopPut (AOP (result), "a", offset++);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR) {
+    aopPut ( AOP (left), "dpl", 0);
+    aopPut ( AOP (left), "dph", 1);
+    pi->generated = 1;
+  }
+  freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
 
@@ -7028,7 +7039,7 @@ genGenPointerGet (operand * left,
 /* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
 static void
-genPointerGet (iCode * ic)
+genPointerGet (iCode * ic, iCode *pi)
 {
   operand *left, *result;
   sym_link *type, *etype;
@@ -7048,21 +7059,6 @@ genPointerGet (iCode * ic)
     {
       /* we have to go by the storage class */
       p_type = PTR_TYPE (SPEC_OCLS (etype));
-
-/*  if (SPEC_OCLS(etype)->codesp ) { */
-/*      p_type = CPOINTER ;  */
-/*  } */
-/*  else */
-/*      if (SPEC_OCLS(etype)->fmap && !SPEC_OCLS(etype)->paged) */
-/*    p_type = FPOINTER ; */
-/*      else */
-/*    if (SPEC_OCLS(etype)->fmap && SPEC_OCLS(etype)->paged) */
-/*        p_type = PPOINTER; */
-/*    else */
-/*        if (SPEC_OCLS(etype) == idata ) */
-/*      p_type = IPOINTER; */
-/*        else */
-/*      p_type = POINTER ; */
     }
 
   /* now that we have the pointer type we assign
@@ -7072,23 +7068,23 @@ genPointerGet (iCode * ic)
 
     case POINTER:
     case IPOINTER:
-      genNearPointerGet (left, result, ic);
+      genNearPointerGet (left, result, ic, pi);
       break;
 
     case PPOINTER:
-      genPagedPointerGet (left, result, ic);
+      genPagedPointerGet (left, result, ic, pi);
       break;
 
     case FPOINTER:
-      genFarPointerGet (left, result, ic);
+      genFarPointerGet (left, result, ic, pi);
       break;
 
     case CPOINTER:
-      emitcodePointerGet (left, result, ic);
+      genCodePointerGet (left, result, ic, pi);
       break;
 
     case GPOINTER:
-      genGenPointerGet (left, result, ic);
+      genGenPointerGet (left, result, ic, pi);
       break;
     }
 
@@ -7297,7 +7293,8 @@ genDataPointerSet (operand * right,
 static void
 genNearPointerSet (operand * right,
                   operand * result,
-                  iCode * ic)
+                  iCode * ic,
+                  iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -7335,7 +7332,6 @@ genNearPointerSet (operand * right,
   else
     rname = aopGet (AOP (result), 0, FALSE, FALSE);
 
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -7357,16 +7353,16 @@ genNearPointerSet (operand * right,
            }
          else
            emitcode ("mov", "@%s,%s", rname, l);
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
          offset++;
        }
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut (AOP (result),rname,0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -7376,10 +7372,11 @@ genNearPointerSet (operand * right,
          if size > 0 && this could be used again
          we have to point it back to where it
          belongs */
-      if (AOP_SIZE (right) > 1 &&
-         !OP_SYMBOL (result)->remat &&
-         (OP_SYMBOL (result)->liveTo > ic->seq ||
-          ic->depth))
+      if ((AOP_SIZE (right) > 1 &&
+          !OP_SYMBOL (result)->remat &&
+          (OP_SYMBOL (result)->liveTo > ic->seq ||
+           ic->depth)) &&
+         !pi)
        {
          int size = AOP_SIZE (right) - 1;
          while (size--)
@@ -7388,9 +7385,9 @@ genNearPointerSet (operand * right,
     }
 
   /* done */
+  if (pi) pi->generated = 1;
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
-
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -7399,7 +7396,8 @@ genNearPointerSet (operand * right,
 static void
 genPagedPointerSet (operand * right,
                    operand * result,
-                   iCode * ic)
+                   iCode * ic,
+                   iCode * pi)
 {
   asmop *aop = NULL;
   regs *preg = NULL;
@@ -7426,7 +7424,6 @@ genPagedPointerSet (operand * right,
   else
     rname = aopGet (AOP (result), 0, FALSE, FALSE);
 
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bitfield then unpack the bits */
@@ -7445,7 +7442,7 @@ genPagedPointerSet (operand * right,
          MOVA (l);
          emitcode ("movx", "@%s,a", rname);
 
-         if (size)
+         if (size || pi)
            emitcode ("inc", "%s", rname);
 
          offset++;
@@ -7453,9 +7450,9 @@ genPagedPointerSet (operand * right,
     }
 
   /* now some housekeeping stuff */
-  if (aop)
+  if (aop) /* we had to allocate for this iCode */
     {
-      /* we had to allocate for this iCode */
+      if (pi) aopPut (AOP (result),rname,0);
       freeAsmop (NULL, aop, ic, TRUE);
     }
   else
@@ -7477,6 +7474,8 @@ genPagedPointerSet (operand * right,
     }
 
   /* done */
+  if (pi) pi->generated = 1;
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 
 
@@ -7487,7 +7486,7 @@ genPagedPointerSet (operand * right,
 /*-----------------------------------------------------------------*/
 static void
 genFarPointerSet (operand * right,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (right));
@@ -7508,7 +7507,6 @@ genFarPointerSet (operand * right,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
@@ -7524,11 +7522,16 @@ genFarPointerSet (operand * right,
          char *l = aopGet (AOP (right), offset++, FALSE, FALSE);
          MOVA (l);
          emitcode ("movx", "@dptr,a");
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
-
+  if (pi && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD) {
+    aopPut (AOP(result),"dpl",0);
+    aopPut (AOP(result),"dph",1);
+    pi->generated=1;
+  }
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 }
 
@@ -7537,7 +7540,7 @@ genFarPointerSet (operand * right,
 /*-----------------------------------------------------------------*/
 static void
 genGenPointerSet (operand * right,
-                 operand * result, iCode * ic)
+                 operand * result, iCode * ic, iCode * pi)
 {
   int size, offset;
   sym_link *retype = getSpec (operandType (right));
@@ -7563,7 +7566,6 @@ genGenPointerSet (operand * right,
        }
     }
   /* so dptr know contains the address */
-  freeAsmop (result, NULL, ic, TRUE);
   aopOp (right, ic, FALSE);
 
   /* if bit then unpack */
@@ -7579,11 +7581,17 @@ genGenPointerSet (operand * right,
          char *l = aopGet (AOP (right), offset++, FALSE, FALSE);
          MOVA (l);
          emitcode ("lcall", "__gptrput");
-         if (size)
+         if (size || pi)
            emitcode ("inc", "dptr");
        }
     }
 
+  if (pi && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD) {
+    aopPut (AOP(result),"dpl",0);
+    aopPut (AOP(result),"dph",1);
+    pi->generated=1;
+  }
+  freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 }
 
@@ -7591,7 +7599,7 @@ genGenPointerSet (operand * right,
 /* genPointerSet - stores the value into a pointer location        */
 /*-----------------------------------------------------------------*/
 static void
-genPointerSet (iCode * ic)
+genPointerSet (iCode * ic, iCode *pi)
 {
   operand *right, *result;
   sym_link *type, *etype;
@@ -7622,19 +7630,19 @@ genPointerSet (iCode * ic)
 
     case POINTER:
     case IPOINTER:
-      genNearPointerSet (right, result, ic);
+      genNearPointerSet (right, result, ic, pi);
       break;
 
     case PPOINTER:
-      genPagedPointerSet (right, result, ic);
+      genPagedPointerSet (right, result, ic, pi);
       break;
 
     case FPOINTER:
-      genFarPointerSet (right, result, ic);
+      genFarPointerSet (right, result, ic, pi);
       break;
 
     case GPOINTER:
-      genGenPointerSet (right, result, ic);
+      genGenPointerSet (right, result, ic, pi);
       break;
     }
 
@@ -7787,8 +7795,7 @@ genAssign (iCode * ic)
   aopOp (right, ic, FALSE);
 
   /* special case both in far space */
-  if ((AOP_TYPE (right) == AOP_DPTR ||
-       AOP_TYPE (right) == AOP_DPTR2) &&
+  if (AOP_TYPE (right) == AOP_DPTR &&
       IS_TRUE_SYMOP (result) &&
       isOperandInFarSpace (result))
     {
@@ -8385,12 +8392,12 @@ gen51Code (iCode * lic)
          break;
 
        case GET_VALUE_AT_ADDRESS:
-         genPointerGet (ic);
+         genPointerGet (ic, hasInc(IC_LEFT(ic),ic));
          break;
 
        case '=':
          if (POINTER_SET (ic))
-           genPointerSet (ic);
+           genPointerSet (ic, hasInc (IC_RESULT(ic),ic));
          else
            genAssign (ic);
          break;
@@ -8421,8 +8428,6 @@ gen51Code (iCode * lic)
 
        default:
          ic = ic;
-         /*      piCode(ic,stdout); */
-
        }
     }