* src/pic16/ralloc.c (packRegsForAccUse): disabled functions with #if to eliminate...
[fw/sdcc] / src / pic16 / gen.c
index 35eebd89c4a0358db87a6b734875e77861824951..e52b0ed56f36a0a9d1d579e5aa8eb5bb87a0d00b 100644 (file)
@@ -1,5 +1,5 @@
 /*-------------------------------------------------------------------------
 gen.c - source file for code generation for pic16
+ gen.c - source file for code generation for pic16
 
   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
          and -  Jean-Louis VERN.jlvern@writeme.com (1999)
 #include "ralloc.h"
 #include "pcode.h"
 #include "gen.h"
-
+#include "genutils.h"
+#include "device.h"
+#include "main.h"
 
 extern void pic16_genUMult8X8_16 (operand *, operand *,operand *,pCodeOpReg *);
 extern void pic16_genSMult8X8_16 (operand *, operand *,operand *,pCodeOpReg *);
 void pic16_genMult8X8_8 (operand *, operand *,operand *);
-pCode *pic16_AssembleLine(char *line);
+pCode *pic16_AssembleLine(char *line, int peeps);
 extern void pic16_printpBlock(FILE *of, pBlock *pb);
 static asmop *newAsmop (short type);
-static pCodeOp *popRegFromString(char *str, int size, int offset);
+static pCodeOp *pic16_popRegFromString(char *str, int size, int offset);
+extern pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...);
 static void mov2w (asmop *aop, int offset);
 static int aopIdx (asmop *aop, int offset);
 
@@ -61,7 +64,6 @@ static int optimized_for_speed = 0;
   hack hack
 
 */
-int options_no_movff = 1;
 
 /* max_key keeps track of the largest label number used in 
    a function. This is then used to adjust the label offset
@@ -75,6 +77,11 @@ unsigned int pic16aopLiteral (value *val, int offset);
 const char *pic16_AopType(short type);
 static iCode *ifxForOp ( operand *op, iCode *ic );
 
+void pic16_pushpCodeOp(pCodeOp *pcop);
+void pic16_poppCodeOp(pCodeOp *pcop);
+
+
+
 #define BYTEofLONG(l,b) ( (l>> (b<<3)) & 0xff)
 
 /* this is the down and dirty file with all kinds of 
@@ -86,12 +93,24 @@ static char *zero = "#0x00";
 static char *one  = "#0x01";
 static char *spname = "sp";
 
+
+/*
+ * Function return value policy (MSB-->LSB):
+ *  8 bits     -> WREG
+ * 16 bits     -> PRODL:WREG
+ * 24 bits     -> PRODH:PRODL:WREG
+ * 32 bits     -> FSR0L:PRODH:PRODL:WREG
+ * >32 bits    -> on stack, and FSR0 points to the beginning
+ *
+ */
+
 char *fReturnpic16[] = {"temp1","temp2","temp3","temp4" };
 //char *fReturn390[] = {"dpl","dph","dpx", "b","a" };
 unsigned pic16_fReturnSizePic = 4; /* shared with ralloc.c */
 static char **fReturn = fReturnpic16;
 
-static char *accUse[] = {"a","b"};
+static char *accUse[] = {"WREG"};
 
 //static short rbank = -1;
 
@@ -184,6 +203,32 @@ void DEBUGpic16_pic16_AopTypeSign(int line_no, operand *left, operand *right, op
 
 }
 
+void pic16_emitpcomment (char *fmt, ...)
+{
+    va_list ap;
+    char lb[INITIAL_INLINEASM];  
+    char *lbp = lb;
+
+    va_start(ap,fmt);   
+
+    lb[0] = ';';
+    vsprintf(lb+1,fmt,ap);
+
+    while (isspace(*lbp)) lbp++;
+
+    if (lbp && *lbp) 
+        lineCurr = (lineCurr ?
+                    connectLine(lineCurr,newLineNode(lb)) :
+                    (lineHead = newLineNode(lb)));
+    lineCurr->isInline = _G.inLine;
+    lineCurr->isDebug  = _G.debugLine;
+
+    pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(lb));
+    va_end(ap);
+
+//     fprintf(stderr, "%s\n", lb);
+}
+
 void DEBUGpic16_emitcode (char *inst,char *fmt, ...)
 {
     va_list ap;
@@ -219,7 +264,6 @@ void DEBUGpic16_emitcode (char *inst,char *fmt, ...)
 //     fprintf(stderr, "%s\n", lb);
 }
 
-
 void pic16_emitpLabel(int key)
 {
   pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(NULL,key+100+labelOffset));
@@ -243,6 +287,10 @@ void pic16_emitpcodeNULLop(PIC_OPCODE poc)
 
 }
 
+
+#if 1
+#define pic16_emitcode DEBUGpic16_emitcode
+#else
 /*-----------------------------------------------------------------*/
 /* pic16_emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
@@ -274,11 +322,12 @@ void pic16_emitcode (char *inst,char *fmt, ...)
 
 // VR    fprintf(stderr, "lb = <%s>\n", lbp);
 
-    if(pic16_debug_verbose)
-      pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(lb));
+//    if(pic16_debug_verbose)
+//      pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(lb));
 
     va_end(ap);
 }
+#endif
 
 
 /*-----------------------------------------------------------------*/
@@ -430,6 +479,7 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx)
   //  DEBUGpic16_emitcode("; ***","%s lbl->key=%d, (lab offset=%d)",__FUNCTION__,resIfx->lbl->key,labelOffset);
 
 }
+#if 0
 /*-----------------------------------------------------------------*/
 /* pointerCode - returns the code for a pointer type               */
 /*-----------------------------------------------------------------*/
@@ -439,7 +489,7 @@ static int pointerCode (sym_link *etype)
     return PTR_TYPE(SPEC_OCLS(etype));
 
 }
-
+#endif
 /*-----------------------------------------------------------------*/
 /* aopForSym - for a true symbol                                   */
 /*-----------------------------------------------------------------*/
@@ -459,6 +509,9 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
     /* if it is on the stack or indirectly addressable */
     /* space we need to assign either r0 or r1 to it   */    
     if ((sym->onStack && !options.stack10bit) || sym->iaccess) {
+
+       DEBUGpic16_emitcode("; ***", "sum->onStack || sym->iaccess");
+       
         sym->aop = aop = newAsmop(0);
         aop->aopu.aop_ptr = getFreePtr(ic,&aop,result);
         aop->size = getSize(sym->type);
@@ -528,7 +581,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
         sym->aop = aop = newAsmop (AOP_CRY);
         aop->aopu.aop_dir = sym->rname ;
         aop->size = getSize(sym->type);
-       //DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d",__LINE__,sym->rname,aop->size);
+       DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d",__LINE__,sym->rname,aop->size);
         return aop;
     }
     /* if it is in direct space */
@@ -537,6 +590,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
         aop->aopu.aop_dir = sym->rname ;
         aop->size = getSize(sym->type);
        DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d",__LINE__,sym->rname,aop->size);
+       pic16_allocDirReg( IC_LEFT(ic) );
         return aop;
     }
 
@@ -556,24 +610,29 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
     /* in which case DPTR gets the address */
     sym->aop = aop = newAsmop(AOP_PCODE);
 
-    aop->aopu.pcop = pic16_popGetImmd(sym->rname,0,0);
-    PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space);
-    PCOI(aop->aopu.pcop)->index = 0;
+/* change the next if to 1 to revert to good old immediate code */
+       if(IN_CODESPACE(space)) {
+               aop->aopu.pcop = pic16_popGetImmd(sym->rname, 0, 0);
+               PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space);
+               PCOI(aop->aopu.pcop)->index = 0;
+       } else {
+               /* try to allocate via direct register */
+               aop->aopu.pcop = pic16_popRegFromString(sym->rname, getSize(sym->type), 0);
+//             aop->size = getSize( sym->type );
+       }
 
-    DEBUGpic16_emitcode(";","%d: rname %s, val %d, const = %d",
-                       __LINE__,sym->rname, 0, PCOI(aop->aopu.pcop)->_const);
+       DEBUGpic16_emitcode(";","%d: rname %s, val %d, const = %d",
+               __LINE__,sym->rname, 0, PCOI(aop->aopu.pcop)->_const);
 
-    pic16_allocDirReg (IC_LEFT(ic));
+       pic16_allocDirReg (IC_LEFT(ic));
 
-    aop->size = FPTRSIZE; 
-/*
-    DEBUGpic16_emitcode(";","%d size = %d, name =%s",__LINE__,aop->size,sym->rname);
-    sym->aop = aop = newAsmop(AOP_DPTR);
-    pic16_emitcode ("mov","dptr,#%s", sym->rname);
-    aop->size = getSize(sym->type);
+       if(IN_DIRSPACE( space ))
+               aop->size = PTRSIZE;
+       else if(IN_CODESPACE( space ))
+               aop->size = FPTRSIZE;
+       else aop->size = AOP_SIZE( IC_LEFT(ic) );
 
     DEBUGpic16_emitcode(";","%d size = %d",__LINE__,aop->size);
-*/
 
     /* if it is in code space */
     if (IN_CODESPACE(space))
@@ -588,40 +647,77 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
 static asmop *aopForRemat (operand *op) // x symbol *sym)
 {
   symbol *sym = OP_SYMBOL(op);
-  iCode *ic = NULL;
+  iCode *ic = NULL, *oldic;
   asmop *aop = newAsmop(AOP_PCODE);
   int val = 0;
   int offset = 0;
+  int viaimmd=0;
 
-  ic = sym->rematiCode;
 
-  DEBUGpic16_emitcode(";","%s %d",__FUNCTION__,__LINE__);
-  if(IS_OP_POINTER(op)) {
-    DEBUGpic16_emitcode(";","%s %d IS_OP_POINTER",__FUNCTION__,__LINE__);
-  }
-  for (;;) {
-    if (ic->op == '+') {
-      val += (int) operandLitValue(IC_RIGHT(ic));
-    } else if (ic->op == '-') {
-      val -= (int) operandLitValue(IC_RIGHT(ic));
-    } else
-      break;
+       ic = sym->rematiCode;
+
+       DEBUGpic16_emitcode(";","%s %d",__FUNCTION__,__LINE__);
        
-    ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
-  }
+       if(IS_OP_POINTER(op)) {
+               DEBUGpic16_emitcode(";","%s %d IS_OP_POINTER",__FUNCTION__,__LINE__);
+       }
+
+       for (;;) {
+               oldic = ic;
+
+//             pic16_emitpcomment("ic: %s\n", printILine(ic));
+       
+               if (ic->op == '+') {
+                       val += (int) operandLitValue(IC_RIGHT(ic));
+               } else if (ic->op == '-') {
+                       val -= (int) operandLitValue(IC_RIGHT(ic));
+               } else
+                       break;
+               
+               ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
+       }
+
+       offset = OP_SYMBOL(IC_LEFT(ic))->offset;
+
+       if(!op->isaddr)viaimmd++; else viaimmd=0;
+               
+/* set the following if to 1 to revert to good old immediate code */
+       if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(op)))
+               || viaimmd) {
+
+               pic16_emitpcomment("%s:%d immediate", __FILE__, __LINE__);
+
+               aop->aopu.pcop = pic16_popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname, 0, val);
+
+#if 0
+               PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op));
+#else
+               PCOI(aop->aopu.pcop)->_const = IS_CODEPTR(operandType(op));
+#endif
+
+               PCOI(aop->aopu.pcop)->index = val;
+       } else {
+               pic16_emitpcomment("%s:%d dir", __FILE__, __LINE__);
+
+               aop->aopu.pcop = pic16_popRegFromString(OP_SYMBOL(IC_LEFT(ic))->rname, getSize( OP_SYMBOL( IC_LEFT(ic))->type), val);
+//             aop->size = AOP_SIZE( IC_LEFT(ic) );
+       }
+
 
-  offset = OP_SYMBOL(IC_LEFT(ic))->offset;
-  aop->aopu.pcop = pic16_popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname,0,val);
-  PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op));
-  PCOI(aop->aopu.pcop)->index = val;
+       DEBUGpic16_emitcode(";","%d: rname %s, val %d, const = %d",
+               __LINE__,OP_SYMBOL(IC_LEFT(ic))->rname,
+#if 0
+               val, IS_PTR_CONST(operandType(op)));
+#else
+               val, IS_CODEPTR(operandType(op)));
+#endif
 
-  DEBUGpic16_emitcode(";","%d: rname %s, val %d, const = %d",
-                     __LINE__,OP_SYMBOL(IC_LEFT(ic))->rname,
-                     val, IS_PTR_CONST(operandType(op)));
+//     DEBUGpic16_emitcode(";","aop type  %s",pic16_AopType(AOP_TYPE(IC_LEFT(ic))));
 
-  //    DEBUGpic16_emitcode(";","aop type  %s",pic16_AopType(AOP_TYPE(IC_LEFT(ic))));
+       pic16_allocDirReg (IC_LEFT(ic));
 
-  pic16_allocDirReg (IC_LEFT(ic));
+       if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(op)) ))
+               aop->code = 1;
 
   return aop;        
 }
@@ -754,7 +850,8 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
     if (!op)
         return ;
 
-       DEBUGpic16_emitcode(";","%d",__LINE__);
+//     DEBUGpic16_emitcode(";","%s %d",__FUNCTION__, __LINE__);
+
     /* if this a literal */
     if (IS_OP_LITERAL(op)) {
         op->aop = aop = newAsmop(AOP_LIT);
@@ -765,7 +862,11 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
 
     {
       sym_link *type = operandType(op);
+#if 0
       if(IS_PTR_CONST(type))
+#else
+      if(IS_CODEPTR(type))
+#endif
        DEBUGpic16_emitcode(";","%d aop type is const pointer",__LINE__);
     }
 
@@ -821,16 +922,22 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
             return;
         }
 
+#if 1
        if (sym->accuse) {
            int i;
             aop = op->aop = sym->aop = newAsmop(AOP_ACC);
             aop->size = getSize(sym->type);
-            for ( i = 0 ; i < 2 ; i++ )
-                aop->aopu.aop_str[i] = accUse[i];
+            for ( i = 0 ; i < 1 ; i++ ) {
+               aop->aopu.aop_str[i] = accUse[i];
+//                aop->aopu.pcop = pic16_popRegFromString("WREG", aop->size, sym->usl.spillLoc->offset);
+           }
+           fprintf(stderr, "%s:%d allocating AOP_ACC for sym= %s\n", __FILE__, __LINE__, sym->name);
            DEBUGpic16_emitcode(";","%d size=%d",__LINE__,aop->size);
             return;  
        }
+#endif
 
+#if 1
         if (sym->ruonly ) {
          /*
          sym->aop = op->aop = aop = newAsmop(AOP_PCODE);
@@ -849,7 +956,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
          DEBUGpic16_emitcode(";","%d",__LINE__);
          return;
         }
-
+#endif
         /* else spill location  */
        if (sym->usl.spillLoc && getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
            /* force a new aop if sizes differ */
@@ -862,7 +969,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
 
        sym->aop = op->aop = aop = newAsmop(AOP_PCODE);
        //aop->aopu.pcop = pic16_popGetImmd(sym->usl.spillLoc->rname,0,sym->usl.spillLoc->offset);
-       aop->aopu.pcop = popRegFromString(sym->usl.spillLoc->rname, 
+       aop->aopu.pcop = pic16_popRegFromString(sym->usl.spillLoc->rname, 
                                          getSize(sym->type), 
                                          sym->usl.spillLoc->offset);
         aop->size = getSize(sym->type);
@@ -872,7 +979,11 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
 
     {
       sym_link *type = operandType(op);
+#if 0
       if(IS_PTR_CONST(type)) 
+#else
+      if(IS_CODEPTR(type)) 
+#endif
        DEBUGpic16_emitcode(";","%d aop type is const pointer",__LINE__);
     }
 
@@ -1105,7 +1216,9 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
        
     case AOP_ACC:
         DEBUGpic16_emitcode(";Warning -pic port ignoring get(AOP_ACC)","%d",__LINE__);
-       return "AOP_accumulator_bug";
+//        fprintf(stderr, "%s:%d Warning -pic port ignoring get(AOP_ACC)\n",__FILE__, __LINE__);
+//        assert( 0 );
+       return aop->aopu.aop_str[offset];       //->"AOP_accumulator_bug";
 
     case AOP_LIT:
        sprintf(s,"0x%02x", pic16aopLiteral (aop->aopu.aop_lit,offset));
@@ -1151,14 +1264,16 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
 /*-----------------------------------------------------------------*/
 pCodeOp *pic16_popGetTempReg(void)
 {
-
   pCodeOp *pcop;
 
-  pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP);
-  if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
-    PCOR(pcop)->r->wasUsed=1;
-    PCOR(pcop)->r->isFree=0;
-  }
+       pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP);
+       if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
+               PCOR(pcop)->r->wasUsed=1;
+               PCOR(pcop)->r->isFree=0;
+
+               /* push value on stack */
+               pic16_pushpCodeOp( pcop );
+       }
 
   return pcop;
 }
@@ -1168,10 +1283,11 @@ pCodeOp *pic16_popGetTempReg(void)
 /*-----------------------------------------------------------------*/
 void pic16_popReleaseTempReg(pCodeOp *pcop)
 {
-
-  if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r)
-    PCOR(pcop)->r->isFree = 1;
-
+       if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
+               PCOR(pcop)->r->isFree = 1;
+               
+               pic16_poppCodeOp( pcop );
+       }
 }
 /*-----------------------------------------------------------------*/
 /* pic16_popGetLabel - create a new pCodeOp of type PO_LABEL             */
@@ -1206,22 +1322,30 @@ pCodeOp *pic16_popCopyReg(pCodeOpReg *pc)
   pcor->rIdx = pc->rIdx;
   pcor->r->wasUsed=1;
 
-  //DEBUGpic16_emitcode ("; ***","%s  , copying %s, rIdx=%d",__FUNCTION__,pc->pcop.name,pc->rIdx);
+//  DEBUGpic16_emitcode ("; ***","%s  , copying %s, rIdx=%d",__FUNCTION__,pc->pcop.name,pc->rIdx);
 
   return PCOP(pcor);
 }
+
 /*-----------------------------------------------------------------*/
-/* pic16_popGet - asm operator to pcode operator conversion              */
+/* pic16_popGetLit - asm operator to pcode operator conversion     */
 /*-----------------------------------------------------------------*/
 pCodeOp *pic16_popGetLit(unsigned int lit)
 {
-
   return pic16_newpCodeOpLit(lit);
 }
 
+/*-----------------------------------------------------------------*/
+/* pic16_popGetLit2 - asm operator to pcode operator conversion    */
+/*-----------------------------------------------------------------*/
+pCodeOp *pic16_popGetLit2(unsigned int lit, pCodeOp *arg2)
+{
+  return pic16_newpCodeOpLit2(lit, arg2);
+}
+
 
 /*-----------------------------------------------------------------*/
-/* pic16_popGetImmd - asm operator to pcode immediate conversion         */
+/* pic16_popGetImmd - asm operator to pcode immediate conversion   */
 /*-----------------------------------------------------------------*/
 pCodeOp *pic16_popGetImmd(char *name, unsigned int offset, int index)
 {
@@ -1249,9 +1373,9 @@ pCodeOp *pic16_popGetWithString(char *str)
 }
 
 /*-----------------------------------------------------------------*/
-/* popRegFromString -                                              */
+/* pic16_popRegFromString -                                        */
 /*-----------------------------------------------------------------*/
-static pCodeOp *popRegFromString(char *str, int size, int offset)
+static pCodeOp *pic16_popRegFromString(char *str, int size, int offset)
 {
 
   pCodeOp *pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
@@ -1270,36 +1394,34 @@ static pCodeOp *popRegFromString(char *str, int size, int offset)
 
   PCOR(pcop)->r = pic16_dirregWithName(pcop->name);
   if(PCOR(pcop)->r == NULL) {
-    //fprintf(stderr,"%d - couldn't find %s in allocated registers, size =%d\n",__LINE__,aop->aopu.aop_dir,aop->size);
+//     fprintf(stderr, "%s:%d - couldn't find %s in allocated regsters, size= %d ofs= %d\n",
+//             __FUNCTION__, __LINE__, str, size, offset);
     PCOR(pcop)->r = pic16_allocRegByName (pcop->name,size);
 
        //fprintf(stderr, "allocating new register -> %s\n", str);
 
-    DEBUGpic16_emitcode(";","%d  %s   offset=%d - had to alloc by reg name",__LINE__,pcop->name,offset);
+//    DEBUGpic16_emitcode(";","%d  %s   size= %d offset=%d - had to alloc by reg name",__LINE__,pcop->name,size,offset);
   } else {
-    DEBUGpic16_emitcode(";","%d  %s   offset=%d",__LINE__,pcop->name,offset);
+//    DEBUGpic16_emitcode(";","%d  %s   size= %d offset=%d",__LINE__,pcop->name,size,offset);
   }
   PCOR(pcop)->instance = offset;
 
   return pcop;
 }
 
