Fixed bug #621531 (const & volatile confusion in the type chain).
[fw/sdcc] / src / pic / gen.c
index 28d5db4e1663222fc8112aba877a1cac824271d4..f8b48f8d243ca284937b96dfdb9a542947fccc0e 100644 (file)
@@ -216,7 +216,6 @@ void emitpLabel(int key)
 
 void emitpcode(PIC_OPCODE poc, pCodeOp *pcop)
 {
-
   if(pcop)
     addpCode2pBlock(pb,newpCode(poc,pcop));
   else
@@ -230,6 +229,16 @@ void emitpcodeNULLop(PIC_OPCODE poc)
 
 }
 
+void emitpcodePagesel(const char *label)
+{
+
+  char code[81];
+  strcpy(code,"\tpagesel ");
+  strcat(code,label);
+  addpCode2pBlock(pb,newpCodeInlineP(code));
+
+}
+
 /*-----------------------------------------------------------------*/
 /* pic14_emitcode - writes the code into a file : for now it is simple    */
 /*-----------------------------------------------------------------*/
@@ -601,7 +610,11 @@ static asmop *aopForRemat (operand *op) // x symbol *sym)
 
   offset = OP_SYMBOL(IC_LEFT(ic))->offset;
   aop->aopu.pcop = popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname,0,val,0);
+#if 0
   PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op));
+#else
+  PCOI(aop->aopu.pcop)->_const = IS_CODEPTR(operandType(op));
+#endif
   PCOI(aop->aopu.pcop)->index = val;
 
   DEBUGpic14_emitcode(";","%d: rname %s, val %d, const = %d",
@@ -754,7 +767,11 @@ void aopOp (operand *op, iCode *ic, bool result)
 
     {
       sym_link *type = operandType(op);
+#if 0
       if(IS_PTR_CONST(type))
+#else
+      if(IS_CODEPTR(type))
+#endif
        DEBUGpic14_emitcode(";","%d aop type is const pointer",__LINE__);
     }
 
@@ -873,7 +890,11 @@ void aopOp (operand *op, iCode *ic, bool result)
 
     {
       sym_link *type = operandType(op);
+#if 0
       if(IS_PTR_CONST(type)) 
+#else
+      if(IS_CODEPTR(type)) 
+#endif
        DEBUGpic14_emitcode(";","%d aop type is const pointer",__LINE__);
     }
 
@@ -1182,12 +1203,28 @@ pCodeOp *popGetLabel(unsigned int key)
 
   DEBUGpic14_emitcode ("; ***","%s  key=%d, label offset %d",__FUNCTION__,key, labelOffset);
 
-  if(key>max_key)
+  if(key>(unsigned int)max_key)
     max_key = key;
 
   return newpCodeOpLabel(NULL,key+100+labelOffset);
 }
 
+/*-------------------------------------------------------------------*/
+/* popGetLabel - create a new pCodeOp of type PO_LABEL with offset=1 */
+/*-------------------------------------------------------------------*/
+pCodeOp *popGetHighLabel(unsigned int key)
+{
+  pCodeOp *pcop;
+  DEBUGpic14_emitcode ("; ***","%s  key=%d, label offset %d",__FUNCTION__,key, labelOffset);
+
+  if(key>(unsigned int)max_key)
+    max_key = key;
+
+  pcop = newpCodeOpLabel(NULL,key+100+labelOffset);
+  PCOLAB(pcop)->offset = 1;
+  return pcop;
+}
+
 /*-----------------------------------------------------------------*/
 /* popCopyReg - copy a pcode operator                              */
 /*-----------------------------------------------------------------*/
@@ -1707,44 +1744,6 @@ static void reAdjustPreg (asmop *aop)
 
 }
 
-/*-----------------------------------------------------------------*/
-/* genNotFloat - generates not for float operations              */
-/*-----------------------------------------------------------------*/
-static void genNotFloat (operand *op, operand *res)
-{
-    int size, offset;
-    char *l;
-    symbol *tlbl ;
-
-    DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* we will put 127 in the first byte of 
-    the result */
-    aopPut(AOP(res),"#127",0);
-    size = AOP_SIZE(op) - 1;
-    offset = 1;
-
-    l = aopGet(op->aop,offset++,FALSE,FALSE);
-    MOVA(l);    
-
-    while(size--) {
-        pic14_emitcode("orl","a,%s",
-                 aopGet(op->aop,
-                        offset++,FALSE,FALSE));
-    }
-    tlbl = newiTempLabel(NULL);
-
-    tlbl = newiTempLabel(NULL);
-    aopPut(res->aop,one,1);
-    pic14_emitcode("jz","%05d_DS_",(tlbl->key+100));
-    aopPut(res->aop,zero,1);
-    pic14_emitcode("","%05d_DS_:",(tlbl->key+100));
-
-    size = res->aop->size - 2;
-    offset = 2;    
-    /* put zeros in the rest */
-    while (size--) 
-        aopPut(res->aop,zero,offset++);
-}
 
 #if 0
 /*-----------------------------------------------------------------*/
