* src/pic16/gen.c (genFunction, genEndFunction): fixed return
[fw/sdcc] / src / pic16 / gen.c
index 1d43e8884c5917a07b057b4e3da7079c83feac72..6d6e8bd669bb16b4cb2dfc2b47d77e2bae6c0b32 100644 (file)
@@ -6,6 +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)
   
   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
 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 *);
+void pic16_genMult16X16_16(operand *, operand *, operand *);
+void pic16_genMult32X32_32(operand *, operand *, operand *);
 pCode *pic16_AssembleLine(char *line, int peeps);
 extern void pic16_printpBlock(FILE *of, pBlock *pb);
 static asmop *newAsmop (short type);
-static pCodeOp *pic16_popRegFromString(char *str, int size, int offset);
+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 int aopIdx (asmop *aop, int offset);
 
-static int labelOffset=0;
+int pic16_labelOffset=0;
 extern int pic16_debug_verbose;
 static int optimized_for_speed = 0;
 /*
@@ -73,6 +76,8 @@ static int max_key=0;
 static int GpsuedoStkPtr=0;
 
 pCodeOp *pic16_popGetImmd(char *name, unsigned int offset, int index);
+pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst);
+
 unsigned int pic16aopLiteral (value *val, int offset);
 const char *pic16_AopType(short type);
 static iCode *ifxForOp ( operand *op, iCode *ic );
@@ -80,6 +85,8 @@ static iCode *ifxForOp ( operand *op, iCode *ic );
 void pic16_pushpCodeOp(pCodeOp *pcop);
 void pic16_poppCodeOp(pCodeOp *pcop);
 
+static bool is_LitOp(operand *op);
+static bool is_LitAOp(asmop *aop);
 
 
 #define BYTEofLONG(l,b) ( (l>> (b<<3)) & 0xff)
@@ -117,11 +124,15 @@ static char *accUse[] = {"WREG"};
 static struct {
     short r0Pushed;
     short r1Pushed;
+    short fsr0Pushed;
     short accInUse;
     short inLine;
     short debugLine;
     short nRegsSaved;
+    short ipushRegs;
     set *sendSet;
+    int interruptvector;
+    int usefastretfie;
 } _G;
 
 /* Resolved ifx structure. This structure stores information
@@ -264,7 +275,12 @@ void DEBUGpic16_emitcode (char *inst,char *fmt, ...)
 
 void pic16_emitpLabel(int key)
 {
-  pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(NULL,key+100+labelOffset));
+  pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(NULL,key+100+pic16_labelOffset));
+}
+
+void pic16_emitpLabelFORCE(int key)
+{
+  pic16_addpCode2pBlock(pb,pic16_newpCodeLabelFORCE(NULL,key+100+pic16_labelOffset));
 }
 
 void pic16_emitpcode(PIC_OPCODE poc, pCodeOp *pcop)
@@ -328,16 +344,79 @@ void pic16_emitcode (char *inst,char *fmt, ...)
 #endif
 
 
+/*-----------------------------------------------------------------*/
+/* pic16_emitDebuggerSymbol - associate the current code location  */
+/*   with a debugger symbol                                        */
+/*-----------------------------------------------------------------*/
+void
+pic16_emitDebuggerSymbol (char * debugSym)
+{
+  _G.debugLine = 1;
+  pic16_emitcode (";", "%s ==.", debugSym);
+  _G.debugLine = 0;
+}
+
+
 /*-----------------------------------------------------------------*/
 /* getFreePtr - returns r0 or r1 whichever is free or can be pushed*/
 /*-----------------------------------------------------------------*/
 static regs *getFreePtr (iCode *ic, asmop **aopp, bool result)
 {
-    bool r0iu = FALSE , r1iu = FALSE;
-    bool r0ou = FALSE , r1ou = FALSE;
+//    bool r0iu = FALSE , r1iu = FALSE;
+//    bool r0ou = FALSE , r1ou = FALSE;
+    bool fsr0iu = FALSE, fsr0ou;
+    bool fsr2iu = FALSE, fsr2ou;
+    
+    fprintf(stderr, "%s:%d: getting free ptr from ic = %c result: %d\n", __FUNCTION__, __LINE__, ic->op, result);
+
+    
+    fsr2iu = bitVectBitValue(ic->rUsed, IDX_FSR2);
+    fsr0iu = bitVectBitValue(ic->rUsed, IDX_FSR0);
+    
+    fsr2ou = bitVectBitValue(ic->rMask, IDX_FSR2);
+    fsr0ou = bitVectBitValue(ic->rMask, IDX_FSR0);
+
+    if(bitVectBitValue(ic->rUsed, IDX_WREG)) {
+       fprintf(stderr, "%s:%d WREG is used by this ic\n", __FILE__, __LINE__);
+       DEBUGpic16_emitcode("%s:%d WREG is used by this ic", __FILE__, __LINE__);
+    }
+
+    /* no usage of FSR2 */
+    if(!fsr2iu && !fsr2ou) {
+       ic->rUsed = bitVectSetBit(ic->rUsed, IDX_FSR2);
+       (*aopp)->type = AOP_FSR2;
+
+      return (*aopp)->aopu.aop_ptr = pic16_regWithIdx(IDX_FSR2);
+    }
+       
+    if(!fsr0iu && !fsr0ou) {
+       ic->rUsed = bitVectSetBit(ic->rUsed, IDX_FSR0);
+       (*aopp)->type = AOP_FSR0;
+       
+      return ((*aopp)->aopu.aop_ptr = pic16_regWithIdx(IDX_FSR0));
+    }
+
+    /* now we know they both have usage */
+    /* if fsr0 not used in this instruction */
+    if (!fsr0iu) {
+       if (!_G.fsr0Pushed) {
+               pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_fsr0l) );
+               pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_fsr0h) );
+               _G.fsr0Pushed++;
+       }
+
+       ic->rUsed = bitVectSetBit (ic->rUsed, IDX_FSR0);
+       (*aopp)->type = AOP_FSR0;
+
+      return (*aopp)->aopu.aop_ptr = pic16_regWithIdx (IDX_FSR0);
+    }
+       
 
-       //fprintf(stderr, "%s:%d: getting free ptr from ic = %c\n", __FUNCTION__, __LINE__, ic->op);
+    fprintf(stderr, "%s:%d could not allocate a free pointer\n", __FILE__, __LINE__);
+    assert( 0 );
 
+    return NULL;
+#if 0
     /* the logic: if r0 & r1 used in the instruction
     then we are in trouble otherwise */
 
@@ -411,6 +490,7 @@ endOfWorld :
     werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
            "getFreePtr should never reach here");
     exit(0);
+#endif
 }
 
 /*-----------------------------------------------------------------*/
@@ -445,20 +525,24 @@ static void genSetDPTR(int n)
 /*-----------------------------------------------------------------*/
 static void resolveIfx(resolvedIfx *resIfx, iCode *ifx)
 {
+
+  DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__);
+
   if(!resIfx) 
     return;
 
-  //  DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__);
 
   resIfx->condition = 1;    /* assume that the ifx is true */
   resIfx->generated = 0;    /* indicate that the ifx has not been used */
 
   if(!ifx) {
     resIfx->lbl = newiTempLabel(NULL);  /* oops, there is no ifx. so create a label */
-/*
+
+#if 1
     DEBUGpic16_emitcode("; ***","%s %d null ifx creating new label key =%d",
                        __FUNCTION__,__LINE__,resIfx->lbl->key);
-*/
+#endif
+
   } else {
     if(IC_TRUE(ifx)) {
       resIfx->lbl = IC_TRUE(ifx);
@@ -466,15 +550,16 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx)
       resIfx->lbl = IC_FALSE(ifx);
       resIfx->condition = 0;
     }
-/*
+
+#if 1
     if(IC_TRUE(ifx)) 
       DEBUGpic16_emitcode("; ***","ifx true is non-null");
     if(IC_FALSE(ifx)) 
       DEBUGpic16_emitcode("; ***","ifx false is non-null");
-*/
+#endif
   }
 
-  //  DEBUGpic16_emitcode("; ***","%s lbl->key=%d, (lab offset=%d)",__FUNCTION__,resIfx->lbl->key,labelOffset);
+  DEBUGpic16_emitcode("; ***","%s lbl->key=%d, (lab offset=%d)",__FUNCTION__,resIfx->lbl->key,pic16_labelOffset);
 
 }
 #if 0
@@ -488,32 +573,77 @@ static int pointerCode (sym_link *etype)
 
 }
 #endif
+
 /*-----------------------------------------------------------------*/
 /* aopForSym - for a true symbol                                   */
 /*-----------------------------------------------------------------*/
-static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
+static asmop *aopForSym (iCode *ic, operand *op, bool result)
 {
+    symbol *sym=OP_SYMBOL(op);
     asmop *aop;
     memmap *space= SPEC_OCLS(sym->etype);
 
     DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__);
+
+//    sym = OP_SYMBOL(op);
+
     /* if already has one */
     if (sym->aop) {
            DEBUGpic16_emitcode("; ***", "already has sym %s %d", __FUNCTION__, __LINE__);
         return sym->aop;
     }
 
+    /* if symbol was initially placed onStack then we must re-place it
+     * to direct memory, since pic16 does not have a specific stack */
+    if(sym->onStack) {
+       fprintf(stderr, "%s:%d symbol %s on stack\n", __FILE__, __LINE__, OP_SYMBOL(op)->name);
+    }
+
+
+#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 && !options.stack10bit) || sym->iaccess) {
 
-       DEBUGpic16_emitcode("; ***", "sum->onStack || sym->iaccess");
+       DEBUGpic16_emitcode("; ***", "%s:%d sym->onStack:%d || sym->iaccess:%d",
+               __FUNCTION__, __LINE__, sym->onStack, sym->iaccess);
        
         sym->aop = aop = newAsmop(0);
         aop->aopu.aop_ptr = getFreePtr(ic,&aop,result);
         aop->size = getSize(sym->type);
 
+        fprintf(stderr, "%s:%d\t%s\n", __FILE__, __LINE__, __FUNCTION__);
+
+#if 1
+//        sym->aop = aop = newAsmop (AOP_REG);
+//        aop->aopu.aop_dir = sym->name;       //sym->rname ;
+//     aop->aopu.aop_reg[0] = pic16_regWithIdx(IDX_PLUSW0);    //pic16_pc_plusw2.r;
+//        aop->size = getSize(sym->type);
+       DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d",__LINE__,sym->rname,aop->size);
+       
+//     if(_G.accInUse) {
+//             pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
+//     }
+       
+//     pic16_emitpcode(POC_MOVFF, pic16_popGet2p( pic16_popCopyReg(&pic16_pc_fsr2l), pic16_popCopyReg(&pic16_pc_fsr0l)));
+//     pic16_emitpcode(POC_MOVFF, pic16_popGet2p( pic16_popCopyReg(&pic16_pc_fsr2h), pic16_popCopyReg(&pic16_pc_fsr0h)));
+       
+       
+       /* initialise for stack access via frame pointer */
+       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack));
+
+//     pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
+//             pic16_popCopyReg(&pic16_pc_plusw2), pic16_popCopyReg(&pic16_pc_kzero)));
+       
+//     if(_G.accInUse) {
+//             pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) );
+//     }
+       
+       return (aop);
+#endif
+
+#if 0
         /* now assign the address of the variable to 
         the pointer register */
         if (aop->type != AOP_STK) {
@@ -540,8 +670,12 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
         } else
             aop->aopu.aop_stk = sym->stack;
         return aop;
+#endif
+
     }
-    
+#endif
+
+#if 0
     if (sym->onStack && options.stack10bit)
     {
         /* It's on the 10 bit stack, which is located in
@@ -572,7 +706,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
        aop->size = getSize(sym->type); 
        return aop;
     }
-
+#endif
     //DEBUGpic16_emitcode(";","%d",__LINE__);
     /* if in bit space */
     if (IN_BITSPACE(space)) {
@@ -592,6 +726,15 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
         return aop;
     }
 
+    if (IN_FARSPACE(space)) {
+        sym->aop = aop = newAsmop (AOP_DIR);
+        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;
+    }
+
 #if 0                                                                                          // patch 14
     /* special case for a function */
     if (IS_FUNC(sym->type)) {   
@@ -617,7 +760,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
                PCOI(aop->aopu.pcop)->index = 0;
        } else {
                /* try to allocate via direct register */
-               aop->aopu.pcop = pic16_popRegFromString(sym->rname, getSize(sym->type), sym->offset); // Patch 8
+               aop->aopu.pcop = pic16_popRegFromString(sym->rname, getSize(sym->type), sym->offset, op); // Patch 8
 //             aop->size = getSize( sym->type );
        }
 
@@ -631,7 +774,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
 
        if(IN_DIRSPACE( space ))
                aop->size = PTRSIZE;
-       else if(IN_CODESPACE( space ))
+       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) );
@@ -692,7 +835,7 @@ static asmop *aopForRemat (operand *op) // x symbol *sym)
        if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(op)))
                || viaimmd) {
 
-               pic16_emitpcomment("%s:%d immediate", __FILE__, __LINE__);
+               DEBUGpic16_emitcode("%s:%d immediate", __FILE__, __LINE__);
 
                aop->aopu.pcop = pic16_popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname, 0, val);
 
@@ -704,9 +847,10 @@ static asmop *aopForRemat (operand *op) // x symbol *sym)
 
                PCOI(aop->aopu.pcop)->index = val;
        } else {
-               pic16_emitpcomment("%s:%d dir", __FILE__, __LINE__);
+               DEBUGpic16_emitcode("%s:%d dir", __FILE__, __LINE__);
 
-               aop->aopu.pcop = pic16_popRegFromString(OP_SYMBOL(IC_LEFT(ic))->rname, getSize( OP_SYMBOL( IC_LEFT(ic))->type), val);
+               aop->aopu.pcop = pic16_popRegFromString(OP_SYMBOL(IC_LEFT(ic))->rname,
+                               getSize( OP_SYMBOL( IC_LEFT(ic))->type), val, op);
 //             aop->size = AOP_SIZE( IC_LEFT(ic) );
        }
 