-static pCodeOp *popRegFromIdx(int rIdx)
+static pCodeOp *pic16_popRegFromIdx(int rIdx)
 {
   pCodeOp *pcop;
 
-  DEBUGpic16_emitcode ("; ***","%s,%d  , rIdx=0x%x",
-                      __FUNCTION__,__LINE__,rIdx);
+//     DEBUGpic16_emitcode ("; ***","%s,%d\trIdx=0x%x", __FUNCTION__,__LINE__,rIdx);
 
-  pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
-
-  PCOR(pcop)->rIdx = rIdx;
-  PCOR(pcop)->r = pic16_regWithIdx(rIdx);
-  PCOR(pcop)->r->isFree = 0;
-  PCOR(pcop)->r->wasUsed = 1;
-
-  pcop->type = PCOR(pcop)->r->pc_type;
+       pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
+       PCOR(pcop)->rIdx = rIdx;
+       PCOR(pcop)->r = pic16_regWithIdx(rIdx);
+       PCOR(pcop)->r->isFree = 0;
+       PCOR(pcop)->r->wasUsed = 1;
 
+       pcop->type = PCOR(pcop)->r->pc_type;
 
   return pcop;
 }
@@ -1314,6 +1436,10 @@ pCodeOp *pic16_popGet2(asmop *aop_src, asmop *aop_dst, int offset)
   pCodeOp *temp;
   
        pcop2 = (pCodeOpReg2 *)pic16_popGet(aop_src, offset);
+
+       /* comment the following check, so errors to throw up */
+//     if(!pcop2)return NULL;
+
        temp = pic16_popGet(aop_dst, offset);
        pcop2->pcop2 = temp;
        
@@ -1321,23 +1447,60 @@ pCodeOp *pic16_popGet2(asmop *aop_src, asmop *aop_dst, int offset)
 }
 
 
+
+/*--------------------------------------------------------------------------------.-*/
+/* pic16_popGet2p - a variant of pic16_popGet to handle two memory operand commands */
+/*                  VR 030601 , adapted by Hans Dorn                                */
+/*--------------------------------------------------------------------------------.-*/
+pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst)
+{
+  pCodeOpReg2 *pcop2;
+       pcop2 = (pCodeOpReg2 *)src;
+       pcop2->pcop2 = dst;
+       
+       return PCOP(pcop2);
+}
+
+
+
+/*---------------------------------------------------------------------------------*/
+/* pic16_popCombine2 - combine two pCodeOpReg variables into one for use with      */
+/*                     movff instruction                                           */
+/*---------------------------------------------------------------------------------*/
+pCodeOp *pic16_popCombine2(pCodeOpReg *src, pCodeOpReg *dst, int noalloc)
+{
+  pCodeOpReg2 *pcop2;
+
+       if(!noalloc) {
+               pcop2 = (pCodeOpReg2 *)pic16_popCopyReg(src);
+               pcop2->pcop2 = pic16_popCopyReg(dst);
+       } else {
+               /* the pCodeOp may be already allocated */
+               pcop2 = (pCodeOpReg2 *)(src);
+               pcop2->pcop2 = (pCodeOp *)(dst);
+       }
+
+  return PCOP(pcop2);
+}
+
+
 /*-----------------------------------------------------------------*/
 /* pic16_popGet - asm operator to pcode operator conversion              */
 /*-----------------------------------------------------------------*/
 pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
 {
   //char *s = buffer ;
-    //char *rs;
-
-    pCodeOp *pcop;
+  char *rs;
+  pCodeOp *pcop;
 
     //DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     /* offset is greater than
     size then zero */
 
-    if (offset > (aop->size - 1) &&
-        aop->type != AOP_LIT)
-      return NULL;  //zero;
+//    if (offset > (aop->size - 1) &&
+//        aop->type != AOP_LIT)
+//      return NULL;  //zero;
 
     /* depending on type */
     switch (aop->type) {
@@ -1346,50 +1509,54 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
     case AOP_R1:
     case AOP_DPTR:
     case AOP_DPTR2:
-    case AOP_ACC:
         DEBUGpic16_emitcode(";8051 legacy","%d type = %s",__LINE__,pic16_AopType(aop->type));
+        fprintf(stderr, ";8051 legacy %d type = %s\n",__LINE__,pic16_AopType(aop->type));
+       assert( 0 );
        return NULL;
-       
+
+
+
     case AOP_IMMD:
-      DEBUGpic16_emitcode(";","%d",__LINE__);
+      DEBUGpic16_emitcode(";","%d\tAOP_IMMD",__LINE__);
       return pic16_popGetImmd(aop->aopu.aop_immd,offset,0);
 
-    case AOP_DIR:
-      return popRegFromString(aop->aopu.aop_dir, aop->size, offset);
+    case AOP_ACC:
+      {
+       int rIdx = IDX_WREG;            //aop->aopu.aop_reg[offset]->rIdx;
 
-#if 0
+       fprintf(stderr, "%s:%d returning register AOP_ACC %s\n", __FILE__, __LINE__, aop->aopu.aop_str[offset]);
+
+       DEBUGpic16_emitcode(";","%d\tAOP_ACC", __LINE__);
+       
        pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
-       pcop->type = PO_DIR;
+       PCOR(pcop)->rIdx = rIdx;
+       PCOR(pcop)->r = pic16_typeRegWithIdx(rIdx, REG_SFR, 1); // pic16_regWithIdx(rIdx);
+       PCOR(pcop)->r->wasUsed=1;
+       PCOR(pcop)->r->isFree=0;
 
-       /*
-       if (offset)
-           sprintf(s,"(%s + %d)",
-                   aop->aopu.aop_dir,
-                   offset);
-       else
-           sprintf(s,"%s",aop->aopu.aop_dir);
-       pcop->name = Safe_calloc(1,strlen(s)+1);
-       strcpy(pcop->name,s);   
-       */
-       pcop->name = Safe_calloc(1,strlen(aop->aopu.aop_dir)+1);
-       strcpy(pcop->name,aop->aopu.aop_dir);   
-       PCOR(pcop)->r = pic16_dirregWithName(aop->aopu.aop_dir);
-       if(PCOR(pcop)->r == NULL) {
-         //fprintf(stderr,"%d - couldn't find %s in allocated registers, size =%d\n",__LINE__,aop->aopu.aop_dir,aop->size);
-         PCOR(pcop)->r = pic16_allocRegByName (aop->aopu.aop_dir,aop->size);
-         DEBUGpic16_emitcode(";","%d  %s   offset=%d - had to alloc by reg name",__LINE__,pcop->name,offset);
-       } else {
-         DEBUGpic16_emitcode(";","%d  %s   offset=%d",__LINE__,pcop->name,offset);
-       }
        PCOR(pcop)->instance = offset;
-
+       pcop->type = PCOR(pcop)->r->pc_type;
+//     rs = aop->aopu.aop_reg[offset]->name;
+//     DEBUGpic16_emitcode(";","%d register idx = %d name =%s",__LINE__,rIdx,rs);
        return pcop;
-#endif
+
+
+//     return pic16_popRegFromString(aop->aopu.aop_str[offset], aop->size, offset);
+//      return pic16_newpCodeOpRegFromStr(aop->aopu.aop_str[offset]);
+
+//     assert( 0 );
+      }
+       
+    case AOP_DIR:
+      DEBUGpic16_emitcode(";","%d\tAOP_DIR", __LINE__);
+      return pic16_popRegFromString(aop->aopu.aop_dir, aop->size, offset);
        
     case AOP_REG:
       {
        int rIdx = aop->aopu.aop_reg[offset]->rIdx;
 
+       DEBUGpic16_emitcode(";","%d\tAOP_REG", __LINE__);
+       
        pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
        PCOR(pcop)->rIdx = rIdx;
        PCOR(pcop)->r = pic16_regWithIdx(rIdx);
@@ -1398,12 +1565,14 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
 
        PCOR(pcop)->instance = offset;
        pcop->type = PCOR(pcop)->r->pc_type;
-       //rs = aop->aopu.aop_reg[offset]->name;
-       //DEBUGpic16_emitcode(";","%d regiser idx = %d name =%s",__LINE__,rIdx,rs);
+       rs = aop->aopu.aop_reg[offset]->name;
+       DEBUGpic16_emitcode(";","%d regiser idx = %d name =%s",__LINE__,rIdx,rs);
        return pcop;
       }
 
     case AOP_CRY:
+       DEBUGpic16_emitcode(";","%d\tAOP_CRY", __LINE__);
+
       pcop = pic16_newpCodeOpBit(aop->aopu.aop_dir,-1,1);
       PCOR(pcop)->r = pic16_dirregWithName(aop->aopu.aop_dir);
       //if(PCOR(pcop)->r == NULL)
@@ -1411,11 +1580,13 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
       return pcop;
        
     case AOP_LIT:
+       DEBUGpic16_emitcode(";","%d\tAOP_LIT", __LINE__);
       return pic16_newpCodeOpLit(pic16aopLiteral (aop->aopu.aop_lit,offset));
 
     case AOP_STR:
-      DEBUGpic16_emitcode(";","%d  %s",__LINE__,aop->aopu.aop_str[offset]);
+      DEBUGpic16_emitcode(";","%d AOP_STR %s",__LINE__,aop->aopu.aop_str[offset]);
       return pic16_newpCodeOpRegFromStr(aop->aopu.aop_str[offset]);
+
       /*
       pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
       PCOR(pcop)->r = pic16_allocRegByName(aop->aopu.aop_str[offset]);
@@ -1427,11 +1598,20 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
       */
 
     case AOP_PCODE:
-      DEBUGpic16_emitcode(";","pic16_popGet AOP_PCODE (%s) %d %s",pic16_pCodeOpType(aop->aopu.pcop),
+      DEBUGpic16_emitcode(";","pic16_popGet AOP_PCODE (%s) %d %s offset %d",pic16_pCodeOpType(aop->aopu.pcop),
                          __LINE__, 
-                         ((aop->aopu.pcop->name)? (aop->aopu.pcop->name) : "no name"));
+                         ((aop->aopu.pcop->name)? (aop->aopu.pcop->name) : "no name"), offset);
       pcop = pic16_pCodeOpCopy(aop->aopu.pcop);
+#if 1
+       switch( aop->aopu.pcop->type ) {
+               case PO_DIR: PCOR(pcop)->instance = offset; break;
+               case PO_IMMEDIATE: PCOI(pcop)->offset = offset; break;
+               default:
+                       assert( 0 );    /* should never reach here */;
+       }
+#else
       PCOI(pcop)->offset = offset;
+#endif
       return pcop;
     }
 
@@ -1689,6 +1869,60 @@ static void mov2w (asmop *aop, int offset)
 
 }
 
+
+/* push pcop into stack */
+void pic16_pushpCodeOp(pCodeOp *pcop)
+{
+       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg(&pic16_pc_postdec1)));
+}
+
+/* pop pcop from stack */
+void pic16_poppCodeOp(pCodeOp *pcop)
+{
+       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_preinc1), pcop));
+}
+
+
+/*-----------------------------------------------------------------*/
+/* pushw - pushes wreg to stack                                    */
+/*-----------------------------------------------------------------*/
+void pushw(void)
+{
+        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1));
+}
+
+                
+/*-----------------------------------------------------------------*/
+/* pushaop - pushes aop to stack                                   */
+/*-----------------------------------------------------------------*/
+void pushaop(asmop *aop, int offset)
+{
+        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+        pic16_emitpcode(POC_MOVFF, pic16_popCombine2(PCOR(pic16_popGet(aop, offset)), &pic16_pc_postdec1, 0));
+}
+
+/*-----------------------------------------------------------------*/
+/* popaop - pops aop from stack                                    */
+/*-----------------------------------------------------------------*/
+void popaop(asmop *aop, int offset)
+{
+       DEBUGpic16_emitcode("; ***", "%s  %d", __FUNCTION__, __LINE__);
+       pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_preinc1, PCOR(pic16_popGet(aop, offset)), 0));
+}
+
+void popaopidx(asmop *aop, int offset, int index)
+{
+  int ofs=1;
+
+        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
+       if(STACK_MODEL_LARGE)ofs++;
+
+       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(index + ofs));
+        pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_plusw2, PCOR(pic16_popGet(aop, offset)), 0));
+}
+
 /*-----------------------------------------------------------------*/
 /* reAdjustPreg - points a register back to where it should        */
 /*-----------------------------------------------------------------*/
@@ -1728,44 +1962,6 @@ static void reAdjustPreg (asmop *aop)
 
 }
 
-/*-----------------------------------------------------------------*/
-/* genNotFloat - generates not for float operations              */
-/*-----------------------------------------------------------------*/
-static void genNotFloat (operand *op, operand *res)
-{
-    int size, offset;
-    char *l;
-    symbol *tlbl ;
-
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* we will put 127 in the first byte of 
-    the result */
-    pic16_aopPut(AOP(res),"#127",0);
-    size = AOP_SIZE(op) - 1;
-    offset = 1;
-
-    l = pic16_aopGet(op->aop,offset++,FALSE,FALSE);
-    MOVA(l);    
-
-    while(size--) {
-        pic16_emitcode("orl","a,%s",
-                 pic16_aopGet(op->aop,
-                        offset++,FALSE,FALSE));
-    }
-    tlbl = newiTempLabel(NULL);
-
-    tlbl = newiTempLabel(NULL);
-    pic16_aopPut(res->aop,one,1);
-    pic16_emitcode("jz","%05d_DS_",(tlbl->key+100));
-    pic16_aopPut(res->aop,zero,1);
-    pic16_emitcode("","%05d_DS_:",(tlbl->key+100));
-
-    size = res->aop->size - 2;
-    offset = 2;    
-    /* put zeros in the rest */
-    while (size--) 
-        pic16_aopPut(res->aop,zero,offset++);
-}
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -1874,13 +2070,13 @@ void pic16_toBoolean(operand *oper)
 }
 
 
+#if !defined(GEN_Not)
 /*-----------------------------------------------------------------*/
 /* genNot - generate code for ! operation                          */
 /*-----------------------------------------------------------------*/
-static void genNot (iCode *ic)
+static void pic16_genNot (iCode *ic)
 {
   symbol *tlbl;
-  sym_link *optype = operandType(IC_LEFT(ic));
   int size;
 
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
@@ -1902,13 +2098,7 @@ static void genNot (iCode *ic)
     goto release;
   }
 
-  /* if type float then do float */
-  if (IS_FLOAT(optype)) {
-    genNotFloat(IC_LEFT(ic),IC_RESULT(ic));
-    goto release;
-  }
-
-  size = AOP_SIZE(IC_RESULT(ic));
+  size = AOP_SIZE(IC_LEFT(ic));
   if(size == 1) {
     pic16_emitpcode(POC_COMFW,pic16_popGet(AOP(IC_LEFT(ic)),0));
     pic16_emitpcode(POC_ANDLW,pic16_popGetLit(1));
@@ -1927,12 +2117,14 @@ static void genNot (iCode *ic)
   pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
   pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
 }
+#endif
 
 
+#if !defined(GEN_Cpl)
 /*-----------------------------------------------------------------*/
 /* genCpl - generate code for complement                           */
 /*-----------------------------------------------------------------*/
-static void genCpl (iCode *ic)
+static void pic16_genCpl (iCode *ic)
 {
     int offset = 0;
     int size ;
@@ -1978,6 +2170,7 @@ release:
     pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
     pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* genUminusFloat - unary minus for floating points                */
@@ -2189,49 +2382,131 @@ static void pushSide(operand * oper, int size)
 #endif
 }
 
-/*-----------------------------------------------------------------*/
-/* assignResultValue -                                            */
-/*-----------------------------------------------------------------*/
-static void assignResultValue(operand * oper)
+void pic16_loadFromReturn(operand *op, int offset, pCodeOp *src)
 {
-  int size = AOP_SIZE(oper);
+//             (AOP(left)->aopu.pcop->type == PO_DIR)?
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-
-  DEBUGpic16_pic16_AopType(__LINE__,oper,NULL,NULL);
-
-  if(!GpsuedoStkPtr) {
-    /* The last byte in the assignment is in W */
-    size--;
-    pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size));
-    GpsuedoStkPtr++;
-  }
-
-  while (size--) {
-    pic16_emitpcode(POC_MOVFW,popRegFromIdx(GpsuedoStkPtr-1 + pic16_Gstack_base_addr));
-    GpsuedoStkPtr++;
-    pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size));
-  }
+       if(AOP(op)->aopu.pcop->type == PO_IMMEDIATE) {
+               pic16_emitpcode(POC_MOVFW, src);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(op), offset));
+       } else {
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                       src, pic16_popGet(AOP(op), offset)));
+       }
 }
 
 
 /*-----------------------------------------------------------------*/
-/* genIpush - genrate code for pushing this gets a little complex  */
+/* assignResultValue - assign results to oper, rescall==1 is       */
+/*                     called from genCall() or genPCall()         */
 /*-----------------------------------------------------------------*/
-static void genIpush (iCode *ic)
+static void assignResultValue(operand * oper, int rescall)
 {
+  int size = AOP_SIZE(oper);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d - WARNING no code generated",__FUNCTION__,__LINE__);
-#if 0
-    int size, offset = 0 ;
-    char *l;
-
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_pic16_AopType(__LINE__,oper,NULL,NULL);
 
-    /* if this is not a parm push : ie. it is spill push 
-    and spill push is always done on the local stack */
-    if (!ic->parmPush) {
+       if(rescall) {
+               /* assign result from a call/pcall function() */
+               
+               /* function results are stored in a special order,
+                * see top of file with Function return policy, or manual */
 
-        /* and the item is spilt then do nothing */
+               if(size <= 4) {
+                       /* 8-bits, result in WREG */
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper), 0));
+                       
+                       if(size>1) {
+                               /* 16-bits, result in PRODL:WREG */
+                               pic16_loadFromReturn(oper, 1, pic16_popCopyReg(&pic16_pc_prodl));
+//                             pic16_emitpcode(POC_MOVFF,
+//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_prodl), pic16_popGet(AOP(oper), 1)));
+                       }
+                       
+                       if(size>2) {
+                               /* 24-bits, result in PRODH:PRODL:WREG */
+                               pic16_loadFromReturn(oper, 2, pic16_popCopyReg(&pic16_pc_prodl));
+
+//                             pic16_emitpcode(POC_MOVFF,
+//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_prodh), pic16_popGet(AOP(oper), 2)));
+                       }
+                       
+                       if(size>3) {
+                               /* 32-bits, result in FSR0L:PRODH:PRODL:WREG */
+                               pic16_loadFromReturn(oper, 3, pic16_popCopyReg(&pic16_pc_prodl));
+
+//                             pic16_emitpcode(POC_MOVFF,
+//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_fsr0l), pic16_popGet(AOP(oper), 3)));
+                       }
+               } else {
+                       /* >32-bits, result on stack, and FSR0 points to beginning.
+                        * Fix stack when done */
+                        
+                       while (size--) {
+//                             DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr);
+//                             DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2");
+               
+                               popaopidx(AOP(oper), size, GpsuedoStkPtr);
+                               GpsuedoStkPtr++;
+                       }
+                       
+                       /* fix stack */
+                       pic16_emitpcode(POC_MOVLW, pic16_popGetLit( AOP_SIZE(oper) ));
+                       pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
+                       if(STACK_MODEL_LARGE) {
+                               emitSKPNC;
+                               pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h ));
+                       }
+               }                       
+       } else {
+               if(!GpsuedoStkPtr) {
+//                     DEBUGpic16_emitcode("; ", "pop %d", GpsuedoStkPtr);
+                       /* The last byte in the assignment is in W */
+                       size--;
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size));
+                       GpsuedoStkPtr++;
+               }
+
+               while (size--) {
+//                     DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr);
+//                     DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2");
+               
+                       popaopidx(AOP(oper), size, GpsuedoStkPtr);
+                       GpsuedoStkPtr++;
+
+#if 0
+#if STACK_SUPPORT
+               if(!USE_STACK)
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size));
+#else
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size));
+#endif
+#endif
+
+               }
+       }
+               
+}
+
+
+/*-----------------------------------------------------------------*/
+/* genIpush - genrate code for pushing this gets a little complex  */
+/*-----------------------------------------------------------------*/
+static void genIpush (iCode *ic)
+{
+
+  DEBUGpic16_emitcode ("; ***","%s  %d - WARNING no code generated",__FUNCTION__,__LINE__);
+#if 0
+    int size, offset = 0 ;
+    char *l;
+
+
+    /* if this is not a parm push : ie. it is spill push 
+    and spill push is always done on the local stack */
+    if (!ic->parmPush) {
+
+        /* and the item is spilt then do nothing */
         if (OP_SYMBOL(IC_LEFT(ic))->isspilt)
             return ;
 
@@ -2397,136 +2672,142 @@ static void saverbank (int bank, iCode *ic, bool pushPsw)
 #endif
 }
 
+
+
 /*-----------------------------------------------------------------*/
 /* genCall - generates a call statement                            */
 /*-----------------------------------------------------------------*/
 static void genCall (iCode *ic)
 {
   sym_link *dtype;   
+  int stackParms=0;
+  
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* if caller saves & we have not saved then */
+       if (!ic->regsSaved)
+               saveRegisters(ic);
 
-  /* if caller saves & we have not saved then */
-  if (!ic->regsSaved)
-    saveRegisters(ic);
+       /* if we are calling a function that is not using
+        * the same register bank then we need to save the
+        * destination registers on the stack */
+       dtype = operandType(IC_LEFT(ic));
+       if (currFunc && dtype && 
+               (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)) &&
+               IFFUNC_ISISR(currFunc->type) &&
+               !ic->bankSaved) 
 
-  /* if we are calling a function that is not using
-     the same register bank then we need to save the
-     destination registers on the stack */
-  dtype = operandType(IC_LEFT(ic));
-  if (currFunc && dtype && 
-      (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)) &&
-      IFFUNC_ISISR(currFunc->type) &&
-      !ic->bankSaved) 
+                       saverbank(FUNC_REGBANK(dtype),ic,TRUE);
 
