* src/hc08/gen.c (genPointerSet, genFarPointerSet): moved code from
[fw/sdcc] / src / hc08 / gen.c
index 25e62e95040a05f7af6bf9085ed62b7c655cccdf..273a685f0e2a8f247244c9a5d1dc1a7f93cbbda3 100644 (file)
@@ -27,8 +27,8 @@
 
 -------------------------------------------------------------------------*/
 
-//#define D(x)
-#define D(x) x
+#define D(x)
+//#define D(x) x
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -45,6 +45,8 @@
 char *aopLiteral (value * val, int offset);
 char *aopLiteralLong (value * val, int offset, int size);
 extern int allocInfo;
+static int pushReg (regs *reg, bool freereg);
+static void pullReg (regs *reg);
 
 static char *zero = "#0x00";
 static char *one = "#0x01";
@@ -68,6 +70,7 @@ static struct
     int stackPushes;
     short regsinuse;
     set *sendSet;
+    iCode *current_iCode;
   }
 _G;
 
@@ -168,6 +171,7 @@ emitcode (char *inst, char *fmt,...)
                (lineHead = newLineNode (lb)));
   lineCurr->isInline = _G.inLine;
   lineCurr->isDebug = _G.debugLine;
+  lineCurr->ic = _G.current_iCode;
 
   //printf("%s\n", lb);
   va_end (ap);
@@ -185,6 +189,17 @@ emitLabel (symbol *tlbl)
   emitcode ("", "%05d$:", (tlbl->key +100));
 }
 
+/*-----------------------------------------------------------------*/
+/* hc08_emitDebuggerSymbol - associate the current code location   */
+/*   with a debugger symbol                                        */
+/*-----------------------------------------------------------------*/
+void
+hc08_emitDebuggerSymbol (char * debugSym)
+{
+  _G.debugLine = 1;
+  emitcode ("", "%s ==.", debugSym);
+  _G.debugLine = 0;
+}
 
 
 /*--------------------------------------------------------------------------*/
@@ -211,8 +226,8 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
       return;
     }
 
-  emitcode ("", "; transferRegReg(%s,%s)",
-            sreg->name, dreg->name);  
+  D(emitcode ("", "; transferRegReg(%s,%s)",
+            sreg->name, dreg->name));
 
   srcidx = sreg->rIdx;
   dstidx = dreg->rIdx;
@@ -226,8 +241,8 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
         switch (srcidx)
           {
             case H_IDX: /* H to A */
-              emitcode ("pshh", "");
-              emitcode ("pula", "");
+             pushReg (hc08_reg_h, FALSE);
+             pullReg (hc08_reg_a);
               break;
             case X_IDX: /* X to A */
               emitcode ("txa", "");
@@ -240,12 +255,12 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
         switch (srcidx)
           {
             case A_IDX: /* A to H */
-              emitcode ("psha", "");
-              emitcode ("pulh", "");
+             pushReg (hc08_reg_a, FALSE);
+             pullReg (hc08_reg_h);
               break;
             case X_IDX: /* X to H */
-              emitcode ("pshx", "");
-              emitcode ("pulh", "");
+             pushReg (hc08_reg_x, FALSE);
+             pullReg (hc08_reg_h);
               break;
             default:
               error=1;
@@ -258,8 +273,8 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
               emitcode ("tax", "");
               break;
             case H_IDX: /* H to X */
-              emitcode ("pshh", "");
-              emitcode ("pulx", "");
+             pushReg (hc08_reg_h, FALSE);
+             pullReg (hc08_reg_x);
               break;
             default:
               error=1;
@@ -269,8 +284,8 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
         switch (srcidx)
           {
             case XA_IDX: /* XA to HX */
-              emitcode ("pshx", "");
-              emitcode ("pulh", "");
+             pushReg (hc08_reg_x, FALSE);
+             pullReg (hc08_reg_h);
               emitcode ("tax", "");
               break;
             default:
@@ -282,8 +297,8 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
           {
             case HX_IDX: /* HX to XA */
               emitcode ("txa", "");
-              emitcode ("pshh", "");
-              emitcode ("pulx", "");
+             pushReg (hc08_reg_h, FALSE);
+             pullReg (hc08_reg_x);
               break;
             default:
               error=1;
@@ -304,6 +319,21 @@ transferRegReg (regs *sreg, regs *dreg, bool freesrc)
   hc08_useReg(dreg);
 }
 
+/*--------------------------------------------------------------------------*/
+/* updateCFA - update the debugger information to reflect the current       */
+/*             connonical frame address relative to the stack pointer       */
+/*--------------------------------------------------------------------------*/
+static void
+updateCFA(void)
+{
+  /* there is no frame unless there is a function */
+  if (!currFunc)
+    return;
+  
+  debugFile->writeFrameAddress (NULL, hc08_reg_sp,
+                               1 + _G.stackOfs + _G.stackPushes);
+}
+
 /*--------------------------------------------------------------------------*/
 /* pushReg - Push register reg onto the stack. If freereg is true, reg is   */
 /*           marked free and available for reuse.                           */
@@ -318,24 +348,33 @@ pushReg (regs *reg, bool freereg)
       case A_IDX:
         emitcode ("psha", "");
         _G.stackPushes++;
+       updateCFA();
         break;
       case X_IDX:
         emitcode ("pshx", "");
         _G.stackPushes++;
+       updateCFA();
         break;
       case H_IDX:
         emitcode ("pshh", "");
         _G.stackPushes++;
+       updateCFA();
         break;
       case HX_IDX:
         emitcode ("pshx", "");
+        _G.stackPushes++;
+       updateCFA();
         emitcode ("pshh", "");
-        _G.stackPushes += 2;
+        _G.stackPushes++;
+       updateCFA();
         break;
       case XA_IDX:
         emitcode ("psha", "");
+       updateCFA();
+        _G.stackPushes++;
         emitcode ("pshx", "");
-        _G.stackPushes += 2;
+       updateCFA();
+        _G.stackPushes++;
         break;
       default:
         break;
@@ -358,24 +397,33 @@ pullReg (regs *reg)
       case A_IDX:
         emitcode ("pula", "");
         _G.stackPushes--;
+       updateCFA();
         break;
       case X_IDX:
         emitcode ("pulx", "");
         _G.stackPushes--;
+       updateCFA();
         break;
       case H_IDX:
         emitcode ("pulh", "");
         _G.stackPushes--;
+       updateCFA();
         break;
       case HX_IDX:
         emitcode ("pulx", "");
+        _G.stackPushes--;
+       updateCFA();
         emitcode ("pulh", "");
-        _G.stackPushes -= 2;
+        _G.stackPushes--;
+       updateCFA();
         break;
       case XA_IDX:
         emitcode ("pula", "");
+        _G.stackPushes--;
+       updateCFA();
         emitcode ("pulx", "");
-        _G.stackPushes -= 2;
+        _G.stackPushes--;
+       updateCFA();
         break;
       default:
         break;
@@ -394,6 +442,7 @@ pullNull (int n)
     {
       emitcode("ais","#%d",n);
       _G.stackPushes -= n;
+      updateCFA();
     }
 }
 
@@ -432,23 +481,28 @@ pullOrFreeReg (regs *reg, bool needpull)
 static void
 adjustStack (int n)
 {
-  _G.stackPushes -= n;
   while (n)
     {
       if (n>127)
         {
           emitcode ("ais","#127");
           n -= 127;
+         _G.stackPushes -= 127;
+         updateCFA();
         }
       else if (n<-128)
         {
           emitcode ("ais","#-128");
           n += 128;
+         _G.stackPushes += 128;
+         updateCFA();
         }
       else
         {
           emitcode ("ais", "#%d", n);
+         _G.stackPushes -= n;
           n = 0;
+         updateCFA();
         }
     }    
 }
@@ -543,8 +597,8 @@ loadRegFromAop (regs *reg, asmop *aop, int loffset)
     printf(" reg missing operand link\n");
 #endif
 
-  emitcode ("", ";     loadRegFromAop (%s, %s, %d)",
-            reg->name, aopName (aop), loffset);
+  D(emitcode ("", ";     loadRegFromAop (%s, %s, %d)",
+            reg->name, aopName (aop), loffset));
        
   /* If operand is volatile, we cannot optimize. */
   if (!aop->op || isOperandVolatile (aop->op, FALSE))
@@ -558,7 +612,7 @@ loadRegFromAop (regs *reg, asmop *aop, int loffset)
       && (reg->aopofs == loffset))
     {
       hc08_useReg(reg);
-      emitcode ("","; already had correct value for %s", reg->name);
+      D(emitcode ("","; already had correct value for %s", reg->name));
       return;
     }
 
@@ -568,7 +622,7 @@ loadRegFromAop (regs *reg, asmop *aop, int loffset)
       && operandsEqu(hc08_reg_h->aop->op,aop->op)
       && (hc08_reg_h->aopofs == loffset))
     {
-      emitcode ("","; found correct value for %s in h", reg->name);
+      D(emitcode ("","; found correct value for %s in h", reg->name));
       transferRegReg (hc08_reg_h, reg, FALSE);
       hc08_useReg (reg);
       return;
@@ -579,7 +633,7 @@ loadRegFromAop (regs *reg, asmop *aop, int loffset)
       && operandsEqu(hc08_reg_x->aop->op,aop->op)
       && (hc08_reg_x->aopofs == loffset))
     {
-      emitcode ("","; found correct value for %s in x", reg->name);
+      D(emitcode ("","; found correct value for %s in x", reg->name));
       transferRegReg (hc08_reg_x, reg, FALSE);
       hc08_useReg (reg);
       return;
@@ -589,7 +643,7 @@ loadRegFromAop (regs *reg, asmop *aop, int loffset)
       && operandsEqu(hc08_reg_a->aop->op,aop->op)
       && (hc08_reg_a->aopofs == loffset))
     {
-      emitcode ("","; found correct value for %s in a", reg->name);
+      D(emitcode ("","; found correct value for %s in a", reg->name));
       transferRegReg (hc08_reg_a, reg, FALSE);
       hc08_useReg (reg);
       return;
@@ -702,7 +756,7 @@ forceStackedAop (asmop *aop)
   asmop *newaop = newAsmop (aop->type);
   memcpy (newaop, aop, sizeof(*newaop));
   
-  emitcode("", "; forcedStackAop %s", aopName(aop));
+  D(emitcode("", "; forcedStackAop %s", aopName(aop)));
   for (loffset=0; loffset < newaop->size; loffset++)
     {
       asmop *aopsof = newAsmop (AOP_SOF);
@@ -728,8 +782,8 @@ storeRegToAop (regs *reg, asmop *aop, int loffset)
   int otheridx;
   #endif
 
-  emitcode ("", ";     storeRegToAop (%s, %s, %d), stacked=%d, isaddr=%d",
-            reg->name, aopName (aop), loffset, aop->stacked, aop->isaddr);
+  D(emitcode ("", ";     storeRegToAop (%s, %s, %d), stacked=%d, isaddr=%d",
+            reg->name, aopName (aop), loffset, aop->stacked, aop->isaddr));
 
   if ((reg->rIdx == HX_IDX) && aop->stacked
       && (aop->stk_aop[loffset] || aop->stk_aop[loffset+1]))
@@ -762,7 +816,10 @@ storeRegToAop (regs *reg, asmop *aop, int loffset)
       return;
     }
 
-    switch (regidx)
+  if (aop->type == AOP_DUMMY)
+    return;
+    
+  switch (regidx)
     {
       case A_IDX:
         if ((aop->type == AOP_REG) && (loffset < aop->size))
@@ -842,19 +899,19 @@ storeRegToAop (regs *reg, asmop *aop, int loffset)
                 && operandsEqu(otherreg->aop->op,aop->op)
                 && (otherreg->aopofs == loffset))
               {
-                emitcode("","; marking %s stale", otherreg->name);
+                D(emitcode("","; marking %s stale", otherreg->name));
                 otherreg->aop=NULL;
               }
           }
       if ((!hc08_reg_x->aop || !hc08_reg_h->aop) && hc08_reg_hx->aop)
         {
           hc08_reg_hx->aop = NULL;
-          emitcode("","; marking hx stale");
+          D(emitcode("","; marking hx stale"));
         }
       if ((!hc08_reg_x->aop || !hc08_reg_a->aop) && hc08_reg_xa->aop)
         {
           hc08_reg_xa->aop = NULL;
-          emitcode("","; marking xa stale");
+          D(emitcode("","; marking xa stale"));
         }
     
       reg->aop = aop;
