* as/link/mcs51/lkarea.c (lnkarea2): handle absolute areas, restructured
[fw/sdcc] / src / avr / gen.c
index 8954f2131ae9133cf3c7b062aa1160adf35a3fc0..59dbabcebc9e0eddf6839aaa1a806be213999ade 100644 (file)
@@ -1,5 +1,5 @@
 /*-------------------------------------------------------------------------
-  avrgen.c - source file for code generation for ATMEL AVR
+  gen.c - source file for code generation for ATMEL AVR
 
   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (2000)
 
 #include "SDCCglobl.h"
 #include "newalloc.h"
 
-#ifdef HAVE_SYS_ISA_DEFS_H
-#include <sys/isa_defs.h>
-#else
-#ifdef HAVE_ENDIAN_H
-#include <endian.h>
-#else
-#if !defined(__BORLANDC__) && !defined(_MSC_VER)
-#warning "Cannot determine ENDIANESS of this machine assuming LITTLE_ENDIAN"
-#warning "If you running sdcc on an INTEL 80x86 Platform you are okay"
-#endif
-#endif
-#endif
-
 #include "common.h"
 #include "SDCCpeeph.h"
 #include "ralloc.h"
@@ -64,9 +51,8 @@ static char *spname;
 char *fReturnAVR[] = { "r16", "r17", "r18", "r19" };
 unsigned fAVRReturnSize = 4;   /* shared with ralloc.c */
 char **fAVRReturn = fReturnAVR;
-static short rbank = -1;
-static char *larray[4] = { "lo8", "hi8", "hlo8", "hhi8" };
-static char *tscr[4] = { "r0", "r1", "r24", "r25" };
+static char *larray[4] = { ">", "<", "hlo8", "hhi8" };
+
 static struct {
        short xPushed;
        short zPushed;
@@ -80,12 +66,13 @@ static struct {
 extern int avr_ptrRegReq;
 extern int avr_nRegs;
 extern FILE *codeOutFile;
-static void saverbank (int, iCode *, bool);
 #define RESULTONSTACK(x) \
                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
                          IC_RESULT(x)->aop->type == AOP_STK )
 
 #define MOVR0(x) if (strcmp(x,"r0")) emitcode("mov","r0,%s",x);
+#define MOVR24(x) if (strcmp(x,"r24")) emitcode("mov","r24,%s",x);
+#define AOP_ISHIGHREG(a,n) (a->type == AOP_REG && a->aopu.aop_reg[n] && a->aopu.aop_reg[n]->rIdx >= R16_IDX)
 #define CLRC    emitcode("clc","")
 #define SETC    emitcode("stc","")
 #define MOVA(x)
@@ -94,18 +81,152 @@ static void saverbank (int, iCode *, bool);
 static lineNode *lineHead = NULL;
 static lineNode *lineCurr = NULL;
 
-static unsigned char SLMask[] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0,
-       0xE0, 0xC0, 0x80, 0x00
-};
-static unsigned char SRMask[] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F,
-       0x07, 0x03, 0x01, 0x00
-};
-
 #define LSB     0
 #define MSB16   1
 #define MSB24   2
 #define MSB32   3
 
+#if 0
+// PENDING: Unused.
+/*-----------------------------------------------------------------*/
+/* reAdjustPreg - points a register back to where it should        */
+/*-----------------------------------------------------------------*/
+static void
+reAdjustPreg (asmop * aop)
+{
+       int size;
+
+       aop->coff = 0;
+       if ((size = aop->size) <= 1)
+               return;
+       size--;
+       switch (aop->type) {
+       case AOP_X:
+       case AOP_Z:
+               emitcode ("sbiw", "%s,%d", aop->aopu.aop_ptr->name, size);
+               break;
+       }
+
+}
+
+/*-----------------------------------------------------------------*/
+/* outBitC - output a bit C                                        */
+/*-----------------------------------------------------------------*/
+static void
+outBitC (operand * result)
+{
+       emitcode ("clr", "r0");
+       emitcode ("rol", "r0");
+       outAcc (result);
+}
+
+/*-----------------------------------------------------------------*/
+/* inExcludeList - return 1 if the string is in exclude Reg list   */
+/*-----------------------------------------------------------------*/
+static bool
+inExcludeList (char *s)
+{
+       int i = 0;
+
+       if (options.excludeRegs[i] &&
+           STRCASECMP (options.excludeRegs[i], "none") == 0)
+               return FALSE;
+
+       for (i = 0; options.excludeRegs[i]; i++) {
+               if (options.excludeRegs[i] &&
+                   STRCASECMP (s, options.excludeRegs[i]) == 0)
+                       return TRUE;
+       }
+       return FALSE;
+}
+
+/*-----------------------------------------------------------------*/
+/* findLabelBackwards: walks back through the iCode chain looking  */
+/* for the given label. Returns number of iCode instructions     */
+/* between that label and given ic.          */
+/* Returns zero if label not found.          */
+/*-----------------------------------------------------------------*/
+static int
+findLabelBackwards (iCode * ic, int key)
+{
+       int count = 0;
+
+       while (ic->prev) {
+               ic = ic->prev;
+               count++;
+
+               if (ic->op == LABEL && IC_LABEL (ic)->key == key) {
+                       /* printf("findLabelBackwards = %d\n", count); */
+                       return count;
+               }
+       }
+
+       return 0;
+}
+
+/*-----------------------------------------------------------------*/
+/* addSign - complete with sign                                    */
+/*-----------------------------------------------------------------*/
+static void
+addSign (operand * result, int offset, int sign)
+{
+       int size = (getDataSize (result) - offset);
+       if (size > 0) {
+               if (sign) {
+                       emitcode ("rlc", "a");
+                       emitcode ("subb", "a,acc");
+                       while (size--)
+                               aopPut (AOP (result), "a", offset++);
+               }
+               else
+                       while (size--)
+                               aopPut (AOP (result), zero, offset++);
+       }
+}
+
+/*-----------------------------------------------------------------*/
+/* isLiteralBit - test if lit == 2^n                               */
+/*-----------------------------------------------------------------*/
+static int
+isLiteralBit (unsigned long lit)
+{
+       unsigned long pw[32] = { 1L, 2L, 4L, 8L, 16L, 32L, 64L, 128L,
+               0x100L, 0x200L, 0x400L, 0x800L,
+               0x1000L, 0x2000L, 0x4000L, 0x8000L,
+               0x10000L, 0x20000L, 0x40000L, 0x80000L,
+               0x100000L, 0x200000L, 0x400000L, 0x800000L,
+               0x1000000L, 0x2000000L, 0x4000000L, 0x8000000L,
+               0x10000000L, 0x20000000L, 0x40000000L, 0x80000000L
+       };
+       int idx;
+
+       for (idx = 0; idx < 32; idx++)
+               if (lit == pw[idx])
+                       return idx + 1;
+       return 0;
+}
+
+/*-----------------------------------------------------------------*/
+/* outAcc - output Acc                                             */
+/*-----------------------------------------------------------------*/
+static void
+outAcc (operand * result)
+{
+       int size, offset;
+       size = getDataSize (result);
+       if (size) {
+               aopPut (AOP (result), "r0", 0);
+               size--;
+               offset = 1;
+               /* unsigned or positive */
+               while (size--) {
+                       aopPut (AOP (result), zero, offset++);
+               }
+       }
+}
+
+#endif // End Unused code section
+
 /*-----------------------------------------------------------------*/
 /* emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
@@ -113,7 +234,7 @@ static void
 emitcode (char *inst, char *fmt, ...)
 {
        va_list ap;
-       char lb[MAX_INLINEASM];
+       char lb[INITIAL_INLINEASM];
        char *lbp = lb;
 
        va_start (ap, fmt);
@@ -128,7 +249,7 @@ emitcode (char *inst, char *fmt, ...)
        else
                vsprintf (lb, fmt, ap);
 
-       while (isspace (*lbp))
+       while (isspace ((unsigned char)*lbp))
                lbp++;
 
        if (lbp && *lbp)
@@ -140,6 +261,49 @@ emitcode (char *inst, char *fmt, ...)
        va_end (ap);
 }
 
+/*-----------------------------------------------------------------*/
+/* avr_emitDebuggerSymbol - associate the current code location  */
+/*   with a debugger symbol                                        */
+/*-----------------------------------------------------------------*/
+void
+avr_emitDebuggerSymbol (char * debugSym)
+{
+  _G.debugLine = 1;
+  emitcode ("", "%s ==.", debugSym);
+  _G.debugLine = 0;
+}
+
+/*-----------------------------------------------------------------*/
+/* 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;
+       if (IS_AGGREGATE(type->next)) 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(OP_USES(op),lic->key) || (lic->defKey == op->key)) {
+                       return NULL;
+               }
+               lic = lic->next;
+       }
+       return NULL;
+}
+
 /*-----------------------------------------------------------------*/
 /* getFreePtr - returns X or Z whichever is free or can be pushed  */
 /*-----------------------------------------------------------------*/
@@ -226,7 +390,6 @@ getFreePtr (iCode * ic, asmop ** aopp, bool result, bool zonly)
                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");
