* src/pic16/gen.c (pic16_popCopyReg): made copying work for extended
[fw/sdcc] / src / pic16 / gen.c
index fed12d129bf8bcf7a811ad40103805edbe973277..c440625b188675eda49cb7f1e22664e784dad505 100644 (file)
@@ -1,4 +1,4 @@
-/*-------------------------------------------------------------------------
+ /*-------------------------------------------------------------------------
  gen.c - source file for code generation for pic16
 
   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
@@ -6,7 +6,7 @@
   Bug Fixes  -  Wojciech Stryjewski  wstryj1@tiger.lsu.edu (1999 v2.1.9a)
   PIC port   -  Scott Dattalo scott@dattalo.com (2000)
   PIC16 port -  Martin Dubuc m.dubuc@rogers.com (2002)
-             -  Vangelis Rokas vrokas@otenet.gr (2003,2004)
+             -  Vangelis Rokas vrokas@otenet.gr (2003,2004,2005)
   
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
 #include "device.h"
 #include "main.h"
 
+/* Set the following to 1 to enable the slower/bigger
+ * but more robust generic shifting routine (which also
+ * operates correctly with negative shift values). */
+#define USE_GENERIC_SIGNED_SHIFT 1
+
+/* Set the following to 1 to enable the new
+ * stripped down genCmp version.
+ * This version should be easier to understand,
+ * more reliable and (sigh) slighly slower. */
+#define USE_SIMPLE_GENCMP 1
+
 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 *);
@@ -57,7 +68,6 @@ extern void pic16_printpBlock(FILE *of, pBlock *pb);
 static asmop *newAsmop (short type);
 static pCodeOp *pic16_popRegFromString(char *str, int size, int offset, operand *op);
 extern pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...);
-static void mov2w (asmop *aop, int offset);
 static void mov2f(asmop *dst, asmop *src, int offset);
 static void mov2fp(pCodeOp *dst, asmop *src, int offset);
 static pCodeOp *pic16_popRegFromIdx(int rIdx);
@@ -66,7 +76,9 @@ static pCodeOp *pic16_popRegFromIdx(int rIdx);
 
 int pic16_labelOffset=0;
 extern int pic16_debug_verbose;
+#if !(USE_GENERIC_SIGNED_SHIFT)
 static int optimized_for_speed = 0;
+#endif
 /*
   hack hack
 
@@ -123,6 +135,7 @@ static char *one  = "#0x01";
  
 
 char *fReturnpic16[] = {"WREG", "PRODL", "PRODH", "FSR0L" };
+int fReturnIdx[] = {IDX_WREG, IDX_PRODL, IDX_PRODH, IDX_FSR0L };
 unsigned pic16_fReturnSizePic = 4; /* shared with ralloc.c */
 static char **fReturn = fReturnpic16;
 
@@ -141,24 +154,15 @@ static struct {
     short ipushRegs;
     set *sendSet;
     set *stackRegSet;
-    int interruptvector;
     int usefastretfie;
-    bitVect *fregsUsed;
+    bitVect *fregsUsed;                        /* registers used in function */
+    bitVect *sregsAlloc;
+    set *sregsAllocSet;                        /* registers used to store stack variables */
     int stack_lat;                     /* stack offset latency */
     int resDirect;
     int useWreg;                       /* flag when WREG is used to pass function parameter */
 } _G;
 
-/* Resolved ifx structure. This structure stores information
-   about an iCode ifx that makes it easier to generate code.
-*/
-typedef struct resolvedIfx {
-  symbol *lbl;     /* pointer to a label */
-  int condition;   /* true or false ifx */
-  int generated;   /* set true when the code associated with the ifx
-                   * is generated */
-} resolvedIfx;
-
 extern int pic16_ptrRegReq ;
 extern int pic16_nRegs;
 extern FILE *codeOutFile;
@@ -636,11 +640,29 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result)
     }
 #endif
 
+
+#if 0
+    if(sym->iaccess) {
+      if(space->paged) {
+        fprintf(stderr, "%s:%d symbol %s points to paged data\n", __FILE__, __LINE__, sym->name);
+
+        sym->aop = aop = newAsmop (AOP_PAGED);
+        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;
+      }
+      assert( 0 );
+    }
+#endif
+    
 #if 1
     /* assign depending on the storage class */
     /* if it is on the stack or indirectly addressable */
     /* space we need to assign either r0 or r1 to it   */    
-    if (sym->onStack || sym->iaccess) {
+    if (sym->onStack)  // || sym->iaccess)
+    {
       pCodeOp *pcop[4];
       int i;
       
@@ -654,26 +676,35 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result)
         aop->size = getSize(sym->type);
 
 
-        DEBUGpic16_emitcode("; +++", "%s:%d", __FILE__, __LINE__);
-        pic16_DumpAop("aopForSym", AOP( IC_RESULT(ic) ));
-        if((ic->op == '=') && IC_RESULT(ic) && AOP( IC_RESULT(ic) )
-          && (AOP_TYPE(IC_RESULT(ic)) == AOP_REG) ) {
+        DEBUGpic16_emitcode("; +++ ", "%s:%d\top = %s", __FILE__, __LINE__, pic16_decodeOp(ic->op));
+        if((ic->op == '=' /*|| ic->op == CAST*/) && IC_RESULT(ic) && AOP( IC_RESULT(ic) )
+          && (AOP_TYPE(IC_RESULT(ic)) == AOP_REG)) {
+//          pic16_DumpAop("aopForSym", AOP( IC_RESULT(ic) ));
           
           for(i=0;i<aop->size;i++)
             aop->aopu.stk.pop[i] = pcop[i] = pic16_popRegFromIdx( AOP(IC_RESULT(ic))->aopu.aop_reg[i]->rIdx);
             _G.resDirect = 1;  /* notify that result will be loaded directly from aopForSym */
         } else
+        if(1 && ic->op == SEND) {
+
+          /* if SEND do the send here */
+          _G.resDirect = 1;
+        } else {
           for(i=0;i<aop->size;i++) {
-            aop->aopu.stk.pop[i] = pcop[i] = pic16_popGetTempRegCond( _G.fregsUsed, 0 );
-            _G.fregsUsed = bitVectSetBit(_G.fregsUsed, PCOR(pcop[i])->r->rIdx);
+            aop->aopu.stk.pop[i] = pcop[i] = pic16_popGetTempRegCond(_G.fregsUsed, _G.sregsAlloc, 0 );
+            _G.sregsAlloc = bitVectSetBit(_G.sregsAlloc, PCOR(pcop[i])->r->rIdx);
           }
+        }
 
 
 //        fprintf(stderr, "%s:%d\t%s\tsym size %d\n", __FILE__, __LINE__, __FUNCTION__, aop->size);
 
 #if 1
        DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d stack = %d",__LINE__,sym->rname,aop->size, sym->stack);
-       
+
+       // we do not need to load the value if it is to be defined...
+       if (result) return aop;
+
        if(_G.accInUse) {
                pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
        }
@@ -681,11 +712,28 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result)
        for(i=0;i<aop->size;i++) {
 
          /* initialise for stack access via frame pointer */
-         pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack + i + _G.stack_lat));
-
-         pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
-                         pic16_popCopyReg(&pic16_pc_plusw2), pcop[i]));
-        }
+          // operands on stack are accessible via "FSR2 + index" with index
+          // starting at 2 for arguments and growing from 0 downwards for
+          // local variables (index == 0 is not assigned so we add one here)
+         {
+           int soffs = sym->stack;
+           if (soffs <= 0) {
+             assert (soffs < 0);
+             soffs++;
+           } // if
+
+            if(1 && ic->op == SEND) {
+              pic16_emitpcode(POC_MOVLW, pic16_popGetLit(soffs + aop->size - i - 1 /*+ _G.stack_lat*/));
+              pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                    pic16_popCopyReg( pic16_frame_plusw ),
+                    pic16_popCopyReg(pic16_stack_postdec )));
+            } else {
+              pic16_emitpcode(POC_MOVLW, pic16_popGetLit(soffs + i /*+ _G.stack_lat*/));
+              pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                 pic16_popCopyReg( pic16_frame_plusw ), pcop[i]));
+            }
+         }
+       }
        
        if(_G.accInUse) {
                pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
@@ -759,7 +807,7 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result)
     }
 #endif
 
-#if 0
+#if 1
     /* special case for a function */
     if (IS_FUNC(sym->type)) {   
         sym->aop = aop = newAsmop(AOP_PCODE);
@@ -831,11 +879,15 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result)
                aop->size = PTRSIZE;
        else if(IN_CODESPACE( space ) || IN_FARSPACE( space ))
                aop->size = FPTRSIZE;
-       else if(IC_LEFT(ic)) aop->size = AOP_SIZE( IC_LEFT(ic) );
-       else if(IC_RIGHT(ic)) aop->size = AOP_SIZE( IC_RIGHT(ic) );
+       else if(IC_LEFT(ic) && AOP(IC_LEFT(ic))) aop->size = AOP_SIZE( IC_LEFT(ic) );
+       else if(IC_RIGHT(ic) && AOP(IC_RIGHT(ic))) aop->size = AOP_SIZE( IC_RIGHT(ic) );
        else if(sym->onStack) {
                aop->size = PTRSIZE;
         } else {
+          if(SPEC_SCLS(sym->etype) == S_PDATA) {
+            fprintf(stderr, "%s: %d symbol in PDATA space\n", __FILE__, __LINE__);
+            aop->size = FPTRSIZE;
+          } else
                assert( 0 );
        }
 
@@ -1048,10 +1100,12 @@ bool pic16_sameRegs (asmop *aop1, asmop *aop2 )
         aop2->type != AOP_REG )
         return FALSE ;
 
+    /* This is a bit too restrictive if one is a subset of the other...
     if (aop1->size != aop2->size )
         return FALSE ;
+    */
 
-    for (i = 0 ; i < aop1->size ; i++ ) {
+    for (i = 0 ; i < min(aop1->size, aop2->size) ; i++ ) {
 //        if(aop1->aopu.aop_reg[i]->type != aop2->aopu.aop_reg[i]->type)return FALSE;
 
 //        if(aop1->aopu.aop_reg[i]->type == AOP_REG)
@@ -1149,6 +1203,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
     b) has a spill location */
     if (sym->isspilt || sym->nRegs == 0) {
 
+//      debugf3("symbol %s\tisspilt: %d\tnRegs: %d\n", sym->rname, sym->isspilt, sym->nRegs);
       DEBUGpic16_emitcode(";","%d",__LINE__);
         /* rematerialize it NOW */
         if (sym->remat) {
@@ -1175,7 +1230,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
 #endif
 
 #if 1
-        if (sym->ruonly ) {
+        if (sym->ruonly) {
          /*
          sym->aop = op->aop = aop = newAsmop(AOP_PCODE);
          aop->aopu.pcop = pic16_popGetImmd(sym->usl.spillLoc->rname,0,sym->usl.spillLoc->offset);
@@ -1185,10 +1240,10 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
 
          unsigned i;
 
-         aop = op->aop = sym->aop = newAsmop(AOP_STR);
+         aop = op->aop = sym->aop = newAsmop(AOP_REG);
          aop->size = getSize(sym->type);
          for ( i = 0 ; i < pic16_fReturnSizePic ; i++ )
-           aop->aopu.aop_str[i] = fReturn[i];
+           aop->aopu.aop_reg[i] = PCOR(pic16_popRegFromIdx( fReturnIdx[i] ))->r;
 
          DEBUGpic16_emitcode(";","%d",__LINE__);
          return;
@@ -1209,9 +1264,16 @@ 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 = pic16_popRegFromString(sym->usl.spillLoc->rname, 
-                                         getSize(sym->type), 
-                                         sym->usl.spillLoc->offset, op);
+       if (sym->usl.spillLoc && sym->usl.spillLoc->rname) {
+         aop->aopu.pcop = pic16_popRegFromString(sym->usl.spillLoc->rname, 
+                                                 getSize(sym->type), 
+                                                 sym->usl.spillLoc->offset, op);
+       } else {
+         fprintf (stderr, "%s:%d called for a spillLocation -- assigning WREG instead --- CHECK!\n", __FUNCTION__, __LINE__);
+         DEBUGpic16_emitcode (";","%s:%d called for a spillLocation -- assigning WREG instead --- CHECK", __FUNCTION__, __LINE__);
+         assert (getSize(sym->type) <= 1);
+         aop->aopu.pcop = pic16_popCopyReg (&pic16_pc_wreg);
+       }
         aop->size = getSize(sym->type);
 
         return;
@@ -1301,21 +1363,33 @@ void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop)
 
               /* we must store the result on stack */
               if((op == IC_RESULT(ic)) && RESULTONSTA(ic)) {
+                // operands on stack are accessible via "FSR2 + index" with index
+                // starting at 2 for arguments and growing from 0 downwards for
+                // local variables (index == 0 is not assigned so we add one here)
+                int soffs = OP_SYMBOL(IC_RESULT(ic))->stack;
+                if (soffs <= 0) {
+                  assert (soffs < 0);
+                  soffs++;
+                } // if
                 if(_G.accInUse)pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
                 for(i=0;i<aop->size;i++) {
-                  /* initialise for stack access via frame pointer */
-                  pic16_emitpcode(POC_MOVLW, pic16_popGetLit(OP_SYMBOL(IC_RESULT(ic))->stack + i + _G.stack_lat));
-
-                  pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
-                        aop->aopu.stk.pop[i], pic16_popCopyReg(&pic16_pc_plusw2)));
-                }
+                 /* initialise for stack access via frame pointer */
+                 pic16_emitpcode(POC_MOVLW, pic16_popGetLit(soffs + i /*+ _G.stack_lat*/));
+                 pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+                        aop->aopu.stk.pop[i], pic16_popCopyReg(pic16_frame_plusw)));
+               }
        
                 if(_G.accInUse)pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
               }
 
               if(!_G.resDirect) {
-                for(i=0;i<aop->size;i++)
+                for(i=0;i<aop->size;i++) {
                   PCOR(aop->aopu.stk.pop[i] )->r->isFree = 1;
+
+                  if(bitVectBitValue(_G.sregsAlloc, PCOR(aop->aopu.stk.pop[i])->r->rIdx))
+                      bitVectUnSetBit(_G.sregsAlloc, PCOR(aop->aopu.stk.pop[i])->r->rIdx);
+                }
+                  
               }
               _G.resDirect = 0;
           }
@@ -1481,16 +1555,21 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
        return (rs);
 
     case AOP_LIT:
-       sprintf(s,"0x%02x", pic16aopLiteral (aop->aopu.aop_lit,offset));
+       sprintf(s,"0X%02x", pic16aopLiteral (aop->aopu.aop_lit,offset));
        rs = Safe_calloc(1,strlen(s)+1);
        strcpy(rs,s);   
        return rs;
        
     case AOP_STR:
        aop->coff = offset ;
-       if (strcmp(aop->aopu.aop_str[offset],"a") == 0 &&
-           dname)
-           return "acc";
+
+//     if (strcmp(aop->aopu.aop_str[offset],"a") == 0 &&
+//         dname)
+//         return "acc";
+        if(!strcmp(aop->aopu.aop_str[offset], "WREG")) {
+          aop->type = AOP_ACC;
+          return Safe_strdup("WREG");
+        }
         DEBUGpic16_emitcode(";","%d - %s",__LINE__, aop->aopu.aop_str[offset]);
        
        return aop->aopu.aop_str[offset];
@@ -1502,7 +1581,11 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
        if(pcop->name) {
          DEBUGpic16_emitcode(";","%s offset %d",pcop->name,PCOI(pcop)->offset);
          //sprintf(s,"(%s+0x%02x)", pcop->name,PCOI(aop->aopu.pcop)->offset);
-         sprintf(s,"%s", pcop->name);
+         if (offset) {
+           sprintf(s,"(%s + %d)", pcop->name, offset);
+         } else {
+           sprintf(s,"%s", pcop->name);
+         }
        } else
          sprintf(s,"0x%02x", PCOI(aop->aopu.pcop)->offset);
 
@@ -1511,6 +1594,21 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
       strcpy(rs,s);   
       return rs;
 
+#if 0
+    case AOP_PAGED:
+      DEBUGpic16_emitcode(";","oops AOP_PAGED did this %s\n",s);
+      if (offset) {
+       sprintf(s,"(%s + %d)",
+               aop->aopu.aop_dir,
+               offset);
+      } else
+           sprintf(s,"%s",aop->aopu.aop_dir);
+      DEBUGpic16_emitcode(";","oops AOP_PAGED did this %s\n",s);
+      rs = Safe_calloc(1,strlen(s)+1);
+      strcpy(rs,s);   
+      return rs;
+#endif
+
     case AOP_STA:
         rs = Safe_strdup(PCOR(aop->aopu.stk.pop[offset])->r->name);
         return (rs);
@@ -1575,13 +1673,15 @@ pCodeOp *pic16_popGetTempReg(int lock)
 }
 
 /*-----------------------------------------------------------------*/
-/* pic16_popGetTempRegCond - create a new temporary pCodeOp, but   */
-/*                            don't save if inside v               */
+/* pic16_popGetTempRegCond - create a new temporary pCodeOp which  */
+/*                           is not part of f, but don't save if   */
+/*                           inside v                              */
 /*-----------------------------------------------------------------*/
