* src/SDCCglue.c (tempfileandname): changed un*x tmp search paths to /tmp and /var...
[fw/sdcc] / src / SDCCglue.c
index 1c463a294e25e1eea047a5f106164b86de3afa1c..a4b8d4f8cf7a9891105d0afb1d8857bce73949db 100644 (file)
@@ -39,10 +39,9 @@ symbol *interrupts[INTNO_MAX+1];
 
 void printIval (symbol *, sym_link *, initList *, FILE *);
 set *publics = NULL;            /* public variables */
-set *externs = NULL;            /* Varibles that are declared as extern */
+set *externs = NULL;            /* Variables that are declared as extern */
 
-/* TODO: this should be configurable (DS803C90 uses more than 6) */
-unsigned maxInterrupts = 6;
+unsigned maxInterrupts = 0;
 int allocInfo = 1;
 symbol *mainf;
 set *pipeSet = NULL;            /* set of pipes */
@@ -92,7 +91,7 @@ DEFSETFUNC (rmTmpFiles)
   int ret;
 
   if (name) {
-      ret = unlink (name);
+      ret = remove (name);
       assert(ret == 0);
       Safe_free (name);
   }
@@ -321,7 +320,7 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                       sym->name);
             }
             ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
-                            decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
+                            decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
           }
           codeOutFile = statsg->oFile;
 
@@ -361,22 +360,23 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                    sym->rname, equ,
                    SPEC_ADDR (sym->etype));
         }
-      else {
-        int size = getAllocSize (sym->type);
-        if (size==0) {
-          werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
-        }
-        /* allocate space */
-        if (options.debug) {
-          fprintf (map->oFile, "==.\n");
+      else
+        {
+          int size = getSize (sym->type) + sym->flexArrayLength;
+          if (size==0) {
+            werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
+          }
+          /* allocate space */
+          if (options.debug) {
+            fprintf (map->oFile, "==.\n");
+          }
+          if (IS_STATIC (sym->etype) || sym->level)
+            tfprintf (map->oFile, "!slabeldef\n", sym->rname);
+          else
+            tfprintf (map->oFile, "!labeldef\n", sym->rname);           
+          tfprintf (map->oFile, "\t!ds\n", 
+                    (unsigned int)  size & 0xffff);
         }
-        if (IS_STATIC (sym->etype))
-          tfprintf (map->oFile, "!slabeldef\n", sym->rname);
-        else
-          tfprintf (map->oFile, "!labeldef\n", sym->rname);           
-        tfprintf (map->oFile, "\t!ds\n", 
-                  (unsigned int)  size & 0xffff);
-      }
     }
 }
 