@@ -945,6 +1002,8 @@ storeConstToAop (char *c, asmop *aop, int loffset)
           break;
         loadRegFromConst (aop->aopu.aop_reg[loffset], c);
         break;
+      case AOP_DUMMY:
+        break;
       default:
         if (hc08_reg_a->isFree)
           {
@@ -1073,10 +1132,10 @@ transferAopAop (asmop *srcaop, int srcofs, asmop *dstaop, int dstofs)
       return;
     }
 
-//  emitcode ("", "; transferAopAop (%s, %d, %s, %d)",
-//            aopName (srcaop), srcofs, aopName (dstaop), dstofs);  
-//  emitcode ("", "; srcaop->type = %d", srcaop->type);
-//  emitcode ("", "; dstaop->type = %d", dstaop->type);
+//  D(emitcode ("", "; transferAopAop (%s, %d, %s, %d)",
+//            aopName (srcaop), srcofs, aopName (dstaop), dstofs));
+//  D(emitcode ("", "; srcaop->type = %d", srcaop->type));
+//  D(emitcode ("", "; dstaop->type = %d", dstaop->type));
   
   if (dstofs >= dstaop->size)
     return;
@@ -1155,7 +1214,10 @@ accopWithAop (char *accop, asmop *aop, int loffset)
       accopWithAop (accop, aop->stk_aop[loffset], 0);
       return;
     }
-    
+
+  if (aop->type == AOP_DUMMY)
+    return;
+
   if (aop->type == AOP_REG)
     {
       pushReg (aop->aopu.aop_reg[loffset], FALSE);
@@ -1238,6 +1300,8 @@ rmwWithAop (char *rmwop, asmop *aop, int loffset)
        storeRegToAop (hc08_reg_a, aop, loffset);
         pullOrFreeReg (hc08_reg_a, needpula);
         break;
+      case AOP_DUMMY:
+        break;
       default:
         emitcode (rmwop, "%s", aopAdrStr (aop, loffset, FALSE));
    }
@@ -1352,7 +1416,7 @@ static asmop *
 aopForRemat (symbol * sym)
 {
   iCode *ic = sym->rematiCode;
-  asmop *aop = newAsmop (AOP_IMMD);
+  asmop *aop = NULL;
   int ptr_type=0;
   int val = 0;
 
@@ -1377,22 +1441,40 @@ aopForRemat (symbol * sym)
       ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
     }
 
-  if (val)
-    sprintf (buffer, "(%s %c 0x%04x)",
-            OP_SYMBOL (IC_LEFT (ic))->rname,
-            val >= 0 ? '+' : '-',
-            abs (val) & 0xffff);
-  else
-    strcpy (buffer, OP_SYMBOL (IC_LEFT (ic))->rname);
+  if (ic->op == ADDRESS_OF)
+    {
+      if (val)
+        sprintf (buffer, "(%s %c 0x%04x)",
+                OP_SYMBOL (IC_LEFT (ic))->rname,
+                val >= 0 ? '+' : '-',
+                abs (val) & 0xffff);
+      else
+        strcpy (buffer, OP_SYMBOL (IC_LEFT (ic))->rname);
 
-  aop->aopu.aop_immd.aop_immd1 = Safe_calloc (1, strlen (buffer) + 1);
-  strcpy (aop->aopu.aop_immd.aop_immd1, buffer);
-  /* set immd2 field if required */
-  if (aop->aopu.aop_immd.from_cast_remat) {
+      aop = newAsmop (AOP_IMMD);
+      aop->aopu.aop_immd.aop_immd1 = Safe_calloc (1, strlen (buffer) + 1);
+      strcpy (aop->aopu.aop_immd.aop_immd1, buffer);
+      /* set immd2 field if required */
+      if (aop->aopu.aop_immd.from_cast_remat)
+       {
          sprintf(buffer,"#0x%02x",ptr_type);
          aop->aopu.aop_immd.aop_immd2 = Safe_calloc (1, strlen (buffer) + 1);
          strcpy (aop->aopu.aop_immd.aop_immd2, buffer);
-  }
+       }
+    }
+  else if (ic->op == '=')
+    {
+      val += (int) operandLitValue (IC_RIGHT (ic));
+      val &= 0xffff;
+      sprintf (buffer, "0x%04x", val);
+      aop = newAsmop (AOP_LIT);
+      aop->aopu.aop_lit = constVal (buffer);
+    }
+  else
+    werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+           "unexpected rematerialization");
+
+
 
   return aop;
 }
@@ -1460,10 +1542,10 @@ operandsEqu (operand * op1, operand * op2)
   if (sym1 == sym2)
     return TRUE;
 
-  if (strcmp (sym1->rname, sym2->rname) == 0)
+  if (sym1->rname[0] && sym2->rname[0]
+      && strcmp (sym1->rname, sym2->rname) == 0)
     return TRUE;
 
-
   /* if left is a tmp & right is not */
   if (IS_ITEMP (op1) &&
       !IS_ITEMP (op2) &&
@@ -1671,27 +1753,27 @@ aopOp (operand * op, iCode * ic, bool result)
        }
 #endif
       /* else spill location  */
-//      printf("checking spill loc\n");
-//      if (sym->usl.spillLoc && getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
-      if (sym->usl.spillLoc && sym->usl.spillLoc->aop
-          && sym->usl.spillLoc->aop->size != getSize (sym->type))
+      if (sym->usl.spillLoc)
         {
-         /* force a new aop if sizes differ */
-         sym->usl.spillLoc->aop = NULL;
-         //printf ("forcing new aop\n");
-        }
-      sym->aop = op->aop = aop =
-       aopForSym (ic, sym->usl.spillLoc, result);
+          if (sym->usl.spillLoc->aop
+              && sym->usl.spillLoc->aop->size != getSize (sym->type))
+            {
+             /* force a new aop if sizes differ */
+             sym->usl.spillLoc->aop = NULL;
+             //printf ("forcing new aop\n");
+            }
+         sym->aop = op->aop = aop = aopForSym (ic, sym->usl.spillLoc, result);
+         aop->size = getSize (sym->type);
+         aop->op = op;
+         aop->isaddr = op->isaddr;
+         //printf ("spill symbol %s\n", OP_SYMBOL (op)->name);
+         //printf (" with size = %d\n", aop->size);
+         return;
+       }
+      
+      /* else must be a dummy iTemp */
+      sym->aop = op->aop = aop = newAsmop (AOP_DUMMY);
       aop->size = getSize (sym->type);
-      aop->op = op;
-      aop->isaddr = op->isaddr;
-      //printf ("spill symbol %s\n", OP_SYMBOL (op)->name);
-      //printf (" with size = %d\n", aop->size);
-      /* if (aop->isaddr & IS_ITEMP (op))
-        {
-          aop->psize=aop->size;
-          aop->size = getSize( operandType (op)->next);
-        } */
       return;
     }
 
@@ -1732,7 +1814,7 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
       int stackAdjust;
       int loffset;
 
-      emitcode ("","; freeAsmop restoring stacked %s", aopName(aop));
+      D(emitcode ("","; freeAsmop restoring stacked %s", aopName(aop)));
       aop->stacked = 0;
       stackAdjust = 0;
       for (loffset=0; loffset<aop->size; loffset++)
@@ -1774,7 +1856,7 @@ aopDerefAop (asmop *aop)
   sym_link *type, *etype;
   int p_type;
   
-  emitcode ("", ";     aopDerefAop(%s)", aopName(aop));
+  D(emitcode ("", ";     aopDerefAop(%s)", aopName(aop)));
   if (aop->op)
     {
     
@@ -1853,6 +1935,9 @@ aopAdrStr (asmop * aop, int loffset, bool bit16)
   switch (aop->type)
     {
 
+    case AOP_DUMMY:
+      return zero;
+      
     case AOP_IMMD:
       if (aop->aopu.aop_immd.from_cast_remat && (loffset == (aop->size-1))) {
              sprintf(s,"%s",aop->aopu.aop_immd.aop_immd2);
@@ -1902,7 +1987,7 @@ aopAdrStr (asmop * aop, int loffset, bool bit16)
 
     case AOP_LIT:
       if (bit16)
-        return aopLiteralLong (aop->aopu.aop_lit, loffset, 2);
+        return aopLiteralLong (aop->aopu.aop_lit, /*loffset*/ 0, 2);
       else
         return aopLiteral (aop->aopu.aop_lit, loffset);
 
@@ -2080,9 +2165,12 @@ asmopToBool (asmop *aop, bool resultInA)
             emitcode ("", "%05d$:", (tlbl->key + 100));
           }
         else
-          werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
-                  "Bad rIdx in asmToBool");
-          return;
+          {
+            werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+                    "Bad rIdx in asmToBool");
+            return;
+          }
+        break;
       case AOP_EXT:
         if (resultInA)
           needpula = FALSE;
@@ -2269,6 +2357,7 @@ genUminus (iCode * ic)
   sym_link *optype, *rtype;
   char *sub;
   bool needpula;
+  asmop *result;
 
   D(emitcode (";     genUminus",""));
 