-    saverbank(FUNC_REGBANK(dtype),ic,TRUE);
+       /* if send set is not empty the assign */
+       if (_G.sendSet) {
+         iCode *sic;
 
-  /* if send set is not empty the assign */
-  if (_G.sendSet) {
-    iCode *sic;
-    /* For the Pic port, there is no data stack.
-     * So parameters passed to functions are stored
-     * in registers. (The pCode optimizer will get
-     * rid of most of these :).
-     */
-    int psuedoStkPtr=-1; 
-    int firstTimeThruLoop = 1;
+               /* For the Pic port, there is no data stack.
+                * So parameters passed to functions are stored
+                * in registers. (The pCode optimizer will get
+                * rid of most of these :). */
 
-    _G.sendSet = reverseSet(_G.sendSet);
+         int psuedoStkPtr=-1; 
+         int firstTimeThruLoop = 1;
 
-    /* First figure how many parameters are getting passed */
-    for (sic = setFirstItem(_G.sendSet) ; sic ; 
-        sic = setNextItem(_G.sendSet)) {
+               _G.sendSet = reverseSet(_G.sendSet);
 
-      pic16_aopOp(IC_LEFT(sic),sic,FALSE);
-      psuedoStkPtr += AOP_SIZE(IC_LEFT(sic));
-      pic16_freeAsmop (IC_LEFT(sic),NULL,sic,FALSE);
-    }
+               /* First figure how many parameters are getting passed */
+               for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) {
+                       pic16_aopOp(IC_LEFT(sic),sic,FALSE);
+                       psuedoStkPtr += AOP_SIZE(IC_LEFT(sic));
+                       pic16_freeAsmop (IC_LEFT(sic),NULL,sic,FALSE);
+               }
 
-    for (sic = setFirstItem(_G.sendSet) ; sic ; 
-        sic = setNextItem(_G.sendSet)) {
-      int size, offset = 0;
+               stackParms = psuedoStkPtr;
 
-      pic16_aopOp(IC_LEFT(sic),sic,FALSE);
-      size = AOP_SIZE(IC_LEFT(sic));
+               for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) {
+                 int size, offset = 0;
 
-      while (size--) {
-       DEBUGpic16_emitcode ("; ","%d left %s",__LINE__,
-                            pic16_AopType(AOP_TYPE(IC_LEFT(sic))));
+                       pic16_aopOp(IC_LEFT(sic),sic,FALSE);
+                       size = AOP_SIZE(IC_LEFT(sic));
 
-       if(!firstTimeThruLoop) {
-         /* If this is not the first time we've been through the loop
-          * then we need to save the parameter in a temporary
-          * register. The last byte of the last parameter is
-          * passed in W. */
-         pic16_emitpcode(POC_MOVWF,popRegFromIdx(--psuedoStkPtr + pic16_Gstack_base_addr));
+                       while (size--) {
+                               DEBUGpic16_emitcode ("; ","%d left %s",__LINE__,
+                                       pic16_AopType(AOP_TYPE(IC_LEFT(sic))));
+                               DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1);
 
+                               if(!firstTimeThruLoop) {
+                                       /* If this is not the first time we've been through the loop
+                                        * then we need to save the parameter in a temporary
+                                        * register. The last byte of the last parameter is
+                                        * passed in W. */
+
+                                       pushw();
+                                       --psuedoStkPtr;         // sanity check
+                               }
+                       
+                               firstTimeThruLoop=0;
+
+                               mov2w (AOP(IC_LEFT(sic)),  offset);
+                               offset++;
+                       }
+                       pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE);
+               }
+               _G.sendSet = NULL;
        }
-       firstTimeThruLoop=0;
 
-       //if (strcmp(l,fReturn[offset])) {
-       mov2w (AOP(IC_LEFT(sic)),  offset);
-/*
-       if ( ((AOP(IC_LEFT(sic))->type) == AOP_PCODE) ||
-            ((AOP(IC_LEFT(sic))->type) == AOP_LIT) )
-         pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(IC_LEFT(sic)),offset));
-       else
-         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(IC_LEFT(sic)),offset));
-*/
-       //}
-       offset++;
-      }
-      pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE);
-    }
-    _G.sendSet = NULL;
-  }
-  /* make the call */
-  pic16_emitpcode(POC_CALL,pic16_popGetWithString(OP_SYMBOL(IC_LEFT(ic))->rname[0] ?
-                                     OP_SYMBOL(IC_LEFT(ic))->rname :
-                                     OP_SYMBOL(IC_LEFT(ic))->name));
-
-  GpsuedoStkPtr=0;
-  /* if we need assign a result value */
-  if ((IS_ITEMP(IC_RESULT(ic)) && 
-       (OP_SYMBOL(IC_RESULT(ic))->nRegs ||
-       OP_SYMBOL(IC_RESULT(ic))->spildir )) ||
-      IS_TRUE_SYMOP(IC_RESULT(ic)) ) {
-
-    _G.accInUse++;
-    pic16_aopOp(IC_RESULT(ic),ic,FALSE);
-    _G.accInUse--;
+       /* make the call */
+       pic16_emitpcode(POC_CALL,pic16_popGetWithString(OP_SYMBOL(IC_LEFT(ic))->rname[0] ?
+                       OP_SYMBOL(IC_LEFT(ic))->rname :
+                       OP_SYMBOL(IC_LEFT(ic))->name));
+
+       GpsuedoStkPtr=0;
+       /* if we need assign a result value */
+       if ((IS_ITEMP(IC_RESULT(ic)) && 
+               (OP_SYMBOL(IC_RESULT(ic))->nRegs ||
+               OP_SYMBOL(IC_RESULT(ic))->spildir )) ||
+               IS_TRUE_SYMOP(IC_RESULT(ic)) ) {
 
-    assignResultValue(IC_RESULT(ic));
+               _G.accInUse++;
+               pic16_aopOp(IC_RESULT(ic),ic,FALSE);
+               _G.accInUse--;
 
-    DEBUGpic16_emitcode ("; ","%d left %s",__LINE__,
+               assignResultValue(IC_RESULT(ic), 1);
+
+               DEBUGpic16_emitcode ("; ","%d left %s",__LINE__,
                         pic16_AopType(AOP_TYPE(IC_RESULT(ic))));
                
-    pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE);
-  }
+               pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE);
+       }
 
-  /* adjust the stack for parameters if 
-     required */
-  if (ic->parmBytes) {
-    int i;
-    if (ic->parmBytes > 3) {
-      pic16_emitcode("mov","a,%s",spname);
-      pic16_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff);
-      pic16_emitcode("mov","%s,a",spname);
-    } else 
-      for ( i = 0 ; i <  ic->parmBytes ;i++)
-       pic16_emitcode("dec","%s",spname);
+       if(stackParms>0) {
+               pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms));
+               pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
+               if(STACK_MODEL_LARGE) {
+                       emitSKPNC;
+                       pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h ));
+               }
+       }
 
-  }
+       /* adjust the stack for parameters if required */
+//     fprintf(stderr, "%s:%d: %s ic->parmBytes= %d\n", __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, ic->parmBytes);
 
-  /* if register bank was saved then pop them */
-  if (ic->bankSaved)
-    unsaverbank(FUNC_REGBANK(dtype),ic,TRUE);
+       if (ic->parmBytes) {
+         int i;
 
-  /* if we hade saved some registers then unsave them */
-  if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
-    unsaveRegisters (ic);
+               if (ic->parmBytes > 3) {
+                       pic16_emitcode("mov","a,%s",spname);
+                       pic16_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff);
+                       pic16_emitcode("mov","%s,a",spname);
+               } else 
+                       for ( i = 0 ; i <  ic->parmBytes ;i++)
+                               pic16_emitcode("dec","%s",spname);
+       }
 
+       /* if register bank was saved then pop them */
+       if (ic->bankSaved)
+               unsaverbank(FUNC_REGBANK(dtype),ic,TRUE);
 
+       /* if we hade saved some registers then unsave them */
+       if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
+               unsaveRegisters (ic);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2609,7 +2890,7 @@ static void genPcall (iCode *ic)
         pic16_aopOp(IC_RESULT(ic),ic,FALSE);
         _G.accInUse--;
        
-       assignResultValue(IC_RESULT(ic));
+       assignResultValue(IC_RESULT(ic), 1);
 
         pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
     }
@@ -2692,189 +2973,217 @@ static bool inExcludeList(char *s)
 /*-----------------------------------------------------------------*/
 static void genFunction (iCode *ic)
 {
-    symbol *sym;
-    sym_link *ftype;
+  symbol *sym;
+  sym_link *ftype;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,labelOffset,max_key);
+       DEBUGpic16_emitcode ("; ***","%s  %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,labelOffset,max_key);
 
-    labelOffset += (max_key+4);
-    max_key=0;
-    GpsuedoStkPtr=0;
-    _G.nRegsSaved = 0;
-    /* create the function header */
-    pic16_emitcode(";","-----------------------------------------");
-    pic16_emitcode(";"," function %s",(sym = OP_SYMBOL(IC_LEFT(ic)))->name);
-    pic16_emitcode(";","-----------------------------------------");
+       labelOffset += (max_key+4);
+       max_key=0;
+       GpsuedoStkPtr=0;
+       _G.nRegsSaved = 0;
 
-    pic16_emitcode("","%s:",sym->rname);
-    pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,sym->rname));
+       ftype = operandType(IC_LEFT(ic));
 
-    ftype = operandType(IC_LEFT(ic));
+       if(/*!IFFUNC_ISNAKED(ftype) &&*/ IFFUNC_ISISR(ftype)) {
+               /* create an absolute section at the interrupt vector:
+                * that is 0x0008 for interrupt 1, 0x0018 interrupt 2 */
+         int ivec;
+         symbol *asym;
+         char asymname[128];
+         
+               {
+                 int i;
 
-    /* if critical function then turn interrupts off */
-    if (IFFUNC_ISCRITICAL(ftype))
-        pic16_emitcode("clr","ea");
+                       sym = OP_SYMBOL( IC_LEFT(ic));
+                       for(i=1;i<=2;i++)
+                               if(STRCASECMP(interrupts[i]->name, sym->name))break;
+                       
+                       if(i>2) {
+                               fprintf(stderr, "PIC16 port: %s:%d: interrupt function but cannot locate symbol (%s)\n",
+                                       __FILE__, __LINE__, sym->name);
+                               exit(-1);
+                       }
+                       ivec = i;
+               }
+               sprintf(asymname, "ivec_%s", sym->name);
+               asym = newSymbol(asymname, 0);
+               pic16_emitcode(";","-----------------------------------------");
+               pic16_emitcode(";"," interrupt vector %d for function %s", ivec, sym->name);
+               pic16_emitcode(";","-----------------------------------------");
+               pic16_addpCode2pBlock(pb, pic16_newpCodeFunction(moduleName, asym->name));
+               pic16_emitpcode(POC_GOTO, pic16_popGetWithString( sym->rname ));
+               
+               pic16_pBlockConvert2Absolute(pb);
+       }
 
-    /* here we need to generate the equates for the
-       register bank if required */
-#if 0
-    if (FUNC_REGBANK(ftype) != rbank) {
-        int i ;
-
-        rbank = FUNC_REGBANK(ftype);
-        for ( i = 0 ; i < pic16_nRegs ; i++ ) {
-            if (strcmp(regspic16[i].base,"0") == 0)
-                pic16_emitcode("","%s = 0x%02x",
-                         regspic16[i].dname,
-                         8*rbank+regspic16[i].offset);
-            else
-                pic16_emitcode ("","%s = %s + 0x%02x",
-                          regspic16[i].dname,
-                          regspic16[i].base,
-                          8*rbank+regspic16[i].offset);
-        }
-    }
-#endif
 
-    /* if this is an interrupt service routine then
-    save acc, b, dpl, dph  */
-    if (IFFUNC_ISISR(sym->type)) {
+       /* create the function header */
+       pic16_emitcode(";","-----------------------------------------");
+       pic16_emitcode(";"," function %s",(sym = OP_SYMBOL(IC_LEFT(ic)))->name);
+       pic16_emitcode(";","-----------------------------------------");
 
-#if 0
-       pic16_addpCode2pBlock(pb,pic16_newpCode(POC_BRA,pic16_newpCodeOp("END_OF_INTERRUPT+2",PO_STR)));
-       
-       /* what is the reason of having these 3 NOPS? VR - 030701 */
-       pic16_emitpcodeNULLop(POC_NOP);
-       pic16_emitpcodeNULLop(POC_NOP);
-       pic16_emitpcodeNULLop(POC_NOP);
-#endif
+       pic16_emitcode("","%s:",sym->rname);
+       pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(moduleName,sym->rname));
 
-      pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_wsave));
-      pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_status));
-      pic16_emitpcode(POC_CLRF,   pic16_popCopyReg(&pic16_pc_status));
-      pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_ssave));
-
-      pic16_pBlockConvert2ISR(pb);
-#if 0  
-       if (!inExcludeList("acc"))          
-           pic16_emitcode ("push","acc");      
-       if (!inExcludeList("b"))
-           pic16_emitcode ("push","b");
-       if (!inExcludeList("dpl"))
-           pic16_emitcode ("push","dpl");
-       if (!inExcludeList("dph"))
-           pic16_emitcode ("push","dph");
-       if (options.model == MODEL_FLAT24 && !inExcludeList("dpx"))
        {
-           pic16_emitcode ("push", "dpx");
-           /* Make sure we're using standard DPTR */
-           pic16_emitcode ("push", "dps");
-           pic16_emitcode ("mov", "dps, #0x00");
-           if (options.stack10bit)
-           {   
-               /* This ISR could conceivably use DPTR2. Better save it. */
-               pic16_emitcode ("push", "dpl1");
-               pic16_emitcode ("push", "dph1");
-               pic16_emitcode ("push", "dpx1");
-           }
+         absSym *ab;
+
+               for(ab = setFirstItem(absSymSet); ab; ab = setNextItem(absSymSet))
+                       if(strcmp(ab->name, sym->name)) {
+                               pic16_pBlockConvert2Absolute(pb);
+                               break;
+                       }
+               
+       }
+
+
+       if(IFFUNC_ISNAKED(ftype)) {
+               DEBUGpic16_emitcode("; ***", "_naked function, no prologue");
+               return;
        }
-       /* if this isr has no bank i.e. is going to
-          run with bank 0 , then we need to save more
-          registers :-) */
-       if (!FUNC_REGBANK(sym->type)) {
+       
+       /* if critical function then turn interrupts off */
+       if (IFFUNC_ISCRITICAL(ftype))
+               pic16_emitcode("clr","ea");
+
+       /* if this is an interrupt service routine then
+        * save acc, b, dpl, dph  */
+       if (IFFUNC_ISISR(sym->type)) {
+         int i;
+               /* an ISR should save: WREG, STATUS, BSR, PRODL, PRODH */
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_status ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodl ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodh ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l ));
+               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h ));
+
+
+#if 0
+               /* VR -- this is the old code which clears STATUS register for
+                * the ISR routine. Why? */
+               pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_wsave));
+               pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_status));
+               pic16_emitpcode(POC_CLRF,   pic16_popCopyReg(&pic16_pc_status));
+               pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_ssave));
+#endif
 
-           /* if this function does not call any other
-              function then we can be economical and
-              save only those registers that are used */
-           if (! IFFUNC_HASFCALL(sym->type)) {
-               int i;
+               pic16_pBlockConvert2ISR(pb);
+
+#if 0
+               if (!inExcludeList("acc"))          
+                       pic16_emitcode ("push","acc");  
+               if (!inExcludeList("b"))
+                       pic16_emitcode ("push","b");
+               if (!inExcludeList("dpl"))
+                       pic16_emitcode ("push","dpl");
+               if (!inExcludeList("dph"))
+                       pic16_emitcode ("push","dph");
+#endif
 
                /* if any registers used */
                if (sym->regsUsed) {
-                   /* save the registers used */
-                   for ( i = 0 ; i < sym->regsUsed->size ; i++) {
-                       if (bitVectBitValue(sym->regsUsed,i) ||
-                          (pic16_ptrRegReq && (i == R0_IDX || i == R1_IDX)) )
-                         pic16_emitcode("push","junk");//"%s",pic16_regWithIdx(i)->dname);                         
-                   }
+                       /* save the registers used */
+                       DEBUGpic16_emitcode("; **", "Saving used registers in stack");
+                       for ( i = 0 ; i < sym->regsUsed->size ; i++) {
+                               if (bitVectBitValue(sym->regsUsed,i)) {
+//                                     fprintf(stderr, "%s:%d function %s uses register %s\n",
+//                                                     __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+//                                                     pic16_regWithIdx(i)->name);
+
+                                       pic16_pushpCodeOp( pic16_popRegFromIdx(i) );
+                                       _G.nRegsSaved++;
+                               }
+                       }
                }
-               
-           } else {
-               /* this function has  a function call cannot
-                  determines register usage so we will have the
-                  entire bank */
-               saverbank(0,ic,FALSE);
-           }       
-       }
-#endif
-    } else {
-       /* if callee-save to be used for this function
-          then save the registers being used in this function */
-       if (IFFUNC_CALLEESAVES(sym->type)) {
-           int i;
+       } else {
+               /* emit code to setup stack frame if user enabled,
+                * and function is not main() */
+        
+//             fprintf(stderr, "function name: %s\n", sym->name);
+               if(strcmp(sym->name, "main")) {
+                       if(!options.ommitFramePtr || sym->regsUsed) {
+                       /* setup the stack frame */
+                               pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr2l, &pic16_pc_postdec1, 0));
+                               pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1l, &pic16_pc_fsr2l, 0));
+                               if(STACK_MODEL_LARGE)
+                                       pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1h, &pic16_pc_fsr2h, 0));
+                       }
+               }
+
+               /* if callee-save to be used for this function
+               * then save the registers being used in this function */
+               if (IFFUNC_CALLEESAVES(sym->type)) {
+                 int i;
            
-           /* if any registers used */
-           if (sym->regsUsed) {
-               /* save the registers used */
-               for ( i = 0 ; i < sym->regsUsed->size ; i++) {
-                   if (bitVectBitValue(sym->regsUsed,i) ||
-                      (pic16_ptrRegReq && (i == R0_IDX || i == R1_IDX)) ) {
-                     //pic16_emitcode("push","%s",pic16_regWithIdx(i)->dname);
-                       _G.nRegsSaved++;
-                   }
+//                     fprintf(stderr, "%s:%d function sym->regsUsed= %p\n", __FILE__, __LINE__, sym->regsUsed);
+                       
+                       /* if any registers used */
+                       if (sym->regsUsed) {
+                               /* save the registers used */
+                               DEBUGpic16_emitcode("; **", "Saving used registers in stack");
+                               for ( i = 0 ; i < sym->regsUsed->size ; i++) {
+                                       if (bitVectBitValue(sym->regsUsed,i)) {
+//                                             fprintf(stderr, "%s:%d function %s uses register %s\n",
+//                                                             __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+//                                                             pic16_regWithIdx(i)->name);
+
+                                               pic16_pushpCodeOp( pic16_popRegFromIdx(i) );
+//                                             pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
+//                                                     PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))),
+//                                                     &pic16_pc_postdec1, 0));
+                                               _G.nRegsSaved++;
+                                       }
+                               }
+                       }
                }
-           }
-       }
-    }
+       }
 
-    /* set the register bank to the desired value */
-    if (FUNC_REGBANK(sym->type) || FUNC_ISISR(sym->type)) {
-        pic16_emitcode("push","psw");
-        pic16_emitcode("mov","psw,#0x%02x",(FUNC_REGBANK(sym->type) << 3)&0x00ff);   
-    }
 
