Improved the Register Banking algorithm.
[fw/sdcc] / src / pic / genarith.c
index 1741ad978be83da47c3551c1e7df64ca41ed8027..8b4aa9646260532df65ed693de1a09ff6097bc7a 100644 (file)
@@ -905,14 +905,13 @@ void genPlus (iCode *ic)
        if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) )
          emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),0));
        else {
-         if( (AOP_TYPE(IC_LEFT(ic)) == AOP_IMMD) ||
-             (AOP_TYPE(IC_LEFT(ic)) == AOP_LIT) ) {
-           emitpcode(POC_ADDLW, popGet(AOP(IC_LEFT(ic)),0));
-         } else {
-           emitpcode(POC_ADDFW, popGet(AOP(IC_LEFT(ic)),0));
-           if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC)
-             emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
-         }
+         PIC_OPCODE poc = POC_ADDFW;
+
+         if ((AOP_TYPE(IC_LEFT(ic)) == AOP_PCODE) && AOP(IC_LEFT(ic))->aopu.pcop->type == PO_LITERAL)
+           poc = POC_ADDLW;
+         emitpcode(poc, popGet(AOP(IC_LEFT(ic)),0));
+         if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC)
+           emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
        }
       }
     }
@@ -1104,22 +1103,33 @@ bool genMinusDec (iCode *ic)
 }
 
 /*-----------------------------------------------------------------*/
-/* addSign - complete with sign                                    */
+/* addSign - propogate sign bit to higher bytes                    */
 /*-----------------------------------------------------------------*/
 void addSign(operand *result, int offset, int sign)
 {
-    int size = (pic14_getDataSize(result) - offset);
-    DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    if(size > 0){
-        if(sign){
-            pic14_emitcode("rlc","a");
-            pic14_emitcode("subb","a,acc");
-            while(size--)
-                aopPut(AOP(result),"a",offset++); 
-        } else
-            while(size--)
-                aopPut(AOP(result),"#0",offset++);
-    }
+  int size = (pic14_getDataSize(result) - offset);
+  DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+
+  if(size > 0){
+    if(sign && offset) {
+
+      if(size == 1) {
+       emitpcode(POC_CLRF,popGet(AOP(result),offset));
+       emitpcode(POC_BTFSC,newpCodeOpBit(aopGet(AOP(result),offset-1,FALSE,FALSE),7,0));
+       emitpcode(POC_DECF, popGet(AOP(result),offset));
+      } else {
+
+       emitpcode(POC_MOVLW, popGetLit(0));
+       emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(result),offset-1,FALSE,FALSE),7,0));
+       emitpcode(POC_MOVLW, popGetLit(0xff));
+       while(size--)
+         emitpcode(POC_MOVWF, popGet(AOP(result),size));
+
+      }
+    } else
+      while(size--)
+       emitpcode(POC_CLRF,popGet(AOP(result),offset++));
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -1490,6 +1500,7 @@ void genUMult8XLit_16 (operand *left,
 
   unsigned int lit;
   unsigned int i,have_first_bit;
+  int same;
 
   if (AOP_TYPE(right) != AOP_LIT){
     fprintf(stderr,"%s %d - right operand is not a literal\n",__FILE__,__LINE__);
@@ -1505,11 +1516,55 @@ void genUMult8XLit_16 (operand *left,
   lit &= 0xff;
   pic14_emitcode(";","Unrolled 8 X 8 multiplication");
 
+  same = pic14_sameRegs(AOP(left), AOP(result));
+
+  if(same) {
+    switch(lit) {
+    case 0:
+      emitpcode(POC_CLRF,  popGet(AOP(left),0));
+      return;
+    case 2:
+      emitpcode(POC_MOVFW, popGet(AOP(left),0));
+      emitpcode(POC_ADDWF, popGet(AOP(left),0));
+      return;
+    case 3:
+      emitpcode(POC_MOVFW, popGet(AOP(left),0));
+      emitpcode(POC_ADDWF, popGet(AOP(left),0));
+      emitpcode(POC_ADDWF, popGet(AOP(left),0));
+      return;
+    case 4:
+      emitpcode(POC_MOVFW, popGet(AOP(left),0));
+      emitpcode(POC_ADDWF, popGet(AOP(left),0));
+      emitpcode(POC_ADDWF, popGet(AOP(left),0));
+      return;
+    case 16:
+      emitpcode(POC_SWAPFW, popGet(AOP(left),0));
+      emitpcode(POC_ANDLW,  popGetLit(0xf0));
+      emitpcode(POC_MOVWF,  popGet(AOP(left),0));
+      return;
+    case 17:
+      emitpcode(POC_SWAPFW, popGet(AOP(left),0));
+      emitpcode(POC_ANDLW,  popGetLit(0xf0));
+      emitpcode(POC_ADDWF,  popGet(AOP(left),0));
+      return;
+
+    }
+  } else {
+
+    switch(lit) {
+    case 0:
+      emitpcode(POC_CLRF,  popGet(AOP(result),0));
+      emitpcode(POC_CLRF,  popCopyReg(result_hi));
+      return;
+    case 2:
+      emitpcode(POC_MOVFW, popGet(AOP(left),0));
+      emitpcode(POC_MOVWF, popGet(AOP(result),0));
+      emitpcode(POC_ADDWF, popGet(AOP(result),0));
+      emitpcode(POC_CLRF,  popCopyReg(result_hi));
+      emitpcode(POC_RLF,   popCopyReg(result_hi));
+      return;
+    }
 
-  if(!lit) {
-    emitpcode(POC_CLRF,  popGet(AOP(result),0));
-    emitpcode(POC_CLRF,  popCopyReg(result_hi));
-    return;
   }
 
   emitpcode(POC_MOVFW, popGet(AOP(left),0));
@@ -1553,6 +1608,11 @@ void genUMult8X8_16 (operand *left,
     result_hi = PCOR(popGet(AOP(result),1));
   }
 
+  if (AOP_TYPE(right) == AOP_LIT) {
+    genUMult8XLit_16(left,right,result,result_hi);
+    return;
+  }
+
   if(!looped) {
     pic14_emitcode(";","Unrolled 8 X 8 multiplication");
 
@@ -1591,16 +1651,19 @@ void genUMult8X8_16 (operand *left,
 
   } else {
     symbol  *tlbl = newiTempLabel(NULL);
-    pCodeOp *temp = popGetTempReg();
+    pCodeOp *temp;
 
 
     pic14_emitcode(";","Looped 8 X 8 multiplication");
 
     emitpcode(POC_CLRF,  popGet(AOP(result),0));
     emitpcode(POC_CLRF,  popCopyReg(result_hi));
+
     emitpcode(POC_BSF,   newpCodeOpBit(aopGet(AOP(result),0,FALSE,FALSE),7,0));
 
     emitpcode(POC_MOVFW, popGet(AOP(right),0));
+
+    temp = popGetTempReg();
     emitpcode(POC_MOVWF, popCopyReg(PCOR(temp)));
 
     emitpcode(POC_MOVFW, popGet(AOP(left),0));