-pCodeOp *pic16_popGetTempRegCond(bitVect *v, int lock)
+pCodeOp *pic16_popGetTempRegCond(bitVect *f, bitVect *v, int lock)
 {
-  pCodeOp *pcop;
+  pCodeOp *pcop=NULL;
   symbol *cfunc;
+  int i;
 
 //    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
@@ -1594,15 +1694,58 @@ pCodeOp *pic16_popGetTempRegCond(bitVect *v, int lock)
     cfunc = currFunc;
     currFunc = NULL;
 
-    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;
+    i = bitVectFirstBit(f);
+    while(i < 128) {
 
-      if(!bitVectBitValue(v, PCOR(pcop)->r->rIdx)) {
-      /* push value on stack */
-        pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) );
+      /* bypass registers that are used by function */
+      if(!bitVectBitValue(f, i)) {
+      
+        /* bypass registers that are already allocated for stack access */
+        if(!bitVectBitValue(v, i))  {
+        
+//          debugf("getting register rIdx = %d\n", i);
+          /* ok, get the operand */
+          pcop = pic16_newpCodeOpReg( i );
+    
+          /* should never by NULL */
+          assert( pcop != NULL );
+
+          
+          /* sanity check */
+          if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
+            int found=0;
+            
+              PCOR(pcop)->r->wasUsed=1;
+              PCOR(pcop)->r->isFree=0;
+
+
+              {
+                regs *sr;
+              
+                  for(sr=setFirstItem(_G.sregsAllocSet);sr;sr=setNextItem(_G.sregsAllocSet)) {
+
+                    if(sr->rIdx == PCOR(pcop)->r->rIdx) {
+                      /* already used in previous steps, break */
+                      found=1;          
+                      break;
+                    }
+                  }
+              }
+
+              /* caller takes care of the following */
+//              bitVectSetBit(v, i);
+
+              if(!found) {
+                /* push value on stack */
+                pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) );
+                addSet(&_G.sregsAllocSet, PCOR(pcop)->r);
+              }
+          
+            break;
+          }
+        }
       }
+      i++;
     }
 
     currFunc = cfunc;
@@ -1622,6 +1765,7 @@ void pic16_popReleaseTempReg(pCodeOp *pcop, int lock)
 
   if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
     PCOR(pcop)->r->isFree = 1;
+
     pic16_poppCodeOp( pic16_pCodeOpCopy(pcop) );
   }
 }
@@ -1647,16 +1791,20 @@ pCodeOp *pic16_popCopyReg(pCodeOpReg *pc)
   pCodeOpReg *pcor;
 
   pcor = Safe_calloc(1,sizeof(pCodeOpReg) );
-  pcor->pcop.type = pc->pcop.type;
+  memcpy (pcor, pc, sizeof (pCodeOpReg));
+  pcor->r->wasUsed = 1;
+  
+  //pcor->pcop.type = pc->pcop.type;
   if(pc->pcop.name) {
     if(!(pcor->pcop.name = Safe_strdup(pc->pcop.name)))
       fprintf(stderr,"oops %s %d",__FILE__,__LINE__);
   } else
     pcor->pcop.name = NULL;
 
-  pcor->r = pc->r;
-  pcor->rIdx = pc->rIdx;
-  pcor->r->wasUsed=1;
+  //pcor->r = pc->r;
+  //pcor->rIdx = pc->rIdx;
+  //pcor->r->wasUsed=1;
+  //pcor->instance = pc->instance;
 
 //  DEBUGpic16_emitcode ("; ***","%s  , copying %s, rIdx=%d",__FUNCTION__,pc->pcop.name,pc->rIdx);
 
@@ -1908,9 +2056,17 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
       DEBUGpic16_emitcode(";","%d\tAOP_DIR", __LINE__);
       return pic16_popRegFromString(aop->aopu.aop_dir, aop->size, offset, NULL);
        
+#if 0
+    case AOP_PAGED:
+      DEBUGpic16_emitcode(";","%d\tAOP_DIR", __LINE__);
+      return pic16_popRegFromString(aop->aopu.aop_dir, aop->size, offset, NULL);
+#endif
+
     case AOP_REG:
       {
-       int rIdx = aop->aopu.aop_reg[offset]->rIdx;
+       int rIdx;
+       assert (aop && aop->aopu.aop_reg[offset] != NULL);
+       rIdx = aop->aopu.aop_reg[offset]->rIdx;
 
        DEBUGpic16_emitcode(";","%d\tAOP_REG", __LINE__);
        
@@ -1924,9 +2080,9 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        PCOR(pcop)->instance = offset;
        pcop->type = PCOR(pcop)->r->pc_type;
        
-       DEBUGpic16_emitcode(";*+*", "%d\tAOP_REG type = %s\n", __LINE__, dumpPicOptype(pcop->type));
+       DEBUGpic16_emitcode(";*+*", "%d\tAOP_REG type = %s", __LINE__, dumpPicOptype(pcop->type));
        rs = aop->aopu.aop_reg[offset]->name;
-       DEBUGpic16_emitcode(";","%d regiser idx = %d name = %s",__LINE__,rIdx,rs);
+       DEBUGpic16_emitcode(";","%d register idx = %d name = %s",__LINE__,rIdx,rs);
        return pcop;
       }
 
@@ -1963,16 +2119,16 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
                          __LINE__, 
                          ((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; // patch 8
-               case PO_IMMEDIATE: PCOI(pcop)->offset = offset; break;
-               default:
-                       assert( 0 );    /* should never reach here */;
-       }
-#else
-      PCOI(pcop)->offset = offset;
-#endif
+      switch( aop->aopu.pcop->type ) {
+        case PO_DIR: PCOR(pcop)->instance += offset; break;
+        case PO_IMMEDIATE: PCOI(pcop)->offset = offset; break;
+        case PO_WREG:
+            assert (offset==0);
+            break;
+        default:
+         fprintf (stderr, "%s: unhandled aop->aopu.pcop->type %d\n", __FUNCTION__, aop->aopu.pcop->type);
+          assert( 0 ); /* should never reach here */;
+      }
       return pcop;
     }
 
@@ -2215,9 +2371,9 @@ void pic16_aopPut (asmop *aop, char *s, int offset)
 }
 
 /*-----------------------------------------------------------------*/
-/* mov2w - generate either a MOVLW or MOVFW based operand type     */
+/* pic16_mov2w - generate either a MOVLW or MOVFW based operand type     */
 /*-----------------------------------------------------------------*/
-static void mov2w (asmop *aop, int offset)
+void pic16_mov2w (asmop *aop, int offset)
 {
   DEBUGpic16_emitcode ("; ***","%s  %d  offset=%d",__FUNCTION__,__LINE__,offset);
 
@@ -2251,7 +2407,7 @@ static void mov2fp(pCodeOp *dst, asmop *src, int offset)
 
 void pic16_testStackOverflow(void)
 {
-#define        GSTACK_TEST_NAME        "__gstack_test"
+#define        GSTACK_TEST_NAME        "_gstack_test"
 
   pic16_emitpcode(POC_CALL, pic16_popGetWithString( GSTACK_TEST_NAME ));
   
@@ -2259,7 +2415,8 @@ void pic16_testStackOverflow(void)
     symbol *sym;
 
       sym = newSymbol( GSTACK_TEST_NAME , 0 );
-      strcpy(sym->rname, GSTACK_TEST_NAME);
+      sprintf(sym->rname, "%s%s", port->fun_prefix, GSTACK_TEST_NAME);
+//      strcpy(sym->rname, GSTACK_TEST_NAME);
       checkAddSym(&externs, sym);
   }
 
@@ -2269,7 +2426,7 @@ void pic16_testStackOverflow(void)
 void pic16_pushpCodeOp(pCodeOp *pcop)
 {
 //     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg(&pic16_pc_postdec1)));
+  pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg( pic16_stack_postdec )));   //&pic16_pc_postdec1)));
   if(pic16_options.gstack)
     pic16_testStackOverflow();
     
@@ -2278,7 +2435,7 @@ void pic16_pushpCodeOp(pCodeOp *pcop)
 /* pop pcop from stack */
 void pic16_poppCodeOp(pCodeOp *pcop)
 {
-  pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_preinc1), pcop));
+  pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg( pic16_stack_preinc ), pcop));
   if(pic16_options.gstack)
     pic16_testStackOverflow();
 }
@@ -2290,7 +2447,7 @@ void pic16_poppCodeOp(pCodeOp *pcop)
 void pushw(void)
 {
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1));
+  pic16_emitpcode(POC_MOVWF, pic16_popCopyReg( pic16_stack_postdec ));
   if(pic16_options.gstack)
     pic16_testStackOverflow();
 }
@@ -2305,13 +2462,12 @@ void pushaop(asmop *aop, int offset)
 
   if(is_LitAOp(aop)) {
     pic16_emitpcode(POC_MOVLW, pic16_popGet(aop, offset));
-    pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1));
+    pic16_emitpcode(POC_MOVWF, pic16_popCopyReg( pic16_stack_postdec ));
   } else {
     pic16_emitpcode(POC_MOVFF,
-      pic16_popGet2p(pic16_popGet(aop, offset), pic16_popCopyReg(&pic16_pc_postdec1)));
+      pic16_popGet2p(pic16_popGet(aop, offset), pic16_popCopyReg( pic16_stack_postdec )));
   }
 
-//  pic16_emitpcode(POC_MOVFF, pic16_popCombine2(PCOR(pic16_popGet(aop, offset)), &pic16_pc_postdec1, 0));
   if(pic16_options.gstack)
     pic16_testStackOverflow();
 }
@@ -2322,7 +2478,7 @@ void pushaop(asmop *aop, int offset)
 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));
+  pic16_emitpcode(POC_MOVFF, pic16_popCombine2(pic16_stack_preinc, PCOR(pic16_popGet(aop, offset)), 0));
   if(pic16_options.gstack)
     pic16_testStackOverflow();
 }
@@ -2336,11 +2492,12 @@ void popaopidx(asmop *aop, int offset, int index)
     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));
+    pic16_emitpcode(POC_MOVFF, pic16_popCombine2(pic16_frame_plusw, PCOR(pic16_popGet(aop, offset)), 0));
     if(pic16_options.gstack)
       pic16_testStackOverflow();
 }
 
+#if !(USE_GENERIC_SIGNED_SHIFT)
 /*-----------------------------------------------------------------*/
 /* reAdjustPreg - points a register back to where it should        */
 /*-----------------------------------------------------------------*/
@@ -2379,7 +2536,7 @@ static void reAdjustPreg (asmop *aop)
     }   
 
 }
-
+#endif
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -2649,6 +2806,8 @@ static void genUminus (iCode *ic)
 {
   int size, i;
   sym_link *optype, *rtype;
+  symbol *label;
+  int needLabel=0;
 
     FENTRY;    
     
@@ -2677,21 +2836,33 @@ static void genUminus (iCode *ic)
 
     /* otherwise subtract from zero by taking the 2's complement */
     size = AOP_SIZE(IC_LEFT(ic));
-
-    for(i=0; i<size; i++) {
-      if (pic16_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) )
-        pic16_emitpcode(POC_COMF,  pic16_popGet(AOP(IC_LEFT(ic)),i));
-      else {
-        pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(IC_LEFT(ic)),i));
-        pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(IC_RESULT(ic)),i));
-      }
-    }
-
-    pic16_emitpcode(POC_INCF,  pic16_popGet(AOP(IC_RESULT(ic)),0));
-    for(i=1; i<size; i++) {
-      emitSKPNZ;
-      pic16_emitpcode(POC_INCF,  pic16_popGet(AOP(IC_RESULT(ic)),i));
-    }
+    label = newiTempLabel ( NULL );
+    
+    if (pic16_sameRegs (AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic)))) {
+      for (i=size-1; i > 0; i--) {
+        pic16_emitpcode (POC_COMF, pic16_popGet (AOP(IC_LEFT(ic)), i));
+      } // for
+      pic16_emitpcode (POC_NEGF, pic16_popGet (AOP(IC_LEFT(ic)), 0));
+      for (i=1; i < size; i++) {
+        if (i == size - 1) { emitSKPNZ; } else { pic16_emitpcode (POC_BNZ, pic16_popGetLabel (label->key)); needLabel++; }
+        pic16_emitpcode (POC_INCF, pic16_popGet (AOP(IC_LEFT(ic)), i));
+      } // for
+    } else {
+      for (i=size-1; i >= 0; i--) {
+        pic16_emitpcode (POC_COMFW, pic16_popGet (AOP(IC_LEFT(ic)), i));
+       pic16_emitpcode (POC_MOVWF, pic16_popGet (AOP(IC_RESULT(ic)), i));
+      } // for
+      if (size > 1) {
+        for (i=0; i < size-2; i++) {
+          pic16_emitpcode (POC_INCF, pic16_popGet (AOP(IC_RESULT(ic)),i));
+          pic16_emitpcode (POC_BNZ, pic16_popGetLabel (label->key)); needLabel++;
+       } // for
+        pic16_emitpcode (POC_INFSNZ, pic16_popGet (AOP(IC_RESULT(ic)), size-2));
+      } // if
+      pic16_emitpcode (POC_INCF, pic16_popGet(AOP(IC_RESULT(ic)), size-1));
+    }
+    if (needLabel)
+      pic16_emitpLabel (label->key);
 
 release:
     /* release the aops */
@@ -2889,10 +3060,10 @@ static void assignResultValue(operand * oper, int rescall)
                        
        /* fix stack */
        pic16_emitpcode(POC_MOVLW, pic16_popGetLit( AOP_SIZE(oper) ));
-       pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
+       pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( pic16_stackpnt_lo ));      //&pic16_pc_fsr1l ));
        if(STACK_MODEL_LARGE) {
          emitSKPNC;
-         pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h ));
+         pic16_emitpcode(POC_INCF, pic16_popCopyReg( pic16_stackpnt_hi ));     //&pic16_pc_fsr1h ));
        }
       }                        
     } else {
@@ -2903,6 +3074,9 @@ static void assignResultValue(operand * oper, int rescall)
 
 
       /* its called from genReceive (probably) -- VR */
+      /* I hope this code will not be called from somewhere else in the future! 
+       * We manually set the pseudo stack pointer in genReceive. - dw
+       */
       if(!GpsuedoStkPtr && _G.useWreg) {
 //        DEBUGpic16_emitcode("; ", "pop %d", GpsuedoStkPtr);
 
@@ -2913,6 +3087,7 @@ static void assignResultValue(operand * oper, int rescall)
           offset++;
 //          debugf("receive from WREG\n", 0);
         }
+       GpsuedoStkPtr++; /* otherwise the calculation below fails (-_G.useWreg) */
       }
 //      GpsuedoStkPtr++;
       _G.stack_lat = AOP_SIZE(oper)-1;
@@ -2920,7 +3095,7 @@ static void assignResultValue(operand * oper, int rescall)
       while (size) {
         size--;
         GpsuedoStkPtr++;
-        popaopidx(AOP(oper), offset, GpsuedoStkPtr);
+        popaopidx(AOP(oper), offset, GpsuedoStkPtr - _G.useWreg);
 //        debugf("receive from STACK\n", 0);
         offset++;
       }
@@ -3137,7 +3312,7 @@ static void genCall (iCode *ic)
 //     stackParms = psuedoStkPtr;
 //     fprintf(stderr, "%s:%d ic parmBytes = %d\n", __FILE__, __LINE__, ic->parmBytes);
     fname = OP_SYMBOL(IC_LEFT(ic))->rname[0]?OP_SYMBOL(IC_LEFT(ic))->rname:OP_SYMBOL(IC_LEFT(ic))->name;
-    inwparam = inWparamList(OP_SYMBOL(IC_LEFT(ic))->name);
+    inwparam = (inWparamList(OP_SYMBOL(IC_LEFT(ic))->name)) || (FUNC_ISWPARAM(OP_SYM_TYPE(IC_LEFT(ic))));
 
 #if 0
     gpsimDebug_StackDump(__FILE__, __LINE__, fname );
@@ -3188,7 +3363,7 @@ static void genCall (iCode *ic)
                 
                 firstTimeThruLoop=0;
 
-                mov2w (AOP(IC_LEFT(sic)), size);
+                pic16_mov2w (AOP(IC_LEFT(sic)), size);
 
 //                offset++;
               }
@@ -3202,8 +3377,10 @@ static void genCall (iCode *ic)
                 DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1);
 
 //                pushaop(AOP(IC_LEFT(sic)), size);
-                mov2w (AOP(IC_LEFT(sic)), size);
-                pushw();
+                pic16_mov2w (AOP(IC_LEFT(sic)), size);
+
+                if(!_G.resDirect)
+                  pushw();
               }
             }
 
@@ -3253,14 +3430,14 @@ static void genCall (iCode *ic)
     
     if(stackParms>0) {
       if(stackParms == 1) {
-        pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1l));
+        pic16_emitpcode(POC_INCF, pic16_popCopyReg(pic16_stackpnt_lo ));       //&pic16_pc_fsr1l));
       } else {
         pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms));
-        pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
+        pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( pic16_stackpnt_lo ));     //&pic16_pc_fsr1l ));
       }
       if(STACK_MODEL_LARGE) {
         emitSKPNC;
-        pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h ));
+        pic16_emitpcode(POC_INCF, pic16_popCopyReg( pic16_stackpnt_hi ));      //&pic16_pc_fsr1h ));
       }
     }
 
@@ -3325,7 +3502,7 @@ static void genPcall (iCode *ic)
             pic16_AopType(AOP_TYPE(IC_LEFT(sic))));
             DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1);
 
-            mov2w (AOP(IC_LEFT(sic)), size);
+            pic16_mov2w (AOP(IC_LEFT(sic)), size);
             pushw();
           }
 
@@ -3341,6 +3518,11 @@ static void genPcall (iCode *ic)
     // push return address
     // push $ on return stack, then replace with retlbl
 
