Implemented generic part of RFE #1880202
[fw/sdcc] / src / SDCCglue.c
index c05896df5a0e7b88cbbe22737f0be19fc33b4709..7aed6a580456f63c456c665ceb9af7c44a785d5a 100644 (file)
@@ -44,9 +44,8 @@ set *externs = NULL;            /* Variables that are declared as extern */
 unsigned maxInterrupts = 0;
 int allocInfo = 1;
 symbol *mainf;
-set *pipeSet = NULL;            /* set of pipes */
-set *tmpfileSet = NULL;         /* set of tmp file created by the compiler */
-set *tmpfileNameSet = NULL;     /* All are unlinked at close. */
+int noInit = 0;                 /* no initialization */
+
 
 /*-----------------------------------------------------------------*/
 /* closePipes - closes all pipes created by the compiler           */
@@ -110,7 +109,7 @@ aopLiteralLong (value * val, int offset, int size)
 
   if (!val) {
     // assuming we have been warned before
-    val=constVal("0");
+    val = constCharVal (0);
   }
 
   /* if it is a float then it gets tricky */
@@ -184,10 +183,9 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
         dbuf_tprintf (&map->oBuf, "\t!area\n", map->sname);
     }
 
-  for (sym = setFirstItem (map->syms); sym;
-       sym = setNextItem (map->syms))
+  for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms))
     {
-      symbol *newSym=NULL;
+      symbol *newSym = NULL;
 
       /* if extern then add it into the extern list */
       if (IS_EXTERN (sym->etype))
@@ -207,9 +205,10 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 
       /* for bitvar locals and parameters */
       if (!arFlag && !sym->allocreq && sym->level
-          && !SPEC_ABSA (sym->etype)) {
-        continue;
-      }
+          && !SPEC_ABSA (sym->etype))
+        {
+          continue;
+        }
 
       /* if global variable & not static or extern
          and addPublics allowed then add it to the public set */
@@ -217,7 +216,7 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
            (sym->_isparm && !IS_REGPARM (sym->etype))) &&
           addPublics &&
           !IS_STATIC (sym->etype) &&
-          (IS_FUNC(sym->type) ? (sym->used || IFFUNC_HASBODY(sym->type)) : 1))
+          (IS_FUNC (sym->type) ? (sym->used || IFFUNC_HASBODY (sym->type)) : 1))
         {
           addSetHead (&publics, sym);
         }
