* src/pic16/glue.c (pic16_printIvalChar): fixed string
authortecodev <tecodev@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Thu, 26 May 2005 15:25:48 +0000 (15:25 +0000)
committertecodev <tecodev@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Thu, 26 May 2005 15:25:48 +0000 (15:25 +0000)
  initializers with \0, bug #1208187
* src/pic16/main.c (_process_pragma): added sanity checks
  for stack position and size, emit warnings when appropriate

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@3773 4a8a32a2-be11-0410-ad9d-d568d2c75423

ChangeLog
src/pic16/glue.c
src/pic16/main.c

index 753936896fc86b1ebfde3520cb696849f50c8ed5..7de1ae314491b33d683bf8fedada036072a4c867 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2005-05-26 Raphael Neider <rneider AT web.de>
+
+       * src/pic16/glue.c (pic16_printIvalChar): fixed string
+         initializers with \0, bug #1208187
+       * src/pic16/main.c (_process_pragma): added sanity checks
+         for stack position and size, emit warnings when appropriate
+
 2005-05-21 Maarten Brock <sourceforge.brock AT dse.nl>
 
        * device/lib/_strncpy.c: fixed not filling with \0
index 397f6a996d672da9cfd931f7f36243fdcf058c6b..c35c02222b1fe28f61dcfead7abb2e79ab2c9d13 100644 (file)
@@ -638,7 +638,7 @@ static int
 pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, char ptype, void *p)
 {
   value *val;
-  int remain, len;
+  int remain, len, ilen;
 
   if(!p)
     return 0;
@@ -648,11 +648,14 @@ pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, ch
 #endif
 
   if(!s) {
+    /* length of initializer string (might contain \0, so do not use strlen) */
+    ilen = getNelements (type, ilist);
+
     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;
+        DCL_ELEM (type) = ilen;
 
       /* len is 0 if declartion equals initializer,
        * >0 if declaration greater than initializer
@@ -661,23 +664,20 @@ pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, ch
        * 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);
+      len = DCL_ELEM (type) - ilen;
 
-//      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));
+//      fprintf(stderr, "%s:%d ilen = %i len = %i DCL_ELEM(type) = %i SPEC_CVAL-len = %i\n", __FILE__, __LINE__,
+//        ilen, 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++)
+        /* emit initializer */
+        for(remain=0; remain<ilen; remain++)
           pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
 
-        if(len>0) {
-          len -= remain;
+         /* fill array with 0x00 */
           while(len--) {
             pic16_emitDB(0x00, ptype, p);
           }
-        }
-
       } else {
         werror (W_EXCESS_INITIALIZERS, "array of chars", sym->name, sym->lineDef);
 
index a490a264fa16e0555080d1db40a8bf16fd8cfd48..bb5bd72ba508a4b8122b25a3b8cb8ce1cfde8fb3 100644 (file)
@@ -187,6 +187,12 @@ _process_pragma(const char *sz)
     regs *reg;
     symbol *sym;
 
+      if (!stackPosS) {
+        fprintf (stderr, "%s:%d: #pragma stack [stack-pos] [stack-length] -- stack-position missing\n", filename, lineno-1);
+
+        return 0; /* considered an error */
+      }
+
       stackPosVal = constVal( stackPosS );
       stackPos = (unsigned int)floatFromVal( stackPosVal );
 
@@ -204,7 +210,32 @@ _process_pragma(const char *sz)
       }
 
 //      fprintf(stderr, "Initializing stack pointer at 0x%x len 0x%x\n", stackPos, stackLen);
-        
+
+      /* check sanity of stack */
+      if ((stackPos >> 8) != ((stackPos+stackLen) >> 8)) {
+        fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] crosses memory bank boundaries (not fully tested)\n",
+               filename,lineno-1, stackPos, stackPos+stackLen-1);
+      }
+
+      if (pic16) {
+        if (stackPos < pic16->acsSplitOfs) {
+          fprintf (stderr, "%s:%u: warning: stack [0x%03X, 0x%03X] intersects with the access bank [0x000,0x%03x] -- this is highly discouraged!\n",
+               filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->acsSplitOfs);
+        }
+
+        if (stackPos+stackLen > 0xF00 + pic16->acsSplitOfs) {
+          fprintf (stderr, "%s:%u: warning: stack [0x%03X,0x%03X] intersects with special function registers [0x%03X,0xFFF]-- this is highly discouraged!\n",
+               filename, lineno-1, stackPos, stackPos+stackLen-1, 0xF00 + pic16->acsSplitOfs);
+        }
+
+        if (stackPos+stackLen > pic16->RAMsize) {
+          fprintf (stderr, "%s:%u: error: stack [0x%03X,0x%03X] is placed outside available memory [0x000,0x%03X]!\n",
+               filename, lineno-1, stackPos, stackPos+stackLen-1, pic16->RAMsize-1);
+          exit(-1);
+          return 1;    /* considered an error, but this reports "invalid pragma stack"... */
+        }
+      }
+
       reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
       addSet(&pic16_fix_udata, reg);
     
@@ -233,7 +264,8 @@ _process_pragma(const char *sz)
 
       if (!symname || !location) {
         fprintf (stderr, "%s:%d: #pragma code [symbol] [location] -- symbol or location missing\n", filename, lineno-1);
-        return 1; /* considered an error */
+       exit (-1);
+        return 1; /* considered an error, but this reports "invalid pragma code"... */
       }
 
       absS = Safe_calloc(1, sizeof(absSym));
@@ -266,7 +298,8 @@ _process_pragma(const char *sz)
     
       if (!symname || !sectname) {
         fprintf (stderr, "%s:%d: #pragma udata [section-name] [symbol] -- section-name or symbol missing!\n", filename, lineno-1);
-        return 1; /* considered an error */
+       exit (-1);
+        return 1; /* considered an error, but this reports "invalid pragma code"... */
       }
     
       while(symname) {