@@ -799,7 +943,8 @@ static bool operandsEqu ( operand *op1, operand *op2)
     if (sym1 == sym2)
         return TRUE ;
 
-    if (strcmp(sym1->rname,sym2->rname) == 0)
+    if (sym1->rname[0] && sym2->rname[0]
+        && strcmp (sym1->rname, sym2->rname) == 0)
         return TRUE;
 
 
@@ -830,6 +975,11 @@ bool pic16_sameRegs (asmop *aop1, asmop *aop2 )
     if (aop1 == aop2)
         return TRUE ;
 
+    DEBUGpic16_emitcode(";***", "%s aop1->type = %s\taop2->type = %s\n", __FUNCTION__,
+               pic16_AopType(aop1->type), pic16_AopType(aop2->type));
+
+    if(aop1->type == AOP_ACC && aop2->type == AOP_ACC)return TRUE;
+
     if (aop1->type != AOP_REG ||
         aop2->type != AOP_REG )
         return FALSE ;
@@ -891,7 +1041,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
     /* if this is a true symbol */
     if (IS_TRUE_SYMOP(op)) {    
        DEBUGpic16_emitcode(";","%d - true symop",__LINE__);
-      op->aop = aopForSym(ic,OP_SYMBOL(op),result);
+      op->aop = aopForSym(ic, op, result);
       return ;
     }
 
@@ -978,7 +1128,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result)
        //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);
+                                         sym->usl.spillLoc->offset, op);
         aop->size = getSize(sym->type);
 
         return;
@@ -1024,8 +1174,24 @@ void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop)
 
     /* depending on the asmop type only three cases need work AOP_RO
        , AOP_R1 && AOP_STK */
-#if 0
+#if 1
     switch (aop->type) {
+        case AOP_FSR0 :
+            if (_G.fsr0Pushed ) {
+                if (pop) {
+                   pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_fsr0h) );
+                   pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_fsr0l) );
+//                    pic16_emitcode ("pop","ar0");
+                    _G.fsr0Pushed--;
+                }
+            }
+            bitVectUnSetBit(ic->rUsed,IDX_FSR0);
+            break;
+
+        case AOP_FSR2 :
+            bitVectUnSetBit(ic->rUsed,IDX_FSR2);
+            break;
+
         case AOP_R0 :
             if (_G.r0Pushed ) {
                 if (pop) {
@@ -1123,65 +1289,39 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
 
     /* depending on type */
     switch (aop->type) {
-       
-    case AOP_R0:
-    case AOP_R1:
-        DEBUGpic16_emitcode(";","%d",__LINE__);
-       /* if we need to increment it */       
-       while (offset > aop->coff) {        
-           pic16_emitcode ("inc","%s",aop->aopu.aop_ptr->name);  
-           aop->coff++;
-       }
-       
-       while (offset < aop->coff) {
-           pic16_emitcode("dec","%s",aop->aopu.aop_ptr->name);
-           aop->coff--;
-       }
-       
-       aop->coff = offset ;
-       if (aop->paged) {
-           pic16_emitcode("movx","a,@%s",aop->aopu.aop_ptr->name);
-           return (dname ? "acc" : "a");
-       }       
-       sprintf(s,"@%s",aop->aopu.aop_ptr->name);
-       rs = Safe_calloc(1,strlen(s)+1);
-       strcpy(rs,s);   
-       return rs;
-       
-    case AOP_DPTR:
-    case AOP_DPTR2:
-        DEBUGpic16_emitcode(";","%d",__LINE__);
-    if (aop->type == AOP_DPTR2)
-    {
-        genSetDPTR(1);
-    }
-    
-       while (offset > aop->coff) {
-           pic16_emitcode ("inc","dptr");
-           aop->coff++;
-       }
-       
-       while (offset < aop->coff) {        
-           pic16_emitcode("lcall","__decdptr");
-           aop->coff--;
-       }
-       
-       aop->coff = offset;
-       if (aop->code) {
-           pic16_emitcode("clr","a");
-           pic16_emitcode("movc","a,@a+dptr");
+
+    case AOP_FSR0:
+    case AOP_FSR2:
+      sprintf(s, "%s", aop->aopu.aop_ptr->name);
+      rs = Safe_calloc(1, strlen(s)+1);
+      strcpy(rs, s);
+      return (rs);
+      
+#if 0
+      /* if we need to increment it */
+      while (offset > aop->coff)
+        {
+          emitcode ("inc", "%s", aop->aopu.aop_ptr->name);
+          aop->coff++;
         }
-    else {
-           pic16_emitcode("movx","a,@dptr");
-    }
-           
-    if (aop->type == AOP_DPTR2)
-    {
-        genSetDPTR(0);
-    }
-           
-    return (dname ? "acc" : "a");
-       
+
+      while (offset < aop->coff)
+        {
+          emitcode ("dec", "%s", aop->aopu.aop_ptr->name);
+          aop->coff--;
+        }
+      aop->coff = offset;
+      if (aop->paged)
+        {
+          emitcode ("movx", "a,@%s", aop->aopu.aop_ptr->name);
+          return (dname ? "acc" : "a");
+        }
+      sprintf (s, "@%s", aop->aopu.aop_ptr->name);
+      rs = Safe_calloc (1, strlen (s) + 1);
+      strcpy (rs, s);
+      return rs;
+#endif
+
        
     case AOP_IMMD:
        if (bit16) 
@@ -1222,10 +1362,12 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
       return aop->aopu.aop_dir;
        
     case AOP_ACC:
-        DEBUGpic16_emitcode(";Warning -pic port ignoring get(AOP_ACC)","%d",__LINE__);
+        DEBUGpic16_emitcode(";Warning -pic port ignoring get(AOP_ACC)","%d\toffset: %d",__LINE__, offset);
 //        fprintf(stderr, "%s:%d Warning -pic port ignoring get(AOP_ACC)\n",__FILE__, __LINE__);
 //        assert( 0 );
-       return aop->aopu.aop_str[offset];       //->"AOP_accumulator_bug";
+//     return aop->aopu.aop_str[offset];       //->"AOP_accumulator_bug";
+       rs = Safe_strdup("WREG");
+       return (rs);
 
     case AOP_LIT:
        sprintf(s,"0x%02x", pic16aopLiteral (aop->aopu.aop_lit,offset));
@@ -1258,8 +1400,13 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
       strcpy(rs,s);   
       return rs;
 
+      case AOP_STK:
+//        pCodeOp *pcop = aop->aop
+       break;
+
     }
 
+    fprintf(stderr, "%s:%d unsupported aop->type: %s\n", __FILE__, __LINE__, pic16_AopType(aop->type));
     werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
            "aopget got unsupported aop->type");
     exit(0);
@@ -1272,6 +1419,12 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname)
 pCodeOp *pic16_popGetTempReg(void)
 {
   pCodeOp *pcop;
+  symbol *cfunc;
+
+//     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
+       cfunc = currFunc;
+       currFunc = NULL;
 
        pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP);
        if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
@@ -1279,21 +1432,25 @@ pCodeOp *pic16_popGetTempReg(void)
                PCOR(pcop)->r->isFree=0;
 
                /* push value on stack */
-               pic16_pushpCodeOp( pcop );
+               pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) );
        }
 
+       currFunc = cfunc;
+
   return pcop;
 }
 
 /*-----------------------------------------------------------------*/
-/* pic16_popGetTempReg - create a new temporary pCodeOp                  */
+/* pic16_popReleaseTempReg - create a new temporary pCodeOp                  */
 /*-----------------------------------------------------------------*/
 void pic16_popReleaseTempReg(pCodeOp *pcop)
 {
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
        if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) {
                PCOR(pcop)->r->isFree = 1;
                
-               pic16_poppCodeOp( pcop );
+               pic16_poppCodeOp( pic16_pCodeOpCopy(pcop) );
        }
 }
 /*-----------------------------------------------------------------*/
@@ -1302,12 +1459,12 @@ void pic16_popReleaseTempReg(pCodeOp *pcop)
 pCodeOp *pic16_popGetLabel(unsigned int key)
 {
 
-  DEBUGpic16_emitcode ("; ***","%s  key=%d, label offset %d",__FUNCTION__,key, labelOffset);
+  DEBUGpic16_emitcode ("; ***","%s  key=%d, label offset %d",__FUNCTION__,key, pic16_labelOffset);
 
   if(key>max_key)
     max_key = key;
 
-  return pic16_newpCodeOpLabel(NULL,key+100+labelOffset);
+  return pic16_newpCodeOpLabel(NULL,key+100+pic16_labelOffset);
 }
 
 /*-----------------------------------------------------------------*/
@@ -1381,7 +1538,7 @@ pCodeOp *pic16_popGetWithString(char *str)
 /*-----------------------------------------------------------------*/
 /* pic16_popRegFromString -                                        */
 /*-----------------------------------------------------------------*/
-static pCodeOp *pic16_popRegFromString(char *str, int size, int offset)
+static pCodeOp *pic16_popRegFromString(char *str, int size, int offset, operand *op)
 {
 
   pCodeOp *pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
@@ -1402,9 +1559,10 @@ static pCodeOp *pic16_popRegFromString(char *str, int size, int offset)
   if(PCOR(pcop)->r == NULL) {
 //     fprintf(stderr, "%s:%d - couldn't find %s in allocated regsters, size= %d ofs= %d\n",
 //             __FUNCTION__, __LINE__, str, size, offset);
-    PCOR(pcop)->r = pic16_allocRegByName (pcop->name,size);
 
-       //fprintf(stderr, "allocating new register -> %s\n", str);
+
+//    PCOR(pcop)->r = pic16_allocRegByName (pcop->name,size, op);
+       fprintf(stderr, "%s:%d: WARNING: need to allocate new register by name -> %s\n", __FILE__, __LINE__, str);
 
 //    DEBUGpic16_emitcode(";","%d  %s   size= %d offset=%d - had to alloc by reg name",__LINE__,pcop->name,size,offset);
   } else {
@@ -1424,6 +1582,7 @@ static pCodeOp *pic16_popRegFromIdx(int rIdx)
        pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
        PCOR(pcop)->rIdx = rIdx;
        PCOR(pcop)->r = pic16_regWithIdx(rIdx);
+
        PCOR(pcop)->r->isFree = 0;
        PCOR(pcop)->r->wasUsed = 1;
 
@@ -1521,6 +1680,17 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        return NULL;
 
 
+    case AOP_FSR0:
+    case AOP_FSR2:
+      pcop = Safe_calloc(1, sizeof(pCodeOpReg));
+      PCOR(pcop)->rIdx = aop->aopu.aop_ptr->rIdx+2;    /* access PLUSW register */
+      PCOR(pcop)->r = pic16_regWithIdx( PCOR(pcop)->rIdx );
+      PCOR(pcop)->r->wasUsed = 1;
+      PCOR(pcop)->r->isFree = 0;
+      
+      PCOR(pcop)->instance = offset;
+      pcop->type = PCOR(pcop)->r->pc_type;
+      return (pcop);
 
     case AOP_IMMD:
       DEBUGpic16_emitcode(";","%d\tAOP_IMMD",__LINE__);
@@ -1555,7 +1725,7 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        
     case AOP_DIR:
       DEBUGpic16_emitcode(";","%d\tAOP_DIR", __LINE__);
-      return pic16_popRegFromString(aop->aopu.aop_dir, aop->size, offset);
+      return pic16_popRegFromString(aop->aopu.aop_dir, aop->size, offset, NULL);
        
     case AOP_REG:
       {
@@ -1564,22 +1734,23 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        DEBUGpic16_emitcode(";","%d\tAOP_REG", __LINE__);
        
        pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
+//     pcop->type = PO_GPR_REGISTER;
        PCOR(pcop)->rIdx = rIdx;
-       PCOR(pcop)->r = pic16_regWithIdx(rIdx);
+       PCOR(pcop)->r = pic16_allocWithIdx( rIdx );     //pic16_regWithIdx(rIdx);
        PCOR(pcop)->r->wasUsed=1;
        PCOR(pcop)->r->isFree=0;
 
        PCOR(pcop)->instance = offset;
        pcop->type = PCOR(pcop)->r->pc_type;
        rs = aop->aopu.aop_reg[offset]->name;
-       DEBUGpic16_emitcode(";","%d regiser idx = %d name =%s",__LINE__,rIdx,rs);
+       DEBUGpic16_emitcode(";","%d regiser idx = %d name = %s",__LINE__,rIdx,rs);
        return pcop;
       }
 
     case AOP_CRY:
        DEBUGpic16_emitcode(";","%d\tAOP_CRY", __LINE__);
 
-      pcop = pic16_newpCodeOpBit(aop->aopu.aop_dir,-1,1);
+      pcop = pic16_newpCodeOpBit(aop->aopu.aop_dir,-1,1, PO_GPR_REGISTER);
       PCOR(pcop)->r = pic16_dirregWithName(aop->aopu.aop_dir);
       //if(PCOR(pcop)->r == NULL)
       //fprintf(stderr,"%d - couldn't find %s in allocated registers\n",__LINE__,aop->aopu.aop_dir);
@@ -1862,16 +2033,15 @@ void pic16_aopPut (asmop *aop, char *s, int offset)
 static void mov2w (asmop *aop, int offset)
 {
 
-  if(!aop)
-    return;
+//  if(!aop)
+//    return;
 
-  DEBUGpic16_emitcode ("; ***","%s  %d  offset=%d",__FUNCTION__,__LINE__,offset);
+       DEBUGpic16_emitcode ("; ***","%s  %d  offset=%d",__FUNCTION__,__LINE__,offset);
 
-  if ( aop->type == AOP_PCODE ||
-       aop->type == AOP_LIT )
-    pic16_emitpcode(POC_MOVLW,pic16_popGet(aop,offset));
-  else
-    pic16_emitpcode(POC_MOVFW,pic16_popGet(aop,offset));
+       if(is_LitAOp(aop))
+               pic16_emitpcode(POC_MOVLW,pic16_popGet(aop,offset));
+       else
+               pic16_emitpcode(POC_MOVFW,pic16_popGet(aop,offset));
 
 }
 
@@ -1879,6 +2049,7 @@ static void mov2w (asmop *aop, int offset)
 /* push pcop into stack */
 void pic16_pushpCodeOp(pCodeOp *pcop)
 {
+//     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
        pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg(&pic16_pc_postdec1)));
 }
 