@@ -247,95 +246,110 @@ emitRegularMap (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 ((SPEC_OCLS(sym->etype)==xidata) && !SPEC_ABSA (sym->etype)) {
-          /* create a new "XINIT (CODE)" symbol, that will be emitted later
-             in the static seg */
-          newSym=copySymbol (sym);
-          SPEC_OCLS(newSym->etype)=xinit;
-          SNPRINTF (newSym->name, sizeof(newSym->name), "__xinit_%s", sym->name);
-          SNPRINTF (newSym->rname, sizeof(newSym->rname), "__xinit_%s", sym->rname);
-          if (IS_SPEC (newSym->type))
-            SPEC_CONST (newSym->type) = 1;
-          else
-            DCL_PTR_CONST (newSym->type) = 1;
-          SPEC_STAT(newSym->etype)=1;
-          resolveIvalSym(newSym->ival, newSym->type);
+      if (sym->ival && sym->level == 0)
+        {
+          if ((SPEC_OCLS (sym->etype) == xidata) && !SPEC_ABSA (sym->etype))
+            {
+              /* create a new "XINIT (CODE)" symbol, that will be emitted later
+                 in the static seg */
+              newSym=copySymbol (sym);
+              SPEC_OCLS(newSym->etype)=xinit;
+              SNPRINTF (newSym->name, sizeof(newSym->name), "__xinit_%s", sym->name);
+              SNPRINTF (newSym->rname, sizeof(newSym->rname), "__xinit_%s", sym->rname);
+              if (IS_SPEC (newSym->type))
+                SPEC_CONST (newSym->type) = 1;
+              else
+                DCL_PTR_CONST (newSym->type) = 1;
+              SPEC_STAT(newSym->etype)=1;
+              resolveIvalSym(newSym->ival, newSym->type);
 
-          // add it to the "XINIT (CODE)" segment
-          addSet(&xinit->syms, newSym);
+              // add it to the "XINIT (CODE)" segment
+              addSet(&xinit->syms, newSym);
 
-          if (!SPEC_ABSA (sym->etype))
+              if (!SPEC_ABSA (sym->etype))
+                {
+                  struct dbuf_s tmpBuf;
+
+                  dbuf_init(&tmpBuf, 4096);
+                  // before allocation we must parse the sym->ival tree
+                  // but without actually generating initialization code
+                  ++noAlloc;
+                  resolveIvalSym (sym->ival, sym->type);
+                  ++noInit;
+                  printIval (sym, sym->type, sym->ival, &tmpBuf);
+                  --noInit;
+                  --noAlloc;
+                  dbuf_destroy(&tmpBuf);
+                }
+            }
+          else
             {
-              struct dbuf_s tmpBuf;
+              if (IS_AGGREGATE (sym->type))
+                {
+                  ival = initAggregates (sym, sym->ival, NULL);
+                }
+              else
+                {
+                  if (getNelements (sym->type, sym->ival)>1)
+                    {
+                      werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar", sym->name);
+                    }
+                  ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
+                                  decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
+                }
+              codeOutBuf = &statsg->oBuf;
 
-              dbuf_init(&tmpBuf, 4096);
-              // before allocation we must parse the sym->ival tree
-              // but without actually generating initialization code
-              noAlloc++;
-              resolveIvalSym (sym->ival, sym->type);
-              printIval (sym, sym->type, sym->ival, &tmpBuf);
-              noAlloc--;
-              dbuf_destroy(&tmpBuf);
-            }
-        } else {
-          if (IS_AGGREGATE (sym->type)) {
-            ival = initAggregates (sym, sym->ival, NULL);
-          } else {
-            if (getNelements(sym->type, sym->ival)>1) {
-              werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar",
-                      sym->name);
-            }
-            ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
-                            decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
-          }
-          codeOutBuf = &statsg->oBuf;
-
-          if (ival) {
-            // set ival's lineno to where the symbol was defined
-            setAstLineno (ival, lineno=sym->lineDef);
-            // check if this is not a constant expression
-            if (!constExprTree(ival)) {
-              werror (E_CONST_EXPECTED, "found expression");
-              // but try to do it anyway
+              if (ival)
+                {
+                  // set ival's lineno to where the symbol was defined
+                  setAstFileLine (ival, filename = sym->fileDef, lineno = sym->lineDef);
+                  // check if this is not a constant expression
+                  if (!constExprTree (ival))
+                    {
+                      werror (E_CONST_EXPECTED, "found expression");
+                    // but try to do it anyway
+                    }
+                  allocInfo = 0;
+                  if (!astErrors (ival))
+                    eBBlockFromiCode (iCodeFromAst (ival));
+                  allocInfo = 1;
+                }
             }
-            allocInfo = 0;
-            if (!astErrors(ival))
-              eBBlockFromiCode (iCodeFromAst (ival));
-            allocInfo = 1;
-          }
         }
-      }
 
       /* if it has an absolute address then generate
          an equate for this no need to allocate space */
       if (SPEC_ABSA (sym->etype) && !sym->ival)
         {
-          char *equ="=";
+          char *equ = "=";
           if (options.debug) {
             dbuf_printf (&map->oBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype));
           }