@@ -299,10 +462,10 @@ aopForSym (iCode * ic, symbol * sym, bool result)
                                                   _G.nRegsSaved));
                                }
                                else {
-                                       emitcode ("subi", "%s,lo8(%d)",
+                                       emitcode ("subi", "%s,<(%d)",
                                                  aop->aopu.aop_ptr->name,
                                                  sym->stack - _G.nRegsSaved);
-                                       emitcode ("sbci", "%s,hi8(%d)",
+                                       emitcode ("sbci", "%s,>(%d)",
                                                  aop->aop_ptr2->name,
                                                  sym->stack - _G.nRegsSaved);
                                }
@@ -314,10 +477,10 @@ aopForSym (iCode * ic, symbol * sym, bool result)
                                                  sym->stack);
                                }
                                else {
-                                       emitcode ("subi", "%s,lo8(-%d)",
+                                       emitcode ("subi", "%s,<(-%d)",
                                                  aop->aopu.aop_ptr->name,
                                                  sym->stack);
-                                       emitcode ("sbci", "%s,hi8(-%d)",
+                                       emitcode ("sbci", "%s,>(-%d)",
                                                  aop->aop_ptr2->name,
                                                  sym->stack);
                                }
@@ -360,8 +523,8 @@ aopForSym (iCode * ic, symbol * sym, bool result)
 
        aop->aopu.aop_ptr = getFreePtr (ic, &aop, result, aop->code);
        aop->size = getSize (sym->type);
-       emitcode ("ldi", "%s,lo8(%s)", aop->aopu.aop_ptr->name, sym->rname);
-       emitcode ("ldi", "%s,hi8(%s)", aop->aop_ptr2);
+       emitcode ("ldi", "%s,<(%s)", aop->aopu.aop_ptr->name, sym->rname);
+       emitcode ("ldi", "%s,>(%s)", aop->aop_ptr2);
 
        return aop;
 }
@@ -506,7 +669,7 @@ sameRegs (asmop * aop1, asmop * aop2)
 static int
 isRegPair (asmop * aop)
 {
-       if (!aop || aop->size != 2)
+       if (!aop || aop->size < 2)
                return 0;
        if (aop->type == AOP_X || aop->type == AOP_Z)
                return 1;
@@ -519,6 +682,22 @@ isRegPair (asmop * aop)
        return 0;
 }
 
+/*-----------------------------------------------------------------*/
+/* allHigh - all registers are high registers                      */
+/*-----------------------------------------------------------------*/
+static int allHigh (asmop * aop)
+{
+       int i;
+       
+       if (aop->type == AOP_X || aop->type == AOP_Z)
+               return 1;
+       if (aop->type != AOP_REG)
+               return 0;
+       for (i=0; i < aop->size ; i++ ) 
+               if (aop->aopu.aop_reg[i]->rIdx < R16_IDX) return 0;
+       return 1;
+}
+
 /*-----------------------------------------------------------------*/
 /* aopOp - allocates an asmop for an operand  :                    */
 /*-----------------------------------------------------------------*/
@@ -568,7 +747,7 @@ aopOp (operand * op, iCode * ic, bool result)
 
 
        /* if the type is a conditional */
-       if (sym->regType == REG_CND) {
+       if (sym->regType & REG_CND) {
                aop = op->aop = sym->aop = newAsmop (AOP_CRY);
                aop->size = 0;
                return;
@@ -579,6 +758,8 @@ aopOp (operand * op, iCode * ic, bool result)
           b) has a spill location */
        if (sym->isspilt || sym->nRegs == 0) {
 
+               asmop *oldAsmOp = NULL;
+
                /* rematerialize it NOW */
                if (sym->remat) {
                        sym->aop = op->aop = aop = aopForRemat (sym);
@@ -600,8 +781,17 @@ aopOp (operand * op, iCode * ic, bool result)
                }
 
                /* else spill location  */
+               if (sym->usl.spillLoc && getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
+                   /* force a new aop if sizes differ */
+                   oldAsmOp = sym->usl.spillLoc->aop;
+                   sym->usl.spillLoc->aop = NULL;
+               }
                sym->aop = op->aop = aop =
                        aopForSym (ic, sym->usl.spillLoc, result);
+               if (getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
+                       /* Don't reuse the new aop, go with the last one */
+                       sym->usl.spillLoc->aop = oldAsmOp;
+               }
                aop->size = getSize (sym->type);
                return;
        }
@@ -676,10 +866,10 @@ freeAsmop (operand * op, asmop * aaop, iCode * ic, bool pop)
                                                  stk + 1);
                                }
                                else {
-                                       emitcode ("subi", "%s,lo8(%d)",
+                                       emitcode ("subi", "%s,<(%d)",
                                                  aop->aopu.aop_ptr->name,
                                                  -(stk + 1));
-                                       emitcode ("sbci", "%s,hi8(%d)",
+                                       emitcode ("sbci", "%s,>(%d)",
                                                  aop->aop_ptr2->name,
                                                  -(stk + 1));
                                }
@@ -809,7 +999,7 @@ aopGet (asmop * aop, int offset)
 
        case AOP_LIT:
                s = aopLiteral (aop->aopu.aop_lit, offset);
-               emitcode ("ldi", "%s,lo8(%s)",
+               emitcode ("ldi", "%s,<(%s)",
                          (rs = ((offset & 1) ? "r24" : "r25")), s);
                return rs;
 
@@ -858,7 +1048,7 @@ aopPut (asmop * aop, char *s, int offset)
                break;
 
        case AOP_REG:
-               if (toupper (*s) != 'R') {
+               if (toupper ((unsigned char)*s) != 'R') {
                        if (s == zero) {
                                emitcode ("clr", "%s",
                                          aop->aopu.aop_reg[offset]->name);
@@ -926,7 +1116,7 @@ aopPut (asmop * aop, char *s, int offset)
 
        case AOP_CRY:
                /* if used only for a condition code check */
-               assert (toupper (*s) == 'R');
+               assert (toupper ((unsigned char)*s) == 'R');
                if (offset == 0) {
                        emitcode ("xrl", "r0,r0");
                        emitcode ("cpi", "%s,0", s);
@@ -955,27 +1145,6 @@ aopPut (asmop * aop, char *s, int offset)
 
 }
 
-/*-----------------------------------------------------------------*/
-/* reAdjustPreg - points a register back to where it should        */
-/*-----------------------------------------------------------------*/
-static void
-reAdjustPreg (asmop * aop)
-{
-       int size;
-
-       aop->coff = 0;
-       if ((size = aop->size) <= 1)
-               return;
-       size--;
-       switch (aop->type) {
-       case AOP_X:
-       case AOP_Z:
-               emitcode ("sbiw", "%s,%d", aop->aopu.aop_ptr->name, size);
-               break;
-       }
-
-}
-
 #define AOP(op) op->aop
 #define AOP_TYPE(op) AOP(op)->type
 #define AOP_SIZE(op) AOP(op)->size
@@ -1062,36 +1231,6 @@ getDataSize (operand * op)
        return size;
 }
 
-/*-----------------------------------------------------------------*/
-/* outAcc - output Acc                                             */
-/*-----------------------------------------------------------------*/
-static void
-outAcc (operand * result)
-{
-       int size, offset;
-       size = getDataSize (result);
-       if (size) {
-               aopPut (AOP (result), "r0", 0);
-               size--;
-               offset = 1;
-               /* unsigned or positive */
-               while (size--) {
-                       aopPut (AOP (result), zero, offset++);
-               }
-       }
-}
-
-/*-----------------------------------------------------------------*/
-/* outBitC - output a bit C                                        */
-/*-----------------------------------------------------------------*/
-static void
-outBitC (operand * result)
-{
-       emitcode ("clr", "r0");
-       emitcode ("rol", "r0");
-       outAcc (result);
-}
-
 /*-----------------------------------------------------------------*/
 /* toBoolean - emit code for orl a,operator(sizeop)                */
 /*-----------------------------------------------------------------*/
@@ -1100,10 +1239,15 @@ toBoolean (operand * oper, char *r, bool clr)
 {
        int size = AOP_SIZE (oper);
        int offset = 0;
-       if (clr)
+       if (clr) {
                emitcode ("clr", "%s", r);
-       while (size--)
-               emitcode ("or", "%s,%s", r, aopGet (AOP (oper), offset++));
+               while (size--)
+                       emitcode ("or", "%s,%s", r, aopGet (AOP (oper), offset++));
+       } else {
+               size--;
+               emitcode("mov","%s,%s",r,aopGet (AOP (oper), offset++));
+               if (size) while (size--) emitcode ("or", "%s,%s", r, aopGet (AOP (oper), offset++));
+       }
 }
 
 
@@ -1126,17 +1270,17 @@ genNot (iCode * ic)
                genNotFloat (IC_LEFT (ic), IC_RESULT (ic));
                goto release;
        }
-       emitcode ("clr", "r0");
+       emitcode ("clr", "r24");
        tlbl = newiTempLabel (NULL);
        size = AOP_SIZE (IC_LEFT (ic));
        offset = 0;
        if (size == 1) {
-               emitcode ("cpse", "%s,r0", aopGet (AOP (IC_LEFT (ic)), 0));
+               emitcode ("cpse", "%s,r24", aopGet (AOP (IC_LEFT (ic)), 0));
        }
        else {
                while (size--) {
                        if (offset)
-                               emitcode ("cpc", "%s,r0",
+                               emitcode ("cpc", "%s,r24",
                                          aopGet (AOP (IC_LEFT (ic)),
                                                  offset));
                        else
@@ -1147,9 +1291,9 @@ genNot (iCode * ic)
                }
                emitcode ("bne", "L%05d", tlbl->key);
        }
-       emitcode ("ldi", "r0,1");
+       emitcode ("ldi", "r24,1");
        emitcode ("", "L%05d:", tlbl->key);
-       aopPut (AOP (IC_RESULT (ic)), "r0", 0);
+       aopPut (AOP (IC_RESULT (ic)), "r24", 0);
        size = AOP_SIZE (IC_RESULT (ic)) - 1;
        offset = 1;
        while (size--)
@@ -1279,7 +1423,7 @@ genUminus (iCode * ic)
                size = AOP_SIZE (IC_LEFT (ic)) - 1;
                offset = 1;
                while (size--) {
-                       emitcode ("sbci", "%s,lo8(-1)",
+                       emitcode ("sbci", "%s,0xff",
                                  aopGet (AOP (IC_RESULT (ic)), offset++));
                }
        }
@@ -1408,7 +1552,7 @@ static void
 genCall (iCode * ic)
 {
 
-       /* if send set is not empty the assign */
+       /* if send set is not empty then assign */
        if (_G.sendSet) {
                iCode *sic;
                int rnum = 16;
@@ -1452,9 +1596,9 @@ genCall (iCode * ic)
                        emitcode ("sbiw", "r28,%d", ic->parmBytes);
                }
                else {
-                       emitcode ("subi", "r28,lo8(%d)",
+                       emitcode ("subi", "r28,<(%d)",
                                  ic->parmBytes);
-                       emitcode ("sbci", "r29,hi8(%d)",
+                       emitcode ("sbci", "r29,>(%d)",
                                  ic->parmBytes);
                }
        }
@@ -1535,9 +1679,9 @@ genPcall (iCode * ic)
                        emitcode ("sbiw", "r28,%d", ic->parmBytes);
                }
                else {
-                       emitcode ("subi", "r28,lo8(%d)",
+                       emitcode ("subi", "r28,<(%d)",
                                  ic->parmBytes);
-                       emitcode ("sbci", "r29,hi8(%d)",
+                       emitcode ("sbci", "r29,>(%d)",
                                  ic->parmBytes);
                }
        }
@@ -1569,26 +1713,6 @@ resultRemat (iCode * ic)
 #define STRCASECMP strcasecmp
 #endif
 
-/*-----------------------------------------------------------------*/
-/* inExcludeList - return 1 if the string is in exclude Reg list   */
-/*-----------------------------------------------------------------*/
-static bool
-inExcludeList (char *s)
-{
-       int i = 0;
-
-       if (options.excludeRegs[i] &&
-           STRCASECMP (options.excludeRegs[i], "none") == 0)
-               return FALSE;
-
-       for (i = 0; options.excludeRegs[i]; i++) {
-               if (options.excludeRegs[i] &&
-                   STRCASECMP (s, options.excludeRegs[i]) == 0)
-                       return TRUE;
-       }
-       return FALSE;
-}
-
 /*-----------------------------------------------------------------*/
 /* genFunction - generated code for function entry                 */
 /*-----------------------------------------------------------------*/
@@ -1596,7 +1720,7 @@ static void
 genFunction (iCode * ic)
 {
        symbol *sym;
-       sym_link *fetype;
+       sym_link *ftype;
        int i = 0;
 
        _G.nRegsSaved = 0;
@@ -1607,13 +1731,13 @@ genFunction (iCode * ic)
        emitcode (";", "-----------------------------------------");
 
        emitcode ("", "%s:", sym->rname);
-       fetype = getSpec (operandType (IC_LEFT (ic)));
+       ftype = operandType (IC_LEFT (ic));
 
        /* if critical function then turn interrupts off */
-       if (SPEC_CRTCL (fetype))
+       if (IFFUNC_ISCRITICAL (ftype))
                emitcode ("cli", "");
 
-       if (IS_ISR (sym->etype)) {
+       if (IFFUNC_ISISR (sym->type)) {
        }
 
        /* save the preserved registers that are used in this function */
@@ -1650,8 +1774,8 @@ genFunction (iCode * ic)
                        emitcode ("sbiw", "r28,%d", sym->stack);
                }
                else {
-                       emitcode ("subi", "r28,lo8(%d)", sym->stack);
-                       emitcode ("sbci", "r29,hi8(%d)", sym->stack);
+                       emitcode ("subi", "r28,<(%d)", sym->stack);
+                       emitcode ("sbci", "r29,>(%d)", sym->stack);
                }
                emitcode ("out", "__SP_L__,r28");
                emitcode ("out", "__SP_H__,r29");
@@ -1673,8 +1797,8 @@ genEndFunction (iCode * ic)
                        emitcode ("adiw", "r28,%d", sym->stack);
                }
                else {
-                       emitcode ("subi", "r28,lo8(-%d)", sym->stack);
-                       emitcode ("sbci", "r29,hi8(-%d)", sym->stack);
+                       emitcode ("subi", "r28,<(-%d)", sym->stack);
+                       emitcode ("sbci", "r29,>(-%d)", sym->stack);
                }
                emitcode ("out", "__SP_L__,r28");
                emitcode ("out", "__SP_H__,r29");
@@ -1694,11 +1818,11 @@ genEndFunction (iCode * ic)
        }
        if (bitVectBitValue (sym->regsUsed, R27_IDX)) {
                _G.nRegsSaved--;
-               emitcode ("push", "r27");
+               emitcode ("pop", "r27");
        }
        if (bitVectBitValue (sym->regsUsed, R26_IDX)) {
                _G.nRegsSaved--;
-               emitcode ("push", "r26");
+               emitcode ("pop", "r26");
        }
        for (i = R15_IDX; i >= R2_IDX; i--) {
                if (bitVectBitValue (sym->regsUsed, i)) {
@@ -1707,10 +1831,14 @@ genEndFunction (iCode * ic)
                }
        }
 
-       if (SPEC_CRTCL (sym->etype))
+       if (IFFUNC_ISCRITICAL (sym->type))
                emitcode ("sti", "");
 
-       if (IS_ISR (sym->etype)) {
+       if (options.debug && currFunc) {
+               debugFile->writeEndFunction (currFunc, ic, 1);
+       }
+
+       if (IFFUNC_ISISR (sym->type)) {
                emitcode ("rti", "");
        }
        else {
@@ -1785,31 +1913,7 @@ genLabel (iCode * ic)
 static void
 genGoto (iCode * ic)
 {
-       emitcode ("rjmp", "L%05d:", (IC_LABEL (ic)->key + 100));
-}
-
-/*-----------------------------------------------------------------*/
-/* findLabelBackwards: walks back through the iCode chain looking  */
-/* for the given label. Returns number of iCode instructions     */
-/* between that label and given ic.          */
-/* Returns zero if label not found.          */
-/*-----------------------------------------------------------------*/
-static int
-findLabelBackwards (iCode * ic, int key)
-{
-       int count = 0;
-
-       while (ic->prev) {
-               ic = ic->prev;
-               count++;
-
-               if (ic->op == LABEL && IC_LABEL (ic)->key == key) {
-                       /* printf("findLabelBackwards = %d\n", count); */
-                       return count;
-               }
-       }
-
-       return 0;
+       emitcode ("rjmp", "L%05d", (IC_LABEL (ic)->key));
 }
 
 /*-----------------------------------------------------------------*/
@@ -1819,6 +1923,7 @@ static bool
 genPlusIncr (iCode * ic)
 {
        unsigned int icount;
+       int offset = 0;
 
        /* will try to generate an increment */
        /* if the right side is not a literal
@@ -1826,8 +1931,7 @@ genPlusIncr (iCode * ic)
        if (AOP_TYPE (IC_RIGHT (ic)) != AOP_LIT)
                return FALSE;
 
-       icount =
-               (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.
+       icount = (unsigned int) floatFromVal (AOP (IC_RIGHT (ic))->aopu.
                                             aop_lit);
 
        /* if the sizes are greater than 2 or they are not the same regs
@@ -1844,39 +1948,46 @@ genPlusIncr (iCode * ic)
                                  aopGet (AOP (IC_LEFT (ic)), 0));
                        return TRUE;
                }
-               emitcode ("subi", "%s,lo8(%d)",
-                         aopGet (AOP (IC_LEFT (ic)), 0), -icount);
-               return TRUE;
+               if (AOP_ISHIGHREG( AOP (IC_LEFT (ic)),0)) {
+                       emitcode ("subi", "%s,<(%d)",
+                                 aopGet (AOP (IC_LEFT (ic)), 0), 0-icount);
+                       return TRUE;
+               }
+       }
+
+       for (offset = 0 ; offset < AOP_SIZE(IC_RESULT(ic)) ; offset++) {
+               if (!(AOP_ISHIGHREG(AOP(IC_RESULT(ic)),offset))) return FALSE;
        }
 
        if (AOP_SIZE (IC_RESULT (ic)) <= 3) {
                /* if register pair and starts with 26/30 then adiw */
                if (isRegPair (AOP (IC_RESULT (ic))) && icount > 0
                    && icount < 64
-                   && (IS_REGIDX (AOP (IC_RESULT (ic)), R26_IDX)
-                       || IS_REGIDX (AOP (IC_RESULT (ic)), R30_IDX))) {
+                   && (IS_REGIDX (AOP (IC_RESULT (ic)), R26_IDX) || 
+                       IS_REGIDX (AOP (IC_RESULT (ic)), R24_IDX) || 
+                       IS_REGIDX (AOP (IC_RESULT (ic)), R30_IDX))) {
                        emitcode ("adiw", "%s,%d",
                                  aopGet (AOP (IC_RESULT (ic)), 0), icount);
                        return TRUE;
                }
 
                /* use subi */
-               emitcode ("subi", "%s,lo8(%d)",
-                         aopGet (AOP (IC_RESULT (ic)), 0), -icount);
-               emitcode ("sbci", "%s,hi8(%d)",
-                         aopGet (AOP (IC_RESULT (ic)), 1), -icount);
+               emitcode ("subi", "%s,<(%d)",
+                         aopGet (AOP (IC_RESULT (ic)), 0), 0-icount);
+               emitcode ("sbci", "%s,>(%d)",
+                         aopGet (AOP (IC_RESULT (ic)), 1), 0-icount);
                return TRUE;
        }
 
        /* for 32 bit longs */
-       emitcode ("subi", "%s,lo8(%d)", aopGet (AOP (IC_RESULT (ic)), 0),
-                 -icount);
-       emitcode ("sbci", "%s,hi8(%d)", aopGet (AOP (IC_RESULT (ic)), 1),
-                 -icount);
+       emitcode ("subi", "%s,<(%d)", aopGet (AOP (IC_RESULT (ic)), 0),
+                 0-icount);
+       emitcode ("sbci", "%s,>(%d)", aopGet (AOP (IC_RESULT (ic)), 1),
+                 0-icount);
        emitcode ("sbci", "%s,hlo8(%d)", aopGet (AOP (IC_RESULT (ic)), 2),
-                 -icount);
+                 0-icount);
        emitcode ("sbci", "%s,hhi8(%d)", aopGet (AOP (IC_RESULT (ic)), 3),
-                 -icount);
+                 0-icount);
        return TRUE;
 
 }
@@ -1957,16 +2068,27 @@ genPlus (iCode * ic)
                                  aopGet (AOP (IC_RIGHT (ic)), offset));
                }
                else {
-                       if (offset == 0)
-                               l = "subi";
-                       else
-                               l = "sbci";
-
-                       emitcode (l, "%s,%s(-%d)",
-                                 aopGet (AOP (IC_RESULT (ic)), offset),
-                                 larray[offset],
-                                 (int) floatFromVal (AOP (IC_RIGHT (ic))->
-                                                     aopu.aop_lit));
+                       if (AOP_ISHIGHREG( AOP( IC_RESULT(ic)),offset)) {
+                               if (offset == 0)
+                                       l = "subi";
+                               else
+                                       l = "sbci";
+                               
+                               emitcode (l, "%s,%s(-%d)",
+                                         aopGet (AOP (IC_RESULT (ic)), offset),
+                                         larray[offset],
+                                         (int) floatFromVal (AOP (IC_RIGHT (ic))->
+                                                             aopu.aop_lit));
+                       } else {
+                               if (offset == 0)
+                                       l = "add";
+                               else
+                                       l = "adc";
+                               
+                               emitcode (l, "%s,%s",
+                                         aopGet (AOP (IC_RESULT (ic)), offset),
+                                         aopGet (AOP (IC_RIGHT (ic)), offset));
+                       }
                }
                offset++;
        }
@@ -1988,6 +2110,7 @@ static bool
 genMinusDec (iCode * ic)
 {
        unsigned int icount;
+       int offset ;
 
        /* will try to generate an increment */
        /* if the right side is not a literal
@@ -2001,7 +2124,7 @@ genMinusDec (iCode * ic)
 
        /* if the sizes are greater than 2 or they are not the same regs
           then we cannot */
-       if (!sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RIGHT (ic))))
+       if (!sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))))
                return FALSE;
 
        /* so we know LEFT & RESULT in the same registers and add
@@ -2013,33 +2136,40 @@ genMinusDec (iCode * ic)
                                  aopGet (AOP (IC_LEFT (ic)), 0));
                        return TRUE;
                }
-               emitcode ("subi", "%s,lo8(%d)",
-                         aopGet (AOP (IC_LEFT (ic)), 0), icount);
-               return TRUE;
+               if (AOP_ISHIGHREG( AOP ( IC_LEFT(ic)),0)) {
+                       emitcode ("subi", "%s,<(%d)",
+                                 aopGet (AOP (IC_LEFT (ic)), 0), icount);
+                       return TRUE;
+               }
+       }
+
+       for (offset = 0 ; offset < AOP_SIZE(IC_RESULT(ic)) ; offset++) {
+               if (!(AOP_ISHIGHREG(AOP(IC_RESULT(ic)),offset))) return FALSE;
        }
 
        if (AOP_SIZE (IC_RESULT (ic)) <= 3) {
                /* if register pair and starts with 26/30 then adiw */
                if (isRegPair (AOP (IC_RESULT (ic))) && icount > 0
                    && icount < 64
-                   && (IS_REGIDX (AOP (IC_RESULT (ic)), R26_IDX)
-                       || IS_REGIDX (AOP (IC_RESULT (ic)), R30_IDX))) {
+                   && (IS_REGIDX (AOP (IC_RESULT (ic)), R26_IDX) || 
+                       IS_REGIDX (AOP (IC_RESULT (ic)), R24_IDX) || 
+                       IS_REGIDX (AOP (IC_RESULT (ic)), R30_IDX))) {
                        emitcode ("sbiw", "%s,%d",
                                  aopGet (AOP (IC_RESULT (ic)), 0), icount);
                        return TRUE;
                }
 
                /* use subi */