+    /* Thanks to Thorsten Klose for pointing out that the following
+     * snippet should be interrupt safe */
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_intcon), pic16_popCopyReg(&pic16_pc_postdec1)));
+    pic16_emitpcode(POC_BCF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_intcon), 7));
+
     pic16_emitpcodeNULLop(POC_PUSH);
 
     pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 0, 0));
@@ -3350,6 +3532,11 @@ static void genPcall (iCode *ic)
     pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 2, 0));
     pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosu));
 
+
+    /* restore interrupt control register */
+    pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_preinc1));
+    pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_intcon));
+
     /* make the call by writing the pointer into pc */
     pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),2), pic16_popCopyReg(&pic16_pc_pclatu)));
     pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),1), pic16_popCopyReg(&pic16_pc_pclath)));
@@ -3387,11 +3574,10 @@ static void genPcall (iCode *ic)
     
     if(stackParms>0) {
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms));
-      pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
+      pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( pic16_stackpnt_lo ));
       if(STACK_MODEL_LARGE) {
-        /* this implies that stackParms < 256 !!! -- VR */
         emitSKPNC;
-        pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h ));
+        pic16_emitpcode(POC_INCF, pic16_popCopyReg( pic16_stackpnt_hi ));
       }
     }
 }
@@ -3469,38 +3655,32 @@ static void genFunction (iCode *ic)
       char asymname[128];
       pBlock *apb;
 
-        {
-          int i, found=-1;
-
-            sym = OP_SYMBOL( IC_LEFT(ic));
-            for(i=0;i<=2;i++) {
-              if(interrupts[i]->name
-                    && !STRCASECMP(interrupts[i]->name, sym->name)) {
-                  found = i;
-                  break;
-              }
-            }
-                       
-            if(found == -1) {
-              fprintf(stderr, "PIC16 port: %s:%d: interrupt function but cannot locate symbol (%s)\n",
-                            __FILE__, __LINE__, sym->name);
-              assert( 0 );
-            }
-            _G.interruptvector = found;
-        }
-
-        sprintf(asymname, "ivec_%d_%s", _G.interruptvector, sym->name);
-        asym = newSymbol(asymname, 0);
+//        debugf("interrupt number: %hhi\n", FUNC_INTNO(sym->type));
 
-        apb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute section"));
-        pic16_addpBlock( apb );
-
-        pic16_addpCode2pBlock(apb, pic16_newpCodeCharP(";-----------------------------------------"));
-        pic16_addpCode2pBlock(apb, pic16_newpCodeFunction(moduleName, asym->name));
-        pic16_addpCode2pBlock(apb, pic16_newpCode(POC_GOTO, pic16_popGetWithString( sym->rname )));
+        if(FUNC_INTNO(sym->type) == INTNO_UNSPEC)
+          sprintf(asymname, "ivec_%s", sym->name);
+        else
+          sprintf(asymname, "ivec_0x%x_%s", FUNC_INTNO(sym->type), sym->name);
+  
+        /* when an interrupt is declared as naked, do not emit the special
+         * wrapper segment at vector address. The user should take care for
+         * this instead. -- VR */
+
+        if(!IFFUNC_ISNAKED(ftype) && (FUNC_INTNO(sym->type) != INTNO_UNSPEC)) {
+          asym = newSymbol(asymname, 0);
+          apb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute section"));
+          pic16_addpBlock( apb );
+
+          pic16_addpCode2pBlock(apb, pic16_newpCodeCharP(";-----------------------------------------"));
+          pic16_addpCode2pBlock(apb, pic16_newpCodeFunction(moduleName, asym->name));
+          //pic16_addpCode2pBlock(apb, pic16_newpCode(POC_GOTO, pic16_popGetWithString( sym->rname )));
+          pic16_addpCode2pBlock(apb, pic16_newpCode(POC_GOTO, pic16_newpCodeOpLabel (sym->rname, 0)));
                
-        /* mark the end of this tiny function */
-        pic16_addpCode2pBlock(apb,pic16_newpCodeFunction(NULL,NULL));
+          /* mark the end of this tiny function */
+          pic16_addpCode2pBlock(apb,pic16_newpCodeFunction(NULL,NULL));
+        } else {
+          sprintf(asymname, "%s", sym->rname);
+        }
 
        {
          absSym *abSym;
@@ -3508,14 +3688,19 @@ static void genFunction (iCode *ic)
            abSym = Safe_calloc(1, sizeof(absSym));
            strcpy(abSym->name, asymname);
 
-           switch( _G.interruptvector ) {
+           switch( FUNC_INTNO(sym->type) ) {
              case 0: abSym->address = 0x000000; break;
               case 1: abSym->address = 0x000008; break;
               case 2: abSym->address = 0x000018; break;
+              
+              default:
+//                fprintf(stderr, "no interrupt number is given\n");
+                abSym->address = -1; break;
             }
 
             /* relocate interrupt vectors if needed */
-            abSym->address += pic16_options.ivt_loc;
+            if(abSym->address != -1)
+              abSym->address += pic16_options.ivt_loc;
 
             addSet(&absSymSet, abSym);
         }
@@ -3529,7 +3714,6 @@ static void genFunction (iCode *ic)
     pic16_emitcode("","%s:",sym->rname);
     pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(moduleName,sym->rname));
 
-
     {
       absSym *ab;
 
@@ -3541,7 +3725,6 @@ static void genFunction (iCode *ic)
         }
     }
 
-
     if(IFFUNC_ISNAKED(ftype)) {
       DEBUGpic16_emitcode("; ***", "_naked function, no prologue");
       return;
@@ -3552,7 +3735,10 @@ static void genFunction (iCode *ic)
       //pic16_emitcode("clr","ea");
     }
 
+    currFunc = sym;            /* update the currFunc symbol */
     _G.fregsUsed = sym->regsUsed;
+    _G.sregsAlloc = newBitVect(128);
+    
 
     /* if this is an interrupt service routine then
      * save wreg, status, bsr, prodl, prodh, fsr0l, fsr0h */
@@ -3560,7 +3746,7 @@ static void genFunction (iCode *ic)
         _G.usefastretfie = 1;  /* use shadow registers by default */
         
         /* an ISR should save: WREG, STATUS, BSR, PRODL, PRODH, FSR0L, FSR0H */
-        if(!(_G.interruptvector == 1)) {
+        if(!FUNC_ISSHADOWREGS(sym->type)) {
           /* do not save WREG,STATUS,BSR for high priority interrupts
            * because they are stored in the hardware shadow registers already */
           _G.usefastretfie = 0;
@@ -3577,23 +3763,27 @@ static void genFunction (iCode *ic)
         pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h ));
         
 //        pic16_pBlockConvert2ISR(pb);
-                
     }
 
     /* emit code to setup stack frame if user enabled,
      * and function is not main() */
-        
-    //fprintf(stderr, "function name: %s\n", sym->name);
+    
+//    debugf(stderr, "function name: %s ARGS=%p\n", sym->name, FUNC_ARGS(sym->type));
     if(strcmp(sym->name, "main")) {
-      if(1  /*!options.ommitFramePtr || sym->regsUsed*/) {
+      if(0 
+        || !options.ommitFramePtr 
+//        || sym->regsUsed
+        || IFFUNC_ARGS(sym->type)
+        || FUNC_HASSTACKPARM(sym->etype)
+        ) {
         /* setup the stack frame */
         if(STACK_MODEL_LARGE)
-          pic16_pushpCodeOp(pic16_popCopyReg(&pic16_pc_fsr2h));
-        pic16_pushpCodeOp(pic16_popCopyReg(&pic16_pc_fsr2l));
-//        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));
+          pic16_pushpCodeOp(pic16_popCopyReg(pic16_framepnt_hi));
+        pic16_pushpCodeOp(pic16_popCopyReg(pic16_framepnt_lo));
+
+        pic16_emitpcode(POC_MOVFF, pic16_popCombine2( pic16_stackpnt_lo, pic16_framepnt_lo, 0));
         if(STACK_MODEL_LARGE)
-          pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1h, &pic16_pc_fsr2h, 0));
+          pic16_emitpcode(POC_MOVFF, pic16_popCombine2( pic16_stackpnt_hi, pic16_framepnt_hi, 0));
       }
     }
 
@@ -3603,12 +3793,12 @@ static void genFunction (iCode *ic)
       if (sym->stack > 127)werror(W_STACK_OVERFLOW, sym->name);
 
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack));
-      pic16_emitpcode(POC_SUBWF, pic16_popCopyReg(&pic16_pc_fsr1l));
-      emitSKPNC;
-      pic16_emitpcode(POC_DECF, pic16_popCopyReg(&pic16_pc_fsr1h));
+      pic16_emitpcode(POC_SUBWF, pic16_popCopyReg( pic16_stackpnt_lo ));       //&pic16_pc_fsr1l));
+      emitSKPC;
+      pic16_emitpcode(POC_DECF, pic16_popCopyReg( pic16_stackpnt_hi ));                //&pic16_pc_fsr1h));
     }
           
-    if(inWparamList(sym->name)) {
+    if(inWparamList(sym->name) || FUNC_ISWPARAM(sym->type)) {
       if(IFFUNC_HASVARARGS(sym->type) || IFFUNC_ISREENT(sym->type))
         _G.useWreg = 0;
       else
@@ -3670,18 +3860,21 @@ static void genEndFunction (iCode *ic)
       /* TODO: add code here -- VR */
     }
     
-    if ((IFFUNC_ISREENT(sym->type) || options.stackAuto)
-          && sym->stack) {
-      pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack));
-      pic16_emitpcode(POC_ADDWF, pic16_popCopyReg(&pic16_pc_fsr1l));
-      emitSKPNC;
-      pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1h));
-    }
-
 //    sym->regsUsed = _G.fregsUsed;
     
     /* now we need to restore the registers */
     /* if any registers used */
+
+    /* first restore registers that might be used for stack access */
+    if(_G.sregsAllocSet) {
+    regs *sr;
+    
+      _G.sregsAllocSet = reverseSet( _G.sregsAllocSet );
+      for(sr=setFirstItem(_G.sregsAllocSet) ; sr; sr=setNextItem(_G.sregsAllocSet)) {
+        pic16_poppCodeOp( pic16_popRegFromIdx( sr->rIdx ) );
+      }
+    }
+
     if (sym->regsUsed) {
       int i;
 
@@ -3695,17 +3888,38 @@ static void genEndFunction (iCode *ic)
           }
         }
         pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_EXIT_END));
+    }
 
+      
+
+    if ((IFFUNC_ISREENT(sym->type) || options.stackAuto)
+          && sym->stack) {
+      if (sym->stack == 1) {
+        pic16_emitpcode(POC_INFSNZ, pic16_popCopyReg( pic16_stackpnt_lo ));
+        pic16_emitpcode(POC_INCF, pic16_popCopyReg( pic16_stackpnt_hi ));
+      } else {
+        // we have to add more than one...
+        pic16_emitpcode(POC_MOVWF, pic16_popCopyReg( pic16_stack_postinc ));   // this holds a return value!
+        pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack-1));
+        pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( pic16_stackpnt_lo ));
+        emitSKPNC;
+        pic16_emitpcode(POC_INCF, pic16_popCopyReg( pic16_stackpnt_hi ));
+        pic16_emitpcode(POC_COMF,  pic16_popCopyReg(&pic16_pc_wreg)); // WREG = -(WREG+1)!
+        pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(pic16_stack_plusw)); // this holds a retrun value!
+      }
     }
 
     if(strcmp(sym->name, "main")) {
-      if(1/*!options.ommitFramePtr ||*/ /*sym->regsUsed*/) {
+      if(0
+        || !options.ommitFramePtr
+//        || sym->regsUsed
+        || IFFUNC_ARGS(sym->type)
+        || FUNC_HASSTACKPARM(sym->etype)
+        ) {
         /* restore stack frame */
         if(STACK_MODEL_LARGE)
-          pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr2h ));
-//          pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0));
-        pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr2l ));
-//        pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0));
+          pic16_poppCodeOp( pic16_popCopyReg( pic16_framepnt_hi ));
+        pic16_poppCodeOp( pic16_popCopyReg( pic16_framepnt_lo ));
       }
     }
 
@@ -3717,14 +3931,14 @@ static void genEndFunction (iCode *ic)
       pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodh ));
       pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodl ));
 
-      if(!(_G.interruptvector == 1)) {
+      if(!FUNC_ISSHADOWREGS(sym->type)) {
         /* do not restore interrupt vector for WREG,STATUS,BSR
          * for high priority interrupt, see genFunction */
        pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
        pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_status ));
        pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
       }
-      _G.interruptvector = 0;          /* sanity check */
+//      _G.interruptvector = 0;                /* sanity check */
 
 
       /* if debug then send end of function */
@@ -3764,19 +3978,39 @@ static void genEndFunction (iCode *ic)
 }
 
 
-void pic16_storeForReturn(operand *op, int offset, pCodeOp *dest)
+void pic16_storeForReturn(iCode *ic, /*operand *op,*/ int offset, pCodeOp *dest)
 {
-  if(is_LitOp(op)) {
-    unsigned long lit = (unsigned long)floatFromVal(AOP(op)->aopu.aop_lit);
-      if(lit == 0) {
+  unsigned long lit=1;
+  operand *op;
+
+    op = IC_LEFT(ic);
+  
+    // this fails for is_LitOp(op) (if op is an AOP_PCODE)
+    if(AOP_TYPE(op) == AOP_LIT) {
+      if(!IS_FLOAT(operandType( op ))) {
+        lit = (unsigned long)floatFromVal(AOP(op)->aopu.aop_lit);
+      } else {
+        union {
+          unsigned long lit_int;
+          float lit_float;
+        } info;
+       
+        /* take care if literal is a float */
+        info.lit_float = floatFromVal(AOP(op)->aopu.aop_lit);
+        lit = info.lit_int;
+      }
+    }
+
+    if(is_LitOp(op)) {
+      if(/*(OP_LIVETO(op) <= ic->seq) &&*/ (lit == 0)) {
         pic16_emitpcode(POC_CLRF, dest);
       } else {
         pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset));
         if(dest->type != PO_WREG)pic16_emitpcode(POC_MOVWF, dest);
       }
-  } else {
-    if(dest->type == PO_WREG && (offset == 0)) {
-      pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset));
+    } else {
+      if(dest->type == PO_WREG && (offset == 0)) {
+        pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset));
       return;
     }
     pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op), offset), dest));
@@ -3804,27 +4038,16 @@ static void genRet (iCode *ic)
        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));  // patch 12
+         if(size>3)
+           pic16_storeForReturn(ic, /*IC_LEFT(ic),*/ 3, pic16_popCopyReg(&pic16_pc_fsr0l));
+          
+          if(size>2)
+            pic16_storeForReturn(ic, /*IC_LEFT(ic),*/ 2, pic16_popCopyReg(&pic16_pc_prodh));
 
-               pic16_storeForReturn(IC_LEFT(ic), 0, pic16_popCopyReg(&pic16_pc_wreg));  // patch 12
-//             pic16_emitpcode(POC_MOVFF,
-//                     pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)), 0), pic16_popCopyReg(&pic16_pc_wreg)));
+          if(size>1)
+            pic16_storeForReturn(ic, /*IC_LEFT(ic),*/ 1, pic16_popCopyReg(&pic16_pc_prodl));
+          
+          pic16_storeForReturn(ic, /*IC_LEFT(ic),*/ 0, pic16_popCopyReg(&pic16_pc_wreg));
 
        } else {
                /* >32-bits, setup stack and FSR0 */
@@ -3840,13 +4063,13 @@ static void genRet (iCode *ic)
                        
                /* setup FSR0 */
                pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
-                       pic16_popCopyReg(&pic16_pc_fsr1l), pic16_popCopyReg(&pic16_pc_fsr0l)));
+                       pic16_popCopyReg( pic16_stackpnt_lo ), 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)));
+                               pic16_popCopyReg( pic16_stackpnt_hi ), pic16_popCopyReg(&pic16_pc_fsr0h)));
                } else {
-                       pic16_emitpcode(POC_CLRF, pic16_popCopyReg( &pic16_pc_fsr1h ) );
+                       pic16_emitpcode(POC_CLRF, pic16_popCopyReg( pic16_stackpnt_hi ));
                }
        }
                                
@@ -4497,7 +4720,7 @@ static void genIfxJump (iCode *ic, char *jval)
        if(strcmp(jval,"a") == 0)
          emitSKPZ;
        else if (strcmp(jval,"c") == 0)
-         emitSKPC;
+         emitSKPNC;
        else {
          DEBUGpic16_emitcode ("; ***","%d - assuming %s is in bit space",__LINE__,jval);         
          pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(jval,-1,1, PO_GPR_REGISTER));
@@ -4512,7 +4735,7 @@ static void genIfxJump (iCode *ic, char *jval)
        if(strcmp(jval,"a") == 0)
          emitSKPNZ;
        else if (strcmp(jval,"c") == 0)
-         emitSKPNC;
+         emitSKPC;
        else {
          DEBUGpic16_emitcode ("; ***","%d - assuming %s is in bit space",__LINE__,jval);         
          pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(jval,-1,1, PO_GPR_REGISTER));
@@ -4528,6 +4751,33 @@ static void genIfxJump (iCode *ic, char *jval)
     ic->generated = 1;
 }
 
