* .version: bumped version number to 2.4.5
[fw/sdcc] / src / pic16 / glue.c
index b0c21d45cbcf13c3844d10c7014c6ac78aa1925f..5e2ddab73c551e194966c434efab8b3d06a8a22d 100644 (file)
@@ -76,8 +76,6 @@ extern void initialComments (FILE * afile);
 extern void printPublics (FILE * afile);
 
 void  pic16_pCodeInitRegisters(void);
-pCodeOp *pic16_popGetLit(unsigned int lit);
-pCodeOp *pic16_popGetLit2(unsigned int lit, pCodeOp *arg2);
 pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
 extern void pic16_pCodeConstString(char *name, char *value);
 
@@ -132,26 +130,29 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
        for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) {
 
 #if 0
-               fprintf(stderr, "%s\t%s: sym: %s\tused: %d\textern: %d\tstatic: %d\taggregate: %d\n",
+               fprintf(stderr, "%s\t%s: sym: %s\tused: %d\textern: %d\tstatic: %d\taggregate: %d\tregister: 0x%x\tfunction: %d\n",
                        __FUNCTION__,
                        map->sname, sym->name, sym->used, IS_EXTERN(sym->etype), IS_STATIC(sym->etype),
-                       IS_AGGREGATE(sym->type));
+                       IS_AGGREGATE(sym->type), (SPEC_SCLS(sym->etype) == S_REGISTER), IS_FUNC(sym->type));
                printTypeChain( sym->type, stderr );
                fprintf(stderr, "\n");
 #endif
 
-//             if(PIC16_IS_CONFIG_ADDRESS(SPEC_ADDR(sym->etype)))
-//                     continue;
-
-
                /* if extern then add to externs */
                if (IS_EXTERN (sym->etype)) {
-                       checkAddSym(&externs, sym);
+                       /* reduce overhead while linking by not declaring
+                        * extern unused external functions (usually declared
+                        * in header files) */
+                       if(IS_FUNC(sym->type) && !sym->used)continue;
+                       
+                       /* make sure symbol is not in publics section */
+                       if(!checkSym(publics, sym))
+                               checkAddSym(&externs, sym);
                        continue;
                }
                
                /* if allocation required check is needed
-                *  then check if the symbol really requires
+                * then check if the symbol really requires
                 * allocation only for local variables */
                 if (arFlag && !IS_AGGREGATE (sym->type) &&
                        !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
@@ -189,16 +190,17 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 #endif
 
                                reg = pic16_allocDirReg( operandFromSymbol( sym ));
-//                             addSet(&pic16_dynAllocRegs, reg);
                                
                                {
                                  sectSym *ssym;
                                  int found=0;
                                  
+#if 1
                                        for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
                                                if(!strcmp(ssym->name, reg->name))found=1;
                                        }
-                                       
+#endif
+
                                        if(!found)checkAddReg(&pic16_rel_udata, reg);
                                }
                        }
@@ -294,10 +296,17 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                                          sectSym *ssym;
                                          int found=0;
                                  
+#if 0
+                                               fprintf(stderr, "%s:%d sym->rname: %s reg: %p reg->name: %s\n", __FILE__, __LINE__,
+                                                       sym->rname, reg, (reg?reg->name:"<<NULL>>"));
+#endif
+
+#if 1
                                                for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
                                                        if(!strcmp(ssym->name, reg->name))found=1;
                                                }
