X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCval.c;h=d893ccba608a3a902fbe64b934858e0cd78eb62e;hb=d8be5b9eef951edcd88af05a6800d46fffed9582;hp=a9b17d639e144ca60507c3006adca4b74bd17dbf;hpb=0299234dcc9718e0017d8a1a2d04c6b64220af2c;p=fw%2Fsdcc diff --git a/src/SDCCval.c b/src/SDCCval.c index a9b17d63..d893ccba 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -337,31 +337,29 @@ value *cheapestVal (value *val) { } if (SPEC_USIGN(val->type)) { - if (uval<=0xff) { - SPEC_NOUN(val->type)=V_CHAR; + if (uval<=0xffff) { SPEC_LONG(val->type)=0; - } else { - 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 { + } else { // not unsigned if (sval<0) { - if (sval>=-128) { - SPEC_NOUN(val->type)=V_CHAR; + if (sval>=-32768) { SPEC_LONG(val->type)=0; - } else { - if (sval>=-32768) { - SPEC_LONG(val->type)=0; + SPEC_CVAL(val->type).v_int = sval; + if (sval>=-128) { + SPEC_NOUN(val->type)=V_CHAR; } } - } else { - if (sval<=127) { - SPEC_NOUN(val->type)=V_CHAR; + } else { // sval>=0 + SPEC_USIGN(val->type)=1; + if (sval<=65535) { SPEC_LONG(val->type)=0; - } else { - if (sval<=32767) { - SPEC_LONG(val->type)=0; + SPEC_CVAL(val->type).v_int = sval; + if (sval<=255) { + SPEC_NOUN(val->type)=V_CHAR; } } } @@ -420,7 +418,7 @@ value *constVal (char *s) short hex = 0, octal = 0; char scanFmt[10]; int scI = 0; - unsigned long sval; + double dval; val = newValue (); /* alloc space for value */ @@ -431,10 +429,6 @@ value *constVal (char *s) SPEC_NOUN (val->type) = V_CHAR; 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; - hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0); /* set the octal flag */ @@ -444,43 +438,72 @@ value *constVal (char *s) /* create the scan string */ scanFmt[scI++] = '%'; + scanFmt[scI++] = 'l'; + if (octal) scanFmt[scI++] = 'o'; else if (hex) scanFmt[scI++] = 'x'; else - scanFmt[scI++] = 'd'; + scanFmt[scI++] = 'f'; - scanFmt[scI++] = 'L'; scanFmt[scI++] = '\0'; - sscanf (s, scanFmt, &sval); + if (octal || hex) { + unsigned long sval; + sscanf (s, scanFmt, &sval); + dval=sval; + } else { + sscanf (s, scanFmt, &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; + } - if (sval<0) { // "-28u" will still be signed and negative + if (dval<0) { // "-28u" will still be signed and negative SPEC_USIGN (val->type) = 0; - if (sval<-32768) { // check if we have to promote to long + 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; - SPEC_CVAL (val->type).v_long=sval; - } else { - SPEC_CVAL (val->type).v_int=sval; - if (sval<-128) { // check if we have to promote to int - SPEC_NOUN (val->type) = V_INT; - } } - } else { - if (sval>0xffff) { // check if we have to promote to long + } else { // >=0 + if (dval>0xff) { // check if we have to promote to int SPEC_NOUN (val->type) = V_INT; + } + if (dval>0xffff) { // check if we have to promote to long int SPEC_LONG (val->type) = 1; - SPEC_CVAL (val->type).v_ulong=sval; - } else { - SPEC_CVAL (val->type).v_uint=sval; - if (sval>0xff) { // check if we have to promote to int - SPEC_NOUN (val->type) = V_INT; - } } } + if (SPEC_LONG (val->type)) + { + if (SPEC_USIGN (val->type)) + { + SPEC_CVAL (val->type).v_ulong = dval; + } + else + { + SPEC_CVAL (val->type).v_long = dval; + } + } + else + { + if (SPEC_USIGN (val->type)) + { + SPEC_CVAL (val->type).v_uint = dval; + } + else + { + SPEC_CVAL (val->type).v_int = dval; + } + } + return val; } @@ -489,40 +512,34 @@ value *constVal (char *s) /param src Pointer to 'x' from start of hex character value */ -char hexEscape(char **src) - +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); + 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; + return (char) value; } + /*------------------------------------------------------------------*/ /* 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 */ /*------------------------------------------------------------------*/ -char octalEscape (char **str) { +unsigned char octalEscape (char **str) { int digits; unsigned value=0; @@ -811,20 +828,20 @@ charVal (char *s) case '5' : case '6' : case '7' : - SPEC_CVAL (val->type).v_int = octalEscape(&s); + SPEC_CVAL (val->type).v_uint = octalEscape(&s); break; case 'x': - SPEC_CVAL (val->type).v_int = hexEscape(&s) ; + 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; } @@ -842,7 +859,7 @@ valFromType (sym_link * type) } /*------------------------------------------------------------------*/ -/* floatFromVal - value to unsinged integer conversion */ +/* floatFromVal - value to double float conversion */ /*------------------------------------------------------------------*/ double floatFromVal (value * val) @@ -863,23 +880,41 @@ 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; + } + + if (SPEC_NOUN (val->etype) == 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; + } + + if (IS_BITVAR(val->etype)) { + return (double) SPEC_CVAL (val->etype).v_ulong; + } + + if (SPEC_NOUN (val->etype) == V_VOID) { + return (double) SPEC_CVAL (val->etype).v_ulong; + } + + // we are lost ! + werror (E_INTERNAL_ERROR, __FILE__, __LINE__, + "floatFromVal: unknown value"); + return 0; } @@ -909,6 +944,14 @@ valUnaryPM (value * val) 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; + } + } return val; } @@ -933,6 +976,8 @@ valComplement (value * val) else SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int; } + // ~(unsigned 3) now really is signed + SPEC_USIGN(val->etype)=0; return val; } @@ -976,7 +1021,7 @@ valMult (value * lval, value * rval) 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_LONG (val->type) = 1; if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval); @@ -991,15 +1036,6 @@ 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 cheapestVal(val); } @@ -1116,7 +1152,7 @@ valPlus (value * lval, value * rval) SPEC_USIGN (rval->etype) && (floatFromVal(lval)+floatFromVal(rval))>=0; - SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype)); + SPEC_LONG (val->type) = 1; if (IS_FLOAT (val->type)) SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval); @@ -1131,16 +1167,6 @@ 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 cheapestVal(val); } @@ -1209,7 +1235,7 @@ valShift (value * lval, value * rval, int lr) 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_LONG (val->type) = 1; if (SPEC_LONG (val->type)) { @@ -1222,18 +1248,6 @@ valShift (value * lval, value * rval, int lr) (long) floatFromVal (lval) << (long) floatFromVal (rval) : \ (long) floatFromVal (lval) >> (long) floatFromVal (rval); } - else - { - if (SPEC_USIGN (val->type)) { - SPEC_CVAL (val->type).v_uint = lr ? - (unsigned) floatFromVal (lval) << (unsigned) floatFromVal (rval) :\ - (unsigned) floatFromVal (lval) >> (unsigned) floatFromVal (rval); - } else { - SPEC_CVAL (val->type).v_int = lr ? - (int) floatFromVal (lval) << (int) floatFromVal (rval) : \ - (int) floatFromVal (lval) >> (int) floatFromVal (rval); - } - } return cheapestVal(val); } @@ -1413,39 +1427,27 @@ valCastLiteral (sym_link * dtype, double fval) SPEC_SCLS (val->etype) = S_LITERAL; /* if it is not a specifier then we can assume that */ /* it will be an unsigned long */ - if (!IS_SPEC (val->type)) - { + if (!IS_SPEC (val->type)) { SPEC_CVAL (val->etype).v_ulong = (unsigned long) fval; return val; - } + } if (SPEC_NOUN (val->etype) == V_FLOAT) - SPEC_CVAL (val->etype).v_float = fval; - else - { - if (SPEC_LONG (val->etype)) - { + SPEC_CVAL (val->etype).v_float = fval; + else { + unsigned long l = fval; + if (SPEC_LONG (val->etype)) { if (SPEC_USIGN (val->etype)) - SPEC_CVAL (val->etype).v_ulong = (unsigned long) fval; + SPEC_CVAL (val->etype).v_ulong = (unsigned long) l; else - SPEC_CVAL (val->etype).v_long = (long) fval; - } - else - { + SPEC_CVAL (val->etype).v_long = (long) l; + } else { if (SPEC_USIGN (val->etype)) - if (SPEC_NOUN (val->etype)==V_CHAR) { - SPEC_CVAL (val->etype).v_uint = (unsigned char) fval; - } else { - SPEC_CVAL (val->etype).v_uint = (unsigned int) fval; - } + SPEC_CVAL (val->etype).v_uint = (unsigned short)l; else - if (SPEC_NOUN (val->etype)==V_CHAR) { - SPEC_CVAL (val->etype).v_int = (char) fval; - } else { - SPEC_CVAL (val->etype).v_int = (int) fval; - } - } - } + SPEC_CVAL (val->etype).v_int = (short)l; + } + } return val; } @@ -1455,7 +1457,6 @@ valCastLiteral (sym_link * dtype, double fval) int getNelements (sym_link * type, initList * ilist) { - sym_link *etype = getSpec (type); int i; if (!ilist) @@ -1466,13 +1467,13 @@ getNelements (sym_link * type, initList * ilist) /* if type is a character array and there is only one (string) initialiser then get the length of the string */ - if (IS_ARRAY (type) && IS_CHAR (etype) && !ilist->next) + if (IS_ARRAY (type) && IS_CHAR (type->next) && !ilist->next) { ast *iast = ilist->init.node; value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL); if (!v) { - werror (W_INIT_WRONG); + werror (E_CONST_EXPECTED); return 0; } @@ -1489,7 +1490,6 @@ getNelements (sym_link * type, initList * ilist) i++; ilist = ilist->next; } - return i; }