+static void genIfxpCOpJump (iCode *ic, pCodeOp *jop)
+{
+  FENTRY;
+  
+    /* if true label then we jump if condition
+    supplied is true */
+    if ( IC_TRUE(ic) ) {
+      DEBUGpic16_emitcode ("; ***","%d - assuming is in bit space",__LINE__);    
+      pic16_emitpcode(POC_BTFSC, jop);
+
+      pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ic)->key));
+      pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ic)->key+100 + pic16_labelOffset);
+
+    } else {
+      /* false label is present */
+      DEBUGpic16_emitcode ("; ***","%d - assuming is in bit space",__LINE__);
+      pic16_emitpcode(POC_BTFSS, jop);
+         
+      pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ic)->key));
+      pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ic)->key+100 + pic16_labelOffset);
+    }
+
+
+    /* mark the icode as generated */
+    ic->generated = 1;
+}
+
 #if 0
 // not needed ATM
 
@@ -4594,14 +4844,15 @@ static void genSkipc(resolvedIfx *rifx)
     return;
 
   if(rifx->condition)
-    emitSKPC;
-  else
     emitSKPNC;
+  else
+    emitSKPC;
 
   pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rifx->lbl->key));
   rifx->generated = 1;
 }
 
+#if !(USE_SIMPLE_GENCMP)
 /*-----------------------------------------------------------------*/
 /* genSkipz2                                                       */
 /*-----------------------------------------------------------------*/
@@ -4620,6 +4871,7 @@ static void genSkipz2(resolvedIfx *rifx, int invert_condition)
   pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rifx->lbl->key));
   rifx->generated = 1;
 }
+#endif
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -4648,6 +4900,7 @@ static void genSkipz(iCode *ifx, int condition)
 }
 #endif
 
+#if !(USE_SIMPLE_GENCMP)
 /*-----------------------------------------------------------------*/
 /* genSkipCond                                                     */
 /*-----------------------------------------------------------------*/
@@ -4665,6 +4918,7 @@ static void genSkipCond(resolvedIfx *rifx,operand *op, int offset, int bit)
   pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rifx->lbl->key));
   rifx->generated = 1;
 }
+#endif
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -4695,10 +4949,242 @@ static int genChkZeroes(operand *op, int lit,  int size)
 }
 #endif
 
+
+/*-----------------------------------------------------------------*/
+/* mov2w_regOrLit :- move to WREG either the offset's byte from    */
+/*                  aop (if it's NOT a literal) or from lit (if    */
+/*                  aop is a literal)                              */
+/*-----------------------------------------------------------------*/
+void mov2w_regOrLit (asmop *aop, unsigned long lit, int offset) {
+  if (aop->type == AOP_LIT) {
+    pic16_emitpcode (POC_MOVLW, pic16_popGetLit(lit >> (offset*8)));
+  } else {
+    pic16_emitpcode (POC_MOVFW, pic16_popGet (aop, offset));
+  }
+}
+
 /*-----------------------------------------------------------------*/
 /* genCmp :- greater or less than comparison                       */
 /*-----------------------------------------------------------------*/
-#if 1
+
+#if USE_SIMPLE_GENCMP          /* { */
+
+/* genCmp performs a left < right comparison, stores
+ * the outcome in result (if != NULL) and generates
+ * control flow code for the ifx (if != NULL).
+ *
+ * This version leaves in sequences like
+ * "B[CS]F STATUS,0; BTFS[CS] STATUS,0"
+ * which should be optmized by the peephole
+ * optimizer - RN 2005-01-01 */
+static void genCmp (operand *left,operand *right,
+                    operand *result, iCode *ifx, int sign)
+{
+  resolvedIfx rIfx;
+  int size;
+  int offs;
+  symbol *templbl;
+  operand *dummy;
+  unsigned long lit;
+  unsigned long mask;
+  int performedLt;
+
+  FENTRY;
+  
+  assert (AOP_SIZE(left) == AOP_SIZE(right));
+  assert (left && right);
+
+  size = AOP_SIZE(right) - 1;
+  mask = (0x100UL << (size*8)) - 1;
+  // in the end CARRY holds "left < right" (performedLt == 1) or "left >= right" (performedLt == 0)
+  performedLt = 1;
+  templbl = NULL;
+  lit = 0;
+  
+  resolveIfx (&rIfx, ifx);
+
+  /* handle for special cases */
+  if(pic16_genCmp_special(left, right, result, ifx, &rIfx, sign))
+      return;
+
+  /**********************************************************************
+   * handle bits - bit compares are promoted to int compares seemingly! *
+   **********************************************************************/
+#if 0
+  // THIS IS COMPLETELY UNTESTED!
+  if (AOP_TYPE(left) == AOP_CRY && AOP_TYPE(right) == AOP_CRY) {
+    pCodeOp *pcleft = pic16_popGet(AOP(left), 0);
+    pCodeOp *pcright = pic16_popGet(AOP(right), 0);
+    assert (pcleft->type == PO_GPR_BIT && pcright->type == PO_GPR_BIT);
+
+    emitSETC;
+    // 1 < {0,1} is false --> clear C by skipping the next instruction
+    //pic16_emitpcode (POC_BTFSS, pic16_popCopyGPR2Bit (AOP(left),0), PCORB(pcleft)->bit);
+    pic16_emitpcode (POC_BTFSS, pic16_popGet (AOP(left), 0));
+    // {0,1} < 0 is false --> clear C by NOT skipping the next instruction
+    pic16_emitpcode (POC_BTFSS, pic16_popCopyGPR2Bit (pic16_popGet(AOP(right),0), PCORB(pcright)->bit));
+    emitCLRC; // only skipped for left=0 && right=1
+
+    goto correct_result_in_carry;
+  } // if
+#endif
+
+  /*************************************************
+   * make sure that left is register (or the like) *
+   *************************************************/
+  if (!isAOP_REGlike(left)) {
+    DEBUGpc ("swapping arguments (AOP_TYPEs %d/%d)", AOP_TYPE(left), AOP_TYPE(right));
+    assert (isAOP_LIT(left));
+    assert (isAOP_REGlike(right));
+    // swap left and right
+    // left < right <==> right > left <==> (right >= left + 1)
+    lit = (unsigned long)floatFromVal(AOP(left)->aopu.aop_lit);
+
+    if ( (!sign && (lit & mask) == mask) || (sign && (lit & mask) == (mask >> 1)) ) {
+      // MAXVALUE < right? always false
+      if (performedLt) emitCLRC; else emitSETC;
+      goto correct_result_in_carry;
+    } // if
+
+    // This fails for lit = 0xFF (unsigned) AND lit = 0x7F (signed),
+    // that's why we handled it above.
+    lit++;
+
+    dummy = left;
+    left = right;
+    right = dummy;
+
+    performedLt ^= 1; // instead of "left < right" we check for "right >= left+1"
+  } else if (isAOP_LIT(right)) {
+    lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
+  } // if
+
+  assert (isAOP_REGlike(left)); // left must be register or the like
+  assert (isAOP_REGlike(right) || isAOP_LIT(right)); // right may be register-like or a literal
+
+  /*************************************************
+   * special cases go here                         *
+   *************************************************/
+
+  if (isAOP_LIT(right)) {
+    if (!sign) {
+      // unsigned comparison to a literal
+      DEBUGpc ("unsigned compare: left %s lit(0x%X=%lu), size=%d", performedLt ? "<" : ">=", lit, lit, size+1);
+      if (lit == 0) {
+       // unsigned left < 0? always false
+       if (performedLt) emitCLRC; else emitSETC;
+       goto correct_result_in_carry;
+      }
+    } else {
+      // signed comparison to a literal
+      DEBUGpc ("signed compare: left %s lit(0x%X=%ld), size=%d, mask=%x", performedLt ? "<" : ">=", lit, lit, size+1, mask);
+      if ((lit & mask) == ((0x80 << (size*8)) & mask)) {
+       // signed left < 0x80000000? always false
+       if (performedLt) emitCLRC; else emitSETC;
+       goto correct_result_in_carry;
+      } else if (lit == 0) {
+       // compare left < 0; set CARRY if SIGNBIT(left) is set
+       if (performedLt) emitSETC; else emitCLRC;
+       pic16_emitpcode (POC_BTFSS, pic16_popCopyGPR2Bit(pic16_popGet (AOP(left), size), 7));
+       if (performedLt) emitCLRC; else emitSETC;
+       goto correct_result_in_carry;
+      }
+    } // if (!sign)
+  } // right is literal
+
+  /*************************************************
+   * perform a general case comparison             *
+   * make sure we get CARRY==1 <==> left >= right  *
+   *************************************************/
+  // compare most significant bytes
+  //DEBUGpc ("comparing bytes at offset %d", size);
+  if (!sign) {
+    // unsigned comparison
+    mov2w_regOrLit (AOP(right), lit, size);
+    pic16_emitpcode (POC_SUBFW, pic16_popGet (AOP(left), size));
+  } else {
+    // signed comparison
+    // (add 2^n to both operands then perform an unsigned comparison)
+    if (isAOP_LIT(right)) {
+      // left >= LIT <-> LIT-left <= 0 <-> LIT-left == 0 OR !(LIT-left >= 0)
+      unsigned char litbyte = (lit >> (8*size)) & 0xFF;
+
+      if (litbyte == 0x80) {
+       // left >= 0x80 -- always true, but more bytes to come
+       pic16_mov2w (AOP(left), size);
+       pic16_emitpcode (POC_XORLW, pic16_popGetLit (0x80)); // set ZERO flag
+       emitSETC;
+      } else {
+       // left >= LIT <-> left + (-LIT) >= 0 <-> left + (0x100-LIT) >= 0x100
+       pic16_mov2w (AOP(left), size);
+       pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x80));
+       pic16_emitpcode (POC_ADDLW, pic16_popGetLit ((0x100 - (litbyte + 0x80)) & 0x00FF));
+      } // if
+    } else {
+      pCodeOp *pctemp = pic16_popCopyReg(&pic16_pc_prodl);
+      //pCodeOp *pctemp = pic16_popGetTempReg(1);
+      pic16_mov2w (AOP(left), size);
+      pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x80));
+      pic16_emitpcode (POC_MOVWF, pctemp);
+      pic16_mov2w (AOP(right), size);
+      pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x80));
+      pic16_emitpcode (POC_SUBFW, pctemp);
+      //pic16_popReleaseTempReg(pctemp, 1);
+    }
+  } // if (!sign)
+
+  // compare remaining bytes (treat as unsigned case from above)
+  templbl = newiTempLabel ( NULL );
+  offs = size;
+  while (offs--) {
+    //DEBUGpc ("comparing bytes at offset %d", offs);
+    pic16_emitpcode (POC_BNZ, pic16_popGetLabel (templbl->key));
+    mov2w_regOrLit (AOP(right), lit, offs);
+    pic16_emitpcode (POC_SUBFW, pic16_popGet (AOP(left), offs));
+  } // while (offs)
+  pic16_emitpLabel (templbl->key);
+  goto result_in_carry;
+
+result_in_carry:
+  
+  /****************************************************
+   * now CARRY contains the result of the comparison: *
+   * SUBWF sets CARRY iff                             *
+   * F-W >= 0 <==> F >= W <==> !(F < W)               *
+   * (F=left, W=right)                                *
+   ****************************************************/
+
+  if (performedLt) {
+    if (result && AOP_TYPE(result) != AOP_CRY) {
+      // value will be stored
+      emitTOGC;
+    } else {
+      // value wil only be used in the following genSkipc()
+      rIfx.condition ^= 1;
+    }
+  } // if
+
+correct_result_in_carry:
+
+  // assign result to variable (if neccessary)
+  if (result && AOP_TYPE(result) != AOP_CRY) {
+    //DEBUGpc ("assign result");
+    size = AOP_SIZE(result);
+    while (size--) {
+      pic16_emitpcode (POC_CLRF, pic16_popGet (AOP(result), size));
+    } // while
+    pic16_emitpcode (POC_RLCF, pic16_popGet (AOP(result), 0));
+  } // if (result)
+
+  // perform conditional jump
+  if (ifx) {
+    //DEBUGpc ("generate control flow");
+    genSkipc (&rIfx);
+    ifx->generated = 1;
+  } // if
+}
+
+#elif 1                /* } */
                /* { */
       /* original code */
 static void genCmp (operand *left,operand *right,
@@ -4749,6 +5235,7 @@ static void genCmp (operand *left,operand *right,
   /* if left & right are bit variables */
   if (AOP_TYPE(left) == AOP_CRY &&
       AOP_TYPE(right) == AOP_CRY ) {
+    assert (0 && "bit variables used in genCmp");
     pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir);
     pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir);
   } else {
@@ -5159,14 +5646,16 @@ static void genCmp (operand *left,operand *right,
            pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
            pic16_emitpcode(POC_ADDLW, pic16_popGetLit( 0x80));
            pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x100-i));
-           rFalseIfx.condition ^= 1;
-           genSkipc(&rFalseIfx);
-
-
-           if(ifx) ifx->generated = 1;
 
-           if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result)))
+           if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) {
+             emitTOGC;
+             if(ifx) ifx->generated = 1;
              goto check_carry;
+           } else {
+             rFalseIfx.condition ^= 1;
+             genSkipc(&rFalseIfx);
+             if(ifx) ifx->generated = 1;
+           }
 
            return;
          }
@@ -5418,7 +5907,7 @@ check_carry:
 
 }
 
-#else  /* old version of genCmp() */   /* } else { */
+#elif 0        /* VR version of genCmp() */    /* } else { */
 
 /* check all condition and return appropriate instruction, POC_CPFSGT or POC_CPFFSLT */
 static int selectCompareOp(resolvedIfx *rIfx, iCode *ifx,
@@ -5528,10 +6017,6 @@ static void compareAop(resolvedIfx *resIfx, iCode *ifx, symbol *falselbl,
   }
 }
 
-
-  
-
-#if 1  /* { */
 static void genCmp (operand *left, operand *right,
                     operand *result, iCode *ifx, int sign)
 {
@@ -5683,7 +6168,6 @@ static void genCmp (operand *left, operand *right,
         }
       } else {
 
-
         /* unsigned compare */      
         DEBUGpic16_emitcode ("; ***","%s: %d: unsigned compare", __FUNCTION__, __LINE__);
     
@@ -5694,10 +6178,8 @@ static void genCmp (operand *left, operand *right,
           compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, NULL, NULL);
 
         }
-
         if(ifx)ifx->generated = 1;
 
-
         if(AOP_SIZE(result)) {
           pic16_emitpLabel(falselbl->key);
           pic16_outBitC( result );
@@ -5782,463 +6264,6 @@ static void genCmp (operand *left, operand *right,
     }
 }
 