@@ -2041,19 +2212,31 @@ void pic16_outAcc(operand *result)
 }
 
 /*-----------------------------------------------------------------*/
-/* pic16_outBitC - output a bit C                                        */
+/* pic16_outBitC - output a bit C                                  */
+/*                 Move to result the value of Carry flag -- VR    */
 /*-----------------------------------------------------------------*/
 void pic16_outBitC(operand *result)
 {
+  int i;
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     /* if the result is bit */
-    if (AOP_TYPE(result) == AOP_CRY) 
+    if (AOP_TYPE(result) == AOP_CRY) {
+       fprintf(stderr, "%s:%d: pic16 port warning: unsupported case\n", __FILE__, __LINE__);
         pic16_aopPut(AOP(result),"c",0);
-    else {
+    } else {
+
+       i = AOP_SIZE(result);
+       while(i--) {
+               pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), i));
+       }
+       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result), 0));
+       
+/*
         pic16_emitcode("clr","a  ; %d", __LINE__);
         pic16_emitcode("rlc","a");
         pic16_outAcc(result);
+*/
     }
 }
 
@@ -2213,55 +2396,54 @@ static void genUminus (iCode *ic)
   int size, i;
   sym_link *optype, *rtype;
 
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       
+       /* assign asmops */
+       pic16_aopOp(IC_LEFT(ic),ic,FALSE);
+       pic16_aopOp(IC_RESULT(ic),ic,TRUE);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  /* assign asmops */
-  pic16_aopOp(IC_LEFT(ic),ic,FALSE);
-  pic16_aopOp(IC_RESULT(ic),ic,TRUE);
-
-  /* if both in bit space then special
-     case */
-  if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY &&
-      AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { 
-
-    pic16_emitpcode(POC_BCF,   pic16_popGet(AOP(IC_RESULT(ic)),0));
-    pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(IC_LEFT(ic)),0));
-    pic16_emitpcode(POC_BSF,   pic16_popGet(AOP(IC_RESULT(ic)),0));
+       /* if both in bit space then special case */
+       if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY
+               && AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { 
 
-    goto release; 
-  } 
+               pic16_emitpcode(POC_BCF,   pic16_popGet(AOP(IC_RESULT(ic)),0));
+               pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(IC_LEFT(ic)),0));
+               pic16_emitpcode(POC_BSF,   pic16_popGet(AOP(IC_RESULT(ic)),0));
+               
+               goto release; 
+       } 
 
-  optype = operandType(IC_LEFT(ic));
-  rtype = operandType(IC_RESULT(ic));
+       optype = operandType(IC_LEFT(ic));
+       rtype = operandType(IC_RESULT(ic));
 
-  /* if float then do float stuff */
-  if (IS_FLOAT(optype)) {
-    genUminusFloat(IC_LEFT(ic),IC_RESULT(ic));
-    goto release;
-  }
+       /* if float then do float stuff */
+       if (IS_FLOAT(optype)) {
+               genUminusFloat(IC_LEFT(ic),IC_RESULT(ic));
+               goto release;
+       }
 
-  /* otherwise subtract from zero by taking the 2's complement */
-  size = AOP_SIZE(IC_LEFT(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));
-    }
-  }
+       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));
-  }
+       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));
+       }
 
- release:
-  /* release the aops */
-  pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
-  pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);    
+release:
+       /* release the aops */
+       pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
+       pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);    
 }
 
 /*-----------------------------------------------------------------*/
@@ -2425,29 +2607,21 @@ static void assignResultValue(operand * oper, int rescall)
                        if(size>1) {
                                /* 16-bits, result in PRODL:WREG */
                                pic16_loadFromReturn(oper, 1, pic16_popCopyReg(&pic16_pc_prodl));
-//                             pic16_emitpcode(POC_MOVFF,
-//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_prodl), pic16_popGet(AOP(oper), 1)));
                        }
                        
                        if(size>2) {
                                /* 24-bits, result in PRODH:PRODL:WREG */
                                pic16_loadFromReturn(oper, 2, pic16_popCopyReg(&pic16_pc_prodh)); // patch 14
-
-//                             pic16_emitpcode(POC_MOVFF,
-//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_prodh), pic16_popGet(AOP(oper), 2)));
                        }
                        
                        if(size>3) {
                                /* 32-bits, result in FSR0L:PRODH:PRODL:WREG */
                                pic16_loadFromReturn(oper, 3, pic16_popCopyReg(&pic16_pc_fsr0l)); // patch14
-
-//                             pic16_emitpcode(POC_MOVFF,
-//                                     pic16_popGet2p(pic16_popCopyReg(&pic16_pc_fsr0l), pic16_popGet(AOP(oper), 3)));
                        }
                } else {
                        /* >32-bits, result on stack, and FSR0 points to beginning.
                         * Fix stack when done */
-                        
+                        /* FIXME FIXME */
                        while (size--) {
 //                             DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr);
 //                             DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2");
@@ -2500,8 +2674,22 @@ static void assignResultValue(operand * oper, int rescall)
 /*-----------------------------------------------------------------*/
 static void genIpush (iCode *ic)
 {
+  int size, offset=0;
 
   DEBUGpic16_emitcode ("; ***","%s  %d - WARNING no code generated",__FUNCTION__,__LINE__);
+
+
+       pic16_aopOp(IC_LEFT(ic), ic, FALSE);
+       
+
+       size = AOP_SIZE( IC_LEFT(ic) );
+       
+       while(size--) {
+               mov2w( AOP(IC_LEFT(ic)), offset );
+               pushw();
+               offset++;
+       }
+       
 #if 0
     int size, offset = 0 ;
     char *l;
@@ -2704,6 +2892,11 @@ static void genCall (iCode *ic)
 
                        saverbank(FUNC_REGBANK(dtype),ic,TRUE);
 
+
+       /* initialise stackParms for IPUSH pushes */
+//     stackParms = psuedoStkPtr;
+//     fprintf(stderr, "%s:%d ic parmBytes = %d\n", __FILE__, __LINE__, ic->parmBytes);
+
        /* if send set is not empty the assign */
        if (_G.sendSet) {
          iCode *sic;
@@ -2782,6 +2975,10 @@ static void genCall (iCode *ic)
                pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE);
        }
 
+       if(!stackParms && ic->parmBytes) {
+               stackParms = ic->parmBytes;
+       }
+       
        if(stackParms>0) {
                pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms));
                pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l ));
@@ -2793,7 +2990,7 @@ static void genCall (iCode *ic)
 
        /* adjust the stack for parameters if required */
 //     fprintf(stderr, "%s:%d: %s ic->parmBytes= %d\n", __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, ic->parmBytes);
-
+#if 0
        if (ic->parmBytes) {
          int i;
 
@@ -2805,7 +3002,9 @@ static void genCall (iCode *ic)
                        for ( i = 0 ; i <  ic->parmBytes ;i++)
                                pic16_emitcode("dec","%s",spname);
        }
+#endif
 
+#if 0
        /* if register bank was saved then pop them */
        if (ic->bankSaved)
                unsaverbank(FUNC_REGBANK(dtype),ic,TRUE);
@@ -2813,6 +3012,7 @@ static void genCall (iCode *ic)
        /* if we hade saved some registers then unsave them */
        if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype))
                unsaveRegisters (ic);
+#endif
 }
 
 
@@ -2906,9 +3106,7 @@ static void genPcall (iCode *ic)
        // push return address
        // push $ on return stack, then replace with retlbl
 
-       // Note: retlbl is supplied as dummy operand to PUSH
-       // This has the nice side effect of keeping the label from being optimized out :o)
-       pic16_emitpcode(POC_PUSH, pic16_popGetLabel(retlbl->key));      
+       pic16_emitpcodeNULLop(POC_PUSH);
 
        pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 0, 0));
        pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosl));
@@ -2918,17 +3116,22 @@ static void genPcall (iCode *ic)
        pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosu));
 
        /* make the call by writing the pointer into pc */
-// FIXME Disabled writes to PCLATU because of gpsim problems
-#if 0
        pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),2), pic16_popCopyReg(&pic16_pc_pclatu)));
-#endif
        pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),1), pic16_popCopyReg(&pic16_pc_pclath)));
+
        // note: MOVFF to PCL not allowed
        pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)),0));
        pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_pcl));
 
+
+//     pic16_emitpcode(POC_GOTO, pic16_popGetLabel(retlbl->key));
+//     pic16_emitpcodeNULLop(POC_NOP);
+//     pic16_emitpcodeNULLop(POC_NOP);
+
        /* return address is here: (X) */
-       pic16_emitpLabel(retlbl->key);
+       pic16_emitpLabelFORCE(retlbl->key);
+
+//     pic16_emitpcodeNULLop(POC_NOP);
 
        pic16_freeAsmop (IC_LEFT(ic),NULL,ic,TRUE);
 
@@ -3155,10 +3358,10 @@ static void genFunction (iCode *ic)
 {
   symbol *sym;
   sym_link *ftype;
+  
+       DEBUGpic16_emitcode ("; ***","%s  %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,pic16_labelOffset,max_key);
 
-       DEBUGpic16_emitcode ("; ***","%s  %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,labelOffset,max_key);
-
-       labelOffset += (max_key+4);
+       pic16_labelOffset += (max_key+4);
        max_key=0;
        GpsuedoStkPtr=0;
        _G.nRegsSaved = 0;
@@ -3166,20 +3369,18 @@ static void genFunction (iCode *ic)
        ftype = operandType(IC_LEFT(ic));
        sym = OP_SYMBOL(IC_LEFT(ic));
 
-       if(/*!IFFUNC_ISNAKED(ftype) &&*/ IFFUNC_ISISR(ftype)) {
+       if(IFFUNC_ISISR(sym->type /*ftype*/)) {
                /* create an absolute section at the interrupt vector:
-                * that is 0x0008 for interrupt 1, 0x0018 interrupt 2 */
-         int ivec;
+                * that is 0x0008 for interrupt 1 (high), 0x0018 interrupt 2 (low) */
          symbol *asym;
          char asymname[128];
-         
+         pBlock *apb;
+
                {
                  int i, found=-1;
 
                        sym = OP_SYMBOL( IC_LEFT(ic));
                        for(i=0;i<=2;i++) {
-//                             fprintf(stderr, "comparing name int %d\t%s with %s\n",
-//                                             i, interrupts[i]->name, sym->name);
                                if(interrupts[i]->name
                                        && !STRCASECMP(interrupts[i]->name, sym->name)) {
                                        found = i;
@@ -3190,23 +3391,28 @@ static void genFunction (iCode *ic)
                        if(found == -1) {
                                fprintf(stderr, "PIC16 port: %s:%d: interrupt function but cannot locate symbol (%s)\n",
                                        __FILE__, __LINE__, sym->name);
-                               exit(-1);
+                               assert( 0 );
                        }
-                       ivec = found;
+                       _G.interruptvector = found;
                }
-               sprintf(asymname, "ivec_%d_%s", ivec, sym->name);
+
+               sprintf(asymname, "ivec_%d_%s", _G.interruptvector, sym->name);
                asym = newSymbol(asymname, 0);
-               pic16_emitcode(";","-----------------------------------------");
-               pic16_emitcode(";"," interrupt vector %d for function %s", ivec, sym->name);
-               pic16_emitcode(";","-----------------------------------------");
 
-               pic16_addpCode2pBlock(pb, pic16_newpCodeFunction(moduleName, asym->name));
-               pic16_pBlockConvert2Absolute(pb);
+               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_emitpcode(POC_GOTO, pic16_popGetWithString( sym->rname ));
+               pic16_addpCode2pBlock(apb,
+                       pic16_newpCode(POC_GOTO, pic16_popGetWithString( sym->rname )));
                
                /* mark the end of this tiny function */
-               pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL));
+               pic16_addpCode2pBlock(apb,pic16_newpCodeFunction(NULL,NULL));
 
                {
                  absSym *abSym;
@@ -3214,12 +3420,15 @@ static void genFunction (iCode *ic)
                        abSym = Safe_calloc(1, sizeof(absSym));
                        abSym->name = Safe_strdup( asymname );
 
-                       switch( ivec ) {
+                       switch( _G.interruptvector ) {
                                case 0: abSym->address = 0x000000; break;
                                case 1: abSym->address = 0x000008; break;
                                case 2: abSym->address = 0x000018; break;
                        }
 
+                       /* relocate interrupt vectors if needed */
+                       abSym->address += pic16_options.ivt_loc;
+
                        addSet(&absSymSet, abSym);
                }
        }
@@ -3233,6 +3442,7 @@ static void genFunction (iCode *ic)
        pic16_emitcode("","%s:",sym->rname);
        pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(moduleName,sym->rname));
 
+
        {
          absSym *ab;
 
@@ -3241,7 +3451,7 @@ static void genFunction (iCode *ic)
                                pic16_pBlockConvert2Absolute(pb);
                                break;
                        }
-               
+
        }
 
 
@@ -3258,16 +3468,28 @@ static void genFunction (iCode *ic)
         * save acc, b, dpl, dph  */
        if (IFFUNC_ISISR(sym->type)) {
          int i;
+
+               _G.usefastretfie = 1;   /* use shadow registers by default */
                /* an ISR should save: WREG, STATUS, BSR, PRODL, PRODH, FSR0L, FSR0H */
-               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
-               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_status ));
-               pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
+               if(!(_G.interruptvector == 1)) {
+
+                       /* do not save WREG,STATUS,BSR for high priority interrupts
+                        * because they are stored in the hardware shadow registers already */
+                       _G.usefastretfie = 0;
+                       pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
+                       pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_status ));
+                       pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
+               }
+
+
+                /* these should really be optimized somehow, because not all
+                 * interrupt handlers modify them */
                pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodl ));
                pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodh ));
                pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l ));
                pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h ));
 