@@ -1859,7 +1858,6 @@ void pic14_toBoolean(operand *oper)
 static void genNot (iCode *ic)
 {
   symbol *tlbl;
-  sym_link *optype = operandType(IC_LEFT(ic));
   int size;
 
   DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
@@ -1881,13 +1879,7 @@ static void genNot (iCode *ic)
     goto release;
   }
 
-  /* if type float then do float */
-  if (IS_FLOAT(optype)) {
-    genNotFloat(IC_LEFT(ic),IC_RESULT(ic));
-    goto release;
-  }
-
-  size = AOP_SIZE(IC_RESULT(ic));
+  size = AOP_SIZE(IC_LEFT(ic));
   if(size == 1) {
     emitpcode(POC_COMFW,popGet(AOP(IC_LEFT(ic)),0));
     emitpcode(POC_ANDLW,popGetLit(1));
@@ -2554,6 +2546,7 @@ static void genPcall (iCode *ic)
     }
 */
     emitpcode(POC_CALL,popGetLabel(albl->key));
+    emitpcodePagesel(popGetLabel(blbl->key)->name); /* Must restore PCLATH before goto, without destroying W */
     emitpcode(POC_GOTO,popGetLabel(blbl->key));
     emitpLabel(albl->key);
 
@@ -2691,17 +2684,21 @@ static void genFunction (iCode *ic)
     }
 #endif
 
-    /* if this is an interrupt service routine then
-    save acc, b, dpl, dph  */
+    /* if this is an interrupt service routine */
     if (IFFUNC_ISISR(sym->type)) {
+/*  already done in pic14createInterruptVect() - delete me
       addpCode2pBlock(pb,newpCode(POC_GOTO,newpCodeOp("END_OF_INTERRUPT+1",PO_STR)));
       emitpcodeNULLop(POC_NOP);
       emitpcodeNULLop(POC_NOP);
       emitpcodeNULLop(POC_NOP);
+*/
       emitpcode(POC_MOVWF,  popCopyReg(&pc_wsave));
       emitpcode(POC_SWAPFW, popCopyReg(&pc_status));
       emitpcode(POC_CLRF,   popCopyReg(&pc_status));
       emitpcode(POC_MOVWF,  popCopyReg(&pc_ssave));
+      emitpcode(POC_MOVFW,  popCopyReg(&pc_pclath));
+      emitpcode(POC_MOVWF,  popCopyReg(&pc_psave));
+      emitpcode(POC_CLRF,   popCopyReg(&pc_pclath));/* durring an interrupt PCLATH must be cleared before a goto or call statement */
 
       pBlockConvert2ISR(pb);
 #if 0  
@@ -2936,18 +2933,17 @@ static void genEndFunction (iCode *ic)
                pic14_emitcode(";","XG$%s$0$0 ==.",currFunc->name);
            _G.debugLine = 0;
        }
-       
-        pic14_emitcode ("reti","");
-
-       emitpcode(POC_CLRF,   popCopyReg(&pc_status));
-       emitpcode(POC_SWAPFW, popCopyReg(&pc_ssave));
-       emitpcode(POC_MOVWF,  popCopyReg(&pc_status));
-       emitpcode(POC_SWAPF,  popCopyReg(&pc_wsave));
-       emitpcode(POC_SWAPFW, popCopyReg(&pc_wsave));
-       addpCode2pBlock(pb,newpCodeLabel("END_OF_INTERRUPT",-1));
-
-       emitpcodeNULLop(POC_RETFIE);
 
