From: borutr Date: Sat, 15 Nov 2008 14:13:47 +0000 (+0000) Subject: * src/pic16/glue.c, src/SDCC.y, src/SDCCast.c, src/SDCCglue.c, X-Git-Url: https://git.gag.com/?p=fw%2Fsdcc;a=commitdiff_plain;h=b880df34d5377ceee7a8e9465e73aee076fa8d49 * src/pic16/glue.c, src/SDCC.y, src/SDCCast.c, src/SDCCglue.c, 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 --- diff --git a/src/SDCC.y b/src/SDCC.y index d162cc84..fa165cef 100644 --- a/src/SDCC.y +++ b/src/SDCC.y @@ -926,6 +926,7 @@ struct_declarator if (!bitsize) bitsize = BITVAR_PAD; $$->bitVar = bitsize; + $$->bitUnnamed = 1; } | declarator ':' constant_expr { diff --git a/src/SDCCast.c b/src/SDCCast.c index 2e6d1f8e..896a9f6b 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -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; } diff --git a/src/SDCCglue.c b/src/SDCCglue.c index 37428849..060ef515 100644 --- a/src/SDCCglue.c +++ b/src/SDCCglue.c @@ -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 */ diff --git a/src/SDCCsymt.c b/src/SDCCsymt.c index e17d7c51..c7623d45 100644 --- a/src/SDCCsymt.c +++ b/src/SDCCsymt.c @@ -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: */ diff --git a/src/SDCCsymt.h b/src/SDCCsymt.h index ae82145e..8fa773c7 100644 --- a/src/SDCCsymt.h +++ b/src/SDCCsymt.h @@ -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 diff --git a/src/pic16/glue.c b/src/pic16/glue.c index b26f0478..374537b0 100644 --- a/src/pic16/glue.c +++ b/src/pic16/glue.c @@ -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; } /*-----------------------------------------------------------------*/