-    if (IFFUNC_ISREENT(sym->type) || options.stackAuto) {
+       
+#if 0
+       if (IFFUNC_ISREENT(sym->type) || options.stackAuto) {
 
-       if (options.useXstack) {
-           pic16_emitcode("mov","r0,%s",spname);
-           pic16_emitcode("mov","a,_bp");
-           pic16_emitcode("movx","@r0,a");
-           pic16_emitcode("inc","%s",spname);
-       }
-       else
-       {
-           /* set up the stack */
-           pic16_emitcode ("push","_bp");     /* save the callers stack  */
+               if (options.useXstack) {
+                       pic16_emitcode("mov","r0,%s",spname);
+                       pic16_emitcode("mov","a,_bp");
+                       pic16_emitcode("movx","@r0,a");
+                       pic16_emitcode("inc","%s",spname);
+               } else {
+                       /* set up the stack */
+                       pic16_emitcode ("push","_bp");     /* save the callers stack  */
+               }
+               pic16_emitcode ("mov","_bp,%s",spname);
        }
-       pic16_emitcode ("mov","_bp,%s",spname);
-    }
-
-    /* adjust the stack for the function */
-    if (sym->stack) {
+#endif
+       DEBUGpic16_emitcode("; ", "need to adjust stack = %d", sym->stack);
 
-       int i = sym->stack;
-       if (i > 256 ) 
-           werror(W_STACK_OVERFLOW,sym->name);
+       /* adjust the stack for the function */
+       if (sym->stack) {
+         int i = sym->stack;
 
-       if (i > 3 && sym->recvSize < 4) {              
+               if (i > 127 ) 
+                       werror(W_STACK_OVERFLOW,sym->name);
 
-           pic16_emitcode ("mov","a,sp");
-           pic16_emitcode ("add","a,#0x%02x",((char)sym->stack & 0xff));
-           pic16_emitcode ("mov","sp,a");
-          
+               if (i > 3 && sym->recvSize < 4) {              
+                       pic16_emitcode ("mov","a,sp");
+                       pic16_emitcode ("add","a,#0x%02x",((char)sym->stack & 0xff));
+                       pic16_emitcode ("mov","sp,a");
+               } else
+                       while(i--)
+                               pic16_emitcode("inc","sp");
        }
-       else
-           while(i--)
-               pic16_emitcode("inc","sp");
-    }
-
-     if (sym->xstack) {
 
-       pic16_emitcode ("mov","a,_spx");
-       pic16_emitcode ("add","a,#0x%02x",((char)sym->xstack & 0xff));
-       pic16_emitcode ("mov","_spx,a");
-    }    
+       if (sym->xstack) {
+               DEBUGpic16_emitcode("; ", "%s", __FUNCTION__);
 
+               pic16_emitcode ("mov","a,_spx");
+               pic16_emitcode ("add","a,#0x%02x",((char)sym->xstack & 0xff));
+               pic16_emitcode ("mov","_spx,a");
+       }
+    
 }
 
 /*-----------------------------------------------------------------*/
@@ -2886,10 +3195,17 @@ static void genEndFunction (iCode *ic)
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
+    if(IFFUNC_ISNAKED(sym->type)) {
+       DEBUGpic16_emitcode("; ***", "_naked function, no epilogue");
+       return;
+    }
+
+#if 0
     if (IFFUNC_ISREENT(sym->type) || options.stackAuto)
     {
         pic16_emitcode ("mov","%s,_bp",spname);
     }
+#endif
 
     /* if use external stack but some variables were
     added to the local stack then decrement the
@@ -2901,6 +3217,7 @@ static void genEndFunction (iCode *ic)
     }
 
 
+#if 0
     if ((IFFUNC_ISREENT(sym->type) || options.stackAuto)) {
        if (options.useXstack) {
            pic16_emitcode("mov","r0,%s",spname);
@@ -2913,114 +3230,90 @@ static void genEndFunction (iCode *ic)
            pic16_emitcode ("pop","_bp");
        }
     }
+#endif
 
     /* restore the register bank  */    
     if (FUNC_REGBANK(sym->type) || FUNC_ISISR(sym->type))
         pic16_emitcode ("pop","psw");
 
-    if (IFFUNC_ISISR(sym->type)) {
+       if (IFFUNC_ISISR(sym->type)) {
 
-       /* now we need to restore the registers */
-       /* if this isr has no bank i.e. is going to
-          run with bank 0 , then we need to save more
-          registers :-) */
-       if (!FUNC_REGBANK(sym->type)) {
-           
-           /* if this function does not call any other
-              function then we can be economical and
-              save only those registers that are used */
-           if (! IFFUNC_HASFCALL(sym->type)) {
-               int i;
+               /* now we need to restore the registers */
                
                /* if any registers used */
                if (sym->regsUsed) {
-                   /* save the registers used */
-                   for ( i = sym->regsUsed->size ; i >= 0 ; i--) {
-                       if (bitVectBitValue(sym->regsUsed,i) ||
-                          (pic16_ptrRegReq && (i == R0_IDX || i == R1_IDX)) )
-                         pic16_emitcode("pop","junk");//"%s",pic16_regWithIdx(i)->dname);
-                   }
+                 int i;
+
+                       /* restore registers used */
+                       DEBUGpic16_emitcode("; **", "Restoring used registers from stack");
+                       for ( i = sym->regsUsed->size; i >= 0; i--) {
+                               if (bitVectBitValue(sym->regsUsed,i)) {
+//                                     fprintf(stderr, "%s:%d function %s uses register %s\n",
+//                                                     __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+//                                                     pic16_regWithIdx(i)->name);
+        
+                                       pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
+                                                       &pic16_pc_preinc1,
+                                                       PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+                               }
+                       }
                }
-               
-           } else {
-               /* this function has  a function call cannot
-                  determines register usage so we will have the
-                  entire bank */
-               unsaverbank(0,ic,FALSE);
-           }       
-       }
-#if 0
-       if (options.model == MODEL_FLAT24 && !inExcludeList("dpx"))
-       {
-           if (options.stack10bit)
-           {
-               pic16_emitcode ("pop", "dpx1");
-               pic16_emitcode ("pop", "dph1");
-               pic16_emitcode ("pop", "dpl1");
-           }   
-           pic16_emitcode ("pop", "dps");
-           pic16_emitcode ("pop", "dpx");
-       }
-       if (!inExcludeList("dph"))
-           pic16_emitcode ("pop","dph");
-       if (!inExcludeList("dpl"))
-           pic16_emitcode ("pop","dpl");
-       if (!inExcludeList("b"))
-           pic16_emitcode ("pop","b");
-       if (!inExcludeList("acc"))
-           pic16_emitcode ("pop","acc");
-
-        if (IFFUNC_ISCRITICAL(sym->type))
-            pic16_emitcode("setb","ea");
-#endif
-
-       /* if debug then send end of function */
-/*     if (options.debug && currFunc) { */
-       if (currFunc) {
-           _G.debugLine = 1;
-           pic16_emitcode(";","C$%s$%d$%d$%d ==.",
-                    FileBaseName(ic->filename),currFunc->lastLine,
-                    ic->level,ic->block); 
-           if (IS_STATIC(currFunc->etype))         
-               pic16_emitcode(";","XF%s$%s$0$0 ==.",moduleName,currFunc->name); 
-           else
-               pic16_emitcode(";","XG$%s$0$0 ==.",currFunc->name);
-           _G.debugLine = 0;
-       }
        
-//     pic16_emitcode ("reti","");
-
-       pic16_emitpcode(POC_CLRF,   pic16_popCopyReg(&pic16_pc_status));
-       pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_ssave));
-       pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_status));
-       pic16_emitpcode(POC_SWAPF,  pic16_popCopyReg(&pic16_pc_wsave));
-       pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_wsave));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h ));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodh ));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodl ));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_status ));
+               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
+       
 
+               /* if debug then send end of function */
+/*     if (options.debug && currFunc)  */
+               if (currFunc) {
+                       _G.debugLine = 1;
+                       pic16_emitcode(";","C$%s$%d$%d$%d ==.",
+                                       FileBaseName(ic->filename),currFunc->lastLine,
+                                       ic->level,ic->block); 
+                       if (IS_STATIC(currFunc->etype))     
+                               pic16_emitcode(";","XF%s$%s$0$0 ==.",moduleName,currFunc->name); 
+                       else
+                               pic16_emitcode(";","XG$%s$0$0 ==.",currFunc->name);
+                       _G.debugLine = 0;
+               }
+       
 #if 0
-       pic16_addpCode2pBlock(pb,pic16_newpCodeLabel("END_OF_INTERRUPT",-1));
+               pic16_emitpcode(POC_CLRF,   pic16_popCopyReg(&pic16_pc_status));
+               pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_ssave));
+               pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_status));
+               pic16_emitpcode(POC_SWAPF,  pic16_popCopyReg(&pic16_pc_wsave));
+               pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_wsave));
 #endif
 
-       pic16_emitpcodeNULLop(POC_RETFIE);
-
-    }
-    else {
-        if (IFFUNC_ISCRITICAL(sym->type))
-            pic16_emitcode("setb","ea");
+               pic16_emitpcodeNULLop(POC_RETFIE);
+       }
+       else {
+               if (IFFUNC_ISCRITICAL(sym->type))
+                       pic16_emitcode("setb","ea");
        
-       if (IFFUNC_CALLEESAVES(sym->type)) {
-           int i;
-           
-           /* if any registers used */
-           if (sym->regsUsed) {
+       /* if any registers used */
+       if (sym->regsUsed) {
+         int i;
                /* save the registers used */
-               for ( i = sym->regsUsed->size ; i >= 0 ; i--) {
-                   if (bitVectBitValue(sym->regsUsed,i) ||
-                      (pic16_ptrRegReq && (i == R0_IDX || i == R1_IDX)) )
-                     pic16_emitcode("pop","junk");//"%s",pic16_regWithIdx(i)->dname);
+               DEBUGpic16_emitcode("; **", "Restoring used registers from stack");
+               for ( i = sym->regsUsed->size; i >= 0; i--) {
+                       if (bitVectBitValue(sym->regsUsed,i)) {
+//                             fprintf(stderr, "%s:%d function %s uses register %s\n",
+//                                             __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+//                                             pic16_regWithIdx(i)->name);
+        
+                               pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
+                                       &pic16_pc_preinc1,
+                                       PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+                       }
                }
-           }
-           
        }
+       
 
        /* if debug then send end of function */
        if (currFunc) {
@@ -3035,6 +3328,20 @@ static void genEndFunction (iCode *ic)
            _G.debugLine = 0;
        }
 
+       /* insert code to restore stack frame, if user enabled it
+        * and function is not main() */
+        
+       if(strcmp(sym->name, "main")) {
+               if(!options.ommitFramePtr || sym->regsUsed) {
+                       /* restore stack frame */
+                       if(STACK_MODEL_LARGE)
+                               pic16_emitpcode(POC_MOVFF,
+                                       pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0));
+                       pic16_emitpcode(POC_MOVFF,
+                                       pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0));
+               }
+       }
+
         pic16_emitcode ("return","");
        pic16_emitpcodeNULLop(POC_RETURN);
 
@@ -3044,71 +3351,141 @@ static void genEndFunction (iCode *ic)
 
 }
 
+
+void pic16_storeForReturn(operand *op, int offset, pCodeOp *dest)
+{
+//             (AOP(left)->aopu.pcop->type == PO_DIR)?
+
+       if(AOP(op)->aopu.pcop->type == PO_IMMEDIATE) {
+               pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset));
+               pic16_emitpcode(POC_MOVWF, dest);
+       } else {
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                       pic16_popGet(AOP(op), offset), dest));
+       }
+}
+
 /*-----------------------------------------------------------------*/
 /* genRet - generate code for return statement                     */
 /*-----------------------------------------------------------------*/
 static void genRet (iCode *ic)
 {
-  int size,offset = 0 , pushed = 0;
-    
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  /* if we have no return value then
-     just generate the "ret" */
-  if (!IC_LEFT(ic)) 
-    goto jumpret;       
-    
-  /* we have something to return then
-     move the return value into place */
-  pic16_aopOp(IC_LEFT(ic),ic,FALSE);
-  size = AOP_SIZE(IC_LEFT(ic));
+  int size;
+  operand *left;
+
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* if we have no return value then
+        * just generate the "ret" */
+       
+       if (!IC_LEFT(ic)) 
+               goto jumpret;       
     
-  while (size--) {
-    char *l ;
-    if (AOP_TYPE(IC_LEFT(ic)) == AOP_DPTR) {
-      /* #NOCHANGE */
-      l = pic16_aopGet(AOP(IC_LEFT(ic)),offset++,
-                FALSE,TRUE);
-      pic16_emitcode("push","%s",l);
-      pushed++;
-    } else {
-      l = pic16_aopGet(AOP(IC_LEFT(ic)),offset,
-                FALSE,FALSE);
-      if (strcmp(fReturn[offset],l)) {
-       if( ( (AOP(IC_LEFT(ic))->type) == AOP_IMMD) ||
-           ((AOP(IC_LEFT(ic))->type) == AOP_LIT) ) {
-         pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
-       }else {
-         pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
+       /* we have something to return then
+        * move the return value into place */
+       pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE);
+       size = AOP_SIZE(IC_LEFT(ic));
+
+       if(size <= 4) {
+               if(size>3) {
+                       pic16_storeForReturn(IC_LEFT(ic), 3, pic16_popCopyReg(&pic16_pc_fsr0l));
+//                     pic16_emitpcode(POC_MOVFF,
+//                             pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)), 3), pic16_popCopyReg(&pic16_pc_fsr0l)));
+               }
+               if(size>2) {
+                       pic16_storeForReturn(IC_LEFT(ic), 2, pic16_popCopyReg(&pic16_pc_prodh));
+//                     pic16_emitpcode(POC_MOVFF,
+//                             pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)), 2), pic16_popCopyReg(&pic16_pc_prodh)));
+               }
+               if(size>1) {
+                       pic16_storeForReturn(IC_LEFT(ic), 1, pic16_popCopyReg(&pic16_pc_prodl));
+//                     pic16_emitpcode(POC_MOVFF,
+//                             pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)), 1), pic16_popCopyReg(&pic16_pc_prodl)));
+               }
+
+               pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)), 0));
+
+//             pic16_storeForReturn(IC_LEFT(ic), 0, pic16_popCopyReg(&pic16_pc_wreg));
+//             pic16_emitpcode(POC_MOVFF,
+//                     pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)), 0), pic16_popCopyReg(&pic16_pc_wreg)));
+
+       } else {
+               /* >32-bits, setup stack and FSR0 */
+               while (size--) {
+//                     DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr);
+//                     DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2");
+
+                       pic16_pushpCodeOp( pic16_popGet( AOP( IC_LEFT(ic) ), size) );
+
+//                     popaopidx(AOP(oper), size, GpseudoStkPtr);
+                       GpsuedoStkPtr++;
+               }
+                       
+               /* setup FSR0 */
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                       pic16_popCopyReg(&pic16_pc_fsr1l), pic16_popCopyReg(&pic16_pc_fsr0l)));
+
+               if(STACK_MODEL_LARGE) {
+                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                               pic16_popCopyReg(&pic16_pc_fsr1h), pic16_popCopyReg(&pic16_pc_fsr0h)));
+               } else {
+                       pic16_emitpcode(POC_CLRF, pic16_popCopyReg( &pic16_pc_fsr1h ) );
+               }
        }
-       if(size) {
-         pic16_emitpcode(POC_MOVWF,popRegFromIdx(offset + pic16_Gstack_base_addr));
+                               
+#if 0
+       /* old code, left here for reference -- VR */    
+       while (size--) {
+         char *l ;
+
+               if (AOP_TYPE(IC_LEFT(ic)) == AOP_DPTR) {
+                       /* #NOCHANGE */
+                       l = pic16_aopGet(AOP(IC_LEFT(ic)),offset++, FALSE,TRUE);
+                       pic16_emitpcomment("push %s",l);
+                       pushed++;
+               } else {
+                       DEBUGpic16_emitcode(";", "%d", __LINE__);
+                       l = pic16_aopGet(AOP(IC_LEFT(ic)),offset, FALSE,FALSE);
+                       DEBUGpic16_emitcode(";", "%d l= %s", __LINE__, l);       
+                       
+                       if (strcmp(fReturn[offset],l)) {
+                               if( ( (AOP(IC_LEFT(ic))->type) == AOP_IMMD)
+                                       || ((AOP(IC_LEFT(ic))->type) == AOP_LIT) ) {
+                                       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
+                               } else {
+                                       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
+                               }
+                               
+                               if(size) {
+                                       pic16_emitpcode(POC_MOVWF, pic16_popRegFromIdx(offset + pic16_Gstack_base_addr));
+                               }
+                               offset++;
+                       }
+               }
+       }    
+
+       if (pushed) {
+               while(pushed) {
+                       pushed--;
+                       if (strcmp(fReturn[pushed],"a"))
+                               pic16_emitcode("pop",fReturn[pushed]);
+                       else
+                               pic16_emitcode("pop","acc");
+               }
        }
-       offset++;
-      }
-    }
-  }    
+#endif
 
-  if (pushed) {
-    while(pushed) {
-      pushed--;
-      if (strcmp(fReturn[pushed],"a"))
-       pic16_emitcode("pop",fReturn[pushed]);
-      else
-       pic16_emitcode("pop","acc");
-    }
-  }
-  pic16_freeAsmop (IC_LEFT(ic),NULL,ic,TRUE);
+
+       pic16_freeAsmop (IC_LEFT(ic),NULL,ic,TRUE);
     
- jumpret:
-  /* generate a jump to the return label
-     if the next is not the return statement */
-  if (!(ic->next && ic->next->op == LABEL &&
-       IC_LABEL(ic->next) == returnLabel)) {
+jumpret:
+       /* generate a jump to the return label
+        * if the next is not the return statement */
+       if (!(ic->next && ic->next->op == LABEL
+               && IC_LABEL(ic->next) == returnLabel)) {
        
-    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(returnLabel->key));
-    pic16_emitcode("goto","_%05d_DS_",returnLabel->key+100 + labelOffset);
-  }
-    
+               pic16_emitpcode(POC_GOTO,pic16_popGetLabel(returnLabel->key));
+               pic16_emitcode("goto","_%05d_DS_",returnLabel->key+100 + labelOffset);
+       }
 }
 
 /*-----------------------------------------------------------------*/
@@ -3183,30 +3560,17 @@ static void genMultOneByte (operand *left,
   if(size == 1) {
 
     if (AOP_TYPE(right) == AOP_LIT){
-      pic16_emitcode("multiply ","lit val:%s by variable %s and store in %s", 
+      pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", 
                     pic16_aopGet(AOP(right),0,FALSE,FALSE), 
                     pic16_aopGet(AOP(left),0,FALSE,FALSE), 
                     pic16_aopGet(AOP(result),0,FALSE,FALSE));
-      pic16_emitcode("call","genMultLit");
     } else {
-      pic16_emitcode("multiply ","variable :%s by variable %s and store in %s", 
+      pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", 
                     pic16_aopGet(AOP(right),0,FALSE,FALSE), 
                     pic16_aopGet(AOP(left),0,FALSE,FALSE), 
                     pic16_aopGet(AOP(result),0,FALSE,FALSE));
-      pic16_emitcode("call","pic16_genMult8X8_8");
-
     }
     pic16_genMult8X8_8 (left, right,result);
-
-
-    /* signed or unsigned */
-    //pic16_emitcode("mov","b,%s", pic16_aopGet(AOP(right),0,FALSE,FALSE));
-    //l = pic16_aopGet(AOP(left),0,FALSE,FALSE);
-    //MOVA(l);       
-    //pic16_emitcode("mul","ab");
-    /* if result size = 1, mul signed = mul unsigned */
-    //pic16_aopPut(AOP(result),"a",0);
-
   } else {  // (size > 1)
 
     pic16_emitcode("multiply (size>1) ","variable :%s by variable %s and store in %s", 
@@ -3296,7 +3660,7 @@ static void genMult (iCode *ic)
     pic16_emitcode("multiply ","sizes are greater than 2... need to insert proper algor.");
 
     /* should have been converted to function call */
-    //assert(0) ;
+       assert(0) ;
 
 release :
     pic16_freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
@@ -3636,6 +4000,7 @@ static void genIfxJump (iCode *ic, char *jval)
 /*-----------------------------------------------------------------*/
 static void genSkip(iCode *ifx,int status_bit)
 {
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   if(!ifx)
     return;
 
@@ -3656,7 +4021,7 @@ static void genSkip(iCode *ifx,int status_bit)
     }
 
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-    pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
 
   } else {
 
@@ -3675,7 +4040,7 @@ static void genSkip(iCode *ifx,int status_bit)
       break;
     }
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-    pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
 
   }
 
@@ -3715,6 +4080,7 @@ static void genSkipz2(resolvedIfx *rifx, int invert_condition)
   rifx->generated = 1;
 }
 
+#if 0
 /*-----------------------------------------------------------------*/
 /* genSkipz                                                        */
 /*-----------------------------------------------------------------*/
@@ -3739,6 +4105,8 @@ static void genSkipz(iCode *ifx, int condition)
     pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
 
 }
+#endif
+
 /*-----------------------------------------------------------------*/
 /* genSkipCond                                                     */
 /*-----------------------------------------------------------------*/
@@ -4381,11 +4749,11 @@ static void genCmp (operand *left,operand *right,
       /* Sigh. thus sucks... */
       if(size) {
        pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),size));
-       pic16_emitpcode(POC_MOVWF, popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_MOVWF, pic16_popRegFromIdx(pic16_Gstack_base_addr));
        pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80));
-       pic16_emitpcode(POC_XORWF, popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_XORWF, pic16_popRegFromIdx(pic16_Gstack_base_addr));
        pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),size));