-          if (TARGET_IS_XA51) {
-            if (map==sfr) {
-              equ="sfr";
-            } else if (map==bit || map==sfrbit) {
-              equ="bit";
+          if (TARGET_IS_XA51)
+            {
+              if (map == sfr)
+                {
+                  equ = "sfr";
+                }
+              else if (map == bit || map == sfrbit)
+                {
+                  equ="bit";
+                }
             }
-          }
-          dbuf_printf (&map->oBuf, "%s\t%s\t0x%04x\n",
-                   sym->rname, equ,
-                   SPEC_ADDR (sym->etype));
+          dbuf_printf (&map->oBuf, "%s\t%s\t0x%04x\n", sym->rname, equ, SPEC_ADDR (sym->etype));
         }
       else
         {
           int size = getSize (sym->type) + sym->flexArrayLength;
-          if (size==0) {
-            werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
-          }
+          if (size == 0)
+            {
+              werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
+            }
           /* allocate space */
-          if (options.debug) {
-            dbuf_printf (&map->oBuf, "==.\n");
-          }
+          if (options.debug)
+            {
+              dbuf_printf (&map->oBuf, "==.\n");
+            }
           if (SPEC_ABSA (sym->etype))
             {
               dbuf_tprintf (&map->oBuf, "\t!org\n", SPEC_ADDR (sym->etype));
@@ -630,7 +644,7 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, struct dbuf_s * o
 
   if (!(val = list2val (ilist))) {
     // assuming a warning has been thrown
-    val=constVal("0");
+    val = constCharVal (0);
   }
 
   if (val->type != type) {
@@ -704,7 +718,7 @@ void printIvalBitFields(symbol **sym, initList **ilist, struct dbuf_s * oBuf)
           (IS_BITFIELD(lsym->next->type)) &&
           (SPEC_BSTR(lsym->next->etype)))) break;
     lsym = lsym->next;
-    lilist = lilist->next;
+    lilist = lilist ? lilist->next : NULL;
   } while (1);
   switch (size) {
   case 1:
@@ -714,9 +728,9 @@ void printIvalBitFields(symbol **sym, initList **ilist, struct dbuf_s * oBuf)
   case 2:
     dbuf_tprintf (oBuf, "\t!dw !constword\n",ival);
     break;
-  case 4: /* EEP: why is this db and not dw? */
-    dbuf_tprintf (oBuf, "\t!db  !constword,!constword\n",
-             (ival >> 8) & 0xffff, (ival & 0xffff));
+  case 4:
+    dbuf_tprintf (oBuf, "\t!dw  !constword,!constword\n",
+             (ival >> 16) & 0xffff, (ival & 0xffff));
     break;
   }
   *sym = lsym;
@@ -881,7 +895,7 @@ printIvalFuncPtr (sym_link * type, initList * ilist, struct dbuf_s * oBuf)
 
   if (!val) {
     // an error has been thrown already
-    val=constVal("0");
+    val = constCharVal (0);
   }
 
   if (IS_LITERAL(val->etype)) {
@@ -1035,7 +1049,7 @@ printIvalCharPtr (symbol * sym, sym_link * type, value * val, struct dbuf_s * oB
         }
     }
 
-  if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
+  if (!noInit && val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
     addSet (&statsg->syms, val->sym);
   }
 
@@ -1411,7 +1425,7 @@ initialComments (FILE * afile)
   time (&t);
   fprintf (afile, "%s", iComments1);
   fprintf (afile, "; Version " SDCC_VERSION_STR " #%s (%s) (%s)\n",
-           getBuildNumber(), __DATE__, getBuildEnvironment());
+           getBuildNumber(), getBuildDate(), getBuildEnvironment());
   fprintf (afile, "; This file was generated %s", asctime (localtime (&t)));
   fprintf (afile, "%s", iComments2);
 }
@@ -1531,7 +1545,8 @@ emitOverlay (struct dbuf_s * aBuf)
                        sym->rname,
                        SPEC_ADDR (sym->etype));
             }
-          else {
+          else
+            {
               int size = getSize(sym->type);
 
               if (size==0) {
@@ -1549,30 +1564,6 @@ emitOverlay (struct dbuf_s * aBuf)
     }
 }
 
-
-/*-----------------------------------------------------------------*/
-/* spacesToUnderscores - replace spaces with underscores        */
-/*-----------------------------------------------------------------*/
-static char *
-spacesToUnderscores (char *dest, const char *src, size_t len)
-{
-  unsigned int i;
-  char *p;
-
-  assert(dest != NULL);
-  assert(src != NULL);
-  assert(len > 0);
-
-  --len;
-  for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) {
-    *p++ = isspace((unsigned char)*src) ? '_' : *src;
-  }
-  *p = '\0';
-
-  return dest;
-}
-
-
 /*-----------------------------------------------------------------*/
 /* glue - the final glue that hold the whole thing together        */
 /*-----------------------------------------------------------------*/