@@ -2292,7 +2381,10 @@ genUminus (iCode * ic)
 
   if (size == 1)
     {
-      needpula = pushRegIfUsed (hc08_reg_a);
+      if (!IS_AOP_A (AOP (IC_LEFT (ic))))
+        needpula = pushRegIfUsed (hc08_reg_a);
+      else
+        needpula = FALSE;
       loadRegFromAop (hc08_reg_a, AOP( IC_LEFT (ic)), 0);
       emitcode ("nega", "");
       hc08_freeReg (hc08_reg_a);
@@ -2302,18 +2394,26 @@ genUminus (iCode * ic)
     }
   else
     {
+      if (IS_AOP_XA (AOP (IC_RESULT (ic))))
+        result = forceStackedAop (AOP (IC_RESULT (ic)));
+      else
+        result = AOP (IC_RESULT (ic));
+       
       needpula = pushRegIfUsed (hc08_reg_a);
       sub="sub";
       while (size--)
         {
           loadRegFromConst (hc08_reg_a, zero);
           accopWithAop (sub, AOP( IC_LEFT (ic)), offset);
-          storeRegToAop (hc08_reg_a, AOP( IC_RESULT (ic)), offset++);
+          storeRegToAop (hc08_reg_a, result, offset++);
           sub = "sbc";
         }
-        storeRegSignToUpperAop (hc08_reg_a, AOP( IC_RESULT (ic)), offset, 
+        storeRegSignToUpperAop (hc08_reg_a, result, offset, 
                                 SPEC_USIGN (operandType (IC_LEFT (ic))));
       pullOrFreeReg (hc08_reg_a, needpula);
+      
+      if (IS_AOP_XA (AOP (IC_RESULT (ic))))
+        freeAsmop (NULL, result, ic, TRUE);
     }
 
 
@@ -2321,7 +2421,7 @@ genUminus (iCode * ic)
 release:
   /* release the aops */
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
-  freeAsmop (IC_LEFT (ic), NULL, ic, TRUE);
+  freeAsmop (IC_LEFT (ic), NULL, ic, FALSE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2750,6 +2850,10 @@ genFunction (iCode * ic)
 
   emitcode ("", "%s:", sym->rname);
   ftype = operandType (IC_LEFT (ic));
+  
+  _G.stackOfs = 0;
+  _G.stackPushes = 0;
+  debugFile->writeFrameAddress (NULL, hc08_reg_sp, 0);
 
   if (IFFUNC_ISNAKED(ftype))
   {
@@ -2765,7 +2869,7 @@ genFunction (iCode * ic)
     {
 
       if (!inExcludeList ("h"))
-       emitcode ("pshh", "");
+        pushReg (hc08_reg_h, FALSE);
     }
   else
     {
@@ -2847,6 +2951,8 @@ genEndFunction (iCode * ic)
   if (IFFUNC_ISNAKED(sym->type))
   {
       emitcode(";", "naked function: no epilogue.");
+      if (options.debug && currFunc)
+        debugFile->writeEndFunction (currFunc, ic, 0);
       return;
   }
 
@@ -2888,21 +2994,13 @@ genEndFunction (iCode * ic)
     {
 
       if (!inExcludeList ("h"))
-       emitcode ("pulh", "");
+        pullReg (hc08_reg_h);
 
 
       /* if debug then send end of function */
       if (options.debug && currFunc)
        {
-         _G.debugLine = 1;
-         emitcode ("", "C$%s$%d$%d$%d ==.",
-                   FileBaseName (ic->filename), currFunc->lastLine,
-                   ic->level, ic->block);
-         if (IS_STATIC (currFunc->etype))
-           emitcode ("", "XF%s$%s$0$0 ==.", moduleName, currFunc->name);
-         else
-           emitcode ("", "XG$%s$0$0 ==.", currFunc->name);
-         _G.debugLine = 0;
+         debugFile->writeEndFunction (currFunc, ic, 1);
        }
 
       emitcode ("rti", "");
@@ -2921,7 +3019,7 @@ genEndFunction (iCode * ic)
                {
                  if (bitVectBitValue (sym->regsUsed, i) ||
                      (hc08_ptrRegReq && (i == HX_IDX || i == HX_IDX)))
-                   emitcode ("pop", "%s", hc08_regWithIdx (i)->dname);
+                   emitcode ("pop", "%s", hc08_regWithIdx (i)->name);
                }
            }
 
@@ -2930,15 +3028,7 @@ genEndFunction (iCode * ic)
       /* if debug then send end of function */
       if (options.debug && currFunc)
        {
-         _G.debugLine = 1;
-         emitcode ("", "C$%s$%d$%d$%d ==.",
-                   FileBaseName (ic->filename), currFunc->lastLine,
-                   ic->level, ic->block);
-         if (IS_STATIC (currFunc->etype))
-           emitcode ("", "XF%s$%s$0$0 ==.", moduleName, currFunc->name);
-         else
-           emitcode ("", "XG$%s$0$0 ==.", currFunc->name);
-         _G.debugLine = 0;
+         debugFile->writeEndFunction (currFunc, ic, 1);
        }
 
       emitcode ("rts", "");
@@ -3033,6 +3123,8 @@ genLabel (iCode * ic)
   /* special case never generate */
   if (IC_LABEL (ic) == entryLabel)
     return;
+         
+  debugFile->writeLabel(IC_LABEL (ic), ic);
 
   emitcode ("", "%05d$:", (IC_LABEL (ic)->key + 100));
 
@@ -3108,15 +3200,23 @@ genPlusIncr (iCode * ic)
 
   icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.aop_lit);
 
-  emitcode ("", "; IS_AOP_HX = %d", IS_AOP_HX (AOP (left)));
+  D(emitcode ("", "; IS_AOP_HX = %d", IS_AOP_HX (AOP (left))));
   
   if ((IS_AOP_HX (AOP (left)) ||
        ( (AOP_TYPE (left) == AOP_DIR) && (AOP_TYPE (result) == AOP_DIR) )
       )
       && (icount>=-128) && (icount<=127) && (size==2))
     {
-      needpulx = pushRegIfUsed (hc08_reg_x);
-      needpulh = pushRegIfUsed (hc08_reg_h);
+      if (!IS_AOP_HX (AOP (left)))
+        {
+          needpulx = pushRegIfUsed (hc08_reg_x);
+          needpulh = pushRegIfUsed (hc08_reg_h);
+        }
+      else
+        {
+          needpulx = FALSE;
+          needpulh = FALSE;
+        }
       loadRegFromAop (hc08_reg_hx, AOP(left), 0);
       emitcode ("aix","#%d", icount);
       hc08_dirtyReg (hc08_reg_hx, FALSE);
@@ -3126,8 +3226,8 @@ genPlusIncr (iCode * ic)
       return TRUE;
     }
 
-  emitcode ("", "; icount = %d, sameRegs=%d", icount, 
-            sameRegs (AOP (left), AOP (result)));
+  D(emitcode ("", "; icount = %d, sameRegs=%d", icount, 
+            sameRegs (AOP (left), AOP (result))));
   
   if ((icount > 255) || (icount<0))
     return FALSE;
@@ -3149,7 +3249,10 @@ genPlusIncr (iCode * ic)
     }
   else
     {
-      needpula = pushRegIfUsed (hc08_reg_a);
+      if (!IS_AOP_A (AOP (result)) && !IS_AOP_XA (AOP (result)))
+        needpula = pushRegIfUsed (hc08_reg_a);
+      else
+        needpula = FALSE;
       loadRegFromAop (hc08_reg_a, AOP (result), 0);
       accopWithAop ("add", AOP (IC_RIGHT (ic)), 0);
       hc08_useReg (hc08_reg_a);
@@ -3208,9 +3311,9 @@ genPlus (iCode * ic)
   if (genPlusIncr (ic) == TRUE)
     goto release;
 
-  emitcode("",";  left size = %d", getDataSize (IC_LEFT(ic)));
-  emitcode("",";  right size = %d", getDataSize (IC_RIGHT(ic)));
-  emitcode("",";  result size = %d", getDataSize (IC_RESULT(ic)));
+  D(emitcode("",";  left size = %d", getDataSize (IC_LEFT(ic))));
+  D(emitcode("",";  right size = %d", getDataSize (IC_RIGHT(ic))));
+  D(emitcode("",";  result size = %d", getDataSize (IC_RESULT(ic))));
   
   size = getDataSize (IC_RESULT (ic));
 
@@ -3266,8 +3369,16 @@ genMinusDec (iCode * ic)
   if ((AOP_TYPE (left) == AOP_DIR) && (AOP_TYPE (result) == AOP_DIR)
       && (icount>=-127) && (icount<=128) && (size==2))
     {
-      needpulx = pushRegIfUsed (hc08_reg_x);
-      needpulh = pushRegIfUsed (hc08_reg_h);
+      if (!IS_AOP_HX (AOP (left)))
+        {
+          needpulx = pushRegIfUsed (hc08_reg_x);
+          needpulh = pushRegIfUsed (hc08_reg_h);
+        }
+      else
+        {
+          needpulx = FALSE;
+          needpulh = FALSE;
+        }
       loadRegFromAop (hc08_reg_hx, AOP(left), 0);
       emitcode ("aix","#%d", -icount);
       hc08_dirtyReg (hc08_reg_hx, FALSE);
@@ -3375,7 +3486,7 @@ genMultOneByte (operand * left,
                operand * right,
                operand * result)
 {
-  sym_link *opetype = operandType (result);
+  /* sym_link *opetype = operandType (result); */
   symbol *tlbl1, *tlbl2, *tlbl3, *tlbl4;
   int size=AOP_SIZE(result);
   bool negLiteral = FALSE;
@@ -3396,13 +3507,15 @@ genMultOneByte (operand * left,
       operand *t = right;
       right = left;
       left = t;
-      //emitcode (";", "swapped left and right");
+      //D(emitcode (";", "swapped left and right"));
     }
 
-  if (SPEC_USIGN(opetype))
+  if (size == 1
+      || (SPEC_USIGN(operandType(left)) &&
+         SPEC_USIGN(operandType(right))))
     {
       // just an unsigned 8*8=8/16 multiply
-      //emitcode (";","unsigned");
+      //D(emitcode (";","unsigned"));
 
       loadRegFromAop (hc08_reg_a, AOP (left), 0);
       loadRegFromAop (hc08_reg_x, AOP (right), 0);
@@ -3417,7 +3530,7 @@ genMultOneByte (operand * left,
   // we have to do a signed multiply
 
 
-  //emitcode (";", "signed");
+  //D(emitcode (";", "signed"));
   adjustStack (-1);
   emitcode ("clr", "1,s");
 
@@ -3859,8 +3972,7 @@ genCmp (operand * left, operand * right,
            }
          else
            {
-              needpula = pushRegIfUsed (hc08_reg_a);
-             loadRegFromAop (hc08_reg_a, AOP (left), AOP_SIZE (left) -1);
+             loadRegFromAop (hc08_reg_a, AOP (left), AOP_SIZE (left) -1);
              emitcode ("rola", "");
              hc08_useReg (hc08_reg_a);
            }
@@ -4135,44 +4247,49 @@ genPointerGetSetOfs (iCode *ic)
   asmop *derefaop;
 
   /* Make sure we have a next iCode */
-  emitcode("","; checking lic");
+  D(emitcode("","; checking lic"));
   if (!lic)
     return FALSE;
 
   /* Make sure the result of the addition is an iCode */
-  emitcode("","; checking IS_ITEMP");
+  D(emitcode("","; checking IS_ITEMP"));
   if (!IS_ITEMP (IC_RESULT (ic)))
     return FALSE;
 
   /* Make sure the next iCode is a pointer set or get */
   pset = POINTER_SET(lic);
   pget = POINTER_GET(lic);
-  emitcode("","; pset=%d, pget=%d",pset,pget);
+  D(emitcode("","; pset=%d, pget=%d",pset,pget));
   if (!pset && !pget)
     return FALSE;
 
-  emitcode("", "; checking pset operandsEqu");
+  /* Make sure this is the only use of the pointer */
+  if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
+    return FALSE;
+    
+  D(emitcode("", "; checking pset operandsEqu"));
   if (pset & !operandsEqu (IC_RESULT (ic), IC_RESULT (lic)))
     return FALSE;
 
-  emitcode("", "; checking pget operandsEqu");
+  D(emitcode("", "; checking pget operandsEqu"));
   if (pget & !operandsEqu (IC_RESULT (ic), IC_LEFT (lic)))
     return FALSE;
 
-  emitcode("", "; checking IS_SYMOP");
+  D(emitcode("", "; checking IS_SYMOP"));
   if (!IS_SYMOP (IC_LEFT (ic)))
     return FALSE;
 
-  emitcode("", "; checking !IS_TRUE_SYMOP");
+  D(emitcode("", "; checking !IS_TRUE_SYMOP"));
   if (IS_TRUE_SYMOP (IC_LEFT (ic)))
     return FALSE;
 
   sym = OP_SYMBOL (IC_LEFT (ic));
   
-  emitcode("", "; checking remat");
+  D(emitcode("", "; checking remat"));
   if (!sym->remat)
     return FALSE;
     
+  
   if (pget)
     {
       D(emitcode (";     genPointerGetOfs",""));
@@ -4182,9 +4299,27 @@ genPointerGetSetOfs (iCode *ic)
       
       aopOp (IC_RIGHT(ic), ic, FALSE);
       aopOp (IC_RESULT(lic), lic, FALSE);
-      
 
-      loadRegFromAop (hc08_reg_hx, AOP (IC_RIGHT (ic)), 0);
+      if (AOP_SIZE (IC_RIGHT (ic)) == 1)
+        {
+          if (SPEC_USIGN (getSpec (operandType (IC_RIGHT (ic)))))
+            {
+              loadRegFromAop (hc08_reg_x, AOP (IC_RIGHT (ic)), 0);
+              loadRegFromConst (hc08_reg_h, zero);
+            }
+          else
+            {
+              loadRegFromAop (hc08_reg_a, AOP (IC_RIGHT (ic)), 0);
+              transferRegReg (hc08_reg_a, hc08_reg_x, FALSE);
+              emitcode ("rola","");
+              emitcode ("clra","");
+              emitcode ("sbc", "#0");
+              hc08_useReg (hc08_reg_a);
+              transferRegReg (hc08_reg_a, hc08_reg_h, FALSE);
+           }
+        }
+      else
+        loadRegFromAop (hc08_reg_hx, AOP (IC_RIGHT (ic)), 0);
       size = AOP_SIZE (IC_RESULT(lic));
       derefaop->size = size;
       offset=0;
@@ -4218,8 +4353,26 @@ genPointerGetSetOfs (iCode *ic)
       aopOp (IC_RIGHT(ic), ic, FALSE);
       aopOp (IC_RIGHT(lic), lic, FALSE);
       
-
-      loadRegFromAop (hc08_reg_hx, AOP (IC_RIGHT (ic)), 0);
+      if (AOP_SIZE (IC_RIGHT (ic)) == 1)
+        {
+          if (SPEC_USIGN (getSpec (operandType (IC_RIGHT (ic)))))
+            {
+              loadRegFromAop (hc08_reg_x, AOP (IC_RIGHT (ic)), 0);
+              loadRegFromConst (hc08_reg_h, zero);
+            }
+          else
+            {
+              loadRegFromAop (hc08_reg_a, AOP (IC_RIGHT (ic)), 0);
+              transferRegReg (hc08_reg_a, hc08_reg_x, FALSE);
+              emitcode ("rola","");
+              emitcode ("clra","");
+              emitcode ("sbc", "#0");
+              hc08_useReg (hc08_reg_a);
+              transferRegReg (hc08_reg_a, hc08_reg_h, FALSE);
+           }
+        }
+      else
+        loadRegFromAop (hc08_reg_hx, AOP (IC_RIGHT (ic)), 0);
       size = AOP_SIZE (IC_RIGHT(lic));
       derefaop->size = size;
       offset=0;
@@ -4459,12 +4612,12 @@ genAnd (iCode * ic, iCode * ifx)
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  D(emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
            AOP_TYPE (result),
-           AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+           AOP_TYPE (left), AOP_TYPE (right)));
+  D(emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
            AOP_SIZE (result),
-           AOP_SIZE (left), AOP_SIZE (right));
+           AOP_SIZE (left), AOP_SIZE (right)));
 #endif
 
   /* if left is a literal & right is not then exchange them */
@@ -4483,6 +4636,35 @@ genAnd (iCode * ic, iCode * ifx)
       left = tmp;
     }
 
+
+  if (AOP_TYPE (result) == AOP_CRY)
+    {
+      symbol *tlbl;
+      wassertl (ifx, "AOP_CPY result without ifx");
+      
+      tlbl = newiTempLabel (NULL);
+      size = (AOP_SIZE (left) >= AOP_SIZE (right)) ? AOP_SIZE (left) : AOP_SIZE (right);
+      offset = 0;
+      while (size--)
+        {
+          loadRegFromAop (hc08_reg_a, AOP (left), offset);
+          if ((AOP_TYPE (right) == AOP_LIT)
+              && (((lit >> (offset*8)) & 0xff) == 0xff))
+            emitcode ("tsta","");
+          else
+            accopWithAop ("and", AOP (right), offset);
+          hc08_freeReg( hc08_reg_a);      
+          if (size)
+            emitBranch ("bne", tlbl);
+          else
+            {
+              emitLabel (tlbl);
+              genIfxJump (ifx, "a");
+            }
+          offset++;
+        }
+    }
+  
   size = AOP_SIZE (result);
 
   if (AOP_TYPE (right) == AOP_LIT)
@@ -4499,7 +4681,7 @@ genAnd (iCode * ic, iCode * ifx)
           goto release;
         }
     }
-
+    
   offset = 0;
   while (size--)
     {
@@ -4534,12 +4716,12 @@ genOr (iCode * ic, iCode * ifx)
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  D(emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
            AOP_TYPE (result),
-           AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+           AOP_TYPE (left), AOP_TYPE (right)));
+  D(emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
            AOP_SIZE (result),
-           AOP_SIZE (left), AOP_SIZE (right));
+           AOP_SIZE (left), AOP_SIZE (right)));
 #endif
 
   /* if left is a literal & right is not then exchange them */