@@ -431,7 +431,7 @@ initPointer (initList * ilist, sym_link *toType)
   if (IS_AST_OP (expr) && expr->opval.op == '&') {
     /* address of symbol */
     if (IS_AST_SYM_VALUE (expr->left)) {
-      val = copyValue (AST_VALUE (expr->left));
+      val = AST_VALUE (expr->left);
       val->type = newLink (DECLARATOR);
       if (SPEC_SCLS (expr->left->etype) == S_CODE) {
         DCL_TYPE (val->type) = CPOINTER;
@@ -774,11 +774,16 @@ printIvalStruct (symbol * sym, sym_link * type,
     iloop = ilist->init.deep;
   }
 
-  for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
-    if (IS_BITFIELD(sflds->type)) {
-      printIvalBitFields(&sflds,&iloop,oFile);
-    } else {
-      printIval (sym, sflds->type, iloop, oFile);
+  if (SPEC_STRUCT (type)->type == UNION) {
+    printIval (sym, sflds->type, iloop, oFile);
+    iloop = iloop->next;
+  } else {
+    for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
+      if (IS_BITFIELD(sflds->type)) {
+        printIvalBitFields(&sflds,&iloop,oFile);
+      } else {
+        printIval (sym, sflds->type, iloop, oFile);
+      }
     }
   }
   if (iloop) {
@@ -791,21 +796,29 @@ printIvalStruct (symbol * sym, sym_link * type,
 /* printIvalChar - generates initital value for character array    */
 /*-----------------------------------------------------------------*/
 int 
-printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
+printIvalChar (symbol * sym, sym_link * type, initList * ilist, FILE * oFile, char *s)
 {
   value *val;
+  unsigned int size = DCL_ELEM (type);
 
   if (!s)
     {
-
       val = list2val (ilist);
       /* if the value is a character string  */
       if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
         {
-          if (!DCL_ELEM (type))
-            DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
+          if (!size)
+            {
+              /* we have not been given a size, but now we know it */
+              size = strlen (SPEC_CVAL (val->etype).v_char) + 1;
+              /* but first check, if it's a flexible array */
+              if (sym && IS_STRUCT (sym->type))
+                sym->flexArrayLength = size;
+              else
+                DCL_ELEM (type) = size;
+            }
 
-          printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
+          printChar (oFile, SPEC_CVAL (val->etype).v_char, size);
 
           return 1;
         }
@@ -824,6 +837,7 @@ void
 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
                 FILE * oFile)
 {
+  value *val;
   initList *iloop;
   unsigned int size = 0;
 
@@ -832,11 +846,16 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist,
     /* array of characters can be init  */
     /* by a string                      */
     if (IS_CHAR (type->next)) {
-      if (!IS_LITERAL(list2val(ilist)->etype)) {
+      val = list2val(ilist);
+      if (!val) {
+        werrorfl (ilist->filename, ilist->lineno, E_INIT_STRUCT, sym->name);
+        return;
+      }
+      if (!IS_LITERAL(val->etype)) {
         werrorfl (ilist->filename, ilist->lineno, E_CONST_EXPECTED);
         return;
       }
-      if (printIvalChar (type,
+      if (printIvalChar (sym, type,
                          (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
                          oFile, SPEC_CVAL (sym->etype).v_char))
         return;
@@ -865,8 +884,12 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist,
       }
     }
   } else {
-    // we have not been given a size, but we now know it
-    DCL_ELEM (type) = size;
+    /* we have not been given a size, but now we know it */
+    /* but first check, if it's a flexible array */
+    if (IS_STRUCT (sym->type))
+      sym->flexArrayLength = size * getSize (type->next);
+    else
+      DCL_ELEM (type) = size;
   }
 
   return;
@@ -1230,8 +1253,7 @@ emitStaticSeg (memmap * map, FILE * out)
       if (IS_EXTERN (sym->etype))
         continue;
 
-      /* if it is not static add it to the public
-         table */
+      /* if it is not static add it to the public table */
       if (!IS_STATIC (sym->etype))
         {
           addSetHead (&publics, sym);
@@ -1254,8 +1276,8 @@ emitStaticSeg (memmap * map, FILE * out)
         fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block);
       }
       
-      /* if it has an absolute address */
-      if (SPEC_ABSA (sym->etype))
+      /* if it has an absolute address and no initializer */
+      if (SPEC_ABSA (sym->etype) && !sym->ival)
         {
           if (options.debug)
             fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype));
@@ -1272,6 +1294,10 @@ emitStaticSeg (memmap * map, FILE * out)
           /* if it has an initial value */
           if (sym->ival)
             {
+              if (SPEC_ABSA (sym->etype))
+                {
+                  tfprintf (out, "\t!org\n", SPEC_ADDR (sym->etype));
+                }
               fprintf (out, "%s:\n", sym->rname);
               noAlloc++;
               resolveIvalSym (sym->ival, sym->type);
@@ -1320,7 +1346,8 @@ emitMaps (void)
      data, idata & bit & xdata */
   emitRegularMap (data, TRUE, TRUE);
   emitRegularMap (idata, TRUE, TRUE);
-  emitRegularMap (bit, TRUE, FALSE);
+  emitRegularMap (bit, TRUE, TRUE);
+  emitRegularMap (pdata, TRUE, TRUE);
   emitRegularMap (xdata, TRUE, TRUE);
   if (port->genXINIT) {
     emitRegularMap (xidata, TRUE, TRUE);
@@ -1330,11 +1357,16 @@ emitMaps (void)
   emitRegularMap (home, TRUE, FALSE);
   emitRegularMap (code, TRUE, FALSE);
 
+  if (options.const_seg) {
+    tfprintf (code->oFile, "\t!area\n", options.const_seg);
+  }
   emitStaticSeg (statsg, code->oFile);
   if (port->genXINIT) {
     tfprintf (code->oFile, "\t!area\n", xinit->sname);
     emitStaticSeg (xinit, code->oFile);
   }
+  tfprintf (code->oFile, "\t!area\n", c_abs->sname);
+  emitStaticSeg (c_abs, code->oFile);
   inInitMode--;
 }
 
@@ -1355,7 +1387,6 @@ flushStatics (void)
 void 
 createInterruptVect (FILE * vFile)
 {
-  unsigned i = 0;
   mainf = newSymbol ("main", 0);
   mainf->block = 0;
 
@@ -1376,34 +1407,14 @@ createInterruptVect (FILE * vFile)
       return;
     }
 
-  tfprintf (vFile, "\t!areacode\n", CODE_NAME);
+  tfprintf (vFile, "\t!areacode\n", HOME_NAME);
   fprintf (vFile, "__interrupt_vect:\n");
 
 
   if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts)))
     {
-      /* "generic" interrupt table header (if port doesn't specify one).
-       * Look suspiciously like 8051 code to me...
-       */
-
-      fprintf (vFile, "\tljmp\t__sdcc_gsinit_startup\n");
-
-      /* now for the other interrupts */
-      for (; i < maxInterrupts; i++)
-        {
-          if (interrupts[i])
-            {
-              fprintf (vFile, "\tljmp\t%s\n", interrupts[i]->rname);
-              if ( i != maxInterrupts - 1 )
-                fprintf (vFile, "\t.ds\t5\n");
-            }
-          else
-            {
-              fprintf (vFile, "\treti\n");
-              if ( i != maxInterrupts - 1 )
-                fprintf (vFile, "\t.ds\t7\n");
-            }
-        }
+      /* There's no such thing as a "generic" interrupt table header. */
+      wassert(0);
     }
 }
 
@@ -1580,7 +1591,7 @@ spacesToUnderscores (char *dest, const char *src, size_t len)
 
   --len;
   for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) {
-    *p++ = isspace(*src) ? '_' : *src;
+    *p++ = isspace((unsigned char)*src) ? '_' : *src;
   }
   *p = '\0';
 
@@ -1616,7 +1627,7 @@ glue (void)
     cdbStructBlock (0);
 
   vFile = tempfile ();
-  /* PENDING: this isnt the best place but it will do */
+  /* PENDING: this isn't the best place but it will do */
   if (port->general.glue_up_main)
     {
       /* create the interrupt vector table */
@@ -1712,7 +1723,7 @@ glue (void)
     {
       /* copy the sbit segment */
       fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, "; special function bits \n");
+      fprintf (asmFile, "; special function bits\n");
       fprintf (asmFile, "%s", iComments2);
       copyFile (asmFile, sfrbit->oFile);
   
@@ -1720,16 +1731,32 @@ glue (void)
       if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3])
       {
          fprintf (asmFile, "%s", iComments2);
-         fprintf (asmFile, "; overlayable register banks \n");
+         fprintf (asmFile, "; overlayable register banks\n");
          fprintf (asmFile, "%s", iComments2);
          if(RegBankUsed[0])
-            fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
+           fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
          if(RegBankUsed[1]||options.parms_in_bank1)
-            fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
+           fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
          if(RegBankUsed[2])
-            fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
+           fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
          if(RegBankUsed[3])
-            fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
+           fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
+      }
+      if(BitBankUsed)
+      {
+         fprintf (asmFile, "%s", iComments2);
+         fprintf (asmFile, "; overlayable bit register bank\n");
+         fprintf (asmFile, "%s", iComments2);
+         fprintf (asmFile, "\t.area BIT_BANK\t(REL,OVR,DATA)\n");
+         fprintf (asmFile, "bits:\n\t.ds 1\n");
+         fprintf (asmFile, "\tb0 = bits[0]\n");
+         fprintf (asmFile, "\tb1 = bits[1]\n");
+         fprintf (asmFile, "\tb2 = bits[2]\n");
+         fprintf (asmFile, "\tb3 = bits[3]\n");
+         fprintf (asmFile, "\tb4 = bits[4]\n");
+         fprintf (asmFile, "\tb5 = bits[5]\n");
+         fprintf (asmFile, "\tb6 = bits[6]\n");
+         fprintf (asmFile, "\tb7 = bits[7]\n");
       }
     }
 
@@ -1774,18 +1801,26 @@ glue (void)
     copyFile (asmFile, bit->oFile);
   }
 
