* src/pic16/glue.c (printIvalChar): fixed bug when emitting
[fw/sdcc] / src / pic16 / glue.c
index f4087a6f3744b50eb66a496cf3f206fda7be4bf3..0b6b4fd6d0a4b1fe55f0ab2128a2ae8e84998285 100644 (file)
@@ -69,6 +69,8 @@ extern DEFSETFUNC (closeTmpFiles);
 extern DEFSETFUNC (rmTmpFiles);
 
 extern void pic16_AnalyzeBanking (void);
+extern void pic16_OptimizeJumps ();
+extern void pic16_OptimizeBanksel ();
 extern void copyFile (FILE * dest, FILE * src);
 extern void pic16_InlinepCode(void);
 extern void pic16_writeUsedRegs(FILE *);
@@ -80,8 +82,6 @@ void  pic16_pCodeInitRegisters(void);
 pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
 extern void pic16_pCodeConstString(char *name, char *value);
 
-#define debugf(frm, rest)       _debugf(__FILE__, __LINE__, frm, rest)
-extern void _debugf(char *f, int l, char *frm, ...);
 
 /*-----------------------------------------------------------------*/
 /* aopLiteral - string from a literal value                        */
@@ -176,16 +176,14 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                  
                        checkAddSym(&publics, sym);
                } else
+                        /* new version */
                        if(IS_STATIC(sym->etype)
-                               && !(sym->ival && !sym->level)
-                       ) {
+                               && !sym->ival) /* && !sym->level*/ {
                          regs *reg;
-                               /* add it to udata list */
+                          sectSym *ssym;
+                          int found=0;
 
-//                             fprintf(stderr, "%s:%d adding %s (%s) remat=%d\n", __FILE__, __LINE__,
-//                                     sym->name, sym->rname, sym->remat);
-                                       
-                                               //, OP_SYMBOL(operandFromSymbol(sym))->name);
+//                            debugf("adding symbol %s\n", sym->name);
 #define SET_IMPLICIT   1
 
 #if SET_IMPLICIT
@@ -195,22 +193,20 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 
                                reg = pic16_allocDirReg( operandFromSymbol( sym ));
                                
-                               {
-                                 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(reg) {
+                                  for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
+                                   if(!strcmp(ssym->name, reg->name))found=1;
+                                  }
 
-                                       if(!found)
-                                               checkAddReg(&pic16_rel_udata, reg);
-                                       else
-                                               checkAddSym(&publics, sym);
+                                  if(!found)
+                                    checkAddReg(&pic16_rel_udata, reg);
+#if 0
+                                  else
+                                    debugf("Did find %s in pic16_rel_udata already. Check!\n", reg->name);
+//                                 checkAddSym(&publics, sym);
+#endif
 
-                               }
+                                }
                        }
 
                /* if extern then do nothing or is a function
@@ -223,28 +219,9 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                        continue;
                }
 
-#if 0
-               /* print extra debug info if required */
-               if (options.debug || sym->level == 0) {
-                       cdbWriteSymbol (sym);   //, cdbFile, FALSE, FALSE);
-
-                       if (!sym->level)        /* global */
-                               if (IS_STATIC (sym->etype))
-                                       fprintf (map->oFile, "F%s_", moduleName);               /* scope is file */
-                               else
-                                       fprintf (map->oFile, "G_");     /* scope is global */
-                       else
-                               /* symbol is local */
-                               fprintf (map->oFile, "L%s_", (sym->localof ? sym->localof->name : "-null-"));
-                       fprintf (map->oFile, "%s_%d_%d", sym->name, sym->level, sym->block);
-               }
-#endif
-
-
                /* if is has an absolute address then generate
                an equate for this no need to allocate space */
                if (SPEC_ABSA (sym->etype)) {
-//                     if (options.debug || sym->level == 0)
 //                             fprintf (stderr,"; %s == 0x%04x\t\treqv= %p nRegs= %d\n",
 //                                     sym->name, SPEC_ADDR (sym->etype), sym->reqv, sym->regType);
 
@@ -309,17 +286,16 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                                                        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);
-                                                       }
+                                                if(reg) {
+                                                 for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
+                                                   if(!strcmp(ssym->name, reg->name))found=1;
+                                                  }
 
+                                                  if(!found)
+                                                    if(checkAddReg(&pic16_rel_udata, reg)) {
+                                                      addSetHead(&publics, sym);
+                                                    }
+                                                }
                                        }
 
 
@@ -353,7 +329,9 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                /* if it has an initial value then do it only if
                        it is a global variable */
 