@@ -1582,21 +1573,13 @@ glue (void)
   struct dbuf_s vBuf;
   struct dbuf_s ovrBuf;
   FILE *asmFile;
-  char moduleBuf[PATH_MAX];
   int mcs51_like;
 
-  dbuf_init(&vBuf, 4096);
-  dbuf_init(&ovrBuf, 4096);
+  dbuf_init (&vBuf, 4096);
+  dbuf_init (&ovrBuf, 4096);
 
-  if(port->general.glue_up_main &&
-    (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51 || TARGET_IS_DS400))
-    {
-      mcs51_like=1; /*So it has bits, sfr, sbits, data, idata, etc...*/
-    }
-  else
-    {
-      mcs51_like=0;
-    }
+  mcs51_like = (port->general.glue_up_main &&
+    (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51 || TARGET_IS_DS400));
 
   /* print the global struct definitions */
   if (options.debug)
@@ -1614,7 +1597,7 @@ glue (void)
   /* do the overlay segments */
   emitOverlay (&ovrBuf);
 
-  outputDebugSymbols();
+  outputDebugSymbols ();
 
   /* now put it all together into the assembler file */
   /* create the assembler file name */
@@ -1633,21 +1616,20 @@ glue (void)
   if (!(asmFile = fopen (scratchFileName, "w")))
     {
       werror (E_FILE_OPEN_ERR, scratchFileName);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   /* initial comments */
   initialComments (asmFile);
 
   /* print module name */
-  tfprintf (asmFile, "\t!module\n",
-    spacesToUnderscores (moduleBuf, moduleName, sizeof moduleBuf));
-  if(mcs51_like)
+  tfprintf (asmFile, "\t!module\n", moduleName);
+  if (mcs51_like)
     {
       fprintf (asmFile, "\t.optsdcc -m%s", port->target);
 
       switch(options.model)
-      {
+        {
         case MODEL_SMALL:   fprintf (asmFile, " --model-small");   break;
         case MODEL_COMPACT: fprintf (asmFile, " --model-compact"); break;
         case MODEL_MEDIUM:  fprintf (asmFile, " --model-medium");  break;
@@ -1655,7 +1637,7 @@ glue (void)
         case MODEL_FLAT24:  fprintf (asmFile, " --model-flat24");  break;
         case MODEL_PAGE0:   fprintf (asmFile, " --model-page0");   break;
         default: break;
-      }
+        }
       /*if(options.stackAuto)      fprintf (asmFile, " --stack-auto");*/
       if(options.useXstack)      fprintf (asmFile, " --xstack");
       /*if(options.intlong_rent)   fprintf (asmFile, " --int-long-rent");*/
@@ -1664,7 +1646,7 @@ glue (void)
       if(options.parms_in_bank1) fprintf (asmFile, " --parms-in-bank1");
       fprintf (asmFile, "\n");
     }
-  else if(TARGET_Z80_LIKE || TARGET_IS_HC08)
+  else if (TARGET_Z80_LIKE || TARGET_IS_HC08)
     {
       fprintf (asmFile, "\t.optsdcc -m%s\n", port->target);
     }
@@ -1682,7 +1664,7 @@ glue (void)
   if (port->assembler.externGlobal)
     printExterns (asmFile);
 
-  if(( mcs51_like )
+  if (( mcs51_like )
      ||( TARGET_IS_Z80 )) /*.p.t.20030924 need to output SFR table for Z80 as well */
     {
       /* copy the sfr segment */
@@ -1692,7 +1674,7 @@ glue (void)
       dbuf_write_and_destroy (&sfr->oBuf, asmFile);
     }
 
-  if(mcs51_like)
+  if (mcs51_like)
     {
       /* copy the sbit segment */
       fprintf (asmFile, "%s", iComments2);
@@ -1701,21 +1683,21 @@ glue (void)
       dbuf_write_and_destroy (&sfrbit->oBuf, asmFile);
 
       /*JCF: Create the areas for the register banks*/
-      if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3])
+      if (RegBankUsed[0] || RegBankUsed[1] || RegBankUsed[2] || RegBankUsed[3])
         {
           fprintf (asmFile, "%s", iComments2);
           fprintf (asmFile, "; overlayable register banks\n");
           fprintf (asmFile, "%s", iComments2);
-          if(RegBankUsed[0])
+          if (RegBankUsed[0])
             fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
-          if(RegBankUsed[1]||options.parms_in_bank1)
+          if (RegBankUsed[1] || options.parms_in_bank1)
             fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
-          if(RegBankUsed[2])
+          if (RegBankUsed[2])
             fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
-          if(RegBankUsed[3])
+          if (RegBankUsed[3])
             fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
         }
-      if(BitBankUsed)
+      if (BitBankUsed)
         {
           fprintf (asmFile, "%s", iComments2);
           fprintf (asmFile, "; overlayable bit register bank\n");
@@ -1750,7 +1732,7 @@ glue (void)
     }
 
   /* create the stack segment MOF */
-  if (mainf && IFFUNC_HASBODY(mainf->type))
+  if (mainf && IFFUNC_HASBODY (mainf->type))
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; Stack segment in internal ram \n");
@@ -1760,7 +1742,7 @@ glue (void)
     }
 
   /* create the idata segment */
