src/SDCCmain.c (tempfileandname): added check for missing TMP/TEMP/TMPDIR variable
[fw/sdcc] / src / SDCCglue.c
index 8da2373b20efcec722d02ca81def21c604c141e8..9b58776a875091436ab962e67943a084aea3c1e7 100644 (file)
@@ -283,7 +283,8 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
          it is a global variable */
       if (sym->ival && sym->level == 0) {
        if (SPEC_OCLS(sym->etype)==xidata) {
-         // create a new "XINIT (CODE)" symbol, that will be emitted later
+         /* 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);
@@ -621,10 +622,6 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile)
        if (ilist->type == INIT_DEEP)
                ilist = ilist->init.deep;
 
-       if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
-         werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
-       }
-
        if (!(val = list2val (ilist))) {
          // assuming a warning has been thrown
          val=constVal("0");
@@ -787,8 +784,7 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist,
                FILE * oFile)
 {
   initList *iloop;
-  int lcnt = 0, size = 0;
-  sym_link *last_type;
+  int size = 0;
 
   /* take care of the special   case  */
   /* array of characters can be init  */
@@ -810,40 +806,28 @@ printIvalArray (symbol * sym, sym_link * type, initList * ilist,
       return;
     }
 
-  iloop = ilist->init.deep;
-  lcnt = DCL_ELEM (type);
-  for (last_type = type->next; 
-       last_type && IS_DECL(last_type) && DCL_ELEM (last_type); 
-       last_type = last_type->next) {
-    lcnt *= DCL_ELEM (last_type);
-  }
-
-  for (;;)
+  for (iloop=ilist->init.deep; iloop; iloop=iloop->next)
     {
-      size++;
       printIval (sym, type->next, iloop, oFile);
-      iloop = (iloop ? iloop->next : NULL);
-
-
-      /* if not array limits given & we */
-      /* are out of initialisers then   */
-      if (!DCL_ELEM (type) && !iloop)
-       break;
-
-      /* no of elements given and we    */
-      /* have generated for all of them */
-      if (!--lcnt) {
-       /* if initializers left */
-       if (iloop) {
-         werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
-       }
+      
+      if (++size > DCL_ELEM(type)) {
+       werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
        break;
       }
     }
-
-  /* if we have not been given a size  */
-  if (!DCL_ELEM (type))
+  
+  if (DCL_ELEM(type)) {
+    // pad with zeros if needed
+    if (size<DCL_ELEM(type)) {
+      size = (DCL_ELEM(type) - size) * getSize(type->next);
+      while (size--) {
+       tfprintf (oFile, "\t!db !constbyte\n", 0);
+      }
+    }
+  } else {
+    // we have not been given a size, but we now know it
     DCL_ELEM (type) = size;
+  }
 
   return;
 }
@@ -1089,6 +1073,8 @@ printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 void 
 printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 {
+  sym_link *itype;
+  
   if (!ilist)
     return;
 
@@ -1102,17 +1088,43 @@ printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
       return;
     }
 
-  /* if this is a pointer */
-  if (IS_PTR (type))
+  /* if this is an array   */
+  if (IS_ARRAY (type))
     {
-      printIvalPtr (sym, type, ilist, oFile);
+      printIvalArray (sym, type, ilist, oFile);
       return;
     }
 
-  /* if this is an array   */
-  if (IS_ARRAY (type))
+  // not an aggregate, ilist must be a node
+  if (ilist->type!=INIT_NODE) {
+      // or a 1-element list
+    if (ilist->init.deep->next) {
+      werror (W_EXCESS_INITIALIZERS, "scalar", 
+             sym->name, sym->lineDef);
+    } else {
+      ilist=ilist->init.deep;
+    }
+  }
+
+  // and the type must match
+  itype=ilist->init.node->ftype;
+
+  if (compareType(type, itype)==0) {
+    // special case for literal strings
+    if (IS_ARRAY (itype) && IS_CHAR (getSpec(itype)) &&
+       // which are really code pointers
+       IS_PTR(type) && DCL_TYPE(type)==CPOINTER) {
+      // no sweat
+    } else {
+      werror (E_TYPE_MISMATCH, "assignment", " ");
+      printFromToType(itype, type);
+    }
+  }
+
+  /* if this is a pointer */
+  if (IS_PTR (type))
     {
-      printIvalArray (sym, type, ilist, oFile);
+      printIvalPtr (sym, type, ilist, oFile);
       return;
     }
 
@@ -1788,12 +1800,23 @@ tempfileandname(char *fname, size_t len)
 
   const char *tmpdir = NULL;
   int fd;
+  static int warning_emitted;
 
   if ((tmpdir = getenv ("TMP")) == NULL)
     if ((tmpdir = getenv ("TEMP")) == NULL)
       tmpdir = getenv ("TMPDIR");
 
-#ifndef _WIN32
+#if defined(_WIN32)
+  if (tmpdir == NULL)
+    {
+      tmpdir = "c:\\";
+      if (!warning_emitted)
+        {
+          fprintf (stderr, "TMP not defined in environment, using %s for temporary files\n", tmpdir);
+         warning_emitted = 1;
+       }
+    }
+#else
   {
     /* try with /usr/tmp and /tmp on Un*x systems */
     struct stat statbuf;
@@ -1803,6 +1826,10 @@ tempfileandname(char *fname, size_t len)
         tmpdir = "/usr/tmp";
       else if (stat("/tmp", &statbuf) != -1)
         tmpdir = "/tmp";
+      if (!warning_emitted)                                                                                                {
+          fprintf (stderr, "TMP not defined in environment, using %s for temporary files\n", tmpdir);
+          warning_emitted = 1;
+        }
     }
   }
 #endif