The function name in a pointer to function variable was getting declared
[fw/sdcc] / src / pic / gen.c
index b7e80966bfd7925f0b446596e8106799c6dbd6b2..95b75a2889064a4e9a3474b51c1ddcc5f827b4c7 100644 (file)
@@ -60,7 +60,7 @@ static int optimized_for_speed = 0;
 static int max_key=0;
 static int GpsuedoStkPtr=0;
 
-pCodeOp *popGetImmd(char *name, unsigned int offset, int index);
+pCodeOp *popGetImmd(char *name, unsigned int offset, int index,int is_func);
 unsigned int pic14aopLiteral (value *val, int offset);
 const char *AopType(short type);
 static iCode *ifxForOp ( operand *op, iCode *ic );
@@ -523,11 +523,19 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
 
     /* special case for a function */
     if (IS_FUNC(sym->type)) {   
+
+      sym->aop = aop = newAsmop(AOP_PCODE);
+      aop->aopu.pcop = popGetImmd(sym->rname,0,0,1);
+      PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space);
+      PCOI(aop->aopu.pcop)->_function = 1;
+      PCOI(aop->aopu.pcop)->index = 0;
+      aop->size = FPTRSIZE; 
+      /*
         sym->aop = aop = newAsmop(AOP_IMMD);    
-        //_ALLOC_ATOMIC(aop->aopu.aop_immd,strlen(sym->rname)+1);
        aop->aopu.aop_immd = Safe_calloc(1,strlen(sym->rname)+1);
         strcpy(aop->aopu.aop_immd,sym->rname);
         aop->size = FPTRSIZE; 
+      */
        DEBUGpic14_emitcode(";","%d size = %d, name =%s",__LINE__,aop->size,sym->rname);
         return aop;
     }
@@ -537,7 +545,7 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result)
     /* in which case DPTR gets the address */
     sym->aop = aop = newAsmop(AOP_PCODE);
 
-    aop->aopu.pcop = popGetImmd(sym->rname,0,0);
+    aop->aopu.pcop = popGetImmd(sym->rname,0,0,0);
     PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space);
     PCOI(aop->aopu.pcop)->index = 0;
 
@@ -592,7 +600,7 @@ 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);
+  aop->aopu.pcop = popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname,0,val,0);
   PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op));
   PCOI(aop->aopu.pcop)->index = val;
 
@@ -813,22 +821,34 @@ void aopOp (operand *op, iCode *ic, bool result)
        }
 
         if (sym->ruonly ) {
-         /*
-         sym->aop = op->aop = aop = newAsmop(AOP_PCODE);
-         aop->aopu.pcop = popGetImmd(sym->usl.spillLoc->rname,0,sym->usl.spillLoc->offset);
-         //allocDirReg (IC_LEFT(ic));
-         aop->size = getSize(sym->type);
-         */
+         if(sym->isptr) {  // && sym->uptr 
+           aop = op->aop = sym->aop = newAsmop(AOP_PCODE);
+           aop->aopu.pcop = newpCodeOp(NULL,PO_GPR_POINTER); //popCopyReg(&pc_fsr);
 
-         unsigned i;
+           //PCOI(aop->aopu.pcop)->_const = 0;
+           //PCOI(aop->aopu.pcop)->index = 0;
+           /*
+             DEBUGpic14_emitcode(";","%d: rname %s, val %d, const = %d",
+             __LINE__,sym->rname, 0, PCOI(aop->aopu.pcop)->_const);
+           */
+           //allocDirReg (IC_LEFT(ic));
 
-         aop = op->aop = sym->aop = newAsmop(AOP_STR);
-         aop->size = getSize(sym->type);
-         for ( i = 0 ; i < fReturnSizePic ; i++ )
-           aop->aopu.aop_str[i] = fReturn[i];
+           aop->size = getSize(sym->type);
+           DEBUGpic14_emitcode(";","%d",__LINE__);
+           return;
 
-         DEBUGpic14_emitcode(";","%d",__LINE__);
-         return;
+         } else {
+
+           unsigned i;
+
+           aop = op->aop = sym->aop = newAsmop(AOP_STR);
+           aop->size = getSize(sym->type);
+           for ( i = 0 ; i < fReturnSizePic ; i++ )
+             aop->aopu.aop_str[i] = fReturn[i];
+
+           DEBUGpic14_emitcode(";","%d",__LINE__);
+           return;
+         }
         }
 
         /* else spill location  */