-#else    /* } else { */
-
-/* new version of genCmp -- VR 20041012 */
-static void genCmp (operand *left,operand *right,
-                    operand *result, iCode *ifx, int sign)
-{
-  int size; //, offset = 0 ;
-  unsigned long lit = 0L,i = 0;
-  resolvedIfx rFalseIfx;
-  int willCheckCarry=0;
-  //  resolvedIfx rTrueIfx;
-  symbol *truelbl;
-
-    FENTRY;
-  
-  /* General concept:
-   * subtract right from left if at the end the carry flag is set then we
-   * know that left is greater than right */
-            
-  resolveIfx(&rFalseIfx,ifx);
-  truelbl  = newiTempLabel(NULL);
-  size = max(AOP_SIZE(left),AOP_SIZE(right));
-
-  DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
-
-  /* POC_CPFSGT        compare f, wreg, skip if f greater than wreg
-   * POC_CPFSLT compare f, wreg, skip if f less then wreg */
-  
-
-  /* if literal is on the right then swap with left */
-  if ((AOP_TYPE(right) == AOP_LIT)) {
-    operand *tmp = right ;
-    unsigned long mask = (0x100 << (8*(size-1))) - 1;
-
-      lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
-
-//      lit = (lit - 1) & mask;
-      right = left;
-      left = tmp;
-      rFalseIfx.condition ^= 1;                /* reverse compare */
-  } else
-  if ((AOP_TYPE(left) == AOP_LIT)) {
-    /* float compares are handled by support functions */
-    lit = (unsigned long)floatFromVal(AOP(left)->aopu.aop_lit);
-  }
-
-
-  //if(IC_TRUE(ifx) == NULL)
-  /* if left & right are bit variables */
-  if (AOP_TYPE(left) == AOP_CRY &&
-      AOP_TYPE(right) == AOP_CRY ) {
-
-    pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir);
-    pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir);
-
-  } else {
-    symbol *lbl  = newiTempLabel(NULL);
-
-    if(AOP_TYPE(left) == AOP_LIT) {
-      DEBUGpic16_emitcode(";left lit","lit = 0x%x, sign=%d",lit,sign);
-
-      if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result)))
-        willCheckCarry = 1;
-      else willCheckCarry = 0;
-
-      /* Special cases */
-      if((lit == 0) && (sign == 0)) {
-        /* unsigned compare to 0 */
-        DEBUGpic16_emitcode("; unsigned compare to 0","lit = 0x%x, sign=%d",lit,sign);
-        
-       size--;
-       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-       while(size) 
-         pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),--size));
-
-       genSkipz2(&rFalseIfx,0);
-       if(ifx)ifx->generated = 1;
-       return;
-      }
-
-      if(size==1) {
-       /* Special cases */
-       lit &= 0xff;
-       if(((lit == 0xff) && !sign) || ((lit==0x7f) && sign)) {
-         /* degenerate compare can never be true */
-         if(rFalseIfx.condition == 0)
-           pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rFalseIfx.lbl->key));
-
-         if(ifx) ifx->generated = 1;
-         return;
-       }
-
-       if(sign) {
-         /* signed comparisons to a literal byte */
-          DEBUGpic16_emitcode(";signed compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign);
-
-         int lp1 = (lit+1) & 0xff;
-
-         DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x condition = %d lp1 = %i",__LINE__,lit, rFalseIfx.condition, lp1);
-         switch (lp1) {
-         case 0:
-           rFalseIfx.condition ^= 1;
-           genSkipCond(&rFalseIfx,right,0,7);
-           break;
-         case 0x7f:
-           pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
-           pic16_emitpcode(POC_XORLW, pic16_popGetLit(0x7f));
-           genSkipz2(&rFalseIfx,1);
-           break;
-         default:
-           pic16_emitpcode(POC_MOVLW, pic16_popGetLit( lit ));
-           
-           if(rFalseIfx.condition)
-             pic16_emitpcode(POC_CPFSLT, pic16_popGet(AOP(right), 0));
-            else
-              pic16_emitpcode(POC_CPFSGT, pic16_popGet(AOP(right), 0));
-
-           if(willCheckCarry) {
-              if(!rFalseIfx.condition) { emitCLRC; emitSETC; }
-              else { emitSETC; emitCLRC; }
-              
-            } else {
-              pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key));
-            }              
-                     
-/*         pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
-           pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80));
-           pic16_emitpcode(POC_ADDLW, pic16_popGetLit(((-(lit+1)) & 0xff) ^ 0x80));
-           rFalseIfx.condition ^= 1;
-           genSkipc(&rFalseIfx);
-*/
-           break;
-         }
-       } else {
-         /* unsigned comparisons to a literal byte */
-          DEBUGpic16_emitcode("; unsigned compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign);
-
-         switch(lit & 0xff ) {
-                         /* special cases */
-         case 0:
-           pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
-           genSkipz2(&rFalseIfx,0);
-           break;
-         case 0x7f:
-           rFalseIfx.condition ^= 1;
-           genSkipCond(&rFalseIfx,right,0,7);
-           break;
-         default:
-           pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lit+1) & 0xff));
-           pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0));
-           DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-           rFalseIfx.condition ^= 1;
-           if (AOP_TYPE(result) == AOP_CRY)
-             genSkipc(&rFalseIfx);
-           else {
-             pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),0));
-             pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0));
-           }         
-           break;
-         }
-       }
-
-       if(ifx) ifx->generated = 1;
-       if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result)))
-               goto check_carry;
-       return;
-
-      } else {
-
-       /* Size is greater than 1 */
-
-       if(sign) {
-         int lp1 = lit+1;
-
-         size--;
-
-         if(lp1 == 0) {
-           /* this means lit = 0xffffffff, or -1 */
-
-
-           DEBUGpic16_emitcode(";left lit = -1","line = %d ",__LINE__);
-           rFalseIfx.condition ^= 1;
-           genSkipCond(&rFalseIfx,right,size,7);
-           if(ifx) ifx->generated = 1;
-           return;
-         }
-
-         if(lit == 0) {
-           int s = size;
-
-           if(rFalseIfx.condition) {
-             pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
-           }
-
-           pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-           while(size--)
-             pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size));
-
-
-           emitSKPZ;
-           if(rFalseIfx.condition) {
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
-             pic16_emitpLabel(truelbl->key);
-           }else {
-             rFalseIfx.condition ^= 1;
-             genSkipCond(&rFalseIfx,right,s,7);
-           }
-
-           if(ifx) ifx->generated = 1;
-           return;
-         }
-
-         if((size == 1) &&  (0 == (lp1&0xff))) {
-           /* lower byte of signed word is zero */
-           DEBUGpic16_emitcode(";left lit","line = %d  0x%x+1 low byte is zero",__LINE__,lit);
-           i = ((lp1 >> 8) & 0xff) ^0x80;
-           pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-           pic16_emitpcode(POC_ADDLW, pic16_popGetLit( 0x80));
-           pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x100-i));
-           rFalseIfx.condition ^= 1;
-           genSkipc(&rFalseIfx);
-
-
-           if(ifx) ifx->generated = 1;
-           return;
-         }
-
-         if(lit & (0x80 << (size*8))) {
-           /* Lit is less than zero */
-           DEBUGpic16_emitcode(";left lit","line = %d  0x%x is less than 0",__LINE__,lit);
-           //rFalseIfx.condition ^= 1;
-           //genSkipCond(&rFalseIfx,left,size,7);
-           //rFalseIfx.condition ^= 1;
-           pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
-           //pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
-
-           if(rFalseIfx.condition)
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
-           else
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
-
-
-         } else {
-           /* Lit is greater than or equal to zero */
-           DEBUGpic16_emitcode(";left lit","line = %d  0x%x is greater than 0",__LINE__,lit);
-           //rFalseIfx.condition ^= 1;
-           //genSkipCond(&rFalseIfx,right,size,7);
-           //rFalseIfx.condition ^= 1;
-
-           //pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0));
-           //pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
-
-           pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
-           if(rFalseIfx.condition)
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
-           else
-             pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
-
-         }
-
-
-         pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lp1 >> (size*8)) & 0xff));
-         pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size));
-
-         while(size--) {
-
-           pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lp1 >> (size*8)) & 0xff));
-           emitSKPNZ;
-           pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size));
-         }
-         rFalseIfx.condition ^= 1;
-         //rFalseIfx.condition = 1;
-         genSkipc(&rFalseIfx);
-
-         pic16_emitpLabel(truelbl->key);
-
-         if(ifx) ifx->generated = 1;
-         return;
-         // end of if (sign)
-       } else {
-
-         /* compare word or long to an unsigned literal on the right.*/
-
-
-         size--;
-         if(lit < 0xff) {
-           DEBUGpic16_emitcode ("; ***","%s  %d lit =0x%x < 0xff",__FUNCTION__,__LINE__,lit);
-           switch (lit) {
-           case 0:
-             break; /* handled above */
-/*
-           case 0xff:
-             pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-             while(size--)
-               pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size));
-             genSkipz2(&rFalseIfx,0);
-             break;
-*/
-           default:
-             pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-             while(--size)
-               pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size));
-
-             emitSKPZ;
-             if(rFalseIfx.condition)
-               pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
-             else
-               pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
-
-
-             pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit+1));
-             pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0));
-
-             rFalseIfx.condition ^= 1;
-             genSkipc(&rFalseIfx);
-           }
-
-           pic16_emitpLabel(truelbl->key);
-
-           if(ifx) ifx->generated = 1;
-           return;
-         }
-
-
-         lit++;
-         DEBUGpic16_emitcode ("; ***","%s  %d lit =0x%x",__FUNCTION__,__LINE__,lit);
-         i = (lit >> (size*8)) & 0xff;
-
-         pic16_emitpcode(POC_MOVLW, pic16_popGetLit(i));
-         pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size));
-
-         while(size--) {
-           i = (lit >> (size*8)) & 0xff;
-
-           if(i) {
-             pic16_emitpcode(POC_MOVLW, pic16_popGetLit(i));
-             emitSKPNZ;
-             pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size));
-           } else {
-             /* this byte of the lit is zero, 
-              * if it's not the last then OR in the variable */
-             if(size)
-               pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size));
-           }
-         }
-
-
-         pic16_emitpLabel(lbl->key);
-
-         rFalseIfx.condition ^= 1;
-
-         genSkipc(&rFalseIfx);
-       }
-
-       if(sign)
-         pic16_emitpLabel(truelbl->key);
-       if(ifx) ifx->generated = 1;
-       return;
-      }
-    }
-    /* Compare two variables */
-
-    DEBUGpic16_emitcode(";sign","%d",sign);
-
-    size--;
-    if(sign) {
-      /* Sigh. thus sucks... */
-      if(size) {
-       pCodeOp *pctemp;
-       
-       pctemp = pic16_popGetTempReg(1);
-       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),size));
-       pic16_emitpcode(POC_MOVWF, pctemp);             //pic16_pic16_popRegFromIdx(pic16_Gstack_base_addr));
-       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80));
-       pic16_emitpcode(POC_XORWF, pctemp);             //pic16_popRegFromIdx(pic16_Gstack_base_addr));
-       pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),size));
-       pic16_emitpcode(POC_SUBFW, pctemp);             //pic16_popRegFromIdx(pic16_Gstack_base_addr));
-       pic16_popReleaseTempReg(pctemp, 1);
-      } else {
-       /* Signed char comparison */
-       /* Special thanks to Nikolai Golovchenko for this snippet */
-       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
-       pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),0));
-       pic16_emitpcode(POC_RRCFW,  pic16_popGet(AOP(left),0)); /* could be any register */
-       pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(left),0));
-       pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),0));
-       pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80));
-
-       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-       genSkipc(&rFalseIfx);
-         
-       if(ifx) ifx->generated = 1;
-       return;
-      }
-
-    } else {
-
-      pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-      pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),size));
-    }
-
-
-    /* The rest of the bytes of a multi-byte compare */
-    while (size) {
-
-      emitSKPZ;
-      pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(lbl->key));
-      size--;
-
-      pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size));
-      pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),size));
-
-
-    }
-
-    pic16_emitpLabel(lbl->key);
-
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if ((AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) || 
-       (AOP_TYPE(result) == AOP_REG)) {
-      pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),0));
-      pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0));
-    } else {
-      genSkipc(&rFalseIfx);
-    }        
-    //genSkipc(&rFalseIfx);
-    if(ifx) ifx->generated = 1;
-
-    return;
-
-  }
-
-check_carry:
-  if ((AOP_TYPE(result) != AOP_CRY) 
-       && AOP_SIZE(result)) {
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-
-    if(!ifx)pic16_emitpLabel( rFalseIfx.lbl->key );
-
-    pic16_outBitC(result);
-  } else {
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* if the result is used in the next
-       ifx conditional branch then generate
-       code a little differently */
-    if (ifx )
-      genIfxJump (ifx,"c");
-    else
-      pic16_outBitC(result);
-    /* leave the result in acc */
-  }
-
-}
-#endif /* } */
-
-
 #endif /* } */
 
 
@@ -6651,6 +6676,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
   int preserve_result = 0;
   int generate_result = 0;
   int i=0;
+  unsigned long lit = -1;
 
   FENTRY;
   
@@ -6675,6 +6701,10 @@ static void genCmpEq (iCode *ic, iCode *ifx)
       left = tmp;
     }
 
+  if (AOP_TYPE(right) == AOP_LIT) {
+    lit = (unsigned long) floatFromVal (AOP(right)->aopu.aop_lit);
+  }
+
   if ( regsInCommon(left, result) || regsInCommon(right, result) )
     preserve_result = 1;
 
@@ -6696,9 +6726,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
           else
             pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), i));
         }
-      if(is_LitOp(right))
-        pic16_emitpcode(POC_XORLW, pic16_popGet(AOP(right), i)); 
-      else
+      if(is_LitOp(right)) {
+        if (is_LitOp(left) || (0 != ((lit >> (8*i))&0x00FF))) {
+          pic16_emitpcode(POC_XORLW, pic16_popGet(AOP(right), i)); 
+        }
+      } else
         pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right), i)); 
 
       pic16_emitpcode(POC_BNZ,pic16_popGetLabel(falselbl->key));
@@ -7095,12 +7127,14 @@ static iCode *ifxForOp ( operand *op, iCode *ic )
           return ic->next;
     }
 
+    /*
     if (ic->next &&
         ic->next->op == IFX &&
         IC_COND(ic->next)->key == op->key) {
       DEBUGpic16_emitcode ("; WARNING ","%d IGNORING liveTo range in %s",__LINE__,__FUNCTION__);
       return ic->next;
     }
+    */
 
     DEBUGpic16_emitcode ("; NULL :(","%d",__LINE__);
     if (ic->next &&
@@ -7163,7 +7197,7 @@ static void genAndOp (iCode *ic)
     only those used in arthmetic operations remain */
     pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE);
     pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE);
-    pic16_aopOp((result=IC_RESULT(ic)),ic,FALSE);
+    pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE);
 
     DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
 
@@ -7212,7 +7246,7 @@ static void genOrOp (iCode *ic)
     only those used in arthmetic operations remain */
     pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE);
     pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE);
-    pic16_aopOp((result=IC_RESULT(ic)),ic,FALSE);
+    pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE);
 
     DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
 
@@ -7507,9 +7541,9 @@ static void genAnd (iCode *ic, iCode *ifx)
       else {
         if(ifx) {
           pic16_emitpcode(POC_BRA, pic16_popGetLabel(rIfx.lbl->key));
-          pic16_emitpLabel(tlbl->key);
           ifx->generated = 1;
         }
+        pic16_emitpLabel(tlbl->key);
         goto release;
       }
     }
