* as/link/mcs51/lkarea.c (lnkarea2): handle absolute areas, restructured
[fw/sdcc] / src / SDCCglue.c
index 2e616548c66f7aab3bf1cfa19d44c0a485bf317f..704656eb518010f1f3e12b75cfed7ba4dce8ee6f 100644 (file)
@@ -91,7 +91,7 @@ DEFSETFUNC (rmTmpFiles)
   int ret;
 
   if (name) {
-      ret = unlink (name);
+      ret = remove (name);
       assert(ret == 0);
       Safe_free (name);
   }
@@ -281,7 +281,7 @@ 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) {
+        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);
@@ -309,8 +309,6 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
               printIval (sym, sym->type, sym->ival, tmpFile);
               noAlloc--;
             }
-
-          sym->ival=NULL;
         } else {
           if (IS_AGGREGATE (sym->type)) {
             ival = initAggregates (sym, sym->ival, NULL);
@@ -338,12 +336,11 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
             allocInfo = 1;
           }
         }         
-        sym->ival = NULL;
       }
 
       /* if it has an absolute address then generate
          an equate for this no need to allocate space */
-      if (SPEC_ABSA (sym->etype))
+      if (SPEC_ABSA (sym->etype) && !sym->ival)
         {
           char *equ="=";
           if (options.debug) {
@@ -360,22 +357,27 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                    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);
-        }
-        /* 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 (SPEC_ABSA (sym->etype))
+            {
+              tfprintf (map->oFile, "\t!org\n", SPEC_ADDR (sym->etype));
+            }
           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);
-      }
+            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);
+        }
+      sym->ival = NULL;
     }
 }
 
@@ -430,7 +432,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;
@@ -1252,8 +1254,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);
@@ -1276,8 +1277,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));
@@ -1294,6 +1295,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);
@@ -1342,6 +1347,8 @@ emitMaps (void)
      data, idata & bit & xdata */
   emitRegularMap (data, TRUE, TRUE);
   emitRegularMap (idata, TRUE, TRUE);
+  emitRegularMap (d_abs, TRUE, TRUE);
+  emitRegularMap (i_abs, TRUE, TRUE);
   emitRegularMap (bit, TRUE, TRUE);
   emitRegularMap (pdata, TRUE, TRUE);
   emitRegularMap (xdata, TRUE, TRUE);
@@ -1361,6 +1368,8 @@ emitMaps (void)
     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--;
 }
 
@@ -1787,6 +1796,15 @@ glue (void)
     copyFile (asmFile, idata->oFile);
   }
 
+  /* create the absolute idata/data segment */
+  if ( (i_abs) && (mcs51_like) ) {
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; absolute internal ram data\n");
+    fprintf (asmFile, "%s", iComments2);
+    copyFile (asmFile, d_abs->oFile);
+    copyFile (asmFile, i_abs->oFile);
+  }
+
   /* copy the bit segment */
   if (mcs51_like) {
     fprintf (asmFile, "%s", iComments2);
@@ -1925,7 +1943,6 @@ glue (void)
 
   if (mainf && IFFUNC_HASBODY(mainf->type))
     {
-
       /* entry point @ start of HOME */
       fprintf (asmFile, "__sdcc_program_startup:\n");
 
@@ -1961,7 +1978,7 @@ glue (void)
 /** Creates a temporary file with unique file name
     Scans, in order:
     - TMP, TEMP, TMPDIR env. variables
-    - if Un*x system: /usr/tmp and /tmp
+    - if Un*x system: /tmp and /var/tmp
     - root directory using mkstemp() if available
     - default location using tempnam()
 */
@@ -1994,14 +2011,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