* src/pic16/glue.c, src/SDCC.y, src/SDCCast.c, src/SDCCglue.c,
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 15 Nov 2008 14:13:47 +0000 (14:13 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 15 Nov 2008 14:13:47 +0000 (14:13 +0000)
  src/SDCCsymt.c, src/SDCCsymt.h:
  unnamed bitfields are not initialized (gcc, msvc comatibility)

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

src/SDCC.y
src/SDCCast.c
src/SDCCglue.c
src/SDCCsymt.c
src/SDCCsymt.h
src/pic16/glue.c

index d162cc84583694afc41bfa86a4dbff42b765eea1..fa165cef8a7241af3357d437e5b64218ae986435 100644 (file)
@@ -926,6 +926,7 @@ struct_declarator
                            if (!bitsize)
                              bitsize = BITVAR_PAD;
                            $$->bitVar = bitsize;
+                           $$->bitUnnamed = 1;
                         }
    | declarator ':' constant_expr
                         {
index 2e6d1f8e8cdda4e9a7f602fd2be6c76608d09b03..896a9f6bad54c41db310c4aaff5f08817846b2d9 100644 (file)
@@ -971,7 +971,7 @@ createIvalType (ast * sym, sym_link * type, initList * ilist)
 /* createIvalStruct - generates initial value for structures       */
 /*-----------------------------------------------------------------*/
 static ast *
-createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
+createIvalStruct (ast *sym, sym_link *type, initList *ilist, ast *rootValue)
 {
   ast *rast = NULL;
   ast *lAst;
@@ -979,7 +979,6 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
   initList *iloop;
   sym_link * etype = getSpec (type);
 
-  sflds = SPEC_STRUCT (type)->fields;
   if (ilist && ilist->type != INIT_DEEP)
     {
       werror (E_INIT_STRUCT, "");
@@ -988,20 +987,20 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
 
   iloop = ilist ? ilist->init.deep : NULL;
 
-  for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
+  for (sflds = SPEC_STRUCT (type)->fields; sflds; sflds = sflds->next)
     {
       /* if we have come to end */
       if (!iloop && (!AST_SYMBOL (rootValue)->islocal || SPEC_STAT (etype)))
+        break;
+
+      if (!IS_BITFIELD (sflds->type) || !SPEC_BUNNAMED (sflds->etype))
         {
-          break;
+          sflds->implicit = 1;
+          lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
+          lAst = decorateType (resolveSymbols (lAst), RESULT_TYPE_NONE);
+          rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast, rootValue)), RESULT_TYPE_NONE);
+          iloop = iloop ? iloop->next : NULL;
         }
-
-      sflds->implicit = 1;
-      lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
-      lAst = decorateType (resolveSymbols (lAst), RESULT_TYPE_NONE);
-      rast = decorateType (resolveSymbols (createIval (lAst, sflds->type,
-                                                       iloop, rast, rootValue)),
-                           RESULT_TYPE_NONE);
     }
 
   if (iloop)
@@ -1012,7 +1011,7 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
                   sym->opval.val->sym->name);
       else
         werrorfl (sym->filename, sym->lineno, E_INIT_COUNT);
-  }
+    }
 
   return rast;
 }
index 374288498b38375c57fce62185a1f477f7e710ce..060ef51547a28d7d0ba30125c03b0ca0ec8cf7d0 100644 (file)
@@ -646,68 +646,65 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, struct dbuf_s * o
 /*-----------------------------------------------------------------*/
 /* printIvalBitFields - generate initializer for bitfields         */
 /*-----------------------------------------------------------------*/