@@ -8193,6 +8227,7 @@ static void genXor (iCode *ic, iCode *ifx)
 static void genInline (iCode *ic)
 {
   char *buffer, *bp, *bp1;
+  char *cbuf;
     
        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
@@ -8200,8 +8235,66 @@ static void genInline (iCode *ic)
 
        buffer = bp = bp1 = Safe_calloc(1, strlen(IC_INLINE(ic))+1);
        strcpy(buffer,IC_INLINE(ic));
+       
+       while((bp1=strstr(bp, "\\n"))) {
+         *bp1++ = '\n';
+         *bp1++ = ' ';
+         bp = bp1;
+        }
+        bp = bp1 = buffer;
 
-//     fprintf(stderr, "%s:%d inline asm : < %s >\n", __FILE__, __LINE__, buffer);
+       cbuf = Safe_strdup( buffer );
+
+        if(asmInlineMap)
+        {
+          symbol *sym;
+          char *s;
+          int cblen;
+
+            cbuf = Safe_strdup(buffer);
+            cblen = strlen(buffer)+1;
+            memset(cbuf, 0, cblen);
+
+            bp = buffer;
+            bp1 = cbuf;
+            while(*bp) {
+              if(*bp != '%')*bp1++ = *bp++;
+              else {
+                int i;
+
+                  bp++;
+                  i = *bp - '0';
+                  if(i>elementsInSet(asmInlineMap))break;
+                  
+                  bp++;
+                  s = indexSet(asmInlineMap, i);
+                  DEBUGpc("searching symbol s = `%s'", s);
+                  sym = findSym(SymbolTab, NULL, s);
+
+                  if(sym->reqv) {
+                    strcat(bp1, sym->reqv->operand.symOperand->regs[0]->name);
+                  } else {
+                    strcat(bp1, sym->rname);
+                  }
+                  
+                  while(*bp1)bp1++;
+              }
+              
+              if(strlen(bp1) > cblen - 16) {
+                int i = strlen(cbuf);
+                cblen += 50;
+                cbuf = realloc(cbuf, cblen);
+                memset(cbuf+i, 0, 50);
+                bp1 = cbuf + i;
+              }
+            }
+            
+            free(buffer);
+            buffer = Safe_strdup( cbuf );
+            free(cbuf);
+            
+            bp = bp1 = buffer;
+        }
 
        /* emit each line as a code */
        while (*bp) {
@@ -8249,7 +8342,7 @@ static void genRRC (iCode *ic)
   left = IC_LEFT(ic);
   result=IC_RESULT(ic);
   pic16_aopOp (left,ic,FALSE);
-  pic16_aopOp (result,ic,FALSE);
+  pic16_aopOp (result,ic,TRUE);
 
   DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
 
@@ -8294,7 +8387,7 @@ static void genRLC (iCode *ic)
   left = IC_LEFT(ic);
   result=IC_RESULT(ic);
   pic16_aopOp (left,ic,FALSE);
-  pic16_aopOp (result,ic,FALSE);
+  pic16_aopOp (result,ic,TRUE);
 
   DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
 
@@ -9321,7 +9414,7 @@ void pic16_genLeftShiftLiteral (operand *left,
                                  operand *result,
                                  iCode *ic)
 {    
-    int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
+    int shCount = (int) abs(floatFromVal (AOP(right)->aopu.aop_lit));
     int size;
 
     FENTRY;
@@ -9329,7 +9422,7 @@ void pic16_genLeftShiftLiteral (operand *left,
     pic16_freeAsmop(right,NULL,ic,TRUE);
 
     pic16_aopOp(left,ic,FALSE);
-    pic16_aopOp(result,ic,FALSE);
+    pic16_aopOp(result,ic,TRUE);
 
     size = getSize(operandType(result));
 
@@ -9396,6 +9489,8 @@ static void genMultiAsm( PIC_OPCODE poc, operand *reg, int size, int endian)
   }
 
 }
+
+#if !(USE_GENERIC_SIGNED_SHIFT)
 /*-----------------------------------------------------------------*/
 /* genLeftShift - generates code for left shifting                 */
 /*-----------------------------------------------------------------*/
@@ -9577,7 +9672,7 @@ static void genLeftShift (iCode *ic)
   pic16_freeAsmop(left,NULL,ic,TRUE);
   pic16_freeAsmop(result,NULL,ic,TRUE);
 }
-
+#endif
 
 
 #if 0
@@ -9929,13 +10024,13 @@ static void genRightShiftLiteral (operand *left,
                                   iCode *ic,
                                   int sign)
 {    
-  int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
+  int shCount = (int) abs(floatFromVal (AOP(right)->aopu.aop_lit));
   int lsize,res_size;
 
   pic16_freeAsmop(right,NULL,ic,TRUE);
 
   pic16_aopOp(left,ic,FALSE);
-  pic16_aopOp(result,ic,FALSE);
+  pic16_aopOp(result,ic,TRUE);
 
   DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d result:%d left:%d",__FUNCTION__,__LINE__,shCount,AOP_SIZE(result),AOP_SIZE(left));
 
@@ -9950,8 +10045,10 @@ static void genRightShiftLiteral (operand *left,
 
   /* I suppose that the left size >= result size */
   if(shCount == 0){
-    while(res_size--)
-      movLeft2Result(left, lsize, result, res_size);
+    assert (res_size <= lsize);
+    while (res_size--) {
+      mov2f (AOP(result), AOP(left), res_size);
+    } // for
   }
 
   else if(shCount >= (lsize * 8)){
@@ -10001,6 +10098,7 @@ static void genRightShiftLiteral (operand *left,
   pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
+#if !(USE_GENERIC_SIGNED_SHIFT)
 /*-----------------------------------------------------------------*/
 /* genSignedRightShift - right shift of signed number              */
 /*-----------------------------------------------------------------*/
@@ -10141,7 +10239,10 @@ static void genSignedRightShift (iCode *ic)
   pic16_freeAsmop(result,NULL,ic,TRUE);
   pic16_freeAsmop(right,NULL,ic,TRUE);
 }
+#endif
 
+#if !(USE_GENERIC_SIGNED_SHIFT)
+#warning This implementation of genRightShift() is incomplete!
 /*-----------------------------------------------------------------*/
 /* genRightShift - generate code for right shifting                */
 /*-----------------------------------------------------------------*/
@@ -10250,19 +10351,184 @@ static void genRightShift (iCode *ic)
     }
     reAdjustPreg(AOP(result));
 
-    pic16_emitcode("","%05d_DS_:",tlbl1->key+100);
-    pic16_emitcode("djnz","b,%05d_DS_",tlbl->key+100);
+    pic16_emitcode("","%05d_DS_:",tlbl1->key+100);
+    pic16_emitcode("djnz","b,%05d_DS_",tlbl->key+100);
+
+release:
+    pic16_freeAsmop(left,NULL,ic,TRUE);
+    pic16_freeAsmop (right,NULL,ic,TRUE);
+    pic16_freeAsmop(result,NULL,ic,TRUE);
+}
+#endif
+
+#if (USE_GENERIC_SIGNED_SHIFT)
+/*-----------------------------------------------------------------*/
+/* genGenericShift - generates code for left or right shifting     */
+/*-----------------------------------------------------------------*/
+static void genGenericShift (iCode *ic, int isShiftLeft) {
+  operand *left,*right, *result;
+  int offset;
+  int sign, signedCount;
+  symbol *label_complete, *label_loop_pos, *label_loop_neg, *label_negative;
+  PIC_OPCODE pos_shift, neg_shift;
+
+  FENTRY;
+
+  right = IC_RIGHT(ic);
+  left  = IC_LEFT(ic);
+  result = IC_RESULT(ic);
+
+  pic16_aopOp(right,ic,FALSE);
+  pic16_aopOp(left,ic,FALSE);
+  pic16_aopOp(result,ic,TRUE);
+
+  sign = !SPEC_USIGN(operandType (left));
+  signedCount = !SPEC_USIGN(operandType (right));
+
+  /* if the shift count is known then do it 
+     as efficiently as possible */
+  if (AOP_TYPE(right) == AOP_LIT) {
+    long lit = (long)floatFromVal(AOP(right)->aopu.aop_lit);
+    if (signedCount && lit < 0) { lit = -lit; isShiftLeft = !isShiftLeft; }
+    // we should modify right->aopu.aop_lit here!
+    // Instead we use abs(shCount) in genXXXShiftLiteral()...
+    // lit > 8*size is handled in pic16_genXXXShiftLiteral()
+    if (isShiftLeft)
+      pic16_genLeftShiftLiteral (left,right,result,ic);
+    else
+      genRightShiftLiteral (left,right,result,ic, sign);
+
+    goto release;
+  } // if (right is literal)
+
+  /* shift count is unknown then we have to form a loop.
+   * Note: we take only the lower order byte since shifting
+   * more than 32 bits make no sense anyway, ( the
+   * largest size of an object can be only 32 bits )
+   * Note: we perform arithmetic shifts if the left operand is
+   * signed and we do an (effective) right shift, i. e. we
+   * shift in the sign bit from the left. */
+   
+  label_complete = newiTempLabel ( NULL );
+  label_loop_pos = newiTempLabel ( NULL );
+  label_loop_neg = NULL;
+  label_negative = NULL;
+  pos_shift = isShiftLeft ? POC_RLCF : POC_RRCF;
+  neg_shift = isShiftLeft ? POC_RRCF : POC_RLCF;
+
+  if (signedCount) {
+    // additional labels needed
+    label_loop_neg = newiTempLabel ( NULL );
+    label_negative = newiTempLabel ( NULL );
+  } // if
+
+  // copy source to result -- this will effectively truncate the left operand to the size of result!
+  // (e.g. char c = 0x100 << -3 will become c = 0x00 >> 3 == 0x00 instad of 0x20)
+  // This is fine, as it only occurs for left shifting with negative count which is not standardized!
+  for (offset=0; offset < min(AOP_SIZE(left), AOP_SIZE(result)); offset++) {
+    mov2f (AOP(result),AOP(left), offset);
+  } // for
+
+  // if result is longer than left, fill with zeros (or sign)
+  if (AOP_SIZE(left) < AOP_SIZE(result)) {
+    if (sign && AOP_SIZE(left) > 0) {
+      // shift signed operand -- fill with sign
+      pic16_emitpcode (POC_CLRF, pic16_popCopyReg (&pic16_pc_wreg));
+      pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(left)-1), 7));
+      pic16_emitpcode (POC_MOVLW, pic16_popGetLit (0xFF));
+      for (offset=AOP_SIZE(left); offset < AOP_SIZE(result); offset++) {
+        pic16_emitpcode (POC_MOVWF, pic16_popGet (AOP(result), offset));
+      } // for
+    } else {
+      // shift unsigned operand -- fill result with zeros
+      for (offset=AOP_SIZE(left); offset < AOP_SIZE(result); offset++) {
+        pic16_emitpcode (POC_CLRF, pic16_popGet (AOP(result), offset));
+      } // for
+    }
+  } // if (size mismatch)
+
+  pic16_mov2w (AOP(right), 0);
+  pic16_emitpcode (POC_BZ, pic16_popGetLabel (label_complete->key));
+  if (signedCount) pic16_emitpcode (POC_BN, pic16_popGetLabel (label_negative->key));
+  
+#if 0
+  // perform a shift by one (shift count is positive)
+  // cycles used for shifting {unsigned,signed} values on n bytes by [unsigned,signed] shift count c>0:
+  // 2n+[2,3]+({1,3}+n+3)c-2+[0,2]=({4,6}+n)c+2n+[0,3]         ({5,7}c+[2,5] / {6,8}c+[4, 7] / {8,10}c+[ 8,11])
+  pic16_emitpLabel (label_loop_pos->key);
+  emitCLRC;
+  if (sign && (pos_shift == POC_RRCF)) {
+    pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+    emitSETC;
+  } // if
+  genMultiAsm (pos_shift, result, AOP_SIZE(result), pos_shift == POC_RRCF);
+  pic16_emitpcode (POC_DECFSZ, pic16_popCopyReg (&pic16_pc_wreg));
+  pic16_emitpcode (POC_BRA, pic16_popGetLabel (label_loop_pos->key));
+#else
+  // perform a shift by one (shift count is positive)
+  // cycles used for shifting {unsigned,signed} values on n bytes by [unsigned,signed] shift count c>0:
+  // 2n+[2,3]+2+({0,2}+n+3)c-1+[0,2]=({3,5}+n)c+2n+[3,6]       ({4,6}c+[5,8] / {5,7}c+[7,10] / {7, 9}c+[11,14])
+  // This variant is slower for 0<c<3, equally fast for c==3, and faster for 3<c.
+  pic16_emitpcode (POC_NEGF, pic16_popCopyReg (&pic16_pc_wreg));
+  emitCLRC;
+  pic16_emitpLabel (label_loop_pos->key);
+  if (sign && (pos_shift == POC_RRCF)) {
+    pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+    emitSETC;
+  } // if
+  genMultiAsm (pos_shift, result, AOP_SIZE(result), pos_shift == POC_RRCF);
+  //pic16_emitpcode (POC_INCF, pic16_popCopyReg (&pic16_pc_wreg)); // gpsim does not like this...
+  pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x01));
+  pic16_emitpcode (POC_BNC, pic16_popGetLabel (label_loop_pos->key));
+#endif
+
+  if (signedCount) {
+    pic16_emitpcode (POC_BRA, pic16_popGetLabel (label_complete->key));
+
+    pic16_emitpLabel (label_negative->key);
+    // perform a shift by -1 (shift count is negative)
+    // 2n+4+1+({0,2}+n+3)*c-1=({3,5}+n)c+2n+4                  ({4,6}c+6 / {5,7}c+8 / {7,9}c+12)
+    emitCLRC;
+    pic16_emitpLabel (label_loop_neg->key);
+    if (sign && (neg_shift == POC_RRCF)) {
+      pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+      emitSETC;
+    } // if
+    genMultiAsm (neg_shift, result, AOP_SIZE(result), neg_shift == POC_RRCF);
+    //pic16_emitpcode (POC_INCF, pic16_popCopyReg (&pic16_pc_wreg)); // gpsim does not like this...
+    pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x01));
+    pic16_emitpcode (POC_BNC, pic16_popGetLabel (label_loop_neg->key));
+  } // if (signedCount)
+
+  pic16_emitpLabel (label_complete->key);
 
 release:
-    pic16_freeAsmop(left,NULL,ic,TRUE);
-    pic16_freeAsmop (right,NULL,ic,TRUE);
-    pic16_freeAsmop(result,NULL,ic,TRUE);
+  pic16_freeAsmop (right,NULL,ic,TRUE);
+  pic16_freeAsmop(left,NULL,ic,TRUE);
+  pic16_freeAsmop(result,NULL,ic,TRUE);
+}
+
+static void genLeftShift (iCode *ic) {
+  genGenericShift (ic, 1);
 }
 
+static void genRightShift (iCode *ic) {
+  genGenericShift (ic, 0);
+}
+#endif
 
-void pic16_loadFSR0(operand *op)
+
+/* load FSR0 with address of/from op according to is_LitOp() or if lit is 1 */
+void pic16_loadFSR0(operand *op, int lit)
 {
-       pic16_emitpcode(POC_LFSR, pic16_popGetLit2(0, pic16_popGet(AOP(op), 0)));
+  if(OP_SYMBOL(op)->remat || is_LitOp( op )) {
+    pic16_emitpcode(POC_LFSR, pic16_popGetLit2(0, pic16_popGet(AOP(op), 0)));
+  } else {
+    assert (!OP_SYMBOL(op)->remat);
+    // set up FSR0 with address of result
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op),0), pic16_popCopyReg(&pic16_pc_fsr0l)));
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op),1), pic16_popCopyReg(&pic16_pc_fsr0h)));
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -10293,28 +10559,20 @@ static void genUnpackBits (operand *result, operand *left, char *rname, int ptyp
       /* it is a single bit, so use the appropriate bit instructions */
       DEBUGpic16_emitcode (";","%s %d optimize bit read",__FUNCTION__,__LINE__);
 
-      if(IS_BITFIELD(etype)/* && !IS_ITEMP(left)*/) {
-        pic16_emitpcode(POC_MOVFW, pic16_popGet( AOP(result), 0 ));
-        pic16_emitpcode(POC_BCF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr));
-      } else {
-        pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg));
-      }
+      pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg));
       
-      if((ptype == POINTER) && (result)) {
+      // distinguish (p->bitfield) and p.bitfield, remat seems to work...
+      if(OP_SYMBOL(left)->remat && (ptype == POINTER) && (result)) {
         /* workaround to reduce the extra lfsr instruction */
         pic16_emitpcode(POC_BTFSC,
               pic16_popCopyGPR2Bit(pic16_popGet(AOP(left), 0), bstr));
       } else {
+       pic16_loadFSR0 (left, 0);
         pic16_emitpcode(POC_BTFSC,
               pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr));
       }
        
-
-      if(IS_BITFIELD(etype)) {
-        pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr));
-      } else {
-        pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg));
-      }      
+      pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg));
 
       pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0 ));      
       return;
@@ -10325,7 +10583,12 @@ static void genUnpackBits (operand *result, operand *left, char *rname, int ptyp
         /* the following call to pic16_loadFSR0 is temporary until
          * optimization to handle single bit assignments is added
          * to the function. Until then use the old safe way! -- VR */
-        pic16_loadFSR0( left );
+
+    if (OP_SYMBOL(left)->remat) {
+       // access symbol directly
+       pic16_mov2w (AOP(left), 0);
+    } else {
+        pic16_loadFSR0( left, 0 );
  
        /* read the first byte  */
        switch (ptype) {
@@ -10339,9 +10602,10 @@ static void genUnpackBits (operand *result, operand *left, char *rname, int ptyp
                case CPOINTER:
                        pic16_emitcode("clr","a");
                        pic16_emitcode("movc","a","@a+dptr");
+                       assert (0);
                        break;
        }
-       
+    }
 
        /* if we have bitdisplacement then it fits   */
        /* into this byte completely or if length is */
@@ -10432,7 +10696,9 @@ static void genDataPointerGet(operand *left,
   int size, offset = 0, leoffset=0 ;
 
        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-       pic16_aopOp(result, ic, FALSE);
+       pic16_aopOp(result, ic, TRUE);
+
+       FENTRY;
 
        size = AOP_SIZE(result);
 //     fprintf(stderr, "%s:%d size= %d\n", __FILE__, __LINE__, size);
@@ -10484,9 +10750,9 @@ static void genDataPointerGet(operand *left,
        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), offset); // patch 8
+//             pic16_DumpOp("(result)",result);
+               if(is_LitAOp(AOP(result))) {
+                       pic16_mov2w(AOP(left), offset); // patch 8
                        pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), offset));
                } else {
                        pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
@@ -10511,7 +10777,7 @@ static void genNearPointerGet (operand *left,
                               operand *result, 
                               iCode *ic)
 {
-  asmop *aop = NULL;
+//  asmop *aop = NULL;
   //regs *preg = NULL ;
   sym_link *rtype, *retype;
   sym_link *ltype = operandType(left);    
@@ -10541,32 +10807,60 @@ static void genNearPointerGet (operand *left,
     }
     
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    pic16_aopOp (result,ic,TRUE);
+    
+    DEBUGpic16_pic16_AopType(__LINE__, left, NULL, result);
+
+#if 1
+    if(IS_BITFIELD( retype )
+      && (SPEC_BLEN(operandType(result))==1)
+    ) {
+      iCode *nextic;
+      pCodeOp *jop;
+      int bitstrt, bytestrt;
+
+        /* if this is bitfield of size 1, see if we are checking the value
+         * of a single bit in an if-statement,
+         * if yes, then don't generate usual code, but execute the
+         * genIfx directly -- VR */
+
+        nextic = ic->next;
+
+        /* CHECK: if next iCode is IFX
+         * and current result operand is nextic's conditional operand
+         * and current result operand live ranges ends at nextic's key number
+         */
+        if((nextic->op == IFX)
+          && (result == IC_COND(nextic))
+          && (OP_LIVETO(result) == nextic->seq)
+          && (OP_SYMBOL(left)->remat)  // below fails for "if (p->bitfield)"
+          ) {
+            /* everything is ok then */
+            /* find a way to optimize the genIfx iCode */
+
+            bytestrt = SPEC_BSTR(operandType(result))/8;
+            bitstrt = SPEC_BSTR(operandType(result))%8;
+            
+            jop = pic16_popCopyGPR2Bit(pic16_popGet(AOP(left), 0), bitstrt);
+
+            genIfxpCOpJump(nextic, jop);
+            
+            pic16_freeAsmop(left, NULL, ic, TRUE);
+            pic16_freeAsmop(result, NULL, ic, TRUE);
+            return;
+        }
+    }
+#endif
+
 
     /* if the value is already in a pointer register
      * then don't need anything more */
-    if (!AOP_INPREG(AOP(left))) {
+    if (1 || !AOP_INPREG(AOP(left))) {  // AOP_INPREG(AOP(left)) is not always correct...
       /* 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 */
-      /* bitfields will be handled by genUnpackBits */
-      if(!IS_BITFIELD(retype)) {
-
-        if(is_LitAOp( AOP(left) )) {
-          pic16_loadFSR0( left );
-        } else {
-            // set up FSR0 with address from left
-            pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10
-            pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10
-        }
-      }
+      ;
     }
-//    else
-//    rname = pic16_aopGet(AOP(left),0,FALSE,FALSE);
-    
-    pic16_aopOp (result,ic,FALSE);
-    
-    DEBUGpic16_pic16_AopType(__LINE__, left, NULL, result);
 
     /* if bitfield then unpack the bits */
     if (IS_BITFIELD(retype)) 
@@ -10578,11 +10872,8 @@ static void genNearPointerGet (operand *left,
        
       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-      /* fsr0 is loaded already -- VR */
-//      pic16_loadFSR0( left );
+      pic16_loadFSR0( left, 0 );
 
-//      pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
-//      pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0));
       while(size--) {
         if(size) {
           pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0),
@@ -10592,29 +10883,9 @@ static void genNearPointerGet (operand *left,
                 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 ) {
-
-               pic16_emitcode("mov","a,@%s",rname);
-               pic16_aopPut(AOP(result),"a",offset);
-           } else {
-               sprintf(buffer,"@%s",rname);
-               pic16_aopPut(AOP(result),buffer,offset);
-           }
-           offset++ ;
-           if (size)
-               pic16_emitcode("inc","%s",rname);
-       }
-*/
     }
 
+#if 0
     /* now some housekeeping stuff */
     if (aop) {
       /* we had to allocate for this iCode */
@@ -10636,6 +10907,7 @@ static void genNearPointerGet (operand *left,
 //          pic16_emitcode("dec","%s",rname);
        }
     }
+#endif
 
     /* done */
     pic16_freeAsmop(left,NULL,ic,TRUE);
@@ -10675,7 +10947,7 @@ static void genPagedPointerGet (operand *left,
        rname = pic16_aopGet(AOP(left),0,FALSE,FALSE);
     
     pic16_freeAsmop(left,NULL,ic,TRUE);
-    pic16_aopOp (result,ic,FALSE);
+    pic16_aopOp (result,ic,TRUE);
 
     /* if bitfield then unpack the bits */
     if (IS_BITFIELD(retype)) 
@@ -10753,7 +11025,7 @@ static void genFarPointerGet (operand *left,
     }
     /* so dptr know contains the address */
     pic16_freeAsmop(left,NULL,ic,TRUE);
-    pic16_aopOp(result,ic,FALSE);
+    pic16_aopOp(result,ic,TRUE);
 
     /* if bit then unpack */
     if (IS_BITFIELD(retype)) 
@@ -10772,6 +11044,7 @@ static void genFarPointerGet (operand *left,
 
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
+
 #if 0
 /*-----------------------------------------------------------------*/
 /* genCodePointerGet - get value from code space                  */
@@ -10824,6 +11097,7 @@ static void genCodePointerGet (operand *left,
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 #endif
+
 #if 0
 /*-----------------------------------------------------------------*/
 /* genGenPointerGet - gget value from generic pointer space        */
@@ -10865,7 +11139,7 @@ static void genGenPointerGet (operand *left,
                // 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--) {
@@ -10899,11 +11173,10 @@ static void genGenPointerGet (operand *left,
 {
   int size, offset, lit;
   sym_link *retype = getSpec(operandType(result));
-  char fgptrget[32];
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     pic16_aopOp(left,ic,FALSE);
-    pic16_aopOp(result,ic,FALSE);
+    pic16_aopOp(result,ic,TRUE);
     size = AOP_SIZE(result);
 
     DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
@@ -10935,30 +11208,10 @@ static void genGenPointerGet (operand *left,
       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_prodl)));
       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), 2));
       
-      switch( size ) {
-        case 1: strcpy(fgptrget, "__gptrget1"); break;
-        case 2: strcpy(fgptrget, "__gptrget2"); break;
-        case 3: strcpy(fgptrget, "__gptrget3"); break;
-        case 4: strcpy(fgptrget, "__gptrget4"); break;
-        default:
-          werror(W_POSSBUG2, __FILE__, __LINE__);
-          abort();
-      }
-      
-      pic16_emitpcode(POC_CALL, pic16_popGetWithString( fgptrget ));
+      pic16_callGenericPointerRW(0, size);
       
       assignResultValue(result, 1);
       
-      {
-        symbol *sym;
-
-          sym = newSymbol( fgptrget, 0 );
-          strcpy(sym->rname, fgptrget);
-          checkAddSym(&externs, sym);
-
-//          fprintf(stderr, "%s:%d adding extern symbol %s in externs\n", __FILE__, __LINE__, fgptrget);
-      }
-              
       goto release;
     }
 
@@ -10992,29 +11245,11 @@ static void genConstPointerGet (operand *left,
   DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result);
 
   DEBUGpic16_emitcode ("; "," %d getting const pointer",__LINE__);
-#if 0                                                                  // patch 15
-  pic16_emitpcode(POC_CALL,pic16_popGetLabel(albl->key));
-  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(blbl->key));
-  pic16_emitpLabel(albl->key);
-
-  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));
-  pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_pcl));
-
-  pic16_emitpLabel(blbl->key);
-
-  pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),0));
-#endif                                                                 // patch 15
-
 
   // set up table pointer
   if( (AOP_TYPE(left) == AOP_PCODE) 
       && ((AOP(left)->aopu.pcop->type == PO_IMMEDIATE)
-          || (AOP(left)->aopu.pcop->type == PO_DIR)))                  // patch 15 ......
+          || (AOP(left)->aopu.pcop->type == PO_DIR)))
     {
       pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),0));
       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptrl));