@@ -1204,10 +1224,10 @@ pCodeOp *popGetLit(unsigned int lit)
 /*-----------------------------------------------------------------*/
 /* popGetImmd - asm operator to pcode immediate conversion         */
 /*-----------------------------------------------------------------*/
-pCodeOp *popGetImmd(char *name, unsigned int offset, int index)
+pCodeOp *popGetImmd(char *name, unsigned int offset, int index,int is_func)
 {
 
-  return newpCodeOpImmd(name, offset,index, 0);
+  return newpCodeOpImmd(name, offset,index, 0, is_func);
 }
 
 
@@ -1311,7 +1331,7 @@ pCodeOp *popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        
     case AOP_IMMD:
       DEBUGpic14_emitcode(";","%d",__LINE__);
-      return popGetImmd(aop->aopu.aop_immd,offset,0);
+      return popGetImmd(aop->aopu.aop_immd,offset,0,0);
 
     case AOP_DIR:
       return popRegFromString(aop->aopu.aop_dir, aop->size, offset);
@@ -1357,7 +1377,7 @@ pCodeOp *popGet (asmop *aop, int offset) //, bool bit16, bool dname)
        PCOR(pcop)->instance = offset;
        pcop->type = PCOR(pcop)->r->pc_type;
        //rs = aop->aopu.aop_reg[offset]->name;
-       //DEBUGpic14_emitcode(";","%d regiser idx = %d name =%s",__LINE__,rIdx,rs);
+       DEBUGpic14_emitcode(";","%d regiser idx = %d ",__LINE__,rIdx);
        return pcop;
       }
 
@@ -1437,7 +1457,7 @@ void aopPut (asmop *aop, char *s, int offset)
              emitpcode(POC_CLRF,popGet(aop,offset));
              break;
            } else
-             emitpcode(POC_MOVLW,popGetImmd(s,offset,0));
+             emitpcode(POC_MOVLW,popGetImmd(s,offset,0,0));
          }
 
          emitpcode(POC_MOVWF,popGet(aop,offset));
@@ -1640,7 +1660,8 @@ void mov2w (asmop *aop, int offset)
   DEBUGpic14_emitcode ("; ***","%s  %d  offset=%d",__FUNCTION__,__LINE__,offset);
 
   if ( aop->type == AOP_PCODE ||
-       aop->type == AOP_LIT )
+       aop->type == AOP_LIT ||
+       aop->type == AOP_IMMD )
     emitpcode(POC_MOVLW,popGet(aop,offset));
   else
     emitpcode(POC_MOVFW,popGet(aop,offset));