-                pic16_pBlockConvert2ISR(pb);
+//                pic16_pBlockConvert2ISR(pb);
                 
                /* if any registers used */
                if (sym->regsUsed) {
@@ -3275,12 +3497,21 @@ static void genFunction (iCode *ic)
                        DEBUGpic16_emitcode("; **", "Saving used registers in stack");
                        for ( i = 0 ; i < sym->regsUsed->size ; i++) {
                                if (bitVectBitValue(sym->regsUsed,i)) {
-//                                     fprintf(stderr, "%s:%d function %s uses register %s\n",
-//                                                     __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
-//                                                     pic16_regWithIdx(i)->name);
+#if 0
+                                       fprintf(stderr, "%s:%d function %s uses register %s\n",
+                                                       __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+                                                       pic16_regWithIdx(i)->name);
+#endif
 
                                        pic16_pushpCodeOp( pic16_popRegFromIdx(i) );
                                        _G.nRegsSaved++;
+
+                                       if(!pic16_regWithIdx(i)->wasUsed) {
+                                               fprintf(stderr, "%s:%d register %s is used in function but was wasUsed = 0d\n",
+                                                       __FILE__, __LINE__, pic16_regWithIdx(i)->name);
+
+                                               pic16_regWithIdx(i)->wasUsed = 1;
+                                       }
                                }
                        }
                }
@@ -3290,7 +3521,7 @@ static void genFunction (iCode *ic)
         
 //             fprintf(stderr, "function name: %s\n", sym->name);
                if(strcmp(sym->name, "main")) {
-                       if(!options.ommitFramePtr || sym->regsUsed) {
+                       if(/*!options.ommitFramePtr || */sym->regsUsed) {
                        /* setup the stack frame */
                                pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr2l, &pic16_pc_postdec1, 0));
                                pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1l, &pic16_pc_fsr2l, 0));
@@ -3301,26 +3532,44 @@ static void genFunction (iCode *ic)
 
                /* if callee-save to be used for this function
                * then save the registers being used in this function */
-               if (IFFUNC_CALLEESAVES(sym->type)) {
+//             if (IFFUNC_CALLEESAVES(sym->type))
+               {
                  int i;
-           
-//                     fprintf(stderr, "%s:%d function sym->regsUsed= %p\n", __FILE__, __LINE__, sym->regsUsed);
-                       
+
+//                     fprintf(stderr, "%s:%d function sym->regsUsed= %d\n", __FILE__, __LINE__, sym->regsUsed->size);
+
+//                     pic16_emitpcomment("entry regsUsed: %d\n", sym->regsUsed?sym->regsUsed->size:-1);
+
                        /* if any registers used */
                        if (sym->regsUsed) {
                                /* save the registers used */
                                DEBUGpic16_emitcode("; **", "Saving used registers in stack");
                                for ( i = 0 ; i < sym->regsUsed->size ; i++) {
                                        if (bitVectBitValue(sym->regsUsed,i)) {
-//                                             fprintf(stderr, "%s:%d function %s uses register %s\n",
-//                                                             __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
-//                                                             pic16_regWithIdx(i)->name);
+
+#if 0
+                                               fprintf(stderr, "%s:%d function %s uses register %s (wasUsed: %d, %p)\n",
+                                                               __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+                                                               pic16_regWithIdx(i)->name,
+                                                               pic16_regWithIdx(i)->wasUsed,
+                                                               pic16_regWithIdx(i));
+#endif
 
                                                pic16_pushpCodeOp( pic16_popRegFromIdx(i) );
+
 //                                             pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
 //                                                     PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))),
 //                                                     &pic16_pc_postdec1, 0));
+
                                                _G.nRegsSaved++;
+
+                                               if(!pic16_regWithIdx(i)->wasUsed) {
+                                                       fprintf(stderr, "%s:%d register %s is used in function but was wasUsed = 0d\n",
+                                                               __FILE__, __LINE__, pic16_regWithIdx(i)->name);
+
+                                                       pic16_regWithIdx(i)->wasUsed = 1;
+                                               }
+                                       
                                        }
                                }
                        }
@@ -3386,6 +3635,8 @@ static void genEndFunction (iCode *ic)
        return;
     }
 
+    /* add code for ISCRITICAL */
+
 #if 0
     if (IFFUNC_ISREENT(sym->type) || options.stackAuto)
     {
@@ -3418,14 +3669,8 @@ static void genEndFunction (iCode *ic)
     }
 #endif
 
-    /* restore the register bank  */    
-    if (FUNC_REGBANK(sym->type) || FUNC_ISISR(sym->type))
-        pic16_emitcode ("pop","psw");
-
        if (IFFUNC_ISISR(sym->type)) {
-
                /* now we need to restore the registers */
-               
                /* if any registers used */
                if (sym->regsUsed) {
                  int i;
@@ -3434,13 +3679,17 @@ static void genEndFunction (iCode *ic)
                        DEBUGpic16_emitcode("; **", "Restoring used registers from stack");
                        for ( i = sym->regsUsed->size; i >= 0; i--) {
                                if (bitVectBitValue(sym->regsUsed,i)) {
-//                                     fprintf(stderr, "%s:%d function %s uses register %s\n",
+
+//                                     fprintf(stderr, "%s:%d function %s uses register %s (restoring)\n",
 //                                                     __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
 //                                                     pic16_regWithIdx(i)->name);
-        
-                                       pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
-                                                       &pic16_pc_preinc1,
-                                                       PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+
+                                       pic16_poppCodeOp( pic16_popRegFromIdx(i) );
+
+//                                     pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
+//                                                     &pic16_pc_preinc1,
+//                                                     PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+
                                }
                        }
                }
@@ -3449,103 +3698,106 @@ static void genEndFunction (iCode *ic)
                pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l));
                pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodh ));
                pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodl ));
-               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_bsr ));
-               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_status ));
-               pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_wreg ));
+
+               if(!(_G.interruptvector == 1)) {
+                       /* 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 */
+
+//             pic16_pBlockConvert2ISR(pb);
+
 
                /* if debug then send end of function */
 /*     if (options.debug && currFunc)  */
                if (currFunc) {
-                       _G.debugLine = 1;
-                       pic16_emitcode(";","C$%s$%d$%d$%d ==.",
-                                       FileBaseName(ic->filename),currFunc->lastLine,
-                                       ic->level,ic->block); 
-                       if (IS_STATIC(currFunc->etype))     
-                               pic16_emitcode(";","XF%s$%s$0$0 ==.",moduleName,currFunc->name); 
-                       else
-                               pic16_emitcode(";","XG$%s$0$0 ==.",currFunc->name);
-                       _G.debugLine = 0;
+                       debugFile->writeEndFunction (currFunc, ic, 1);
                }
        
-#if 0
-               pic16_emitpcode(POC_CLRF,   pic16_popCopyReg(&pic16_pc_status));
-               pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_ssave));
-               pic16_emitpcode(POC_MOVWF,  pic16_popCopyReg(&pic16_pc_status));
-               pic16_emitpcode(POC_SWAPF,  pic16_popCopyReg(&pic16_pc_wsave));
-               pic16_emitpcode(POC_SWAPFW, pic16_popCopyReg(&pic16_pc_wsave));
-#endif
-
+               if(_G.usefastretfie)
+                       pic16_emitpcode(POC_RETFIE, pic16_newpCodeOpLit(1));
+               else
                pic16_emitpcodeNULLop(POC_RETFIE);
-       }
-       else {
+               _G.usefastretfie = 0;
+       else {
                if (IFFUNC_ISCRITICAL(sym->type))
                        pic16_emitcode("setb","ea");
        
-       /* if any registers used */
-       if (sym->regsUsed) {
-         int i;
-               /* save the registers used */
-               DEBUGpic16_emitcode("; **", "Restoring used registers from stack");
-               for ( i = sym->regsUsed->size; i >= 0; i--) {
-                       if (bitVectBitValue(sym->regsUsed,i)) {
-//                             fprintf(stderr, "%s:%d function %s uses register %s\n",
-//                                             __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
-//                                             pic16_regWithIdx(i)->name);
-        
-                               pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
-                                       &pic16_pc_preinc1,
-                                       PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+
+//             pic16_emitpcomment("exit regsUsed: %d\n", sym->regsUsed?sym->regsUsed->size:-1);
+
+               /* if any registers used */
+               if (sym->regsUsed) {
+                 int i;
+                       /* save the registers used */
+                       DEBUGpic16_emitcode("; **", "Restoring used registers from stack");
+                       for ( i = sym->regsUsed->size; i >= 0; i--) {
+                               if (bitVectBitValue(sym->regsUsed,i)) {
+       
+//                                     fprintf(stderr, "%s:%d function %s uses register %s (restoring)\n",
+//                                                     __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name,
+//                                                     pic16_regWithIdx(i)->name);
+       
+                                       pic16_poppCodeOp( pic16_popRegFromIdx(i) );
+                                       
+//                                     pic16_emitpcode(POC_MOVFF, pic16_popCombine2(
+//                                             &pic16_pc_preinc1,
+//                                             PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0));
+
+                                       _G.nRegsSaved--;
+                               }
                        }
                }
-       }
        
+//             pic16_emitpcomment("%s: _G.nRegsSaved upon exit from function: %d\n", __FUNCTION__, _G.nRegsSaved);
+               /* if debug then send end of function */
+               if (currFunc) {
+                       debugFile->writeEndFunction (currFunc, ic, 1);
+               }
 
-       /* if debug then send end of function */
-       if (currFunc) {
-           _G.debugLine = 1;
-           pic16_emitcode(";","C$%s$%d$%d$%d ==.",
-                    FileBaseName(ic->filename),currFunc->lastLine,
-                    ic->level,ic->block); 
-           if (IS_STATIC(currFunc->etype))         
-               pic16_emitcode(";","XF%s$%s$0$0 ==.",moduleName,currFunc->name); 
-           else
-               pic16_emitcode(";","XG$%s$0$0 ==.",currFunc->name);
-           _G.debugLine = 0;
-       }
-
-       /* insert code to restore stack frame, if user enabled it
-        * and function is not main() */
+               /* insert code to restore stack frame, if user enabled it
+                * and function is not main() */
         
-       if(strcmp(sym->name, "main")) {
-               if(!options.ommitFramePtr || sym->regsUsed) {
-                       /* restore stack frame */
-                       if(STACK_MODEL_LARGE)
+
+               if(strcmp(sym->name, "main")) {
+                       if(/*!options.ommitFramePtr ||*/ sym->regsUsed) {
+                               /* restore stack frame */
+                               if(STACK_MODEL_LARGE)
+                                       pic16_emitpcode(POC_MOVFF,
+                                               pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0));
                                pic16_emitpcode(POC_MOVFF,
-                                       pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0));
-                       pic16_emitpcode(POC_MOVFF,
-                                       pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0));
+                                               pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0));
+                       }
                }
-       }
 
-        pic16_emitcode ("return","");
-       pic16_emitpcodeNULLop(POC_RETURN);
+               pic16_emitpcodeNULLop(POC_RETURN);
 
-       /* Mark the end of a function */
-       pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL));
-    }
+               /* Mark the end of a function */
+               pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL));
+       }
 
 }
 
 
 void pic16_storeForReturn(operand *op, int offset, pCodeOp *dest)
 {
-//             (AOP(left)->aopu.pcop->type == PO_DIR)?
 
-       if(AOP(op)->aopu.pcop->type == PO_IMMEDIATE) {
+       if(is_LitOp(op)) {
                pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset)); // patch 12
-               pic16_emitpcode(POC_MOVWF, dest);
+
+               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));
+                       return;
+               }
+               
                pic16_emitpcode(POC_MOVFF, pic16_popGet2p(
                        pic16_popGet(AOP(op), offset), dest));
        }
@@ -3670,7 +3922,7 @@ jumpret:
                && IC_LABEL(ic->next) == returnLabel)) {
        
                pic16_emitpcode(POC_GOTO,pic16_popGetLabel(returnLabel->key));
-               pic16_emitcode("goto","_%05d_DS_",returnLabel->key+100 + labelOffset);
+               pic16_emitcode("goto","_%05d_DS_",returnLabel->key+100 + pic16_labelOffset);
        }
 }
 
@@ -3679,13 +3931,15 @@ jumpret:
 /*-----------------------------------------------------------------*/
 static void genLabel (iCode *ic)
 {
+
+
     /* special case never generate */
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     if (IC_LABEL(ic) == entryLabel)
         return ;
 
     pic16_emitpLabel(IC_LABEL(ic)->key);
-    pic16_emitcode("","_%05d_DS_:",(IC_LABEL(ic)->key+100 + labelOffset));
+    pic16_emitcode("","_%05d_DS_:",(IC_LABEL(ic)->key+100 + pic16_labelOffset));
 }
 
 /*-----------------------------------------------------------------*/
@@ -3695,7 +3949,7 @@ static void genLabel (iCode *ic)
 static void genGoto (iCode *ic)
 {
   pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_LABEL(ic)->key));
-  pic16_emitcode ("goto","_%05d_DS_",(IC_LABEL(ic)->key+100)+labelOffset);
+  pic16_emitcode ("goto","_%05d_DS_",(IC_LABEL(ic)->key+100)+pic16_labelOffset);
 }
 
 