-               emitcode ("subi", "%s,lo8(%d)",
+               emitcode ("subi", "%s,<(%d)",
                          aopGet (AOP (IC_RESULT (ic)), 0), icount);
-               emitcode ("sbci", "%s,hi8(%d)",
+               emitcode ("sbci", "%s,>(%d)",
                          aopGet (AOP (IC_RESULT (ic)), 1), icount);
                return TRUE;
        }
        /* for 32 bit longs */
-       emitcode ("subi", "%s,lo8(%d)", aopGet (AOP (IC_RESULT (ic)), 0),
+       emitcode ("subi", "%s,<(%d)", aopGet (AOP (IC_RESULT (ic)), 0),
                  icount);
-       emitcode ("sbci", "%s,hi8(%d)", aopGet (AOP (IC_RESULT (ic)), 1),
+       emitcode ("sbci", "%s,>(%d)", aopGet (AOP (IC_RESULT (ic)), 1),
                  icount);
        emitcode ("sbci", "%s,hlo8(%d)", aopGet (AOP (IC_RESULT (ic)), 2),
                  icount);
@@ -2049,26 +2179,6 @@ genMinusDec (iCode * ic)
 
 }
 
-/*-----------------------------------------------------------------*/
-/* addSign - complete with sign                                    */
-/*-----------------------------------------------------------------*/
-static void
-addSign (operand * result, int offset, int sign)
-{
-       int size = (getDataSize (result) - offset);
-       if (size > 0) {
-               if (sign) {
-                       emitcode ("rlc", "a");
-                       emitcode ("subb", "a,acc");
-                       while (size--)
-                               aopPut (AOP (result), "a", offset++);
-               }
-               else
-                       while (size--)
-                               aopPut (AOP (result), zero, offset++);
-       }
-}
-
 /*-----------------------------------------------------------------*/
 /* genMinus - generates code for subtraction                       */
 /*-----------------------------------------------------------------*/
@@ -2106,16 +2216,27 @@ genMinus (iCode * ic)
                                  aopGet (AOP (IC_RIGHT (ic)), offset));
                }
                else {
-                       if (offset == 0)
-                               l = "subi";
-                       else
-                               l = "sbci";
-
-                       emitcode (l, "%s,%s(%d)",
-                                 aopGet (AOP (IC_RESULT (ic)), offset),
-                                 larray[offset],
-                                 (int) floatFromVal (AOP (IC_RIGHT (ic))->
-                                                     aopu.aop_lit));
+                       if (AOP_ISHIGHREG(AOP (IC_RESULT (ic)),offset)) {
+                               if (offset == 0)
+                                       l = "subi";
+                               else
+                                       l = "sbci";
+                               
+                               emitcode (l, "%s,%s(%d)",
+                                         aopGet (AOP (IC_RESULT (ic)), offset),
+                                         larray[offset],
+                                         (int) floatFromVal (AOP (IC_RIGHT (ic))->
+                                                             aopu.aop_lit));
+                       } else {
+                               if (offset == 0)
+                                       l = "sub";
+                               else
+                                       l = "sbc";
+                               
+                               emitcode (l, "%s,%s",
+                                         aopGet (AOP (IC_RESULT (ic)), offset),
+                                         aopGet (AOP (IC_RIGHT (ic)), offset));
+                       }
                }
                offset++;
        }
@@ -2173,7 +2294,7 @@ genMultOneByte (operand * left, operand * right, operand * result)
                                lbl = newiTempLabel (NULL);
                                emitcode ("ldi", "r24,0");
                                emitcode ("brcc", "L%05d", lbl->key);
-                               emitcode ("ldi", "r24,lo8(-1)");
+                               emitcode ("ldi", "r24,0xff)");
                                emitcode ("", "L%05d:", lbl->key);
                                while (size--)
                                        aopPut (AOP (result), "r24",
@@ -2206,7 +2327,7 @@ genMult (iCode * ic)
        }
 
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 
       release:
        freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
@@ -2221,7 +2342,7 @@ static void
 genDiv (iCode * ic)
 {
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2231,7 +2352,7 @@ static void
 genMod (iCode * ic)
 {
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 
 }
 
@@ -2264,7 +2385,7 @@ revavrcnd (int type)
                if (rar[i].rtype == type)
                        return rar[i].type;
        }
-       assert (1);             /* cannot happen */
+       assert (0);             /* cannot happen */
        return 0;               /* makes the compiler happy */
 }
 