@@ -11022,25 +11257,20 @@ static void genConstPointerGet (operand *left,
       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptrh));
       pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),2));
       pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptru));
-    }
-  else
-    {
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_tblptrl)));
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_tblptrh)));
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),2), pic16_popCopyReg(&pic16_pc_tblptru)));
-    }
-
+  } else {
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_tblptrl)));
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_tblptrh)));
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),2), pic16_popCopyReg(&pic16_pc_tblptru)));
+  }
 
-  while(size--)
-    {
-      pic16_emitpcodeNULLop(POC_TBLRD_POSTINC);
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_tablat), pic16_popGet(AOP(result),offset)));
-      offset++;
-    }
+  while(size--) {
+    pic16_emitpcodeNULLop(POC_TBLRD_POSTINC);
+    pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_tablat), pic16_popGet(AOP(result),offset)));
+    offset++;
+  }
     
   pic16_freeAsmop(left,NULL,ic,TRUE);
   pic16_freeAsmop(result,NULL,ic,TRUE);
-
 }
 
 
@@ -11049,9 +11279,9 @@ static void genConstPointerGet (operand *left,
 /*-----------------------------------------------------------------*/
 static void genPointerGet (iCode *ic)
 {
-    operand *left, *result ;
-    sym_link *type, *etype;
-    int p_type;
+  operand *left, *result ;
+  sym_link *type, *etype;
+  int p_type;
 
     FENTRY;
     
@@ -11072,57 +11302,56 @@ static void genPointerGet (iCode *ic)
 
     /* if left is of type of pointer then it is simple */
     if (IS_PTR(type) && !IS_FUNC(type->next)) 
-        p_type = DCL_TYPE(type);
+      p_type = DCL_TYPE(type);
     else {
-       /* we have to go by the storage class */
-       p_type = PTR_TYPE(SPEC_OCLS(etype));
+      /* we have to go by the storage class */
+      p_type = PTR_TYPE(SPEC_OCLS(etype));
 
-       DEBUGpic16_emitcode ("; ***","%d - resolve pointer by storage class",__LINE__);
+      DEBUGpic16_emitcode ("; ***","%d - resolve pointer by storage class",__LINE__);
 
-       if (SPEC_OCLS(etype)->codesp ) {
-         DEBUGpic16_emitcode ("; ***","%d - cpointer",__LINE__);
-         //p_type = CPOINTER ; 
-       }
-       else
-           if (SPEC_OCLS(etype)->fmap && !SPEC_OCLS(etype)->paged)
-             DEBUGpic16_emitcode ("; ***","%d - fpointer",__LINE__);
-              /*p_type = FPOINTER ;*/ 
-           else
-               if (SPEC_OCLS(etype)->fmap && SPEC_OCLS(etype)->paged)
-                 DEBUGpic16_emitcode ("; ***","%d - ppointer",__LINE__);
-/*                 p_type = PPOINTER; */
-               else
-                   if (SPEC_OCLS(etype) == idata )
-                     DEBUGpic16_emitcode ("; ***","%d - ipointer",__LINE__);
-/*                     p_type = IPOINTER; */
-                   else
-                     DEBUGpic16_emitcode ("; ***","%d - pointer",__LINE__);
-/*                     p_type = POINTER ; */
+      if (SPEC_OCLS(etype)->codesp ) {
+        DEBUGpic16_emitcode ("; ***","%d - cpointer",__LINE__);
+        //p_type = CPOINTER ;  
+      } else
+      if (SPEC_OCLS(etype)->fmap && !SPEC_OCLS(etype)->paged) {
+        DEBUGpic16_emitcode ("; ***","%d - fpointer",__LINE__);
+        /*p_type = FPOINTER ;*/ 
+      } else
+      if (SPEC_OCLS(etype)->fmap && SPEC_OCLS(etype)->paged) {
+        DEBUGpic16_emitcode ("; ***","%d - ppointer",__LINE__);
+        /* p_type = PPOINTER; */
+      } else
+      if (SPEC_OCLS(etype) == idata ) {
+        DEBUGpic16_emitcode ("; ***","%d - ipointer",__LINE__);
+        /* p_type = IPOINTER; */
+      } else {
+        DEBUGpic16_emitcode ("; ***","%d - pointer",__LINE__);
+        /* p_type = POINTER ; */
+      }
     }
 
     /* now that we have the pointer type we assign
     the pointer values */
     switch (p_type) {
-
-    case POINTER:      
-    case IPOINTER:
+      case POINTER:    
+      case IPOINTER:
        genNearPointerGet (left,result,ic);
        break;
 
-    case PPOINTER:
+      case PPOINTER:
        genPagedPointerGet(left,result,ic);
        break;
 
-    case FPOINTER:
+      case FPOINTER:
        genFarPointerGet (left,result,ic);
        break;
 
-    case CPOINTER:
+      case CPOINTER:
        genConstPointerGet (left,result,ic);
        //pic16_emitcodePointerGet (left,result,ic);
        break;
 
-    case GPOINTER:
+      case GPOINTER:
 #if 0
       if (IS_PTR_CONST(type))
        genConstPointerGet (left,result,ic);
@@ -11136,7 +11365,6 @@ static void genPointerGet (iCode *ic)
              "genPointerGet: illegal pointer type");
     
     }
-
 }
 
 /*-----------------------------------------------------------------*/
@@ -11151,7 +11379,6 @@ static void genPackBits (sym_link    *etype , operand *result,
   int rLen = 0 ;
   int blen, bstr ;   
   sym_link *retype;
-  char *l ;
 
        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
        blen = SPEC_BLEN(etype);
@@ -11168,7 +11395,7 @@ static void genPackBits (sym_link    *etype , operand *result,
 
                        lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
 //                     pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
-                       if((p_type == POINTER) && (result)) {
+                       if(OP_SYMBOL(result)->remat && (p_type == POINTER) && (result)) {
                                /* workaround to reduce the extra lfsr instruction */
                                if(lit) {
                                        pic16_emitpcode(POC_BSF,
@@ -11178,7 +11405,7 @@ static void genPackBits (sym_link    *etype , operand *result,
                                                pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), 0), bstr));
                                }
                        } else {
-                                pic16_loadFSR0( result );
+                                pic16_loadFSR0(result, 0);
                                if(lit) {
                                        pic16_emitpcode(POC_BSF,
                                                pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr));
@@ -11190,7 +11417,7 @@ static void genPackBits (sym_link    *etype , operand *result,
        
                  return;
                }
-
+                /* move literal to W */
                pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right), 0));
                offset++;
        } else
@@ -11221,80 +11448,143 @@ static void genPackBits (sym_link    *etype , operand *result,
             pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0));
             
             return;
-        } else
+        } else {
+          /* move right to W */
           pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++));
+        }
 
-       /* if the bit lenth is less than or    */
+       /* if the bit length is less than or   */
        /* it exactly fits a byte then         */
        if((shCnt=SPEC_BSTR(etype))
                || SPEC_BLEN(etype) <= 8 )  {
-
-               pic16_emitpcode(POC_ANDLW, pic16_popGetLit((1U << blen)-1));
-
-               /* shift left acc */
-               AccLsh(shCnt);
-
-               /* using PRODL as a temporary register here */
-               pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_prodl));
-
-               switch (p_type) {
+               int fsr0_setup = 0;
+
+               if (blen != 8 || bstr != 0) {
+                 // we need to combine the value with the old value
+                 pic16_emitpcode(POC_ANDLW, pic16_popGetLit((1U << blen)-1));
+
+         DEBUGpic16_emitcode(";", "shCnt = %d SPEC_BSTR(etype) = %d:%d", shCnt,
+               SPEC_BSTR(etype), SPEC_BLEN(etype));
+               
+                 /* shift left acc */
+                 AccLsh(shCnt);
+
+                 /* using PRODH as a temporary register here */
+                 pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_prodh));
+
+                if (OP_SYMBOL(result)->remat) {
+                  // access symbol directly
+                  pic16_mov2w (AOP(result), 0);
+                } else {
+                 /* get old value */
+                 switch (p_type) {
                        case FPOINTER:
                        case POINTER:
-                               pic16_loadFSR0( result );
+                               pic16_loadFSR0( result, 0 );
+                               fsr0_setup = 1;
                                pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
 //                             pic16_emitcode ("mov","b,a");
 //                             pic16_emitcode("mov","a,@%s",rname);
                                break;
 
                        case GPOINTER:
-                                werror(W_POSSBUG2, __FILE__, __LINE__);
+                               if (AOP(result)->aopu.aop_reg[2]) {
+                                 // prepare call to __gptrget1, this is actually genGenPointerGet(result, WREG, ?ic?)
+                                 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_prodl)));
+                                 pic16_emitpcode (POC_MOVFW, pic16_popGet(AOP(result),2));
+
+                                  pic16_callGenericPointerRW(0, 1);
+                               } else {
+                                 // data pointer (just 2 byte given)
+                                 pic16_loadFSR0( result, 0 );
+                                 fsr0_setup = 1;
+                                 pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+                               }
+                               
+                               // warnings will be emitted below
+                               //pic16_emitpcomment ("; =?= genPackBits, GPOINTER...");
+                                //werror(W_POSSBUG2, __FILE__, __LINE__);
                                break;
 
-               }
+                       default:
+                               assert (0 && "invalid pointer type specified");
+                               break;
+                 }
+                }
 #if 1
-               pic16_emitpcode(POC_ANDLW, pic16_popGetLit(
+                 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));
+                 pic16_emitpcode(POC_IORFW, pic16_popCopyReg(&pic16_pc_prodh));
+               } // if (blen != 8 || bstr != 0)
+
+               /* write new value back */
+              if (OP_SYMBOL(result)->remat) {
+               pic16_emitpcode (POC_MOVWF, pic16_popGet(AOP(result),0));
+              } else {
+               switch (p_type) {
+                       case FPOINTER:
+                       case POINTER:
+                               if (!fsr0_setup) pic16_loadFSR0( result, 0 );
+                               pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+                               break;
+
+                       case GPOINTER:
+                               if (AOP(result)->aopu.aop_reg[2]) {
+                                 // prepare call to __gptrset1, this is actually genGenPointerSet(WREG, result, ?ic?)
+                                 pic16_emitpcode (POC_MOVWF, pic16_popCopyReg (pic16_stack_postdec/*pic16_pc_postdec1*/));
+                                 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_prodl)));
+                                 pic16_emitpcode (POC_MOVFW, pic16_popGet(AOP(result),2));
+                                  
+                                  pic16_callGenericPointerRW(1, 1);
+                               } else {
+                                 // data pointer (just 2 byte given)
+                                 if (!fsr0_setup) pic16_loadFSR0( result, 0 );
+                                 pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+                               }
+                               
+                               // this should work in all cases (as soon as gptrget/gptrput work on EEPROM and PROGRAM MEMORY)
+                               //pic16_emitpcomment ("; =?= genPackBits, GPOINTER access");
+                                werror(W_POSSBUG2, __FILE__, __LINE__);
+                               break;
+
+                       default:
+                               assert (0 && "invalid pointer type specified");
+                               break;
+               }
+              }
 #endif
 
          return;
        }
 
 
+#if 0
        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);
+#endif
 
 
-    /* if we r done */
-    if ( SPEC_BLEN(etype) <= 8 )
-        return ;
-
-    pic16_emitcode("inc","%s",rname);
-    rLen = SPEC_BLEN(etype) ;     
-
-
-
+    pic16_loadFSR0(result, 0);                 // load FSR0 with address of result
+    rLen = SPEC_BLEN(etype)-8;
+    
     /* now generate for lengths greater than one byte */
     while (1) {
-
-        l = pic16_aopGet(AOP(right),offset++,FALSE,TRUE);
-
         rLen -= 8 ;
-        if (rLen <= 0 )
-            break ;
+        if (rLen <= 0 ) {
+          mov2fp(pic16_popCopyReg(&pic16_pc_prodh), AOP(right), offset);
+          break ;
+        }
 
         switch (p_type) {
             case POINTER:
-                if (*l == '@') {
-                    MOVA(l);
-                    pic16_emitcode("mov","@%s,a",rname);
-                } else
-                    pic16_emitcode("mov","@%s,%s",rname,l);
+                  pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postinc0));
                 break;
 
+/*
             case FPOINTER:
                 MOVA(l);
                 pic16_emitcode("movx","@dptr,a");
@@ -11304,21 +11594,25 @@ static void genPackBits (sym_link    *etype , operand *result,
                 MOVA(l);
                 DEBUGpic16_emitcode(";lcall","__gptrput");
                 break;  
+*/
+          default:
+            assert(0);
         }   
-        pic16_emitcode ("inc","%s",rname);
-    }
 