-  /* if external stack then reserve space of it */
+  /* copy paged external ram data */
+  if (mcs51_like)
+    {
+      fprintf (asmFile, "%s", iComments2);
+      fprintf (asmFile, "; paged external ram data\n");
+      fprintf (asmFile, "%s", iComments2);
+      copyFile (asmFile, pdata->oFile);
+    }
+
+  /* if external stack then reserve space for it */
   if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
     {
       fprintf (asmFile, "%s", iComments2);
       fprintf (asmFile, "; external stack \n");
       fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, "\t.area XSEG (XDATA)\n");      /* MOF */
-      fprintf (asmFile, "\t.ds 256\n");
+      fprintf (asmFile, "\t.area XSTK (PAG,XDATA)\n"
+               "__start__xstack:\n\t.ds\t1\n\n");
     }
 
-
-  /* copy xtern ram data */
+  /* copy external ram data */
   if (mcs51_like) {
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; external ram data\n");
@@ -1793,7 +1828,7 @@ glue (void)
     copyFile (asmFile, xdata->oFile);
   }
 
-  /* copy xternal initialized ram data */
+  /* copy external initialized ram data */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; external initialized ram data\n");
   fprintf (asmFile, "%s", iComments2);
@@ -1826,6 +1861,7 @@ glue (void)
    * the post_static_name area will immediately follow the static_name
    * area.
    */