@@ -2319,7 +2440,7 @@ genCmp (iCode * ic, iCode * ifx, int br_type)
        if (ifx) {
                if (size == 1) {
                        if (AOP_TYPE (right) == AOP_LIT) {
-                               emitcode ("cpi", "%s,lo8(%d)",
+                               emitcode ("cpi", "%s,<(%d)",
                                          aopGet (AOP (left), 0),
                                          (int)
                                          floatFromVal (AOP (IC_RIGHT (ic))->
@@ -2389,7 +2510,7 @@ static void
 genCmpGt (iCode * ic, iCode * ifx)
 {
        /* should have transformed by the parser */
-       assert (1);
+       assert (0);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2535,28 +2656,6 @@ genOrOp (iCode * ic)
        freeAsmop (result, NULL, ic, TRUE);
 }
 
-/*-----------------------------------------------------------------*/
-/* isLiteralBit - test if lit == 2^n                               */
-/*-----------------------------------------------------------------*/
-static int
-isLiteralBit (unsigned long lit)
-{
-       unsigned long pw[32] = { 1L, 2L, 4L, 8L, 16L, 32L, 64L, 128L,
-               0x100L, 0x200L, 0x400L, 0x800L,
-               0x1000L, 0x2000L, 0x4000L, 0x8000L,
-               0x10000L, 0x20000L, 0x40000L, 0x80000L,
-               0x100000L, 0x200000L, 0x400000L, 0x800000L,
-               0x1000000L, 0x2000000L, 0x4000000L, 0x8000000L,
-               0x10000000L, 0x20000000L, 0x40000000L, 0x80000000L
-       };
-       int idx;
-
-       for (idx = 0; idx < 32; idx++)
-               if (lit == pw[idx])
-                       return idx + 1;
-       return 0;
-}
-
 enum {
        AVR_AND = 0, AVR_OR, AVR_XOR
 };
@@ -2587,7 +2686,7 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                                (int) floatFromVal (AOP (right)->aopu.
                                                    aop_lit);
                        int p2 = powof2 (lit);
-                       if (bitop == AVR_AND && p2) {   /* right side is a power of 2 */
+                       if (bitop == AVR_AND && (p2 >= 0)) {    /* right side is a power of 2 */
                                l = aopGet (AOP (left), p2 / 8);
                                if (IC_TRUE (ifx)) {
                                        emitcode ("sbrc", "%s,%d", l,
@@ -2605,62 +2704,40 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                        else {  /* right not power of two */
                                int eh = OP_SYMBOL (left)->liveTo <= ic->seq;
                                if (size == 1) {
-                                       if (eh) {
+                                       if (eh && AOP_ISHIGHREG(AOP(IC_LEFT(ic)),0)) {
                                                emitcode (bopnames_lit[bitop],
-                                                         "%s,lo8(%d)",
-                                                         aopGet (AOP
-                                                                 (IC_LEFT
-                                                                  (ic)), 0),
-                                                         lit);
+                                                         "%s,<(%d)",
+                                                         aopGet (AOP (IC_LEFT (ic)), 0), lit);
                                        }
                                        else {
-                                               MOVR0 (aopGet
-                                                      (AOP (IC_LEFT (ic)),
-                                                       0));
-                                               emitcode (bopnames_lit[bitop],
-                                                         "r0,lo8(%d)", lit);
+                                               MOVR24 (aopGet (AOP (IC_LEFT (ic)), 0));
+                                               emitcode (bopnames_lit[bitop], "r24,<(%d)", lit);
                                        }
                                        lbl = newiTempLabel (NULL);
                                        if (IC_TRUE (ifx)) {
-                                               emitcode ("breq", "L%05d",
-                                                         lbl->key);
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_TRUE (ifx)->key);
+                                               emitcode ("breq", "L%05d", lbl->key);
+                                               emitcode ("rjmp", "L%05d", IC_TRUE (ifx)->key);
                                        }
                                        else {
-                                               emitcode ("brne", "L%05d",
-                                                         lbl->key);
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_FALSE (ifx)->
-                                                         key);
+                                               emitcode ("brne", "L%05d", lbl->key);
+                                               emitcode ("rjmp", "L%05d", IC_FALSE (ifx)-> key);
                                        }
                                        emitcode ("", "L%05d:", lbl->key);
                                }
                                else if (size == 2) {
-                                       emitcode ("mov", "r24,%s",
-                                                 aopGet (AOP (IC_LEFT (ic)),
-                                                         0));
-                                       emitcode ("mov", "r25,%s",
-                                                 aopGet (AOP (IC_LEFT (ic)),
-                                                         1));
-                                       emitcode (bopnames_lit[bitop],
-                                                 "r24,lo8(%d)", lit);
-                                       emitcode (bopnames_lit[bitop],
-                                                 "r25,hi8(%d)", lit);
+                                       emitcode ("mov", "r24,%s", aopGet (AOP (IC_LEFT (ic)), 0));
+                                       emitcode ("mov", "r25,%s", aopGet (AOP (IC_LEFT (ic)), 1));
+                                       emitcode (bopnames_lit[bitop], "r24,<(%d)", lit);
+                                       emitcode (bopnames_lit[bitop], "r25,>(%d)", lit);
                                        emitcode ("sbiw", "r24,0");
                                        lbl = newiTempLabel (NULL);
                                        if (IC_TRUE (ifx)) {
-                                               emitcode ("breq", "L%05d",
-                                                         lbl->key);
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_TRUE (ifx)->key);
+                                               emitcode ("breq", "L%05d", lbl->key);
+                                               emitcode ("rjmp", "L%05d", IC_TRUE (ifx)->key);
                                        }
                                        else {
-                                               emitcode ("brne", "L%05d",
-                                                         lbl->key);
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_FALSE (ifx)->
-                                                         key);
+                                               emitcode ("brne", "L%05d", lbl->key);
+                                               emitcode ("rjmp", "L%05d", IC_FALSE (ifx)->key);
                                        }
                                        emitcode ("", "L%05d:", lbl->key);
                                }
@@ -2668,43 +2745,28 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                                        lbl = newiTempLabel (NULL);
                                        lbl1 = newiTempLabel (NULL);
                                        while (size--) {
-                                               if (eh) {
-                                                       emitcode (bopnames_lit
-                                                                 [bitop],
-                                                                 "%s,lo8(%d)",
-                                                                 aopGet (AOP
-                                                                         (IC_LEFT
-                                                                          (ic)),
-                                                                         offset),
+                                               if (eh && AOP_ISHIGHREG(AOP(IC_LEFT(ic)),offset)) {
+                                                       emitcode (bopnames_lit [bitop], "%s,<(%d)",
+                                                                 aopGet (AOP (IC_LEFT (ic)), offset),
                                                                  lit);
                                                }
                                                else {
-                                                       MOVR0 (aopGet
-                                                              (AOP
-                                                               (IC_LEFT
-                                                                (ic)),
-                                                               offset));
-                                                       emitcode ("andi",
-                                                                 "r0,lo8(%d)",
-                                                                 lit);
+                                                       char *l = aopGet (AOP (IC_LEFT (ic)), offset);
+                                                       MOVR24 (l);
+                                                       emitcode ("andi", "r24,<(%d)", lit);
                                                }
-                                               emitcode ("brne", "L%05d",
-                                                         lbl->key);
+                                               emitcode ("brne", "L%05d", lbl->key);
                                                offset++;
                                        }
                                        /* all are zero */
                                        if (IC_FALSE (ifx))
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_FALSE (ifx)->
-                                                         key);
+                                               emitcode ("rjmp", "L%05d", IC_FALSE (ifx)-> key);
                                        else
-                                               emitcode ("rjmp", "L%05d",
-                                                         lbl1->key);
+                                               emitcode ("rjmp", "L%05d", lbl1->key);
                                        emitcode ("", "L%05d:", lbl->key);
                                        /* not zero */
                                        if (IC_TRUE (ifx))
-                                               emitcode ("rjmp", "L%05d",
-                                                         IC_TRUE (ifx)->key);
+                                               emitcode ("rjmp", "L%05d", IC_TRUE (ifx)->key);
                                        emitcode ("", "L%05d:", lbl1->key);
 
                                }
@@ -2715,25 +2777,18 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                        int reh = OP_SYMBOL (right)->liveTo <= ic->seq;
                        if (size == 1) {
                                if (eh) {
-                                       emitcode (bopnames[bitop], "%s,%s",
-                                                 aopGet (AOP (IC_LEFT (ic)),
-                                                         0),
-                                                 aopGet (AOP (IC_RIGHT (ic)),
-                                                         0));
+                                       emitcode (bopnames[bitop], "%s,%s", aopGet (AOP (IC_LEFT (ic)), 0),
+                                                 aopGet (AOP (IC_RIGHT (ic)), 0));
                                }
                                else if (reh) {
                                        emitcode (bopnames[bitop], "%s,%s",
-                                                 aopGet (AOP (IC_RIGHT (ic)),
-                                                         0),
-                                                 aopGet (AOP (IC_LEFT (ic)),
-                                                         0));
+                                                 aopGet (AOP (IC_RIGHT (ic)), 0),
+                                                 aopGet (AOP (IC_LEFT (ic)), 0));
                                }
                                else {
-                                       MOVR0 (aopGet
-                                              (AOP (IC_LEFT (ic)), 0));
+                                       MOVR0 (aopGet (AOP (IC_LEFT (ic)), 0));
                                        emitcode (bopnames[bitop], "r0,%s",
-                                                 aopGet (AOP (IC_RIGHT (ic)),
-                                                         0));
+                                                 aopGet (AOP (IC_RIGHT (ic)), 0));
                                }
                                lbl = newiTempLabel (NULL);
                                if (IC_TRUE (ifx)) {
@@ -2761,13 +2816,11 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                                lbl = newiTempLabel (NULL);
                                if (IC_TRUE (ifx)) {
                                        emitcode ("breq", "L%05d", lbl->key);
-                                       emitcode ("rjmp", "L%05d",
-                                                 IC_TRUE (ifx)->key);
+                                       emitcode ("rjmp", "L%05d", IC_TRUE (ifx)->key);
                                }
                                else {
                                        emitcode ("brne", "L%05d", lbl->key);
-                                       emitcode ("rjmp", "L%05d",
-                                                 IC_FALSE (ifx)->key);
+                                       emitcode ("rjmp", "L%05d", IC_FALSE (ifx)->key);
                                }
                                emitcode ("", "L%05d:", lbl->key);
                        }
@@ -2776,54 +2829,32 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                                lbl1 = newiTempLabel (NULL);
                                while (size--) {
                                        if (eh) {
-                                               emitcode (bopnames[bitop],
-                                                         "%s,%s",
-                                                         aopGet (AOP
-                                                                 (IC_LEFT
-                                                                  (ic)),
-                                                                 offset),
-                                                         aopGet (AOP
-                                                                 (IC_RIGHT
-                                                                  (ic)),
-                                                                 offset));
+                                               emitcode (bopnames[bitop], "%s,%s",
+                                                         aopGet (AOP (IC_LEFT (ic)), offset),
+                                                         aopGet (AOP (IC_RIGHT (ic)), offset));
                                        }
                                        else if (reh) {
-                                               emitcode (bopnames[bitop],
-                                                         "%s,%s",
-                                                         aopGet (AOP
-                                                                 (IC_RIGHT
-                                                                  (ic)),
-                                                                 offset),
-                                                         aopGet (AOP
-                                                                 (IC_LEFT
-                                                                  (ic)),
-                                                                 offset));
+                                               emitcode (bopnames[bitop], "%s,%s",
+                                                         aopGet (AOP (IC_RIGHT (ic)), offset),
+                                                         aopGet (AOP (IC_LEFT (ic)), offset));
                                        }
                                        else {
-                                               MOVR0 (aopGet
-                                                      (AOP (IC_LEFT (ic)),
-                                                       offset));
-                                               emitcode (bopnames[bitop],
-                                                         "r0,%s",
-                                                         aopGet (AOP
-                                                                 (IC_RIGHT
-                                                                  (ic)),
-                                                                 offset));
+                                               MOVR0 (aopGet (AOP (IC_LEFT (ic)), offset));
+                                               emitcode (bopnames[bitop], "r0,%s",
+                                                         aopGet (AOP (IC_RIGHT (ic)), offset));
                                        }
                                        emitcode ("brne", "L%05d", lbl->key);
                                        offset++;
                                }
                                /* all are zero */
                                if (IC_FALSE (ifx))
-                                       emitcode ("rjmp", "L%05d",
-                                                 IC_FALSE (ifx)->key);
+                                       emitcode ("rjmp", "L%05d", IC_FALSE (ifx)->key);
                                else
                                        emitcode ("rjmp", "L%05d", lbl1->key);
                                emitcode ("", "L%05d:", lbl->key);
                                /* not zero */
                                if (IC_TRUE (ifx))
-                                       emitcode ("rjmp", "L%05d",
-                                                 IC_TRUE (ifx)->key);
+                                       emitcode ("rjmp", "L%05d", IC_TRUE (ifx)->key);
                                emitcode ("", "L%05d:", lbl1->key);
 
                        }
@@ -2856,19 +2887,18 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                        }
                }
                if (samerl) {
-                       if (AOP_TYPE (IC_RIGHT (ic)) == AOP_LIT
-                           && (bitop == AVR_AND || bitop == AVR_OR)) {
+                       if (AOP_TYPE (IC_RIGHT (ic)) == AOP_LIT && 
+                           AOP_ISHIGHREG(AOP(IC_LEFT(ic)),offset) &&
+                           (bitop == AVR_AND || bitop == AVR_OR)) {
                                emitcode (bopnames_lit[bitop], "%s,%s(%d)",
                                          aopGet (AOP (IC_LEFT (ic)), offset),
                                          larray[offset],
-                                         (int) floatFromVal (AOP (right)->
-                                                             aopu.aop_lit));
+                                         (int) floatFromVal (AOP (right)-> aopu.aop_lit));
                        }
                        else {
                                emitcode (bopnames[bitop], "%s,%s",
                                          aopGet (AOP (IC_LEFT (ic)), offset),
-                                         aopGet (AOP (IC_RIGHT (ic)),
-                                                 offset));
+                                         aopGet (AOP (IC_RIGHT (ic)), offset));
                        }
                }
                else if (samerr) {
@@ -2924,11 +2954,11 @@ genXor (iCode * ic, iCode * ifx)
 static void
 genInline (iCode * ic)
 {
-       char buffer[MAX_INLINEASM];
-       char *bp = buffer;
-       char *bp1 = buffer;
+       char *buffer, *bp, *bp1;
 
        _G.inLine += (!options.asmpeep);
+
+       buffer = bp = bp1 = Safe_calloc(1, strlen(IC_INLINE(ic))+1);
        strcpy (buffer, IC_INLINE (ic));
 
        /* emit each line as a code */
@@ -3040,13 +3070,13 @@ genGetHbit (iCode * ic)
        if (!sameRegs (AOP (left), AOP (result))) {
                emitcode ("clr", "%s", aopGet (AOP (result), size - 1));
                emitcode ("sbrc", "%s,7", aopGet (AOP (left), size - 1));
-               emitcode ("subi", "%s,lo8(-1)",
+               emitcode ("subi", "%s,<(-1)",
                          aopGet (AOP (result), size - 1));
        }
        else {
                emitcode ("clr", "r0");
                emitcode ("sbrc", "%s,7", aopGet (AOP (left), size - 1));
-               emitcode ("subi", "r0,lo8(-1)");
+               emitcode ("subi", "r0,<(-1)");
                aopPut (AOP (result), "r0", 0);
        }
        offset = 1;
@@ -3087,8 +3117,14 @@ genShiftLeftLit (iCode * ic)
                if (!sameRegs (AOP (left), AOP (result)))
                        aopPut (AOP (result), aopGet (AOP (left), 0), 0);
                if (shCount >= 4) {
-                       emitcode ("swap", "%s", aopGet (AOP (result), 0));
-                       emitcode ("andi", "%s,0xf0");
+                       if (AOP_ISHIGHREG(AOP(result),0)) {
+                               emitcode ("swap", "%s", aopGet (AOP (result), 0));
+                               emitcode ("andi", "%s,0xf0");
+                       } else {
+                               emitcode ("ldi","r24,0xf0");
+                               emitcode ("swap", "%s", aopGet (AOP (result), 0));
+                               emitcode ("and", "%s,r24");
+                       }
                        shCount -= 4;
                }
                if (shCount == 1) {
@@ -3102,10 +3138,14 @@ genShiftLeftLit (iCode * ic)
        case 2:
                if (shCount >= 12) {
                        aopPut (AOP (result), aopGet (AOP (left), 0), 1);
-                       aopPut (AOP (result), zero, 0);
+                       aopPut (AOP (result), zero, 0);                 
                        emitcode ("swap", "%s", aopGet (AOP (result), 1));
-                       emitcode ("andi", "%s,0xf0",
-                                 aopGet (AOP (result), 1));
+                       if (AOP_ISHIGHREG(AOP(result),1)) {
+                               emitcode ("andi", "%s,0xf0", aopGet (AOP (result), 1));
+                       } else {
+                               emitcode ("ldi","r24,0xf0");
+                               emitcode ("and", "%s,r24", aopGet (AOP (result), 1));
+                       }
                        shCount -= 12;
                        lByteZ = 1;
                }
@@ -3123,20 +3163,27 @@ genShiftLeftLit (iCode * ic)
                                aopPut (AOP (result), aopGet (AOP (left), 1),
                                        1);
                        }
-                       emitcode ("mov", "r1,%s", aopGet (AOP (result), 0));
+                       emitcode ("mov", "r24,%s", aopGet (AOP (result), 0));
+                       emitcode ("andi", "r24,0x0f");
+                       if (!(AOP_ISHIGHREG(AOP(result),0) && AOP_ISHIGHREG(AOP(result),1))) {
+                               emitcode("ldi","r25,0xf0");
+                       }
                        emitcode ("swap", "%s", aopGet (AOP (result), 0));
-                       emitcode ("andi", "%s,0xf0",
-                                 aopGet (AOP (result), 0));
-                       emitcode ("andi", "r1,0x0f");
+                       if (AOP_ISHIGHREG(AOP(result),0)) {
+                               emitcode ("andi", "%s,0xf0", aopGet (AOP (result), 0));
+                       } else {
+                               emitcode ("and", "%s,r25", aopGet (AOP (result), 0));
+                       }
                        emitcode ("swap", "%s", aopGet (AOP (result), 1));
-                       emitcode ("andi", "%s,0xf0",
-                                 aopGet (AOP (result), 1));
-                       emitcode ("or", "%s,r1", aopGet (AOP (result), 1));
+                       if (AOP_ISHIGHREG(AOP(result),1)) {
+                               emitcode ("andi", "%s,0xf0", aopGet (AOP (result), 1));
+                       } else {
+                               emitcode ("and", "%s,r25", aopGet (AOP (result), 1));
+                       }
+                       emitcode ("or", "%s,r24", aopGet (AOP (result), 1));
                        while (shCount--) {
-                               emitcode ("lsl", "%s",
-                                         aopGet (AOP (result), 0));
-                               emitcode ("rol", "%s",
-                                         aopGet (AOP (result), 1));
+                               emitcode ("lsl", "%s", aopGet (AOP (result), 0));
+                               emitcode ("rol", "%s", aopGet (AOP (result), 1));
                        }
                }
                if (!lByteZ && !sameRegs (AOP (result), AOP (left))
@@ -3150,14 +3197,11 @@ genShiftLeftLit (iCode * ic)
                }
                while (shCount--) {
                        if (lByteZ) {
-                               emitcode ("lsl", "%s",
-                                         aopGet (AOP (result), 1));
+                               emitcode ("lsl", "%s", aopGet (AOP (result), 1));
                        }
                        else {
-                               emitcode ("lsl", "%s",
-                                         aopGet (AOP (result), 0));
-                               emitcode ("rol", "%s",
-                                         aopGet (AOP (result), 1));
+                               emitcode ("lsl", "%s", aopGet (AOP (result), 0));
+                               emitcode ("rol", "%s", aopGet (AOP (result), 1));
                        }
                }
                break;
@@ -3204,38 +3248,28 @@ genShiftLeftLit (iCode * ic)
                        switch (lByteZ) {
                        case 0:
                                while (shCount--) {
-                                       emitcode ("lsl", "%s",
-                                                 aopGet (AOP (result), 0));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 1));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 2));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 3));
+                                       emitcode ("lsl", "%s", aopGet (AOP (result), 0));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 1));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 2));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 3));
                                }
                                break;
                        case 1:
                                while (shCount--) {
-                                       emitcode ("lsl", "%s",
-                                                 aopGet (AOP (result), 1));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 2));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 3));
+                                       emitcode ("lsl", "%s", aopGet (AOP (result), 1));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 2));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 3));
                                }
                                break;
                        case 2:
                                while (shCount--) {
-                                       emitcode ("lsl", "%s",
-                                                 aopGet (AOP (result), 2));
-                                       emitcode ("rol", "%s",
-                                                 aopGet (AOP (result), 3));
+                                       emitcode ("lsl", "%s", aopGet (AOP (result), 2));
+                                       emitcode ("rol", "%s", aopGet (AOP (result), 3));
                                }
                                break;
                        case 3:
                                while (shCount--) {
-                                       emitcode ("lsl", "%s",
-                                                 aopGet (AOP (result), 3));
+                                       emitcode ("lsl", "%s", aopGet (AOP (result), 3));
                                }
                                break;
                        }