-       pic16_emitpcode(POC_SUBFW, popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_SUBFW, pic16_popRegFromIdx(pic16_Gstack_base_addr));
       } else {
        /* Signed char comparison */
        /* Special thanks to Nikolai Golovchenko for this snippet */
@@ -4792,6 +5160,8 @@ static void genCmpEq (iCode *ic, iCode *ifx)
     operand *left, *right, *result;
     unsigned long lit = 0L;
     int size,offset=0;
+    symbol *falselbl  = newiTempLabel(NULL);
+
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
@@ -4821,9 +5191,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
 
     if(ifx && !AOP_SIZE(result)){
         symbol *tlbl;
+       DEBUGpic16_emitcode ("; ***","%s  %d CASE 1",__FUNCTION__,__LINE__);
         /* if they are both bit variables */
         if (AOP_TYPE(left) == AOP_CRY &&
             ((AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(right) == AOP_LIT))) {
+               DEBUGpic16_emitcode ("; ***","%s  %d CASE 11",__FUNCTION__,__LINE__);
             if(AOP_TYPE(right) == AOP_LIT){
                 unsigned long lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
                 if(lit == 0L){
@@ -4854,96 +5226,102 @@ static void genCmpEq (iCode *ic, iCode *ifx)
             }
             pic16_emitcode("","%05d_DS_:",tlbl->key+100+labelOffset);
 
-           {
-             /* left and right are both bit variables, result is carry */
-             resolvedIfx rIfx;
+               {
+               /* left and right are both bit variables, result is carry */
+                       resolvedIfx rIfx;
              
-             resolveIfx(&rIfx,ifx);
-
-             pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),0));
-             pic16_emitpcode(POC_ANDFW,pic16_popGet(AOP(left),0));
-             pic16_emitpcode(POC_BTFSC,pic16_popGet(AOP(right),0));
-             pic16_emitpcode(POC_ANDLW,pic16_popGet(AOP(left),0));
-             genSkipz2(&rIfx,0);
-           }
+                       resolveIfx(&rIfx,ifx);
+
+                       pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),0));
+                       pic16_emitpcode(POC_ANDFW,pic16_popGet(AOP(left),0));
+                       pic16_emitpcode(POC_BTFSC,pic16_popGet(AOP(right),0));
+                       pic16_emitpcode(POC_ANDLW,pic16_popGet(AOP(left),0));
+                       genSkipz2(&rIfx,0);
+               }
         } else {
 
-         /* They're not both bit variables. Is the right a literal? */
-         if(AOP_TYPE(right) == AOP_LIT) {
-           lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
-           
-           switch(size) {
-
-           case 1:
-             switch(lit & 0xff) {
-             case 1:
-               if ( IC_TRUE(ifx) ) {
-                 pic16_emitpcode(POC_DECFW,pic16_popGet(AOP(left),offset));
-                 emitSKPNZ;
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-               } else {
-                 pic16_emitpcode(POC_DECFSZW,pic16_popGet(AOP(left),offset));
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-               }
-               break;
-             case 0xff:
-               if ( IC_TRUE(ifx) ) {
-                 pic16_emitpcode(POC_INCFW,pic16_popGet(AOP(left),offset));
-                 emitSKPNZ;
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-               } else {
-                 pic16_emitpcode(POC_INCFSZW,pic16_popGet(AOP(left),offset));
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-               }
-               break;
-             default:
-               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offset));
-               if(lit)
-                 pic16_emitpcode(POC_XORLW,pic16_popGetLit(lit & 0xff));
-               genSkip(ifx,'z');
-             }
-
+                       DEBUGpic16_emitcode ("; ***","%s  %d CASE 12",__FUNCTION__,__LINE__);
 
-             /* end of size == 1 */
-             break;
+                       /* They're not both bit variables. Is the right a literal? */
+                       if(AOP_TYPE(right) == AOP_LIT) {
+                       lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
+           
+                       switch(size) {
+
+                               case 1:
+                                       switch(lit & 0xff) {
+                                               case 1:
+                                                               if ( IC_TRUE(ifx) ) {
+                                                                       pic16_emitpcode(POC_DECFW,pic16_popGet(AOP(left),offset));
+                                                                       emitSKPNZ;
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
+                                                               } else {
+                                                                       pic16_emitpcode(POC_DECFSZW,pic16_popGet(AOP(left),offset));
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
+                                                               }
+                                                               break;
+                                               case 0xff:
+                                                               if ( IC_TRUE(ifx) ) {
+                                                                       pic16_emitpcode(POC_INCFW,pic16_popGet(AOP(left),offset));
+                                                                       emitSKPNZ;
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
+                                                               } else {
+                                                                       pic16_emitpcode(POC_INCFSZW,pic16_popGet(AOP(left),offset));
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
+                                                               }
+                                                               break;
+                                               default:
+                                                               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offset));
+                                                               if(lit)
+                                                                       pic16_emitpcode(POC_XORLW,pic16_popGetLit(lit & 0xff));
+                                                               genSkip(ifx,'z');
+                                       } // switch lit
+
+
+                                       /* end of size == 1 */
+                                       break;
              
-           case 2:
-             genc16bit2lit(left,lit,offset);
-             genSkip(ifx,'z');
-             break;
-             /* end of size == 2 */
-
-           default:
-             /* size is 4 */
-             if(lit==0) {
-               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),1));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
-
-             } else {
-
-               /* search for patterns that can be optimized */
-
-               genc16bit2lit(left,lit,0);
-               lit >>= 16;
-               if(lit) {
-                 genSkipz(ifx,IC_TRUE(ifx) == NULL);
-                 //genSkip(ifx,'z');
-                 genc16bit2lit(left,lit,2);
-               } else {
-                 pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
-                 pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
-
-               }
-               
-             }
-
-             genSkip(ifx,'z');
-           }
+                               case 2:
+                                       genc16bit2lit(left,lit,offset);
+                                       genSkip(ifx,'z');
+                                       break;
+                                       /* end of size == 2 */
+
+                               default:
+                                       /* size is 4 */
+                                       if(lit==0) {
+                                               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),1));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
+                                               genSkip(ifx,'z');
+                                       } else {
+                                               /* search for patterns that can be optimized */
+
+                                               genc16bit2lit(left,lit,0);
+                                               lit >>= 16;
+                                               if(lit) {
+                                                               if(IC_TRUE(ifx))
+                                                               emitSKPZ; // if hi word unequal
+                                                               else
+                                                               emitSKPNZ; // if hi word equal
+                                                               // fail early
+                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(falselbl->key));
+                                                       genc16bit2lit(left,lit,2);
+                                                       genSkip(ifx,'z');
+                                               } else {
+                                                       pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
+                                                       pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
+                                                       genSkip(ifx,'z');
+                                               }
+                                       }
+                                               pic16_emitpLabel(falselbl->key);
+                                               break;
+
+                       } // switch size
          
-           ifx->generated = 1;
-           goto release ;
+                       ifx->generated = 1;
+                       goto release ;
            
 
          } else if(AOP_TYPE(right) == AOP_CRY ) {
@@ -4963,11 +5341,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
            if ( IC_TRUE(ifx) ) {
              emitSKPNZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-             pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
            } else {
              emitSKPZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-             pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
            }
 
          } else {
@@ -4983,15 +5361,25 @@ static void genCmpEq (iCode *ic, iCode *ifx)
              if ( IC_TRUE(ifx) ) {
                if(size) {
                  emitSKPZ;
+               
+                       DEBUGpic16_emitcode (";","\tIC_TRUE emitSKPZ");
+
                  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key));
                  pic16_emitcode(" goto","_%05d_DS_",tlbl->key+100+labelOffset);
                } else {
                  emitSKPNZ;
+
+                       DEBUGpic16_emitcode (";","\tIC_TRUE emitSKPNZ");
+
+
                  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
                  pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
                }
              } else {
                emitSKPZ;
+
+                       DEBUGpic16_emitcode (";","\tnot IC_TRUE emitSKPZ");
+
                pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
                pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
              }
@@ -5011,6 +5399,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
     /* if they are both bit variables */
     if (AOP_TYPE(left) == AOP_CRY &&
         ((AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(right) == AOP_LIT))) {
+       DEBUGpic16_emitcode ("; ***","%s  %d CASE 2",__FUNCTION__,__LINE__);
         if(AOP_TYPE(right) == AOP_LIT){
             unsigned long lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
             if(lit == 0L){
@@ -5043,7 +5432,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
         pic16_outBitC(result);
     } else {
       
-      DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+      DEBUGpic16_emitcode ("; ***","%s  %d CASE 3",__FUNCTION__,__LINE__);
       gencjne(left,right,result,ifx);
 /*
       if(ifx) 
@@ -6118,36 +6507,48 @@ static void genXor (iCode *ic, iCode *ifx)
 /*-----------------------------------------------------------------*/
 static void genInline (iCode *ic)
 {
-    char *buffer, *bp, *bp1;
+  char *buffer, *bp, *bp1;
     
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-    _G.inLine += (!options.asmpeep);
+       _G.inLine += (!options.asmpeep);
 
-    buffer = bp = bp1 = Safe_calloc(1, strlen(IC_INLINE(ic))+1);
-    strcpy(buffer,IC_INLINE(ic));
+       buffer = bp = bp1 = Safe_calloc(1, strlen(IC_INLINE(ic))+1);
+       strcpy(buffer,IC_INLINE(ic));
 
-    /* emit each line as a code */
-    while (*bp) {
-        if (*bp == '\n') {
-            *bp++ = '\0';
+//     fprintf(stderr, "%s:%d inline asm : < %s >\n", __FILE__, __LINE__, buffer);
+
+       /* emit each line as a code */
+       while (*bp) {
+               if (*bp == '\n') {
+                       *bp++ = '\0';
+
+                       if(*bp1)
+#if 0
+                               pic16_addpCode2pBlock(pb, pic16_newpCodeAsmDir(bp1, NULL)); // inline directly, no process
+#else
+                               pic16_addpCode2pBlock(pb, pic16_AssembleLine(bp1, 1));
+#endif
+                       bp1 = bp;
+               } else {
+                       if (*bp == ':') {
+                               bp++;
+                               *bp = '\0';
+                               bp++;
+                               pic16_emitcode(bp1,"");
+                               bp1 = bp;
+                       } else
+                               bp++;
+               }
+       }
+
+       if ((bp1 != bp) && *bp1)
+#if 0
+               pic16_addpCode2pBlock(pb, pic16_newpCodeAsmDir(bp1, NULL)); // inline directly, no process
+#else
+               pic16_addpCode2pBlock(pb, pic16_AssembleLine(bp1, 1));
+#endif
 
-           if(*bp1)
-             pic16_addpCode2pBlock(pb,pic16_AssembleLine(bp1));
-            bp1 = bp;
-        } else {
-            if (*bp == ':') {
-                bp++;
-                *bp = '\0';
-                bp++;
-                pic16_emitcode(bp1,"");
-                bp1 = bp;
-            } else
-                bp++;
-        }
-    }
-    if ((bp1 != bp) && *bp1)
-      pic16_addpCode2pBlock(pb,pic16_AssembleLine(bp1));
 
     Safe_free(buffer);
 
@@ -6162,6 +6563,8 @@ static void genRRC (iCode *ic)
   operand *left , *result ;
   int size, offset = 0, same;
 
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
   /* rotate right with carry */
   left = IC_LEFT(ic);
   result=IC_RESULT(ic);
@@ -6174,6 +6577,8 @@ static void genRRC (iCode *ic)
 
   size = AOP_SIZE(result);    
 
+  DEBUGpic16_emitcode ("; ***","%s  %d size:%d same:%d",__FUNCTION__,__LINE__,size,same);
+
   /* get the lsb and put it into the carry */
   pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),size-1));
 
@@ -6240,6 +6645,10 @@ static void genRLC (iCode *ic)
   pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
+
+/* gpasm can get the highest order bit with HIGH/UPPER
+ * so the following probably is not needed -- VR */
 /*-----------------------------------------------------------------*/
 /* genGetHbit - generates code get highest order bit               */
 /*-----------------------------------------------------------------*/
@@ -6269,6 +6678,7 @@ static void genGetHbit (iCode *ic)
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
+#if 0
 /*-----------------------------------------------------------------*/
 /* AccRol - rotate left accumulator by known count                 */
 /*-----------------------------------------------------------------*/
@@ -6306,46 +6716,90 @@ static void AccRol (int shCount)
             break;
     }
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* AccLsh - left shift accumulator by known count                  */
 /*-----------------------------------------------------------------*/
 static void AccLsh (int shCount)
 {
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(shCount != 0){
-        if(shCount == 1)
-            pic16_emitcode("add","a,acc");
-        else 
-           if(shCount == 2) {
-            pic16_emitcode("add","a,acc");
-            pic16_emitcode("add","a,acc");
-        } else {
-            /* rotate left accumulator */
-            AccRol(shCount);
-            /* and kill the lower order bits */
-            pic16_emitcode("anl","a,#0x%02x", SLMask[shCount]);
-        }
-    }
-}
-
-/*-----------------------------------------------------------------*/
-/* AccRsh - right shift accumulator by known count                 */
-/*-----------------------------------------------------------------*/
-static void AccRsh (int shCount)
-{
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(shCount != 0){
-        if(shCount == 1){
-            CLRC;
-            pic16_emitcode("rrc","a");
-        } else {
-            /* rotate right accumulator */
-            AccRol(8 - shCount);
-            /* and kill the higher order bits */
-            pic16_emitcode("anl","a,#0x%02x", SRMask[shCount]);
-        }
-    }
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       switch(shCount){
+               case 0 :
+                       return;
+                       break;
+               case 1 :
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 2 :
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 3 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 4 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 5 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 6 :
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 7 :
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+       }
+
+       pic16_emitpcode(POC_ANDLW,pic16_popGetLit(SLMask[shCount]));
+}
+
+/*-----------------------------------------------------------------*/
+/* AccRsh - right shift accumulator by known count                 */
+/*-----------------------------------------------------------------*/
+static void AccRsh (int shCount, int andmask)
+{
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       switch(shCount){
+               case 0 :
+                       return; break;
+               case 1 :
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+//                     andmask = 0;    /* no need */
+                       break;
+               case 2 :
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+//                     andmask = 0;    /* no need */
+                       break;
+               case 3 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 4 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 5 :
+                       pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 6 :
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+               case 7 :
+                       pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+                       break;
+       }
+       
+       if(andmask)
+               pic16_emitpcode(POC_ANDLW,pic16_popGetLit(SRMask[shCount]));
+       else
+               DEBUGpic16_emitcode("; ***", "%s omitting masking the result", __FUNCTION__);
 }
 
 #if 0
@@ -6683,11 +7137,23 @@ static void movLeft2Result (operand *left, int offl,
 static void shiftL2Left2Result (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
+  int same = pic16_sameRegs(AOP(result), AOP(left));
+  int i;
 
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d same:%d offl:%d offr:%d",__FUNCTION__,__LINE__,shCount,same,offl,offr);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  if (same && (offl != offr)) { // shift bytes
+    if (offr > offl) {
+       for(i=1;i>-1;i--) {
+         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),offl+i));
+         pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offr+i));
+       }
+    } else { // just treat as different later on
+               same = 0;
+    }
+  }
 
-  if(pic16_sameRegs(AOP(result), AOP(left))) {
+  if(same) {
     switch(shCount) {
     case 0:
       break;
@@ -6700,9 +7166,9 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_RLCF,  pic16_popGet(AOP(result),offr+MSB16));
 
       while(--shCount) {
-       emitCLRC;
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               emitCLRC;
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
 
       break;
@@ -6716,8 +7182,8 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_XORWF, pic16_popGet(AOP(result),offr));
       pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),offr+MSB16));
       if(shCount >=5) {
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
     case 6:
@@ -6756,9 +7222,9 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr+MSB16));
 
       while(--shCount) {
-       emitCLRC;
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               emitCLRC;
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
 
@@ -6775,8 +7241,8 @@ static void shiftL2Left2Result (operand *left, int offl,
 
 
       if(shCount == 5) {
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
     case 6:
@@ -6811,29 +7277,21 @@ static void shiftR2Left2Result (operand *left, int offl,
                                 operand *result, int offr,
                                 int shCount, int sign)
 {
-  int same=0;
-
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  same = pic16_sameRegs(AOP(result), AOP(left));
+  int same = pic16_sameRegs(AOP(result), AOP(left));
+  int i;
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d same:%d sign:%d",__FUNCTION__,__LINE__,shCount,same,sign);
 
-  if(same && ((offl + MSB16) == offr)){
-    same=1;
-    /* don't crash result[offr] */
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
-    pic16_emitcode("xch","a,%s", pic16_aopGet(AOP(left),offl+MSB16,FALSE,FALSE));
-  }
-/* else {
-    movLeft2Result(left,offl, result, offr);
-    MOVA(pic16_aopGet(AOP(left),offl+MSB16,FALSE,FALSE));
+  if (same && (offl != offr)) { // shift right bytes
+    if (offr < offl) {
+       for(i=0;i<2;i++) {
+         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),offl+i));
+         pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offr+i));
+       }
+    } else { // just treat as different later on
+               same = 0;
+    }
   }
-*/
-  /* a:x >> shCount (x = lsb(result))*/
-/*
-  if(sign)
-    AccAXRshS( pic16_aopGet(AOP(result),offr,FALSE,FALSE) , shCount);
-  else {
-    AccAXRsh( pic16_aopGet(AOP(result),offr,FALSE,FALSE) , shCount);
-*/
+
   switch(shCount) {
   case 0:
     break;
@@ -6857,9 +7315,9 @@ static void shiftR2Left2Result (operand *left, int offl,
 
     while(--shCount) {
       if(sign)
-       pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(result),offr+MSB16));
       else
-       emitCLRC;
+               emitCLRC;
       pic16_emitpcode(POC_RRCF,pic16_popGet(AOP(result),offr+MSB16));
       pic16_emitpcode(POC_RRCF,pic16_popGet(AOP(result),offr));
     }
@@ -6963,13 +7421,13 @@ static void shiftLLeftOrResult (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
+
+    pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offl));
     /* shift left accumulator */
     AccLsh(shCount);
     /* or with result */
-    pic16_emitcode("orl","a,%s", pic16_aopGet(AOP(result),offr,FALSE,FALSE));
     /* back to result */
-    pic16_aopPut(AOP(result),"a",offr);
+    pic16_emitpcode(POC_IORWF,pic16_popGet(AOP(result),offr));
 }
 
 /*-----------------------------------------------------------------*/
@@ -6979,13 +7437,13 @@ static void shiftRLeftOrResult (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
+    
+    pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offl));
     /* shift right accumulator */
-    AccRsh(shCount);
+    AccRsh(shCount, 1);
     /* or with result */
-    pic16_emitcode("orl","a,%s", pic16_aopGet(AOP(result),offr,FALSE,FALSE));
     /* back to result */
-    pic16_aopPut(AOP(result),"a",offr);
+    pic16_emitpcode(POC_IORWF,pic16_popGet(AOP(result),offr));
 }
 
 /*-----------------------------------------------------------------*/
@@ -7004,7 +7462,7 @@ static void genlshTwo (operand *result,operand *left, int shCount)
 {
     int size;
     
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
     size = pic16_getDataSize(result);
 
     /* if shCount >= 8 */
@@ -7031,64 +7489,63 @@ static void genlshTwo (operand *result,operand *left, int shCount)
 
 /*-----------------------------------------------------------------*/
 /* shiftLLong - shift left one long from left to result            */
-/* offl = LSB or MSB16                                             */
+/* offr = LSB or MSB16                                             */
 /*-----------------------------------------------------------------*/
 static void shiftLLong (operand *left, operand *result, int offr )
 {
-    char *l;
     int size = AOP_SIZE(result);
+    int same = pic16_sameRegs(AOP(left),AOP(result));
+       int i;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(size >= LSB+offr){
-        l = pic16_aopGet(AOP(left),LSB,FALSE,FALSE);
-        MOVA(l);
-        pic16_emitcode("add","a,acc");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB16+offr && offr != LSB )
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),LSB+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",LSB+offr);
-    }
+    DEBUGpic16_emitcode ("; ***","%s  %d  offr:%d size:%d",__FUNCTION__,__LINE__,offr,size);
 
-    if(size >= MSB16+offr){
-       if (!(pic16_sameRegs(AOP(result),AOP(left)) && size >= MSB16+offr && offr != LSB) ) {
-           l = pic16_aopGet(AOP(left),MSB16,FALSE,FALSE);
-           MOVA(l);
+       if (same && (offr == MSB16)) { //shift one byte
+               for(i=size-1;i>=MSB16;i--) {
+                       pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),i-1));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(left),i));
+               }
+       } else {
+               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),LSB+offr));
        }