+  tfprintf (asmFile, "\t!area\n", port->mem.home_name);
   tfprintf (asmFile, "\t!area\n", port->mem.static_name);       /* MOF */
   tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
   tfprintf (asmFile, "\t!area\n", port->mem.static_name);
@@ -1893,15 +1929,9 @@ glue (void)
   tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
   copyFile (asmFile, home->oFile);
 
-  /* copy over code */
-  fprintf (asmFile, "%s", iComments2);
-  fprintf (asmFile, "; code\n");
-  fprintf (asmFile, "%s", iComments2);
-  tfprintf (asmFile, "\t!areacode\n", CODE_NAME);
   if (mainf && IFFUNC_HASBODY(mainf->type))
     {
-
-      /* entry point @ start of CSEG */
+      /* entry point @ start of HOME */
       fprintf (asmFile, "__sdcc_program_startup:\n");
 
       /* put in jump or call to main */
@@ -1917,6 +1947,11 @@ glue (void)
           fprintf (asmFile, "\tsjmp .\n");
         }
     }
+  /* copy over code */
+  fprintf (asmFile, "%s", iComments2);
+  fprintf (asmFile, "; code\n");
+  fprintf (asmFile, "%s", iComments2);
+  tfprintf (asmFile, "\t!areacode\n", options.code_seg);
   copyFile (asmFile, code->oFile);
 
   if (port->genAssemblerEnd) {
@@ -1930,9 +1965,9 @@ glue (void)
 
 /** Creates a temporary file with unique file name
     Scans, in order:
-    - TMP, TEMP, TMPDIR env. varibles
-    - if Un*x system: /usr/tmp and /tmp
-    - root directory using mkstemp() if avaliable
+    - TMP, TEMP, TMPDIR env. variables
+    - if Un*x system: /tmp and /var/tmp
+    - root directory using mkstemp() if available
     - default location using tempnam()
 */
 static int
@@ -1964,14 +1999,14 @@ tempfileandname(char *fname, size_t len)
   }
 #else
   {
-    /* try with /usr/tmp and /tmp on Un*x systems */
+    /* try with /tmp and /var/tmp on Un*x systems */
     struct stat statbuf;
 
     if (tmpdir == NULL) {
-      if (stat("/usr/tmp", &statbuf) != -1)
-        tmpdir = "/usr/tmp";
-      else if (stat("/tmp", &statbuf) != -1)
+      if (stat("/tmp", &statbuf) != -1)
         tmpdir = "/tmp";
+      else if (stat("/var/tmp", &statbuf) != -1)
+        tmpdir = "/var/tmp";
     }
   }
 #endif