@@ -3347,44 +3381,34 @@ genShiftRightLit (iCode * ic)
                                        aopGet (AOP (left), offset), offset);
                                offset++;
                        }
-                       size = size = AOP_SIZE (result);
+                       size = AOP_SIZE (result);
                        offset = 0;
                }
                /* be as economical as possible */
                if (shCount <= 4) {
-                       offset = size - 1;
                        while (shCount--) {
-                               offset = size - 1;
                                size = AOP_SIZE (result);
+                               offset = size - 1;
                                while (size--) {
-                                       if (offset == (size - 1))
-                                               emitcode ("asr", "%s",
-                                                         aopGet (AOP
-                                                                 (result),
-                                                                 offset));
+                                       /* highest order byte */
+                                       if (offset == (AOP_SIZE(result)-1)) 
+                                               emitcode ("asr", "%s", aopGet (AOP (result), offset));
                                        else
-                                               emitcode ("lsr", "%s",
-                                                         aopGet (AOP
-                                                                 (result),
-                                                                 offset));
-                                       offset--;
+                                               emitcode ("ror", "%s", aopGet (AOP (result), offset));
+                                       offset--;                                               
                                }
                        }
                }
                else {
-                       emitcode ("ldi", "r24,lo8(%d)", shCount);
+                       emitcode ("ldi", "r24,<(%d)", shCount);
                        tlbl = newiTempLabel (NULL);
                        emitcode ("", "L%05d:", tlbl->key);
                        offset = size - 1;
                        while (size--) {
-                               if (offset == (size - 1))
-                                       emitcode ("asr", "%s",
-                                                 aopGet (AOP (result),
-                                                         offset));
+                               if (offset == (AOP_SIZE(result) - 1))
+                                       emitcode ("asr", "%s", aopGet (AOP (result), offset));
                                else
-                                       emitcode ("lsr", "%s",
-                                                 aopGet (AOP (result),
-                                                         offset));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), offset));
                                offset--;
                        }
                        emitcode ("dec", "r24");
@@ -3404,7 +3428,12 @@ genShiftRightLit (iCode * ic)
                        aopPut (AOP (result), aopGet (AOP (left), 0), 0);
                if (shCount >= 4) {
                        emitcode ("swap", "%s", aopGet (AOP (result), 0));
-                       emitcode ("andi", "%s,0x0f");
+                       if (AOP_ISHIGHREG(AOP(result),0)) {
+                               emitcode ("andi", "%s,0x0f",aopGet(AOP(result),0));
+                       } else {
+                               emitcode ("ldi","r24,0x0f");
+                               emitcode ("and", "%s,r24",aopGet(AOP(result),0));
+                       }
                        shCount -= 4;
                }
                while (shCount--)
@@ -3415,8 +3444,12 @@ genShiftRightLit (iCode * ic)
                        aopPut (AOP (result), aopGet (AOP (left), 1), 0);
                        aopPut (AOP (result), zero, 1);
                        emitcode ("swap", "%s", aopGet (AOP (result), 0));
-                       emitcode ("andi", "%s,0x0f",
-                                 aopGet (AOP (result), 0));
+                       if (AOP_ISHIGHREG(AOP(result),0)) {
+                               emitcode ("andi", "%s,0x0f", aopGet (AOP (result), 0));
+                       } else {
+                               emitcode ("ldi","r24,0x0f");
+                               emitcode ("and", "%s,r24",aopGet(AOP(result),0));
+                       }
                        shCount -= 12;
                        hByteZ = 1;
                }
@@ -3429,25 +3462,30 @@ genShiftRightLit (iCode * ic)
                if (shCount >= 4) {
                        shCount -= 4;
                        if (!sameRegs (AOP (left), AOP (result))) {
-                               aopPut (AOP (result), aopGet (AOP (left), 0),
-                                       0);
-                               aopPut (AOP (result), aopGet (AOP (left), 1),
-                                       1);
+                               aopPut (AOP (result), aopGet (AOP (left), 0), 0);
+                               aopPut (AOP (result), aopGet (AOP (left), 1), 1);
                        }
-                       emitcode ("mov", "r1,%s", aopGet (AOP (result), 1));
+                       if (!(AOP_ISHIGHREG(AOP(result),0) && AOP_ISHIGHREG(AOP(result),1))) {
+                               emitcode("ldi","r25,0x0f");
+                       }
+                       emitcode ("mov", "r24,%s", aopGet (AOP (result), 1));
+                       emitcode ("andi", "r24,0xf0");
                        emitcode ("swap", "%s", aopGet (AOP (result), 0));
-                       emitcode ("andi", "%s,0x0f",
-                                 aopGet (AOP (result), 0));
-                       emitcode ("andi", "r1,0xf0");
-                       emitcode ("or", "%s,r1", aopGet (AOP (result), 0));
+                       if (AOP_ISHIGHREG(AOP(result),0)) {
+                               emitcode ("andi", "%s,0x0f", aopGet (AOP (result), 0));
+                       } else {
+                               emitcode ("and", "%s,r25", aopGet (AOP (result), 0));
+                       }
+                       emitcode ("or", "%s,r24", aopGet (AOP (result), 0));
                        emitcode ("swap", "%s", aopGet (AOP (result), 1));
-                       emitcode ("andi", "%s,0x0f",
-                                 aopGet (AOP (result), 1));
+                       if (AOP_ISHIGHREG(AOP(result),1)) {
+                               emitcode ("andi", "%s,0x0f", aopGet (AOP (result), 1));
+                       } else {
+                               emitcode ("and", "%s,r24", aopGet (AOP (result), 1));                           
+                       }
                        while (shCount--) {
-                               emitcode ("lsr", "%s",
-                                         aopGet (AOP (result), 1));
-                               emitcode ("ror", "%s",
-                                         aopGet (AOP (result), 0));
+                               emitcode ("lsr", "%s", aopGet (AOP (result), 1));
+                               emitcode ("ror", "%s", aopGet (AOP (result), 0));
                        }
 
                }
@@ -3455,21 +3493,17 @@ genShiftRightLit (iCode * ic)
                    && shCount) {
                        offset = 0;
                        while (size--) {
-                               aopPut (AOP (result),
-                                       aopGet (AOP (left), offset), offset);
+                               aopPut (AOP (result), aopGet (AOP (left), offset), offset);
                                offset++;
                        }
                }
                while (shCount--) {
                        if (hByteZ) {
-                               emitcode ("lsr", "%s",
-                                         aopGet (AOP (result), 0));
+                               emitcode ("lsr", "%s", aopGet (AOP (result), 0));
                        }
                        else {
-                               emitcode ("lsr", "%s",
-                                         aopGet (AOP (result), 1));
-                               emitcode ("ror", "%s",
-                                         aopGet (AOP (result), 0));
+                               emitcode ("lsr", "%s", aopGet (AOP (result), 1));
+                               emitcode ("ror", "%s", aopGet (AOP (result), 0));
                        }
                }
                break;
@@ -3517,38 +3551,28 @@ genShiftRightLit (iCode * ic)
                        switch (hByteZ) {
                        case 0:
                                while (shCount--) {
-                                       emitcode ("lsr", "%s",
-                                                 aopGet (AOP (result), 3));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 2));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 1));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 0));
+                                       emitcode ("lsr", "%s", aopGet (AOP (result), 3));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 2));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 1));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 0));
                                }
                                break;
                        case 1:
                                while (shCount--) {
-                                       emitcode ("lsr", "%s",
-                                                 aopGet (AOP (result), 2));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 1));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 0));
+                                       emitcode ("lsr", "%s", aopGet (AOP (result), 2));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 1));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 0));
                                }
                                break;
                        case 2:
                                while (shCount--) {
-                                       emitcode ("lsr", "%s",
-                                                 aopGet (AOP (result), 1));
-                                       emitcode ("ror", "%s",
-                                                 aopGet (AOP (result), 0));
+                                       emitcode ("lsr", "%s", aopGet (AOP (result), 1));
+                                       emitcode ("ror", "%s", aopGet (AOP (result), 0));
                                }
                                break;
                        case 3:
                                while (shCount--) {
-                                       emitcode ("lsr", "%s",
-                                                 aopGet (AOP (result), 0));
+                                       emitcode ("lsr", "%s", aopGet (AOP (result), 0));
                                }
                                break;
                        }