-void printIvalBitFields(symbol **sym, initList **ilist, struct dbuf_s * oBuf)
+static void
+printIvalBitFields (symbol **sym, initList **ilist, struct dbuf_s * oBuf)
 {
-  value *val ;
   symbol *lsym = *sym;
-  initList *lilist = *ilist ;
+  initList *lilist = *ilist;
   unsigned long ival = 0;
   int size = 0;
 
-  do
+  while (lsym)
     {
-      unsigned long i;
-      val = list2val (lilist);
-      if (size)
+
+      if (0 == SPEC_BLEN (lsym->etype))
         {
-          if (SPEC_BLEN (lsym->etype) > 8)
-            {
-              size += ((SPEC_BLEN (lsym->etype) / 8) +
-                       (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
-            }
+          /* bit-field structure member with a width of 0 */
+          break;
         }
-      else
+      else if (!SPEC_BUNNAMED (lsym->etype))
         {
-          size = ((SPEC_BLEN (lsym->etype) / 8) +
-                  (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
-        }
+          /* not an unnamed bit-field structure member */
+          value *val = list2val (lilist);
+          int bit_length = SPEC_BLEN (lsym->etype);
 
-      /* check if the literal value is within bounds */
-      if (val &&
-        checkConstantRange (lsym->etype, val->etype, '=', FALSE) == CCR_OVL &&
-        !options.lessPedantic)
-        {
-          werror (W_LIT_OVERFLOW);
-        }
+          if (size)
+            {
+              if (bit_length > 8)
+                size += (bit_length + 7) / 8;
+            }
+          else
+            size = (bit_length + 7) / 8;
 
-      i = ulFromVal (val);
-      i &= (1 << SPEC_BLEN (lsym->etype)) - 1;
-      i <<= SPEC_BSTR (lsym->etype);
-      ival |= i;
-      if (!(lsym->next &&
-        (IS_BITFIELD (lsym->next->type)) &&
-        (SPEC_BSTR (lsym->next->etype))))
-        break;
+          /* check if the literal value is within bounds */
+          if (val &&
+            checkConstantRange (lsym->etype, val->etype, '=', FALSE) == CCR_OVL &&
+            !options.lessPedantic)
+            {
+              werror (W_LIT_OVERFLOW);
+            }
+
+          ival |= (ulFromVal (val) & ((1ul << bit_length) - 1ul)) << SPEC_BSTR (lsym->etype);
+          lilist = lilist ? lilist->next : NULL;
+        }
       lsym = lsym->next;
-      lilist = lilist ? lilist->next : NULL;
     }
-  while (1);
 
   switch (size)
-  {
-  case 1:
-    dbuf_tprintf (oBuf, "\t!db !constbyte\n", ival);
-    break;
+    {
+    case 1:
+      dbuf_tprintf (oBuf, "\t!db !constbyte\n", ival);
+      break;
 
-  case 2:
-    dbuf_tprintf (oBuf, "\t!dw !constword\n", ival);
-    break;
+    case 2:
+      dbuf_tprintf (oBuf, "\t!dw !constword\n", ival);
+      break;
 
-  case 4:
-    dbuf_tprintf (oBuf, "\t!dw  !constword,!constword\n",
-      (ival >> 16) & 0xffff, (ival & 0xffff));
-    break;
-  }
+    case 4:
+      dbuf_tprintf (oBuf, "\t!dw  !constword,!constword\n",
+        (ival >> 16) & 0xffff, (ival & 0xffff));
+      break;
+    }
   *sym = lsym;
   *ilist = lilist;
 }
@@ -715,7 +712,7 @@ void printIvalBitFields(symbol **sym, initList **ilist, struct dbuf_s * oBuf)
 /*-----------------------------------------------------------------*/
 /* printIvalStruct - generates initial value for structures        */
 /*-----------------------------------------------------------------*/
-void
+static void
 printIvalStruct (symbol * sym, sym_link * type,
                  initList * ilist, struct dbuf_s * oBuf)
 {
@@ -724,31 +721,37 @@ printIvalStruct (symbol * sym, sym_link * type,
 
   sflds = SPEC_STRUCT (type)->fields;
 
-  if (ilist) {
-    if (ilist->type != INIT_DEEP) {
-      werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
-      return;
-    }
+  if (ilist)
+    {
+      if (ilist->type != INIT_DEEP)
+        {
+          werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
+          return;
+        }
 
-    iloop = ilist->init.deep;
-  }
+      iloop = ilist->init.deep;
+    }
 
-  if (SPEC_STRUCT (type)->type == UNION) {
-    printIval (sym, sflds->type, iloop, oBuf, TRUE);
-    iloop = iloop ? iloop->next : NULL;
-  } else {
-    for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
-      if (IS_BITFIELD(sflds->type)) {
-        printIvalBitFields(&sflds, &iloop, oBuf);
-      } else {
-        printIval (sym, sflds->type, iloop, oBuf, TRUE);
-      }
+  if (SPEC_STRUCT (type)->type == UNION)
+    {
+      printIval (sym, sflds->type, iloop, oBuf, 1);
+      iloop = iloop ? iloop->next : NULL;
     }
-  }
-  if (iloop) {
+  else
+    {
+      while (sflds)
+        {
+          if (IS_BITFIELD (sflds->type))
+            printIvalBitFields(&sflds, &iloop, oBuf);
+          else
+            printIval (sym, sflds->type, iloop, oBuf, 1);
+            sflds = sflds->next;
+            iloop = iloop ? iloop->next : NULL;
+        }
+    }
+
+  if (iloop)
     werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "struct", sym->name);
-  }
-  return;
 }
 
 /*-----------------------------------------------------------------*/
@@ -768,6 +771,9 @@ printIvalChar (symbol * sym, sym_link * type, initList * ilist, struct dbuf_s *
         {
           if (!size)
             {
+              werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+                "size should never be 0");
+
               /* 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 */
index e17d7c515205afbccafa61195eb4955d6f729a8b..c7623d458dd7e37206ce3f4a01c85522535bd461 100644 (file)
@@ -1272,6 +1272,8 @@ compStructSize (int su, structdef * sdef)
     /* if this is a bit field  */
     if (loop->bitVar) {
 
+      SPEC_BUNNAMED (loop->etype) = loop->bitUnnamed;
+
       /* change it to a unsigned bit */
       SPEC_NOUN (loop->etype) = V_BITFIELD;
       /* ISO/IEC 9899 J.3.9 implementation defined behaviour: */
index ae82145e5ce959269916d363b26d04dc06f600b0..8fa773c7d44946218fc11d26092cce483fd88d2d 100644 (file)
@@ -67,10 +67,10 @@ enum {
 #define GPTYPE_XSTACK    0x60
 #define GPTYPE_CODE      0x80
 #else
-#define GPTYPE_FAR     (port->gp_tags.tag_far)
-#define GPTYPE_NEAR    (port->gp_tags.tag_near)
-#define GPTYPE_XSTACK  (port->gp_tags.tag_xstack)
-#define GPTYPE_CODE    (port->gp_tags.tag_code)
+#define GPTYPE_FAR      (port->gp_tags.tag_far)
+#define GPTYPE_NEAR     (port->gp_tags.tag_near)
+#define GPTYPE_XSTACK   (port->gp_tags.tag_xstack)
+#define GPTYPE_CODE     (port->gp_tags.tag_code)
 #endif
 
 #define HASHTAB_SIZE 256
@@ -164,10 +164,11 @@ typedef struct specifier
     unsigned b_typedef:1;               /* is typedefed               */
     unsigned b_isregparm:1;             /* is the first parameter     */
     unsigned b_isenum:1;                /* is an enumerated type      */
-    unsigned _addr;                     /* address of symbol          */
-    unsigned _stack;                    /* stack offset for stacked v */
+    unsigned b_bitUnnamed:1;            /* is an unnamed bit-field    */
     unsigned _bitStart;                 /* bit start position         */
     int _bitLength;                     /* bit length                 */
+    unsigned _addr;                     /* address of symbol          */
+    unsigned _stack;                    /* stack offset for stacked v */
     int argreg;                         /* reg no for regparm         */
     union
       {                                 /* Values if constant or enum */
@@ -337,7 +338,8 @@ typedef struct symbol
         struct set *itmpStack;          /* symbols spilt @ this stack location */
       }
     usl;
-    short bitVar;                       /* this is a bit variable    */
+    char bitVar;                        /* if bitVar != 0: this is a bit variable, bitVar is the size in bits */
+    char bitUnnamed:1;                  /* unnamed bit variable */
     unsigned offset;                    /* offset from top if struct */
 
     int lineDef;                        /* defined line number        */
@@ -441,6 +443,7 @@ extern sym_link *validateLink(sym_link  *l,
 #define SPEC_CVAL(x) validateLink(x, "SPEC_CVAL", #x, SPECIFIER, __FILE__, __LINE__)->select.s.const_val
 #define SPEC_BSTR(x) validateLink(x, "SPEC_BSTR", #x, SPECIFIER, __FILE__, __LINE__)->select.s._bitStart
 #define SPEC_BLEN(x) validateLink(x, "SPEC_BLEN", #x, SPECIFIER, __FILE__, __LINE__)->select.s._bitLength
+#define SPEC_BUNNAMED(x) validateLink(x, "SPEC_BLEN", #x, SPECIFIER, __FILE__, __LINE__)->select.s.b_bitUnnamed
 
 /* Sleaze: SPEC_ISR_SAVED_BANKS is only used on
  * function type symbols, which obviously cannot
index b26f0478ef131c2164e07b570e04ad02028bf752..374537b008356c8eba41de16afe2d110f6cb790e 100644 (file)
@@ -773,14 +773,13 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
 /* pic16_printIvalBitFields - generate initializer for bitfields   */
 /*-----------------------------------------------------------------*/
 static void
-pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *p)
+pic16_printIvalBitFields (symbol **sym, initList **ilist, char ptype, void *p)
 {
-  value *val ;
   symbol *lsym = *sym;
-  initList *lilist = *ilist ;
+  initList *lilist = *ilist;
   unsigned long ival = 0;
+  int size = 0;
   unsigned long i;
-  int size =0;
 
 
 #if DEBUG_PRINTIVAL
@@ -788,31 +787,35 @@ pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *p)
 #endif
 
 
-  do {
-    val = list2val(lilist);
-    if (size) {
-      if (SPEC_BLEN(lsym->etype) > 8) {
-        size += ((SPEC_BLEN (lsym->etype) / 8) +
-                 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
-      }
-    } else {
-      size = ((SPEC_BLEN (lsym->etype) / 8) +
-              (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
+  while (lsym)
+    {
+      if (0 == SPEC_BLEN (lsym->etype))
+        {
+          /* bit-field structure member with a width of 0 */
+          break;
+        }
+      else if (!SPEC_BUNNAMED (lsym->etype))
+        {
+          /* not an unnamed bit-field structure member */
+          value *val = list2val (lilist);
+          int bit_length = SPEC_BLEN (lsym->etype);
+
+          if (size)
+            {
+              if (bit_length > 8)
+                size += (bit_length + 7) / 8;
+            }
+          else
+            size = (bit_length + 7) / 8;
+
+          ival |= (ulFromVal (val) & ((1ul << bit_length) - 1ul)) << SPEC_BSTR (lsym->etype);
+          lilist = lilist->next;
+        }
+      lsym = lsym->next;
     }
-    i = (ulFromVal (val) & ((1ul << SPEC_BLEN (lsym->etype)) - 1ul));
-    i <<= SPEC_BSTR (lsym->etype);
-    ival |= i;
-    if (! ( lsym->next &&
-          (lilist && lilist->next) &&
-          (IS_BITFIELD(lsym->next->type)) &&
-          (SPEC_BSTR(lsym->next->etype)))) break;
-    lsym = lsym->next;
-    lilist = lilist->next;
-  } while (1);
-
-  for (i = 0; i < size; i++) {
-    pic16_emitDB(BYTE_IN_LONG(ival, i), ptype, p);
-  } // for
+
+  for (i = 0; i < size; i++)
+    pic16_emitDB (BYTE_IN_LONG (ival, i), ptype, p);
 
   *sym = lsym;
   *ilist = lilist;
@@ -836,27 +839,33 @@ pic16_printIvalStruct (symbol * sym, sym_link * type,
 
   sflds = SPEC_STRUCT (type)->fields;
 
-  if (ilist) {
-    if (ilist->type != INIT_DEEP) {
-      werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
-      return;
-    }
+  if (ilist)
+    {
+      if (ilist->type != INIT_DEEP)
+        {
+          werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
+          return;
+        }
 
-    iloop = ilist->init.deep;
-  }
+      iloop = ilist->init.deep;
+    }
 
-  for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
-//    fprintf(stderr, "%s:%d sflds: %p\tiloop = %p\n", __FILE__, __LINE__, sflds, iloop);
-    if (IS_BITFIELD(sflds->type)) {
-      pic16_printIvalBitFields(&sflds, &iloop, ptype, p);
-    } else {
-      pic16_printIval (sym, sflds->type, iloop, ptype, p);
+  while (sflds)
+    {
+//      fprintf(stderr, "%s:%d sflds: %p\tiloop = %p\n", __FILE__, __LINE__, sflds, iloop);
+      if (IS_BITFIELD (sflds->type))
+        {
+          pic16_printIvalBitFields (&sflds, &iloop, ptype, p);
+        }
+      else
+        {
+          pic16_printIval (sym, sflds->type, iloop, ptype, p);
+          sflds = sflds->next;
+          iloop = iloop ? iloop->next : NULL;
+        }
     }
-  }
-  if (iloop) {
+  if (iloop)
     werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "struct", sym->name);
-  }
-  return;
 }
 
 /*-----------------------------------------------------------------*/