@@ -4558,14 +4740,34 @@ genOr (iCode * ic, iCode * ifx)
       left = tmp;
     }
 
-  /* if right is bit then exchange them */
-  if (AOP_TYPE (right) == AOP_CRY &&
-      AOP_TYPE (left) != AOP_CRY)
+  if (AOP_TYPE (result) == AOP_CRY)
     {
-      operand *tmp = right;
-      right = left;
-      left = tmp;
+      symbol *tlbl;
+      wassertl (ifx, "AOP_CPY result without ifx");
+      
+      tlbl = newiTempLabel (NULL);
+      size = (AOP_SIZE (left) >= AOP_SIZE (right)) ? AOP_SIZE (left) : AOP_SIZE (right);
+      offset = 0;
+      while (size--)
+        {
+          loadRegFromAop (hc08_reg_a, AOP (left), offset);
+          if ((AOP_TYPE (right) == AOP_LIT)
+              && (((lit >> (offset*8)) & 0xff) == 0))
+            emitcode ("tsta","");
+          else
+            accopWithAop ("ora", AOP (right), offset);
+          hc08_freeReg( hc08_reg_a);      
+          if (size)
+            emitBranch ("bne", tlbl);
+          else
+            {
+              emitLabel (tlbl);
+              genIfxJump (ifx, "a");
+            }
+          offset++;
+        }
     }
+  
   if (AOP_TYPE (right) == AOP_LIT)
     lit = (unsigned long) floatFromVal (AOP (right)->aopu.aop_lit);
 
@@ -4581,8 +4783,6 @@ genOr (iCode * ic, iCode * ifx)
       goto release;
     }
     
-
-
   offset = 0;
   while (size--)
     {
@@ -4616,12 +4816,12 @@ genXor (iCode * ic, iCode * ifx)
   aopOp ((result = IC_RESULT (ic)), ic, TRUE);
 
 #ifdef DEBUG_TYPE
-  emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
+  D(emitcode ("", "; Type res[%d] = l[%d]&r[%d]",
            AOP_TYPE (result),
-           AOP_TYPE (left), AOP_TYPE (right));
-  emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
+           AOP_TYPE (left), AOP_TYPE (right)));
+  D(emitcode ("", "; Size res[%d] = l[%d]&r[%d]",
            AOP_SIZE (result),
-           AOP_SIZE (left), AOP_SIZE (right));
+           AOP_SIZE (left), AOP_SIZE (right)));
 #endif
 
   /* if left is a literal & right is not ||
@@ -4641,14 +4841,34 @@ genXor (iCode * ic, iCode * ifx)
       left = tmp;
     }
 
-  /* if right is bit then exchange them */
-  if (AOP_TYPE (right) == AOP_CRY &&
-      AOP_TYPE (left) != AOP_CRY)
+  if (AOP_TYPE (result) == AOP_CRY)
     {
-      operand *tmp = right;
-      right = left;
-      left = tmp;
+      symbol *tlbl;
+      wassertl (ifx, "AOP_CPY result without ifx");
+      
+      tlbl = newiTempLabel (NULL);
+      size = (AOP_SIZE (left) >= AOP_SIZE (right)) ? AOP_SIZE (left) : AOP_SIZE (right);
+      offset = 0;
+      while (size--)
+        {
+          loadRegFromAop (hc08_reg_a, AOP (left), offset);
+          if ((AOP_TYPE (right) == AOP_LIT)
+              && (((lit >> (offset*8)) & 0xff) == 0))
+            emitcode ("tsta","");
+          else
+            accopWithAop ("eor", AOP (right), offset);
+          hc08_freeReg( hc08_reg_a);      
+          if (size)
+            emitBranch ("bne", tlbl);
+          else
+            {
+              emitLabel (tlbl);
+              genIfxJump (ifx, "a");
+            }
+          offset++;
+        }
     }
+    
   if (AOP_TYPE (right) == AOP_LIT)
     lit = (unsigned long) floatFromVal (AOP (right)->aopu.aop_lit);
 
@@ -4662,7 +4882,6 @@ genXor (iCode * ic, iCode * ifx)
       hc08_freeReg( hc08_reg_a);      
     }
 
-
 //release:
   freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
   freeAsmop (right, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
@@ -4965,7 +5184,55 @@ genGetHbit (iCode * ic)
   emitcode ("rola", "");
   hc08_dirtyReg (hc08_reg_a, FALSE);
   storeRegToFullAop (hc08_reg_a, AOP (result), FALSE);
+  hc08_freeReg (hc08_reg_a);
+  
+  freeAsmop (left, NULL, ic, TRUE);
+  freeAsmop (result, NULL, ic, TRUE);
+}
+
+/*-----------------------------------------------------------------*/
+/* genSwap - generates code to swap nibbles or bytes               */
+/*-----------------------------------------------------------------*/
+static void
+genSwap (iCode * ic)
+{
+  operand *left, *result;
+
+  D(emitcode (";     genSwap",""));
+
+  left = IC_LEFT (ic);
+  result = IC_RESULT (ic);
+  aopOp (left, ic, FALSE);
+  aopOp (result, ic, FALSE);
   
+  switch (AOP_SIZE (left))
+    {
+    case 1: /* swap nibbles in byte */
+      loadRegFromAop (hc08_reg_a, AOP (left), 0);
+      emitcode ("nsa", "");
+      hc08_dirtyReg (hc08_reg_a, FALSE);
+      storeRegToAop (hc08_reg_a, AOP (result), 0);
+      hc08_freeReg (hc08_reg_a);
+      break;
+    case 2: /* swap bytes in a word */
+      if (operandsEqu (left, result))
+        {
+          loadRegFromAop (hc08_reg_a, AOP (left), 0);
+          hc08_useReg (hc08_reg_a);
+          transferAopAop (AOP (left), 1, AOP (result), 0);
+          storeRegToAop (hc08_reg_a, AOP (result), 1);
+          hc08_freeReg (hc08_reg_a);
+        }
+      else
+        {
+          transferAopAop (AOP (left), 0, AOP (result), 1);
+          transferAopAop (AOP (left), 1, AOP (result), 0);
+        }
+      break;
+    default:
+      wassertl(FALSE, "unsupported SWAP operand size");
+    }
+    
   freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
 }
@@ -5277,7 +5544,7 @@ XAccRsh (int shCount, bool sign)
       return;
     }
   
-  shCount &= 0x000f;           // shCount : 0..7
+  shCount &= 0x000f;           // shCount : 0..f
 
   /* if we can beat 2n cycles or bytes for some special case, do it here */
   switch (shCount)
@@ -5305,7 +5572,7 @@ XAccRsh (int shCount, bool sign)
       **   rola       1  1        bcde fgh0  0000 000a   0
       **   lslx       1  1        cdef gh00  0000 000a   b
       **   rola       1  1        cdef gh00  0000 00ab   0
-      **   clrx       1  1        0000 0000  0000 000a   0
+      **   clrx       1  1        0000 0000  0000 00ab   0
       ** total: 6 cycles, 6 bytes
       */
       loadRegFromConst (hc08_reg_x, zero);
@@ -5361,12 +5628,12 @@ XAccRsh (int shCount, bool sign)
       ;
     }
 
-  /* lslx/rola is only 2 cycles and bytes, so an unrolled loop is often  */
+  /* lsrx/rora is only 2 cycles and bytes, so an unrolled loop is often  */
   /* the fastest and shortest.                                           */
   for (i=0;i<shCount;i++)
     {
-      rmwWithReg ("lsl", hc08_reg_x);
-      rmwWithReg ("rol", hc08_reg_a);
+      rmwWithReg ("lsr", hc08_reg_x);
+      rmwWithReg ("ror", hc08_reg_a);
     }
 
 }
