X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCval.c;h=11d80e4aa9411c5e6ccd810e1ed0a3eef346f8a5;hb=487fdab5c8b8e767c319a7c6556a891a51a69309;hp=fdb2c4be0a491999e3aedde062033bc193951a18;hpb=5fed4a0d90d3cdabd0b98a38dd966b327f87a3e7;p=fw%2Fsdcc diff --git a/src/SDCCval.c b/src/SDCCval.c index fdb2c4be..11d80e4a 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "newalloc.h" int cNestLevel; @@ -57,6 +58,7 @@ newiList (int type, void *ilist) nilist->type = type; nilist->lineno = mylineno; + nilist->filename = currFname; switch (type) { @@ -102,28 +104,28 @@ convertIListToConstList(initList *src, literalList **lList) { initList *iLoop; literalList *head, *last, *newL; - + head = last = NULL; - + if (!src || src->type != INIT_DEEP) { - return FALSE; + return FALSE; } - + iLoop = src->init.deep; - + while (iLoop) { - if (iLoop->type != INIT_NODE) - { - return FALSE; - } - - if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node)))) - { - return FALSE; - } - iLoop = iLoop->next; + if (iLoop->type != INIT_NODE) + { + return FALSE; + } + + if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node), RESULT_TYPE_NONE))) + { + return FALSE; + } + iLoop = iLoop->next; } // We've now established that the initializer list contains only literal values. @@ -131,37 +133,37 @@ convertIListToConstList(initList *src, literalList **lList) iLoop = src->init.deep; while (iLoop) { - double val = AST_LIT_VALUE(iLoop->init.node); - - if (last && last->literalValue == val) - { - last->count++; - } - else - { - newL = Safe_alloc(sizeof(literalList)); - newL->literalValue = val; - newL->count = 1; - newL->next = NULL; - - if (last) - { - last->next = newL; - } - else - { - head = newL; - } - last = newL; - } - iLoop = iLoop->next; + double val = AST_LIT_VALUE(iLoop->init.node); + + if (last && last->literalValue == val) + { + last->count++; + } + else + { + newL = Safe_alloc(sizeof(literalList)); + newL->literalValue = val; + newL->count = 1; + newL->next = NULL; + + if (last) + { + last->next = newL; + } + else + { + head = newL; + } + last = newL; + } + iLoop = iLoop->next; } - - if (!head) + + if (!head) { - return FALSE; + return FALSE; } - + *lList = head; return TRUE; } @@ -170,29 +172,29 @@ literalList * copyLiteralList(literalList *src) { literalList *head, *prev, *newL; - + head = prev = NULL; - + while (src) { - newL = Safe_alloc(sizeof(literalList)); - - newL->literalValue = src->literalValue; - newL->count = src->count; - newL->next = NULL; - - if (prev) - { - prev->next = newL; - } - else - { - head = newL; - } - prev = newL; - src = src->next; + newL = Safe_alloc(sizeof(literalList)); + + newL->literalValue = src->literalValue; + newL->count = src->count; + newL->next = NULL; + + if (prev) + { + prev->next = newL; + } + else + { + head = newL; + } + prev = newL; + src = src->next; } - + return head; } @@ -268,19 +270,28 @@ list2expr (initList * ilist) /*------------------------------------------------------------------*/ /* resolveIvalSym - resolve symbols in initial values */ /*------------------------------------------------------------------*/ -void -resolveIvalSym (initList * ilist) +void +resolveIvalSym (initList * ilist, sym_link * type) { + RESULT_TYPE resultType; + if (!ilist) return; if (ilist->type == INIT_NODE) - ilist->init.node = decorateType (resolveSymbols (ilist->init.node)); + { + if (IS_PTR (type)) + resultType = RESULT_TYPE_INT; + else + resultType = getResultTypeFromType (getSpec (type)); + ilist->init.node = decorateType (resolveSymbols (ilist->init.node), + resultType); + } if (ilist->type == INIT_DEEP) - resolveIvalSym (ilist->init.deep); + resolveIvalSym (ilist->init.deep, type); - resolveIvalSym (ilist->next); + resolveIvalSym (ilist->next, type); } /*-----------------------------------------------------------------*/ @@ -305,108 +316,56 @@ symbolVal (symbol * sym) if (*sym->rname) { - SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname); + SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname); } else { - SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name); + SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name); } return val; } -#if defined(REDUCE_LITERALS) /*--------------------------------------------------------------------*/ -/* cheapestVal - convert a val to the cheapest as possible value */ +/* cheapestVal - try to reduce 'signed int' to 'char' */ /*--------------------------------------------------------------------*/ -static value *cheapestVal (value *val) { - TYPE_DWORD sval=0; - TYPE_UDWORD uval=0; - - if (IS_FLOAT(val->type) || IS_CHAR(val->type)) +static value * +cheapestVal (value *val) +{ + /* only int can be reduced */ + if (!IS_INT(val->type)) return val; - if (SPEC_LONG(val->type)) { - if (SPEC_USIGN(val->type)) { - uval=SPEC_CVAL(val->type).v_ulong; - } else { - sval=SPEC_CVAL(val->type).v_long; - } - } else { - if (SPEC_USIGN(val->type)) { - uval=SPEC_CVAL(val->type).v_uint; - } else { - sval=SPEC_CVAL(val->type).v_int; - } - } - - if (SPEC_USIGN(val->type)) { - if (uval<=0xffff) { - SPEC_LONG(val->type)=0; - SPEC_CVAL(val->type).v_uint = (TYPE_UWORD)uval; - if (uval<=0xff) { - SPEC_NOUN(val->type)=V_CHAR; - } - } - } else { // not unsigned - if (sval<0) { - if (sval>=-32768) { - SPEC_LONG(val->type)=0; - SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval; - if (sval>=-128) { - SPEC_NOUN(val->type)=V_CHAR; - } - } - } else { // sval>=0 - if (sval<=32767) { - SPEC_LONG(val->type)=0; - SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval; - if (sval<=127) { - SPEC_NOUN(val->type)=V_CHAR; - } - } - } - } - return val; -} - -#else + /* long must not be changed */ + if (SPEC_LONG(val->type)) + return val; -static value *cheapestVal (value *val) -{ - /* - signed/unsigned must no be changed. - - long must not be changed. + /* unsigned must not be changed */ + if (SPEC_USIGN(val->type)) + return val; - the only possible reduction is from signed int to signed char, + /* the only possible reduction is from signed int to (un)signed char, because it's automatically promoted back to signed int. a reduction from unsigned int to unsigned char is a bug, because an _unsigned_ char is promoted to _signed_ int! */ - if (IS_INT(val->type) && - !SPEC_USIGN(val->type) && - !SPEC_LONG(val->type) && - SPEC_CVAL(val->type).v_int >= -128 && - SPEC_CVAL(val->type).v_int <= 127) - + if (SPEC_CVAL(val->type).v_int < -128 || + SPEC_CVAL(val->type).v_int > 255) { - SPEC_NOUN(val->type) = V_CHAR; + /* not in the range of (un)signed char */ + return val; } - /* this could be too aggressive: - 'unsigned char' promotes to 'signed int', so that we can - reduce it the other way */ - if (IS_INT(val->type) && - !SPEC_USIGN(val->type) && - !SPEC_LONG(val->type) && - SPEC_CVAL(val->type).v_int >= 128 && - SPEC_CVAL(val->type).v_int <= 255) + SPEC_NOUN(val->type) = V_CHAR; + + /* 'unsigned char' promotes to 'signed int', so that we can + reduce it the other way */ + if (SPEC_CVAL(val->type).v_int >= 0) { - SPEC_NOUN(val->type) = V_CHAR; SPEC_USIGN(val->type) = 1; } return (val); } -#endif /*-----------------------------------------------------------------*/ /* valueFromLit - creates a value from a literal */ @@ -449,6 +408,29 @@ constFloatVal (char *s) return val; } +/*-----------------------------------------------------------------*/ +/* constFixed16x16Val - converts a FIXED16X16 constant to value */ +/*-----------------------------------------------------------------*/ +value * +constFixed16x16Val (char *s) +{ + value *val = newValue (); + double sval; + + if (sscanf (s, "%lf", &sval) != 1) + { + werror (E_INVALID_FLOAT_CONST, s); + return constVal ("0"); + } + + val->type = val->etype = newLink (SPECIFIER); + SPEC_NOUN (val->type) = V_FLOAT; + SPEC_SCLS (val->type) = S_LITERAL; + SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble ( sval ); + + return val; +} + /*-----------------------------------------------------------------*/ /* constVal - converts an INTEGER constant into a cheapest value */ /*-----------------------------------------------------------------*/ @@ -456,13 +438,11 @@ value *constVal (char *s) { value *val; short hex = 0, octal = 0; - char scanFmt[10]; - int scI = 0; double dval; - val = newValue (); /* alloc space for value */ + val = newValue (); /* alloc space for value */ - val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */ + val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */ SPEC_SCLS (val->type) = S_LITERAL; // let's start with a signed char SPEC_NOUN (val->type) = V_CHAR; @@ -474,85 +454,79 @@ value *constVal (char *s) if (!hex && *s == '0' && *(s + 1)) octal = 1; - /* create the scan string */ - scanFmt[scI++] = '%'; - - scanFmt[scI++] = 'l'; - - if (octal) - scanFmt[scI++] = 'o'; - else if (hex) - scanFmt[scI++] = 'x'; - else - scanFmt[scI++] = 'f'; - - scanFmt[scI++] = '\0'; - - if (octal || hex) { + errno = 0; + if (hex || octal) { unsigned long sval; - sscanf (s, scanFmt, &sval); + sval = strtoul (s, NULL, 0); dval=sval; + if (errno) { + dval = 4294967295.0; + werror (W_INVALID_INT_CONST, s, dval); + } } else { - sscanf (s, scanFmt, &dval); + sscanf (s, "%lf", &dval); } /* Setup the flags first */ - /* set the _long flag if 'lL' is found */ - if (strchr (s, 'l') || strchr (s, 'L')) { - SPEC_NOUN (val->type) = V_INT; - SPEC_LONG (val->type) = 1; - } - /* set the unsigned flag if 'uU' is found */ if (strchr (s, 'u') || strchr (s, 'U')) { SPEC_USIGN (val->type) = 1; } - if (dval<0) { // "-28u" will still be signed and negative - if (dval<-128) { // check if we have to promote to int - SPEC_NOUN (val->type) = V_INT; - } - if (dval<-32768) { // check if we have to promote to long int - SPEC_LONG (val->type) = 1; - } - } else { // >=0 - if (dval>0xff || /* check if we have to promote to int */ - SPEC_USIGN (val->type)) { /* if it's unsigned, we can't use unsigned - char. After an integral promotion it will - be a signed int; this certainly isn't what - the programer wants */ - SPEC_NOUN (val->type) = V_INT; - } - else if (dval>0x7f && !SPEC_USIGN (val->type)) { // check if we have to promote to int -#if 0 - if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */ - dval<=0xff) { - SPEC_USIGN (val->type) = 1; - } else { - SPEC_NOUN (val->type) = V_INT; + /* set the b_long flag if 'lL' is found */ + if (strchr (s, 'l') || strchr (s, 'L')) { + SPEC_NOUN (val->type) = V_INT; + SPEC_LONG (val->type) = 1; + } else { + if (dval<0) { // "-28u" will still be signed and negative + if (dval<-128) { // check if we have to promote to int + SPEC_NOUN (val->type) = V_INT; } -#else - /* this is quite agressive: 'unsigned char' will be promoted to 'signed int', - so that the signedness of a char shouldn't matter */ - SPEC_USIGN (val->type) = 1; -#endif - } - if (dval>0xffff && SPEC_USIGN (val->type)) { // check if we have to promote to long - SPEC_LONG (val->type) = 1; - } - else if (dval>0x7fff && !SPEC_USIGN (val->type)) { // check if we have to promote to long int - if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */ - dval<=0xffff) { + if (dval<-32768) { // check if we have to promote to long int + SPEC_LONG (val->type) = 1; + } + } else { // >=0 + if (dval>0xff || /* check if we have to promote to int */ + SPEC_USIGN (val->type)) { /* if it's unsigned, we can't use unsigned + char. After an integral promotion it will + be a signed int; this certainly isn't what + the programer wants */ + SPEC_NOUN (val->type) = V_INT; + } + else { /* store char's always as unsigned; this helps other optimizations */ SPEC_USIGN (val->type) = 1; - } else { + } + if (dval>0xffff && SPEC_USIGN (val->type)) { // check if we have to promote to long SPEC_LONG (val->type) = 1; - if (dval>0x7fffffff) { + } + else if (dval>0x7fff && !SPEC_USIGN (val->type)) { // check if we have to promote to long int + if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */ + dval<=0xffff) { SPEC_USIGN (val->type) = 1; - } + } else { + SPEC_LONG (val->type) = 1; + if (dval>0x7fffffff) { + SPEC_USIGN (val->type) = 1; + } + } } } } + /* check for out of range */ + if (dval<-2147483648.0) { + dval = -2147483648.0; + werror (W_INVALID_INT_CONST, s, dval); + } + if (dval>2147483647.0 && !SPEC_USIGN (val->type)) { + dval = 2147483647.0; + werror (W_INVALID_INT_CONST, s, dval); + } + if (dval>4294967295.0) { + dval = 4294967295.0; + werror (W_INVALID_INT_CONST, s, dval); + } + if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) @@ -589,11 +563,11 @@ unsigned char hexEscape(char **src) char *s ; unsigned long value ; - (*src)++ ; /* Skip over the 'x' */ - s = *src ; /* Save for error detection */ - + (*src)++ ; /* Skip over the 'x' */ + s = *src ; /* Save for error detection */ + value = strtol (*src, src, 16); - + if (s == *src) { // no valid hex found werror(E_INVALID_HEX); @@ -631,9 +605,9 @@ unsigned char octalEscape (char **str) { return value; } -/*! +/*! /fn int copyStr (char *dest, char *src) - + Copies a source string to a dest buffer interpreting escape sequences and special characters @@ -643,7 +617,7 @@ unsigned char octalEscape (char **str) { */ -int +int copyStr (char *dest, char *src) { @@ -652,70 +626,70 @@ copyStr (char *dest, char *src) while (*src) { if (*src == '\"') - src++; + src++; else if (*src == '\\') - { - src++; - switch (*src) - { - case 'n': - *dest++ = '\n'; - break; - case 't': - *dest++ = '\t'; - break; - case 'v': - *dest++ = '\v'; - break; - case 'b': - *dest++ = '\b'; - break; - case 'r': - *dest++ = '\r'; - break; - case 'f': - *dest++ = '\f'; - break; - case 'a': - *dest++ = '\a'; - break; - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - *dest++ = octalEscape(&src); - src-- ; - break; - - case 'x': - *dest++ = hexEscape(&src) ; - src-- ; - break ; - - case '\\': - *dest++ = '\\'; - break; - case '\?': - *dest++ = '\?'; - break; - case '\'': - *dest++ = '\''; - break; - case '\"': - *dest++ = '\"'; - break; - default: - *dest++ = *src; - } - src++; - } + { + src++; + switch (*src) + { + case 'n': + *dest++ = '\n'; + break; + case 't': + *dest++ = '\t'; + break; + case 'v': + *dest++ = '\v'; + break; + case 'b': + *dest++ = '\b'; + break; + case 'r': + *dest++ = '\r'; + break; + case 'f': + *dest++ = '\f'; + break; + case 'a': + *dest++ = '\a'; + break; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + *dest++ = octalEscape(&src); + src-- ; + break; + + case 'x': + *dest++ = hexEscape(&src) ; + src-- ; + break ; + + case '\\': + *dest++ = '\\'; + break; + case '\?': + *dest++ = '\?'; + break; + case '\'': + *dest++ = '\''; + break; + case '\"': + *dest++ = '\"'; + break; + default: + *dest++ = *src; + } + src++; + } else - *dest++ = *src++; + *dest++ = *src++; } *dest++ = '\0'; @@ -731,7 +705,7 @@ strVal (char *s) { value *val; - val = newValue (); /* get a new one */ + val = newValue (); /* get a new one */ /* get a declarator */ val->type = newLink (DECLARATOR); @@ -848,68 +822,68 @@ charVal (char *s) SPEC_USIGN(val->type) = 1; SPEC_SCLS (val->type) = S_LITERAL; - s++; /* get rid of quotation */ + s++; /* get rid of quotation */ /* if \ then special processing */ if (*s == '\\') { - s++; /* go beyond the backslash */ + s++; /* go beyond the backslash */ switch (*s) - { - case 'n': - SPEC_CVAL (val->type).v_uint = '\n'; - break; - case 't': - SPEC_CVAL (val->type).v_uint = '\t'; - break; - case 'v': - SPEC_CVAL (val->type).v_uint = '\v'; - break; - case 'b': - SPEC_CVAL (val->type).v_uint = '\b'; - break; - case 'r': - SPEC_CVAL (val->type).v_uint = '\r'; - break; - case 'f': - SPEC_CVAL (val->type).v_uint = '\f'; - break; - case 'a': - SPEC_CVAL (val->type).v_uint = '\a'; - break; - case '\\': - SPEC_CVAL (val->type).v_uint = '\\'; - break; - case '\?': - SPEC_CVAL (val->type).v_uint = '\?'; - break; - case '\'': - SPEC_CVAL (val->type).v_uint = '\''; - break; - case '\"': - SPEC_CVAL (val->type).v_uint = '\"'; - break; - - case '0' : - case '1' : - case '2' : - case '3' : - case '4' : - case '5' : - case '6' : - case '7' : - SPEC_CVAL (val->type).v_uint = octalEscape(&s); - break; - - case 'x': - SPEC_CVAL (val->type).v_uint = hexEscape(&s) ; - break; - - default: - SPEC_CVAL (val->type).v_uint = (unsigned char)*s; - break; - } + { + case 'n': + SPEC_CVAL (val->type).v_uint = '\n'; + break; + case 't': + SPEC_CVAL (val->type).v_uint = '\t'; + break; + case 'v': + SPEC_CVAL (val->type).v_uint = '\v'; + break; + case 'b': + SPEC_CVAL (val->type).v_uint = '\b'; + break; + case 'r': + SPEC_CVAL (val->type).v_uint = '\r'; + break; + case 'f': + SPEC_CVAL (val->type).v_uint = '\f'; + break; + case 'a': + SPEC_CVAL (val->type).v_uint = '\a'; + break; + case '\\': + SPEC_CVAL (val->type).v_uint = '\\'; + break; + case '\?': + SPEC_CVAL (val->type).v_uint = '\?'; + break; + case '\'': + SPEC_CVAL (val->type).v_uint = '\''; + break; + case '\"': + SPEC_CVAL (val->type).v_uint = '\"'; + break; + + case '0' : + case '1' : + case '2' : + case '3' : + case '4' : + case '5' : + case '6' : + case '7' : + SPEC_CVAL (val->type).v_uint = octalEscape(&s); + break; + + case 'x': + SPEC_CVAL (val->type).v_uint = hexEscape(&s) ; + break; + + default: + SPEC_CVAL (val->type).v_uint = (unsigned char)*s; + break; + } } - else /* not a backslash */ + else /* not a backslash */ SPEC_CVAL (val->type).v_uint = (unsigned char)*s; return val; @@ -950,12 +924,15 @@ floatFromVal (value * val) if (SPEC_NOUN (val->etype) == V_FLOAT) return (double) SPEC_CVAL (val->etype).v_float; + if (SPEC_NOUN (val->etype) == V_FIXED16X16) + return (double) doubleFromFixed16x16( SPEC_CVAL (val->etype).v_fixed16x16 ); + if (SPEC_LONG (val->etype)) { if (SPEC_USIGN (val->etype)) - return (double) SPEC_CVAL (val->etype).v_ulong; + return (double) SPEC_CVAL (val->etype).v_ulong; else - return (double) SPEC_CVAL (val->etype).v_long; + return (double) SPEC_CVAL (val->etype).v_long; } if (SPEC_NOUN (val->etype) == V_INT) { @@ -982,7 +959,7 @@ floatFromVal (value * val) // we are lost ! werror (E_INTERNAL_ERROR, __FILE__, __LINE__, - "floatFromVal: unknown value"); + "floatFromVal: unknown value"); return 0; } @@ -995,31 +972,33 @@ valUnaryPM (value * val) /* depending on type */ if (SPEC_NOUN (val->etype) == V_FLOAT) SPEC_CVAL (val->etype).v_float = -1.0 * SPEC_CVAL (val->etype).v_float; + else if (SPEC_NOUN (val->etype) == V_FIXED16X16) + SPEC_CVAL (val->etype).v_fixed16x16 = -SPEC_CVAL (val->etype).v_fixed16x16; else { if (SPEC_LONG (val->etype)) - { - if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_ulong = 0-SPEC_CVAL (val->etype).v_ulong; - else - SPEC_CVAL (val->etype).v_long = -SPEC_CVAL (val->etype).v_long; - } + { + if (SPEC_USIGN (val->etype)) + SPEC_CVAL (val->etype).v_ulong = 0-SPEC_CVAL (val->etype).v_ulong; + else + SPEC_CVAL (val->etype).v_long = -SPEC_CVAL (val->etype).v_long; + } else - { - if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint; - else - SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int; - } - } - // -(unsigned 3) now really is signed - SPEC_USIGN(val->etype)=0; - // -(unsigned char)135 now really is an int - if (SPEC_NOUN(val->etype) == V_CHAR) { - if (SPEC_CVAL(val->etype).v_int < -128) { - SPEC_NOUN(val->etype) = V_INT; + { + if (SPEC_USIGN (val->etype)) + SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint; + else + SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int; + + if (SPEC_NOUN(val->etype) == V_CHAR) + { + /* promote to 'signed int', cheapestVal() might reduce it again */ + SPEC_USIGN(val->etype) = 0; + SPEC_NOUN(val->etype) = V_INT; + } + return cheapestVal (val); + } } - } return val; } @@ -1033,19 +1012,25 @@ valComplement (value * val) if (SPEC_LONG (val->etype)) { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_ulong = ~SPEC_CVAL (val->etype).v_ulong; + SPEC_CVAL (val->etype).v_ulong = ~SPEC_CVAL (val->etype).v_ulong; else - SPEC_CVAL (val->etype).v_long = ~SPEC_CVAL (val->etype).v_long; + SPEC_CVAL (val->etype).v_long = ~SPEC_CVAL (val->etype).v_long; } else { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint; + SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint; else - SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int; + SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int; + + if (SPEC_NOUN(val->etype) == V_CHAR) + { + /* promote to 'signed int', cheapestVal() might reduce it again */ + SPEC_USIGN(val->etype) = 0; + SPEC_NOUN(val->etype) = V_INT; + } + return cheapestVal (val); } - // ~(unsigned 3) now really is signed - SPEC_USIGN(val->etype)=0; return val; } @@ -1059,17 +1044,23 @@ valNot (value * val) if (SPEC_LONG (val->etype)) { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_ulong = !SPEC_CVAL (val->etype).v_ulong; + SPEC_CVAL (val->etype).v_ulong = !SPEC_CVAL (val->etype).v_ulong; else - SPEC_CVAL (val->etype).v_long = !SPEC_CVAL (val->etype).v_long; + SPEC_CVAL (val->etype).v_long = !SPEC_CVAL (val->etype).v_long; } else { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint; + SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint; else - SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int; + SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int; + } + /* ANSI: result type is int, value is 0 or 1 */ + /* sdcc will hold this in an 'unsigned char' */ + SPEC_USIGN(val->etype) = 1; + SPEC_LONG (val->etype) = 0; + SPEC_NOUN(val->etype) = V_CHAR; return val; } @@ -1083,51 +1074,41 @@ valMult (value * lval, value * rval) /* create a new value */ val = newValue (); - val->type = val->etype = newLink (SPECIFIER); - SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) || - IS_FLOAT (rval->etype) ? V_FLOAT : V_INT); - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); - /* both signed char and unsigned char are promoted to signed int */ - if (IS_CHAR (lval->etype)) - { - SPEC_USIGN (lval->etype) = 0; - SPEC_NOUN (lval->etype) = V_INT; - } - if (IS_CHAR (rval->etype)) - { - SPEC_USIGN (rval->etype) = 0; - SPEC_NOUN (rval->etype) = V_INT; - } - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); + val->type = val->etype = computeType (lval->etype, + rval->etype, + RESULT_TYPE_INT, + '*'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ + if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval); + else + if (IS_FIXED16X16 (val->type)) + SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble(floatFromVal (lval) * floatFromVal (rval)); /* signed and unsigned mul are the same, as long as the precision of the result isn't bigger than the precision of the operands. */ else if (SPEC_LONG (val->type)) SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) * - (TYPE_UDWORD) floatFromVal (rval); - else if (SPEC_USIGN (val->type)) /* unsigned */ + (TYPE_UDWORD) floatFromVal (rval); + else if (SPEC_USIGN (val->type)) /* unsigned int */ { TYPE_UDWORD ul = (TYPE_UWORD) floatFromVal (lval) * (TYPE_UWORD) floatFromVal (rval); SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) ul; - if (!options.lessPedantic && - ul != (TYPE_UWORD) ul) + if (ul != (TYPE_UWORD) ul) werror (W_INT_OVL); } - else /* int */ + else /* signed int */ { TYPE_DWORD l = (TYPE_WORD) floatFromVal (lval) * (TYPE_WORD) floatFromVal (rval); SPEC_CVAL (val->type).v_int = (TYPE_WORD) l; - if (!options.lessPedantic && - l != (TYPE_WORD) l) + if (l != (TYPE_WORD) l) werror (W_INT_OVL); } - return cheapestVal(val); + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1146,49 +1127,36 @@ valDiv (value * lval, value * rval) /* create a new value */ val = newValue (); - val->type = val->etype = newLink(SPECIFIER); - SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) || - IS_FLOAT (rval->etype) ? V_FLOAT : V_INT); - SPEC_SCLS (val->etype) = S_LITERAL; - /* both signed char and unsigned char are promoted to signed int */ - if (IS_CHAR (lval->etype)) - { - SPEC_USIGN (lval->etype) = 0; - SPEC_NOUN (lval->etype) = V_INT; - } - if (IS_CHAR (rval->etype)) - { - SPEC_USIGN (rval->etype) = 0; - SPEC_NOUN (rval->etype) = V_INT; - } - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); + val->type = val->etype = computeType (lval->etype, + rval->etype, + RESULT_TYPE_INT, + '/'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval); else + if (IS_FIXED16X16 (val->type)) + SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) / floatFromVal (rval) ); + else if (SPEC_LONG (val->type)) { - if (SPEC_LONG (val->type)) - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) / - (TYPE_UDWORD) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) / - (TYPE_DWORD) floatFromVal (rval); - } + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) / + (TYPE_UDWORD) floatFromVal (rval); else - { - if (SPEC_USIGN (val->type)) { - SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) / - (TYPE_UWORD) floatFromVal (rval); - } else { - SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) / - (TYPE_WORD) floatFromVal (rval); - } - } + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) / + (TYPE_DWORD) floatFromVal (rval); } - return cheapestVal(val); + else + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) / + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) / + (TYPE_WORD) floatFromVal (rval); + } + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1200,34 +1168,32 @@ valMod (value * lval, value * rval) value *val; /* create a new value */ - val = newValue (); - val->type = val->etype = newLink (SPECIFIER); - SPEC_NOUN (val->type) = V_INT; /* type is int */ - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); + val = newValue(); + val->type = val->etype = computeType (lval->etype, + rval->etype, + RESULT_TYPE_INT, + '%'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) % - (TYPE_UDWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) % + (TYPE_UDWORD) floatFromVal (rval); else - SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) % - (TYPE_DWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) % + (TYPE_DWORD) floatFromVal (rval); } else { - if (SPEC_USIGN (val->type)) { - SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) % - (TYPE_UWORD) floatFromVal (rval); - } else { - SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) % - (TYPE_WORD) floatFromVal (rval); - } + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) % + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) % + (TYPE_WORD) floatFromVal (rval); } - - return cheapestVal(val); + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1239,36 +1205,37 @@ valPlus (value * lval, value * rval) value *val; /* create a new value */ - val = newValue (); - val->type = val->etype = newLink (SPECIFIER); - SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) || - IS_FLOAT (rval->etype) ? V_FLOAT : V_INT); - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); - /* both signed char and unsigned char are promoted to signed int */ - if (IS_CHAR (lval->etype)) - { - SPEC_USIGN (lval->etype) = 0; - SPEC_NOUN (lval->etype) = V_INT; - } - if (IS_CHAR (rval->etype)) - { - SPEC_USIGN (rval->etype) = 0; - SPEC_NOUN (rval->etype) = V_INT; - } - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); + val = newValue(); + val->type = val->etype = computeType (lval->etype, + rval->etype, + RESULT_TYPE_INT, + '+'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ + if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval); else + if (IS_FIXED16X16 (val->type)) + SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) + floatFromVal (rval) ); + else if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) + - (TYPE_UDWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) + + (TYPE_UDWORD) floatFromVal (rval); else - SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) + - (TYPE_DWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) + + (TYPE_DWORD) floatFromVal (rval); } - return cheapestVal(val); + else + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) + + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) + + (TYPE_WORD) floatFromVal (rval); + } + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1280,36 +1247,37 @@ valMinus (value * lval, value * rval) value *val; /* create a new value */ - val = newValue (); - val->type = val->etype = newLink (SPECIFIER); - SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) || - IS_FLOAT (rval->etype) ? V_FLOAT : V_INT); - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); - /* both signed char and unsigned char are promoted to signed int */ - if (IS_CHAR (lval->etype)) - { - SPEC_USIGN (lval->etype) = 0; - SPEC_NOUN (lval->etype) = V_INT; - } - if (IS_CHAR (rval->etype)) - { - SPEC_USIGN (rval->etype) = 0; - SPEC_NOUN (rval->etype) = V_INT; - } - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); + val = newValue(); + val->type = val->etype = computeType (lval->etype, + rval->etype, + RESULT_TYPE_INT, + '-'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ + if (IS_FLOAT (val->type)) - SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval); + SPEC_CVAL (val->type).v_float = floatFromVal (lval) - floatFromVal (rval); else + if (IS_FIXED16X16 (val->type)) + SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) - floatFromVal (rval) ); + else if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) - - (TYPE_UDWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) - + (TYPE_UDWORD) floatFromVal (rval); else - SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) - - (TYPE_DWORD) floatFromVal (rval); + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) - + (TYPE_DWORD) floatFromVal (rval); } - return cheapestVal(val); + else + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) - + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) - + (TYPE_WORD) floatFromVal (rval); + } + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1321,28 +1289,53 @@ valShift (value * lval, value * rval, int lr) value *val; /* create a new value */ - val = newValue (); - val->type = val->etype = newIntLink (); - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_NOUN (val->etype) = V_INT; - /* 'unsigned char' promotes to 'signed int' */ - if (!IS_CHAR (lval->etype)) - SPEC_USIGN (val->type) = SPEC_USIGN (lval->etype); - SPEC_LONG (val->type) = SPEC_LONG (lval->etype); - - if (SPEC_USIGN (val->type)) + val = newValue(); + val->type = val->etype = computeType (lval->etype, + NULL, + RESULT_TYPE_INT, + 'S'); + SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */ + + if (getSize (val->type) * 8 <= (TYPE_UDWORD) floatFromVal (rval) && + /* left shift */ + (lr || + /* right shift and unsigned */ + (!lr && SPEC_USIGN (rval->type)))) { - SPEC_CVAL (val->type).v_ulong = lr ? - (TYPE_UDWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ - (TYPE_UDWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + werror (W_SHIFT_CHANGED, (lr ? "left" : "right")); + } + + if (SPEC_LONG (val->type)) + { + if (SPEC_USIGN (val->type)) + { + SPEC_CVAL (val->type).v_ulong = lr ? + (TYPE_UDWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ + (TYPE_UDWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + } + else + { + SPEC_CVAL (val->type).v_long = lr ? + (TYPE_DWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ + (TYPE_DWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + } } else { - SPEC_CVAL (val->type).v_long = lr ? - (TYPE_DWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ - (TYPE_DWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + if (SPEC_USIGN (val->type)) + { + SPEC_CVAL (val->type).v_uint = lr ? + (TYPE_UWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ + (TYPE_UWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + } + else + { + SPEC_CVAL (val->type).v_int = lr ? + (TYPE_WORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \ + (TYPE_WORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval); + } } - return cheapestVal(val); + return cheapestVal (val); } /*------------------------------------------------------------------*/ @@ -1357,9 +1350,9 @@ valCompare (value * lval, value * rval, int ctype) val = newValue (); val->type = val->etype = newCharLink (); val->type->class = SPECIFIER; - SPEC_NOUN (val->type) = V_CHAR; /* type is char */ + SPEC_NOUN (val->type) = V_CHAR; /* type is char */ SPEC_USIGN (val->type) = 1; - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ + SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ switch (ctype) { @@ -1381,55 +1374,69 @@ valCompare (value * lval, value * rval, int ctype) case EQ_OP: if (SPEC_NOUN(lval->type) == V_FLOAT || - SPEC_NOUN(rval->type) == V_FLOAT) - { - SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval); - } + SPEC_NOUN(rval->type) == V_FLOAT) + { + SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval); + } else - { - /* integrals: ignore signedness */ - TYPE_UDWORD l, r; - - l = (TYPE_UDWORD) floatFromVal (lval); - if (SPEC_NOUN(lval->type) == V_CHAR) - l &= 0xffff; /* promote to int */ - else if (!SPEC_LONG (lval->type)) - l &= 0xffff; - - r = (TYPE_UDWORD) floatFromVal (rval); - if (SPEC_NOUN(rval->type) == V_CHAR) - r &= 0xffff; /* promote to int */ - else if (!SPEC_LONG (rval->type)) - r &= 0xffff; - - SPEC_CVAL (val->type).v_int = l == r; - } + if (SPEC_NOUN(lval->type) == V_FIXED16X16 || + SPEC_NOUN(rval->type) == V_FIXED16X16) + { + SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval); + } + else + { + /* integrals: ignore signedness */ + TYPE_UDWORD l, r; + + l = (TYPE_UDWORD) floatFromVal (lval); + r = (TYPE_UDWORD) floatFromVal (rval); + /* In order to correctly compare 'signed int' and 'unsigned int' it's + neccessary to strip them to 16 bit. + Literals are reduced to their cheapest type, therefore left and + right might have different types. It's neccessary to find a + common type: int (used for char too) or long */ + if (!IS_LONG (lval->etype) && + !IS_LONG (rval->etype)) + { + r = (TYPE_UWORD) r; + l = (TYPE_UWORD) l; + } + SPEC_CVAL (val->type).v_int = l == r; + } break; case NE_OP: if (SPEC_NOUN(lval->type) == V_FLOAT || - SPEC_NOUN(rval->type) == V_FLOAT) - { - SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval); - } + SPEC_NOUN(rval->type) == V_FLOAT) + { + SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval); + } + else + if (SPEC_NOUN(lval->type) == V_FIXED16X16 || + SPEC_NOUN(rval->type) == V_FIXED16X16) + { + SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval); + } else - { - /* integrals: ignore signedness */ - TYPE_UDWORD l, r; - - l = (TYPE_UDWORD) floatFromVal (lval); - if (SPEC_NOUN(lval->type) == V_CHAR) - l &= 0xffff; /* promote to int */ - else if (!SPEC_LONG (lval->type)) - l &= 0xffff; - - r = (TYPE_UDWORD) floatFromVal (rval); - if (SPEC_NOUN(rval->type) == V_CHAR) - r &= 0xffff; /* promote to int */ - else if (!SPEC_LONG (rval->type)) - r &= 0xffff; - - SPEC_CVAL (val->type).v_int = l != r; - } + { + /* integrals: ignore signedness */ + TYPE_UDWORD l, r; + + l = (TYPE_UDWORD) floatFromVal (lval); + r = (TYPE_UDWORD) floatFromVal (rval); + /* In order to correctly compare 'signed int' and 'unsigned int' it's + neccessary to strip them to 16 bit. + Literals are reduced to their cheapest type, therefore left and + right might have different types. It's neccessary to find a + common type: int (used for char too) or long */ + if (!IS_LONG (lval->etype) && + !IS_LONG (rval->etype)) + { + r = (TYPE_UWORD) r; + l = (TYPE_UWORD) l; + } + SPEC_CVAL (val->type).v_int = l != r; + } break; } @@ -1447,73 +1454,73 @@ valBitwise (value * lval, value * rval, int op) /* create a new value */ val = newValue (); - val->type = copyLinkChain (getSize(rval->type) > getSize(lval->type) ? - rval->type : lval->type); + val->type = computeType (lval->etype, rval->etype, RESULT_TYPE_CHAR, op); val->etype = getSpec (val->type); + SPEC_SCLS (val->etype) = S_LITERAL; switch (op) { case '&': if (SPEC_LONG (val->type)) - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) & - (unsigned long) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) & - (long) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) & + (TYPE_UDWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) & + (TYPE_DWORD) floatFromVal (rval); + } else - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) & - (unsigned) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) & (int) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) & + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) & (TYPE_WORD) floatFromVal (rval); + } break; case '|': if (SPEC_LONG (val->type)) - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) | - (unsigned long) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) | - (long) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) | + (TYPE_UDWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) | + (TYPE_DWORD) floatFromVal (rval); + } else - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) | - (unsigned) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_int = - (int) floatFromVal (lval) | (int) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) | + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = + (TYPE_WORD) floatFromVal (lval) | (TYPE_WORD) floatFromVal (rval); + } break; case '^': if (SPEC_LONG (val->type)) - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) ^ - (unsigned long) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) ^ - (long) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) ^ + (TYPE_UDWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) ^ + (TYPE_DWORD) floatFromVal (rval); + } else - { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) ^ - (unsigned) floatFromVal (rval); - else - SPEC_CVAL (val->type).v_int = - (int) floatFromVal (lval) ^ (int) floatFromVal (rval); - } + { + if (SPEC_USIGN (val->type)) + SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) ^ + (TYPE_UWORD) floatFromVal (rval); + else + SPEC_CVAL (val->type).v_int = + (TYPE_WORD) floatFromVal (lval) ^ (TYPE_WORD) floatFromVal (rval); + } break; } @@ -1532,8 +1539,8 @@ valLogicAndOr (value * lval, value * rval, int op) val = newValue (); val->type = val->etype = newCharLink (); val->type->class = SPECIFIER; - SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ - SPEC_USIGN (val->type) = 0; + SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ + SPEC_USIGN (val->type) = 1; switch (op) { @@ -1563,7 +1570,13 @@ valCastLiteral (sym_link * dtype, double fval) return NULL; val = newValue (); - val->etype = getSpec (val->type = copyLinkChain (dtype)); + if (dtype) + val->etype = getSpec (val->type = copyLinkChain (dtype)); + else + { + val->etype = val->type = newLink (SPECIFIER); + SPEC_NOUN (val->etype) = V_VOID; + } SPEC_SCLS (val->etype) = S_LITERAL; /* if it is not a specifier then we can assume that */ @@ -1575,22 +1588,30 @@ valCastLiteral (sym_link * dtype, double fval) if (SPEC_NOUN (val->etype) == V_FLOAT) SPEC_CVAL (val->etype).v_float = fval; + else if (SPEC_NOUN (val->etype) == V_FIXED16X16) + SPEC_CVAL (val->etype).v_fixed16x16 = fixed16x16FromDouble( fval ); + else if (SPEC_NOUN (val->etype) == V_BIT || + SPEC_NOUN (val->etype) == V_SBIT) + SPEC_CVAL (val->etype).v_uint = l ? 1 : 0; + else if (SPEC_NOUN (val->etype) == V_BITFIELD) + SPEC_CVAL (val->etype).v_uint = l & + (0xffffu >> (16 - SPEC_BLEN (val->etype))); else if (SPEC_NOUN (val->etype) == V_CHAR) { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint= (TYPE_UBYTE) l; + SPEC_CVAL (val->etype).v_uint= (TYPE_UBYTE) l; else - SPEC_CVAL (val->etype).v_int = (TYPE_BYTE) l; + SPEC_CVAL (val->etype).v_int = (TYPE_BYTE) l; } else { if (SPEC_LONG (val->etype)) { - if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_ulong = (TYPE_UDWORD) l; - else - SPEC_CVAL (val->etype).v_long = (TYPE_DWORD) l; + if (SPEC_USIGN (val->etype)) + SPEC_CVAL (val->etype).v_ulong = (TYPE_UDWORD) l; + else + SPEC_CVAL (val->etype).v_long = (TYPE_DWORD) l; } else { - if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint = (TYPE_UWORD)l; - else - SPEC_CVAL (val->etype).v_int = (TYPE_WORD)l; + if (SPEC_USIGN (val->etype)) + SPEC_CVAL (val->etype).v_uint = (TYPE_UWORD)l; + else + SPEC_CVAL (val->etype).v_int = (TYPE_WORD)l; } } return val; @@ -1617,16 +1638,16 @@ getNelements (sym_link * type, initList * ilist) ast *iast = ilist->init.node; value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL); if (!v) - { - werror (E_CONST_EXPECTED); - return 0; - } + { + werror (E_CONST_EXPECTED); + return 0; + } if (IS_ARRAY (v->type) && IS_CHAR (v->etype)) - // yep, it's a string - { - return DCL_ELEM (v->type); - } + // yep, it's a string + { + return DCL_ELEM (v->type); + } } i = 0; @@ -1652,16 +1673,16 @@ valForArray (ast * arrExpr) if (IS_AST_OP (arrExpr->left)) { if (arrExpr->left->opval.op == '[') - lval = valForArray (arrExpr->left); + lval = valForArray (arrExpr->left); else if (arrExpr->left->opval.op == '.') - lval = valForStructElem (arrExpr->left->left, - arrExpr->left->right); + lval = valForStructElem (arrExpr->left->left, + arrExpr->left->right); else if (arrExpr->left->opval.op == PTR_OP && - IS_ADDRESS_OF_OP (arrExpr->left->left)) - lval = valForStructElem (arrExpr->left->left->left, - arrExpr->left->right); + IS_ADDRESS_OF_OP (arrExpr->left->left)) + lval = valForStructElem (arrExpr->left->left->left, + arrExpr->left->right); else - return NULL; + return NULL; } else if (!IS_AST_SYM_VALUE (arrExpr->left)) @@ -1673,15 +1694,15 @@ valForArray (ast * arrExpr) val = newValue (); if (!lval) { - SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname); + SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname); } else { - SNPRINTF (buffer, sizeof(buffer), "%s", lval->name); + SNPRINTF (buffer, sizeof(buffer), "%s", lval->name); } SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer, - (int) AST_LIT_VALUE (arrExpr->right) * size); + (int) AST_LIT_VALUE (arrExpr->right) * size); val->type = newLink (DECLARATOR); if (SPEC_SCLS (arrExpr->left->etype) == S_CODE) @@ -1696,7 +1717,7 @@ valForArray (ast * arrExpr) DCL_TYPE (val->type) = EEPPOINTER; else DCL_TYPE (val->type) = POINTER; - val->type->next = arrExpr->left->ftype; + val->type->next = arrExpr->left->ftype->next; val->etype = getSpec (val->type); return val; } @@ -1715,15 +1736,15 @@ valForStructElem (ast * structT, ast * elemT) if (IS_AST_OP (structT)) { if (structT->opval.op == '[') - lval = valForArray (structT); + lval = valForArray (structT); else if (structT->opval.op == '.') - lval = valForStructElem (structT->left, structT->right); + lval = valForStructElem (structT->left, structT->right); else if (structT->opval.op == PTR_OP && - IS_ADDRESS_OF_OP (structT->left)) - lval = valForStructElem (structT->left->left, - structT->right); + IS_ADDRESS_OF_OP (structT->left)) + lval = valForStructElem (structT->left->left, + structT->right); else - return NULL; + return NULL; } if (!IS_AST_SYM_VALUE (elemT)) @@ -1733,7 +1754,7 @@ valForStructElem (ast * structT, ast * elemT) return NULL; if ((sym = getStructElement (SPEC_STRUCT (structT->etype), - AST_SYMBOL (elemT))) == NULL) + AST_SYMBOL (elemT))) == NULL) { return NULL; } @@ -1741,15 +1762,15 @@ valForStructElem (ast * structT, ast * elemT) val = newValue (); if (!lval) { - SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname); + SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname); } else { - SNPRINTF (buffer, sizeof(buffer), "%s", lval->name); + SNPRINTF (buffer, sizeof(buffer), "%s", lval->name); } SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer, - (int) sym->offset); + (int) sym->offset); val->type = newLink (DECLARATOR); if (SPEC_SCLS (structT->etype) == S_CODE) @@ -1786,8 +1807,8 @@ valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op) val = newValue (); SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)", - AST_SYMBOL (aexpr)->rname, op, - getSize (type->next) * (int) AST_LIT_VALUE (cnst)); + AST_SYMBOL (aexpr)->rname, op, + getSize (type->next) * (int) AST_LIT_VALUE (cnst)); val->type = type; val->etype = getSpec (val->type); @@ -1809,7 +1830,7 @@ valForCastArr (ast * aexpr, sym_link * type) val = newValue (); SNPRINTF (val->name, sizeof(val->name), "(%s)", - AST_SYMBOL (aexpr)->rname); + AST_SYMBOL (aexpr)->rname); val->type = type; val->etype = getSpec (val->type);