-               if (sym->ival && sym->level == 0) {
+               if (sym->ival
+                 && ((sym->level == 0)
+                     || IS_STATIC(sym->etype)) ) {
                  ast *ival = NULL;
 
 #if 0
@@ -566,8 +544,8 @@ 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)
+void pic16_printGPointerType (const char *iname, const unsigned int itype,
+  char ptype, void *p)
 {
   char buf[256];
   
@@ -576,6 +554,7 @@ void pic16_printGPointerType (const char *iname, const char *oname, const unsign
     switch( itype ) {
       case FPOINTER:
       case CPOINTER:
+      case FUNCTION:
         {
           sprintf(buf, "UPPER(%s)", iname);
           pic16_emitDS(buf, ptype, p);
@@ -585,6 +564,9 @@ void pic16_printGPointerType (const char *iname, const char *oname, const unsign
         sprintf(buf, "0x80");
         pic16_emitDS(buf, ptype, p);
         break;
+      default:
+        debugf("itype = %d\n", itype );
+        assert( 0 );
     }
 
     pic16_flushDB(ptype, p);
@@ -652,10 +634,10 @@ pic16_printIvalType (symbol *sym, sym_link * type, initList * ilist, char ptype,
 /* pic16_printIvalChar - generates initital value for character array */
 /*--------------------------------------------------------------------*/
 static int 
-pic16_printIvalChar (sym_link * type, initList * ilist, char *s, char ptype, void *p)
+pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, char ptype, void *p)
 {
   value *val;
-  unsigned int remain;
+  int remain, len;
 
   if(!p)
     return 0;
@@ -671,14 +653,38 @@ pic16_printIvalChar (sym_link * type, initList * ilist, char *s, char ptype, voi
       if(!DCL_ELEM (type))
         DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
 
-      for(remain=0; remain<strlen(SPEC_CVAL(val->etype).v_char)+1; remain++)
-        pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
-      
-      if((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0) {
-        while(remain--) {
+      /* len is 0 if declartion equals initializer,
+       * >0 if declaration greater than initializer
+       * <0 if declaration less than initializer
+       * Strategy: if >0 emit 0x00 for the rest of the length,
+       * if <0 then emit only the length of declaration elements
+       * and warn user
+       */
+      len = DCL_ELEM (type) - (strlen (SPEC_CVAL (val->etype).v_char)+1);
+
+//      fprintf(stderr, "%s:%d len = %i DCL_ELEM(type) = %i SPEC_CVAL-len = %i\n", __FILE__, __LINE__,
+//        len, DCL_ELEM(type), strlen(SPEC_CVAL(val->etype).v_char));
+
+      remain=0;
+      if(len >= 0) {
+        for(remain=0; remain<strlen(SPEC_CVAL(val->etype).v_char)+1; remain++)
+          pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
+
+        len -= remain;
+        while(len--) {
           pic16_emitDB(0x00, ptype, p);
         }
+
+      } else {
+        werror (W_EXCESS_INITIALIZERS, "array of chars", sym->name, sym->lineDef);
+
+        for(remain=0; remain<DCL_ELEM (type); remain++)
+          pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
       }
+      
+
+//      if((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0) {
+//      }
       return 1;
     } else return 0;
   } else {
@@ -715,7 +721,7 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
       return;
     }
 
-    if(pic16_printIvalChar (type,
+    if(pic16_printIvalChar (sym, type,
                       (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
                       SPEC_CVAL (sym->etype).v_char, ptype, p))
       return;
@@ -906,7 +912,7 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
             // this is a literal string
             type=CPOINTER;
           }
-          pic16_printGPointerType(val->name, sym->name, type, type, ptype, p);
+          pic16_printGPointerType(val->name, type, ptype, p);
         }
       else
         {
@@ -987,22 +993,20 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
 
   /* now generate the name */
   if (!val->sym) {
-      pic16_printPointerType (val->name, ptype, p);
+      pic16_printGPointerType (val->name, DCL_TYPE(val->type), ptype, p);
   } else {
-      pic16_printPointerType (val->sym->rname, ptype, p);
+      pic16_printGPointerType (val->sym->rname, DCL_TYPE(val->type), ptype, p);
 
-      if(IS_FUNC(val->sym->type) && !val->sym->used) {
+      if(IS_FUNC(val->sym->type) && !val->sym->used && !IS_STATIC(val->sym->etype)) {
         
         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");
-       }
+         if(checkAddSym(&externs, val->sym) && (ptype == 'f')) {
+           /* this has not been declared as extern
+            * so declare it as a 'late extern' just after the symbol */
+           fprintf((FILE *)p, ";\tdeclare symbol as extern\n");
+           fprintf((FILE *)p, "\textern\t%s\n", val->sym->rname);
+           fprintf((FILE *)p, ";\tcontinue variable declaration\n");
+          }
       }
   }
 
@@ -1085,9 +1089,8 @@ void pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char p
     }
   else if (size == 3)
     {
-      pic16_printGPointerType (val->name, sym->name, (IS_PTR(type)?DCL_TYPE(type):PTR_TYPE(SPEC_OCLS(sym->etype))),
-                         (IS_PTR (val->type) ? DCL_TYPE (val->type) :
-                          PTR_TYPE (SPEC_OCLS (val->etype))), ptype, p);
+      pic16_printGPointerType (val->name, (IS_PTR(type)?DCL_TYPE(type):PTR_TYPE(SPEC_OCLS(sym->etype))),
+                          ptype, p);
     } else
        assert(0);
   return;
@@ -1423,7 +1426,7 @@ pic16createInterruptVect (FILE * vFile)
                return;
        }
 #endif
-
+#if 0
        if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
                fprintf (vFile, ";\t.area\t%s\n", CODE_NAME);
                fprintf(vFile, ".intvecs\tcode\t0x%06x\n", pic16_options.ivt_loc);
@@ -1434,6 +1437,7 @@ pic16createInterruptVect (FILE * vFile)
                        port->genIVT(vFile, interrupts, maxInterrupts);
                }
        }
+#endif
        
 }
 
@@ -1469,6 +1473,8 @@ pic16printPublics (FILE *afile)
        fprintf (afile, "%s", iComments2);
 
        for(sym = setFirstItem (publics); sym; sym = setNextItem (publics))
+         /* sanity check */
+         if(!IS_STATIC(sym->etype))
                fprintf(afile, "\tglobal %s\n", sym->rname);
 }
 
@@ -1554,27 +1560,6 @@ pic16emitOverlay (FILE * afile)
          if (IS_FUNC (sym->type))
            continue;
 
-#if 0
-         /* print extra debug info if required */
-         if (options.debug || sym->level == 0)
-           {
-
-             cdbSymbol (sym, cdbFile, FALSE, FALSE);
-
-             if (!sym->level)
-               {               /* global */
-                 if (IS_STATIC (sym->etype))
-                   fprintf (afile, "F%s_", moduleName);        /* scope is file */
-                 else
-                   fprintf (afile, "G_");      /* scope is global */
-               }
-             else
-               /* symbol is local */
-               fprintf (afile, "L%s_",
-                        (sym->localof ? sym->localof->name : "-null-"));
-             fprintf (afile, "%s_%d_%d", sym->name, sym->level, sym->block);
-           }
-#endif
 
          /* if is has an absolute address then generate
             an equate for this no need to allocate space */
@@ -1604,15 +1589,18 @@ pic16emitOverlay (FILE * afile)
 
 void emitStatistics(FILE *asmFile)
 {
+  unsigned long isize, udsize;
   statistics.isize = pic16_countInstructions();
+  isize = (statistics.isize >= 0) ? statistics.isize : 0;
+  udsize = (statistics.udsize >= 0) ? statistics.udsize : 0;
        
   fprintf (asmFile, "\n\n; Statistics:\n");
-  fprintf (asmFile, "; code size:\t%ld (0x%lx) bytes\n;\t\t%ld (0x%lx) words\n",
-    statistics.isize, statistics.isize,
-    statistics.isize>>1, statistics.isize>>1);
-  fprintf (asmFile, "; udata size:\t%ld (0x%lx) bytes\n", 
-    statistics.udsize, statistics.udsize);
-  fprintf (asmFile, "; access size:\t%ld (0x%lx) bytes\n",
+  fprintf (asmFile, "; code size:\t%5ld (0x%04lx) bytes (%3.2f%%)\n;           \t%5ld (0x%04lx) words\n",
+    isize, isize, (isize*100.0)/(128 << 10),
+    isize>>1, isize>>1);
+  fprintf (asmFile, "; udata size:\t%5ld (0x%04lx) bytes (%3.2f%%)\n",
+    udsize, udsize, (udsize*100.0) / ((pic16 ? pic16->RAMsize : 0x200) -256));
+  fprintf (asmFile, "; access size:\t%5ld (0x%04lx) bytes\n",
     statistics.intsize, statistics.intsize);
 
   fprintf (asmFile, "\n\n");
@@ -1740,9 +1728,21 @@ pic16glue ()
     /* Put all variables into a cblock */
     pic16_AnalyzeBanking();
 
+#if 0
     if(pic16_options.opt_flags & OF_LR_SUPPORT) {
       pic16_OptimizeLocalRegs();
     }
+#endif
+
+    /* remove redundant BANKSELs -- added by RN 2005-01-17 */
+    if(pic16_options.opt_banksel > 1) {
+      pic16_OptimizeBanksel();
+    }
+           
+    /* turn GOTOs into BRAs -- added by RN 2004-11-16 */
+    if(pic16_options.opt_flags & OF_OPTIMIZE_GOTO) {
+      pic16_OptimizeJumps();
+    }
 
     /* print the extern variables to this module */
     pic16_printExterns(asmFile);