X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCval.c;h=b5685aae5bc8ac7247b5b22836d88751312183db;hb=bdc517ae4a26d2047e7379cbd7d94b2ab665c2df;hp=bfab16715b18024c6326bd5f26240abeab3d9fbc;hpb=29779804200986ce903b5086441b49265a122dc5;p=fw%2Fsdcc diff --git a/src/SDCCval.c b/src/SDCCval.c index bfab1671..b5685aae 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -39,7 +39,7 @@ newValue () { value *val; - val = Safe_calloc (1, sizeof (value)); + val = Safe_alloc (sizeof (value)); return val; } @@ -53,7 +53,7 @@ newiList (int type, void *ilist) initList *nilist; - nilist = Safe_calloc (1, sizeof (initList)); + nilist = Safe_alloc (sizeof (initList)); nilist->type = type; nilist->lineno = yylineno; @@ -97,6 +97,107 @@ revinit (initList * val) return prev; } +bool +convertIListToConstList(initList *src, literalList **lList) +{ + initList *iLoop; + literalList *head, *last, *newL; + + head = last = NULL; + + if (!src || src->type != INIT_DEEP) + { + 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; + } + + // We've now established that the initializer list contains only literal values. + + 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; + } + + if (!head) + { + return FALSE; + } + + *lList = head; + return TRUE; +} + +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; + } + + return head; +} + + + /*------------------------------------------------------------------*/ /* copyIlist - copy initializer list */ /*------------------------------------------------------------------*/ @@ -211,11 +312,67 @@ symbolVal (symbol * sym) return val; } +/*--------------------------------------------------------------------*/ +/* cheapestVal - convert a val to the cheapest as possible value */ +/*--------------------------------------------------------------------*/ +value *cheapestVal (value *val) { + long sval=0; + unsigned long uval=0; + + if (IS_FLOAT(val->type) || IS_CHAR(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 = 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 = sval & 0xffff; + } + if (sval>=-128) { + SPEC_NOUN(val->type)=V_CHAR; + SPEC_CVAL(val->type).v_int &= 0xff; + } + } else { // sval>=0 + SPEC_USIGN(val->type)=1; + if (sval<=65535) { + SPEC_LONG(val->type)=0; + SPEC_CVAL(val->type).v_int = sval; + } + if (sval<=255) { + SPEC_NOUN(val->type)=V_CHAR; + } + } + } + return val; +} + /*-----------------------------------------------------------------*/ /* valueFromLit - creates a value from a literal */ /*-----------------------------------------------------------------*/ value * -valueFromLit (float lit) +valueFromLit (double lit) { char buffer[50]; @@ -236,9 +393,9 @@ value * constFloatVal (char *s) { value *val = newValue (); - float sval; + double sval; - if (sscanf (s, "%f", &sval) != 1) + if (sscanf (s, "%lf", &sval) != 1) { werror (E_INVALID_FLOAT_CONST, s); return constVal ("0"); @@ -254,31 +411,24 @@ constFloatVal (char *s) } /*-----------------------------------------------------------------*/ -/* constVal - converts a INTEGER constant into a value */ +/* constVal - converts an INTEGER constant into a cheapest value */ /*-----------------------------------------------------------------*/ -value * -constVal (char *s) +value *constVal (char *s) { value *val; short hex = 0, octal = 0; char scanFmt[10]; int scI = 0; - unsigned long sval; + long sval; val = newValue (); /* alloc space for value */ val->type = val->etype = newLink (); /* create the spcifier */ val->type->class = SPECIFIER; - SPEC_NOUN (val->type) = V_INT; SPEC_SCLS (val->type) = S_LITERAL; - - /* set the _unsigned flag if 'uU' found */ - if (strchr (s, 'u') || strchr (s, 'U')) - SPEC_USIGN (val->type) = 1; - - /* set the _long flag if 'lL' is found */ - if (strchr (s, 'l') || strchr (s, 'L')) - SPEC_LONG (val->type) = 1; + // let's start with an unsigned char + SPEC_NOUN (val->type) = V_CHAR; + SPEC_USIGN (val->type) = 1; hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0); @@ -289,58 +439,139 @@ constVal (char *s) /* create the scan string */ scanFmt[scI++] = '%'; + scanFmt[scI++] = 'l'; + if (octal) scanFmt[scI++] = 'o'; else if (hex) scanFmt[scI++] = 'x'; - else if (SPEC_USIGN (val->type)) - scanFmt[scI++] = 'u'; else scanFmt[scI++] = 'd'; scanFmt[scI++] = '\0'; - /* if hex or octal then set the unsigned flag */ - if (hex || octal) - { - SPEC_USIGN (val->type) = 1; - sscanf (s, scanFmt, &sval); - } - else - sval = atol (s); + sscanf (s, scanFmt, &sval); + /* 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; + } - if (SPEC_LONG (val->type) || sval > 32768) + if (sval<0) { // "-28u" will still be signed and negative + SPEC_USIGN (val->type) = 0; + if (sval<-128) { // check if we have to promote to int + SPEC_NOUN (val->type) = V_INT; + } + if (sval<-32768) { // check if we have to promote to long int + SPEC_LONG (val->type) = 1; + } + } else { // >=0 + if (sval>0xff) { // check if we have to promote to int + SPEC_NOUN (val->type) = V_INT; + } + if (sval>0xffff) { // check if we have to promote to long int + SPEC_LONG (val->type) = 1; + } + } + + if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = sval; + { + SPEC_CVAL (val->type).v_ulong = sval; + } else - SPEC_CVAL (val->type).v_long = sval; - SPEC_LONG (val->type) = 1; + { + SPEC_CVAL (val->type).v_long = sval; + } } else { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_uint = sval; + { + SPEC_CVAL (val->type).v_uint = sval; + } else - SPEC_CVAL (val->type).v_int = sval; + { + SPEC_CVAL (val->type).v_int = sval; + } } - // check the size and make it a short if required - if (sval < 256) - SPEC_SHORT (val->etype) = 1; - return val; +} +/*! /fn char hexEscape(char **src) + + /param src Pointer to 'x' from start of hex character value +*/ + +unsigned char hexEscape(char **src) +{ + char *s ; + unsigned long value ; + + (*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); + } else { + if (value > 255) { + werror(W_ESC_SEQ_OOR_FOR_CHAR); + } + } + return (char) value; } /*------------------------------------------------------------------*/ -/* copyStr - copies src to dest ignoring leading & trailing \"s */ +/* octalEscape - process an octal constant of max three digits */ +/* return the octal value, throw a warning for illegal octal */ +/* adjust src to point at the last proccesed char */ /*------------------------------------------------------------------*/ -void + +unsigned char octalEscape (char **str) { + int digits; + unsigned value=0; + + for (digits=0; digits<3; digits++) { + if (**str>='0' && **str<='7') { + value = value*8 + (**str-'0'); + (*str)++; + } else { + break; + } + } + if (digits) { + if (value > 255 /* || (**str>='0' && **str<='7') */ ) { + werror (W_ESC_SEQ_OOR_FOR_CHAR); + } + } + return value; +} + +/*! + /fn int copyStr (char *dest, char *src) + + Copies a source string to a dest buffer interpreting escape sequences + and special characters + + /param dest Buffer to receive the resultant string + /param src Buffer containing the source string with escape sequecnes + /return Number of characters in output string + +*/ + +int copyStr (char *dest, char *src) + { - unsigned int x; + char *OriginalDest = dest ; + while (*src) { if (*src == '\"') @@ -371,22 +602,24 @@ copyStr (char *dest, char *src) case 'a': *dest++ = '\a'; break; + case '0': - /* embedded octal or hex constant */ - if (*(src + 1) == 'x' || - *(src + 1) == 'X') - { - x = strtol (src, &src, 16); - *dest++ = x; - } - else - { - /* must be octal */ - x = strtol (src, &src, 8); - *dest++ = x; - } + 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; @@ -408,7 +641,9 @@ copyStr (char *dest, char *src) *dest++ = *src++; } - *dest = '\0'; + *dest++ = '\0'; + + return dest - OriginalDest ; } /*------------------------------------------------------------------*/ @@ -424,14 +659,14 @@ strVal (char *s) /* get a declarator */ val->type = newLink (); DCL_TYPE (val->type) = ARRAY; - DCL_ELEM (val->type) = strlen (s) - 1; val->type->next = val->etype = newLink (); val->etype->class = SPECIFIER; SPEC_NOUN (val->etype) = V_CHAR; SPEC_SCLS (val->etype) = S_LITERAL; - SPEC_CVAL (val->etype).v_char = Safe_calloc (1, strlen (s) + 1); - copyStr (SPEC_CVAL (val->etype).v_char, s); + SPEC_CVAL (val->etype).v_char = Safe_alloc (strlen (s) + 1); + DCL_ELEM (val->type) = copyStr (SPEC_CVAL (val->etype).v_char, s); + return val; } @@ -506,7 +741,7 @@ copyValueChain (value * src) } /*------------------------------------------------------------------*/ -/* copyValue - copies contents of a vlue to a fresh one */ +/* copyValue - copies contents of a value to a fresh one */ /*------------------------------------------------------------------*/ value * copyValue (value * src) @@ -529,12 +764,14 @@ value * charVal (char *s) { value *val; +// unsigned uValue ; val = newValue (); val->type = val->etype = newLink (); val->type->class = SPECIFIER; SPEC_NOUN (val->type) = V_CHAR; + SPEC_USIGN(val->type) = 1; SPEC_SCLS (val->type) = S_LITERAL; s++; /* get rid of quotation */ @@ -577,20 +814,29 @@ charVal (char *s) case '\"': SPEC_CVAL (val->type).v_int = '\"'; break; - case '0': - sscanf (s, "%o", &SPEC_CVAL (val->type).v_int); + + 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': - s++; /* go behond the 'x' */ - sscanf (s, "%x", &SPEC_CVAL (val->type).v_int); + SPEC_CVAL (val->type).v_uint = hexEscape(&s) ; break; + default: - SPEC_CVAL (val->type).v_int = *s; + SPEC_CVAL (val->type).v_uint = (unsigned char)*s; break; } } else /* not a backslash */ - SPEC_CVAL (val->type).v_int = *s; + SPEC_CVAL (val->type).v_uint = (unsigned char)*s; return val; } @@ -629,28 +875,31 @@ floatFromVal (value * val) if (SPEC_NOUN (val->etype) == V_FLOAT) return (double) SPEC_CVAL (val->etype).v_float; - else + + if (SPEC_LONG (val->etype)) { - if (SPEC_LONG (val->etype)) - { - if (SPEC_USIGN (val->etype)) - return (double) SPEC_CVAL (val->etype).v_ulong; - else - return (double) SPEC_CVAL (val->etype).v_long; - } + if (SPEC_USIGN (val->etype)) + return (double) SPEC_CVAL (val->etype).v_ulong; else - { - if (SPEC_USIGN (val->etype)) - return (double) SPEC_CVAL (val->etype).v_uint; - else - return (double) SPEC_CVAL (val->etype).v_int; - } + return (double) SPEC_CVAL (val->etype).v_long; } + + if (SPEC_NOUN(val->etype)==V_INT) { + if (SPEC_USIGN (val->etype)) + return (double) SPEC_CVAL (val->etype).v_uint; + else + return (double) SPEC_CVAL (val->etype).v_int; + } else { // SPEC_NOUN==V_CHAR + if (SPEC_USIGN (val->etype)) + return (double) ((unsigned char)SPEC_CVAL (val->etype).v_uint); + else + return (double) ((signed char)SPEC_CVAL (val->etype).v_int); + } } /*------------------------------------------------------------------*/ -/* valUnaryPM - dones the unary +/- operation on a constant */ +/* valUnaryPM - does the unary +/- operation on a constant */ /*------------------------------------------------------------------*/ value * valUnaryPM (value * val) @@ -663,20 +912,24 @@ valUnaryPM (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 = 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 = -SPEC_CVAL (val->etype).v_uint; + 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) { + SPEC_CVAL (val->etype).v_uint &= 0xff; + } } } + // -(unsigned 3) now really is signed + SPEC_USIGN(val->etype)=0; return val; - } /*------------------------------------------------------------------*/ @@ -699,6 +952,9 @@ valComplement (value * val) 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; + if (SPEC_NOUN (val->etype)==V_CHAR) { + SPEC_CVAL (val->etype).v_uint &= 0xff; + } } return val; } @@ -723,6 +979,9 @@ valNot (value * val) 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; + if (SPEC_NOUN (val->etype)==V_CHAR) { + SPEC_CVAL (val->etype).v_uint &= 0xff; + } } return val; } @@ -742,8 +1001,8 @@ valMult (value * lval, value * rval) 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_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); + SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype)); + SPEC_LONG (val->type) = 1; if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval); @@ -758,17 +1017,8 @@ valMult (value * lval, value * rval) SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) * (long) 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); - } } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -787,12 +1037,12 @@ valDiv (value * lval, value * rval) /* create a new value */ val = newValue (); - val->type = val->etype = ((floatFromVal (lval) / floatFromVal (rval)) < 256 ? - newCharLink () : newIntLink ()); - if (IS_FLOAT (lval->etype) || IS_FLOAT (rval->etype)) - SPEC_NOUN (val->etype) = V_FLOAT; + val->type = val->etype = newLink(); + val->type->class = SPECIFIER; + SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) || + IS_FLOAT (rval->etype) ? V_FLOAT : V_INT); SPEC_SCLS (val->etype) = S_LITERAL; - SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); + SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype)); SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); if (IS_FLOAT (val->type)) @@ -802,7 +1052,8 @@ valDiv (value * lval, value * rval) if (SPEC_LONG (val->type)) { if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) / + SPEC_CVAL (val->type).v_ulong = + (unsigned long) floatFromVal (lval) / (unsigned long) floatFromVal (rval); else SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) / @@ -810,15 +1061,16 @@ valDiv (value * lval, value * rval) } else { - if (SPEC_USIGN (val->type)) + if (SPEC_USIGN (val->type)) { SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) / (unsigned) floatFromVal (rval); - else + } else { SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) / (int) floatFromVal (rval); + } } } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -835,7 +1087,7 @@ valMod (value * lval, value * rval) val->type->class = 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_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype)); SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); if (SPEC_LONG (val->type)) @@ -849,15 +1101,16 @@ valMod (value * lval, value * rval) } else { - if (SPEC_USIGN (val->type)) + if (SPEC_USIGN (val->type)) { SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) % (unsigned) floatFromVal (rval); - else + } else { SPEC_CVAL (val->type).v_int = (unsigned) floatFromVal (lval) % (unsigned) floatFromVal (rval); + } } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -875,8 +1128,12 @@ valPlus (value * lval, value * rval) 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_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); + SPEC_USIGN (val->type) = + SPEC_USIGN (lval->etype) && + SPEC_USIGN (rval->etype) && + (floatFromVal(lval)+floatFromVal(rval))>=0; + + SPEC_LONG (val->type) = 1; if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval); @@ -891,17 +1148,8 @@ valPlus (value * lval, value * rval) SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) + (long) 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); - } } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -919,7 +1167,11 @@ valMinus (value * lval, value * rval) 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_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype)); + SPEC_USIGN (val->type) = + SPEC_USIGN (lval->etype) && + SPEC_USIGN (rval->etype) && + (floatFromVal(lval)-floatFromVal(rval))>=0; + SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); if (IS_FLOAT (val->type)) @@ -928,23 +1180,27 @@ valMinus (value * lval, value * rval) { if (SPEC_LONG (val->type)) { - if (SPEC_USIGN (val->type)) - SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) - + if (SPEC_USIGN (val->type)) { + SPEC_CVAL (val->type).v_ulong = + (unsigned long) floatFromVal (lval) - (unsigned long) floatFromVal (rval); - else + } else { SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) - (long) floatFromVal (rval); + } } else { - if (SPEC_USIGN (val->type)) + 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); + } else { + SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) - + (int) floatFromVal (rval); + } } } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -957,57 +1213,24 @@ valShift (value * lval, value * rval, int lr) /* create a new value */ val = newValue (); - val->type = val->etype = newLink (); - val->type->class = SPECIFIER; - SPEC_NOUN (val->type) = V_INT; /* type is int */ + val->type = val->etype = newIntLink (); 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)); + SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) & SPEC_USIGN (rval->etype)); + SPEC_LONG (val->type) = 1; - if (lr) - { - 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); - } - 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); - } - } - else + if (SPEC_LONG (val->type)) { - 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 = lr ? + (unsigned long) floatFromVal (lval) << (unsigned long) floatFromVal (rval) : \ + (unsigned long) floatFromVal (lval) >> (unsigned long) 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); - } + SPEC_CVAL (val->type).v_long = lr ? + (long) floatFromVal (lval) << (long) floatFromVal (rval) : \ + (long) floatFromVal (lval) >> (long) floatFromVal (rval); } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -1022,7 +1245,8 @@ valCompare (value * lval, value * rval, int ctype) val = newValue (); val->type = val->etype = newCharLink (); val->type->class = SPECIFIER; - SPEC_NOUN (val->type) = V_INT; /* type is int */ + SPEC_NOUN (val->type) = V_CHAR; /* type is char */ + SPEC_USIGN (val->type) = 1; SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ switch (ctype) @@ -1135,7 +1359,7 @@ valBitwise (value * lval, value * rval, int op) break; } - return val; + return cheapestVal(val); } /*------------------------------------------------------------------*/ @@ -1151,6 +1375,7 @@ valLogicAndOr (value * lval, value * rval, int op) val->type = val->etype = newCharLink (); val->type->class = SPECIFIER; SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */ + SPEC_USIGN (val->type) = 0; switch (op) { @@ -1203,9 +1428,12 @@ valCastLiteral (sym_link * dtype, double fval) else { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_uint = (unsigned int) fval; + SPEC_CVAL (val->etype).v_uint = (unsigned short)fval; else - SPEC_CVAL (val->etype).v_int = (int) fval; + SPEC_CVAL (val->etype).v_int = (short)fval; + if (SPEC_NOUN (val->etype)==V_CHAR) { + SPEC_CVAL (val->etype).v_uint &= 0xff; + } } } return val; @@ -1227,22 +1455,22 @@ getNelements (sym_link * type, initList * ilist) ilist = ilist->init.deep; /* if type is a character array and there is only one - initialiser then get the length of the string */ + (string) initialiser then get the length of the string */ if (IS_ARRAY (type) && IS_CHAR (etype) && !ilist->next) { ast *iast = ilist->init.node; value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL); if (!v) { - werror (E_INIT_WRONG); + werror (W_INIT_WRONG); return 0; } - if (!IS_ARRAY (v->type) || !IS_CHAR (v->etype)) + + if (IS_ARRAY (v->type) && IS_CHAR (v->etype)) + // yep, it's a string { - werror (E_INIT_WRONG); - return 0; + return DCL_ELEM (v->type); } - return DCL_ELEM (v->type); } i = 0; @@ -1408,3 +1636,25 @@ valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op) val->etype = getSpec (val->type); return val; } + +/*-----------------------------------------------------------------*/ +/* valForCastAggr - will return value for a cast of an aggregate */ +/* with no constant */ +/*-----------------------------------------------------------------*/ +value * +valForCastArr (ast * aexpr, sym_link * type) +{ + value *val; + + if (!IS_AST_SYM_VALUE (aexpr)) + return NULL; + + val = newValue (); + + sprintf (val->name, "(%s)", + AST_SYMBOL (aexpr)->rname); + + val->type = type; + val->etype = getSpec (val->type); + return val; +}