@@ -1892,39 +1913,42 @@ static void genNot (iCode *ic)
 /*-----------------------------------------------------------------*/
 static void genCpl (iCode *ic)
 {
-    int offset = 0;
-    int size ;
+  operand *left, *result;
+  int size, offset=0;  
 
 
-    DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-    /* assign asmOps to operand & result */
-    aopOp (IC_LEFT(ic),ic,FALSE);
-    aopOp (IC_RESULT(ic),ic,TRUE);
+  DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
+  aopOp((left = IC_LEFT(ic)),ic,FALSE);
+  aopOp((result=IC_RESULT(ic)),ic,TRUE);
 
-    /* if both are in bit space then 
-    a special case */
-    if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY &&
-        AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { 
+  /* if both are in bit space then 
+     a special case */
+  if (AOP_TYPE(result) == AOP_CRY &&
+      AOP_TYPE(left) == AOP_CRY ) { 
 
-        pic14_emitcode("mov","c,%s",IC_LEFT(ic)->aop->aopu.aop_dir); 
-        pic14_emitcode("cpl","c"); 
-        pic14_emitcode("mov","%s,c",IC_RESULT(ic)->aop->aopu.aop_dir); 
-        goto release; 
-    
+    pic14_emitcode("mov","c,%s",left->aop->aopu.aop_dir); 
+    pic14_emitcode("cpl","c"); 
+    pic14_emitcode("mov","%s,c",result->aop->aopu.aop_dir); 
+    goto release; 
+  } 
 
-    size = AOP_SIZE(IC_RESULT(ic));
-    while (size--) {
-        char *l = aopGet(AOP(IC_LEFT(ic)),offset,FALSE,FALSE);
-        MOVA(l);       
-        pic14_emitcode("cpl","a");
-        aopPut(AOP(IC_RESULT(ic)),"a",offset++);
-    }
+  size = AOP_SIZE(result);
+  while (size--) {
+
+    if(AOP_TYPE(left) == AOP_ACC) 
+      emitpcode(POC_XORLW, popGetLit(0xff));
+    else
+      emitpcode(POC_COMFW,popGet(AOP(left),offset));
+
+    emitpcode(POC_MOVWF,popGet(AOP(result),offset));
+
+  }
 
 
 release:
     /* release the aops */
-    freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
-    freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
+    freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
+    freeAsmop(result,NULL,ic,TRUE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2153,6 +2177,7 @@ static void assignResultValue(operand * oper)
     size--;
     emitpcode(POC_MOVWF, popGet(AOP(oper),size));
     GpsuedoStkPtr++;
+  DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
   }
 
   while (size--) {
@@ -2483,8 +2508,10 @@ static void genCall (iCode *ic)
 static void genPcall (iCode *ic)
 {
     sym_link *dtype;
-    symbol *rlbl = newiTempLabel(NULL);
-
+    symbol *albl = newiTempLabel(NULL);
+    symbol *blbl = newiTempLabel(NULL);
+    PIC_OPCODE poc;
+    operand *left;
 
     DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
     /* if caller saves & we have not saved then */
@@ -2500,54 +2527,48 @@ static void genPcall (iCode *ic)
         (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)))
         saverbank(FUNC_REGBANK(dtype),ic,TRUE);
 
-
-    /* push the return address on to the stack */
-    pic14_emitcode("mov","a,#%05d_DS_",(rlbl->key+100));
-    pic14_emitcode("push","acc");    
-    pic14_emitcode("mov","a,#(%05d_DS_ >> 8)",(rlbl->key+100));
-    pic14_emitcode("push","acc");
-    
-    if (options.model == MODEL_FLAT24)
-    {
-       pic14_emitcode("mov","a,#(%05d_DS_ >> 16)",(rlbl->key+100));
-       pic14_emitcode("push","acc");    
-    }
-
-    /* now push the calling address */
-    aopOp(IC_LEFT(ic),ic,FALSE);
+    left = IC_LEFT(ic);
+    aopOp(left,ic,FALSE);
+    DEBUGpic14_AopType(__LINE__,left,NULL,NULL);
 
     pushSide(IC_LEFT(ic), FPTRSIZE);
 
-    freeAsmop(IC_LEFT(ic),NULL,ic,TRUE); 
-
-    /* if send set is not empty the assign */
+    /* if send set is not empty, assign parameters */
     if (_G.sendSet) {
-       iCode *sic ;
 
-       for (sic = setFirstItem(_G.sendSet) ; sic ; 
-            sic = setNextItem(_G.sendSet)) {
-           int size, offset = 0;
-           aopOp(IC_LEFT(sic),sic,FALSE);
-           size = AOP_SIZE(IC_LEFT(sic));
-           while (size--) {
-               char *l = aopGet(AOP(IC_LEFT(sic)),offset,
-                               FALSE,FALSE);
-               if (strcmp(l,fReturn[offset]))
-                   pic14_emitcode("mov","%s,%s",
-                            fReturn[offset],
-                            l);
-               offset++;
-           }
-           freeAsmop (IC_LEFT(sic),NULL,sic,TRUE);
-       }
-       _G.sendSet = NULL;
+       DEBUGpic14_emitcode ("; ***","%s  %d - WARNING arg-passing to indirect call not supported",__FUNCTION__,__LINE__);
+       /* no way to pass args - W always gets used to make the call */
+    }
+/* first idea - factor out a common helper function and call it.
+   But don't know how to get it generated only once in its own block
+
+    if(AOP_TYPE(IC_LEFT(ic)) == AOP_DIR) {
+           char *rname;
+           char *buffer;
+           rname = IC_LEFT(ic)->aop->aopu.aop_dir;
+           DEBUGpic14_emitcode ("; ***","%s  %d AOP_DIR %s",__FUNCTION__,__LINE__,rname);
+           buffer = Safe_calloc(1,strlen(rname)+16);
+           sprintf(buffer, "%s_goto_helper", rname);
+           addpCode2pBlock(pb,newpCode(POC_CALL,newpCodeOp(buffer,PO_STR)));
+           free(buffer);
     }
+*/
+    emitpcode(POC_CALL,popGetLabel(albl->key));
+    emitpcode(POC_GOTO,popGetLabel(blbl->key));
+    emitpLabel(albl->key);
 
-    pic14_emitcode("ret","");
-    pic14_emitcode("","%05d_DS_:",(rlbl->key+100));
+    poc = ( (AOP_TYPE(left) == AOP_PCODE) ? POC_MOVLW : POC_MOVFW);
+    
+    emitpcode(poc,popGet(AOP(left),1));
+    emitpcode(POC_MOVWF,popCopyReg(&pc_pclath));
+    emitpcode(poc,popGet(AOP(left),0));
+    emitpcode(POC_MOVWF,popCopyReg(&pc_pcl));
 
+    emitpLabel(blbl->key);
 
-    /* if we need assign a result value */
+    freeAsmop(IC_LEFT(ic),NULL,ic,TRUE); 
+
+    /* if we need to assign a result value */
     if ((IS_ITEMP(IC_RESULT(ic)) &&
          (OP_SYMBOL(IC_RESULT(ic))->nRegs ||
           OP_SYMBOL(IC_RESULT(ic))->spildir)) ||
@@ -2562,20 +2583,6 @@ static void genPcall (iCode *ic)
         freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
     }
 
-    /* adjust the stack for parameters if 
-    required */
-    if (ic->parmBytes) {
-        int i;
-        if (ic->parmBytes > 3) {
-            pic14_emitcode("mov","a,%s",spname);
-            pic14_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff);
-            pic14_emitcode("mov","%s,a",spname);
-        } else 
-            for ( i = 0 ; i <  ic->parmBytes ;i++)
-                pic14_emitcode("dec","%s",spname);
-
-    }
-
     /* if register bank was saved then unsave them */
     if (currFunc && dtype && 
         (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)))
