* device/lib/_gptrget.c,
[fw/sdcc] / src / pic16 / glue.c
index d92db723a0c14a7db1372b63935921910e61b88a..f78f83a3cd12b805923474c1b2cb2681a87e57f1 100644 (file)
@@ -132,22 +132,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 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)) &&
@@ -291,6 +298,8 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                                          sectSym *ssym;
                                          int found=0;
                                  
+                                               fprintf(stderr, "%s:%d sym->rname: %s reg: %p reg->name: %s\n", __FILE__, __LINE__,
+                                                       sym->rname, reg, (reg?reg->name:"<<NULL>>"));
 #if 1
                                                for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
                                                        if(!strcmp(ssym->name, reg->name))found=1;
@@ -564,6 +573,8 @@ void pic16_printGPointerType (const char *iname, const char *oname, const unsign
 }
 
 
+/* set to 0 to disable debug messages */
+#define DEBUG_PRINTIVAL        0
 
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalType - generates ival for int/char               */
@@ -576,6 +587,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;
@@ -624,13 +640,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)
     {
 
@@ -676,6 +694,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                      */
@@ -742,6 +763,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);
@@ -798,6 +824,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) {
@@ -837,7 +868,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);
 
@@ -911,6 +945,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
@@ -942,6 +981,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;
@@ -1138,6 +1191,7 @@ static void
 pic16emitStaticSeg (memmap * map)
 {
   symbol *sym;
+  static int didcode=0;
 
   //fprintf(stderr, "%s\n",__FUNCTION__);
 
@@ -1253,6 +1307,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;
@@ -1265,6 +1320,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));
@@ -1371,10 +1433,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);
 }
 
 /*-----------------------------------------------------------------*/
@@ -1385,7 +1448,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);
 
@@ -1401,7 +1464,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);
        
@@ -1560,9 +1627,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 */
@@ -1696,14 +1763,14 @@ 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);