-                                       
+#endif
+
                                                if(!found)
                                                        if(checkAddReg(&pic16_rel_udata, reg)) {
                                                                addSetHead(&publics, sym);
@@ -367,7 +376,7 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                                        addSet(&rel_idataSymSet, copySymbol(sym));
 
 //                                     ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
-//                                             decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
+//                                             decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
                        }
 
                        if(ival) {
@@ -550,21 +559,32 @@ void pic16_printPointerType (const char *name, char ptype, void *p)
 /* printGPointerType - generates ival for generic pointer type     */
 /*-----------------------------------------------------------------*/
 void pic16_printGPointerType (const char *iname, const char *oname, const unsigned int itype,
-                   const unsigned int type, char ptype, void *p)
+  const unsigned int type, char ptype, void *p)
 {
-  _pic16_printPointerType (iname, ptype, p);
-
-  if(itype == FPOINTER || itype == CPOINTER) { // || itype == GPOINTER) {
-    char buf[256];
+  char buf[256];
+  
+    _pic16_printPointerType (iname, ptype, p);
 
-       sprintf(buf, "UPPER(%s)", iname);
-       pic16_emitDS(buf, ptype, p);
-  }
+    switch( itype ) {
+      case FPOINTER:
+      case CPOINTER:
+        {
+          sprintf(buf, "UPPER(%s)", iname);
+          pic16_emitDS(buf, ptype, p);
+        }; break;
+      case POINTER:
+      case IPOINTER:
+        sprintf(buf, "0x80");
+        pic16_emitDS(buf, ptype, p);
+        break;
+    }
 
-  pic16_flushDB(ptype, p);
+    pic16_flushDB(ptype, p);
 }
 
 