@@ -3013,8 +3020,10 @@ static void genRet (iCode *ic)
       l = aopGet(AOP(IC_LEFT(ic)),offset,
                 FALSE,FALSE);
       if (strcmp(fReturn[offset],l)) {
-       if( ( (AOP(IC_LEFT(ic))->type) == AOP_IMMD) ||
-           ((AOP(IC_LEFT(ic))->type) == AOP_LIT) ) {
+      if ((((AOP(IC_LEFT(ic))->type) == AOP_PCODE) && 
+          AOP(IC_LEFT(ic))->aopu.pcop->type == PO_IMMEDIATE) ||
+         ( (AOP(IC_LEFT(ic))->type) == AOP_IMMD) ||
+         ( (AOP(IC_LEFT(ic))->type) == AOP_LIT) ) {
          emitpcode(POC_MOVLW, popGet(AOP(IC_LEFT(ic)),offset));
        }else {
          emitpcode(POC_MOVFW, popGet(AOP(IC_LEFT(ic)),offset));
@@ -5469,21 +5478,18 @@ static void genAnd (iCode *ic, iCode *ifx)
            emitpcode(POC_CLRF,popGet(AOP(result),offset));
            break;
          case 0xff:
-           if(AOP_TYPE(left) == AOP_ACC) {
-             emitpcode(POC_MOVWF,popGet(AOP(result),offset));
-           } else {
+           if(AOP_TYPE(left) != AOP_ACC) {
              pic14_emitcode("movf","%s,w",
                             aopGet(AOP(left),offset,FALSE,FALSE));
              pic14_emitcode("movwf","%s",
                             aopGet(AOP(result),offset,FALSE,FALSE));
              emitpcode(POC_MOVFW,popGet(AOP(left),offset));
-             emitpcode(POC_MOVWF,popGet(AOP(result),offset));
            }
+           emitpcode(POC_MOVWF,popGet(AOP(result),offset));
            break;
          default:
            if(AOP_TYPE(left) == AOP_ACC) {
              emitpcode(POC_ANDLW, popGetLit(t));
-             emitpcode(POC_MOVWF,popGet(AOP(result),offset));
            } else {
              pic14_emitcode("movlw","0x%x",t);
              pic14_emitcode("andwf","%s,w",
@@ -5493,8 +5499,8 @@ static void genAnd (iCode *ic, iCode *ifx)
              
              emitpcode(POC_MOVLW, popGetLit(t));
              emitpcode(POC_ANDFW,popGet(AOP(left),offset));
-             emitpcode(POC_MOVWF,popGet(AOP(result),offset));
            }
+           emitpcode(POC_MOVWF,popGet(AOP(result),offset));
          }
          continue;
        }
@@ -5779,25 +5785,20 @@ static void genOr (iCode *ic, iCode *ifx)
            int t = (lit >> (offset*8)) & 0x0FFL;
            switch(t) { 
            case 0x00:
-             emitpcode(POC_MOVFW,  popGet(AOP(left),offset));
+             if (AOP_TYPE(left) != AOP_ACC) {
+               emitpcode(POC_MOVFW,  popGet(AOP(left),offset));
+             }
              emitpcode(POC_MOVWF,  popGet(AOP(result),offset));
 
-             pic14_emitcode("movf","%s,w",
-                      aopGet(AOP(left),offset,FALSE,FALSE));
-             pic14_emitcode("movwf","%s",
-                      aopGet(AOP(result),offset,FALSE,FALSE));
              break;
            default:
-             emitpcode(POC_MOVLW,  popGetLit(t));
-             emitpcode(POC_IORFW,  popGet(AOP(left),offset));
-             emitpcode(POC_MOVWF,  popGet(AOP(result),offset));
-
-             pic14_emitcode("movlw","0x%x",t);
-             pic14_emitcode("iorwf","%s,w",
-                      aopGet(AOP(left),offset,FALSE,FALSE));
-             pic14_emitcode("movwf","%s",
-                      aopGet(AOP(result),offset,FALSE,FALSE));
-             
+             if (AOP_TYPE(left) == AOP_ACC) {
+               emitpcode(POC_IORLW,  popGetLit(t));
+             } else {
+               emitpcode(POC_MOVLW,  popGetLit(t));
+               emitpcode(POC_IORFW,  popGet(AOP(left),offset));
+             }
+             emitpcode(POC_MOVWF,  popGet(AOP(result),offset));              
            }
            continue;
          }
@@ -6014,7 +6015,9 @@ static void genXor (iCode *ic, iCode *ifx)
        int t = (lit >> (offset*8)) & 0x0FFL;
        switch(t) { 
        case 0x00:
-         emitpcode(POC_MOVFW,popGet(AOP(left),offset));
+         if (AOP_TYPE(left) != AOP_ACC) {
+           emitpcode(POC_MOVFW,popGet(AOP(left),offset));
+         }
          emitpcode(POC_MOVWF,popGet(AOP(result),offset));
          pic14_emitcode("movf","%s,w",
                         aopGet(AOP(left),offset,FALSE,FALSE));
@@ -6022,16 +6025,20 @@ static void genXor (iCode *ic, iCode *ifx)
                         aopGet(AOP(result),offset,FALSE,FALSE));
          break;
        case 0xff:
-         emitpcode(POC_COMFW,popGet(AOP(left),offset));
+         if (AOP_TYPE(left) == AOP_ACC) {
+           emitpcode(POC_XORLW, popGetLit(t));
+         } else {
+           emitpcode(POC_COMFW,popGet(AOP(left),offset));
+         }
          emitpcode(POC_MOVWF,popGet(AOP(result),offset));
-         pic14_emitcode("comf","%s,w",
-                        aopGet(AOP(left),offset,FALSE,FALSE));
-         pic14_emitcode("movwf","%s",
-                        aopGet(AOP(result),offset,FALSE,FALSE));
          break;
        default:
-         emitpcode(POC_MOVLW, popGetLit(t));
-         emitpcode(POC_XORFW,popGet(AOP(left),offset));
+         if (AOP_TYPE(left) == AOP_ACC) {
+           emitpcode(POC_XORLW, popGetLit(t));
+         } else {
+           emitpcode(POC_MOVLW, popGetLit(t));
+           emitpcode(POC_XORFW,popGet(AOP(left),offset));
+         }
          emitpcode(POC_MOVWF,popGet(AOP(result),offset));
          pic14_emitcode("movlw","0x%x",t);
          pic14_emitcode("xorwf","%s,w",
@@ -9337,8 +9344,7 @@ static void genAssign (iCode *ic)
        emitpcode(POC_INCF, popGet(AOP(result),0));
       }
     } else {
-  DEBUGpic14_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
-      emitpcode(POC_MOVFW, popGet(AOP(right),offset));
+      mov2w (AOP(right), offset);
       emitpcode(POC_MOVWF, popGet(AOP(result),offset));
     }