-        pic16_emitcode("rlc","a");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB24+offr && offr != LSB)
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),MSB16+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",MSB16+offr);
+       
+    if (size >= LSB+offr ){
+               if (same) {
+                       pic16_emitpcode(POC_ADDWF,pic16_popGet(AOP(left),LSB+offr));
+               } else {
+                       pic16_emitpcode(POC_ADDFW,pic16_popGet(AOP(left),LSB));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),LSB+offr));
+               }
+        }
+
+    if(size >= MSB16+offr){
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB16+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB16));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB16+offr));
+               }
     }
 
     if(size >= MSB24+offr){
-       if (!(pic16_sameRegs(AOP(left),AOP(left)) && size >= MSB24+offr && offr != LSB)) {
-           l = pic16_aopGet(AOP(left),MSB24,FALSE,FALSE);
-           MOVA(l);
-       }
-        pic16_emitcode("rlc","a");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB32+offr && offr != LSB )
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),MSB24+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",MSB24+offr);
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB24+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB24));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB24+offr));
+               }
     }
 
     if(size > MSB32+offr){
-       if (!(pic16_sameRegs(AOP(result),AOP(left)) && size >= MSB32+offr && offr != LSB)) {
-           l = pic16_aopGet(AOP(left),MSB32,FALSE,FALSE);
-           MOVA(l);    
-       }
-        pic16_emitcode("rlc","a");
-        pic16_aopPut(AOP(result),"a",MSB32+offr);
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB32+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB32));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB32+offr));
+               }
     }
     if(offr != LSB)
-        pic16_aopPut(AOP(result),zero,LSB);       
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -7110,9 +7567,11 @@ static void genlshFour (operand *result, operand *left, int shCount)
             shiftL1Left2Result(left, LSB, result, MSB32, shCount);
         else
             movLeft2Result(left, LSB, result, MSB32);
-        pic16_aopPut(AOP(result),zero,LSB);
-        pic16_aopPut(AOP(result),zero,MSB16);
-        pic16_aopPut(AOP(result),zero,MSB32);
+
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB16));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB24));
+
         return;
     }
 
@@ -7127,8 +7586,8 @@ static void genlshFour (operand *result, operand *left, int shCount)
             movLeft2Result(left, MSB16, result, MSB32);
             movLeft2Result(left, LSB, result, MSB24);
         }
-        pic16_aopPut(AOP(result),zero,MSB16);
-        pic16_aopPut(AOP(result),zero,LSB);
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB16));
         return;
     }    
 
@@ -7147,7 +7606,7 @@ static void genlshFour (operand *result, operand *left, int shCount)
                 movLeft2Result(left, MSB24, result, MSB32);
                 movLeft2Result(left, MSB16, result, MSB24);
                 movLeft2Result(left, LSB, result, MSB16);
-                pic16_aopPut(AOP(result),zero,LSB);
+                               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
             }
             else if(shCount == 1)
                 shiftLLong(left, result, MSB16);
@@ -7155,15 +7614,16 @@ static void genlshFour (operand *result, operand *left, int shCount)
                 shiftL2Left2Result(left, MSB16, result, MSB24, shCount);
                 shiftL1Left2Result(left, LSB, result, MSB16, shCount);
                 shiftRLeftOrResult(left, LSB, result, MSB24, 8 - shCount);
-                pic16_aopPut(AOP(result),zero,LSB);
+                               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
             }
         }
     }
 
     /* 1 <= shCount <= 7 */
-    else if(shCount <= 2){
+    else if(shCount <= 3)
+    { 
         shiftLLong(left, result, LSB);
-        if(shCount == 2)
+        while(--shCount >= 1)
             shiftLLong(result, result, LSB);
     }
     /* 3 <= shCount <= 7, optimize */
@@ -7185,7 +7645,7 @@ static void genLeftShiftLiteral (operand *left,
     int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
     int size;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
     pic16_freeAsmop(right,NULL,ic,TRUE);
 
     pic16_aopOp(left,ic,FALSE);
@@ -7335,8 +7795,8 @@ static void genLeftShift (iCode *ic)
 
       tlbl = newiTempLabel(NULL);
       if (!pic16_sameRegs(AOP(left),AOP(result))) {
-       pic16_emitpcode(POC_MOVFW,  pic16_popGet(AOP(left),0));
-       pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),0));
+               pic16_emitpcode(POC_MOVFW,  pic16_popGet(AOP(left),0));
+               pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),0));
       }
 
       pic16_emitpcode(POC_COMFW,  pic16_popGet(AOP(right),0));
@@ -7398,7 +7858,8 @@ static void genLeftShift (iCode *ic)
   pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
 
   /* offset should be 0, 1 or 3 */
-  pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x07 + ((offset&3) << 3)));
+  
+  pic16_emitpcode(POC_ANDLW, pic16_popGetLit((size<<3)-1));
   emitSKPNZ;
   pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(tlbl1->key));
 
@@ -7441,7 +7902,7 @@ static void genrshOne (operand *result, operand *left,
 static void genrshTwo (operand *result,operand *left,
                        int shCount, int sign)
 {
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
   /* if shCount >= 8 */
   if (shCount >= 8) {
     shCount -= 8 ;
@@ -7471,31 +7932,58 @@ static void genrshTwo (operand *result,operand *left,
 static void shiftRLong (operand *left, int offl,
                         operand *result, int sign)
 {
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(!sign)
-        pic16_emitcode("clr","c");
-    MOVA(pic16_aopGet(AOP(left),MSB32,FALSE,FALSE));
+    int size = AOP_SIZE(result);
+    int same = pic16_sameRegs(AOP(left),AOP(result));
+    int i;
+    DEBUGpic16_emitcode ("; ***","%s  %d  offl:%d size:%d",__FUNCTION__,__LINE__,offl,size);
+
+       if (same && (offl == MSB16)) { //shift one byte right
+               for(i=MSB16;i<size;i++) {
+                       pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),i));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(left),i-1));
+               }
+       }
+
     if(sign)
-        pic16_emitcode("mov","c,acc.7");
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB32-offl);
-    if(offl == MSB16)
+               pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(left),MSB32));
+       else
+               emitCLRC;
+
+       if (same) {
+               if (offl == LSB)
+               pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB32));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB32));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB32-offl));
+       }
+
+    if(offl == MSB16) {
         /* add sign of "a" */
         pic16_addSign(result, MSB32, sign);
+       }
 
-    MOVA(pic16_aopGet(AOP(left),MSB24,FALSE,FALSE));
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB24-offl);
-
-    MOVA(pic16_aopGet(AOP(left),MSB16,FALSE,FALSE));
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB16-offl);
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB24));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB24));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB24-offl));
+       }
+       
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB16));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB16));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB16-offl));
+       }
 
-    if(offl == LSB){
-        MOVA(pic16_aopGet(AOP(left),LSB,FALSE,FALSE));
-        pic16_emitcode("rrc","a");
-        pic16_aopPut(AOP(result),"a",LSB);
-    }
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),LSB));
+       } else {
+       if(offl == LSB){
+               pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),LSB));
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),LSB));
+       }
+       }
 }
 
 /*-----------------------------------------------------------------*/
@@ -7535,7 +8023,7 @@ static void genrshFour (operand *result, operand *left,
       movLeft2Result(left, MSB32, result, MSB24);
       pic16_addSign(result, MSB32, sign);
     }
-    else{
+    else{ //shcount >= 2
       shiftR2Left2Result(left, MSB16, result, LSB, shCount, 0);
       shiftLLeftOrResult(left, MSB32, result, MSB16, 8 - shCount);
       /* the last shift is signed */
@@ -7569,12 +8057,13 @@ static void genRightShiftLiteral (operand *left,
   int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
   int lsize,res_size;
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   pic16_freeAsmop(right,NULL,ic,TRUE);
 
   pic16_aopOp(left,ic,FALSE);
   pic16_aopOp(result,ic,FALSE);
 
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d result:%d left:%d",__FUNCTION__,__LINE__,shCount,AOP_SIZE(result),AOP_SIZE(left));
+
 #if VIEW_SIZE
   pic16_emitcode("; shift right ","result %d, left %d",AOP_SIZE(result),
                 AOP_SIZE(left));
@@ -7784,17 +8273,17 @@ static void genSignedRightShift (iCode *ic)
 static void genRightShift (iCode *ic)
 {
     operand *right, *left, *result;
-    sym_link *retype ;
+    sym_link *letype ;
     int size, offset;
     char *l;
     symbol *tlbl, *tlbl1 ;
 
     /* if signed then we do it the hard way preserve the
     sign bit moving it inwards */
-    retype = getSpec(operandType(IC_RESULT(ic)));
+    letype = getSpec(operandType(IC_LEFT(ic)));
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-    if (!SPEC_USIGN(retype)) {
+    if (!SPEC_USIGN(letype)) {
         genSignedRightShift (ic);
         return ;
     }
@@ -7905,49 +8394,53 @@ static void genUnpackBits (operand *result, char *rname, int ptype)
     sym_link *etype;
     int offset = 0 ;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    etype = getSpec(operandType(result));
-
-    /* read the first byte  */
-    switch (ptype) {
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       etype = getSpec(operandType(result));
 
-    case POINTER:
-    case IPOINTER:
-       pic16_emitcode("mov","a,@%s",rname);
-       break;
-       
-    case PPOINTER:
-       pic16_emitcode("movx","a,@%s",rname);
-       break;
+       /* read the first byte  */
+       switch (ptype) {
+               case POINTER:
+               case IPOINTER:
+               case PPOINTER:
+               case FPOINTER:
+               case GPOINTER:
+                       pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+                       break;
+               case CPOINTER:
+                       pic16_emitcode("clr","a");
+                       pic16_emitcode("movc","a","@a+dptr");
+                       break;
+       }
        
-    case FPOINTER:
-       pic16_emitcode("movx","a,@dptr");
-       break;
 
-    case CPOINTER:
-       pic16_emitcode("clr","a");
-       pic16_emitcode("movc","a","@a+dptr");
-       break;
+       /* if we have bitdisplacement then it fits   */
+       /* into this byte completely or if length is */
+       /* less than a byte                          */
+       if ((shCnt = SPEC_BSTR(etype)) || 
+               (SPEC_BLEN(etype) <= 8))  {
 
-    case GPOINTER:
-       pic16_emitcode("lcall","__gptrget");
-       break;
-    }
+               /* shift right acc */
+               AccRsh(shCnt, 0);
 
-    /* if we have bitdisplacement then it fits   */
-    /* into this byte completely or if length is */
-    /* less than a byte                          */
-    if ((shCnt = SPEC_BSTR(etype)) || 
-        (SPEC_BLEN(etype) <= 8))  {
+               pic16_emitpcode(POC_ANDLW, pic16_popGetLit(
+                       (((unsigned char) -1)>>(8 - SPEC_BLEN(etype))) & SRMask[ shCnt ]));
 
-        /* shift right acc */
-        AccRsh(shCnt);
+/* VR -- normally I would use the following, but since we use the hack,
+ * to avoid the masking from AccRsh, why not mask it right now? */
 
-        pic16_emitcode("anl","a,#0x%02x",
-                 ((unsigned char) -1)>>(8 - SPEC_BLEN(etype)));
-        pic16_aopPut(AOP(result),"a",offset);
-        return ;
-    }
+/*
+               pic16_emitpcode(POC_ANDLW, pic16_popGetLit(((unsigned char) -1)>>(8 - SPEC_BLEN(etype))));
+*/
+
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0));
+         return ;
+       }
+
+
+
+       fprintf(stderr, "SDCC pic16 port error: the port currently does not support\n");
+       fprintf(stderr, "bitfields of size >=8. Instead of generating wrong code, bailling out...\n");
+       exit(-1);
 
     /* bit field did not fit in a byte  */
     rlen = SPEC_BLEN(etype) - 8;
@@ -8001,42 +8494,79 @@ static void genUnpackBits (operand *result, char *rname, int ptype)
     return ;
 }
 
-#if 0
-/*-----------------------------------------------------------------*/
-/* genDataPointerGet - generates code when ptr offset is known     */
-/*-----------------------------------------------------------------*/
-static void genDataPointerGet (operand *left, 
-                              operand *result, 
-                              iCode *ic)
+
+static void genDataPointerGet(operand *left,
+                             operand *result,
+                             iCode *ic)
 {
-  int size , offset = 0;
+  int size, offset = 0, leoffset=0 ;
 
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       pic16_aopOp(result, ic, FALSE);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       size = AOP_SIZE(result);
+//     fprintf(stderr, "%s:%d size= %d\n", __FILE__, __LINE__, size);
 
 
-  /* optimization - most of the time, left and result are the same
-   * address, but different types. for the pic code, we could omit
-   * the following
-   */
+#if 0
+       /* The following tests may save a redudant movff instruction when
+        * accessing unions */
+        
+       /* if they are the same */
+       if (operandsEqu (left, result)) {
+               DEBUGpic16_emitcode("; ***", "left and result operands are equ/same");
+               goto release;
+       }
 
-  pic16_aopOp(result,ic,TRUE);
+       /* if they are the same registers */
+       if (pic16_sameRegs(AOP(left),AOP(result))) {
+               DEBUGpic16_emitcode("; ***", "left and result registers are same");
+               goto release;
+       }
+#endif
 
-  DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
+#if 0
+       if ( AOP_TYPE(left) == AOP_PCODE) {
+               fprintf(stderr,"genDataPointerGet   %s, %d\n",
+                               AOP(left)->aopu.pcop->name,
+                               (AOP(left)->aopu.pcop->type == PO_DIR)?
+                               PCOR(AOP(left)->aopu.pcop)->instance:
+                               PCOI(AOP(left)->aopu.pcop)->offset);
+       }
+#endif
 
-  pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),0));
+       if(AOP(left)->aopu.pcop->type == PO_DIR)
+               leoffset=PCOR(AOP(left)->aopu.pcop)->instance;
 
-  size = AOP_SIZE(result);
+       DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
 
-  while (size--) {
-    pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset));
-    offset++;
-  }
+       while (size--) {
+               DEBUGpic16_emitcode("; ***", "%s loop offset=%d leoffset=%d", __FUNCTION__, offset, leoffset);
+               
+               if(AOP(result)->aopu.pcop->type == PO_IMMEDIATE
+                       || AOP(left)->aopu.pcop->type == PO_IMMEDIATE) {
+                       mov2w(AOP(left), leoffset);
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), offset));
+               } else {
+                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                               pic16_popGet(AOP(left), leoffset),
+                               pic16_popGet(AOP(result), offset)));
+               }
 
-  pic16_freeAsmop(left,NULL,ic,TRUE);
-  pic16_freeAsmop(result,NULL,ic,TRUE);
+               offset++;
+               leoffset++;
+       }
+
+//release:
+    pic16_freeAsmop(result,NULL,ic,TRUE);
 }
-#endif
+
+void pic16_loadFSR0(operand *op)
+{
+       pic16_emitpcode(POC_LFSR, pic16_popGetLit2(0, pic16_popGet(AOP(op), 0)));
+}
+
+
 /*-----------------------------------------------------------------*/
 /* genNearPointerGet - pic16_emitcode for near pointer fetch             */
 /*-----------------------------------------------------------------*/
@@ -8046,68 +8576,79 @@ static void genNearPointerGet (operand *left,
 {
     asmop *aop = NULL;
     //regs *preg = NULL ;
-    char *rname ;
     sym_link *rtype, *retype;
     sym_link *ltype = operandType(left);    
-    //char buffer[80];
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-    rtype = operandType(result);
-    retype= getSpec(rtype);
-    
-    pic16_aopOp(left,ic,FALSE);
+       rtype = operandType(result);
+       retype= getSpec(rtype);
     
-    /* if left is rematerialisable and
-       result is not bit variable type and
-       the left is pointer to data space i.e
-       lower 128 bytes of space */
-    if (AOP_TYPE(left) == AOP_PCODE &&  //AOP_TYPE(left) == AOP_IMMD &&
-       !IS_BITVAR(retype)         &&
-       DCL_TYPE(ltype) == POINTER) {
-      //genDataPointerGet (left,result,ic);
-       return ;
-    }
+       pic16_aopOp(left,ic,FALSE);
+
+       /* if left is rematerialisable and
+        * result is not bit variable type and
+        * the left is pointer to data space i.e
+        * lower 128 bytes of space */
+       if (AOP_TYPE(left) == AOP_PCODE
+               && !IS_BITFIELD(retype)
+               && DCL_TYPE(ltype) == POINTER) {
+
+               genDataPointerGet (left,result,ic);
+               pic16_freeAsmop(left, NULL, ic, TRUE);
+         return ;
+       }
     
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
        /* if the value is already in a pointer register
-       then don't need anything more */
-    if (!AOP_INPREG(AOP(left))) {
-       /* otherwise get a free pointer register */
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-/*
-       aop = newAsmop(0);
-       preg = getFreePtr(ic,&aop,FALSE);
-       pic16_emitcode("mov","%s,%s",
-               preg->name,
-               pic16_aopGet(AOP(left),0,FALSE,TRUE));
-       rname = preg->name ;
-*/
-    rname ="BAD";
-    } else
-       rname = pic16_aopGet(AOP(left),0,FALSE,FALSE);
+        * then don't need anything more */
+       if (!AOP_INPREG(AOP(left))) {
+               /* otherwise get a free pointer register */
+               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+               
+               /* VR -- the whole concept is to load FSR0 with the address of the symbol */
+               pic16_loadFSR0( left );
+       }
+//      else
+//     rname = pic16_aopGet(AOP(left),0,FALSE,FALSE);
     
     pic16_aopOp (result,ic,FALSE);
     
       /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
-       genUnpackBits (result,rname,POINTER);
+    if (IS_BITFIELD(retype)) 
+       genUnpackBits (result, NULL, POINTER);
     else {
        /* we have can just get the values */
       int size = AOP_SIZE(result);
-      int offset = 0 ; 
+      int offset = 0 
        
       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-      pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
-      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
+
+       /* fsr0 is loaded already -- VR */
+//     pic16_loadFSR0( left );
+
+//      pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
+//      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
       while(size--) {
-       pic16_emitpcode(POC_MOVFW,pic16_popCopyReg(&pic16_pc_indf0));
-       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offset++));
+
+       if(size) {
+               pic16_emitpcode(POC_MOVFF,
+                       pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0),
+                               pic16_popGet(AOP(result), offset++)));
+       } else {
+               pic16_emitpcode(POC_MOVFF,
+                       pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0),
+                               pic16_popGet(AOP(result), offset++)));
+       }
+      }
+#if 0
+//     pic16_emitpcode(POC_MOVFW,pic16_popCopyReg(&pic16_pc_postinc0));
+//     pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offset++));
        if(size)
          pic16_emitpcode(POC_INCF,pic16_popCopyReg(&pic16_pc_fsr0));
-      }
+#endif
 /*
        while (size--) {
            if (IS_AOP_PREG(result) || AOP_TYPE(result) == AOP_STK ) {
@@ -8141,9 +8682,9 @@ static void genNearPointerGet (operand *left,
            !OP_SYMBOL(left)->remat &&
            ( OP_SYMBOL(left)->liveTo > ic->seq ||
              ic->depth )) {
-           int size = AOP_SIZE(result) - 1;
-           while (size--)
-               pic16_emitcode("dec","%s",rname);
+//         int size = AOP_SIZE(result) - 1;
+//         while (size--)
+//             pic16_emitcode("dec","%s",rname);
        }
     }
 
@@ -8189,7 +8730,7 @@ static void genPagedPointerGet (operand *left,
     pic16_aopOp (result,ic,FALSE);
 
     /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
        genUnpackBits (result,rname,PPOINTER);
     else {
        /* we have can just get the values */
@@ -8267,7 +8808,7 @@ static void genFarPointerGet (operand *left,
     pic16_aopOp(result,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genUnpackBits(result,"dptr",FPOINTER);
     else {
         size = AOP_SIZE(result);
@@ -8317,7 +8858,7 @@ static void genCodePointerGet (operand *left,
     pic16_aopOp(result,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genUnpackBits(result,"dptr",CPOINTER);
     else {
         size = AOP_SIZE(result);
@@ -8341,50 +8882,61 @@ static void genCodePointerGet (operand *left,
 static void genGenPointerGet (operand *left,
                               operand *result, iCode *ic)
 {
-  int size, offset ;
+  int size, offset, lit;
   sym_link *retype = getSpec(operandType(result));
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  pic16_aopOp(left,ic,FALSE);
-  pic16_aopOp(result,ic,FALSE);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       pic16_aopOp(left,ic,FALSE);
+       pic16_aopOp(result,ic,FALSE);
+       size = AOP_SIZE(result);
 
+       DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
 
-  DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
+       if (AOP_TYPE(left) == AOP_IMMD) { // do we ever get here? (untested!)
 
-  /* 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) {
-      pic16_emitcode("mov","dptr,%s",pic16_aopGet(AOP(left),0,TRUE,FALSE));
-      pic16_emitcode("mov","b,#%d",pointerCode(retype));
-    }
-    else { /* we need to get it byte by byte */
+               lit = (unsigned)floatFromVal(AOP(left)->aopu.aop_lit);
+               // load FSR0 from immediate
+               pic16_emitpcode(POC_LFSR,pic16_popGetLit2(0,pic16_popGetLit(lit)));
 
-      pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
-      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
+//             pic16_loadFSR0( left );
 
-      size = AOP_SIZE(result);
-      offset = 0 ;
+               offset = 0;
+               while(size--) {
+                       if(size) {
+                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0), pic16_popGet(AOP(result),offset)));
+                       } else {
+                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0), pic16_popGet(AOP(result),offset)));
+                       }
+                       offset++;
+               }
+               goto release;
 
-      while(size--) {
-       pic16_emitpcode(POC_MOVFW,pic16_popCopyReg(&pic16_pc_indf0));
-       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offset++));
-       if(size)
-         pic16_emitpcode(POC_INCF,pic16_popCopyReg(&pic16_pc_fsr0));
-      }
-      goto release;
-    }
-    //}
-  /* so dptr know contains the address */
+       }
+       else { /* we need to get it byte by byte */
+               // set up FSR0 with address from left
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l)));
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h)));
+
+               offset = 0 ;
+
+               while(size--) {
+                       if(size) {
+                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0), pic16_popGet(AOP(result),offset)));
+                       } else {
+                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0), pic16_popGet(AOP(result),offset)));
+                       }
+                       offset++;
+               }
+               goto release;
+       }
 
   /* if bit then unpack */
