Fixed some pointer bugs. Added option to selectively enable/disable function inlining.
[fw/sdcc] / src / pic / genarith.c
index a4e501bb7489af652e8d55a78d236be5c94a036c..fb6b94980374aaadcc76acd1d4c10498933a2299 100644 (file)
@@ -106,10 +106,67 @@ const char *AopType(short type)
   case AOP_ACC:
     return "AOP_ACC";
     break;
+  case AOP_PCODE:
+    return "AOP_PCODE";
+    break;
   }
 
   return "BAD TYPE";
 }
+
+const char *pCodeOpType(  pCodeOp *pcop)
+{
+
+  if(pcop) {
+
+    switch(pcop->type) {
+
+    case  PO_NONE:
+      return "PO_NONE";
+    case  PO_W:
+      return  "PO_W";
+    case  PO_STATUS:
+      return  "PO_STATUS";
+    case  PO_FSR:
+      return  "PO_FSR";
+    case  PO_INDF:
+      return  "PO_INDF";
+    case  PO_INTCON:
+      return  "PO_INTCON";
+    case  PO_GPR_REGISTER:
+      return  "PO_GPR_REGISTER";
+    case  PO_GPR_BIT:
+      return  "PO_GPR_BIT";
+    case  PO_GPR_TEMP:
+      return  "PO_GPR_TEMP";
+    case  PO_SFR_REGISTER:
+      return  "PO_SFR_REGISTER";
+    case  PO_PCL:
+      return  "PO_PCL";
+    case  PO_PCLATH:
+      return  "PO_PCLATH";
+    case  PO_LITERAL:
+      return  "PO_LITERAL";
+    case  PO_IMMEDIATE:
+      return  "PO_IMMEDIATE";
+    case  PO_DIR:
+      return  "PO_DIR";
+    case  PO_CRY:
+      return  "PO_CRY";
+    case  PO_BIT:
+      return  "PO_BIT";
+    case  PO_STR:
+      return  "PO_STR";
+    case  PO_LABEL:
+      return  "PO_LABEL";
+    case  PO_WILD:
+      return  "PO_WILD";
+    }
+  }
+
+  return "BAD PO_TYPE";
+}
+
 /*-----------------------------------------------------------------*/
 /* genPlusIncr :- does addition with increment if possible         */
 /*-----------------------------------------------------------------*/
@@ -786,6 +843,7 @@ void genPlus (iCode *ic)
     DEBUGpic14_emitcode(";","adding lit to something. size %d",size);
 
     genAddLit (ic,  lit);
+    goto release;
 
   } else if(AOP_TYPE(IC_RIGHT(ic)) == AOP_CRY) {
 
@@ -901,14 +959,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));
        }
       }
     }
@@ -1100,22 +1157,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++));
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -1472,5 +1540,296 @@ void genMinus (iCode *ic)
   freeAsmop(IC_RIGHT(ic),NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
   freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
 }