+        pic14_emitcode ("reti","");
+        emitpcode(POC_MOVFW,  popCopyReg(&pc_psave));
+        emitpcode(POC_MOVWF,  popCopyReg(&pc_pclath));
+        emitpcode(POC_CLRF,   popCopyReg(&pc_status));
+        emitpcode(POC_SWAPFW, popCopyReg(&pc_ssave));
+        emitpcode(POC_MOVWF,  popCopyReg(&pc_status));
+        emitpcode(POC_SWAPF,  popCopyReg(&pc_wsave));
+        emitpcode(POC_SWAPFW, popCopyReg(&pc_wsave));
+        addpCode2pBlock(pb,newpCodeLabel("END_OF_INTERRUPT",-1));
+        emitpcodeNULLop(POC_RETFIE);
     }
     else {
         if (IFFUNC_ISCRITICAL(sym->type))
@@ -5424,7 +5420,7 @@ static void genAnd (iCode *ic, iCode *ifx)
            } else {
              pic14_emitcode("movlw","0x%x", (lit & 0xff));
              pic14_emitcode("andwf","%s,f",aopGet(AOP(left),offset,FALSE,TRUE));
-             if(know_W != (lit&0xff))
+             if(know_W != (int)(lit&0xff))
                emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
              know_W = lit &0xff;
              emitpcode(POC_ANDWF,popGet(AOP(left),offset));
@@ -5731,7 +5727,7 @@ static void genOr (iCode *ic, iCode *ifx)
              emitpcode(POC_BSF,
                        newpCodeOpBit(aopGet(AOP(left),offset,FALSE,FALSE),p,0));
            } else {
-             if(know_W != (lit & 0xff))
+             if(know_W != (int)(lit & 0xff))
                emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
              know_W = lit & 0xff;
              emitpcode(POC_IORWF, popGet(AOP(left),offset));
@@ -8370,6 +8366,7 @@ static void genConstPointerGet (operand *left,
   DEBUGpic14_emitcode ("; "," %d getting const pointer",__LINE__);
 
   emitpcode(POC_CALL,popGetLabel(albl->key));
+  emitpcodePagesel(popGetLabel(blbl->key)->name); /* Must restore PCLATH before goto, without destroying W */
   emitpcode(POC_GOTO,popGetLabel(blbl->key));
   emitpLabel(albl->key);
 
@@ -8408,7 +8405,11 @@ static void genPointerGet (iCode *ic)
     type = operandType(left);
     etype = getSpec(type);
 
+#if 0
     if (IS_PTR_CONST(type))
+#else
+    if (IS_CODEPTR(type))
+#endif
       DEBUGpic14_emitcode ("; ***","%d - const pointer",__LINE__);
 
     /* if left is of type of pointer then it is simple */
@@ -8464,9 +8465,11 @@ static void genPointerGet (iCode *ic)
        break;
 
     case GPOINTER:
+#if 0
       if (IS_PTR_CONST(type))
        genConstPointerGet (left,result,ic);
       else
+#endif
        genGenPointerGet (left,result,ic);
       break;
     }
@@ -9328,7 +9331,7 @@ static void genAssign (iCode *ic)
   DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     if(AOP_TYPE(right) == AOP_LIT) {
       if(lit&0xff) {
-       if(know_W != (lit&0xff))
+       if(know_W != (int)(lit&0xff))
          emitpcode(POC_MOVLW,popGetLit(lit&0xff));
        know_W = lit&0xff;
        emitpcode(POC_MOVWF, popGet(AOP(result),offset));
@@ -9380,6 +9383,8 @@ static void genJumpTab (iCode *ic)
     pic14_emitcode("jmp","@a+dptr");
     pic14_emitcode("","%05d_DS_:",jtab->key+100);
 
+    emitpcode(POC_MOVLW, popGetHighLabel(jtab->key));
+    emitpcode(POC_MOVWF, popCopyReg(&pc_pclath));
     emitpcode(POC_MOVLW, popGetLabel(jtab->key));
     emitpcode(POC_ADDFW, popGet(AOP(IC_JTCOND(ic)),0));
     emitSKPNC;
@@ -9622,9 +9627,17 @@ static void genCast (iCode *ic)
        goto release;
 
       DEBUGpic14_emitcode("; ***","%s  %d",__FUNCTION__,__LINE__);
+#if 0
       if (IS_PTR_CONST(rtype))
+#else
+      if (IS_CODEPTR(rtype))
+#endif
        DEBUGpic14_emitcode ("; ***","%d - right is const pointer",__LINE__);
+#if 0
       if (IS_PTR_CONST(operandType(IC_RESULT(ic))))
+#else
+      if (IS_CODEPTR(operandType(IC_RESULT(ic))))
+#endif
        DEBUGpic14_emitcode ("; ***","%d - result is const pointer",__LINE__);
 
       if ((AOP_TYPE(right) == AOP_PCODE) && AOP(right)->aopu.pcop->type == PO_IMMEDIATE) {
@@ -9912,7 +9925,7 @@ genDummyRead (iCode * ic)
   pic14_emitcode ("; genDummyRead","");
   pic14_emitcode ("; not implemented","");
 
-  ic;
+  ic = ic;
 }
 
 /*-----------------------------------------------------------------*/