@@ -5416,129 +5683,54 @@ movLeft2Result (operand * left, int offl,
 
 
 /*-----------------------------------------------------------------*/
-/* AccAXRsh1 - right shift 0->a:x->c by 1                         */
+/* shiftL2Left2Result - shift left two bytes from left to result   */
 /*-----------------------------------------------------------------*/
 static void
-AccAXRsh1 (char *x)
+shiftL2Left2Result (operand * left, int offl,
+                   operand * result, int offr, int shCount)
 {
-  emitcode ("lsra", "");
-  emitcode ("ror", "%s", x);
-}
+  int i;
+  bool needpula = FALSE;
+  bool needpulx = FALSE;
 
+  if (!IS_AOP_XA (AOP (left)) && !IS_AOP_A (AOP (left)))
+    needpula = pushRegIfUsed (hc08_reg_a);
+  else
+    needpula = FALSE;
+  if (!IS_AOP_XA (AOP (left)))
+    needpulx = pushRegIfUsed (hc08_reg_x);
+  else
+    needpulx = FALSE;
 
-/*-----------------------------------------------------------------*/
-/* AccAXRshS1 - signed right shift s->a:x->c by 1                         */
-/*-----------------------------------------------------------------*/
-static void
-AccAXRshS1 (char *x)
-{
-  emitcode ("asra", "");
-  emitcode ("ror", "%s", x);
-}
+  loadRegFromAop (hc08_reg_xa, AOP (left), offl);
+
+  switch (shCount)
+    {
+      case 7:
+        rmwWithReg ("lsr", hc08_reg_x);
+        rmwWithReg ("ror", hc08_reg_a);
+        transferRegReg (hc08_reg_a, hc08_reg_x, FALSE);
+        rmwWithReg ("clr", hc08_reg_a);
+        rmwWithReg ("ror", hc08_reg_a);
+        break;
+      default:
+        for (i=0; i<shCount; i++)
+          {
+            rmwWithReg ("lsl", hc08_reg_a);
+            rmwWithReg ("rol", hc08_reg_x);
+          }
+    }
+  storeRegToAop (hc08_reg_xa, AOP (result), offl);
+
+  pullOrFreeReg (hc08_reg_x, needpulx);
+  pullOrFreeReg (hc08_reg_a, needpula);
 
-/*-----------------------------------------------------------------*/
-/* AccAXLrl1 - left rotate c<-a:x<-c by 1                          */
-/*-----------------------------------------------------------------*/
-static void
-AccAXLrl1 (char *x)
-{
-  emitcode ("rol", "%s", x);
-  emitcode ("rola", "");
 }
 
+
+#if 0
 /*-----------------------------------------------------------------*/
-/* AccAXLsh1 - left shift a:x<-0 by 1                              */
-/*-----------------------------------------------------------------*/
-static void
-AccAXLsh1 (char *x)
-{
-  emitcode ("lsl", "%s", x);
-  emitcode ("rola", "");
-}
-
-/*-----------------------------------------------------------------*/
-/* AccAXLsh - left shift a:x by known count (0..7)                 */
-/*-----------------------------------------------------------------*/
-static void
-AccAXLsh (char *x, int shCount)
-{
-  int i;
-
-  for (i=0;i<shCount;i++) {
-    AccAXLsh1 (x);
-  }
-}
-
-/*-----------------------------------------------------------------*/
-/* AccAXRsh - right shift a:x known count (0..7)                   */
-/*-----------------------------------------------------------------*/
-static void
-AccAXRsh (char *x, int shCount)
-{
-  int i;
-
-  for (i=0;i<shCount;i++) {
-    AccAXRsh1 (x);
-  }
-  
-}
-
-/*-----------------------------------------------------------------*/
-/* AccAXRshS - right shift signed a:x known count (0..7)           */
-/*-----------------------------------------------------------------*/
-static void
-AccAXRshS (char *x, int shCount)
-{
-  int i;
-
-  for (i=0;i<shCount;i++) {
-    AccAXRshS1 (x);
-  }
-}
-
-/*-----------------------------------------------------------------*/
-/* shiftL2Left2Result - shift left two bytes from left to result   */
-/*-----------------------------------------------------------------*/
-static void
-shiftL2Left2Result (operand * left, int offl,
-                   operand * result, int offr, int shCount)
-{
-  int i;
-  bool needpula = FALSE;
-  bool needpulx = FALSE;
-
-  needpula = pushRegIfUsed (hc08_reg_a);
-  needpulx = pushRegIfUsed (hc08_reg_x);
-
-  loadRegFromAop (hc08_reg_xa, AOP (left), offl);
-
-  switch (shCount)
-    {
-      case 7:
-        rmwWithReg ("lsr", hc08_reg_x);
-        rmwWithReg ("ror", hc08_reg_a);
-        transferRegReg (hc08_reg_a, hc08_reg_x, FALSE);
-        rmwWithReg ("clr", hc08_reg_a);
-        rmwWithReg ("ror", hc08_reg_a);
-        break;
-      default:
-        for (i=0; i<shCount; i++)
-          {
-            rmwWithReg ("lsl", hc08_reg_a);
-            rmwWithReg ("rol", hc08_reg_x);
-          }
-    }
-  storeRegToAop (hc08_reg_xa, AOP (result), offl);
-
-  pullOrFreeReg (hc08_reg_x, needpulx);
-  pullOrFreeReg (hc08_reg_a, needpula);
-
-}
-
-
-#if 0
-/*-----------------------------------------------------------------*/
-/* shiftR2Left2Result - shift right two bytes from left to result  */
+/* shiftR2Left2Result - shift right two bytes from left to result  */
 /*-----------------------------------------------------------------*/
 static void
 shiftR2Left2Result (operand * left, int offl,
@@ -5625,6 +5817,7 @@ genlshTwo (operand * result, operand * left, int shCount)
 
   D(emitcode (";     genlshTwo",""));
 
+  
   size = getDataSize (result);
 
   /* if shCount >= 8 */
@@ -5634,10 +5827,9 @@ genlshTwo (operand * result, operand * left, int shCount)
 
       if (size > 1)
        {
-         if (shCount)
-           shiftL1Left2Result (left, LSB, result, MSB16, shCount);
-         else
-           movLeft2Result (left, LSB, result, MSB16, 0);
+          loadRegFromAop (hc08_reg_a, AOP (left), 0);
+          AccLsh (shCount);
+         storeRegToAop (hc08_reg_a, AOP (result), 1);
        }
       storeConstToAop(zero, AOP (result), LSB);
     }
@@ -5645,10 +5837,9 @@ genlshTwo (operand * result, operand * left, int shCount)
   /*  1 <= shCount <= 7 */
   else
     {
-      if (size == 1)
-       shiftL1Left2Result (left, LSB, result, LSB, shCount);
-      else
-       shiftL2Left2Result (left, LSB, result, LSB, shCount);
+      loadRegFromAop (hc08_reg_xa, AOP (left), 0);
+      XAccLsh (shCount);
+      storeRegToFullAop (hc08_reg_xa, AOP (result), 0);
     }
 }
 
@@ -5811,8 +6002,8 @@ genLeftShiftLiteral (operand * left,
   size = AOP_SIZE (result);
 
 #if VIEW_SIZE
-  emitcode ("; shift left ", "result %d, left %d", size,
-           AOP_SIZE (left));
+  D(emitcode ("; shift left ", "result %d, left %d", size,
+           AOP_SIZE (left)));
 #endif
 
   if (shCount == 0)
@@ -6121,8 +6312,8 @@ genRightShiftLiteral (operand * left,
   aopOp (result, ic, FALSE);
 
 #if VIEW_SIZE
-  emitcode ("; shift right ", "result %d, left %d", AOP_SIZE (result),
-           AOP_SIZE (left));
+  D(emitcode ("; shift right ", "result %d, left %d", AOP_SIZE (result),
+           AOP_SIZE (left)));
 #endif
 
   size = getDataSize (left);
@@ -6253,11 +6444,12 @@ genRightShift (iCode * ic)
   freeAsmop (right, NULL, ic, TRUE);
 }
 
+
 /*-----------------------------------------------------------------*/
 /* genUnpackBits - generates code for unpacking bits               */
 /*-----------------------------------------------------------------*/
 static void
-genUnpackBits (operand * result)
+genUnpackBits (operand * result, iCode *ifx)
 {
   int offset = 0;      /* result byte offset */
   int rsize;           /* result size */
@@ -6278,9 +6470,17 @@ genUnpackBits (operand * result)
     {
       emitcode ("lda", ",x");
       hc08_dirtyReg (hc08_reg_a, FALSE);
-      AccRsh (bstr, FALSE);
-      emitcode ("and", "#0x%02x", ((unsigned char) -1) >> (8 - blen));
-      storeRegToAop (hc08_reg_a, AOP (result), offset++);
+      if (!ifx)
+        {
+          AccRsh (bstr, FALSE);
+          emitcode ("and", "#0x%02x", ((unsigned char) -1) >> (8 - blen));
+          storeRegToAop (hc08_reg_a, AOP (result), offset++);
+        }
+      else
+        {
+          emitcode ("and", "#0x%02x",
+                    (((unsigned char) -1) >> (8 - blen)) << bstr);
+        }
       goto finish;
     }
 
@@ -6290,7 +6490,9 @@ genUnpackBits (operand * result)
     {
       emitcode ("lda", ",x");
       hc08_dirtyReg (hc08_reg_a, FALSE);
-      storeRegToAop (hc08_reg_a, AOP (result), offset++);
+      if (!ifx)
+        storeRegToAop (hc08_reg_a, AOP (result), offset);
+      offset++;
       if (rlen>8)
         emitcode ("aix", "#1");
     }
@@ -6310,21 +6512,33 @@ finish:
       while (rsize--)
         storeConstToAop (zero, AOP (result), offset++);
     }
+  
+  if (ifx && !ifx->generated)
+    {
+      genIfxJump (ifx, "a");
+    }
 }
 
 
 /*-----------------------------------------------------------------*/
-/* genDataPointerGet - generates code when ptr offset is known     */
+/* genUnpackBitsImmed - generates code for unpacking bits          */
 /*-----------------------------------------------------------------*/
 static void