-  //if (IS_BITVAR(retype)) 
-  //  genUnpackBits(result,"dptr",GPOINTER);
+       if (IS_BITFIELD(retype)) 
+               genUnpackBits(result,"BAD",GPOINTER);
 
- release:
-  pic16_freeAsmop(left,NULL,ic,TRUE);
-  pic16_freeAsmop(result,NULL,ic,TRUE);
      release:
+       pic16_freeAsmop(left,NULL,ic,TRUE);
+       pic16_freeAsmop(result,NULL,ic,TRUE);
 
 }
 
@@ -8414,6 +8966,7 @@ static void genConstPointerGet (operand *left,
 
   poc = ( (AOP_TYPE(left) == AOP_PCODE) ? POC_MOVLW : POC_MOVFW);
     
+  /* this performs a goto to the specified address -- Why not to use pointer? -- VR */
   pic16_emitpcode(poc,pic16_popGet(AOP(left),1));
   pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_pclath));
   pic16_emitpcode(poc,pic16_popGet(AOP(left),0));
@@ -8447,7 +9000,11 @@ static void genPointerGet (iCode *ic)
     type = operandType(left);
     etype = getSpec(type);
 
+#if 0
     if (IS_PTR_CONST(type))
+#else
+    if (IS_CODEPTR(type))
+#endif
       DEBUGpic16_emitcode ("; ***","%d - const pointer",__LINE__);
 
     /* if left is of type of pointer then it is simple */
@@ -8503,9 +9060,11 @@ static void genPointerGet (iCode *ic)
        break;
 
     case GPOINTER:
+#if 0
       if (IS_PTR_CONST(type))
        genConstPointerGet (left,result,ic);
       else
+#endif
        genGenPointerGet (left,result,ic);
       break;
     }
@@ -8519,71 +9078,83 @@ static void genPackBits (sym_link    *etype ,
                          operand *right ,
                          char *rname, int p_type)
 {
-    int shCount = 0 ;
-    int offset = 0  ;
-    int rLen = 0 ;
-    int blen, bstr ;   
-    char *l ;
-
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    blen = SPEC_BLEN(etype);
-    bstr = SPEC_BSTR(etype);
-
-    l = pic16_aopGet(AOP(right),offset++,FALSE,FALSE);
-    MOVA(l);   
-
-    /* if the bit lenth is less than or    */
-    /* it exactly fits a byte then         */
-    if (SPEC_BLEN(etype) <= 8 )  {
-        shCount = SPEC_BSTR(etype) ;
-
-        /* shift left acc */
-        AccLsh(shCount);
-
-        if (SPEC_BLEN(etype) < 8 ) { /* if smaller than a byte */
+  int shCnt = 0 ;
+  int offset = 0  ;
+  int rLen = 0 ;
+  int blen, bstr ;   
+  char *l ;
 
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       blen = SPEC_BLEN(etype);
+       bstr = SPEC_BSTR(etype);
 
-            switch (p_type) {
-                case POINTER:
-                    pic16_emitcode ("mov","b,a");
-                    pic16_emitcode("mov","a,@%s",rname);
-                    break;
+       if(AOP_TYPE(right) == AOP_LIT) {
+               pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right), 0));
+               offset++;
+       } else
+               pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++));
 
-                case FPOINTER:
-                    pic16_emitcode ("mov","b,a");
-                    pic16_emitcode("movx","a,@dptr");
-                    break;
+/*     l = pic16_aopGet(AOP(right),offset++,FALSE,FALSE);
+       MOVA(l);   
+*/
+       /* if the bit lenth is less than or    */
+       /* it exactly fits a byte then         */
+       if((shCnt=SPEC_BSTR(etype))
+               || SPEC_BLEN(etype) <= 8 )  {
+
+               /* shift left acc */
+               AccLsh(shCnt);
+
+               /* using PRODL as a temporary register here */
+               pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_prodl));
+
+               switch (p_type) {
+                       case FPOINTER:
+                       case POINTER:
+                       case GPOINTER:
+                               pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+//                             pic16_emitcode ("mov","b,a");
+//                             pic16_emitcode("mov","a,@%s",rname);
+                               break;
+               }
+#if 1
+               pic16_emitpcode(POC_ANDLW, pic16_popGetLit(
+                       (unsigned char)((unsigned char)(0xff << (blen+bstr)) |
+                                       (unsigned char)(0xff >> (8-bstr))) ));
+               pic16_emitpcode(POC_IORFW, pic16_popCopyReg(&pic16_pc_prodl));
+               pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+#endif
 
-                case GPOINTER:
-                    pic16_emitcode ("push","b");
-                    pic16_emitcode ("push","acc");
-                    pic16_emitcode ("lcall","__gptrget");
-                    pic16_emitcode ("pop","b");
-                    break;
-            }
+#if 0
+               pic16_emitcode ("anl","a,#0x%02x",(unsigned char)
+                                       ((unsigned char)(0xFF << (blen+bstr)) | 
+                                       (unsigned char)(0xFF >> (8-bstr)) ) );
+               pic16_emitcode ("orl","a,b");
+               if (p_type == GPOINTER)
+                       pic16_emitcode("pop","b");
 
-            pic16_emitcode ("anl","a,#0x%02x",(unsigned char)
-                      ((unsigned char)(0xFF << (blen+bstr)) | 
-                       (unsigned char)(0xFF >> (8-bstr)) ) );
-            pic16_emitcode ("orl","a,b");
-            if (p_type == GPOINTER)
-                pic16_emitcode("pop","b");
-        }
-    }
+               
+               switch (p_type) {
+                       case POINTER:
+                               pic16_emitcode("mov","@%s,a",rname);
+                               break;
+                       case FPOINTER:
+                               pic16_emitcode("movx","@dptr,a");
+                               break;
+                       case GPOINTER:
+                               DEBUGpic16_emitcode(";lcall","__gptrput");
+                               break;
+               }
+#endif
 
-    switch (p_type) {
-        case POINTER:
-            pic16_emitcode("mov","@%s,a",rname);
-            break;
+         return;
+       }
 
-        case FPOINTER:
-            pic16_emitcode("movx","@dptr,a");
-            break;
 
-        case GPOINTER:
-            DEBUGpic16_emitcode(";lcall","__gptrput");
-            break;
-    }
+       fprintf(stderr, "SDCC pic16 port error: the port currently does not support\n");
+       fprintf(stderr, "bitfields of size >=8. Instead of generating wrong code, bailling out...\n");
+       exit(-1);
+
 
     /* if we r done */
     if ( SPEC_BLEN(etype) <= 8 )
@@ -8592,6 +9163,8 @@ static void genPackBits (sym_link    *etype ,
     pic16_emitcode("inc","%s",rname);
     rLen = SPEC_BLEN(etype) ;     
 
+
+
     /* now generate for lengths greater than one byte */
     while (1) {
 
@@ -8676,65 +9249,53 @@ static void genDataPointerSet(operand *right,
                              operand *result,
                              iCode *ic)
 {
-    int size, offset = 0 ;
-    char *l, buffer[256];
+    int size, offset = 0, resoffset=0 ;
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     pic16_aopOp(right,ic,FALSE);
-    
-    l = pic16_aopGet(AOP(result),0,FALSE,TRUE);
+
     size = AOP_SIZE(right);
-/*
+       fprintf(stderr, "%s:%d size= %d\n", __FILE__, __LINE__, size);
+
+#if 1
     if ( AOP_TYPE(result) == AOP_PCODE) {
       fprintf(stderr,"genDataPointerSet   %s, %d\n",
              AOP(result)->aopu.pcop->name,
+               (AOP(result)->aopu.pcop->type == PO_DIR)?
+             PCOR(AOP(result)->aopu.pcop)->instance:
              PCOI(AOP(result)->aopu.pcop)->offset);
     }
-*/
-
-    // tsd, was l+1 - the underline `_' prefix was being stripped
-    while (size--) {
-      if (offset) {
-       sprintf(buffer,"(%s + %d)",l,offset);
-       fprintf(stderr,"oops  %s\n",buffer);
-      } else
-       sprintf(buffer,"%s",l);
-
-       if (AOP_TYPE(right) == AOP_LIT) {
-         unsigned int lit = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit);
-         lit = lit >> (8*offset);
-         if(lit&0xff) {
-           pic16_emitcode("movlw","%d",lit);
-           pic16_emitcode("movwf","%s",buffer);
-
-           pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit&0xff));
-           //pic16_emitpcode(POC_MOVWF, popRegFromString(buffer));
-           pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
-
-         } else {
-           pic16_emitcode("clrf","%s",buffer);
-           //pic16_emitpcode(POC_CLRF, popRegFromString(buffer));
-           pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),0));
-         }
-       }else {
-         pic16_emitcode("movf","%s,w",pic16_aopGet(AOP(right),offset,FALSE,FALSE));
-         pic16_emitcode("movwf","%s",buffer);
+#endif
 
-         pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset));
-         //pic16_emitpcode(POC_MOVWF, popRegFromString(buffer));
-         pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+       if(AOP(result)->aopu.pcop->type == PO_DIR)
+               resoffset=PCOR(AOP(result)->aopu.pcop)->instance;
 
+       while (size--) {
+               if (AOP_TYPE(right) == AOP_LIT) {
+                 unsigned int lit = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit);
+
+                       lit = lit >> (8*offset);
+                       if(lit&0xff) {
+                               pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit&0xff));
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),resoffset));
+                       } else {
+                               pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),resoffset));
+                       }
+               } else {
+                       mov2w(AOP(right), offset);
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),resoffset));
+               }
+               offset++;
+               resoffset++;
        }
 
-       offset++;
-    }
-
     pic16_freeAsmop(right,NULL,ic,TRUE);
-    pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
+
+
 /*-----------------------------------------------------------------*/
-/* genNearPointerSet - pic16_emitcode for near pointer put                */
+/* genNearPointerSet - pic16_emitcode for near pointer put         */
 /*-----------------------------------------------------------------*/
 static void genNearPointerSet (operand *right,
                                operand *result, 
@@ -8744,120 +9305,114 @@ static void genNearPointerSet (operand *right,
   char *l;
   sym_link *retype;
   sym_link *ptype = operandType(result);
-
+  sym_link *resetype;
     
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  retype= getSpec(operandType(right));
-
-  pic16_aopOp(result,ic,FALSE);
-
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       retype= getSpec(operandType(right));
+       resetype = getSpec(operandType(result));
+  
+       pic16_aopOp(result,ic,FALSE);
     
-  /* if the result is rematerializable &
-     in data space & not a bit variable */
-  //if (AOP_TYPE(result) == AOP_IMMD &&
-  if (AOP_TYPE(result) == AOP_PCODE &&  //AOP_TYPE(result) == AOP_IMMD &&
-      DCL_TYPE(ptype) == POINTER   &&
-      !IS_BITVAR(retype)) {
-    genDataPointerSet (right,result,ic);
-    pic16_freeAsmop(result,NULL,ic,TRUE);
-    return;
-  }
-
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  pic16_aopOp(right,ic,FALSE);
-  DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
-
-  /* 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);
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    //pic16_emitcode("mov","%s,%s",
-    //         preg->name,
-    //         pic16_aopGet(AOP(result),0,FALSE,TRUE));
-    //rname = preg->name ;
-    //pic16_emitcode("movwf","fsr0");
-    pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(result),0));
-    pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_fsr0));
-    pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
-    pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
-    goto release;
+       /* if the result is rematerializable &
+        * in data space & not a bit variable */
+       
+       /* and result is not a bit variable */
+       if (AOP_TYPE(result) == AOP_PCODE
+//             && AOP_TYPE(result) == AOP_IMMD
+               && DCL_TYPE(ptype) == POINTER
+               && !IS_BITFIELD(retype)
+               && !IS_BITFIELD(resetype)) {
+
+               genDataPointerSet (right,result,ic);
+               pic16_freeAsmop(result,NULL,ic,TRUE);
+         return;
+       }
 
-  }// else
-  //   rname = pic16_aopGet(AOP(result),0,FALSE,FALSE);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       pic16_aopOp(right,ic,FALSE);
+       DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
 
+       /* 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 */
+               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-  /* if bitfield then unpack the bits */
-  if (IS_BITVAR(retype)) {
-    werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
-          "The programmer is obviously confused");
-    //genPackBits (retype,right,rname,POINTER);
-    exit(1);
-  }
-  else {
-    /* we have can just get the values */
-    int size = AOP_SIZE(right);
-    int offset = 0 ;    
+               pic16_loadFSR0( result );
+       }
+//     else
+//     rname = pic16_aopGet(AOP(result),0,FALSE,FALSE);
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    while (size--) {
-      l = pic16_aopGet(AOP(right),offset,FALSE,TRUE);
-      if (*l == '@' ) {
-       //MOVA(l);
-       //pic16_emitcode("mov","@%s,a",rname);
-       pic16_emitcode("movf","indf0,w ;1");
-      } else {
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-       if (AOP_TYPE(right) == AOP_LIT) {
-         unsigned int lit = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit);
-         if(lit) {
-           pic16_emitcode("movlw","%s",l);
-           pic16_emitcode("movwf","indf0 ;2");
-         } else 
-           pic16_emitcode("clrf","indf0");
-       }else {
-         pic16_emitcode("movf","%s,w",l);
-         pic16_emitcode("movwf","indf0 ;2");
-       }
-       //pic16_emitcode("mov","@%s,%s",rname,l);
-      }
-      if (size)
-       pic16_emitcode("incf","fsr0,f ;3");
-      //pic16_emitcode("inc","%s",rname);
-      offset++;
-    }
-  }
+       /* if bitfield then unpack the bits */
+       if (IS_BITFIELD(resetype)) {
+               genPackBits (resetype, right, NULL, POINTER);
+       } else {
+               /* we have can just get the values */
+         int size = AOP_SIZE(right);
+         int offset = 0 ;    
+
+               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+               while (size--) {
+                       l = pic16_aopGet(AOP(right),offset,FALSE,TRUE);
+                       if (*l == '@' ) {
+                               //MOVA(l);
+                               //pic16_emitcode("mov","@%s,a",rname);
+                               pic16_emitcode("movf","indf0,w ;1");
+                       } else {
+
+                               if (AOP_TYPE(right) == AOP_LIT) {
+                                 unsigned int lit = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit);
+
+                                       if(lit) {
+                                               pic16_emitcode("movlw","%s",l);
+                                               pic16_emitcode("movwf","indf0 ;2");
+                                       } else 
+                                               pic16_emitcode("clrf","indf0");
+                               } else {
+                                       pic16_emitcode("movf","%s,w",l);
+                                       pic16_emitcode("movwf","indf0 ;2");
+                               }
+                               //pic16_emitcode("mov","@%s,%s",rname,l);
+                       }
+                       if (size)
+                               pic16_emitcode("incf","fsr0,f ;3");
+                       //pic16_emitcode("inc","%s",rname);
+                       offset++;
+               }
+       }
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  /* now some housekeeping stuff */
-  if (aop) {
-    /* we had to allocate for this iCode */
-    pic16_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 */
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    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--)
-       pic16_emitcode("decf","fsr0,f");
-      //pic16_emitcode("dec","%s",rname);
-    }
-  }
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* now some housekeeping stuff */
+       if (aop) {
+               /* we had to allocate for this iCode */
+               pic16_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 */
+               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+               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--)
+                               pic16_emitcode("decf","fsr0,f");
+                       //pic16_emitcode("dec","%s",rname);
+               }
+       }
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  /* done */
- release:
-  pic16_freeAsmop(right,NULL,ic,TRUE);
-  pic16_freeAsmop(result,NULL,ic,TRUE);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* done */
+//release:
+       pic16_freeAsmop(right,NULL,ic,TRUE);
+       pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -8895,7 +9450,7 @@ static void genPagedPointerSet (operand *right,
     pic16_aopOp (right,ic,FALSE);
 
     /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
        genPackBits (retype,right,rname,PPOINTER);
     else {
        /* we have can just get the values */
@@ -8973,7 +9528,7 @@ static void genFarPointerSet (operand *right,
     pic16_aopOp(right,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genPackBits(retype,right,"dptr",FPOINTER);
     else {
         size = AOP_SIZE(right);
@@ -8997,105 +9552,108 @@ static void genFarPointerSet (operand *right,
 static void genGenPointerSet (operand *right,
                               operand *result, iCode *ic)
 {
-  int size, offset ;
-  sym_link *retype = getSpec(operandType(right));
-
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-
-  pic16_aopOp(result,ic,FALSE);
-  pic16_aopOp(right,ic,FALSE);
-  size = AOP_SIZE(right);
-
-  DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
-
-  /* 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) {
-      pic16_emitcode("mov","dptr,%s",pic16_aopGet(AOP(result),0,TRUE,FALSE));
-      pic16_emitcode("mov","b,%s + 1",pic16_aopGet(AOP(result),0,TRUE,FALSE));
-    }
-    else { /* we need to get it byte by byte */
-      //char *l = pic16_aopGet(AOP(result),0,FALSE,FALSE);
-      size = AOP_SIZE(right);
-      offset = 0 ;
-
-      /* hack hack! see if this the FSR. If so don't load W */
-      if(AOP_TYPE(right) != AOP_ACC) {
-
-
-       pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),0));
-       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
+       int size, offset, lit;
+       sym_link *retype = getSpec(operandType(right));
 
-       if(AOP_SIZE(result) > 1) {
-         pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_IRP_BIT));
-         pic16_emitpcode(POC_BTFSC,pic16_newpCodeOpBit(pic16_aopGet(AOP(result),1,FALSE,FALSE),0,0));
-         pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_IRP_BIT));
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-       }
+       pic16_aopOp(result,ic,FALSE);
+       pic16_aopOp(right,ic,FALSE);
+       size = AOP_SIZE(right);
+       offset = 0;
+
+       DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
+
+       /* 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 */
+               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+               // WARNING: anythig until "else" is untested!
+               if (AOP_TYPE(result) == AOP_IMMD) {
+                       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+                       lit = (unsigned)floatFromVal(AOP(result)->aopu.aop_lit);
+                       // load FSR0 from immediate
+                       pic16_emitpcode(POC_LFSR,pic16_popGetLit2(0,pic16_popGetLit(lit)));
+                       offset = 0;
+                       while(size--) {
+                               if(size) {
+                                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_postinc0)));
+                               } else {
+                                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_indf0)));
+                               }
+                               offset++;
+                       }
+                       goto release;
+               }
+               else { /* we need to get it byte by byte */
+                       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+                       //char *l = pic16_aopGet(AOP(result),0,FALSE,FALSE);
+
+                       // set up FSR0 with address of result
+                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),0), pic16_popCopyReg(&pic16_pc_fsr0l)));
+                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),1), pic16_popCopyReg(&pic16_pc_fsr0h)));
+
+                       /* hack hack! see if this the FSR. If so don't load W */
+                       if(AOP_TYPE(right) != AOP_ACC) {
+
+                               DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
+                               while(size--) {
+                                       if(size) {
+                                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_postinc0)));
+                                       } else {
+                                               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_indf0)));
+                                       }
+                                       offset++;
+                               }
+                               goto release;
+                       } 
+                       // right = ACC
+                       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+                       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
+                       goto release;
+       } // if (AOP_TYPE(result) != AOP_IMMD)
+
+       } // if (AOP_TYPE(result) != AOP_STR)
+       /* so dptr know contains the address */
+
+
+       /* if bit then unpack */
+       if (IS_BITFIELD(retype)) 
+               genPackBits(retype,right,"dptr",GPOINTER);
+       else {
+               size = AOP_SIZE(right);
+               offset = 0 ;
 
-       //if(size==2)
-       //pic16_emitpcode(POC_DECF,pic16_popCopyReg(&pic16_pc_fsr0));
-       //if(size==4) {
-       //  pic16_emitpcode(POC_MOVLW,pic16_popGetLit(0xfd));
-       //  pic16_emitpcode(POC_ADDWF,pic16_popCopyReg(&pic16_pc_fsr0));
-       //}
+               DEBUGpic16_emitcode ("; ***","%s  %d size=%d",__FUNCTION__,__LINE__,size);
 
-       while(size--) {
-         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(right),offset++));
-         pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
-         
-         if(size)
-           pic16_emitpcode(POC_INCF,pic16_popCopyReg(&pic16_pc_fsr0));
+               // set up FSR0 with address of result
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),0), pic16_popCopyReg(&pic16_pc_fsr0l)));
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),1), pic16_popCopyReg(&pic16_pc_fsr0h)));
+       
+               while (size--) {
+                       if (AOP_TYPE(right) == AOP_LIT) {
+                               pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),offset));
+                               if (size) {
+                                       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_postinc0));
+                               } else {
+                                       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
+                               }
+                       } else { // no literal
+                               if(size) {
+                                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_postinc0)));
+                               } else {
+                                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),offset), pic16_popCopyReg(&pic16_pc_indf0)));
+                               }
+                       }
+                       offset++;
+       }
        }
 