-  if ( (idata) && (mcs51_like) )
+  if ((idata) && (mcs51_like))
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; indirectly addressable internal ram data\n");
@@ -1769,7 +1751,7 @@ glue (void)
     }
 
   /* create the absolute idata/data segment */
-  if ( (i_abs) && (mcs51_like) )
+  if ((i_abs) && (mcs51_like))
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; absolute internal ram data\n");
@@ -1797,7 +1779,7 @@ glue (void)
     }
 
   /* if external stack then reserve space for it */
-  if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
+  if (mainf && IFFUNC_HASBODY (mainf->type) && options.useXstack)
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; external stack \n");
@@ -1838,7 +1820,7 @@ glue (void)
     }
 
   /* copy the interrupt vector table */
-  if (mainf && IFFUNC_HASBODY(mainf->type))
+  if (mainf && IFFUNC_HASBODY (mainf->type))
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; interrupt vector \n");
@@ -1862,11 +1844,11 @@ glue (void)
   tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
   tfprintf (asmFile, "\t!area\n", port->mem.static_name);
 
-  if (mainf && IFFUNC_HASBODY(mainf->type))
+  if (mainf && IFFUNC_HASBODY (mainf->type))
     {
       if (port->genInitStartup)
         {
-           port->genInitStartup(asmFile);
+           port->genInitStartup (asmFile);
         }
       else
         {
@@ -1902,13 +1884,13 @@ glue (void)
           // if the port can copy the XINIT segment to XISEG
           if (port->genXINIT)
             {
-              port->genXINIT(asmFile);
+              port->genXINIT (asmFile);
             }
         }
     }
   dbuf_write_and_destroy (&statsg->oBuf, asmFile);
 
-  if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
+  if (port->general.glue_up_main && mainf && IFFUNC_HASBODY (mainf->type))
     {
       /* This code is generated in the post-static area.
        * This area is guaranteed to follow the static area
@@ -1925,7 +1907,7 @@ glue (void)
   tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
   dbuf_write_and_destroy (&home->oBuf, asmFile);
 
-  if (mainf && IFFUNC_HASBODY(mainf->type))
+  if (mainf && IFFUNC_HASBODY (mainf->type))
     {
       /* entry point @ start of HOME */
       fprintf (asmFile, "__sdcc_program_startup:\n");
@@ -1952,7 +1934,7 @@ glue (void)
 
   if (port->genAssemblerEnd)
     {
-      port->genAssemblerEnd(asmFile);
+      port->genAssemblerEnd (asmFile);
     }
   fclose (asmFile);
 }