-genDataPointerGet (operand * left,
-                  operand * result,
-                  iCode * ic)
+genUnpackBitsImmed (operand * left,
+                    operand * result,
+                    iCode *ic,
+                    iCode *ifx)
 {
-  int size, offset = 0;
+  int size;
+  int offset = 0;      /* result byte offset */
+  int rsize;           /* result size */
+  int rlen = 0;                /* remaining bitfield length */
+  sym_link *etype;     /* bitfield type information */
+  int blen;            /* bitfield length */
+  int bstr;            /* bitfield starting bit within byte */
   asmop *derefaop;
   
-  D(emitcode (";     genDataPointerGet",""));
+  D(emitcode (";     genUnpackBitsImmed",""));
 
   aopOp (result, ic, TRUE);
   size = AOP_SIZE (result);
@@ -6333,116 +6547,187 @@ genDataPointerGet (operand * left,
   freeAsmop (left, NULL, ic, TRUE);
   derefaop->size = size;
   
-  while (size--)
+  etype = getSpec (operandType (result));
+  rsize = getSize (operandType (result));
+  blen = SPEC_BLEN (etype);
+  bstr = SPEC_BSTR (etype);
+
+  /* if the bitfield is a single bit in the direct page */
+  if (blen == 1 && derefaop->type == AOP_DIR)
+    {
+      if (!ifx && bstr)
+        {
+          symbol *tlbl = newiTempLabel (NULL);
+          
+          loadRegFromConst (hc08_reg_a, zero);
+          emitcode ("brclr", "#%d,%s,%05d$",
+                    bstr, aopAdrStr (derefaop, 0, FALSE),
+                    (tlbl->key + 100));
+          rmwWithReg ("inc", hc08_reg_a);
+          emitLabel (tlbl);
+          storeRegToAop (hc08_reg_a, AOP (result), offset);
+          hc08_freeReg (hc08_reg_a);
+          offset++;
+          goto finish;
+        }
+      else if (ifx)
+        {
+          symbol *tlbl = newiTempLabel (NULL);
+          symbol *jlbl;
+          char * inst;
+          
+          if (IC_TRUE (ifx))
+            {
+              jlbl = IC_TRUE (ifx);
+              inst = "brclr";
+            }
+          else
+            {
+              jlbl = IC_FALSE (ifx);
+              inst = "brset";
+            }
+          emitcode (inst, "#%d,%s,%05d$",
+                    bstr, aopAdrStr (derefaop, 0, FALSE),
+                    (tlbl->key + 100));
+          emitBranch ("jmp", jlbl);
+          emitLabel (tlbl);
+          ifx->generated = 1;
+          offset++;
+          goto finish;
+        }
+    }
+
+  /* If the bitfield length is less than a byte */
+  if (blen < 8)
     {
-      transferAopAop(derefaop, offset, AOP (result), offset);
+      loadRegFromAop (hc08_reg_a, derefaop, 0);
+      if (!ifx)
+        {
+          AccRsh (bstr, FALSE);
+          emitcode ("and", "#0x%02x", ((unsigned char) -1) >> (8 - blen));
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+          storeRegToAop (hc08_reg_a, AOP (result), offset);
+        }
+      else
+        {
+          emitcode ("and", "#0x%02x",
+                    (((unsigned char) -1) >> (8 - blen)) << bstr);
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+        }
       offset++;
+      goto finish;
+    }
+
+  /* Bit field did not fit in a byte. Copy all
+     but the partial byte at the end.  */
+  for (rlen=blen;rlen>=8;rlen-=8)
+    {
+      loadRegFromAop (hc08_reg_a, derefaop, size-offset);
+      if (!ifx)
+        storeRegToAop (hc08_reg_a, AOP (result), offset);
+      else
+        emitcode ("tsta", "");
+      offset++;
+    }
+
+  /* Handle the partial byte at the end */
+  if (rlen)
+    {
+      loadRegFromAop (hc08_reg_a, derefaop, size-offset);
+      emitcode ("and", "#0x%02x", ((unsigned char) -1) >> (8-rlen));
+      storeRegToAop (hc08_reg_a, AOP (result), offset++);
     }
 
+finish:
+  if (offset < rsize)
+    {
+      rsize -= offset;
+      while (rsize--)
+        storeConstToAop (zero, AOP (result), offset++);
+    }
+  
   freeAsmop (NULL, derefaop, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
+  
+  if (ifx && !ifx->generated)
+    {
+      genIfxJump (ifx, "a");
+    }
 }
 
+
 /*-----------------------------------------------------------------*/
-/* genNearPointerGet - emitcode for near pointer fetch             */
+/* genDataPointerGet - generates code when ptr offset is known     */
 /*-----------------------------------------------------------------*/
 static void
-genNearPointerGet (operand * left,
+genDataPointerGet (operand * left,
                   operand * result,
                   iCode * ic,
-                  iCode * pi)
+                   iCode * ifx)
 {
-  int size, offset;
-  sym_link *retype = getSpec (operandType (result));
-
-  D(emitcode (";     genNearPointerGet",""));
-
-  aopOp (left, ic, FALSE);
-
-  /* if left is rematerialisable and
-     result is not bit variable type and
-     the left is pointer to data space i.e
-     lower 128 bytes of space */
-  if ((AOP_TYPE (left) == AOP_IMMD)
-      || (AOP_TYPE (left) == AOP_LIT) 
-      /* !IS_BITVAR (retype) */
-      /* && DCL_TYPE (ltype) == POINTER */ )
-    {
-      genDataPointerGet (left, result, ic);
-      return;
-    }
-
-  /* if the operand is already in hx
-     then we do nothing else we move the value to hx */
-  if (AOP_TYPE (left) != AOP_STR)
-    {
-      /* if this is remateriazable */
-      loadRegFromAop (hc08_reg_x, AOP (left), 0);
-      loadRegFromConst (hc08_reg_h, zero);
-    }
+  int size, offset = 0;
+  asmop *derefaop;
+  
+  D(emitcode (";     genDataPointerGet",""));
 
-  /* so hx now contains the address */
-  aopOp (result, ic, FALSE);
+  aopOp (result, ic, TRUE);
+  size = AOP_SIZE (result);
 
-  /* if bit then unpack */
-  if (IS_BITVAR (retype))
-    genUnpackBits (result);
-  else
+  derefaop = aopDerefAop (AOP (left));
+  freeAsmop (left, NULL, ic, TRUE);
+  derefaop->size = size;
+  
+  while (size--)
     {
-      size = AOP_SIZE (result);
-      offset = size-1;
-
-      while (size--)
-       {
-         accopWithMisc ("lda", ",x");
-         if (size || pi)
-            {
-              rmwWithReg ("inc", hc08_reg_x);
-            }
-          storeRegToAop (hc08_reg_a, AOP (result), offset--);
-          hc08_freeReg (hc08_reg_a);
-       }
+      if (!ifx)
+        transferAopAop (derefaop, offset, AOP (result), offset);
+      else
+        loadRegFromAop (hc08_reg_a, derefaop, offset);
+      offset++;
     }
 
-  freeAsmop (left, NULL, ic, TRUE);
+  freeAsmop (NULL, derefaop, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
   
-  if (pi /* && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR */) {
-    aopOp (IC_RESULT (pi), pi, FALSE);
-    storeRegToAop (hc08_reg_x, AOP (IC_RESULT (pi)), 0);
-    freeAsmop (IC_RESULT (pi), NULL, pi, TRUE);
-    pi->generated = 1;
-  }
-
-  hc08_freeReg (hc08_reg_hx);
-  
+  if (ifx && !ifx->generated)
+    {
+      genIfxJump (ifx, "a");
+    }
 }
 
 
 /*-----------------------------------------------------------------*/
-/* genFarPointerGet - get value from far space                     */
+/* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
 static void
-genFarPointerGet (operand * left,
-                 operand * result, iCode * ic, iCode * pi)
+genPointerGet (iCode * ic, iCode *pi, iCode *ifx)
 {
+  operand *left = IC_LEFT (ic);
+  operand *result = IC_RESULT (ic);
   int size, offset;
   sym_link *retype = getSpec (operandType (result));
 
-  D(emitcode (";     genFarPointerGet",""));
+  D(emitcode (";     genPointerGet",""));
 
+  if (getSize (operandType (result))>1)
+    ifx = NULL;
+  
   aopOp (left, ic, FALSE);
 
   /* if left is rematerialisable and
-     result is not bit variable type and
-     the left is pointer to data space i.e
-     lower 128 bytes of space */
-  if (AOP_TYPE (left) == AOP_IMMD &&
-      !IS_BITVAR (retype)
-      /* && DCL_TYPE (ltype) == POINTER */ )
-    {
-      genDataPointerGet (left, result, ic);
-      return;
+     result is not bit variable type */
+  if (AOP_TYPE (left) == AOP_IMMD || AOP_TYPE (left) == AOP_LIT)
+    {
+      if (!IS_BITVAR (retype))
+        {
+          genDataPointerGet (left, result, ic, ifx);
+          return;
+        }
+      else
+        {
+          genUnpackBitsImmed (left, result, ic, ifx);
+          return;
+        }
     }
 
   /* if the operand is already in hx
@@ -6458,7 +6743,7 @@ genFarPointerGet (operand * left,
 
   /* if bit then unpack */
   if (IS_BITVAR (retype))
-    genUnpackBits (result);
+    genUnpackBits (result, ifx);
   else
     {
       size = AOP_SIZE (result);
@@ -6472,7 +6757,9 @@ genFarPointerGet (operand * left,
              emitcode ("aix", "#1");
              hc08_dirtyReg (hc08_reg_hx, FALSE);
             }
-          storeRegToAop (hc08_reg_a, AOP (result), offset--);
+          if (!ifx)
+            storeRegToAop (hc08_reg_a, AOP (result), offset);
+          offset--;
           hc08_freeReg (hc08_reg_a);
        }
     }
@@ -6480,82 +6767,153 @@ genFarPointerGet (operand * left,
   freeAsmop (left, NULL, ic, TRUE);
   freeAsmop (result, NULL, ic, TRUE);
   
-  if (pi /* && AOP_TYPE (left) != AOP_IMMD && AOP_TYPE (left) != AOP_STR */) {
+  if (pi) {
     aopOp (IC_RESULT (pi), pi, FALSE);
     storeRegToAop (hc08_reg_hx, AOP (IC_RESULT (pi)), 0);
     freeAsmop (IC_RESULT (pi), NULL, pi, TRUE);
     pi->generated = 1;
   }
+  
+  if (ifx && !ifx->generated)
+    {
+      genIfxJump (ifx, "a");
+    }
 
   hc08_freeReg (hc08_reg_hx);
   
 }
 
-
-
 /*-----------------------------------------------------------------*/
-/* genPointerGet - generate code for pointer get                   */
+/* genPackBits - generates code for packed bit storage             */
 /*-----------------------------------------------------------------*/
 static void
-genPointerGet (iCode * ic, iCode *pi)
+genPackBits (sym_link * etype,
+            operand * right)
 {
-  operand *left, *result;
-  sym_link *type, *etype;
-  int p_type;
+  int offset = 0;      /* source byte offset */
+  int rlen = 0;                /* remaining bitfield length */
+  int blen;            /* bitfield length */
+  int bstr;            /* bitfield starting bit within byte */
+  int litval;          /* source literal value (if AOP_LIT) */
+  unsigned char mask;  /* bitmask within current byte */
+  int xoffset = 0;
 
-  D(emitcode (";     genPointerGet",""));
+  D(emitcode (";     genPackBits",""));
 
-  left = IC_LEFT (ic);
-  result = IC_RESULT (ic);
+  blen = SPEC_BLEN (etype);
+  bstr = SPEC_BSTR (etype);
 
-  /* depending on the type of pointer we need to
-     move it to the correct pointer register */
-  type = operandType (left);
-  etype = getSpec (type);
-  /* if left is of type of pointer then it is simple */
-  if (IS_PTR (type) && !IS_FUNC (type->next))
-    p_type = DCL_TYPE (type);
-  else
+  /* If the bitfield length is less than a byte */
+  if (blen < 8)
     {
-      /* we have to go by the storage class */
-      p_type = PTR_TYPE (SPEC_OCLS (etype));
+      mask = ((unsigned char) (0xFF << (blen + bstr)) |
+             (unsigned char) (0xFF >> (8 - bstr)));
+
+      if (AOP_TYPE (right) == AOP_LIT)
+        {
+          /* Case with a bitfield length <8 and literal source
+          */
+          litval = (int) floatFromVal (AOP (right)->aopu.aop_lit);
+          litval <<= bstr;
+          litval &= (~mask) & 0xff;
+
+          emitcode ("lda", ",x");
+          if ((mask|litval)!=0xff)
+            emitcode ("and","#0x%02x", mask);
+          if (litval)
+            emitcode ("ora","#0x%02x", litval);
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+          emitcode ("sta", ",x");
+          
+          hc08_freeReg (hc08_reg_a);
+          return;
+        }
+          
+      /* Case with a bitfield length < 8 and arbitrary source
+      */
+      loadRegFromAop (hc08_reg_a, AOP (right), 0);
+      /* shift and mask source value */
+      AccLsh (bstr);
+      emitcode ("and", "#0x%02x", (~mask) & 0xff);
+      hc08_dirtyReg (hc08_reg_a, FALSE);
+      pushReg (hc08_reg_a, TRUE);
+
+      emitcode ("lda", ",x");
+      emitcode ("and", "#0x%02x", mask);
+      emitcode ("ora", "1,s");
+      emitcode ("sta", ",x");
+      pullReg (hc08_reg_a);
+     
+      hc08_freeReg (hc08_reg_a);
+      return;
     }
 
-  /* special case when cast remat */
-  if (p_type == GPOINTER && OP_SYMBOL(left)->remat &&
-      IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode)) {
-         left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
-         type = operandType (left);
-         p_type = DCL_TYPE (type);
-  }
-  /* now that we have the pointer type we assign
-     the pointer values */
-  switch (p_type)
+  /* Bit length is greater than 7 bits. In this case, copy  */
+  /* all except the partial byte at the end                 */
+  for (rlen=blen;rlen>=8;rlen-=8)
     {
+      if (AOP (right)->type == AOP_DIR)
+        {
+          emitcode ("mov", "%s,x+", aopAdrStr(AOP (right), offset, FALSE));
+         xoffset++;
+        }
+      else
+        {
+          loadRegFromAop (hc08_reg_a, AOP (right), offset);
+          emitcode ("sta", "%d,x", offset);
+        }
+      offset++;
+    }
 
-    case POINTER:
-    case IPOINTER:
-#if 0
-      genNearPointerGet (left, result, ic, pi);
-      break;
-#endif
-    case GPOINTER:
-    case CPOINTER:
-    case FPOINTER:
-      genFarPointerGet (left, result, ic, pi);
-      break;
+  /* If there was a partial byte at the end */
+  if (rlen)
+    {
+      mask = (((unsigned char) -1 << rlen) & 0xff);
+      
+      if (AOP_TYPE (right) == AOP_LIT)
+        {
+          /* Case with partial byte and literal source
+          */
+          litval = (int) floatFromVal (AOP (right)->aopu.aop_lit);
+          litval >>= (blen-rlen);
+          litval &= (~mask) & 0xff;
+          emitcode ("lda", "%d,x", offset - xoffset);
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+          if ((mask|litval)!=0xff)
+            emitcode ("and","#0x%02x", mask);
+          if (litval)
+            emitcode ("ora","#0x%02x", litval);
+          emitcode ("sta", "%d,x", offset - xoffset);
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+          hc08_freeReg (hc08_reg_a);
+          return;
+        }
+      
+      /* Case with partial byte and arbitrary source
+      */
+      loadRegFromAop (hc08_reg_a, AOP (right), offset);
+      emitcode ("and", "#0x%02x", (~mask) & 0xff);
+      hc08_dirtyReg (hc08_reg_a, FALSE);
+      pushReg (hc08_reg_a, TRUE);
 
+      emitcode ("lda", "%d,x", offset - xoffset);
+      emitcode ("and", "#0x%02x", mask);
+      emitcode ("ora", "1,s");
+      emitcode ("sta", "%d,x", offset - xoffset);
+      pullReg (hc08_reg_a);
     }
 
+  hc08_freeReg (hc08_reg_a);
 }
 
 /*-----------------------------------------------------------------*/
 /* genPackBits - generates code for packed bit storage             */
 /*-----------------------------------------------------------------*/
 static void
-genPackBits (sym_link * etype,
-            operand * right)
+genPackBitsImmed (operand *result, sym_link * etype, operand * right, iCode * ic)
 {
+  asmop *derefaop;
+  int size;
   int offset = 0;      /* source byte offset */
   int rlen = 0;                /* remaining bitfield length */
   int blen;            /* bitfield length */
@@ -6563,11 +6921,46 @@ genPackBits (sym_link * etype,
   int litval;          /* source literal value (if AOP_LIT) */
   unsigned char mask;  /* bitmask within current byte */
 
-  D(emitcode (";     genPackBits",""));
+  D(emitcode (";     genPackBitsImmed",""));
 
   blen = SPEC_BLEN (etype);
   bstr = SPEC_BSTR (etype);
-  
+
+  aopOp (right, ic, FALSE);
+  size = AOP_SIZE (right);
+
+  derefaop = aopDerefAop (AOP (result));
+  freeAsmop (result, NULL, ic, TRUE);
+  derefaop->size = size;
+
+  /* if the bitfield is a single bit in the direct page */
+  if (blen == 1 && derefaop->type == AOP_DIR)
+    {
+      if (AOP_TYPE (right) == AOP_LIT)
+        {
+          litval = (int) floatFromVal (AOP (right)->aopu.aop_lit);
+      
+          emitcode ((litval & 1) ? "bset" : "bclr", 
+                    "#%d,%s", bstr, aopAdrStr (derefaop, 0, FALSE));
+        }
+      else
+        {
+          symbol *tlbl1 = newiTempLabel (NULL);
+          symbol *tlbl2 = newiTempLabel (NULL);
+          
+          loadRegFromAop (hc08_reg_a, AOP (right), 0);
+          emitcode ("bit", "#1");
+          emitBranch ("bne", tlbl1);
+          emitcode ("bclr", "#%d,%s", bstr, aopAdrStr (derefaop, 0, FALSE));
+          emitBranch ("bra", tlbl2);
+          emitLabel (tlbl1);
+          emitcode ("bset", "#%d,%s", bstr, aopAdrStr (derefaop, 0, FALSE));
+          emitLabel (tlbl2);
+          hc08_freeReg (hc08_reg_a);
+        }
+      goto release;
+    }
+    
   /* If the bitfield length is less than a byte */
   if (blen < 8)
     {
@@ -6582,16 +6975,16 @@ genPackBits (sym_link * etype,
           litval <<= bstr;
           litval &= (~mask) & 0xff;
 
-          emitcode ("lda", ",x");
+          loadRegFromAop (hc08_reg_a, derefaop, 0);
           if ((mask|litval)!=0xff)
             emitcode ("and","#0x%02x", mask);
           if (litval)
             emitcode ("ora","#0x%02x", litval);
           hc08_dirtyReg (hc08_reg_a, FALSE);
-          emitcode ("sta", ",x");
+          storeRegToAop (hc08_reg_a, derefaop, 0);
           
           hc08_freeReg (hc08_reg_a);
-          return;
+          goto release;
         }
           
       /* Case with a bitfield length < 8 and arbitrary source
@@ -6603,29 +6996,21 @@ genPackBits (sym_link * etype,
       hc08_dirtyReg (hc08_reg_a, FALSE);
       pushReg (hc08_reg_a, TRUE);
 
-      emitcode ("lda", ",x");
+      loadRegFromAop (hc08_reg_a, derefaop, 0);
       emitcode ("and", "#0x%02x", mask);
       emitcode ("ora", "1,s");
-      emitcode ("sta", ",x");
+      storeRegToAop (hc08_reg_a, derefaop, 0);
       pullReg (hc08_reg_a);
      
       hc08_freeReg (hc08_reg_a);
-      return;
+      goto release;
     }
 
   /* Bit length is greater than 7 bits. In this case, copy  */
   /* all except the partial byte at the end                 */
   for (rlen=blen;rlen>=8;rlen-=8)
     {
-      if (AOP (right)->type == AOP_DIR)
-        {
-          emitcode ("mov", "%s,x+", aopAdrStr(AOP (right), offset, FALSE));
-        }
-      else
-        {
-          loadRegFromAop (hc08_reg_a, AOP (right), offset);
-          emitcode ("sta", "%d,x", offset);
-        }
+      transferAopAop (AOP (right), offset, derefaop, size-offset);
       offset++;
     }
 
@@ -6641,32 +7026,37 @@ genPackBits (sym_link * etype,
           litval = (int) floatFromVal (AOP (right)->aopu.aop_lit);
           litval >>= (blen-rlen);
           litval &= (~mask) & 0xff;
-          emitcode ("lda", "%d,x", offset);
-          hc08_dirtyReg (hc08_reg_a, FALSE);
+          loadRegFromAop (hc08_reg_a, derefaop, size-offset);
           if ((mask|litval)!=0xff)
             emitcode ("and","#0x%02x", mask);
           if (litval)
             emitcode ("ora","#0x%02x", litval);
-          emitcode ("sta", "%d,x", offset);
+          hc08_dirtyReg (hc08_reg_a, FALSE);
+          storeRegToAop (hc08_reg_a, derefaop, size-offset);
           hc08_dirtyReg (hc08_reg_a, FALSE);
           hc08_freeReg (hc08_reg_a);
-          return;
+          goto release;
         }
       
       /* Case with partial byte and arbitrary source
       */
-      loadRegFromAop (hc08_reg_a, AOP (right), offset++);
+      loadRegFromAop (hc08_reg_a, AOP (right), offset);
       emitcode ("and", "#0x%02x", (~mask) & 0xff);
       hc08_dirtyReg (hc08_reg_a, FALSE);
       pushReg (hc08_reg_a, TRUE);
 
-      emitcode ("lda", ",x");
+      loadRegFromAop (hc08_reg_a, derefaop, size-offset);
       emitcode ("and", "#0x%02x", mask);
       emitcode ("ora", "1,s");
-      emitcode ("sta", ",x");
+      storeRegToAop (hc08_reg_a, derefaop, size-offset);
+      pullReg (hc08_reg_a);
     }
 
   hc08_freeReg (hc08_reg_a);
+
+release:  
+  freeAsmop (right, NULL, ic, TRUE);
+  freeAsmop (NULL, derefaop, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -6699,103 +7089,40 @@ genDataPointerSet (operand * right,
   freeAsmop (NULL, derefaop, ic, TRUE);
 }
 
-/*-----------------------------------------------------------------*/
-/* genNearPointerSet - emitcode for near pointer put                */
-/*-----------------------------------------------------------------*/
-static void
-genNearPointerSet (operand * right,
-                  operand * result,
-                  iCode * ic,
-                  iCode * pi)
-{
-  int size, offset;
-  sym_link *retype = getSpec (operandType (right));
-  sym_link *letype = getSpec (operandType (result));
-
-  D(emitcode (";     genNearPointerSet",""));
-
-  aopOp (result, ic, FALSE);
-
-  /* if the result is rematerializable &
-     in data space & not a bit variable */
-  if (AOP_TYPE (result) == AOP_IMMD &&
-      /* DCL_TYPE (ptype) == POINTER && */
-      !IS_BITVAR (retype) &&
-      !IS_BITVAR (letype))
-    {
-      genDataPointerSet (right, result, ic);
-      return;
-    }
-
-  /* if the operand is already in hx
-     then we do nothing else we move the value to hx */
-  if (AOP_TYPE (result) != AOP_STR)
-    {
-      loadRegFromAop (hc08_reg_x, AOP (result), 0);
-      loadRegFromConst (hc08_reg_h, zero);
-    }
-  /* so hx now contains the address */
-  aopOp (right, ic, FALSE);
-
-  /* if bit then unpack */
-  if (IS_BITVAR (retype) || IS_BITVAR (letype))
-    genPackBits ((IS_BITVAR (retype) ? retype : letype), right);
-  else
-    {
-      size = AOP_SIZE (right);
-      offset = size-1;
-
-      while (size--)
-       {
-          loadRegFromAop (hc08_reg_a, AOP (right), offset--);
-         accopWithMisc ("sta", ",x");
-         if (size || pi)
-           {
-             rmwWithReg ("inc", hc08_reg_x);
-           }
-          hc08_freeReg (hc08_reg_a);
-       }
-    }
-
-  freeAsmop (result, NULL, ic, TRUE);
-  freeAsmop (right, NULL, ic, TRUE);
-
-  if (pi /* && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD */) {
-    aopOp (IC_RESULT (pi), pi, FALSE);
-    storeRegToAop (hc08_reg_x, AOP (IC_RESULT (pi)), 0);
-    freeAsmop (IC_RESULT (pi), NULL, pi, TRUE);
-    pi->generated=1;
-  }
-
-  hc08_freeReg (hc08_reg_hx);
-  
-}
-
 
 /*-----------------------------------------------------------------*/
-/* genFarPointerSet - set value from far space                     */
+/* genPointerSet - stores the value into a pointer location        */
 /*-----------------------------------------------------------------*/
 static void
-genFarPointerSet (operand * right,
-                 operand * result, iCode * ic, iCode * pi)
+genPointerSet (iCode * ic, iCode *pi)
 {
+  operand *right = IC_RIGHT (ic);
+  operand *result = IC_RESULT (ic);
+  sym_link *type, *etype;
   int size, offset;
   sym_link *retype = getSpec (operandType (right));
   sym_link *letype = getSpec (operandType (result));
 
-  D(emitcode (";     genFarPointerSet",""));
+  D(emitcode (";     genPointerSet",""));
 
+  type = operandType (result);
+  etype = getSpec (type);
+  
   aopOp (result, ic, FALSE);
 
-  /* if the result is rematerializable &
-     in data space & not a bit variable */
-  if (AOP_TYPE (result) == AOP_IMMD &&
-      /* DCL_TYPE (ptype) == POINTER && */
-      !IS_BITVAR (retype) &&
-      !IS_BITVAR (letype))
+  /* if the result is rematerializable */
+  if (AOP_TYPE (result) == AOP_IMMD || AOP_TYPE (result) == AOP_LIT)
     {
-      genDataPointerSet (right, result, ic);
-      return;
+      if (!IS_BITVAR (retype) && !IS_BITVAR (letype))
+        {
+          genDataPointerSet (right, result, ic);
+          return;
+        }
+      else
+        {
+          genPackBitsImmed (result, (IS_BITVAR (retype) ? retype : letype), right, ic);
+          return;
+        }
     }
 
   /* if the operand is already in hx
@@ -6830,7 +7157,7 @@ genFarPointerSet (operand * right,
   freeAsmop (result, NULL, ic, TRUE);
   freeAsmop (right, NULL, ic, TRUE);
 
-  if (pi /* && AOP_TYPE (result) != AOP_STR && AOP_TYPE (result) != AOP_IMMD  */) {
+  if (pi) {
     aopOp (IC_RESULT (pi), pi, FALSE);
     storeRegToAop (hc08_reg_hx, AOP (IC_RESULT (pi)), 0);
     freeAsmop (IC_RESULT (pi), NULL, pi, TRUE);
@@ -6838,69 +7165,6 @@ genFarPointerSet (operand * right,
   }
 
   hc08_freeReg (hc08_reg_hx);
-  
-
-}
-
-
-/*-----------------------------------------------------------------*/
-/* genPointerSet - stores the value into a pointer location        */
-/*-----------------------------------------------------------------*/
-static void
-genPointerSet (iCode * ic, iCode *pi)
-{
-  operand *right, *result;
-  sym_link *type, *etype;
-  int p_type;
-
-  D(emitcode (";     genPointerSet",""));
-
-  right = IC_RIGHT (ic);
-  result = IC_RESULT (ic);
-
-  /* depending on the type of pointer we need to
-     move it to the correct pointer register */
-  type = operandType (result);
-  etype = getSpec (type);
-  /* if left is of type of pointer then it is simple */
-  if (IS_PTR (type) && !IS_FUNC (type->next))
-    {
-      p_type = DCL_TYPE (type);
-    }
-  else
-    {
-      /* we have to go by the storage class */
-      p_type = PTR_TYPE (SPEC_OCLS (etype));
-    }
-
-  /* special case when cast remat */
-  if (p_type == GPOINTER && OP_SYMBOL(result)->remat &&
-      IS_CAST_ICODE(OP_SYMBOL(result)->rematiCode)) {
-         result = IC_RIGHT(OP_SYMBOL(result)->rematiCode);
-         type = operandType (result);
-         p_type = DCL_TYPE (type);
-  }
-  /* now that we have the pointer type we assign
-     the pointer values */
-  switch (p_type)
-    {
-
-    case POINTER:
-    case IPOINTER:
-#if 0
-      genNearPointerSet (right, result, ic, pi);
-      break;
-#endif
-
-    case GPOINTER:
-    case FPOINTER:
-      genFarPointerSet (right, result, ic, pi);
-      break;
-
-    default:
-      werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
-             "genPointerSet: illegal pointer type");
-    }
 
 }
 
@@ -7067,6 +7331,7 @@ genJumpTab (iCode * ic)
   emitcode ("add","1,s");
   transferRegReg (hc08_reg_a, hc08_reg_x, TRUE);
   loadRegFromConst (hc08_reg_h, zero);
+  pullReg (hc08_reg_a);
 
   jtab = newiTempLabel (NULL);
   emitcode ("jmp", "%05d$,x", jtab->key + 100);
@@ -7250,6 +7515,10 @@ genDjnz (iCode * ic, iCode * ifx)
   if (operandLitValue (IC_RIGHT (ic)) != 1)
     return 0;
 
+  /* dbnz doesn't support extended mode */
+  if (isOperandInFarSpace (IC_RESULT (ic)))
+    return 0;
+
   /* if the size of this greater than one then no
      saving */
 //  if (getSize (operandType (IC_RESULT (ic))) > 1)
@@ -7306,6 +7575,105 @@ genReceive (iCode * ic)
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
 }
 
+/*-----------------------------------------------------------------*/
+/* genDummyRead - generate code for dummy read of volatiles        */
+/*-----------------------------------------------------------------*/
+static void
+genDummyRead (iCode * ic)
+{
+  operand *op;
+  int size, offset;
+
+  D(emitcode(";     genDummyRead",""));
+
+  op = IC_RIGHT (ic);
+  if (op && IS_SYMOP (op))
+    {
+
+      aopOp (op, ic, FALSE);
+
+      size = AOP_SIZE (op);
+      offset = 0;
+
+      while (size--)
+        {
+          loadRegFromAop (hc08_reg_a, AOP (op), offset);
+          hc08_freeReg (hc08_reg_a);
+          offset++;
+        }
+
+      freeAsmop (op, NULL, ic, TRUE);
+   }
+  op = IC_LEFT (ic);
+  if (op && IS_SYMOP (op))
+    {
+
+      aopOp (op, ic, FALSE);
+
+      size = AOP_SIZE (op);
+      offset = 0;
+
+      while (size--)
+        {
+          loadRegFromAop (hc08_reg_a, AOP (op), offset);
+          hc08_freeReg (hc08_reg_a);
+          offset++;
+        }
+
+      freeAsmop (op, NULL, ic, TRUE);
+   }
+}
+
+/*-----------------------------------------------------------------*/
+/* genCritical - generate code for start of a critical sequence    */
+/*-----------------------------------------------------------------*/
+static void
+genCritical (iCode *ic)
+{
+  D(emitcode(";     genCritical",""));
+  
+  if (IC_RESULT (ic))
+    aopOp (IC_RESULT (ic), ic, TRUE);
+
+  emitcode ("tpa", "");
+  hc08_dirtyReg (hc08_reg_a, FALSE);
+  emitcode ("sei", "");
+
+  if (IC_RESULT (ic))
+    storeRegToAop (hc08_reg_a, AOP (IC_RESULT (ic)), 0);
+  else
+    pushReg (hc08_reg_a, FALSE);
+
+  hc08_freeReg (hc08_reg_a);
+  if (IC_RESULT (ic))
+    freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
+}
+
+/*-----------------------------------------------------------------*/
+/* genEndCritical - generate code for end of a critical sequence   */
+/*-----------------------------------------------------------------*/
+static void
+genEndCritical (iCode *ic)
+{
+  D(emitcode(";     genEndCritical",""));
+  
+  if (IC_RIGHT (ic))
+    {
+      aopOp (IC_RIGHT (ic), ic, FALSE);
+      loadRegFromAop (hc08_reg_a, AOP (IC_RIGHT (ic)), 0);
+      emitcode ("tap", "");
+      hc08_freeReg (hc08_reg_a);
+      freeAsmop (IC_RIGHT (ic), NULL, ic, TRUE);
+    }
+  else
+    {
+      pullReg (hc08_reg_a);
+      emitcode ("tap", "");
+    }
+}
+
+
+
 /*-----------------------------------------------------------------*/
 /* genhc08Code - generate code for HC08 based controllers          */
 /*-----------------------------------------------------------------*/
@@ -7314,6 +7682,8 @@ genhc08Code (iCode * lic)
 {
   iCode *ic;
   int cln = 0;
+  int clevel = 0;
+  int cblock = 0;
 
   lineHead = lineCurr = NULL;
 
@@ -7323,19 +7693,23 @@ genhc08Code (iCode * lic)
   /* if debug information required */
   if (options.debug && currFunc)
     {
-      debugFile->writeFunction(currFunc);
+      debugFile->writeFunction (currFunc, lic);
+      #if 0
       _G.debugLine = 1;
       if (IS_STATIC (currFunc->etype))
        emitcode ("", "F%s$%s$0$0 ==.", moduleName, currFunc->name);
       else
        emitcode ("", "G$%s$0$0 ==.", currFunc->name);
       _G.debugLine = 0;
+      #endif
     }
   /* stack pointer name */
   if (options.useXstack)
     spname = "_spx";
   else
     spname = "sp";
+  
+  debugFile->writeFrameAddress (NULL, NULL, 0);  /* have no idea where frame is now */
 
   hc08_aop_pass[0] = newAsmop (AOP_REG);
   hc08_aop_pass[0]->size=1;
@@ -7352,16 +7726,31 @@ genhc08Code (iCode * lic)
 
   for (ic = lic; ic; ic = ic->next)
     {
-
+      
+      _G.current_iCode = ic;
+      
+      if (ic->level != clevel || ic->block != cblock)
+       {
+         if (options.debug)
+           {
+             debugFile->writeScope(ic);
+           }
+         clevel = ic->level;
+         cblock = ic->block;
+       }
+       
       if (ic->lineno && cln != ic->lineno)
        {
          if (options.debug)
            {
+             debugFile->writeCLine(ic);
+             #if 0
              _G.debugLine = 1;
              emitcode ("", "C$%s$%d$%d$%d ==.",
                        FileBaseName (ic->filename), ic->lineno,
                        ic->level, ic->block);
              _G.debugLine = 0;
+             #endif
            }
          if (!options.noCcodeInAsm) {
            emitcode ("", ";%s:%d: %s", ic->filename, ic->lineno, 
@@ -7583,7 +7972,10 @@ genhc08Code (iCode * lic)
          break;
 
        case GET_VALUE_AT_ADDRESS:
-         genPointerGet (ic, hasInc(IC_LEFT(ic),ic,getSize(operandType(IC_RESULT(ic)))));
+         genPointerGet (ic,
+                         hasInc (IC_LEFT (ic), ic,
+                                 getSize (operandType (IC_RESULT (ic)))),
+                         ifxForOp (IC_RESULT (ic), ic) );
          break;
 
        case '=':
@@ -7617,22 +8009,40 @@ genhc08Code (iCode * lic)
          addSet (&_G.sendSet, ic);
          break;
 
+       case DUMMY_READ_VOLATILE:
+         genDummyRead (ic);
+         break;
+
+       case CRITICAL:
+         genCritical (ic);
+         break;
+
+       case ENDCRITICAL:
+         genEndCritical (ic);
+         break;
+       
+        case SWAP:
+         genSwap (ic);
+          break;
+
        default:
          ic = ic;
        }
 
       if (!hc08_reg_a->isFree)
-        emitcode("","; forgot to free a");
+        D(emitcode("","; forgot to free a"));
       if (!hc08_reg_x->isFree)
-        emitcode("","; forgot to free x");
+        D(emitcode("","; forgot to free x"));
       if (!hc08_reg_h->isFree)
-        emitcode("","; forgot to free h");
+        D(emitcode("","; forgot to free h"));
       if (!hc08_reg_hx->isFree)
-        emitcode("","; forgot to free hx");
+        D(emitcode("","; forgot to free hx"));
       if (!hc08_reg_xa->isFree)
-        emitcode("","; forgot to free xa");
+        D(emitcode("","; forgot to free xa"));
     }
 
+  debugFile->writeFrameAddress (NULL, NULL, 0);  /* have no idea where frame is now */
+    
 
   /* now we are ready to call the
      peep hole optimizer */