+/*-----------------------------------------------------------------*
+ * genUMult8XLit_16 - unsigned multiplication of two 8-bit numbers.
+ * 
+ * 
+ *-----------------------------------------------------------------*/
+void genUMult8XLit_16 (operand *left,
+                      operand *right,
+                      operand *result,
+                      pCodeOpReg *result_hi)
+
+{
+
+  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__);
+    exit(1);
+  }
+
+
+  if(!result_hi) {
+    result_hi = PCOR(popGet(AOP(result),1));
+  }
+
+  lit = (unsigned int)floatFromVal(AOP(right)->aopu.aop_lit);
+  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;
+    }
+
+  }
+
+  emitpcode(POC_MOVFW, popGet(AOP(left),0));
+  emitpcode(POC_CLRF,  popGet(AOP(result),0));
+  emitpcode(POC_CLRF,  popCopyReg(result_hi));
+
+  have_first_bit = 0;
+  for(i=0; i<8; i++) {
+
+    if(lit & 1) {
+      emitpcode(POC_ADDWF, popCopyReg(result_hi));
+      have_first_bit = 1;
+    }
+
+    if(have_first_bit) {
+      emitpcode(POC_RRF,   popCopyReg(result_hi));
+      emitpcode(POC_RRF,   popGet(AOP(result),0));
+    }
+
+    lit >>= 1;
+  }
+
+}
+
+/*-----------------------------------------------------------------*
+ * genUMult8X8_16 - unsigned multiplication of two 8-bit numbers.
+ * 
+ * 
+ *-----------------------------------------------------------------*/
+void genUMult8X8_16 (operand *left,
+                    operand *right,
+                    operand *result,
+                    pCodeOpReg *result_hi)
+
+{
+
+  int i;
+  int looped = 1;
+
+  if(!result_hi) {
+    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");
+
+    emitpcode(POC_MOVFW, popGet(AOP(right),0));
+    emitpcode(POC_CLRF,  popGet(AOP(result),0));
+    emitpcode(POC_CLRF,  popCopyReg(result_hi));
+    emitCLRC;
+
+    for(i=0; i<8; i++) {
+      emitpcode(POC_BTFSC,  newpCodeOpBit(aopGet(AOP(left),0,FALSE,FALSE),i,0));
+      emitpcode(POC_ADDWF, popCopyReg(result_hi));
+      emitpcode(POC_RRF,   popCopyReg(result_hi));
+      emitpcode(POC_RRF,   popGet(AOP(result),0));
+    }
+
+
+    /*
+      Here's another version that does the same thing and takes the 
+      same number of instructions. The one above is slightly better
+      because the entry instructions have a higher probability of
+      being optimized out.
+    */
+    /*
+      emitpcode(POC_CLRF,  popCopyReg(result_hi));
+      emitpcode(POC_RRFW,  popGet(AOP(left),0));
+      emitpcode(POC_MOVWF, popGet(AOP(result),0));
+      emitpcode(POC_MOVFW, popGet(AOP(right),0));
+
+      for(i=0; i<8; i++) {
+      emitSKPNC;
+      emitpcode(POC_ADDWF, popCopyReg(result_hi));
+      emitpcode(POC_RRF,   popCopyReg(result_hi));
+      emitpcode(POC_RRF,   popGet(AOP(result),0));
+      }
+    */
+
+  } else {
+    symbol  *tlbl = newiTempLabel(NULL);
+    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));
+
+    emitpLabel(tlbl->key);
+
+    emitpcode(POC_RRF,   popCopyReg(PCOR(temp)));
+    emitSKPNC;
+    emitpcode(POC_ADDWF, popCopyReg(result_hi));
+
+    emitpcode(POC_RRF,   popCopyReg(result_hi));
+    emitpcode(POC_RRF,   popGet(AOP(result),0));
+
+    emitSKPC;
+    emitpcode(POC_GOTO,  popGetLabel(tlbl->key));
+
+    popReleaseTempReg(temp);
+
+  }
+}
+
+/*-----------------------------------------------------------------*
+ * genSMult8X8_16 - signed multiplication of two 8-bit numbers
+ *
+ *  this routine will call the unsigned multiply routine and then
+ * post-fix the sign bit.
+ *-----------------------------------------------------------------*/
+void genSMult8X8_16 (operand *left,
+                    operand *right,
+                    operand *result,
+                    pCodeOpReg *result_hi)
+{
 
+  if(!result_hi) {
+    result_hi = PCOR(popGet(AOP(result),1));
+  }
+
+  genUMult8X8_16(left,right,result,result_hi);
 
+  emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(left),0,FALSE,FALSE),7,0));
+  emitpcode(POC_SUBWF, popCopyReg(result_hi));
+  emitpcode(POC_MOVFW, popGet(AOP(left),0));
+  emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(right),0,FALSE,FALSE),7,0));
+  emitpcode(POC_SUBWF, popGet(AOP(result),1));
+  
+}
+
+/*-----------------------------------------------------------------*
+ * genMult8X8_8 - multiplication of two 8-bit numbers
+ *
+ *  this routine will call the unsigned multiply 8X8=>16 routine and
+ * then throw away the high byte of the result.
+ *
+ *-----------------------------------------------------------------*/
+void genMult8X8_8 (operand *left,
+                  operand *right,
+                  operand *result)
+{
+  pCodeOp *result_hi = popGetTempReg();
+
+  if (AOP_TYPE(right) == AOP_LIT)
+    genUMult8XLit_16(left,right,result,PCOR(result_hi));
+  else
+    genUMult8X8_16(left,right,result,PCOR(result_hi));
+
+  popReleaseTempReg(result_hi);
+}
+#if 0
+/*-----------------------------------------------------------------*/
+/* constMult - generates code for multiplication by a constant     */
+/*-----------------------------------------------------------------*/
+void genMultConst(unsigned C)
+{
+
+  unsigned lit;
+  unsigned sr3; // Shift right 3
+  unsigned mask;
+
+  int size = 1;
+
+  /*
+    Convert a string of 3 binary 1's in the lit into
+    0111 = 1000 - 1;
+  */
+
+  mask = 7 << ( (size*8) - 3);
+  lit = C;
+  sr3 = 0;
+
+  while(mask < (1<<size*8)) {
+
+    if( (mask & lit) == lit) {
+      unsigned lsb;
+
+      /* We found 3 (or more) consecutive 1's */
+
+      lsb = mask & ~(mask & (mask-1));  // lsb of mask.
+
+      consecutive_bits = ((lit + lsb) & lit) ^ lit;
+
+      lit ^= consecutive_bits;
+
+      mask <<= 3;
+
+      sr3 |= (consecutive + lsb);
+
+    }
+
+    mask >>= 1;
+
+  }
+
+}
+
+#endif