+/* set to 0 to disable debug messages */
+#define DEBUG_PRINTIVAL        0
 
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalType - generates ival for int/char               */
@@ -577,6 +597,11 @@ pic16_printIvalType (symbol *sym, sym_link * type, initList * ilist, char ptype,
 
 //  fprintf(stderr, "%s for symbol %s\n",__FUNCTION__, sym->rname);
 
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
+
   /* if initList is deep */
   if (ilist && ilist->type == INIT_DEEP)
     ilist = ilist->init.deep;
@@ -625,13 +650,15 @@ static int
 pic16_printIvalChar (sym_link * type, initList * ilist, char *s, char ptype, void *p)
 {
   value *val;
-  int remain;
+  unsigned int remain;
 
   if(!p)
     return 0;
 
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
 
-  // fprintf(stderr, "%s\n",__FUNCTION__);
   if (!s)
     {
 
@@ -677,6 +704,9 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
     return;
 
 
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
   /* take care of the special   case  */
   /* array of characters can be init  */
   /* by a string                      */
@@ -743,6 +773,11 @@ void pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *
   int size =0;
 
 
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
+
   do {
     unsigned long i;
     val = list2val(lilist);
@@ -799,6 +834,11 @@ void pic16_printIvalStruct (symbol * sym, sym_link * type,
   symbol *sflds;
   initList *iloop = NULL;
 
+
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
   sflds = SPEC_STRUCT (type)->fields;
 
   if (ilist) {
@@ -838,7 +878,10 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
      VR - Attempting to port this function to pic16 port - 8-Jun-2004
    */
 
-//     fprintf(stderr, "%s\n",__FUNCTION__);
+
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
 
   size = getSize (type);
 
@@ -912,6 +955,11 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
   value *val;
   int dLvl = 0;
 
+
+#if DEBUG_PRINTIVAL
+  fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
   if (ilist)
     val = list2val (ilist);
   else
@@ -943,6 +991,20 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
       pic16_printPointerType (val->name, ptype, p);
   } else {
       pic16_printPointerType (val->sym->rname, ptype, p);
+
+      if(IS_FUNC(val->sym->type) && !val->sym->used) {
+        
+        if(!checkSym(publics, val->sym))
+         checkAddSym(&externs, val->sym);
+        
+       /* this has not been declared as extern
+        * so declare it as a 'late extern' just after the symbol */
+       if(ptype == 'f') {
+               fprintf((FILE *)p, "declare symbol as extern");
+               fprintf((FILE *)p, "\textern\t%s\n", val->sym->rname);
+               fprintf((FILE *)p, "continue variable declaration");
+       }
+      }
   }
 
   return;
@@ -1003,7 +1065,9 @@ void pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char p
             pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
             pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
             pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
+            break;
         default:
+               fprintf(stderr, "%s:%d size = %d\n", __FILE__, __LINE__, getSize(type));
                assert(0);
         }
       return;
@@ -1117,10 +1181,21 @@ void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptyp
 
 int PIC16_IS_CONFIG_ADDRESS(int address)
 {
+  return ((address >= pic16->cwInfo.confAddrStart && address <= pic16->cwInfo.confAddrEnd));
+}
 
-  return (address >= pic16->cwInfo.confAddrStart && address <= pic16->cwInfo.confAddrEnd);
+int PIC16_IS_IDLOC_ADDRESS(int address)
+{
+   return ((address >= pic16->idInfo.idAddrStart && address <= pic16->idInfo.idAddrEnd));
+}
+
+/* wrapper function for the above */
+int PIC16_IS_HWREG_ADDRESS(int address)
+{
+  return (PIC16_IS_CONFIG_ADDRESS(address) || PIC16_IS_IDLOC_ADDRESS(address));
 }
 
+
 /*-----------------------------------------------------------------*/
 /* emitStaticSeg - emitcode for the static segment                 */
 /*-----------------------------------------------------------------*/
@@ -1128,6 +1203,7 @@ static void
 pic16emitStaticSeg (memmap * map)
 {
   symbol *sym;
+  static int didcode=0;
 
   //fprintf(stderr, "%s\n",__FUNCTION__);
 
@@ -1155,6 +1231,13 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__,
                continue;
         }
 
+        if(SPEC_ABSA(sym->etype) && PIC16_IS_IDLOC_ADDRESS(SPEC_ADDR(sym->etype))) {
+               pic16_assignIdByteValue(SPEC_ADDR(sym->etype),
+                       (char) floatFromVal(list2val(sym->ival)));
+
+               continue;
+        }
+
        /* if it is "extern" then do nothing */
        if (IS_EXTERN (sym->etype) && !SPEC_ABSA(sym->etype)) {
                checkAddSym(&externs, sym);
@@ -1236,6 +1319,7 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__,
 //             fprintf(stderr, "%s:%d spec_absa is false for symbol: %s\n",
 //                     __FILE__, __LINE__, sym->name);
 
+               
          /* if it has an initial value */
          if (sym->ival) {
              pBlock *pb;
@@ -1248,6 +1332,13 @@ CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__,
              pb = pic16_newpCodeChain(NULL, 'P',pic16_newpCodeCharP("; Starting pCode block for Ival"));
              pic16_addpBlock(pb);
 
+             if(!didcode) {
+               /* make sure that 'code' directive is emitted before, once */
+               pic16_addpCode2pBlock(pb, pic16_newpCodeAsmDir("code", NULL));
+               
+               didcode++;
+             }
+                            
 //           fprintf(stderr, "%s:%d [2] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
 
              pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(sym->rname,-1));
@@ -1291,6 +1382,17 @@ void pic16_emitConfigRegs(FILE *of)
                                pic16->cwInfo.crInfo[i].value);
 }
 
+void pic16_emitIDRegs(FILE *of)
+{
+  int i;
+
+       for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart;i++)
+               if(pic16->idInfo.irInfo[i].emit)
+                       fprintf (of, "\t__idlocs 0x%06x, 0x%hhx\n",
+                               pic16->idInfo.idAddrStart+i,
+                               pic16->idInfo.irInfo[i].value);
+}
+
 
 static void
 pic16emitMaps ()
@@ -1343,10 +1445,11 @@ pic16createInterruptVect (FILE * vFile)
 static void
 pic16initialComments (FILE * afile)
 {
-  initialComments (afile);
-  fprintf (afile, "; PIC16 port for the Microchip 16-bit core micros\n");
-  fprintf (afile, iComments2);
-
+       initialComments (afile);
+       fprintf (afile, "; PIC16 port for the Microchip 16-bit core micros\n");
+       if(pic16_mplab_comp)
+               fprintf(afile, "; MPLAB/MPASM/MPASMWIN/MPLINK compatibility mode enabled\n");
+       fprintf (afile, iComments2);
 }
 
 /*-----------------------------------------------------------------*/
@@ -1357,7 +1460,7 @@ pic16printPublics (FILE *afile)
 {
   symbol *sym;
 
-       fprintf (afile, "%s", iComments2);
+       fprintf (afile, "\n%s", iComments2);
        fprintf (afile, "; public variables in this module\n");
        fprintf (afile, "%s", iComments2);
 
@@ -1373,7 +1476,11 @@ pic16_printExterns(FILE *afile)
 {
   symbol *sym;
 
-       fprintf(afile, "%s", iComments2);
+       /* print nothing if no externs to declare */
+       if(!elementsInSet(externs) && !elementsInSet(pic16_builtin_functions))
+               return;
+
+       fprintf(afile, "\n%s", iComments2);
        fprintf(afile, "; extern variables in this module\n");
        fprintf(afile, "%s", iComments2);
        
@@ -1433,6 +1540,7 @@ pic16emitOverlay (FILE * afile)
             and addPublics allowed then add it to the public set */
          if ((sym->_isparm && !IS_REGPARM (sym->etype))
              && !IS_STATIC (sym->etype)) {
+//           fprintf(stderr, "%s:%d %s accessed\n", __FILE__, __LINE__, __FUNCTION__);
              checkAddSym(&publics, sym);
 //         addSetHead (&publics, sym);
          }
@@ -1521,7 +1629,8 @@ pic16glue ()
        addSetHead(&tmpfileSet,ovrFile);
        pic16_pCodeInitRegisters();
 
-       if (mainf && IFFUNC_HASBODY(mainf->type)) {
+
+       if (pic16_options.no_crt && mainf && IFFUNC_HASBODY(mainf->type)) {
          pBlock *pb = pic16_newpCodeChain(NULL,'X',pic16_newpCodeCharP("; Starting pCode block"));
 
                pic16_addpBlock(pb);
@@ -1531,9 +1640,9 @@ pic16glue ()
 
                if(initsfpnt) {
                        pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
-                               pic16_popGetLit2(1, pic16_newpCodeOpRegFromStr("_stack"))));
+                               pic16_popGetLit2(1, pic16_newpCodeOpRegFromStr("_stack_end"))));
                        pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
-                               pic16_popGetLit2(2, pic16_newpCodeOpRegFromStr("_stack"))));
+                               pic16_popGetLit2(2, pic16_newpCodeOpRegFromStr("_stack_end"))));
                }
 
                /* put in the call to main */