@@ -3601,8 +3625,7 @@ genRightShift (iCode * ic)
        sign = !SPEC_USIGN (letype);
        if (!sameRegs (AOP (left), AOP (result))) {
                while (size--) {
-                       aopPut (AOP (result), aopGet (AOP (left), offset),
-                               offset);
+                       aopPut (AOP (result), aopGet (AOP (left), offset), offset);
                        offset++;
                }
                size = AOP_SIZE (result);
@@ -3611,11 +3634,9 @@ genRightShift (iCode * ic)
        while (size--) {
                if (first) {
                        if (sign)
-                               emitcode ("asr", "%s",
-                                         aopGet (AOP (result), offset));
+                               emitcode ("asr", "%s", aopGet (AOP (result), offset));
                        else
-                               emitcode ("lsr", "%s",
-                                         aopGet (AOP (result), offset));
+                               emitcode ("lsr", "%s", aopGet (AOP (result), offset));
                        first = 0;
                }
                else
@@ -3633,10 +3654,53 @@ genRightShift (iCode * ic)
 }
 
 /*-----------------------------------------------------------------*/
-/* R0Rsh - shift right r0 by known count                           */
+/* RRsh - shift right rn by known count                            */
+/*-----------------------------------------------------------------*/
+static void
+RRsh (int shCount,int reg)
+{
+       shCount &= 0x0007;      // shCount : 0..7
+
+       switch (shCount) {
+       case 0:
+               break;
+       case 1:
+               emitcode ("lsr", "r%d",reg);
+               break;
+       case 2:
+               emitcode ("lsr", "r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               break;
+       case 3:
+               emitcode ("swap", "r%d",reg);
+               emitcode ("lsl", "r%d",reg);
+               break;
+       case 4:
+               emitcode ("swap", "r%d",reg);
+               break;
+       case 5:
+               emitcode ("swap", "r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               break;
+       case 6:
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               break;
+       case 7:
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               emitcode ("lsr", "r%d",reg);
+               break;
+       }
+}
+
+/*-----------------------------------------------------------------*/
+/* RLsh - shift left rn by known count                             */
 /*-----------------------------------------------------------------*/
 static void
-R0Rsh (int shCount)
+RLsh (int shCount, int reg)
 {
        shCount &= 0x0007;      // shCount : 0..7
 
@@ -3644,33 +3708,33 @@ R0Rsh (int shCount)
        case 0:
                break;
        case 1:
-               emitcode ("lsr", "r0");
+               emitcode ("lsl", "r%d",reg);
                break;
        case 2:
-               emitcode ("lsr", "r0");
-               emitcode ("lsr", "r0");
+               emitcode ("lsl", "r%d",reg);
+               emitcode ("lsl", "r%d",reg);
                break;
        case 3:
-               emitcode ("swap", "r0");
-               emitcode ("lsl", "r0");
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsr", "r%d",reg);
                break;
        case 4:
-               emitcode ("swap", "r0");
+               emitcode ("swap", "r%d",reg);
                break;
        case 5:
-               emitcode ("swap", "r0");
-               emitcode ("lsr", "r0");
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsl", "r%d",reg);
                break;
        case 6:
-               emitcode ("swap", "r0");
-               emitcode ("lsr", "r0");
-               emitcode ("lsr", "r0");
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsl", "r%d",reg);
+               emitcode ("lsl", "r%d",reg);
                break;
        case 7:
-               emitcode ("swap", "r0");
-               emitcode ("lsr", "r0");
-               emitcode ("lsr", "r0");
-               emitcode ("lsr", "r0");
+               emitcode ("swap","r%d",reg);
+               emitcode ("lsl", "r%d",reg);
+               emitcode ("lsl", "r%d",reg);
+               emitcode ("lsl", "r%d",reg);
                break;
        }
 }
@@ -3696,15 +3760,16 @@ genUnpackBits (operand * result, char *rname, int ptype)
        case IPOINTER:
        case PPOINTER:
        case FPOINTER:
-               emitcode ("ld", "r0,%s+", rname);
+               emitcode ("ld", "r24,%s+", rname);
                break;
 
        case CPOINTER:
-               emitcode ("lpm", "r0,%s+", rname);
+               emitcode ("lpm", "r24,%s+", rname);
                break;
 
        case GPOINTER:
                emitcode ("call","__gptrget_pi");
+               emitcode ("mov","r24,r0");
                break;
        }
 
@@ -3716,16 +3781,16 @@ genUnpackBits (operand * result, char *rname, int ptype)
        if ((shCnt = SPEC_BSTR (etype)) || (SPEC_BLEN (etype) <= 8)) {
 
                /* shift right acc */
-               R0Rsh (shCnt);
+               RRsh (shCnt,24);
 
-               emitcode ("andi", "r0,lo(0x%x)",
+               emitcode ("andi", "r24,lo(0x%x)",
                          ((unsigned char) -1) >> (8 - SPEC_BLEN (etype)));
-               aopPut (AOP (result), "r0", offset++);
+               aopPut (AOP (result), "r24", offset++);
                goto finish;
        }
 
        /* bit field did not fit in a byte  */
-       aopPut (AOP (result), "r0", offset++);
+       aopPut (AOP (result), "r24", offset++);
 
        while (1) {
 
@@ -3735,11 +3800,11 @@ genUnpackBits (operand * result, char *rname, int ptype)
                case IPOINTER:
                case PPOINTER:
                case FPOINTER:
-                       emitcode ("ld", "r0,%s+");
+                       emitcode ("ld", "r24,%s+");
                        break;
 
                case CPOINTER:
-                       emitcode ("lpm", "r0,%s+");
+                       emitcode ("lpm", "r24,%s+");
                        break;
 
                case GPOINTER:
@@ -3752,12 +3817,12 @@ genUnpackBits (operand * result, char *rname, int ptype)
                if (rlen < 8)
                        break;
 
-               aopPut (AOP (result), "r0", offset++);
+               aopPut (AOP (result), "r24", offset++);
 
-       }
+       }
 
        if (rlen) {
-               aopPut (AOP (result), "r0", offset++);
+               aopPut (AOP (result), "r24", offset++);
        }
 
       finish:
@@ -3785,9 +3850,9 @@ genDataPointerGet (operand * left, operand * result, iCode * ic)
        size = AOP_SIZE (result);
        while (size--) {
                if (offset)
-                       sprintf (buffer, "(%s + %d)", l + 1, offset);
+                       sprintf (buffer, "(%s + %d)", l, offset);
                else
-                       sprintf (buffer, "%s", l + 1);
+                       sprintf (buffer, "%s", l);
                emitcode ("lds", "%s,%s", aopGet (AOP (result), offset++),
                          buffer);
        }
@@ -3800,7 +3865,7 @@ genDataPointerGet (operand * left, operand * result, iCode * ic)
 /* genNearPointerGet - emitcode for near pointer fetch             */
 /*-----------------------------------------------------------------*/
 static void
-genMemPointerGet (operand * left, operand * result, iCode * ic)
+genMemPointerGet (operand * left, operand * result, iCode * ic, iCode *pi)
 {
        asmop *aop = NULL;
        regs *preg = NULL;
@@ -3831,7 +3896,9 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
                aop = newAsmop (0);
                preg = getFreePtr (ic, &aop, FALSE, 0);
                if (isRegPair (AOP (left) )) {
-                   emitcode ("movw", "%s,%s",aop->aopu.aop_ptr->name);
+                       emitcode ("movw", "%s,%s",
+                                 aop->aopu.aop_ptr->name,
+                                 aopGet(AOP(left),0));
                } else {
                        emitcode ("mov", "%s,%s", aop->aopu.aop_ptr->name, 
                                  aopGet (AOP (left), 0));
@@ -3854,7 +3921,6 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
                exit (0);
        }
 
-       freeAsmop (left, NULL, ic, TRUE);
        aopOp (result, ic, FALSE);
 
        /* if bitfield then unpack the bits */
@@ -3866,7 +3932,7 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
                int offset = 0;
 
                while (size--) {
-                       if (size) {
+                       if (size || pi) {
                                emitcode ("ld","%s,%s+",aopGet(AOP(result),offset), rname);
                        } else {
                                emitcode ("ld","%s,%s",aopGet(AOP(result),offset), rname);
@@ -3877,6 +3943,20 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
        /* now some housekeeping stuff */
        if (gotFreePtr) {
                /* we had to allocate for this iCode */
+               if (pi) {
+                       if (isRegPair (AOP (left) )) {
+                               emitcode ("movw", "%s,%s",
+                                         aopGet (AOP(left),0),
+                                         aop->aopu.aop_ptr->name);
+                       } else {
+                               emitcode ("mov", "%s,%s", 
+                                         aopGet (AOP (left), 0),
+                                         aop->aopu.aop_ptr->name);
+                               emitcode ("mov", "%s,%s", 
+                                         aopGet (AOP (left), 1),
+                                         aop->aop_ptr2->name);
+                       }
+               }
                freeAsmop (NULL, aop, ic, TRUE);
        } else {
 
@@ -3885,15 +3965,17 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
                   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;
                        emitcode ("sbiw", "%s,%d",frname,size);
                }
        }
 
        /* done */
+       if (pi) pi->generated = 1;
+       freeAsmop (left, NULL, ic, TRUE);
        freeAsmop (result, NULL, ic, TRUE);
 
 }
@@ -3902,11 +3984,12 @@ genMemPointerGet (operand * left, operand * result, iCode * ic)
 /* genCodePointerGet - gget value from code space                  */
 /*-----------------------------------------------------------------*/
 static void
-genCodePointerGet (operand * left, operand * result, iCode * ic)
+genCodePointerGet (operand * left, operand * result, iCode * ic, iCode *pi)
 {
        int size, offset;
        sym_link *retype = getSpec (operandType (result));      
        asmop *aop = NULL;
+       int gotFreePtr = 0;
 
        aopOp (left, ic, FALSE);
 
@@ -3923,7 +4006,9 @@ genCodePointerGet (operand * left, operand * result, iCode * ic)
                        emitcode ("mov", "r30,%s", aopGet (AOP (left), 0));
                        emitcode ("mov", "r31,%s", aopGet (AOP (left), 1));
                }
+               gotFreePtr = 1;
        } 
+
        aopOp (result, ic, FALSE);
 
        /* if bit then unpack */
@@ -3934,79 +4019,137 @@ genCodePointerGet (operand * left, operand * result, iCode * ic)
                offset = 0;
 
                while (size--) {
-                       emitcode ("clr", "a");
-                       emitcode ("movc", "a,@a+dptr");
-                       aopPut (AOP (result), "a", offset++);
-                       if (size)
-                               emitcode ("inc", "dptr");
+                       if (size || pi) {
+                               emitcode ("lpm","%s,Z+",aopGet(AOP(result),offset++));
+                       } else {
+                               emitcode ("lpm","%s,Z",aopGet(AOP(result),offset++));
+                       }
+               }
+       }
+
+       /* now some housekeeping stuff */
+       if (gotFreePtr) {
+               /* we had to allocate for this iCode */
+               if (pi) {
+                       if (isRegPair(AOP (left))) {
+                               emitcode ("movw","%s,r30",aopGet (AOP (left), 0));
+                       } else {                        
+                               emitcode ("mov", "%s,r30", aopGet (AOP (left), 0));
+                               emitcode ("mov", "%s,r31", aopGet (AOP (left), 1));
+                       }
+               }
+               freeAsmop (NULL, aop, ic, TRUE);
+       } else {
+
+               /* we did not allocate which means left
+                  already in a pointer register, then
+                  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)) &&
+                   !pi) {
+                       int size = AOP_SIZE (result) - 1;
+                       emitcode ("sbiw", "r30,%d",size);
                }
        }
 
+       /* done */
+       if (pi) pi->generated=1;
        freeAsmop (left, NULL, ic, TRUE);
        freeAsmop (result, NULL, ic, TRUE);
+
 }
 
 /*-----------------------------------------------------------------*/
 /* genGenPointerGet - gget value from generic pointer space        */
 /*-----------------------------------------------------------------*/
 static void
-genGenPointerGet (operand * left, operand * result, iCode * ic)
+genGenPointerGet (operand * left, operand * result, iCode * ic, iCode *pi)
 {
        int size, offset;
+       int gotFreePtr = 0;
        sym_link *retype = getSpec (operandType (result));
+       asmop *aop = NULL;
 
        aopOp (left, ic, FALSE);
 
        /* if the operand is already in dptr
           then we do nothing else we move the value to dptr */
-       if (AOP_TYPE (left) != AOP_STR) {
-               /* if this is remateriazable */
-               if (AOP_TYPE (left) == AOP_IMMD) {
-                       emitcode ("mov", "dptr,%s", aopGet (AOP (left), 0));
-                       emitcode ("mov", "b,#%d", pointerCode (retype));
-               }
-               else {          /* we need to get it byte by byte */
-                       emitcode ("mov", "dpl,%s", aopGet (AOP (left), 0));
-                       emitcode ("mov", "dph,%s", aopGet (AOP (left), 1));
-                       if (options.model == MODEL_FLAT24) {
-                               emitcode ("mov", "dpx,%s",
-                                         aopGet (AOP (left), 2));
-                               emitcode ("mov", "b,%s",
-                                         aopGet (AOP (left), 3));
-                       }
-                       else {
-                               emitcode ("mov", "b,%s",
-                                         aopGet (AOP (left), 2));
-                       }
+       if (AOP_ISZ(AOP(left))) {
+               aop = AOP(left);
+       } else {
+               aop = newAsmop(0);
+               getFreePtr(ic,&aop,FALSE,TRUE);
+               if (isRegPair(AOP(left))) {
+                       emitcode ("movw", "r30,%s", aopGet (AOP (left), 0));
+               } else {
+                       emitcode ("mov", "r30,%s", aopGet (AOP (left), 0));
+                       emitcode ("mov", "r31,%s", aopGet (AOP (left), 1));
                }
+               emitcode ("mov", "r24,%s", aopGet (AOP (left), 2));
+               gotFreePtr=1;
        }
-       /* so dptr know contains the address */
-       freeAsmop (left, NULL, ic, TRUE);
+
+       /* so Z register now contains the address */
+
        aopOp (result, ic, FALSE);
 
        /* if bit then unpack */
        if (IS_BITVAR (retype))
-               genUnpackBits (result, "dptr", GPOINTER);
+               genUnpackBits (result, "Z", GPOINTER);
        else {
                size = AOP_SIZE (result);
                offset = 0;
 
                while (size--) {
-                       emitcode ("lcall", "__gptrget");
-                       aopPut (AOP (result), "a", offset++);
-                       if (size)
-                               emitcode ("inc", "dptr");
+                       if (size || pi) 
+                               emitcode ("call", "__gptrget_pi");
+                       else
+                               emitcode ("call", "__gptrget");
+                       aopPut (AOP (result), "r0", offset++);
                }
        }
 
-       freeAsmop (result, NULL, ic, TRUE);
-}
 
-/*-----------------------------------------------------------------*/
-/* genPointerGet - generate code for pointer get                   */
+       /* now some housekeeping stuff */
+       if (gotFreePtr) {
+               /* we had to allocate for this iCode */
+               if (pi) {
+                       if (isRegPair(AOP (left))) {
+                               emitcode ("movw","%s,r30",aopGet (AOP (left), 0));
+                       } else {                        
+                               emitcode ("mov", "%s,r30", aopGet (AOP (left), 0));
+                               emitcode ("mov", "%s,r31", aopGet (AOP (left), 1));
+                       }
+               }
+               freeAsmop (NULL, aop, ic, TRUE);
+       } else {
+
+               /* we did not allocate which means left
+                  already in a pointer register, then
+                  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)) &&
+                   !pi) {
+                       int size = AOP_SIZE (result) - 1;
+                       emitcode ("sbiw", "r30,%d",size);
+               }
+       }
+       if (pi) pi->generated=1;
+       freeAsmop (left, NULL, ic, TRUE);
+       freeAsmop (result, NULL, ic, TRUE);
+}
+
+/*-----------------------------------------------------------------*/
+/* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
 static void
-genPointerGet (iCode * ic)
+genPointerGet (iCode * ic, iCode *pi)
 {
        operand *left, *result;
        sym_link *type, *etype;
@@ -4037,15 +4180,15 @@ genPointerGet (iCode * ic)
        case IPOINTER:
        case PPOINTER:
        case FPOINTER:
-               genMemPointerGet (left, result, ic);
+               genMemPointerGet (left, result, ic, pi);
                break;
 
        case CPOINTER:
-               genCodePointerGet (left, result, ic);
+               genCodePointerGet (left, result, ic, pi);
                break;
 
        case GPOINTER:
-               genGenPointerGet (left, result, ic);
+               genGenPointerGet (left, result, ic, pi);
                break;
        }
 
@@ -4055,7 +4198,9 @@ genPointerGet (iCode * ic)
 /* genPackBits - generates code for packed bit storage             */
 /*-----------------------------------------------------------------*/
 static void
-genPackBits (sym_link * etype, operand * right, char *rname, int p_type)
+genPackBits (sym_link * etype,
+            operand * right,
+            char *rname, int p_type)
 {
        int shCount = 0;
        int offset = 0;
@@ -4067,7 +4212,7 @@ genPackBits (sym_link * etype, operand * right, char *rname, int p_type)
        bstr = SPEC_BSTR (etype);
 
        l = aopGet (AOP (right), offset++);
-       MOVA (l);
+       MOVR24 (l);
 
        /* if the bit lenth is less than or    */
        /* it exactly fits a byte then         */
@@ -4075,50 +4220,47 @@ genPackBits (sym_link * etype, operand * right, char *rname, int p_type)
                shCount = SPEC_BSTR (etype);
 
                /* shift left acc */
-               //    AccLsh(shCount);
-
-               if (SPEC_BLEN (etype) < 8) {    /* if smaller than a byte */
+               RLsh (shCount,24);
 
+               if (SPEC_BLEN (etype) < 8) {                    /* if smaller than a byte */
 
                        switch (p_type) {
                        case POINTER:
-                               emitcode ("mov", "b,a");
-                               emitcode ("mov", "a,@%s", rname);
-                               break;
-
+                       case IPOINTER:
+                       case PPOINTER:
                        case FPOINTER:
-                               emitcode ("mov", "b,a");
-                               emitcode ("movx", "a,@dptr");
+                               emitcode ("ld", "r1,%s",rname);
                                break;
 
                        case GPOINTER:
-                               emitcode ("push", "b");
-                               emitcode ("push", "acc");
-                               emitcode ("lcall", "__gptrget");
-                               emitcode ("pop", "b");
+                               emitcode ("push", "r1");
+                               emitcode ("push", "r24");
+                               emitcode ("call", "__gptrget");
+                               emitcode ("pop", "r1");
+                               emitcode ("mov","r24,r0");
                                break;
                        }
 
-                       emitcode ("anl", "a,#0x%02x", (unsigned char)
+                       emitcode ("andi", "r24,#0x%02x", (unsigned char)
                                  ((unsigned char) (0xFF << (blen + bstr)) |
                                   (unsigned char) (0xFF >> (8 - bstr))));
-                       emitcode ("orl", "a,b");
+                       emitcode ("or", "r24,r1");
                        if (p_type == GPOINTER)
-                               emitcode ("pop", "b");
+                               emitcode ("pop", "r1");
                }
        }
 
        switch (p_type) {
        case POINTER:
-               emitcode ("mov", "@%s,a", rname);
-               break;
-
+       case IPOINTER:
+       case PPOINTER:
        case FPOINTER:
-               emitcode ("movx", "@dptr,a");
+               emitcode("st","%s+,r24");
                break;
 
        case GPOINTER:
-               emitcode ("lcall", "__gptrput");
+               emitcode("mov","r0,r24");
+               emitcode ("call", "__gptrput_pi");
                break;
        }
 
@@ -4126,7 +4268,6 @@ genPackBits (sym_link * etype, operand * right, char *rname, int p_type)
        if (SPEC_BLEN (etype) <= 8)
                return;
 
-       emitcode ("inc", "%s", rname);
        rLen = SPEC_BLEN (etype);
 
        /* now generate for lengths greater than one byte */
@@ -4135,75 +4276,64 @@ genPackBits (sym_link * etype, operand * right, char *rname, int p_type)
                l = aopGet (AOP (right), offset++);
 
                rLen -= 8;
-               if (rLen <= 0)
+               if (rLen < 8)
                        break;
 
                switch (p_type) {
                case POINTER:
-                       if (*l == '@') {
-                               MOVA (l);
-                               emitcode ("mov", "@%s,a", rname);
-                       }
-                       else
-                               emitcode ("mov", "@%s,%s", rname, l);
-                       break;
-
+               case IPOINTER:
+               case PPOINTER:
                case FPOINTER:
-                       MOVA (l);
-                       emitcode ("movx", "@dptr,a");
+                       emitcode ("st", "%s+,%s",rname,l);
                        break;
 
                case GPOINTER:
-                       MOVA (l);
-                       emitcode ("lcall", "__gptrput");
+                       MOVR0 (l);
+                       emitcode ("lcall", "__gptrput_pi");
                        break;
                }
-               emitcode ("inc", "%s", rname);
        }
 
-       MOVA (l);
+       MOVR24 (l);
 
        /* last last was not complete */
        if (rLen) {
                /* save the byte & read byte */
                switch (p_type) {
                case POINTER:
-                       emitcode ("mov", "b,a");
-                       emitcode ("mov", "a,@%s", rname);
-                       break;
-
+               case IPOINTER:
+               case PPOINTER:
                case FPOINTER:
-                       emitcode ("mov", "b,a");
-                       emitcode ("movx", "a,@dptr");
+                       emitcode ("st","%s+,r24",rname);
                        break;
-
                case GPOINTER:
-                       emitcode ("push", "b");
-                       emitcode ("push", "acc");
+                       emitcode ("push", "r1");
+                       emitcode ("push", "r24");
                        emitcode ("lcall", "__gptrget");
-                       emitcode ("pop", "b");
+                       emitcode ("mov","r24,r0");
+                       emitcode ("pop", "r1");
                        break;
                }
 
-               emitcode ("anl", "a,#0x%02x", ((unsigned char) -1 << -rLen));
-               emitcode ("orl", "a,b");
+               emitcode ("andi", "r24,0x%02x", (((unsigned char) -1 << rLen) & 0xff));
+               emitcode ("or", "r24,r1");
        }
 
        if (p_type == GPOINTER)
-               emitcode ("pop", "b");
+               emitcode ("pop", "r1");
 
        switch (p_type) {
 
        case POINTER:
-               emitcode ("mov", "@%s,a", rname);
-               break;
-
+       case IPOINTER:
+       case PPOINTER:
        case FPOINTER:
-               emitcode ("movx", "@dptr,a");
+               emitcode ("st", "%s,r24", rname);
                break;
 
        case GPOINTER:
-               emitcode ("lcall", "__gptrput");
+               emitcode ("mov","r0,r24");
+               emitcode ("call", "__gptrput");
                break;
        }
 }
@@ -4223,10 +4353,10 @@ genDataPointerSet (operand * right, operand * result, iCode * ic)
        size = AOP_SIZE (right);
        while (size--) {
                if (offset)
-                       sprintf (buffer, "(%s + %d)", l + 1, offset);
+                       sprintf (buffer, "(%s + %d)", l, offset);
                else
-                       sprintf (buffer, "%s", l + 1);
-               emitcode ("mov", "%s,%s", buffer,
+                       sprintf (buffer, "%s", l);
+               emitcode ("sts", "%s,%s", buffer,
                          aopGet (AOP (right), offset++));
        }
 
@@ -4235,21 +4365,21 @@ genDataPointerSet (operand * right, operand * result, iCode * ic)
 }
 
 /*-----------------------------------------------------------------*/
-/* genNearPointerSet - emitcode for near pointer put                */
+/* genNearPointerSet - emitcode for near pointer put               */
 /*-----------------------------------------------------------------*/
 static void
-genNearPointerSet (operand * right, operand * result, iCode * ic)
+genMemPointerSet (operand * right, operand * result, iCode * ic, iCode *pi)
 {
        asmop *aop = NULL;
-       regs *preg = NULL;
-       char *rname, *l;
+       char *frname = NULL, *rname, *l;
+       int gotFreePtr = 0;
        sym_link *retype;
        sym_link *ptype = operandType (result);
-
+       
        retype = getSpec (operandType (right));
-
+       
        aopOp (result, ic, FALSE);
-
+       
        /* if the result is rematerializable &
           in data space & not a bit variable */
        if (AOP_TYPE (result) == AOP_IMMD &&
@@ -4257,23 +4387,35 @@ genNearPointerSet (operand * right, operand * result, iCode * ic)
                genDataPointerSet (right, result, ic);
                return;
        }
-
-       /* if the value is already in a pointer register
-          then don't need anything more */
-       if (!AOP_INPREG (AOP (result))) {
+       if (!AOP_INPREG(AOP(result))) {
                /* otherwise get a free pointer register */
                aop = newAsmop (0);
-               preg = getFreePtr (ic, &aop, FALSE, 0);
-               emitcode ("mov", "%s,%s",
-                         preg->name, aopGet (AOP (result), 0));
-               rname = preg->name;
+               getFreePtr (ic, &aop, FALSE, 0);
+               if (isRegPair (AOP (result) )) {
+                       emitcode ("movw", "%s,%s",aop->aopu.aop_ptr->name,
+                                 aopGet(AOP (result), 0));
+               } else {
+                       emitcode ("mov", "%s,%s", aop->aopu.aop_ptr->name, 
+                                 aopGet (AOP (result), 0));
+                       emitcode ("mov", "%s,%s", aop->aop_ptr2->name,
+                                 aopGet (AOP (result), 1));
+               }
+               gotFreePtr = 1;         
+       } else {
+               aop = AOP(result);
+               frname = aopGet(aop,0);
        }
-       else
-               rname = aopGet (AOP (result), 0);
-
-       freeAsmop (result, NULL, ic, TRUE);
+       
        aopOp (right, ic, FALSE);
-
+       if (AOP_ISX(aop)) {
+               rname = "X";
+       } else if (AOP_ISZ(aop)) {
+               rname = "Z";
+       } else {
+               werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+                       "pointer not in correct register");
+               exit (0);
+       }
        /* if bitfield then unpack the bits */
        if (IS_BITVAR (retype))
                genPackBits (retype, right, rname, POINTER);
@@ -4284,234 +4426,138 @@ genNearPointerSet (operand * right, operand * result, iCode * ic)
 
                while (size--) {
                        l = aopGet (AOP (right), offset);
-                       if (*l == '@') {
-                               MOVA (l);
-                               emitcode ("mov", "@%s,a", rname);
-                       }
+                       if (size || pi)
+                               emitcode ("st", "%s+,%s", rname,l);
                        else
-                               emitcode ("mov", "@%s,%s", rname, l);
-                       if (size)
-                               emitcode ("inc", "%s", rname);
+                               emitcode ("st", "%s,%s", rname,l);                              
                        offset++;
                }
        }
-
+       
        /* now some housekeeping stuff */
-       if (aop) {
+       if (gotFreePtr) {
                /* we had to allocate for this iCode */
-               freeAsmop (NULL, aop, ic, TRUE);
-       }
-       else {
-               /* we did not allocate which means left
-                  already in a pointer register, then
-                  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)) {
-                       int size = AOP_SIZE (right) - 1;
-                       while (size--)
-                               emitcode ("dec", "%s", rname);
-               }
-       }
-
-       /* done */
-       freeAsmop (right, NULL, ic, TRUE);
-
-
-}
-
-/*-----------------------------------------------------------------*/
-/* genPagedPointerSet - emitcode for Paged pointer put             */
-/*-----------------------------------------------------------------*/
-static void
-genPagedPointerSet (operand * right, operand * result, iCode * ic)
-{
-       asmop *aop = NULL;
-       regs *preg = NULL;
-       char *rname, *l;
-       sym_link *retype;
-
-       retype = getSpec (operandType (right));
-
-       aopOp (result, ic, FALSE);
-
-       /* if the value is already in a pointer register
-          then don't need anything more */
-       if (!AOP_INPREG (AOP (result))) {
-               /* otherwise get a free pointer register */
-               aop = newAsmop (0);
-               preg = getFreePtr (ic, &aop, FALSE, 0);
-               emitcode ("mov", "%s,%s",
-                         preg->name, aopGet (AOP (result), 0));
-               rname = preg->name;
-       }
-       else
-               rname = aopGet (AOP (result), 0);
-
-       freeAsmop (result, NULL, ic, TRUE);
-       aopOp (right, ic, FALSE);
-
-       /* if bitfield then unpack the bits */
-       if (IS_BITVAR (retype))
-               genPackBits (retype, right, rname, PPOINTER);
-       else {
-               /* we have can just get the values */
-               int size = AOP_SIZE (right);
-               int offset = 0;
-
-               while (size--) {
-                       l = aopGet (AOP (right), offset);
-
-                       MOVA (l);
-                       emitcode ("movx", "@%s,a", rname);
-
-                       if (size)
-                               emitcode ("inc", "%s", rname);
-
-                       offset++;
+               if (pi) {
+                       if (isRegPair (AOP (result) )) {
+                               emitcode ("movw", "%s,%s",
+                                         aopGet(AOP(result),0),
+                                         aop->aopu.aop_ptr->name);
+                       } else {
+                               emitcode ("mov", "%s,%s", aop->aopu.aop_ptr->name, 
+                                         aopGet (AOP (result), 0));
+                               emitcode ("mov", "%s,%s", aop->aop_ptr2->name,
+                                         aopGet (AOP (result), 1));
+                       }
                }
-       }
-
-       /* now some housekeeping stuff */
-       if (aop) {
-               /* we had to allocate for this iCode */
                freeAsmop (NULL, aop, ic, TRUE);
-       }
-       else {
+       } else {
+
                /* we did not allocate which means left
                   already in a pointer register, then
                   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 (right)->liveTo > ic->seq || ic->depth)) && !pi) {
                        int size = AOP_SIZE (right) - 1;
-                       while (size--)
-                               emitcode ("dec", "%s", rname);
+                       emitcode ("sbiw", "%s,%d",frname,size);
                }
        }
 
        /* done */
+       if (pi) pi->generated = 1;
+       freeAsmop (result, NULL, ic, TRUE);
        freeAsmop (right, NULL, ic, TRUE);
-
-
 }
 
 /*-----------------------------------------------------------------*/
-/* genFarPointerSet - set value from far space                     */
+/* genGenPointerSet - set value from generic pointer space         */
 /*-----------------------------------------------------------------*/
 static void
-genFarPointerSet (operand * right, operand * result, iCode * ic)
+genGenPointerSet (operand * right, operand * result, iCode * ic, iCode *pi)
 {
        int size, offset;
+       int gotFreePtr = 0;
        sym_link *retype = getSpec (operandType (right));
-
+       asmop *aop = NULL;       
+       
        aopOp (result, ic, FALSE);
-
+       
        /* if the operand is already in dptr
           then we do nothing else we move the value to dptr */
-       if (AOP_TYPE (result) != AOP_STR) {
-               /* if this is remateriazable */
-               if (AOP_TYPE (result) == AOP_IMMD)
-                       emitcode ("mov", "dptr,%s", aopGet (AOP (result), 0));
-               else {          /* we need to get it byte by byte */
-                       emitcode ("mov", "dpl,%s", aopGet (AOP (result), 0));
-                       emitcode ("mov", "dph,%s", aopGet (AOP (result), 1));
-                       if (options.model == MODEL_FLAT24) {
-                               emitcode ("mov", "dpx,%s",
-                                         aopGet (AOP (result), 2));
-                       }
+       if (AOP_ISZ(AOP(result))) {
+               aop = AOP(right);
+       } else {
+               aop = newAsmop(0);
+               getFreePtr(ic,&aop,FALSE,TRUE);
+               if (isRegPair(AOP(result))) {
+                       emitcode ("movw", "r30,%s", aopGet (AOP (result), 0));
+               } else {
+                       emitcode ("mov", "r30,%s", aopGet (AOP (result), 0));
+                       emitcode ("mov", "r31,%s", aopGet (AOP (result), 1));
                }
+               emitcode ("mov", "r24,%s",  aopGet (AOP (result), 2));
+               gotFreePtr=1;
        }
-       /* so dptr know contains the address */
-       freeAsmop (result, NULL, ic, TRUE);
+       
+       /* so Z register now contains the address */
        aopOp (right, ic, FALSE);
-
+       
        /* if bit then unpack */
        if (IS_BITVAR (retype))
-               genPackBits (retype, right, "dptr", FPOINTER);
+               genUnpackBits (result, "Z", GPOINTER);
        else {
                size = AOP_SIZE (right);
                offset = 0;
-
+               
                while (size--) {
-                       char *l = aopGet (AOP (right), offset++);
-                       MOVA (l);
-                       emitcode ("movx", "@dptr,a");
-                       if (size)
-                               emitcode ("inc", "dptr");
+                       char *l = aopGet(AOP (right), offset++);
+                       MOVR0(l);
+                       
+                       if (size || pi) 
+                               emitcode ("call", "__gptrput_pi");
+                       else
+                               emitcode ("call", "__gptrput");
                }
        }
 
-       freeAsmop (right, NULL, ic, TRUE);
-}
-
-/*-----------------------------------------------------------------*/
-/* genGenPointerSet - set value from generic pointer space         */
-/*-----------------------------------------------------------------*/
-static void
-genGenPointerSet (operand * right, operand * result, iCode * ic)
-{
-       int size, offset;
-       sym_link *retype = getSpec (operandType (right));
-
-       aopOp (result, ic, FALSE);
-
-       /* if the operand is already in dptr
-          then we do nothing else we move the value to dptr */
-       if (AOP_TYPE (result) != AOP_STR) {
-               /* if this is remateriazable */
-               if (AOP_TYPE (result) == AOP_IMMD) {
-                       emitcode ("mov", "dptr,%s", aopGet (AOP (result), 0));
-                       emitcode ("mov", "b,%s + 1",
-                                 aopGet (AOP (result), 0));
-               }
-               else {          /* we need to get it byte by byte */
-                       emitcode ("mov", "dpl,%s", aopGet (AOP (result), 0));
-                       emitcode ("mov", "dph,%s", aopGet (AOP (result), 1));
-                       if (options.model == MODEL_FLAT24) {
-                               emitcode ("mov", "dpx,%s",
-                                         aopGet (AOP (result), 2));
-                               emitcode ("mov", "b,%s",
-                                         aopGet (AOP (result), 3));
-                       }
-                       else {
-                               emitcode ("mov", "b,%s",
-                                         aopGet (AOP (result), 2));
+       /* now some housekeeping stuff */
+       if (gotFreePtr) {
+               /* we had to allocate for this iCode */
+               if (pi) {
+                       if (isRegPair(AOP(result))) {
+                               emitcode ("movw", "%s,r30", aopGet (AOP (result), 0));
+                       } else {
+                               emitcode ("mov", "%s,r30", aopGet (AOP (result), 0));
+                               emitcode ("mov", "%s,r31", aopGet (AOP (result), 1));
                        }
                }
-       }
-       /* so dptr know contains the address */
-       freeAsmop (result, NULL, ic, TRUE);
-       aopOp (right, ic, FALSE);
-
-       /* if bit then unpack */
-       if (IS_BITVAR (retype))
-               genPackBits (retype, right, "dptr", GPOINTER);
-       else {
-               size = AOP_SIZE (right);
-               offset = 0;
+               freeAsmop (NULL, aop, ic, TRUE);
+       } else {
 
-               while (size--) {
-                       char *l = aopGet (AOP (right), offset++);
-                       MOVA (l);
-                       emitcode ("lcall", "__gptrput");
-                       if (size)
-                               emitcode ("inc", "dptr");
+               /* we did not allocate which means left
+                  already in a pointer register, then
+                  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)) && !pi) {
+                       int size = AOP_SIZE (right) - 1;
+                       emitcode ("sbiw", "r30,%d",size);
                }
        }
-
+       if (pi) pi->generated = 1;
        freeAsmop (right, NULL, ic, TRUE);
+       freeAsmop (result, NULL, ic, TRUE);
 }
 
 /*-----------------------------------------------------------------*/
 /* 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;
@@ -4532,20 +4578,6 @@ genPointerSet (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
@@ -4554,20 +4586,18 @@ genPointerSet (iCode * ic)
 
        case POINTER:
        case IPOINTER:
-               genNearPointerSet (right, result, ic);
-               break;
-
        case PPOINTER:
-               genPagedPointerSet (right, result, ic);
-               break;
-
        case FPOINTER:
-               genFarPointerSet (right, result, ic);
+               genMemPointerSet (right, result, ic, pi);
                break;
 
        case GPOINTER:
-               genGenPointerSet (right, result, ic);
+               genGenPointerSet (right, result, ic, pi);
                break;
+
+       default:
+         werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
+                 "genPointerSet: illegal pointer type");
        }
 
 }
@@ -4579,32 +4609,39 @@ static void
 genIfx (iCode * ic, iCode * popIc)
 {
        operand *cond = IC_COND (ic);
-       int isbit = 0;
+       char *cname ;
+       symbol *lbl;
+       int tob = 0;
 
        aopOp (cond, ic, FALSE);
 
-       /* get the value into acc */
-       if (AOP_TYPE (cond) != AOP_CRY)
-               toBoolean (cond, "", 0);
-       else
-               isbit = 1;
+       /* get the value into acc */    
+       if (AOP_SIZE(cond) == 1 && AOP_ISHIGHREG(AOP(cond),0)) {
+               cname = aopGet(AOP(cond),0);
+       } else {
+               toBoolean (cond, "r24", 0);
+               tob = 1;
+               cname = "r24";
+       }
        /* the result is now in the accumulator */
        freeAsmop (cond, NULL, ic, TRUE);
 
        /* if there was something to be popped then do it */
-       if (popIc)
+       if (popIc) {
                genIpop (popIc);
-
-       /* if the condition is  a bit variable */
-       /*     if (isbit && IS_ITEMP(cond) && SPIL_LOC(cond)) { */
-       /*  //  genIfxJump(ic,SPIL_LOC(cond)->rname); */
-       /*     } */
-       /*     else */
-       /*  if (isbit && !IS_ITEMP(cond)) */
-       /*    //      genIfxJump(ic,OP_SYMBOL(cond)->rname); */
-       /*  else */
-       /*    // genIfxJump(ic,"a"); */
-
+               emitcode("cpi","%s,0",cname);
+       } else if (!tob) emitcode("cpi","%s,0",cname);
+
+       lbl = newiTempLabel(NULL);
+       if (IC_TRUE(ic)) {
+               emitcode ("breq","L%05d",lbl->key);
+               emitcode ("jmp","L%05d",IC_TRUE(ic)->key);
+               emitcode ("","L%05d:",lbl->key);
+       } else {
+               emitcode ("brne","L%05d",lbl->key);
+               emitcode ("jmp","L%05d",IC_FALSE(ic)->key);
+               emitcode ("","L%05d:",lbl->key);
+       }
        ic->generated = 1;
 }
 
@@ -4618,42 +4655,57 @@ genAddrOf (iCode * ic)
        int size, offset;
 
        aopOp (IC_RESULT (ic), ic, FALSE);
-
+       assert(AOP_SIZE(IC_RESULT(ic)) >= 2);
        /* if the operand is on the stack then we
           need to get the stack offset of this
           variable */
        if (sym->onStack) {
-               /* if it has an offset then we need to compute
-                  it */
+               /* if it has an offset then we need to compute it */
                if (sym->stack) {
-                       emitcode ("mov", "a,_bp");
-                       emitcode ("add", "a,#0x%02x",
-                                 ((char) sym->stack & 0xff));
-                       aopPut (AOP (IC_RESULT (ic)), "a", 0);
+                       if (allHigh(AOP(IC_RESULT(ic)))) {
+                               if (isRegPair (AOP(IC_RESULT(ic)))) {
+                                       emitcode ("movw","%s,r28",aopGet(AOP(IC_RESULT(ic)),0));                                        
+                               } else {
+                                       emitcode ("mov","%s,r28",aopGet(AOP(IC_RESULT(ic)),0));
+                                       emitcode ("mov","%s,r29",aopGet(AOP(IC_RESULT(ic)),1));                                 
+                               }
+                               if (sym->stack < 0) {
+                                       emitcode("subi","%s,<(%d)",aopGet(AOP(IC_RESULT(ic)),0),-sym->stack);
+                                       emitcode("sbci","%s,>(%d)",aopGet(AOP(IC_RESULT(ic)),1),-sym->stack);
+                               } else {
+                                       emitcode("subi","%s,<(-%d)",aopGet(AOP(IC_RESULT(ic)),0),sym->stack);
+                                       emitcode("sbci","%s,>(-%d)",aopGet(AOP(IC_RESULT(ic)),1),sym->stack);
+                               }
+                       } else {
+                               emitcode("movw","r24,r28");
+                               if (sym->stack > -63 && sym->stack < 63) {
+                                       if (sym->stack < 0)
+                                               emitcode("sbiw","r24,%d",-sym->stack);
+                                       else
+                                               emitcode("sbiw","r24,%d",sym->stack);
+                               } else {                                        
+                                       if (sym->stack < 0) {
+                                               emitcode("subi","r24,<(%d)",-sym->stack);
+                                               emitcode("sbci","r25,>(%d)",-sym->stack);
+                                       } else {
+                                               emitcode("subi","r24,<(-%d)",sym->stack);
+                                               emitcode("sbci","r25,>(-%d)",sym->stack);
+                                       }
+                               }
+                               
+                               aopPut(AOP(IC_RESULT(ic)),"r24",0);
+                               aopPut(AOP(IC_RESULT(ic)),"r25",1);
+                       }
                }
                else {
-                       /* we can just move _bp */
-                       aopPut (AOP (IC_RESULT (ic)), "_bp", 0);
+                       aopPut(AOP(IC_RESULT(ic)),"r28",0);
+                       aopPut(AOP(IC_RESULT(ic)),"r29",1);
                }
                /* fill the result with zero */
-               size = AOP_SIZE (IC_RESULT (ic)) - 1;
-
-
-               if (options.stack10bit && size < (FPTRSIZE - 1)) {
-                       fprintf (stderr,
-                                "*** warning: pointer to stack var truncated.\n");
-               }
-
-               offset = 1;
+               size = AOP_SIZE (IC_RESULT (ic)) - 2;
+               offset = 2;
                while (size--) {
-                       /* Yuck! */
-                       if (options.stack10bit && offset == 2) {
-                               aopPut (AOP (IC_RESULT (ic)), "#0x40",
-                                       offset++);
-                       }
-                       else {
-                               aopPut (AOP (IC_RESULT (ic)), zero, offset++);
-                       }
+                       aopPut (AOP (IC_RESULT (ic)), zero, offset++);
                }
 
                goto release;
@@ -4662,13 +4714,13 @@ genAddrOf (iCode * ic)
        /* object not on stack then we need the name */
        size = AOP_SIZE (IC_RESULT (ic));
        offset = 0;
-
+       assert(size<=2);
        while (size--) {
                char s[SDCC_NAME_MAX];
                if (offset)
-                       sprintf (s, "#(%s >> %d)", sym->rname, offset * 8);
+                       sprintf (s, ">(%s)", sym->rname);
                else
-                       sprintf (s, "#%s", sym->rname);
+                       sprintf (s, "<(%s)", sym->rname);
                aopPut (AOP (IC_RESULT (ic)), s, offset++);
        }
 
@@ -4899,8 +4951,6 @@ genCast (iCode * ic)
 
                /* pointer to generic pointer */
                if (IS_GENPTR (ctype)) {
-                       char *l = zero;
-
                        if (IS_PTR (type))
                                p_type = DCL_TYPE (type);
                        else {
@@ -4916,30 +4966,22 @@ genCast (iCode * ic)
                                        aopGet (AOP (right), offset), offset);
                                offset++;
                        }
-                       /* the last byte depending on type */
-                       switch (p_type) {
-                       case IPOINTER:
-                       case POINTER:
-                               l = zero;
-                               break;
-                       case FPOINTER:
-                               l = one;
-                               break;
-                       case CPOINTER:
-                               l = "#0x02";
-                               break;
-                       case PPOINTER:
-                               l = "#0x03";
-                               break;
-
-                       default:
-                               /* this should never happen */
-                               werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
-                                       "got unknown pointer type");
-                               exit (1);
+                   
+                   /* the last byte depending on type */
+                   {
+                       int gpVal = pointerTypeToGPByte(p_type, NULL, NULL);
+                       char gpValStr[10];
+                       
+                       if (gpVal == -1)
+                       {
+                           // pointerTypeToGPByte will have bitched.
+                           exit(1);
                        }
-                       aopPut (AOP (result), l, GPTRSIZE - 1);
-                       goto release;
+                       
+                       sprintf(gpValStr, "#0x%x", gpVal);
+                       aopPut (AOP (result), gpValStr, GPTRSIZE - 1);
+                   }               
+                   goto release;
                }
 
                /* just copy the pointers */
@@ -4972,8 +5014,11 @@ genCast (iCode * ic)
        }
        else {
                /* we need to extend the sign :{ */
+                // PENDING: Does nothing on avr
+#if 0
                char *l = aopGet (AOP (right), AOP_SIZE (right) - 1);
                MOVA (l);
+#endif
                emitcode ("rlc", "a");
                emitcode ("subb", "a,acc");
                while (size--)
@@ -5045,7 +5090,8 @@ genDjnz (iCode * ic, iCode * ifx)
 static char *recvregs[8] = {
        "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23"
 };
-static recvCnt = 0;
+
+static int recvCnt = 0;
 
 /*-----------------------------------------------------------------*/
 /* genReceive - generate code for a receive iCode                  */
@@ -5063,6 +5109,18 @@ genReceive (iCode * ic)
        freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
 }
 
+/*-----------------------------------------------------------------*/
+/* genDummyRead - generate code for dummy read of volatiles        */
+/*-----------------------------------------------------------------*/
+static void
+genDummyRead (iCode * ic)
+{
+  emitcode (";     genDummyRead","");
+  emitcode (";     not implemented","");
+
+  ic = ic;
+}
+
 /*-----------------------------------------------------------------*/
 /* gen51Code - generate code for 8051 based controllers            */
 /*-----------------------------------------------------------------*/
@@ -5078,12 +5136,8 @@ genAVRCode (iCode * lic)
        if (allocInfo)
                printAllocInfo (currFunc, codeOutFile);
        /* if debug information required */
-       /*     if (options.debug && currFunc) { */
-       if (currFunc) {
-               cdbSymbol (currFunc, cdbFile, FALSE, TRUE);
-               _G.debugLine = 1;
-               emitcode ("", ".type %s,@function", currFunc->name);
-               _G.debugLine = 0;
+       if (options.debug && currFunc) {
+               debugFile->writeFunction (currFunc, lic);
        }
        /* stack pointer name */
        spname = "sp";
@@ -5093,11 +5147,7 @@ genAVRCode (iCode * lic)
 
                if (cln != ic->lineno) {
                        if (options.debug) {
-                               _G.debugLine = 1;
-                               emitcode ("", "C$%s$%d$%d$%d ==.",
-                                         FileBaseName (ic->filename),
-                                         ic->lineno, ic->level, ic->block);
-                               _G.debugLine = 0;
+                               debugFile->writeCLine (ic);
                        }
                        emitcode (";", "%s %d", ic->filename, ic->lineno);
                        cln = ic->lineno;
@@ -5260,12 +5310,12 @@ genAVRCode (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;
@@ -5294,10 +5344,12 @@ genAVRCode (iCode * lic)
                        addSet (&_G.sendSet, ic);
                        break;
 
+               case DUMMY_READ_VOLATILE:
+                       genDummyRead (ic);
+                       break;
+
                default:
                        ic = ic;
-                       /*      piCode(ic,stdout); */
-
                }
        }