-    MOVA(l);
+
+        pic16_mov2w(AOP(right), offset++);
+    }
 
     /* last last was not complete */
     if (rLen)   {
         /* save the byte & read byte */
         switch (p_type) {
             case POINTER:
-                pic16_emitcode ("mov","b,a");
-                pic16_emitcode("mov","a,@%s",rname);
+//                pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_prodl));
+                pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
                 break;
 
+/*
             case FPOINTER:
                 pic16_emitcode ("mov","b,a");
                 pic16_emitcode("movx","a,@dptr");
@@ -11330,29 +11624,40 @@ static void genPackBits (sym_link    *etype , operand *result,
                 pic16_emitcode ("lcall","__gptrget");
                 pic16_emitcode ("pop","b");
                 break;
+*/
+            default:
+              assert(0);
         }
-
-        pic16_emitcode ("anl","a,#0x%02x",((unsigned char)-1 << -rLen) );
-        pic16_emitcode ("orl","a,b");
+        DEBUGpic16_emitcode(";", "rLen = %i", rLen);
+        pic16_emitpcode(POC_ANDLW, pic16_popGetLit((unsigned char)-1 << -rLen));
+        pic16_emitpcode(POC_IORFW, pic16_popCopyReg(&pic16_pc_prodh));
+//        pic16_emitcode ("anl","a,#0x%02x",((unsigned char)-1 << -rLen) );
+//        pic16_emitcode ("orl","a,b");
     }
 
-    if (p_type == GPOINTER)
-        pic16_emitcode("pop","b");
+//    if (p_type == GPOINTER)
+//        pic16_emitcode("pop","b");
 
     switch (p_type) {
 
-    case POINTER:
-       pic16_emitcode("mov","@%s,a",rname);
+      case POINTER:
+        pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+//     pic16_emitcode("mov","@%s,a",rname);
        break;
-       
-    case FPOINTER:
+/*
+      case FPOINTER:
        pic16_emitcode("movx","@dptr,a");
        break;
        
-    case GPOINTER:
+      case GPOINTER:
        DEBUGpic16_emitcode(";lcall","__gptrput");
        break;                  
+*/
+      default:
+        assert(0);
     }
+    
+//    pic16_freeAsmop(right, NULL, ic, TRUE);
 }
 /*-----------------------------------------------------------------*/
 /* genDataPointerSet - remat pointer to data space                 */
@@ -11408,7 +11713,7 @@ static void genDataPointerSet(operand *right,
                       pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),offset)); // patch 8
                     }
                 } else {
-                  mov2w(AOP(right), offset);
+                  pic16_mov2w(AOP(right), offset);
                   pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); // patch 8
                 }
                offset++;
@@ -11428,7 +11733,6 @@ static void genNearPointerSet (operand *right,
                                iCode *ic)
 {
   asmop *aop = NULL;
-  char *l;
   sym_link *retype;
   sym_link *ptype = operandType(result);
   sym_link *resetype;
@@ -11460,33 +11764,15 @@ static void genNearPointerSet (operand *right,
 
        /* 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( (AOP_TYPE(result) == AOP_PCODE) 
-//                     && ((AOP(result)->aopu.pcop->type == PO_IMMEDIATE)
-//                             || (AOP(result)->aopu.pcop->type == PO_DIR))) // patch 10
-                if(is_LitAOp( AOP(result) ))
-               {
-                 if(!IS_BITFIELD(resetype))
-                       pic16_loadFSR0( result );  // patch 10
-               } else {
-                 if(!IS_BITFIELD(resetype)) {
-                       // set up FSR0 with address of result
-                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10
-                       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10
-                  }
-               }
+       if (1 || !AOP_INPREG(AOP(result))) {  // AOP_INPREG(AOP(result)) is not always correct...
+         /* otherwise get a free pointer register */
+         DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-       }
-//     else
-//     rname = pic16_aopGet(AOP(result),0,FALSE,FALSE);
+         ;
+        }
 
        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-//     pic16_loadFSR0( result );
-
        /* if bitfield then unpack the bits */
        if (IS_BITFIELD(resetype)) {
                genPackBits (resetype, result, right, NULL, POINTER);
@@ -11495,68 +11781,62 @@ static void genNearPointerSet (operand *right,
          int size = AOP_SIZE(right);
          int offset = 0 ;    
 
+           pic16_loadFSR0(result, 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) {                                                       // patch 10
-                                       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)));            //
-                                       }                                                                               //
-                               }                                                                                       // patch 10
-                       }
+                               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++;
                }
-       }
+        }
 
        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);
+         /* 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);
-               }
-       }
+         /* 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 */
+    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    /* done */
 //release:
-       pic16_freeAsmop(right,NULL,ic,TRUE);
-       pic16_freeAsmop(result,NULL,ic,TRUE);
+    pic16_freeAsmop(right,NULL,ic,TRUE);
+    pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -11822,7 +12102,6 @@ static void genGenPointerSet (operand *right,
 {
   int size;
   sym_link *retype = getSpec(operandType(right));
-  char fgptrput[32];
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
@@ -11865,27 +12144,7 @@ static void genGenPointerSet (operand *right,
                                 pic16_popCopyReg(&pic16_pc_prodl)));
     pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(result), 2));
     
-
-    /* put code here */
-    switch (size) {
-      case 1: strcpy(fgptrput, "__gptrput1"); break;
-      case 2: strcpy(fgptrput, "__gptrput2"); break;
-      case 3: strcpy(fgptrput, "__gptrput3"); break;
-      case 4: strcpy(fgptrput, "__gptrput4"); break;
-      default:
-        werror(W_POSSBUG2, __FILE__, __LINE__);
-        abort();
-    }
-    
-    pic16_emitpcode(POC_CALL, pic16_popGetWithString( fgptrput ));
-    
-    {
-      symbol *sym;
-                  
-        sym = newSymbol( fgptrput, 0 );
-        strcpy(sym->rname, fgptrput);
-        checkAddSym(&externs, sym);
-    }
+    pic16_callGenericPointerRW(1, size);
 
 release:
     pic16_freeAsmop(right,NULL,ic,TRUE);
@@ -11937,27 +12196,26 @@ static void genPointerSet (iCode *ic)
     /* now that we have the pointer type we assign
     the pointer values */
     switch (p_type) {
+      case POINTER:
+      case IPOINTER:
+        genNearPointerSet (right,result,ic);
+        break;
 
-    case POINTER:
-    case IPOINTER:
-       genNearPointerSet (right,result,ic);
-       break;
-
-    case PPOINTER:
-       genPagedPointerSet (right,result,ic);
-       break;
-
-    case FPOINTER:
-       genFarPointerSet (right,result,ic);
+      case PPOINTER:
+        genPagedPointerSet (right,result,ic);
        break;
 
-    case GPOINTER:
-       genGenPointerSet (right,result,ic);
-       break;
+      case FPOINTER:
+        genFarPointerSet (right,result,ic);
+        break;
+        
+      case GPOINTER:
+        genGenPointerSet (right,result,ic);
+        break;
 
-    default:
-      werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
-             "genPointerSet: illegal pointer type");
+      default:
+        werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
+          "genPointerSet: illegal pointer type");
     }
 }
 
@@ -11988,7 +12246,7 @@ static void genIfx (iCode *ic, iCode *popIc)
     /* if the condition is  a bit variable */
     if (isbit && IS_ITEMP(cond) && 
         SPIL_LOC(cond)) {
-      genIfxJump(ic,SPIL_LOC(cond)->rname);
+      genIfxJump(ic,"c");
       DEBUGpic16_emitcode ("; isbit  SPIL_LOC","%s",SPIL_LOC(cond)->rname);
     } else {
       if (isbit && !IS_ITEMP(cond))
@@ -12019,21 +12277,27 @@ static void genAddrOf (iCode *ic)
       /* get address of symbol on stack */
       DEBUGpic16_emitcode(";    ", "%s symbol %s on stack", __FUNCTION__, sym->name);
 #if 0
-      fprintf(stderr, "%s:%d symbol %s on stack offset %d\n", __FILE__, __LINE__,
-                  OP_SYMBOL(left)->name, OP_SYMBOL(left)->stack);
+      fprintf(stderr, "%s:%d symbol %s on stack offset %i\n", __FILE__, __LINE__,
+                  OP_SYMBOL(IC_LEFT(ic))->name, OP_SYMBOL(IC_LEFT(ic))->stack);
 #endif
 
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
-                      pic16_popCopyReg(&pic16_pc_fsr2l),
-                      pic16_popGet(AOP(result), 0)));
-      pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
-                      pic16_popCopyReg(&pic16_pc_fsr2h),
-                      pic16_popGet(AOP(result), 1)));
-      
-      pic16_emitpcode(POC_MOVLW, pic16_popGetLit( OP_SYMBOL( IC_LEFT(ic))->stack ) /*+ _G.stack_lat*/);
-      pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result), 0));
-      emitSKPNC;
-      pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result), 1));
+      // operands on stack are accessible via "FSR2 + index" with index
+      // starting at 2 for arguments and growing from 0 downwards for
+      // local variables (index == 0 is not assigned so we add one here)
+      {
+       int soffs = OP_SYMBOL( IC_LEFT(ic))->stack;
+       if (soffs <= 0) {
+         assert (soffs < 0);
+         soffs++;
+       } // if
+       DEBUGpic16_emitcode("*!*", "accessing stack symbol at offset=%d", soffs);
+       pic16_emitpcode(POC_MOVLW , pic16_popGetLit( soffs & 0x00FF ));
+       pic16_emitpcode(POC_ADDFW , pic16_popCopyReg(pic16_framepnt_lo));
+       pic16_emitpcode(POC_MOVWF , pic16_popGet(AOP(result), 0));
+       pic16_emitpcode(POC_MOVLW , pic16_popGetLit( (soffs >> 8) & 0x00FF ));
+       pic16_emitpcode(POC_ADDFWC, pic16_popCopyReg(pic16_framepnt_hi));
+       pic16_emitpcode(POC_MOVWF , pic16_popGet(AOP(result), 1));
+      }
 
       goto release;
     }
@@ -12046,10 +12310,6 @@ static void genAddrOf (iCode *ic)
     pic16_aopOp((left=IC_LEFT(ic)), ic, FALSE);
     size = AOP_SIZE(IC_RESULT(ic));
 
-
-    /* Assume that what we want the address of is in data space
-     * since there is no stack on the PIC, yet! -- VR */
-  
     pcop0 = PCOP(pic16_newpCodeOpImmd(sym->rname, 0, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
     pcop1 = PCOP(pic16_newpCodeOpImmd(sym->rname, 1, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
     pcop2 = PCOP(pic16_newpCodeOpImmd(sym->rname, 2, 0, IN_CODESPACE( SPEC_OCLS(sym->etype))));
@@ -12207,6 +12467,16 @@ static void genAssign (iCode *ic)
 //  fprintf(stderr, "%s:%d: assigning value 0x%04lx (%d:%d)\n", __FUNCTION__, __LINE__, lit,
 //                     sizeof(unsigned long int), sizeof(float));
 
+
+  if (AOP_TYPE(right) == AOP_REG) {
+    DEBUGpic16_emitcode(";   ", "%s:%d assign from register\n", __FUNCTION__, __LINE__);
+    while (size--) {
+      
+      pic16_emitpcode (POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset++));
+    } // while
+    goto release;
+  }
+
   if(AOP_TYPE(right) != AOP_LIT
        && IN_CODESPACE(SPEC_OCLS(OP_SYMBOL(right)->etype))
        && !IS_FUNC(OP_SYM_TYPE(right))
@@ -12233,7 +12503,7 @@ static void genAssign (iCode *ic)
                                pic16_popCopyReg(&pic16_pc_tblptru)));
        }
 
-       size = min(AOP_SIZE(right), AOP_SIZE(result));
+       size = min(getSize(OP_SYM_ETYPE(right)), AOP_SIZE(result));
        while(size--) {
                pic16_emitpcodeNULLop(POC_TBLRD_POSTINC);
                pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_tablat),
@@ -12241,8 +12511,9 @@ static void genAssign (iCode *ic)
                offset++;
        }
 
-       if(AOP_SIZE(result) > AOP_SIZE(right)) {
-               size = AOP_SIZE(result) - AOP_SIZE(right);
+       size = getSize(OP_SYM_ETYPE(right));
+       if(AOP_SIZE(result) > size) {
+               size = AOP_SIZE(result) - size;
                while(size--) {
                        pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), offset));
                        offset++;
@@ -12288,8 +12559,8 @@ static void genAssign (iCode *ic)
     } else if (AOP_TYPE(right) == AOP_CRY) {
       pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),offset));
       if(offset == 0) {
-//        debugf("%s: BTFSS offset == 0\n", __FUNCTION__);
-       pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(right),0));
+        //debugf("%s: BTFSS offset == 0\n", __FUNCTION__);
+       pic16_emitpcode(POC_BTFSC, pic16_popGet(AOP(right),0));
        pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result),0));
       }
     } else if ( (AOP_TYPE(right) == AOP_PCODE) && (AOP(right)->aopu.pcop->type == PO_IMMEDIATE) ) {
@@ -12383,10 +12654,12 @@ static void genJumpTab (iCode *ic)
     pic16_emitpcode(POC_MOVWF , pic16_popCopyReg(&pic16_pc_pcl));
 
     pic16_emitpLabelFORCE(jtab->key);
-
 #endif
+
     pic16_freeAsmop(IC_JTCOND(ic),NULL,ic,TRUE);
+//          pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_ENTRY_BEGIN));
 
+    pic16_emitpinfo (INF_OPTIMIZATION, pic16_newpCodeOpOpt (OPT_JUMPTABLE_BEGIN, ""));
     /* now generate the jump labels */
     for (jtab = setFirstItem(IC_JTLABELS(ic)) ; jtab;
          jtab = setNextItem(IC_JTLABELS(ic))) {
@@ -12394,6 +12667,7 @@ static void genJumpTab (iCode *ic)
        pic16_emitpcode(POC_GOTO,pic16_popGetLabel(jtab->key));
        
     }
+    pic16_emitpinfo (INF_OPTIMIZATION, pic16_newpCodeOpOpt (OPT_JUMPTABLE_END, ""));
 
 }
 
@@ -12547,8 +12821,8 @@ static void genCast (iCode *ic)
 //     if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic)))
 //             return ;
 
-       pic16_aopOp(right,ic,FALSE) ;
        pic16_aopOp(result,ic,FALSE);
+       pic16_aopOp(right,ic,FALSE) ;
 
        DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result);
 
@@ -12775,7 +13049,13 @@ static void genCast (iCode *ic)
                break;
 
             case GPOINTER:
-              pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), GPTRSIZE-1));
+               if (GPTRSIZE > AOP_SIZE(right)) {
+                 // assume data pointer... THIS MIGHT BE WRONG!
+                 pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80));
+                 pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), GPTRSIZE - 1));
+               } else {
+                 pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), GPTRSIZE-1));
+               }
               break;
               
            default:
@@ -12788,6 +13068,8 @@ static void genCast (iCode *ic)
            goto release ;
        }
        
+       
+       assert( 0 );
        /* just copy the pointers */
        size = AOP_SIZE(result);
        offset = 0 ;
@@ -12815,8 +13097,10 @@ static void genCast (iCode *ic)
     /* we move to result for the size of source */
     size = AOP_SIZE(right);
     offset = 0 ;
+
     while (size--) {
-      mov2f(AOP(result), AOP(right), offset);
+      if(!_G.resDirect)
+        mov2f(AOP(result), AOP(right), offset);
       offset++;
     }
 
@@ -12951,10 +13235,14 @@ static void genReceive (iCode *ic)
       }
   } else {
     DEBUGpic16_emitcode ("; ***","2 %s  %d argreg = %d",__FUNCTION__,__LINE__, SPEC_ARGREG(OP_SYM_ETYPE(IC_RESULT(ic)) ));
-
     _G.accInUse++;
     pic16_aopOp(IC_RESULT(ic),ic,FALSE);  
     _G.accInUse--;
+
+    /* set pseudo stack pointer to where it should be - dw*/
+    GpsuedoStkPtr = ic->parmBytes;
+
+    /* setting GpsuedoStkPtr has side effects here: */
     assignResultValue(IC_RESULT(ic), 0);
   }
 
@@ -12967,10 +13255,26 @@ static void genReceive (iCode *ic)
 static void
 genDummyRead (iCode * ic)
 {
-  pic16_emitcode ("; genDummyRead","");
-  pic16_emitcode ("; not implemented","");
+  operand *op;
+  int i;
 
-  ic = ic;
+  op = IC_RIGHT(ic);
+  if (op && IS_SYMOP(op)) {
+    if (IN_CODESPACE(SPEC_OCLS(OP_SYM_ETYPE(op)))) {
+      fprintf (stderr, "%s: volatile symbols in codespace?!? -- might go wrong...\n", __FUNCTION__);
+      return;
+    }
+    pic16_aopOp (op, ic, FALSE);
+    for (i=0; i < AOP_SIZE(op); i++) {
+      // may need to protect this from the peepholer -- this is not nice but works...
+      pic16_addpCode2pBlock(pb,pic16_newpCodeAsmDir(";", "VOLATILE READ - BEGIN"));
+      pic16_mov2w (AOP(op),i);
+      pic16_addpCode2pBlock(pb,pic16_newpCodeAsmDir(";", "VOLATILE READ - END"));
+    } // for i
+    pic16_freeAsmop (op, NULL, ic, TRUE);
+  } else if (op) {
+    fprintf (stderr, "%s: not implemented for non-symbols (volatile operand might not be read)\n", __FUNCTION__);
+  } // if
 }
 
 /*-----------------------------------------------------------------*/
@@ -13006,7 +13310,7 @@ void genpic16Code (iCode *lic)
 
     for (ic = lic ; ic ; ic = ic->next ) {
 
-      DEBUGpic16_emitcode(";ic ", "\t%c 0x%x",ic->op, ic->op);
+      DEBUGpic16_emitcode(";ic ", "\t%c 0x%x\t(%s)",ic->op, ic->op, pic16_decodeOp(ic->op));
       if ( cln != ic->lineno ) {
         if ( options.debug ) {
           debugFile->writeCLine (ic);