@@ -1576,8 +1685,8 @@ pic16glue ()
        pic16emitOverlay(ovrFile);
        pic16_AnalyzepCode('*');
 
-#if 0
-       {
+#if 1
+       if(pic16_options.dumpcalltree) {
          FILE *cFile;
                sprintf(buffer, dstFileName);
                strcat(buffer, ".calltree");
@@ -1609,18 +1718,18 @@ pic16glue ()
     
        /* initial comments */
        pic16initialComments (asmFile);
-    
+
        /* print module name */
        fprintf(asmFile, "#FILE\t\"%s\"\n", fullSrcFileName);
-    
+
        /* Let the port generate any global directives, etc. */
        if (port->genAssemblerPreamble) {
                port->genAssemblerPreamble(asmFile);
        }
-    
+       
        /* print the extern variables to this module */
        pic16_printExterns(asmFile);
-  
+       
        /* print the global variables in this module */
        pic16printPublics (asmFile);
 
@@ -1631,42 +1740,11 @@ pic16glue ()
        fprintf (asmFile, "%s", iComments2);
        copyFile (asmFile, sfr->oFile);
 #endif
-    
 
        /* Put all variables into a cblock */
        pic16_AnalyzeBanking();
        pic16_writeUsedRegs(asmFile);
 
-#if 0
-       /* create the overlay segments */
-       fprintf (asmFile, "%s", iComments2);
-       fprintf (asmFile, "; overlayable items in internal ram \n");
-       fprintf (asmFile, "%s", iComments2);    
-       copyFile (asmFile, ovrFile);
-#endif
-
-#if 0
-
-       /* create the stack segment MOF */
-       if (mainf && IFFUNC_HASBODY(mainf->type)) {
-               fprintf (asmFile, "%s", iComments2);
-               fprintf (asmFile, "; Stack segment in internal ram \n");
-               fprintf (asmFile, "%s", iComments2);    
-               fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
-                       ";__start__stack:\n;\t.ds\t1\n\n");
-       }
-#endif
-
-#if 0
-       /* no indirect data in pic */
-       /* create the idata segment */
-       fprintf (asmFile, "%s", iComments2);
-       fprintf (asmFile, "; indirectly addressable internal ram data\n");
-       fprintf (asmFile, "%s", iComments2);
-       copyFile (asmFile, idata->oFile);
-#endif
-
-
 #if 0
        /* no xdata in pic */
        /* if external stack then reserve space of it */
@@ -1698,46 +1776,27 @@ pic16glue ()
 
        /* copy the interrupt vector table */
        if(mainf && IFFUNC_HASBODY(mainf->type)) {
-               fprintf (asmFile, "%s", iComments2);
+               fprintf (asmFile, "\n%s", iComments2);
                fprintf (asmFile, "; interrupt vector \n");
                fprintf (asmFile, "%s", iComments2);
                copyFile (asmFile, vFile);
        }
     
        /* copy global & static initialisations */
-       fprintf (asmFile, "%s", iComments2);
+       fprintf (asmFile, "\n%s", iComments2);
        fprintf (asmFile, "; global & static initialisations\n");
        fprintf (asmFile, "%s", iComments2);
     
-#if 0
-       /* copy over code */
-       fprintf (asmFile, "%s", iComments2);
-       fprintf (asmFile, "\tcode\n");
-       fprintf (asmFile, "%s", iComments2);
-#endif
-
        if(pic16_debug_verbose)
                fprintf(asmFile, "; A code from now on!\n");
        pic16_copypCode(asmFile, 'A');
 
 
-       if(mainf && IFFUNC_HASBODY(mainf->type)) {
-               fprintf(asmFile, "\tcode\n");
-               fprintf(asmFile,"__sdcc_gsinit_startup:\n");
-
-#if 0
-               /* FIXME 8051 legacy (?!) - VR 20-Jun-2003 */
-               /* if external stack is specified then the
-                * higher order byte of the xdatalocation is
-                * going into P2 and the lower order going into */
-       
-               if (options.useXstack) {
-                       fprintf(asmFile,";\tmov\tP2,#0x%02x\n",
-                                       (((unsigned int)options.xdata_loc) >> 8) & 0xff);
-                       fprintf(asmFile,";\tmov\t_spx,#0x%02x\n",
-                                       (unsigned int)options.xdata_loc & 0xff);
+       if(pic16_options.no_crt) {
+               if(mainf && IFFUNC_HASBODY(mainf->type)) {
+                       fprintf(asmFile, "\tcode\n");
+                       fprintf(asmFile,"__sdcc_gsinit_startup:\n");
                }
-#endif
        }
 
 //     copyFile (stderr, code->oFile);
@@ -1750,8 +1809,10 @@ pic16glue ()
        pic16_copypCode(asmFile, statsg->dbName);
 
 
-       if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type)) {
-               fprintf (asmFile,"\tgoto\t__sdcc_program_startup\n");
+       if(pic16_options.no_crt) {
+               if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type)) {
+                       fprintf (asmFile,"\tgoto\t__sdcc_program_startup\n");
+               }
        }