@@ -3725,10 +3979,6 @@ static void genMultOneByte (operand *left,
                             operand *right,
                             operand *result)
 {
-  sym_link *opetype = operandType(result);
-
-  // symbol *lbl ;
-  int size,offset;
 
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
@@ -3742,116 +3992,159 @@ static void genMultOneByte (operand *left,
     left = t;
   }
 
-  size = AOP_SIZE(result);
-  if(size == 1) {
+       /* size is already checked in genMult == 1 */
+//     size = AOP_SIZE(result);
 
-    if (AOP_TYPE(right) == AOP_LIT){
-      pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", 
-                    pic16_aopGet(AOP(right),0,FALSE,FALSE), 
-                    pic16_aopGet(AOP(left),0,FALSE,FALSE), 
-                    pic16_aopGet(AOP(result),0,FALSE,FALSE));
-    } else {
-      pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", 
-                    pic16_aopGet(AOP(right),0,FALSE,FALSE), 
-                    pic16_aopGet(AOP(left),0,FALSE,FALSE), 
-                    pic16_aopGet(AOP(result),0,FALSE,FALSE));
-    }
-    pic16_genMult8X8_8 (left, right,result);
-  } else {  // (size > 1)
-
-    pic16_emitcode("multiply (size>1) ","variable :%s by variable %s and store in %s", 
-                  pic16_aopGet(AOP(right),0,FALSE,FALSE), 
-                  pic16_aopGet(AOP(left),0,FALSE,FALSE), 
-                  pic16_aopGet(AOP(result),0,FALSE,FALSE));
-
-    if (SPEC_USIGN(opetype)){
-      pic16_emitcode("multiply ","unsigned result. size = %d",AOP_SIZE(result));
-      pic16_genUMult8X8_16 (left, right, result, NULL);
-
-      if (size > 2) {
-       /* for filling the MSBs */
-       pic16_emitpcode(POC_CLRF,  pic16_popGet(AOP(result),2));
-       pic16_emitpcode(POC_CLRF,  pic16_popGet(AOP(result),3));
-      }
-    }
-    else{
-      pic16_emitcode("multiply ","signed result. size = %d",AOP_SIZE(result));
+       if (AOP_TYPE(right) == AOP_LIT){
+               pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       } else {
+               pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       }
+       
+       pic16_genMult8X8_8 (left, right,result);
+}
 
-      pic16_emitcode("mov","a,b");
+/*-----------------------------------------------------------------*/
+/* genMultOneWord : 16 bit multiplication                          */
+/*-----------------------------------------------------------------*/
+static void genMultOneWord (operand *left,
+                            operand *right,
+                            operand *result)
+{
 
-      /* adjust the MSB if left or right neg */
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
+       DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result);
 
-      /* if one literal */
-      if (AOP_TYPE(right) == AOP_LIT){
-       pic16_emitcode("multiply ","right is a lit");
-       /* AND literal negative */
-       if((int) floatFromVal (AOP(right)->aopu.aop_lit) < 0){
-         /* adjust MSB (c==0 after mul) */
-         pic16_emitcode("subb","a,%s", pic16_aopGet(AOP(left),0,FALSE,FALSE));
+       /* (if two literals, the value is computed before)
+        * if one literal, literal on the right */
+       if (AOP_TYPE(left) == AOP_LIT){
+         operand *t = right;
+               right = left;
+               left = t;
        }
-      }
-      else{
-       pic16_genSMult8X8_16 (left, right, result, NULL);
-      }
 
-      if(size > 2){
-       pic16_emitcode("multiply ","size is greater than 2, so propogate sign");
-       /* get the sign */
-       pic16_emitcode("rlc","a");
-       pic16_emitcode("subb","a,acc");
-      }
-    }
+       /* size is checked already == 2 */
+//     size = AOP_SIZE(result);
 
-    size -= 2;   
-    offset = 2;
-    if (size > 0)
-      while (size--)
-       pic16_emitcode("multiply ","size is way greater than 2, so propogate sign");
-    //pic16_aopPut(AOP(result),"a",offset++);
-  }
+       if (AOP_TYPE(right) == AOP_LIT) {
+               pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       } else {
+               pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       }
+       
+       pic16_genMult16X16_16(left, right,result);
 }
 
+/*-----------------------------------------------------------------*/
+/* genMultOneLong : 32 bit multiplication                          */
+/*-----------------------------------------------------------------*/
+static void genMultOneLong (operand *left,
+                            operand *right,
+                            operand *result)
+{
+
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
+       DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result);
+
+       /* (if two literals, the value is computed before)
+        * if one literal, literal on the right */
+       if (AOP_TYPE(left) == AOP_LIT){
+         operand *t = right;
+               right = left;
+               left = t;
+       }
+
+       /* size is checked already == 4 */
+//     size = AOP_SIZE(result);
+
+       if (AOP_TYPE(right) == AOP_LIT) {
+               pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       } else {
+               pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", 
+                                       pic16_aopGet(AOP(right),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(left),0,FALSE,FALSE), 
+                                       pic16_aopGet(AOP(result),0,FALSE,FALSE));
+       }
+       
+       pic16_genMult32X32_32(left, right,result);
+}
+
+
+
 /*-----------------------------------------------------------------*/
 /* genMult - generates code for multiplication                     */
 /*-----------------------------------------------------------------*/
 static void genMult (iCode *ic)
 {
-    operand *left = IC_LEFT(ic);
-    operand *right = IC_RIGHT(ic);
-    operand *result= IC_RESULT(ic);   
+  operand *left = IC_LEFT(ic);
+  operand *right = IC_RIGHT(ic);
+  operand *result= IC_RESULT(ic);   
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* assign the amsops */
-    pic16_aopOp (left,ic,FALSE);
-    pic16_aopOp (right,ic,FALSE);
-    pic16_aopOp (result,ic,TRUE);
+       DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       /* assign the amsops */
+       pic16_aopOp (left,ic,FALSE);
+       pic16_aopOp (right,ic,FALSE);
+       pic16_aopOp (result,ic,TRUE);
 
-  DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
+       DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
 
-    /* special cases first */
-    /* both are bits */
-    if (AOP_TYPE(left) == AOP_CRY &&
-        AOP_TYPE(right)== AOP_CRY) {
-        genMultbits(left,right,result);
-        goto release ;
-    }
+       /* special cases first *
+       * both are bits */
+       if (AOP_TYPE(left) == AOP_CRY
+               && AOP_TYPE(right)== AOP_CRY) {
+               genMultbits(left,right,result);
+         goto release ;
+       }
 
-    /* if both are of size == 1 */
-    if (AOP_SIZE(left) == 1 &&
-        AOP_SIZE(right) == 1 ) {
-        genMultOneByte(left,right,result);
-        goto release ;
-    }
+       /* if both are of size == 1 */
+       if(AOP_SIZE(left) == 1
+               && AOP_SIZE(right) == 1) {
+               genMultOneByte(left,right,result);
+         goto release ;
+       }
+
+       /* if both are of size == 2 */
+       if(AOP_SIZE(left) == 2
+               && AOP_SIZE(right) == 2) {
+               genMultOneWord(left, right, result);
+         goto release;
+       }
+       
+       /* if both are of size == 4 */
+       if(AOP_SIZE(left) == 4
+               && AOP_SIZE(right) == 4) {
+               genMultOneLong(left, right, result);
+         goto release;
+       }
+       
+       pic16_emitcode("multiply ","sizes are greater than 4 ... need to insert proper algor.");
 
-    pic16_emitcode("multiply ","sizes are greater than 2... need to insert proper algor.");
 
-    /* should have been converted to function call */
+       fprintf(stderr, "operand sizes result: %d left: %d right: %d\n", AOP_SIZE(result), AOP_SIZE(left), AOP_SIZE(right));
+       /* should have been converted to function call */
        assert(0) ;
 
 release :
-    pic16_freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
-    pic16_freeAsmop(right,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
-    pic16_freeAsmop(result,NULL,ic,TRUE); 
+       pic16_freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
+       pic16_freeAsmop(right,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
+       pic16_freeAsmop(result,NULL,ic,TRUE); 
 }
 
 /*-----------------------------------------------------------------*/
@@ -3876,31 +4169,95 @@ static void genDivbits (operand *left,
     pic16_aopPut(AOP(result),"c",0);
 }
 
-/*-----------------------------------------------------------------*/
-/* genDivOneByte : 8 bit division                                  */
-/*-----------------------------------------------------------------*/
-static void genDivOneByte (operand *left,
-                           operand *right,
-                           operand *result)
-{
-    sym_link *opetype = operandType(result);
-    char *l ;
-    symbol *lbl ;
-    int size,offset;
+/*-----------------------------------------------------------------*/
+/* genDivOneByte : 8 bit division                                  */
+/*-----------------------------------------------------------------*/
+static void genDivOneByte (operand *left,
+                           operand *right,
+                           operand *result)
+{
+    sym_link *opetype = operandType(result);
+    char *l ;
+    symbol *lbl ;
+    int size,offset;
+
+       /* result = divident / divisor
+        * - divident may be a register or a literal,
+        * - divisor may be a register or a literal,
+        * so there are 3 cases (literal / literal is optimized
+        * by the front-end) to handle.
+        * In addition we must handle signed and unsigned, which
+        * result in 6 final different cases -- VR */
+
+    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    size = AOP_SIZE(result) - 1;
+    offset = 1;
+    /* signed or unsigned */
+    if (SPEC_USIGN(opetype)) {
+      pCodeOp *pct1,   /* count */
+               *pct2,  /* reste */
+               *pct3;  /* temp */
+      symbol *label1, *label2, *label3;;
+
+
+        /* unsigned is easy */
+
+       pct1 = pic16_popGetTempReg();
+       pct2 = pic16_popGetTempReg();
+       pct3 = pic16_popGetTempReg();
+       
+       label1 = newiTempLabel(NULL);
+       label2 = newiTempLabel(NULL);
+       label3 = newiTempLabel(NULL);
+
+       /* the following algorithm is extracted from divuint.c */
+
+       pic16_emitpcode(POC_MOVLW, pic16_popGetLit( 8 ));
+       pic16_emitpcode(POC_MOVWF, pic16_pCodeOpCopy( pct1 ));
+       
+       pic16_emitpcode(POC_CLRF, pic16_pCodeOpCopy( pct2 ));
+
+       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), 0));
+       
+       pic16_emitpLabel(label1->key);
+       
+       emitCLRC;
+       pic16_emitpcode(POC_RLCF, pic16_pCodeOpCopy( pct2 ));
+
+
+       emitCLRC;
+       pic16_emitpcode(POC_RLCF, pic16_popCopyReg( &pic16_pc_wreg ));
+       
+
+       emitSKPNC;
+       pic16_emitpcode(POC_INCF, pic16_pCodeOpCopy( pct2 ));
+       
+       pic16_emitpcode(POC_MOVWF, pic16_pCodeOpCopy( pct3 ));
+       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), 0));
+       
+       pic16_emitpcode(POC_CPFSLT, pic16_pCodeOpCopy(pct2));
+       pic16_emitpcode(POC_BRA, pic16_popGetLabel(label3->key));
+       pic16_emitpcode(POC_BRA, pic16_popGetLabel(label2->key));
+       
+       pic16_emitpLabel( label3->key );
+       pic16_emitpcode(POC_SUBWF, pic16_pCodeOpCopy(pct2));
+       pic16_emitpcode(POC_INCF, pic16_pCodeOpCopy(pct3));
+       
+       
+
+       pic16_emitpLabel(label2->key);
+       pic16_emitpcode(POC_MOVFW, pic16_pCodeOpCopy(pct3));
+       pic16_emitpcode(POC_DECFSZ, pic16_pCodeOpCopy(pct1));
+       pic16_emitpcode(POC_BRA, pic16_popGetLabel( label1->key ));
+       
+       /* result is in wreg */
+       if(AOP_TYPE(result) != AOP_ACC)
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0));
+
+       pic16_popReleaseTempReg( pct3 );
+       pic16_popReleaseTempReg( pct2 );
+       pic16_popReleaseTempReg( pct1 );
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    size = AOP_SIZE(result) - 1;
-    offset = 1;
-    /* signed or unsigned */
-    if (SPEC_USIGN(opetype)) {
-        /* unsigned is easy */
-        pic16_emitcode("mov","b,%s", pic16_aopGet(AOP(right),0,FALSE,FALSE));
-        l = pic16_aopGet(AOP(left),0,FALSE,FALSE);
-        MOVA(l);        
-        pic16_emitcode("div","ab");
-        pic16_aopPut(AOP(result),"a",0);
-        while (size--)
-            pic16_aopPut(AOP(result),zero,offset++);
         return ;
     }
 
@@ -3969,6 +4326,12 @@ static void genDiv (iCode *ic)
     operand *right = IC_RIGHT(ic);
     operand *result= IC_RESULT(ic);   
 
+
+       /* Division is a very lengthy algorithm, so it is better
+        * to call support routines than inlining algorithm.
+        * Division functions written here just in case someone
+        * wants to inline and not use the support libraries -- VR */
+
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     /* assign the amsops */
     pic16_aopOp (left,ic,FALSE);
@@ -4153,11 +4516,11 @@ static void genIfxJump (iCode *ic, char *jval)
          emitSKPC;
        else {
          DEBUGpic16_emitcode ("; ***","%d - assuming %s is in bit space",__LINE__,jval);         
-         pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(jval,-1,1));
+         pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(jval,-1,1, PO_GPR_REGISTER));
        }
 
        pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ic)->key));
-       pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ic)->key+100 + labelOffset);
+       pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ic)->key+100 + pic16_labelOffset);
 
     }
     else {
@@ -4168,11 +4531,11 @@ static void genIfxJump (iCode *ic, char *jval)
          emitSKPNC;
        else {
          DEBUGpic16_emitcode ("; ***","%d - assuming %s is in bit space",__LINE__,jval);         
-         pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(jval,-1,1));
+         pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(jval,-1,1, PO_GPR_REGISTER));
        }
 
        pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ic)->key));
-       pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ic)->key+100 + labelOffset);
+       pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ic)->key+100 + pic16_labelOffset);
 
     }
 
@@ -4181,6 +4544,9 @@ static void genIfxJump (iCode *ic, char *jval)
     ic->generated = 1;
 }
 
+#if 0
+// not needed ATM
+
 /*-----------------------------------------------------------------*/
 /* genSkip                                                         */
 /*-----------------------------------------------------------------*/
@@ -4207,7 +4573,7 @@ static void genSkip(iCode *ifx,int status_bit)
     }
 
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-    // pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+pic16_labelOffset);
 
   } else {
 
@@ -4226,17 +4592,20 @@ static void genSkip(iCode *ifx,int status_bit)
       break;
     }
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-    // pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+pic16_labelOffset);
 
   }
 
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* genSkipc                                                        */
 /*-----------------------------------------------------------------*/
 static void genSkipc(resolvedIfx *rifx)
 {
+  DEBUGpic16_emitcode ("; ***","%s  %d rifx= %p",__FUNCTION__,__LINE__, rifx);
+  
   if(!rifx)
     return;
 
@@ -4245,7 +4614,7 @@ static void genSkipc(resolvedIfx *rifx)
   else
     emitSKPNC;
 
-  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rifx->lbl->key));
+  pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rifx->lbl->key));
   rifx->generated = 1;
 }
 
@@ -4254,6 +4623,8 @@ static void genSkipc(resolvedIfx *rifx)
 /*-----------------------------------------------------------------*/
 static void genSkipz2(resolvedIfx *rifx, int invert_condition)
 {
+  DEBUGpic16_emitcode ("; ***","%s  %d rifx= %p",__FUNCTION__,__LINE__, rifx);
+  
   if(!rifx)
     return;
 
@@ -4286,9 +4657,9 @@ static void genSkipz(iCode *ifx, int condition)
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
 
   if ( IC_TRUE(ifx) )
-    pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+    pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+pic16_labelOffset);
   else
-    pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+    pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+pic16_labelOffset);
 
 }
 #endif
@@ -4302,9 +4673,9 @@ static void genSkipCond(resolvedIfx *rifx,operand *op, int offset, int bit)
     return;
 
   if(rifx->condition)
-    pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(op),offset,FALSE,FALSE),bit,0));
+    pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(op),offset,FALSE,FALSE),bit,0, PO_GPR_REGISTER));
   else
-    pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(op),offset,FALSE,FALSE),bit,0));
+    pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(op),offset,FALSE,FALSE),bit,0, PO_GPR_REGISTER));
 
 
   pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rifx->lbl->key));
@@ -4396,16 +4767,19 @@ static void genCmp (operand *left,operand *right,
        end the carry flag is set then we know that
        left is greater than right */
 
-    //    {
-
     symbol *lbl  = newiTempLabel(NULL);
 
+#if 0
+       fprintf(stderr, "%s:%s:%d truelbl: %d\tlbl: %d\n",
+               __FILE__, __FUNCTION__, __LINE__, truelbl->key+100+pic16_labelOffset, lbl->key+100+pic16_labelOffset);
+#endif
+
 #ifndef _swapp
     if(AOP_TYPE(right) == AOP_LIT) {
 
       //lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
 
-      DEBUGpic16_emitcode(";right lit","lit = 0x%x,sign=%d",lit,sign);
+      DEBUGpic16_emitcode(";right lit","%d lit = 0x%x,sign=%d",__LINE__, lit,sign);
 
       /* special cases */
 
@@ -4479,7 +4853,7 @@ static void genCmp (operand *left,operand *right,
          //genSkipCond(&rFalseIfx,left,size,7);
          //rFalseIfx.condition ^= 1;
 
-         pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0));
+         pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
          if(rFalseIfx.condition)
            pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
          else
@@ -4549,7 +4923,7 @@ static void genCmp (operand *left,operand *right,
 
          //genSkipCond(&rFalseIfx,left,size,7);
 
-         pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0));
+         pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
 
          if(rFalseIfx.condition)
            pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(truelbl->key));
@@ -4560,7 +4934,7 @@ static void genCmp (operand *left,operand *right,
        } else {
          /* lit is positive */
          DEBUGpic16_emitcode(";right lit","line = %d signed compare to 0x%x",__LINE__,lit);
-         pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0));
+         pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),size,FALSE,FALSE),7,0, PO_GPR_REGISTER));
          if(rFalseIfx.condition)
            pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(rFalseIfx.lbl->key));
          else
@@ -4632,6 +5006,7 @@ static void genCmp (operand *left,operand *right,
 
 
       pic16_emitpLabel(lbl->key);
+//     pic16_emitpLabel(truelbl->key);
       //if(emitFinalCheck)
       genSkipc(&rFalseIfx);
       if(sign)
@@ -4730,7 +5105,8 @@ static void genCmp (operand *left,operand *right,
        }
 
        if(ifx) ifx->generated = 1;
-       //goto check_carry;
+       if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result)))
+               goto check_carry;
        return;
 
       } else {
@@ -4757,7 +5133,7 @@ static void genCmp (operand *left,operand *right,
            int s = size;
 
            if(rFalseIfx.condition) {
-             pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0));
+             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));
            }
 
@@ -4800,7 +5176,7 @@ static void genCmp (operand *left,operand *right,
            //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));
+           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)
@@ -4819,7 +5195,7 @@ static void genCmp (operand *left,operand *right,
            //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));
+           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
@@ -4907,7 +5283,7 @@ static void genCmp (operand *left,operand *right,
              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 it's not the last then OR in the variable */
              if(size)
                pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size));
            }
@@ -4917,6 +5293,7 @@ static void genCmp (operand *left,operand *right,
          pic16_emitpLabel(lbl->key);
 
          rFalseIfx.condition ^= 1;
+
          genSkipc(&rFalseIfx);
        }
 
@@ -4934,12 +5311,16 @@ static void genCmp (operand *left,operand *right,
     if(sign) {
       /* Sigh. thus sucks... */
       if(size) {
+       pCodeOp *pctemp;
+       
+       pctemp = pic16_popGetTempReg();
        pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),size));
-       pic16_emitpcode(POC_MOVWF, pic16_popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_MOVWF, pctemp);             //pic16_pic16_popRegFromIdx(pic16_Gstack_base_addr));
        pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80));
-       pic16_emitpcode(POC_XORWF, pic16_popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_XORWF, pctemp);             //pic16_popRegFromIdx(pic16_Gstack_base_addr));
        pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),size));
-       pic16_emitpcode(POC_SUBFW, pic16_popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_emitpcode(POC_SUBFW, pctemp);             //pic16_popRegFromIdx(pic16_Gstack_base_addr));
+       pic16_popReleaseTempReg(pctemp);
       } else {
        /* Signed char comparison */
        /* Special thanks to Nikolai Golovchenko for this snippet */
@@ -4994,9 +5375,13 @@ static void genCmp (operand *left,operand *right,
 
   }
 
-  // check_carry:
-  if (AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) {
+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__);
@@ -5071,6 +5456,10 @@ static void genCmpLt (iCode *ic, iCode *ifx)
     pic16_freeAsmop(result,NULL,ic,TRUE); 
 }
 
+#if 0
+// not needed ATM
+// FIXME reenable literal optimisation when the pic16 port is stable
+
 /*-----------------------------------------------------------------*/
 /* genc16bit2lit - compare a 16 bit value to a literal             */
 /*-----------------------------------------------------------------*/
@@ -5121,7 +5510,10 @@ static void genc16bit2lit(operand *op, int lit, int offset)
   }
 
 }
+#endif
 
+#if 0
+// not needed ATM
 /*-----------------------------------------------------------------*/
 /* gencjneshort - compare and jump if not equal                    */
 /*-----------------------------------------------------------------*/
@@ -5132,15 +5524,18 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
   int res_offset = 0;  /* the result may be a different size then left or right */
   int res_size = AOP_SIZE(result);
   resolvedIfx rIfx;
-  symbol *lbl;
+  symbol *lbl, *lbl_done;
 
   unsigned long lit = 0L;
+  int preserve_result = 0; /* don't touch result before we are done, if left/right == result */
+
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
   if(result)
     DEBUGpic16_emitcode ("; ***","%s  %d result is not null",__FUNCTION__,__LINE__);
   resolveIfx(&rIfx,ifx);
   lbl =  newiTempLabel(NULL);
+  lbl_done =  newiTempLabel(NULL);
 
 
   /* if the left side is a literal or 
@@ -5155,13 +5550,24 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
   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;
+
+  if(result && !preserve_result)
+    {
+      int i;
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+    }
+
+
   /* if the right side is a literal then anything goes */
   if (AOP_TYPE(right) == AOP_LIT &&
       AOP_TYPE(left) != AOP_DIR ) {
     switch(size) {
     case 2:
       genc16bit2lit(left, lit, 0);
-      emitSKPNZ;
+      emitSKPZ;
       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(lbl->key));
       break;
     default:
@@ -5174,7 +5580,7 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
          pic16_emitpcode(POC_MOVF,pic16_popGet(AOP(left),offset));
        }
 
-       emitSKPNZ;
+       emitSKPZ;
        pic16_emitpcode(POC_GOTO,pic16_popGetLabel(lbl->key));
        offset++;
        if(res_offset < res_size-1)
@@ -5195,7 +5601,7 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
     int lbl_key = lbl->key;
 
     if(result) {
-      pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),res_offset));
+      // pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),res_offset));
       //pic16_emitpcode(POC_INCF,pic16_popGet(AOP(result),res_offset));
     }else {
       DEBUGpic16_emitcode ("; ***","%s  %d -- ERROR",__FUNCTION__,__LINE__);
@@ -5203,7 +5609,7 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
              __FUNCTION__,__LINE__);
       return;
     }
-
+   
 /*     switch(size) { */
 /*     case 2: */
 /*       genc16bit2lit(left, lit, 0); */
@@ -5227,8 +5633,8 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
          break;
        case 1:
          pic16_emitpcode(POC_DECFSZW,pic16_popGet(AOP(left),offset));
-         pic16_emitpcode(POC_INCF,pic16_popGet(AOP(result),res_offset));
-         //pic16_emitpcode(POC_GOTO,pic16_popGetLabel(lbl->key));
+         //pic16_emitpcode(POC_INCF,pic16_popGet(AOP(result),res_offset));
+         pic16_emitpcode(POC_GOTO,pic16_popGetLabel(lbl->key));
          emit_skip=0;
          break;
        case 0xff:
@@ -5304,17 +5710,38 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx)
     }
   }
 
-  pic16_emitpcode(POC_INCF,pic16_popGet(AOP(result),res_offset));
+  if(result && preserve_result)
+    {
+      int i;
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+    }
+
+  pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result), 0));
+
+  if(result && preserve_result)
+    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(lbl_done->key));
+
   if(!rIfx.condition)
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rIfx.lbl->key));
 
   pic16_emitpLabel(lbl->key);
 
+  if(result && preserve_result)
+    {
+      int i;
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+
+      pic16_emitpLabel(lbl_done->key);
+   }
+
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
   if(ifx)
     ifx->generated = 1;
 }
+#endif
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -5339,6 +5766,143 @@ static void gencjne(operand *left, operand *right, iCode *ifx)
 }
 #endif
 
+
+/*-----------------------------------------------------------------*/
+/* is_LitOp - check if operand has to be treated as literal        */
+/*-----------------------------------------------------------------*/
+static bool is_LitOp(operand *op)
+{
+  return (AOP_TYPE(op) == AOP_LIT)
+      || ( (AOP_TYPE(op) == AOP_PCODE)
+          && ( (AOP(op)->aopu.pcop->type == PO_LITERAL)
+              || (AOP(op)->aopu.pcop->type == PO_IMMEDIATE) ));  
+}
+
+/*-----------------------------------------------------------------*/
+/* is_LitAOp - check if operand has to be treated as literal        */
+/*-----------------------------------------------------------------*/
+static bool is_LitAOp(asmop *aop)
+{
+  return (aop->type == AOP_LIT)
+      || ( (aop->type == AOP_PCODE)
+          && ( (aop->aopu.pcop->type == PO_LITERAL)
+              || (aop->aopu.pcop->type == PO_IMMEDIATE) ));  
+}
+
+
+
+/*-----------------------------------------------------------------*/
+/* genCmpEq - generates code for equal to                          */
+/*-----------------------------------------------------------------*/
+static void genCmpEq (iCode *ic, iCode *ifx)
+{
+  operand *left, *right, *result;
+  symbol *falselbl = newiTempLabel(NULL);
+  symbol *donelbl = newiTempLabel(NULL);
+
+  int preserve_result = 0;
+  int generate_result = 0;
+  int i=0;
+
+  pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE);
+  pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE);
+  pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE);
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  DEBUGpic16_pic16_AopType(__LINE__,left,right,result);
+
+  if( (AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(left) == AOP_CRY) )
+    {
+      DEBUGpic16_emitcode ("; ***","%s  %d -- ERROR",__FUNCTION__,__LINE__);
+      fprintf(stderr, "%s  %d error - left/right CRY operands not supported\n",__FUNCTION__,__LINE__);
+      goto release;
+    }
+
+  if (is_LitOp(left) || (AOP_TYPE(right) == AOP_ACC))
+    {
+      operand *tmp = right ;
+      right = left;
+      left = tmp;
+    }
+
+  if ( regsInCommon(left, result) || regsInCommon(right, result) )
+    preserve_result = 1;
+
+  if(result && AOP_SIZE(result))
+    generate_result = 1;
+
+  if(generate_result && !preserve_result)
+    {
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+    }
+
+  for(i=0; i < AOP_SIZE(left); i++)
+    {
+      if(AOP_TYPE(left) != AOP_ACC)
+        {
+          if(is_LitOp(left))
+            pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(left), i));
+          else
+            pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), i));
+        }
+      if(is_LitOp(right))
+        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));
+    }
+
+  // result == true
+
+  if(generate_result && preserve_result)
+    {
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+    }
+
+  if(generate_result)
+    pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result), 0)); // result = true
+
+  if(generate_result && preserve_result)
+    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(donelbl->key));
+
+  if(ifx && IC_TRUE(ifx))
+    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
+
+  if(ifx && IC_FALSE(ifx))
+    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(donelbl->key));
+
+  pic16_emitpLabel(falselbl->key);
+
+  // result == false
+
+  if(ifx && IC_FALSE(ifx))
+    pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
+
+  if(generate_result && preserve_result)
+    {
+      for(i = 0; i < AOP_SIZE(result); i++)
+        pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),i));
+    }
+
+  pic16_emitpLabel(donelbl->key);
+
+  if(ifx)
+    ifx->generated = 1;
+
+release:
+  pic16_freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
+  pic16_freeAsmop(right,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
+  pic16_freeAsmop(result,NULL,ic,TRUE);
+
+}
+
+
+#if 0
+// old version kept for reference
+
 /*-----------------------------------------------------------------*/
 /* genCmpEq - generates code for equal to                          */
 /*-----------------------------------------------------------------*/
@@ -5411,7 +5975,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
                 pic16_emitcode("jc","%05d_DS_",tlbl->key+100);
                 pic16_emitcode("ljmp","%05d_DS_",IC_FALSE(ifx)->key+100);
             }
-            pic16_emitcode("","%05d_DS_:",tlbl->key+100+labelOffset);
+            pic16_emitcode("","%05d_DS_:",tlbl->key+100+pic16_labelOffset);
 
                {
                /* left and right are both bit variables, result is carry */
@@ -5528,11 +6092,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
            if ( IC_TRUE(ifx) ) {
              emitSKPNZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-             // pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+pic16_labelOffset);
            } else {
              emitSKPZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-             // pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+pic16_labelOffset);
            }
 
          } else {
@@ -5552,7 +6116,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
                        DEBUGpic16_emitcode (";","\tIC_TRUE emitSKPZ");
 
                  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key));
-                 pic16_emitcode(" goto","_%05d_DS_",tlbl->key+100+labelOffset);
+                 pic16_emitcode(" goto","_%05d_DS_",tlbl->key+100+pic16_labelOffset);
                } else {
                  emitSKPNZ;
 
@@ -5560,7 +6124,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
 
 
                  pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-                 pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+                 pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+pic16_labelOffset);
                }
              } else {
                emitSKPZ;
@@ -5568,13 +6132,13 @@ static void genCmpEq (iCode *ic, iCode *ifx)
                        DEBUGpic16_emitcode (";","\tnot IC_TRUE emitSKPZ");
 
                pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-               pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+               pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+pic16_labelOffset);
              }
              offset++;
            }
            if(s>1 && IC_TRUE(ifx)) {
              pic16_emitpLabel(tlbl->key);
-             pic16_emitcode("","_%05d_DS_:",tlbl->key+100+labelOffset);                
+             pic16_emitcode("","_%05d_DS_:",tlbl->key+100+pic16_labelOffset);                
            }
          }
         }
@@ -5656,6 +6220,7 @@ release:
     pic16_freeAsmop(right,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* ifxForOp - returns the icode containing the ifx for operand     */
@@ -5670,11 +6235,14 @@ static iCode *ifxForOp ( operand *op, iCode *ic )
     /* if this has register type condition and
     the next instruction is ifx with the same operand
     and live to of the operand is upto the ifx only then */
-    if (ic->next &&
-        ic->next->op == IFX &&
-        IC_COND(ic->next)->key == op->key &&
-        OP_SYMBOL(op)->liveTo <= ic->next->seq )
-        return ic->next;
+    if (ic->next
+       && ic->next->op == IFX
+        && IC_COND(ic->next)->key == op->key
+        && OP_SYMBOL(op)->liveTo <= ic->next->seq
+        ) {
+               DEBUGpic16_emitcode(";", "%d %s", __LINE__, __FUNCTION__);
+          return ic->next;
+    }
 
     if (ic->next &&
         ic->next->op == IFX &&
@@ -5697,6 +6265,35 @@ static iCode *ifxForOp ( operand *op, iCode *ic )
                           ic->next->seq);
     }
 
+#if 0
+    /* the code below is completely untested
+     * it just allows ulong2fs.c compile -- VR */
+        
+    ic = ic->next;
+    fprintf(stderr, "WARNING (%s:%s:%d) untested hack might produce wrong code\n",
+                                       __FILE__, __FUNCTION__, __LINE__);
+       
+    /* if this has register type condition and
+    the next instruction is ifx with the same operand
+    and live to of the operand is upto the ifx only then */
+    if (ic->next &&
+        ic->next->op == IFX &&
+        IC_COND(ic->next)->key == op->key &&
+        OP_SYMBOL(op)->liveTo <= ic->next->seq )
+        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;
+    }
+
+    fprintf(stderr, "WARNING (%s:%s:%d) untested hack might produce wrong code (returning NULL)\n",
+                                       __FILE__, __FUNCTION__, __LINE__);
+
+//  return ic->next->next;             /* this just might work */ /* FIXME FIXME */
+#endif
 
     return NULL;
 }
@@ -5782,9 +6379,9 @@ static void genOrOp (iCode *ic)
         tlbl = newiTempLabel(NULL);
         pic16_toBoolean(left);
        emitSKPZ;
-        pic16_emitcode("goto","%05d_DS_",tlbl->key+100+labelOffset);
+        pic16_emitcode("goto","%05d_DS_",tlbl->key+100+pic16_labelOffset);
         pic16_toBoolean(right);
-        pic16_emitcode("","%05d_DS_:",tlbl->key+100+labelOffset);
+        pic16_emitcode("","%05d_DS_:",tlbl->key+100+pic16_labelOffset);
 
         pic16_outBitAcc(result);
     }
@@ -5976,8 +6573,25 @@ static void genAnd (iCode *ic, iCode *ifx)
            pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ic)->key));
          }
 */
+       DEBUGpic16_emitcode("***", "%d %s", __LINE__, __FUNCTION__);
+       size = AOP_SIZE(left);
+
+       {
+         int bp = posbit, ofs=0;
+         
+           while(bp > 7) {
+             bp -= 8;
+             ofs++;
+           }
+       
+         pic16_emitpcode(((rIfx.condition) ? POC_BTFSC : POC_BTFSS),
+                   pic16_newpCodeOpBit(pic16_aopGet(AOP(left),ofs,FALSE,FALSE),bp,0, PO_GPR_REGISTER));
+
+       }
+/*
          pic16_emitpcode(((rIfx.condition) ? POC_BTFSC : POC_BTFSS),
                    pic16_newpCodeOpBit(pic16_aopGet(AOP(left),0,FALSE,FALSE),posbit,0));
+*/
          pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rIfx.lbl->key));
          
          ifx->generated = 1;
@@ -6041,7 +6655,7 @@ static void genAnd (iCode *ic, iCode *ifx)
            if(p>=0) {
              /* only one bit is set in the literal, so use a bcf instruction */
 //           pic16_emitcode("bcf","%s,%d",pic16_aopGet(AOP(left),offset,FALSE,TRUE),p);
-             pic16_emitpcode(POC_BCF,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offset,FALSE,FALSE),p,0));
+             pic16_emitpcode(POC_BCF,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offset,FALSE,FALSE),p,0, PO_GPR_REGISTER));
 
            } else {
              pic16_emitcode("movlw","0x%x", (lit & 0xff));
@@ -6345,7 +6959,7 @@ static void genOr (iCode *ic, iCode *ifx)
            if(p>=0) {
              /* only one bit is set in the literal, so use a bsf instruction */
              pic16_emitpcode(POC_BSF,
-                       pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offset,FALSE,FALSE),p,0));
+                       pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offset,FALSE,FALSE),p,0, PO_GPR_REGISTER));
            } else {
              if(know_W != (lit & 0xff))
                pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit & 0xff));
@@ -7065,7 +7679,7 @@ static void shiftR1Left2ResultSigned (operand *left, int offl,
     pic16_emitpcode(POC_RLCFW,  pic16_popGet(AOP(result),offr));
     pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x1f));
 
-    pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),3,0));
+    pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),3,0, PO_GPR_REGISTER));
     pic16_emitpcode(POC_IORLW, pic16_popGetLit(0xe0));
 
     pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr));
@@ -7074,7 +7688,7 @@ static void shiftR1Left2ResultSigned (operand *left, int offl,
   case 4:
     pic16_emitpcode(POC_SWAPFW, pic16_popGet(AOP(left),offl));
     pic16_emitpcode(POC_ANDLW,  pic16_popGetLit(0x0f));
-    pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0));
+    pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0, PO_GPR_REGISTER));
     pic16_emitpcode(POC_IORLW,  pic16_popGetLit(0xf0));
     pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),offr));
     break;
@@ -7087,7 +7701,7 @@ static void shiftR1Left2ResultSigned (operand *left, int offl,
     }
     pic16_emitpcode(POC_RRCFW,   pic16_popGet(AOP(result),offr));
     pic16_emitpcode(POC_ANDLW,  pic16_popGetLit(0x07));
-    pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),3,0));
+    pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),3,0, PO_GPR_REGISTER));
     pic16_emitpcode(POC_IORLW,  pic16_popGetLit(0xf8));
     pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),offr));
     break;
@@ -7095,29 +7709,29 @@ static void shiftR1Left2ResultSigned (operand *left, int offl,
   case 6:
     if(same) {
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x00));
-      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0));
+      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0xfe));
-      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),6,0));
+      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),6,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_IORLW, pic16_popGetLit(0x01));
       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr));
     } else {
       pic16_emitpcode(POC_CLRF,  pic16_popGet(AOP(result),offr));
-      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0));
+      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_DECF,  pic16_popGet(AOP(result),offr));
-      pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),6,0));
-      pic16_emitpcode(POC_BCF,   pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),0,0));
+      pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),6,0, PO_GPR_REGISTER));
+      pic16_emitpcode(POC_BCF,   pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),0,0, PO_GPR_REGISTER));
     }
     break;
 
   case 7:
     if(same) {
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x00));
-      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),LSB,FALSE,FALSE),7,0));
+      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),LSB,FALSE,FALSE),7,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0xff));
       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr));
     } else {
       pic16_emitpcode(POC_CLRF,  pic16_popGet(AOP(result),offr));
-      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0));
+      pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),offl,FALSE,FALSE),7,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_DECF,  pic16_popGet(AOP(result),offr));
     }
 
@@ -7536,7 +8150,7 @@ static void shiftR2Left2Result (operand *left, int offl,
     if(sign) {
       pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0xf0 + (shCount-4)*8 ));
       pic16_emitpcode(POC_BTFSC, 
-               pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),7-shCount,0));
+               pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),7-shCount,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),offr+MSB16));
     }
 
@@ -7554,7 +8168,7 @@ static void shiftR2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_ANDLW,pic16_popGetLit(0x03));
       if(sign) {
        pic16_emitpcode(POC_BTFSC, 
-                 pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),1,0));
+                 pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),1,0, PO_GPR_REGISTER));
        pic16_emitpcode(POC_IORLW,pic16_popGetLit(0xfc));
       }
       pic16_emitpcode(POC_XORFW,pic16_popGet(AOP(result),offr+MSB16));
@@ -7572,7 +8186,7 @@ static void shiftR2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_ANDLW,pic16_popGetLit(0x03));
       if(sign) {
        pic16_emitpcode(POC_BTFSC, 
-                 pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),0,0));
+                 pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),0,0, PO_GPR_REGISTER));
        pic16_emitpcode(POC_IORLW,pic16_popGetLit(0xfc));
       }
       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offr+MSB16));
@@ -7963,15 +8577,15 @@ static void genLeftShift (iCode *ic)
     if(optimized_for_speed) {
       pic16_emitpcode(POC_SWAPFW, pic16_popGet(AOP(left),0));
       pic16_emitpcode(POC_ANDLW,  pic16_popGetLit(0xf0));
-      pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),2,0));
+      pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),2,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_MOVFW,  pic16_popGet(AOP(left),0));
       pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),0));
-      pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0));
+      pic16_emitpcode(POC_BTFSS,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_ADDWF,  pic16_popGet(AOP(result),0));
       pic16_emitpcode(POC_RLCFW,   pic16_popGet(AOP(result),0));
       pic16_emitpcode(POC_ANDLW,  pic16_popGetLit(0xfe));
       pic16_emitpcode(POC_ADDFW,  pic16_popGet(AOP(result),0));
-      pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),1,0));
+      pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),1,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_ADDWF,  pic16_popGet(AOP(result),0));
     } else {
 
@@ -8097,7 +8711,7 @@ static void genrshTwo (operand *result,operand *left,
     pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB16));
 
     if(sign) {
-      pic16_emitpcode(POC_BTFSC,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),LSB,FALSE,FALSE),7,0));
+      pic16_emitpcode(POC_BTFSC,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),LSB,FALSE,FALSE),7,0, PO_GPR_REGISTER));
       pic16_emitpcode(POC_DECF, pic16_popGet(AOP(result),MSB16));
     }
   }
@@ -8266,14 +8880,14 @@ static void genRightShiftLiteral (operand *left,
     if(res_size == 1) {
       pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),LSB));
       if(sign) {
-       pic16_emitpcode(POC_BTFSC,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),lsize-1,FALSE,FALSE),7,0));
+       pic16_emitpcode(POC_BTFSC,pic16_newpCodeOpBit(pic16_aopGet(AOP(left),lsize-1,FALSE,FALSE),7,0, PO_GPR_REGISTER));
        pic16_emitpcode(POC_DECF, pic16_popGet(AOP(result),LSB));
       }
     } else {
 
       if(sign) {
        pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0));
-       pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),lsize-1,FALSE,FALSE),7,0));
+       pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(left),lsize-1,FALSE,FALSE),7,0, PO_GPR_REGISTER));
        pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0xff));
        while(res_size--)
          pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),res_size));
@@ -8699,7 +9313,9 @@ static void genDataPointerGet(operand *left,
                DEBUGpic16_emitcode("; ***", "left and result operands are equ/same");
                goto release;
        }
+#endif
 
+#if 0
        /* if they are the same registers */
        if (pic16_sameRegs(AOP(left),AOP(result))) {
                DEBUGpic16_emitcode("; ***", "left and result registers are same");
@@ -8707,6 +9323,15 @@ static void genDataPointerGet(operand *left,
        }
 #endif
 
+#if 1
+       if(!strcmp(pic16_aopGet(AOP(result), 0, TRUE, FALSE),
+               pic16_aopGet(AOP(left), 0, TRUE, FALSE))) {
+               DEBUGpic16_emitcode("; ***", "left and result names are same, skipping moving");
+               goto release;
+       }
+#endif
+
+
 #if 0
        if ( AOP_TYPE(left) == AOP_PCODE) {
                fprintf(stderr,"genDataPointerGet   %s, %d\n",
@@ -8739,7 +9364,7 @@ static void genDataPointerGet(operand *left,
                leoffset++;
        }
 
-//release:
+release:
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
@@ -8767,8 +9392,8 @@ static void genNearPointerGet (operand *left,
     
        pic16_aopOp(left,ic,FALSE);
 
-       pic16_DumpOp("(left)",left);
-       pic16_DumpOp("(result)",result);
+//     pic16_DumpOp("(left)",left);
+//     pic16_DumpOp("(result)",result);
 
        /* if left is rematerialisable and
         * result is not bit variable type and
@@ -9199,11 +9824,13 @@ static void genConstPointerGet (operand *left,
       pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_tablat), pic16_popGet(AOP(result),offset)));
       offset++;
     }
-                                                                       // .... patch 15
+    
   pic16_freeAsmop(left,NULL,ic,TRUE);
   pic16_freeAsmop(result,NULL,ic,TRUE);
 
 }
+
+
 /*-----------------------------------------------------------------*/
 /* genPointerGet - generate code for pointer get                   */
 /*-----------------------------------------------------------------*/
@@ -9290,6 +9917,11 @@ static void genPointerGet (iCode *ic)
 #endif
        genGenPointerGet (left,result,ic);
       break;
+
+    default:
+      werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
+             "genPointerGet: illegal pointer type");
+    
     }
 
 }
@@ -9312,14 +9944,30 @@ static void genPackBits (sym_link    *etype ,
        bstr = SPEC_BSTR(etype);
 
        if(AOP_TYPE(right) == AOP_LIT) {
+               if((blen == 1) && (bstr < 8)) {
+                 unsigned long lit;
+                       /* it is a single bit, so use the appropriate bit instructions */
+
+                       DEBUGpic16_emitcode (";","%s %d optimize bit assignment",__FUNCTION__,__LINE__);
+
+                       lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
+//                     pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+                       if(lit) {
+                               pic16_emitpcode(POC_BSF,
+                                       pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr));
+                       } else {
+                               pic16_emitpcode(POC_BCF,
+                                       pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr));
+                       }
+       
+                 return;
+               }
+
                pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right), 0));
                offset++;
        } else
                pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++));
 
-/*     l = pic16_aopGet(AOP(right),offset++,FALSE,FALSE);
-       MOVA(l);   
-*/
        /* if the bit lenth is less than or    */
        /* it exactly fits a byte then         */
        if((shCnt=SPEC_BSTR(etype))
@@ -9348,28 +9996,6 @@ static void genPackBits (sym_link    *etype ,
                pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
 #endif
 
-#if 0
-               pic16_emitcode ("anl","a,#0x%02x",(unsigned char)
-                                       ((unsigned char)(0xFF << (blen+bstr)) | 
-                                       (unsigned char)(0xFF >> (8-bstr)) ) );
-               pic16_emitcode ("orl","a,b");
-               if (p_type == GPOINTER)
-                       pic16_emitcode("pop","b");
-
-               
-               switch (p_type) {
-                       case POINTER:
-                               pic16_emitcode("mov","@%s,a",rname);
-                               break;
-                       case FPOINTER:
-                               pic16_emitcode("movx","@dptr,a");
-                               break;
-                       case GPOINTER:
-                               DEBUGpic16_emitcode(";lcall","__gptrput");
-                               break;
-               }
-#endif
-
          return;
        }
 
@@ -9478,9 +10104,10 @@ static void genDataPointerSet(operand *right,
     pic16_aopOp(right,ic,FALSE);
 
     size = AOP_SIZE(right);
-       fprintf(stderr, "%s:%d size= %d\n", __FILE__, __LINE__, size);
 
-#if 1
+//     fprintf(stderr, "%s:%d size= %d\n", __FILE__, __LINE__, size);
+
+#if 0
     if ( AOP_TYPE(result) == AOP_PCODE) {
       fprintf(stderr,"genDataPointerSet   %s, %d\n",
              AOP(result)->aopu.pcop->name,
@@ -10034,11 +10661,18 @@ static void genAddrOf (iCode *ic)
 
        size = AOP_SIZE(IC_RESULT(ic));
 
-       if(pic16_debug_verbose) {
-               fprintf(stderr, "%s:%d %s symbol %s , codespace=%d\n",
-                       __FILE__, __LINE__, __FUNCTION__, sym->name, IN_CODESPACE( SPEC_OCLS(sym->etype)));
+
+       if(sym->onStack) {
+               DEBUGpic16_emitcode(";    ", "%s symbol %s on stack", __FUNCTION__, sym->name);
+       
+               return;
        }
        
+//     if(pic16_debug_verbose) {
+//             fprintf(stderr, "%s:%d %s symbol %s , codespace=%d\n",
+//                     __FILE__, __LINE__, __FUNCTION__, sym->name, IN_CODESPACE( SPEC_OCLS(sym->etype)));
+//     }
+       
        /* Assume that what we want the address of is in data space
         * since there is no stack on the PIC, yet! -- VR */
        /* low */
@@ -10181,18 +10815,80 @@ static void genAssign (iCode *ic)
   /* general case */
   size = AOP_SIZE(result);
   offset = 0 ;
-  if(AOP_TYPE(right) == AOP_LIT)
-    lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
+
+  if(AOP_TYPE(right) == AOP_LIT) {
+       if(!IS_FLOAT(operandType( right )))
+               lit = (unsigned long)floatFromVal(AOP(right)->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(right)->aopu.aop_lit);
+               lit = info.lit_int;
+       }
+  }
+
+//  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_LIT
+       && IN_CODESPACE(SPEC_OCLS(OP_SYMBOL(right)->etype))) {
+       DEBUGpic16_emitcode(";   ", "%s:%d symbol in code space, take special care\n", __FUNCTION__, __LINE__);
+
+       // set up table pointer
+       if( (AOP_TYPE(right) == AOP_PCODE)
+               && ((AOP(right)->aopu.pcop->type == PO_IMMEDIATE)
+               || (AOP(right)->aopu.pcop->type == PO_DIR)))
+       {
+               pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),0));
+               pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptrl));
+               pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),1));
+               pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptrh));
+               pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),2));
+               pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptru));
+       } else {
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),0),
+                               pic16_popCopyReg(&pic16_pc_tblptrl)));
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),1),
+                               pic16_popCopyReg(&pic16_pc_tblptrh)));
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),2),
+                               pic16_popCopyReg(&pic16_pc_tblptru)));
+       }
+
+       size = min(AOP_SIZE(right), AOP_SIZE(result));
+       while(size--) {
+               pic16_emitpcodeNULLop(POC_TBLRD_POSTINC);
+               pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_tablat),
+                       pic16_popGet(AOP(result),offset)));
+               offset++;
+       }
+
+       if(AOP_SIZE(result) > AOP_SIZE(right)) {
+               size = AOP_SIZE(result) - AOP_SIZE(right);
+               while(size--) {
+                       pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), offset));
+                       offset++;
+               }
+       }
+       goto release;
+  }
+
+
 
 /* VR - What is this?! */
   if( AOP_TYPE(right) == AOP_DIR  && (AOP_TYPE(result) == AOP_REG) && size==1)  {
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     if(aopIdx(AOP(result),0) == 4) {
   DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+       assert(0);
       pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset));
       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset));
       goto release;
     } else
+//     assert(0);
       DEBUGpic16_emitcode ("; WARNING","%s  %d ignoring register storage",__FUNCTION__,__LINE__);
   }
 
@@ -10483,7 +11179,7 @@ static void genCast (iCode *ic)
                /* we need to or */
                if (AOP_TYPE(right) == AOP_REG) {
                        pic16_emitpcode(POC_BCF,    pic16_popGet(AOP(result),0));
-                       pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0));
+                       pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0, PO_GPR_REGISTER));
                        pic16_emitpcode(POC_BSF,    pic16_popGet(AOP(result),0));
                }
                pic16_toBoolean(right);
@@ -10692,8 +11388,9 @@ static void genCast (iCode *ic)
     size = AOP_SIZE(right);
     offset = 0 ;
     while (size--) {
-      pic16_emitpcode(POC_MOVFW,   pic16_popGet(AOP(right),offset));
-      pic16_emitpcode(POC_MOVWF,   pic16_popGet(AOP(result),offset));
+      pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset));
+//      pic16_emitpcode(POC_MOVFW,   pic16_popGet(AOP(right),offset));
+//      pic16_emitpcode(POC_MOVWF,   pic16_popGet(AOP(result),offset));
       offset++;
     }
 
@@ -10709,15 +11406,15 @@ static void genCast (iCode *ic)
       if(size == 1) {
        /* Save one instruction of casting char to int */
        pic16_emitpcode(POC_CLRF,   pic16_popGet(AOP(result),offset));
-       pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset-1,FALSE,FALSE),7,0));
+       pic16_emitpcode(POC_BTFSC,  pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset-1,FALSE,FALSE),7,0, PO_GPR_REGISTER));
        pic16_emitpcode(POC_DECF,   pic16_popGet(AOP(result),offset));
       } else {
         pic16_emitpcode(POC_CLRF,pic16_popCopyReg(&pic16_pc_wreg));
 
        if(offset)
-         pic16_emitpcode(POC_BTFSC,   pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset-1,FALSE,FALSE),7,0));
+         pic16_emitpcode(POC_BTFSC,   pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset-1,FALSE,FALSE),7,0, PO_GPR_REGISTER));
        else
-         pic16_emitpcode(POC_BTFSC,   pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset,FALSE,FALSE),7,0));
+         pic16_emitpcode(POC_BTFSC,   pic16_newpCodeOpBit(pic16_aopGet(AOP(right),offset,FALSE,FALSE),7,0, PO_GPR_REGISTER));
        
        pic16_emitpcode(POC_MOVLW,   pic16_popGetLit(0xff));
 
@@ -10780,7 +11477,7 @@ static int genDjnz (iCode *ic, iCode *ifx)
       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
 
       pic16_emitcode("decfsz","%s,f",pic16_aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
-      pic16_emitcode ("goto","_%05d_DS_",IC_TRUE(ifx)->key+100 + labelOffset);
+      pic16_emitcode ("goto","_%05d_DS_",IC_TRUE(ifx)->key+100 + pic16_labelOffset);
 
     }
 /*     pic16_emitcode ("sjmp","%05d_DS_",lbl1->key+100); */
@@ -10801,6 +11498,12 @@ static void genReceive (iCode *ic)
 {    
        DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
+#if 0
+       fprintf(stderr, "%s:%d %s for symbol %s\tonStack: %d\n", __FILE__, __LINE__, __FUNCTION__,
+               OP_SYMBOL( IC_RESULT(ic) )->rname, OP_SYMBOL( IC_RESULT(ic) )->onStack);
+#endif
+//     pic16_DumpOp(__FUNCTION__, IC_RESULT(ic));
+
        if (isOperandInFarSpace(IC_RESULT(ic)) &&
                ( OP_SYMBOL(IC_RESULT(ic))->isspilt ||
                IS_TRUE_SYMOP(IC_RESULT(ic))) ) {
@@ -10808,6 +11511,7 @@ static void genReceive (iCode *ic)
          int size = getSize(operandType(IC_RESULT(ic)));
          int offset =  pic16_fReturnSizePic - size;
 
+               assert( 0 );
                while (size--) {
                        pic16_emitcode ("push","%s", (strcmp(fReturn[pic16_fReturnSizePic - offset - 1],"a") ?
                                        fReturn[pic16_fReturnSizePic - offset - 1] : "acc"));
@@ -10874,15 +11578,6 @@ void genpic16Code (iCode *lic)
     if (options.debug && currFunc) {
       if (currFunc) {
        cdbSymbol(currFunc,cdbFile,FALSE,TRUE);
-       _G.debugLine = 1;
-       if (IS_STATIC(currFunc->etype)) {
-         pic16_emitcode("",";F%s$%s$0$0     %d",moduleName,currFunc->name,__LINE__);
-         //pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(moduleName,currFunc->name));
-       } else {
-         pic16_emitcode("",";G$%s$0$0   %d",currFunc->name,__LINE__);
-         //pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(NULL,currFunc->name));
-       }
-       _G.debugLine = 0;
       }
     }
 #endif
@@ -10892,11 +11587,7 @@ void genpic16Code (iCode *lic)
       DEBUGpic16_emitcode(";ic ", "\t%c 0x%x",ic->op, ic->op);
        if ( cln != ic->lineno ) {
            if ( options.debug ) {
-               _G.debugLine = 1;
-               pic16_emitcode("",";C$%s$%d$%d$%d ==.",
-                        FileBaseName(ic->filename),ic->lineno,
-                        ic->level,ic->block);
-               _G.debugLine = 0;
+             debugFile->writeCLine (ic);
            }
            
            if(!options.noCcodeInAsm) {
@@ -10909,8 +11600,10 @@ void genpic16Code (iCode *lic)
        }
        
        if(options.iCodeInAsm) {
+               char *l;
                /* insert here code to print iCode as comment */
-               pic16_emitpcomment("ic:%d: %s", ic->seq, printILine(ic));
+               l = Safe_strdup(printILine(ic));
+               pic16_emitpcomment("ic:%d: %s", ic->seq, l);
        }
        
        /* if the result is marked as