-
-       goto release;
-      } 
-
-      if(aopIdx(AOP(result),0) != 4) {
-
-       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
-       goto release;
-      }
-
-      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
-      goto release;
-
-    }
-  }
-  /* so dptr know contains the address */
-
-
-  /* if bit then unpack */
-  if (IS_BITVAR(retype)) 
-    genPackBits(retype,right,"dptr",GPOINTER);
-  else {
-    size = AOP_SIZE(right);
-    offset = 0 ;
-
-  DEBUGpic16_emitcode ("; ***","%s  %d size=%d",__FUNCTION__,__LINE__,size);
-
-    while (size--) {
-
-      pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),offset));
-      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
-
-      if (AOP_TYPE(right) == AOP_LIT) 
-       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),offset));
-      else
-       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset));
-
-      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_indf0));
-
-      offset++;
-    }
-  }
-
- release:
-  pic16_freeAsmop(right,NULL,ic,TRUE);
-  pic16_freeAsmop(result,NULL,ic,TRUE);
+       release:
+       pic16_freeAsmop(right,NULL,ic,TRUE);
+       pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9212,36 +9770,60 @@ static void genIfx (iCode *ic, iCode *popIc)
 /*-----------------------------------------------------------------*/
 static void genAddrOf (iCode *ic)
 {
-  operand *right, *result, *left;
-  int size, offset ;
-
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-
-
-  //pic16_aopOp(IC_RESULT(ic),ic,FALSE);
+  operand *result, *left;
+  int size;
+  symbol *sym; // = OP_SYMBOL(IC_LEFT(ic));
+  pCodeOp *pcop0, *pcop1, *pcop2;
 
-  pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE);
-  pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE);
-  pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-  DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
+       pic16_aopOp((left=IC_LEFT(ic)), ic, FALSE);
+       pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE);
 
-  size = AOP_SIZE(IC_RESULT(ic));
-  offset = 0;
+       sym = OP_SYMBOL( left );
 
+       size = AOP_SIZE(IC_RESULT(ic));
 
-  while (size--) {
-    pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(left),offset));
-    pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset));
-    offset++;
-  }
+       if(pic16_debug_verbose) {
+               fprintf(stderr, "%s:%d %s symbol %s , codespace=%d\n",
+                       __FILE__, __LINE__, __FUNCTION__, sym->name, IN_CODESPACE( SPEC_OCLS(sym->etype)));
+       }
+       
+       /* Assume that what we want the address of is in data space
+        * since there is no stack on the PIC, yet! -- VR */
+       /* low */
+       pcop0 = PCOP(pic16_newpCodeOpImmd(sym->rname, 0, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
 
+       /* high */
+       pcop1 = PCOP(pic16_newpCodeOpImmd(sym->rname, 1, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
+       
+       /* upper */
+       pcop2 = PCOP(pic16_newpCodeOpImmd(sym->rname, 2, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
+       
 
-  pic16_freeAsmop(left,NULL,ic,FALSE);
-  pic16_freeAsmop(result,NULL,ic,TRUE);
+       if (size == 3) {
+               pic16_emitpcode(POC_MOVLW, pcop0);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0));
+               pic16_emitpcode(POC_MOVLW, pcop1);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 1));
+               pic16_emitpcode(POC_MOVLW, pcop2);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 2));
+       } else
+       if (size == 2) {
+               pic16_emitpcode(POC_MOVLW, pcop0);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+               pic16_emitpcode(POC_MOVLW, pcop1);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1));
+       } else {
+               pic16_emitpcode(POC_MOVLW, pcop0);
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+       }
 
+       pic16_freeAsmop(result,NULL,ic,TRUE);
+       pic16_freeAsmop(left, NULL, ic, FALSE);
 }
 
+
 #if 0
 /*-----------------------------------------------------------------*/
 /* genFarFarAssign - assignment when both are in far space         */
@@ -9389,7 +9971,7 @@ static void genAssign (iCode *ic)
 
 #if 1
        /* This is a hack to turn MOVFW/MOVWF pairs to MOVFF command. It
-          normally should work, but mind that thw W register live range
+          normally should work, but mind that the W register live range
           is not checked, so if the code generator assumes that the W
           is already loaded after such a pair, wrong code will be generated.
           
@@ -9398,7 +9980,6 @@ static void genAssign (iCode *ic)
           USE WITH CARE. Revert to old code by setting 0 to the condition above.
           Vangelis Rokas 030603 (vrokas@otenet.gr) */
           
-       
        pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset));
 #else  
        /* This is the old code, which is assumed(?!) that works fine(!?) */
@@ -9595,141 +10176,174 @@ release:
 /*-----------------------------------------------------------------*/
 static void genCast (iCode *ic)
 {
-    operand *result = IC_RESULT(ic);
-    sym_link *ctype = operandType(IC_LEFT(ic));
-    sym_link *rtype = operandType(IC_RIGHT(ic));
-    operand *right = IC_RIGHT(ic);
-    int size, offset ;
-
-    DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* if they are equivalent then do nothing */
-    if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic)))
-        return ;
-
-    pic16_aopOp(right,ic,FALSE) ;
-    pic16_aopOp(result,ic,FALSE);
-
-    DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
-
-    /* if the result is a bit */
-    if (AOP_TYPE(result) == AOP_CRY) {
-        /* if the right size is a literal then
-        we know what the value is */
-      DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
-        if (AOP_TYPE(right) == AOP_LIT) {
-
-         pic16_emitpcode(  ( ((int) operandLitValue(right)) ? POC_BSF : POC_BCF),
-                     pic16_popGet(AOP(result),0));
+  operand *result = IC_RESULT(ic);
+  sym_link *ctype = operandType(IC_LEFT(ic));
+  sym_link *rtype = operandType(IC_RIGHT(ic));
+  operand *right = IC_RIGHT(ic);
+  int size, offset ;
 
-            if (((int) operandLitValue(right))) 
-             pic16_emitcode("bsf","(%s >> 3), (%s & 7)",
-                      AOP(result)->aopu.aop_dir,
-                      AOP(result)->aopu.aop_dir);
-            else
-             pic16_emitcode("bcf","(%s >> 3), (%s & 7)",
-                      AOP(result)->aopu.aop_dir,
-                      AOP(result)->aopu.aop_dir);
+       DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* if they are equivalent then do nothing */
+       if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic)))
+               return ;
 
-            goto release;
-        }
+       pic16_aopOp(right,ic,FALSE) ;
+       pic16_aopOp(result,ic,FALSE);
 
-        /* the right is also a bit variable */
-        if (AOP_TYPE(right) == AOP_CRY) {
+       DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
 
-         emitCLRC;
-         pic16_emitpcode(POC_BTFSC,  pic16_popGet(AOP(right),0));
+       /* if the result is a bit */
+       if (AOP_TYPE(result) == AOP_CRY) {
+       
+               /* if the right size is a literal then
+                * we know what the value is */
+               DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
+
+               if (AOP_TYPE(right) == AOP_LIT) {
+                       pic16_emitpcode(  ( ((int) operandLitValue(right)) ? POC_BSF : POC_BCF),
+                               pic16_popGet(AOP(result),0));
+
+                       if (((int) operandLitValue(right))) 
+                               pic16_emitcode("bsf","(%s >> 3), (%s & 7)",
+                                       AOP(result)->aopu.aop_dir,
+                                       AOP(result)->aopu.aop_dir);
+                       else
+                               pic16_emitcode("bcf","(%s >> 3), (%s & 7)",
+                                       AOP(result)->aopu.aop_dir,
+                                       AOP(result)->aopu.aop_dir);
+                       goto release;
+               }
 
-         pic16_emitcode("clrc","");
-         pic16_emitcode("btfsc","(%s >> 3), (%s & 7)",
-                  AOP(right)->aopu.aop_dir,
-                  AOP(right)->aopu.aop_dir);
-            pic16_aopPut(AOP(result),"c",0);
-            goto release ;
-        }
+               /* the right is also a bit variable */
+               if (AOP_TYPE(right) == AOP_CRY) {
+                       emitCLRC;
+                       pic16_emitpcode(POC_BTFSC,  pic16_popGet(AOP(right),0));
+
+                       pic16_emitcode("clrc","");
+                       pic16_emitcode("btfsc","(%s >> 3), (%s & 7)",
+                               AOP(right)->aopu.aop_dir,
+                               AOP(right)->aopu.aop_dir);
+                       pic16_aopPut(AOP(result),"c",0);
+                       goto release ;
+               }
 
-        /* we need to or */
-       if (AOP_TYPE(right) == AOP_REG) {
-         pic16_emitpcode(POC_BCF,    pic16_popGet(AOP(result),0));
-         pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0));
-         pic16_emitpcode(POC_BSF,    pic16_popGet(AOP(result),0));
+               /* we need to or */
+               if (AOP_TYPE(right) == AOP_REG) {
+                       pic16_emitpcode(POC_BCF,    pic16_popGet(AOP(result),0));
+                       pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0));
+                       pic16_emitpcode(POC_BSF,    pic16_popGet(AOP(result),0));
+               }
+               pic16_toBoolean(right);
+               pic16_aopPut(AOP(result),"a",0);
+               goto release ;
        }
-       pic16_toBoolean(right);
-       pic16_aopPut(AOP(result),"a",0);
-        goto release ;
-    }
-
-    if ((AOP_TYPE(right) == AOP_CRY) && (AOP_TYPE(result) == AOP_REG)) {
-      int offset = 1;
-      size = AOP_SIZE(result);
-
-      DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
-
-      pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),0));
-      pic16_emitpcode(POC_BTFSC,  pic16_popGet(AOP(right),0));
-      pic16_emitpcode(POC_INCF,   pic16_popGet(AOP(result),0));
 
-      while (size--)
-       pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),offset++));
+       if ((AOP_TYPE(right) == AOP_CRY) && (AOP_TYPE(result) == AOP_REG)) {
+         int offset = 1;
 
-      goto release;
-    }
+               size = AOP_SIZE(result);
 
-    /* if they are the same size : or less */
-    if (AOP_SIZE(result) <= AOP_SIZE(right)) {
+               DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-        /* if they are in the same place */
-      if (pic16_sameRegs(AOP(right),AOP(result)))
-       goto release;
+               pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),0));
+               pic16_emitpcode(POC_BTFSC,  pic16_popGet(AOP(right),0));
+               pic16_emitpcode(POC_INCF,   pic16_popGet(AOP(result),0));
 
-      DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
-      if (IS_PTR_CONST(rtype))
-       DEBUGpic16_emitcode ("; ***","%d - right is const pointer",__LINE__);
-      if (IS_PTR_CONST(operandType(IC_RESULT(ic))))
-       DEBUGpic16_emitcode ("; ***","%d - result is const pointer",__LINE__);
+               while (size--)
+                       pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),offset++));
 
-      if ((AOP_TYPE(right) == AOP_PCODE) && AOP(right)->aopu.pcop->type == PO_IMMEDIATE) {
-       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),0));
-       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
-       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),1));
-       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1));
-        if(AOP_SIZE(result) <2)
-         fprintf(stderr,"%d -- result is not big enough to hold a ptr\n",__LINE__);
+               goto release;
+       }
 
-      } else {
+       /* if they are the same size : or less */
+       if (AOP_SIZE(result) <= AOP_SIZE(right)) {
 
-        /* if they in different places then copy */
-        size = AOP_SIZE(result);
-        offset = 0 ;
-        while (size--) {
-         pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset));
-         pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset));
+               /* if they are in the same place */
+               if (pic16_sameRegs(AOP(right),AOP(result)))
+                       goto release;
 
-         //pic16_aopPut(AOP(result),
-         // pic16_aopGet(AOP(right),offset,FALSE,FALSE),
-         // offset);
+               DEBUGpic16_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
+#if 0
+               if (IS_PTR_CONST(rtype))
+#else
+               if (IS_CODEPTR(rtype))
+#endif
+                       DEBUGpic16_emitcode ("; ***","%d - right is const pointer",__LINE__);
 
-         offset++;
-        }
-      }
-      goto release;
-    }
+#if 0
+               if (IS_PTR_CONST(operandType(IC_RESULT(ic))))
+#else
+               if (IS_CODEPTR(operandType(IC_RESULT(ic))))
+#endif
+                       DEBUGpic16_emitcode ("; ***","%d - result is const pointer",__LINE__);
 
+#if 0
+               if(AOP_TYPE(right) == AOP_IMMD) {
+                 pCodeOp *pcop0, *pcop1, *pcop2;
+                 symbol *sym = OP_SYMBOL( right );
+
+                       size = AOP_SIZE(result);
+                       /* low */
+                       pcop0 = PCOP(pic16_newpCodeOpImmd(sym->rname, 0, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
+                       /* high */
+                       pcop1 = PCOP(pic16_newpCodeOpImmd(sym->rname, 1, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
+                       /* upper */
+                       pcop2 = PCOP(pic16_newpCodeOpImmd(sym->rname, 2, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
+       
+                       if (size == 3) {
+                               pic16_emitpcode(POC_MOVLW, pcop0);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0));
+                               pic16_emitpcode(POC_MOVLW, pcop1);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 1));
+                               pic16_emitpcode(POC_MOVLW, pcop2);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 2));
+                       } else
+                       if (size == 2) {
+                               pic16_emitpcode(POC_MOVLW, pcop0);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+                               pic16_emitpcode(POC_MOVLW, pcop1);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1));
+                       } else {
+                               pic16_emitpcode(POC_MOVLW, pcop0);
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+                       }
+               } else
+#endif
+               if ((AOP_TYPE(right) == AOP_PCODE) && AOP(right)->aopu.pcop->type == PO_IMMEDIATE) {
+                       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),0));
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0));
+                       pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),1));
+                       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1));
+                       if(AOP_SIZE(result) <2)
+                               fprintf(stderr,"%d -- result is not big enough to hold a ptr\n",__LINE__);
+               } else {
+                       /* if they in different places then copy */
+                       size = AOP_SIZE(result);
+                       offset = 0 ;
+                       while (size--) {
+                               pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset));
+                               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset));
+                               offset++;
+                       }
+               }
+               goto release;
+       }
 
-    /* if the result is of type pointer */
-    if (IS_PTR(ctype)) {
+       /* if the result is of type pointer */
+       if (IS_PTR(ctype)) {
+         int p_type;
+         sym_link *type = operandType(right);
+         sym_link *etype = getSpec(type);
 
-       int p_type;
-       sym_link *type = operandType(right);
-       sym_link *etype = getSpec(type);
-      DEBUGpic16_emitcode("; ***","%s  %d - pointer cast",__FUNCTION__,__LINE__);
+               DEBUGpic16_emitcode("; ***","%s  %d - pointer cast",__FUNCTION__,__LINE__);
 
-       /* pointer to generic pointer */
-       if (IS_GENPTR(ctype)) {
-           char *l = zero;
+               /* pointer to generic pointer */
+               if (IS_GENPTR(ctype)) {
+                 char *l = zero;
            
-           if (IS_PTR(type)) 
-               p_type = DCL_TYPE(type);
-           else {
+                       if (IS_PTR(type)) 
+                               p_type = DCL_TYPE(type);
+                       else {
                /* we have to go by the storage class */
                p_type = PTR_TYPE(SPEC_OCLS(etype));
 
@@ -9837,7 +10451,7 @@ static void genCast (iCode *ic)
       while (size--)
        pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),offset++));
     } else {
-      /* we need to extend the sign :{ */
+      /* we need to extend the sign :( */
 
       if(size == 1) {
        /* Save one instruction of casting char to int */
@@ -9932,35 +10546,40 @@ static int genDjnz (iCode *ic, iCode *ifx)
 /*-----------------------------------------------------------------*/
 static void genReceive (iCode *ic)
 {    
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-  if (isOperandInFarSpace(IC_RESULT(ic)) && 
-      ( OP_SYMBOL(IC_RESULT(ic))->isspilt ||
-       IS_TRUE_SYMOP(IC_RESULT(ic))) ) {
+       if (isOperandInFarSpace(IC_RESULT(ic)) &&
+               ( OP_SYMBOL(IC_RESULT(ic))->isspilt ||
+               IS_TRUE_SYMOP(IC_RESULT(ic))) ) {
 
-    int size = getSize(operandType(IC_RESULT(ic)));
-    int offset =  pic16_fReturnSizePic - size;
-    while (size--) {
-      pic16_emitcode ("push","%s", (strcmp(fReturn[pic16_fReturnSizePic - offset - 1],"a") ?
-                                   fReturn[pic16_fReturnSizePic - offset - 1] : "acc"));
-      offset++;
-    }
-    pic16_aopOp(IC_RESULT(ic),ic,FALSE);  
-    size = AOP_SIZE(IC_RESULT(ic));
-    offset = 0;
-    while (size--) {
-      pic16_emitcode ("pop","acc");
-      pic16_aopPut (AOP(IC_RESULT(ic)),"a",offset++);
-    }
-       
-  } else {
-    _G.accInUse++;
-    pic16_aopOp(IC_RESULT(ic),ic,FALSE);  
-    _G.accInUse--;
-    assignResultValue(IC_RESULT(ic));  
-  }
+         int size = getSize(operandType(IC_RESULT(ic)));
+         int offset =  pic16_fReturnSizePic - size;
 
-  pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
+               while (size--) {
+                       pic16_emitcode ("push","%s", (strcmp(fReturn[pic16_fReturnSizePic - offset - 1],"a") ?
+                                       fReturn[pic16_fReturnSizePic - offset - 1] : "acc"));
+                       offset++;
+               }
+
+               DEBUGpic16_emitcode ("; ***","1 %s  %d",__FUNCTION__,__LINE__);
+
+               pic16_aopOp(IC_RESULT(ic),ic,FALSE);  
+               size = AOP_SIZE(IC_RESULT(ic));
+               offset = 0;
+               while (size--) {
+                       pic16_emitcode ("pop","acc");
+                       pic16_aopPut (AOP(IC_RESULT(ic)),"a",offset++);
+               }
+       } else {
+               DEBUGpic16_emitcode ("; ***","2 %s  %d",__FUNCTION__,__LINE__);
+
+               _G.accInUse++;
+               pic16_aopOp(IC_RESULT(ic),ic,FALSE);  
+               _G.accInUse--;
+               assignResultValue(IC_RESULT(ic), 0);
+       }
+
+       pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -9972,7 +10591,7 @@ genDummyRead (iCode * ic)
   pic16_emitcode ("; genDummyRead","");
   pic16_emitcode ("; not implemented","");
 
-  ic;
+  ic = ic;
 }
 
 /*-----------------------------------------------------------------*/
@@ -10015,12 +10634,8 @@ void genpic16Code (iCode *lic)
     }
 #endif
 
-//    dumpiCode(lic);
-
     for (ic = lic ; ic ; ic = ic->next ) {
 
-//      fprintf(stderr, "; VR = %c %x\n", ic->op, ic->op);
-//      DEBUGpic16_emitcode("; VR", "");
       DEBUGpic16_emitcode(";ic ", "\t%c 0x%x",ic->op, ic->op);
        if ( cln != ic->lineno ) {
            if ( options.debug ) {
@@ -10030,18 +10645,21 @@ void genpic16Code (iCode *lic)
                         ic->level,ic->block);
                _G.debugLine = 0;
            }
-           /*
-             pic16_emitcode("#CSRC","%s %d",FileBaseName(ic->filename),ic->lineno);
-             pic16_emitcode ("", ";\t%s:%d: %s", ic->filename, ic->lineno, 
-             printCLine(ic->filename, ic->lineno));
-           */
-           pic16_addpCode2pBlock(pb,
-                           pic16_newpCodeCSource(ic->lineno, 
-                                           ic->filename, 
-                                           printCLine(ic->filename, ic->lineno)));
+           
+           if(!options.noCcodeInAsm) {
+               pic16_addpCode2pBlock(pb,
+                       pic16_newpCodeCSource(ic->lineno, ic->filename, 
+                               printCLine(ic->filename, ic->lineno)));
+           }
 
            cln = ic->lineno ;
        }
+       
+       if(options.iCodeInAsm) {
+               /* insert here code to print iCode as comment */
+               pic16_emitpcomment("ic:%d: %s", ic->seq, printILine(ic));
+       }
+       
        /* if the result is marked as
           spilt and rematerializable or code for
           this has already been generated then
@@ -10052,11 +10670,11 @@ void genpic16Code (iCode *lic)
        /* depending on the operation */
        switch (ic->op) {
        case '!' :
-           genNot(ic);
+           pic16_genNot(ic);
            break;
            
        case '~' :
-           genCpl(ic);
+           pic16_genCpl(ic);
            break;
            
        case UNARYMINUS: