* src/mcs51/gen.c (genNearPointerGet, genPagedPointerGet,
[fw/sdcc] / src / pic16 / gen.c
index 9bd719d2b2c2936d1001b91d7b3b3aaf8c447442..73ca2c02114fd1d6dfecbc69e07f2d717fe402c3 100644 (file)
@@ -273,6 +273,10 @@ void pic16_emitpcodeNULLop(PIC_OPCODE poc)
 
 }
 
+
+#if 1
+#define pic16_emitcode DEBUGpic16_emitcode
+#else
 /*-----------------------------------------------------------------*/
 /* pic16_emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
@@ -309,6 +313,7 @@ void pic16_emitcode (char *inst,char *fmt, ...)
 
     va_end(ap);
 }
+#endif
 
 
 /*-----------------------------------------------------------------*/
@@ -3804,6 +3809,7 @@ static void genIfxJump (iCode *ic, char *jval)
 /*-----------------------------------------------------------------*/
 static void genSkip(iCode *ifx,int status_bit)
 {
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   if(!ifx)
     return;
 
@@ -3824,7 +3830,7 @@ static void genSkip(iCode *ifx,int status_bit)
     }
 
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-    pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
 
   } else {
 
@@ -3843,7 +3849,7 @@ static void genSkip(iCode *ifx,int status_bit)
       break;
     }
     pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-    pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+    // pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
 
   }
 
@@ -3883,6 +3889,7 @@ static void genSkipz2(resolvedIfx *rifx, int invert_condition)
   rifx->generated = 1;
 }
 
+#if 0
 /*-----------------------------------------------------------------*/
 /* genSkipz                                                        */
 /*-----------------------------------------------------------------*/
@@ -3907,6 +3914,8 @@ static void genSkipz(iCode *ifx, int condition)
     pic16_emitcode("goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
 
 }
+#endif
+
 /*-----------------------------------------------------------------*/
 /* genSkipCond                                                     */
 /*-----------------------------------------------------------------*/
@@ -4960,6 +4969,8 @@ static void genCmpEq (iCode *ic, iCode *ifx)
     operand *left, *right, *result;
     unsigned long lit = 0L;
     int size,offset=0;
+    symbol *falselbl  = newiTempLabel(NULL);
+
 
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
@@ -4989,9 +5000,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
 
     if(ifx && !AOP_SIZE(result)){
         symbol *tlbl;
+       DEBUGpic16_emitcode ("; ***","%s  %d CASE 1",__FUNCTION__,__LINE__);
         /* if they are both bit variables */
         if (AOP_TYPE(left) == AOP_CRY &&
             ((AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(right) == AOP_LIT))) {
+               DEBUGpic16_emitcode ("; ***","%s  %d CASE 11",__FUNCTION__,__LINE__);
             if(AOP_TYPE(right) == AOP_LIT){
                 unsigned long lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
                 if(lit == 0L){
@@ -5022,96 +5035,102 @@ static void genCmpEq (iCode *ic, iCode *ifx)
             }
             pic16_emitcode("","%05d_DS_:",tlbl->key+100+labelOffset);
 
-           {
-             /* left and right are both bit variables, result is carry */
-             resolvedIfx rIfx;
+               {
+               /* left and right are both bit variables, result is carry */
+                       resolvedIfx rIfx;
              
-             resolveIfx(&rIfx,ifx);
-
-             pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),0));
-             pic16_emitpcode(POC_ANDFW,pic16_popGet(AOP(left),0));
-             pic16_emitpcode(POC_BTFSC,pic16_popGet(AOP(right),0));
-             pic16_emitpcode(POC_ANDLW,pic16_popGet(AOP(left),0));
-             genSkipz2(&rIfx,0);
-           }
+                       resolveIfx(&rIfx,ifx);
+
+                       pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(left),0));
+                       pic16_emitpcode(POC_ANDFW,pic16_popGet(AOP(left),0));
+                       pic16_emitpcode(POC_BTFSC,pic16_popGet(AOP(right),0));
+                       pic16_emitpcode(POC_ANDLW,pic16_popGet(AOP(left),0));
+                       genSkipz2(&rIfx,0);
+               }
         } else {
 
-         /* They're not both bit variables. Is the right a literal? */
-         if(AOP_TYPE(right) == AOP_LIT) {
-           lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
-           
-           switch(size) {
-
-           case 1:
-             switch(lit & 0xff) {
-             case 1:
-               if ( IC_TRUE(ifx) ) {
-                 pic16_emitpcode(POC_DECFW,pic16_popGet(AOP(left),offset));
-                 emitSKPNZ;
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-               } else {
-                 pic16_emitpcode(POC_DECFSZW,pic16_popGet(AOP(left),offset));
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-               }
-               break;
-             case 0xff:
-               if ( IC_TRUE(ifx) ) {
-                 pic16_emitpcode(POC_INCFW,pic16_popGet(AOP(left),offset));
-                 emitSKPNZ;
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-               } else {
-                 pic16_emitpcode(POC_INCFSZW,pic16_popGet(AOP(left),offset));
-                 pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-               }
-               break;
-             default:
-               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offset));
-               if(lit)
-                 pic16_emitpcode(POC_XORLW,pic16_popGetLit(lit & 0xff));
-               genSkip(ifx,'z');
-             }
-
+                       DEBUGpic16_emitcode ("; ***","%s  %d CASE 12",__FUNCTION__,__LINE__);
 
-             /* end of size == 1 */
-             break;
+                       /* They're not both bit variables. Is the right a literal? */
+                       if(AOP_TYPE(right) == AOP_LIT) {
+                       lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
+           
+                       switch(size) {
+
+                               case 1:
+                                       switch(lit & 0xff) {
+                                               case 1:
+                                                               if ( IC_TRUE(ifx) ) {
+                                                                       pic16_emitpcode(POC_DECFW,pic16_popGet(AOP(left),offset));
+                                                                       emitSKPNZ;
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
+                                                               } else {
+                                                                       pic16_emitpcode(POC_DECFSZW,pic16_popGet(AOP(left),offset));
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
+                                                               }
+                                                               break;
+                                               case 0xff:
+                                                               if ( IC_TRUE(ifx) ) {
+                                                                       pic16_emitpcode(POC_INCFW,pic16_popGet(AOP(left),offset));
+                                                                       emitSKPNZ;
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
+                                                               } else {
+                                                                       pic16_emitpcode(POC_INCFSZW,pic16_popGet(AOP(left),offset));
+                                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
+                                                               }
+                                                               break;
+                                               default:
+                                                               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offset));
+                                                               if(lit)
+                                                                       pic16_emitpcode(POC_XORLW,pic16_popGetLit(lit & 0xff));
+                                                               genSkip(ifx,'z');
+                                       } // switch lit
+
+
+                                       /* end of size == 1 */
+                                       break;
              
-           case 2:
-             genc16bit2lit(left,lit,offset);
-             genSkip(ifx,'z');
-             break;
-             /* end of size == 2 */
-
-           default:
-             /* size is 4 */
-             if(lit==0) {
-               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),1));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
-               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
-
-             } else {
-
-               /* search for patterns that can be optimized */
-
-               genc16bit2lit(left,lit,0);
-               lit >>= 16;
-               if(lit) {
-                 genSkipz(ifx,IC_TRUE(ifx) == NULL);
-                 //genSkip(ifx,'z');
-                 genc16bit2lit(left,lit,2);
-               } else {
-                 pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
-                 pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
-
-               }
-               
-             }
-
-             genSkip(ifx,'z');
-           }
+                               case 2:
+                                       genc16bit2lit(left,lit,offset);
+                                       genSkip(ifx,'z');
+                                       break;
+                                       /* end of size == 2 */
+
+                               default:
+                                       /* size is 4 */
+                                       if(lit==0) {
+                                               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),1));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
+                                               pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
+                                               genSkip(ifx,'z');
+                                       } else {
+                                               /* search for patterns that can be optimized */
+
+                                               genc16bit2lit(left,lit,0);
+                                               lit >>= 16;
+                                               if(lit) {
+                                                               if(IC_TRUE(ifx))
+                                                               emitSKPZ; // if hi word unequal
+                                                               else
+                                                               emitSKPNZ; // if hi word equal
+                                                               // fail early
+                                                       pic16_emitpcode(POC_GOTO,pic16_popGetLabel(falselbl->key));
+                                                       genc16bit2lit(left,lit,2);
+                                                       genSkip(ifx,'z');
+                                               } else {
+                                                       pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),2));
+                                                       pic16_emitpcode(POC_IORFW,pic16_popGet(AOP(left),3));
+                                                       genSkip(ifx,'z');
+                                               }
+                                       }
+                                               pic16_emitpLabel(falselbl->key);
+                                               break;
+
+                       } // switch size
          
-           ifx->generated = 1;
-           goto release ;
+                       ifx->generated = 1;
+                       goto release ;
            
 
          } else if(AOP_TYPE(right) == AOP_CRY ) {
@@ -5131,11 +5150,11 @@ static void genCmpEq (iCode *ic, iCode *ifx)
            if ( IC_TRUE(ifx) ) {
              emitSKPNZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_TRUE(ifx)->key));
-             pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_TRUE(ifx)->key+100+labelOffset);
            } else {
              emitSKPZ;
              pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_FALSE(ifx)->key));
-             pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
+             // pic16_emitcode(" goto","_%05d_DS_",IC_FALSE(ifx)->key+100+labelOffset);
            }
 
          } else {
@@ -5189,6 +5208,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
     /* if they are both bit variables */
     if (AOP_TYPE(left) == AOP_CRY &&
         ((AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(right) == AOP_LIT))) {
+       DEBUGpic16_emitcode ("; ***","%s  %d CASE 2",__FUNCTION__,__LINE__);
         if(AOP_TYPE(right) == AOP_LIT){
             unsigned long lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
             if(lit == 0L){
@@ -5221,7 +5241,7 @@ static void genCmpEq (iCode *ic, iCode *ifx)
         pic16_outBitC(result);
     } else {
       
-      DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+      DEBUGpic16_emitcode ("; ***","%s  %d CASE 3",__FUNCTION__,__LINE__);
       gencjne(left,right,result,ifx);
 /*
       if(ifx) 
@@ -6341,6 +6361,8 @@ static void genRRC (iCode *ic)
   operand *left , *result ;
   int size, offset = 0, same;
 
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
   /* rotate right with carry */
   left = IC_LEFT(ic);
   result=IC_RESULT(ic);
@@ -6353,6 +6375,8 @@ static void genRRC (iCode *ic)
 
   size = AOP_SIZE(result);    
 
+  DEBUGpic16_emitcode ("; ***","%s  %d size:%d same:%d",__FUNCTION__,__LINE__,size,same);
+
   /* get the lsb and put it into the carry */
   pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),size-1));
 
@@ -6448,6 +6472,7 @@ static void genGetHbit (iCode *ic)
     pic16_freeAsmop(result,NULL,ic,TRUE);
 }
 
+#if 0
 /*-----------------------------------------------------------------*/
 /* AccRol - rotate left accumulator by known count                 */
 /*-----------------------------------------------------------------*/
@@ -6485,6 +6510,7 @@ static void AccRol (int shCount)
             break;
     }
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* AccLsh - left shift accumulator by known count                  */
@@ -6492,20 +6518,38 @@ static void AccRol (int shCount)
 static void AccLsh (int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(shCount != 0){
-        if(shCount == 1)
-            pic16_emitcode("add","a,acc");
-        else 
-           if(shCount == 2) {
-            pic16_emitcode("add","a,acc");
-            pic16_emitcode("add","a,acc");
-        } else {
-            /* rotate left accumulator */
-            AccRol(shCount);
-            /* and kill the lower order bits */
-            pic16_emitcode("anl","a,#0x%02x", SLMask[shCount]);
-        }
+    switch(shCount){
+        case 0 :
+                       return;
+            break;
+        case 1 :
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 2 :
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 3 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 4 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 5 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 6 :
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 7 :
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
     }
+    pic16_emitpcode(POC_ANDLW,pic16_popGetLit(SLMask[shCount]));
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -6514,17 +6558,37 @@ static void AccLsh (int shCount)
 static void AccRsh (int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(shCount != 0){
-        if(shCount == 1){
-            CLRC;
-            pic16_emitcode("rrc","a");
-        } else {
-            /* rotate right accumulator */
-            AccRol(8 - shCount);
-            /* and kill the higher order bits */
-            pic16_emitcode("anl","a,#0x%02x", SRMask[shCount]);
-        }
+    switch(shCount){
+        case 0 :
+                       return;
+            break;
+        case 1 :
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 2 :
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 3 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 4 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 5 :
+            pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 6 :
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
+        case 7 :
+            pic16_emitpcode(POC_RLNCFW,pic16_popCopyReg(&pic16_pc_wreg));
+            break;
     }
+    pic16_emitpcode(POC_ANDLW,pic16_popGetLit(SRMask[shCount]));
 }
 
 #if 0
@@ -6862,11 +6926,23 @@ static void movLeft2Result (operand *left, int offl,
 static void shiftL2Left2Result (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
+  int same = pic16_sameRegs(AOP(result), AOP(left));
+  int i;
 
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d same:%d offl:%d offr:%d",__FUNCTION__,__LINE__,shCount,same,offl,offr);
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  if (same && (offl != offr)) { // shift bytes
+    if (offr > offl) {
+       for(i=1;i>-1;i--) {
+         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),offl+i));
+         pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offr+i));
+       }
+    } else { // just treat as different later on
+               same = 0;
+    }
+  }
 
-  if(pic16_sameRegs(AOP(result), AOP(left))) {
+  if(same) {
     switch(shCount) {
     case 0:
       break;
@@ -6879,9 +6955,9 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_RLCF,  pic16_popGet(AOP(result),offr+MSB16));
 
       while(--shCount) {
-       emitCLRC;
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               emitCLRC;
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
 
       break;
@@ -6895,8 +6971,8 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_XORWF, pic16_popGet(AOP(result),offr));
       pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),offr+MSB16));
       if(shCount >=5) {
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
     case 6:
@@ -6935,9 +7011,9 @@ static void shiftL2Left2Result (operand *left, int offl,
       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr+MSB16));
 
       while(--shCount) {
-       emitCLRC;
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               emitCLRC;
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
 
@@ -6954,8 +7030,8 @@ static void shiftL2Left2Result (operand *left, int offl,
 
 
       if(shCount == 5) {
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
-       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr));
+               pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr+MSB16));
       }
       break;
     case 6:
@@ -6990,29 +7066,21 @@ static void shiftR2Left2Result (operand *left, int offl,
                                 operand *result, int offr,
                                 int shCount, int sign)
 {
-  int same=0;
-
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-  same = pic16_sameRegs(AOP(result), AOP(left));
+  int same = pic16_sameRegs(AOP(result), AOP(left));
+  int i;
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d same:%d sign:%d",__FUNCTION__,__LINE__,shCount,same,sign);
 
-  if(same && ((offl + MSB16) == offr)){
-    same=1;
-    /* don't crash result[offr] */
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
-    pic16_emitcode("xch","a,%s", pic16_aopGet(AOP(left),offl+MSB16,FALSE,FALSE));
-  }
-/* else {
-    movLeft2Result(left,offl, result, offr);
-    MOVA(pic16_aopGet(AOP(left),offl+MSB16,FALSE,FALSE));
+  if (same && (offl != offr)) { // shift right bytes
+    if (offr < offl) {
+       for(i=0;i<2;i++) {
+         pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(result),offl+i));
+         pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offr+i));
+       }
+    } else { // just treat as different later on
+               same = 0;
+    }
   }
-*/
-  /* a:x >> shCount (x = lsb(result))*/
-/*
-  if(sign)
-    AccAXRshS( pic16_aopGet(AOP(result),offr,FALSE,FALSE) , shCount);
-  else {
-    AccAXRsh( pic16_aopGet(AOP(result),offr,FALSE,FALSE) , shCount);
-*/
+
   switch(shCount) {
   case 0:
     break;
@@ -7036,9 +7104,9 @@ static void shiftR2Left2Result (operand *left, int offl,
 
     while(--shCount) {
       if(sign)
-       pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(result),offr+MSB16));
+               pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(result),offr+MSB16));
       else
-       emitCLRC;
+               emitCLRC;
       pic16_emitpcode(POC_RRCF,pic16_popGet(AOP(result),offr+MSB16));
       pic16_emitpcode(POC_RRCF,pic16_popGet(AOP(result),offr));
     }
@@ -7142,13 +7210,13 @@ static void shiftLLeftOrResult (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
+
+    pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offl));
     /* shift left accumulator */
     AccLsh(shCount);
     /* or with result */
-    pic16_emitcode("orl","a,%s", pic16_aopGet(AOP(result),offr,FALSE,FALSE));
     /* back to result */
-    pic16_aopPut(AOP(result),"a",offr);
+    pic16_emitpcode(POC_IORWF,pic16_popGet(AOP(result),offr));
 }
 
 /*-----------------------------------------------------------------*/
@@ -7158,13 +7226,13 @@ static void shiftRLeftOrResult (operand *left, int offl,
                                 operand *result, int offr, int shCount)
 {
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    MOVA(pic16_aopGet(AOP(left),offl,FALSE,FALSE));
+    
+    pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),offl));
     /* shift right accumulator */
     AccRsh(shCount);
     /* or with result */
-    pic16_emitcode("orl","a,%s", pic16_aopGet(AOP(result),offr,FALSE,FALSE));
     /* back to result */
-    pic16_aopPut(AOP(result),"a",offr);
+    pic16_emitpcode(POC_IORWF,pic16_popGet(AOP(result),offr));
 }
 
 /*-----------------------------------------------------------------*/
@@ -7183,7 +7251,7 @@ static void genlshTwo (operand *result,operand *left, int shCount)
 {
     int size;
     
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
     size = pic16_getDataSize(result);
 
     /* if shCount >= 8 */
@@ -7210,64 +7278,63 @@ static void genlshTwo (operand *result,operand *left, int shCount)
 
 /*-----------------------------------------------------------------*/
 /* shiftLLong - shift left one long from left to result            */
-/* offl = LSB or MSB16                                             */
+/* offr = LSB or MSB16                                             */
 /*-----------------------------------------------------------------*/
 static void shiftLLong (operand *left, operand *result, int offr )
 {
-    char *l;
     int size = AOP_SIZE(result);
+    int same = pic16_sameRegs(AOP(left),AOP(result));
+       int i;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(size >= LSB+offr){
-        l = pic16_aopGet(AOP(left),LSB,FALSE,FALSE);
-        MOVA(l);
-        pic16_emitcode("add","a,acc");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB16+offr && offr != LSB )
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),LSB+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",LSB+offr);
-    }
+    DEBUGpic16_emitcode ("; ***","%s  %d  offr:%d size:%d",__FUNCTION__,__LINE__,offr,size);
 
-    if(size >= MSB16+offr){
-       if (!(pic16_sameRegs(AOP(result),AOP(left)) && size >= MSB16+offr && offr != LSB) ) {
-           l = pic16_aopGet(AOP(left),MSB16,FALSE,FALSE);
-           MOVA(l);
+       if (same && (offr == MSB16)) { //shift one byte
+               for(i=size-1;i>=MSB16;i--) {
+                       pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),i-1));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(left),i));
+               }
+       } else {
+               pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),LSB+offr));
        }
-        pic16_emitcode("rlc","a");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB24+offr && offr != LSB)
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),MSB16+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",MSB16+offr);
+       
+    if (size >= LSB+offr ){
+               if (same) {
+                       pic16_emitpcode(POC_ADDWF,pic16_popGet(AOP(left),LSB+offr));
+               } else {
+                       pic16_emitpcode(POC_ADDFW,pic16_popGet(AOP(left),LSB));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),LSB+offr));
+               }
+        }
+
+    if(size >= MSB16+offr){
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB16+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB16));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB16+offr));
+               }
     }
 
     if(size >= MSB24+offr){
-       if (!(pic16_sameRegs(AOP(left),AOP(left)) && size >= MSB24+offr && offr != LSB)) {
-           l = pic16_aopGet(AOP(left),MSB24,FALSE,FALSE);
-           MOVA(l);
-       }
-        pic16_emitcode("rlc","a");
-       if (pic16_sameRegs(AOP(left),AOP(result)) && 
-           size >= MSB32+offr && offr != LSB )
-           pic16_emitcode("xch","a,%s",
-                    pic16_aopGet(AOP(left),MSB24+offr,FALSE,FALSE));
-       else        
-           pic16_aopPut(AOP(result),"a",MSB24+offr);
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB24+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB24));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB24+offr));
+               }
     }
 
     if(size > MSB32+offr){
-       if (!(pic16_sameRegs(AOP(result),AOP(left)) && size >= MSB32+offr && offr != LSB)) {
-           l = pic16_aopGet(AOP(left),MSB32,FALSE,FALSE);
-           MOVA(l);    
-       }
-        pic16_emitcode("rlc","a");
-        pic16_aopPut(AOP(result),"a",MSB32+offr);
+               if (same) {
+                       pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB32+offr));
+               } else {
+                       pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),MSB32));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),MSB32+offr));
+               }
     }
     if(offr != LSB)
-        pic16_aopPut(AOP(result),zero,LSB);       
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -7289,9 +7356,11 @@ static void genlshFour (operand *result, operand *left, int shCount)
             shiftL1Left2Result(left, LSB, result, MSB32, shCount);
         else
             movLeft2Result(left, LSB, result, MSB32);
-        pic16_aopPut(AOP(result),zero,LSB);
-        pic16_aopPut(AOP(result),zero,MSB16);
-        pic16_aopPut(AOP(result),zero,MSB32);
+
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB16));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB24));
+
         return;
     }
 
@@ -7306,8 +7375,8 @@ static void genlshFour (operand *result, operand *left, int shCount)
             movLeft2Result(left, MSB16, result, MSB32);
             movLeft2Result(left, LSB, result, MSB24);
         }
-        pic16_aopPut(AOP(result),zero,MSB16);
-        pic16_aopPut(AOP(result),zero,LSB);
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
+               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),MSB16));
         return;
     }    
 
@@ -7326,7 +7395,7 @@ static void genlshFour (operand *result, operand *left, int shCount)
                 movLeft2Result(left, MSB24, result, MSB32);
                 movLeft2Result(left, MSB16, result, MSB24);
                 movLeft2Result(left, LSB, result, MSB16);
-                pic16_aopPut(AOP(result),zero,LSB);
+                               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
             }
             else if(shCount == 1)
                 shiftLLong(left, result, MSB16);
@@ -7334,15 +7403,16 @@ static void genlshFour (operand *result, operand *left, int shCount)
                 shiftL2Left2Result(left, MSB16, result, MSB24, shCount);
                 shiftL1Left2Result(left, LSB, result, MSB16, shCount);
                 shiftRLeftOrResult(left, LSB, result, MSB24, 8 - shCount);
-                pic16_aopPut(AOP(result),zero,LSB);
+                               pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),LSB));
             }
         }
     }
 
     /* 1 <= shCount <= 7 */
-    else if(shCount <= 2){
+    else if(shCount <= 3)
+    { 
         shiftLLong(left, result, LSB);
-        if(shCount == 2)
+        while(--shCount >= 1)
             shiftLLong(result, result, LSB);
     }
     /* 3 <= shCount <= 7, optimize */
@@ -7364,7 +7434,7 @@ static void genLeftShiftLiteral (operand *left,
     int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
     int size;
 
-    DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+    DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
     pic16_freeAsmop(right,NULL,ic,TRUE);
 
     pic16_aopOp(left,ic,FALSE);
@@ -7514,8 +7584,8 @@ static void genLeftShift (iCode *ic)
 
       tlbl = newiTempLabel(NULL);
       if (!pic16_sameRegs(AOP(left),AOP(result))) {
-       pic16_emitpcode(POC_MOVFW,  pic16_popGet(AOP(left),0));
-       pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),0));
+               pic16_emitpcode(POC_MOVFW,  pic16_popGet(AOP(left),0));
+               pic16_emitpcode(POC_MOVWF,  pic16_popGet(AOP(result),0));
       }
 
       pic16_emitpcode(POC_COMFW,  pic16_popGet(AOP(right),0));
@@ -7577,7 +7647,8 @@ static void genLeftShift (iCode *ic)
   pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0));
 
   /* offset should be 0, 1 or 3 */
-  pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x07 + ((offset&3) << 3)));
+  
+  pic16_emitpcode(POC_ANDLW, pic16_popGetLit((size<<3)-1));
   emitSKPNZ;
   pic16_emitpcode(POC_GOTO,  pic16_popGetLabel(tlbl1->key));
 
@@ -7620,7 +7691,7 @@ static void genrshOne (operand *result, operand *left,
 static void genrshTwo (operand *result,operand *left,
                        int shCount, int sign)
 {
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d",__FUNCTION__,__LINE__,shCount);
   /* if shCount >= 8 */
   if (shCount >= 8) {
     shCount -= 8 ;
@@ -7650,31 +7721,60 @@ static void genrshTwo (operand *result,operand *left,
 static void shiftRLong (operand *left, int offl,
                         operand *result, int sign)
 {
+    int size = AOP_SIZE(result);
+    int same = pic16_sameRegs(AOP(left),AOP(result));
+    int i;
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(!sign)
-        pic16_emitcode("clr","c");
-    MOVA(pic16_aopGet(AOP(left),MSB32,FALSE,FALSE));
+
+    DEBUGpic16_emitcode ("; ***","%s  %d  offl:%d size:%d",__FUNCTION__,__LINE__,offl,size);
+
+       if (same && (offl == MSB16)) { //shift one byte right
+               for(i=MSB16;i<size;i++) {
+                       pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),i));
+                       pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(left),i-1));
+               }
+       }
+
     if(sign)
-        pic16_emitcode("mov","c,acc.7");
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB32-offl);
-    if(offl == MSB16)
+               pic16_emitpcode(POC_RLCFW,pic16_popGet(AOP(left),MSB32));
+       else
+               emitCLRC;
+
+       if (same) {
+               if (offl == LSB)
+               pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB32));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB32));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB32-offl));
+       }
+
+    if(offl == MSB16) {
         /* add sign of "a" */
         pic16_addSign(result, MSB32, sign);
+       }
 
-    MOVA(pic16_aopGet(AOP(left),MSB24,FALSE,FALSE));
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB24-offl);
-
-    MOVA(pic16_aopGet(AOP(left),MSB16,FALSE,FALSE));
-    pic16_emitcode("rrc","a");
-    pic16_aopPut(AOP(result),"a",MSB16-offl);
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB24));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB24));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB24-offl));
+       }
+       
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),MSB16));
+       } else {
+       pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),MSB16));
+       pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),MSB16-offl));
+       }
 
-    if(offl == LSB){
-        MOVA(pic16_aopGet(AOP(left),LSB,FALSE,FALSE));
-        pic16_emitcode("rrc","a");
-        pic16_aopPut(AOP(result),"a",LSB);
-    }
+       if (same) {
+       pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(left),LSB));
+       } else {
+       if(offl == LSB){
+               pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),LSB));
+               pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),LSB));
+       }
+       }
 }
 
 /*-----------------------------------------------------------------*/
@@ -7714,7 +7814,7 @@ static void genrshFour (operand *result, operand *left,
       movLeft2Result(left, MSB32, result, MSB24);
       pic16_addSign(result, MSB32, sign);
     }
-    else{
+    else{ //shcount >= 2
       shiftR2Left2Result(left, MSB16, result, LSB, shCount, 0);
       shiftLLeftOrResult(left, MSB32, result, MSB16, 8 - shCount);
       /* the last shift is signed */
@@ -7748,12 +7848,13 @@ static void genRightShiftLiteral (operand *left,
   int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit);
   int lsize,res_size;
 
-  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   pic16_freeAsmop(right,NULL,ic,TRUE);
 
   pic16_aopOp(left,ic,FALSE);
   pic16_aopOp(result,ic,FALSE);
 
+  DEBUGpic16_emitcode ("; ***","%s  %d shCount:%d result:%d left:%d",__FUNCTION__,__LINE__,shCount,AOP_SIZE(result),AOP_SIZE(left));
+
 #if VIEW_SIZE
   pic16_emitcode("; shift right ","result %d, left %d",AOP_SIZE(result),
                 AOP_SIZE(left));
@@ -7963,17 +8064,17 @@ static void genSignedRightShift (iCode *ic)
 static void genRightShift (iCode *ic)
 {
     operand *right, *left, *result;
-    sym_link *retype ;
+    sym_link *letype ;
     int size, offset;
     char *l;
     symbol *tlbl, *tlbl1 ;
 
     /* if signed then we do it the hard way preserve the
     sign bit moving it inwards */
-    retype = getSpec(operandType(IC_RESULT(ic)));
+    letype = getSpec(operandType(IC_LEFT(ic)));
     DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
-    if (!SPEC_USIGN(retype)) {
+    if (!SPEC_USIGN(letype)) {
         genSignedRightShift (ic);
         return ;
     }
@@ -8270,7 +8371,7 @@ static void genNearPointerGet (operand *left,
     pic16_aopOp (result,ic,FALSE);
     
       /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
        genUnpackBits (result,rname,POINTER);
     else {
        /* we have can just get the values */
@@ -8368,7 +8469,7 @@ static void genPagedPointerGet (operand *left,
     pic16_aopOp (result,ic,FALSE);
 
     /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
        genUnpackBits (result,rname,PPOINTER);
     else {
        /* we have can just get the values */
@@ -8446,7 +8547,7 @@ static void genFarPointerGet (operand *left,
     pic16_aopOp(result,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genUnpackBits(result,"dptr",FPOINTER);
     else {
         size = AOP_SIZE(result);
@@ -8496,7 +8597,7 @@ static void genCodePointerGet (operand *left,
     pic16_aopOp(result,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genUnpackBits(result,"dptr",CPOINTER);
     else {
         size = AOP_SIZE(result);
@@ -8558,8 +8659,8 @@ static void genGenPointerGet (operand *left,
   /* so dptr know contains the address */
 
   /* if bit then unpack */
-  //if (IS_BITVAR(retype)) 
-  //  genUnpackBits(result,"dptr",GPOINTER);
+       if (IS_BITFIELD(retype)) 
+       genUnpackBits(result,"BAD",GPOINTER);
 
  release:
   pic16_freeAsmop(left,NULL,ic,TRUE);
@@ -8972,14 +9073,15 @@ static void genNearPointerSet (operand *right,
     goto release;
 
   }// else
-  //   rname = pic16_aopGet(AOP(result),0,FALSE,FALSE);
+//     rname = pic16_aopGet(AOP(result),0,FALSE,FALSE);
 
+  DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
 
   /* if bitfield then unpack the bits */
-  if (IS_BITVAR(retype)) {
+  if (IS_BITFIELD(retype)) {
     werror(E_INTERNAL_ERROR,__FILE__,__LINE__,
           "The programmer is obviously confused");
-    //genPackBits (retype,right,rname,POINTER);
+//     genPackBits (retype,right,"BAD",POINTER);
     exit(1);
   }
   else {
@@ -9081,7 +9183,7 @@ static void genPagedPointerSet (operand *right,
     pic16_aopOp (right,ic,FALSE);
 
     /* if bitfield then unpack the bits */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
        genPackBits (retype,right,rname,PPOINTER);
     else {
        /* we have can just get the values */
@@ -9159,7 +9261,7 @@ static void genFarPointerSet (operand *right,
     pic16_aopOp(right,ic,FALSE);
 
     /* if bit then unpack */
-    if (IS_BITVAR(retype)) 
+    if (IS_BITFIELD(retype)) 
         genPackBits(retype,right,"dptr",FPOINTER);
     else {
         size = AOP_SIZE(right);
@@ -9256,7 +9358,7 @@ static void genGenPointerSet (operand *right,
 
 
   /* if bit then unpack */
-  if (IS_BITVAR(retype)) 
+  if (IS_BITFIELD(retype)) 
     genPackBits(retype,right,"dptr",GPOINTER);
   else {
     size = AOP_SIZE(right);
@@ -10326,6 +10428,7 @@ void genpic16Code (iCode *lic)
        
        if(options.iCodeInAsm) {
                /* insert here code to print iCode as comment */
+               pic16_emitcomment("; ic:%d: %s", ic->seq, printILine(ic));
        }
        
        /* if the result is marked as
@@ -10515,8 +10618,6 @@ void genpic16Code (iCode *lic)
            break;
            
        case SEND:
-             DEBUGpic16_emitcode(";ic ", "\t%c 0x%x\tSEND",ic->op, ic->op);
-
            addSet(&_G.sendSet,ic);
            break;