/* newValue - allocates and returns a new value */
/*-----------------------------------------------------------------*/
value *
-newValue ()
+newValue (void)
{
value *val;
nilist = Safe_alloc (sizeof (initList));
nilist->type = type;
- nilist->lineno = lexLineno;
nilist->filename = lexFilename;
+ nilist->lineno = lexLineno;
switch (type)
{
iLoop = iLoop->next;
}
- // We've now established that the initializer list contains only literal values.
+ /* 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);
+ double val = AST_FLOAT_VALUE(iLoop->init.node);
if (last && last->literalValue == val)
{
/* constFloatVal - converts a FLOAT constant to value */
/*-----------------------------------------------------------------*/
value *
-constFloatVal (char *s)
+constFloatVal (const char *s)
{
value *val = newValue ();
double sval;
if (p == s)
{
werror (E_INVALID_FLOAT_CONST, s);
- return constVal ("0");
+ return constCharVal (0);
}
val->type = val->etype = newLink (SPECIFIER);
/* constFixed16x16Val - converts a FIXED16X16 constant to value */
/*-----------------------------------------------------------------*/
value *
-constFixed16x16Val (char *s)
+constFixed16x16Val (const char *s)
{
value *val = newValue ();
double sval;
if (p == s)
{
werror (E_INVALID_FLOAT_CONST, s);
- return constVal ("0");
+ return constCharVal (0);
}
val->type = val->etype = newLink (SPECIFIER);
value *constVal (const char *s)
{
value *val;
- short hex = 0, octal = 0;
+ bool hex = FALSE, octal = FALSE;
+ char *p;
double dval;
val = newValue (); /* alloc space for value */
- val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */
+ val->type = val->etype = newLink (SPECIFIER); /* create the specifier */
SPEC_SCLS (val->type) = S_LITERAL;
- // let's start with a signed char
+ /* let's start with a signed char */
SPEC_NOUN (val->type) = V_CHAR;
SPEC_USIGN (val->type) = 0;
- hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
-
- /* set the octal flag */
- if (!hex && *s == '0' && *(s + 1))
- octal = 1;
+ if (s[0] == '0')
+ {
+ if (s[1] == 'x' || s[1] == 'X')
+ hex = TRUE;
+ else if (isdigit(s[1]))
+ octal = TRUE;
+ }
errno = 0;
- if (hex || octal) {
- unsigned long sval;
- sval = strtoul (s, NULL, 0);
- dval = sval;
- if (errno) {
- dval = 4294967295.0;
- werror (W_INVALID_INT_CONST, s, dval);
+ if (hex || octal)
+ {
+ dval = strtoul (s, &p, 0);
+ if (errno)
+ {
+ dval = 4294967295.0;
+ werror (W_INVALID_INT_CONST, s, dval);
+ }
+ }
+ else
+ {
+ dval = strtod(s, &p);
}
- } else {
- dval = strtod(s, NULL);
- }
/* Setup the flags first */
/* set the unsigned flag if 'uU' is found */
- if (strchr (s, 'u') || strchr (s, 'U')) {
- SPEC_USIGN (val->type) = 1;
- }
+ if (strchr (p, 'u') || strchr (p, 'U'))
+ {
+ SPEC_USIGN (val->type) = 1;
+ }
/* 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;
- }
- 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;
- }
- 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) {
- SPEC_USIGN (val->type) = 1;
- } else {
- SPEC_LONG (val->type) = 1;
- if (dval>0x7fffffff) {
- SPEC_USIGN (val->type) = 1;
- }
+ if (strchr (p, 'l') || strchr (p, '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;
+ }
+ 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;
+ }
+ 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)
+ {
+ 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 (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))
{
return val;
}
+value *constCharVal (unsigned char v)
+{
+ value *val = newValue (); /* alloc space for value */
+
+ val->type = val->etype = newLink (SPECIFIER); /* create the specifier */
+ SPEC_SCLS (val->type) = S_LITERAL;
+ /* let's start with a signed char */
+ SPEC_NOUN (val->type) = V_CHAR;
+ SPEC_USIGN (val->type) = 1;
+ SPEC_CVAL (val->type).v_uint = v;
+
+ return val;
+}
+
/*------------------------------------------------------------------*/
-/* strVal - converts a string constant to a value */
+/* strVal - converts a string constant to a value */
/*------------------------------------------------------------------*/
value *
strVal (const char *s)
}
/*------------------------------------------------------------------*/
-/* charVal - converts a character constant to a value */
+/* charVal - converts a character constant to a value */
/*------------------------------------------------------------------*/
value *
charVal (const char *s)
{
- value *val;
-
- val = newValue ();
-
- val->type = val->etype = newLink (SPECIFIER);
- SPEC_NOUN (val->type) = V_CHAR;
- SPEC_USIGN(val->type) = 1;
- SPEC_SCLS (val->type) = S_LITERAL;
-
- s++; /* get rid of quotation */
+ /* get rid of quotation */
/* if \ then special processing */
- if (*s == '\\')
+ if (*++s == '\\')
{
- s++; /* go beyond the backslash */
- switch (*s)
+ switch (*++s) /* go beyond the backslash */
{
case 'n':
- SPEC_CVAL (val->type).v_uint = '\n';
- break;
+ return constCharVal ('\n');
case 't':
- SPEC_CVAL (val->type).v_uint = '\t';
- break;
+ return constCharVal ('\t');
case 'v':
- SPEC_CVAL (val->type).v_uint = '\v';
- break;
+ return constCharVal ('\v');
case 'b':
- SPEC_CVAL (val->type).v_uint = '\b';
- break;
+ return constCharVal ('\b');
case 'r':
- SPEC_CVAL (val->type).v_uint = '\r';
- break;
+ return constCharVal ('\r');
case 'f':
- SPEC_CVAL (val->type).v_uint = '\f';
- break;
+ return constCharVal ('\f');
case 'a':
- SPEC_CVAL (val->type).v_uint = '\a';
- break;
+ return constCharVal ('\a');
case '\\':
- SPEC_CVAL (val->type).v_uint = '\\';
- break;
+ return constCharVal ('\\');
case '\?':
- SPEC_CVAL (val->type).v_uint = '\?';
- break;
+ return constCharVal ('\?');
case '\'':
- SPEC_CVAL (val->type).v_uint = '\'';
- break;
+ return constCharVal ('\'');
case '\"':
- SPEC_CVAL (val->type).v_uint = '\"';
- break;
+ return constCharVal ('\"');
case '0' :
case '1' :
case '5' :
case '6' :
case '7' :
- SPEC_CVAL (val->type).v_uint = octalEscape(&s);
- break;
+ return constCharVal (octalEscape (&s));
case 'x':
- SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
- break;
+ return constCharVal (hexEscape (&s));
default:
- SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
- break;
+ return constCharVal (*s);
}
}
else /* not a backslash */
- SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
-
- return val;
+ return constCharVal (*s);
}
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/* floatFromVal - value to double float conversion */
/*------------------------------------------------------------------*/
-#if 0
-double
-floatFromVal (value * val)
-{
- double res;
-
- if (!val)
- return 0;
-
- if (val->etype && SPEC_SCLS (val->etype) != S_LITERAL)
- {
- werror (E_CONST_EXPECTED, val->name);
- return 0;
- }
-
- /* if it is not a specifier then we can assume that */
- /* it will be an unsigned long */
- if (!IS_SPEC (val->type)) {
- //return (double) SPEC_CVAL (val->etype).v_ulong;
- res =SPEC_CVAL (val->etype).v_ulong;
- goto ret;
- }
-
- if (SPEC_NOUN (val->etype) == V_FLOAT) {
- //return (double) SPEC_CVAL (val->etype).v_float;
- res =SPEC_CVAL (val->etype).v_float;
- goto ret;
- }
-
- if (SPEC_NOUN (val->etype) == V_FIXED16X16) {
- res =doubleFromFixed16x16( SPEC_CVAL (val->etype).v_fixed16x16 );
- goto ret;
- }
-
- if (SPEC_LONG (val->etype))
- {
- if (SPEC_USIGN (val->etype)) {
- //return (double) SPEC_CVAL (val->etype).v_ulong;
- res =SPEC_CVAL (val->etype).v_ulong;
- goto ret;
- }
- else {
- //return (double) SPEC_CVAL (val->etype).v_long;
- res =SPEC_CVAL (val->etype).v_long;
- goto ret;
- }
- }
-
- if (SPEC_NOUN (val->etype) == V_INT) {
- if (SPEC_USIGN (val->etype)) {
- //return (double) SPEC_CVAL (val->etype).v_uint;
- res =SPEC_CVAL (val->etype).v_uint;
- goto ret;
- }
- else {
- //return (double) SPEC_CVAL (val->etype).v_int;
- res =SPEC_CVAL (val->etype).v_int;
- goto ret;
- }
- }
-
- if (SPEC_NOUN (val->etype) == V_CHAR) {
- if (SPEC_USIGN (val->etype)) {
- //return (double) (unsigned char)SPEC_CVAL (val->etype).v_uint;
- res =(unsigned char)SPEC_CVAL (val->etype).v_uint;
- goto ret;
- }
- else
- {
- res = (signed char)SPEC_CVAL (val->etype).v_int;
- goto ret;
- }
- }
-
- if (IS_BITVAR(val->etype)) {
- //return (double) SPEC_CVAL (val->etype).v_uint;
- res =SPEC_CVAL (val->etype).v_uint;
- goto ret;
- }
-
- if (SPEC_NOUN (val->etype) == V_VOID) {
- //return (double) SPEC_CVAL (val->etype).v_ulong;
- res = SPEC_CVAL (val->etype).v_ulong;
- goto ret;
- }
-
- // we are lost !
- werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
- "floatFromVal: unknown value");
- return 0;
-
-ret:
- printf("floatFromVal(%f)\n", res);
- return res;
-}
-#endif
-
double
floatFromVal (value * val)
{
return SPEC_CVAL (val->etype).v_float;
if (SPEC_NOUN (val->etype) == V_FIXED16X16)
- return doubleFromFixed16x16( SPEC_CVAL (val->etype).v_fixed16x16 );
+ return doubleFromFixed16x16 (SPEC_CVAL (val->etype).v_fixed16x16);
if (SPEC_LONG (val->etype))
{
return SPEC_CVAL (val->etype).v_long;
}
- if (SPEC_NOUN (val->etype) == V_INT) {
- if (SPEC_USIGN (val->etype))
- return SPEC_CVAL (val->etype).v_uint;
- else
- return SPEC_CVAL (val->etype).v_int;
- }
+ if (SPEC_NOUN (val->etype) == V_INT)
+ {
+ if (SPEC_USIGN (val->etype))
+ return SPEC_CVAL (val->etype).v_uint;
+ else
+ return SPEC_CVAL (val->etype).v_int;
+ }
- if (SPEC_NOUN (val->etype) == V_CHAR) {
- if (SPEC_USIGN (val->etype))
- return (unsigned char)SPEC_CVAL (val->etype).v_uint;
- else
- return (signed char)SPEC_CVAL (val->etype).v_int;
- }
+ if (SPEC_NOUN (val->etype) == V_CHAR)
+ {
+ if (SPEC_USIGN (val->etype))
+ return (unsigned char) SPEC_CVAL (val->etype).v_uint;
+ else
+ return (signed char) SPEC_CVAL (val->etype).v_int;
+ }
if (IS_BITVAR(val->etype))
return SPEC_CVAL (val->etype).v_uint;
if (SPEC_NOUN (val->etype) == V_VOID)
return SPEC_CVAL (val->etype).v_ulong;
- // we are lost !
+ /* we are lost ! */
werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "floatFromVal: unknown value");
return 0;
}
return double2ul (SPEC_CVAL (val->etype).v_float);
if (SPEC_NOUN (val->etype) == V_FIXED16X16)
- return double2ul (doubleFromFixed16x16( SPEC_CVAL (val->etype).v_fixed16x16 ));
+ return double2ul (doubleFromFixed16x16 (SPEC_CVAL (val->etype).v_fixed16x16));
if (SPEC_LONG (val->etype))
{
return SPEC_CVAL (val->etype).v_long;
}
- if (SPEC_NOUN (val->etype) == V_INT) {
- if (SPEC_USIGN (val->etype))
- return SPEC_CVAL (val->etype).v_uint;
- else
- return SPEC_CVAL (val->etype).v_int;
- }
+ if (SPEC_NOUN (val->etype) == V_INT)
+ {
+ if (SPEC_USIGN (val->etype))
+ return SPEC_CVAL (val->etype).v_uint;
+ else
+ return SPEC_CVAL (val->etype).v_int;
+ }
- if (SPEC_NOUN (val->etype) == V_CHAR) {
- if (SPEC_USIGN (val->etype))
- return (unsigned char)SPEC_CVAL (val->etype).v_uint;
- else
- return (signed char)SPEC_CVAL (val->etype).v_int;
- }
+ if (SPEC_NOUN (val->etype) == V_CHAR)
+ {
+ if (SPEC_USIGN (val->etype))
+ return (unsigned char) SPEC_CVAL (val->etype).v_uint;
+ else
+ return (signed char) SPEC_CVAL (val->etype).v_int;
+ }
- if (IS_BITVAR(val->etype)) {
+ if (IS_BITVAR(val->etype))
return SPEC_CVAL (val->etype).v_uint;
- }
- if (SPEC_NOUN (val->etype) == V_VOID) {
+ if (SPEC_NOUN (val->etype) == V_VOID)
return SPEC_CVAL (val->etype).v_ulong;
- }
- // we are lost !
+ /* we are lost ! */
werror (E_INTERNAL_ERROR, __FILE__, __LINE__, "ulFromVal: unknown value");
return 0;
}
double tmp=0, exp=2;
tmp = (value & 0xffff0000) >> 16;
-
+
while(value) {
value &= 0xffff;
if(value & 0x8000)tmp += 1/exp;
exp *= 2;
value <<= 1;
}
-
+
return (tmp);
#else
return ((double)(value * 1.0) / (double)(1UL << 16));
tmp = floor( value );
res = tmp << 16;
value -= tmp;
-
+
tmp = 0;
while(pos--) {
value *= 2;
if(value >= 1.0)tmp |= (1 << pos);
value -= floor( value );
}
-
+
res |= tmp;
return (res);
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;
+ SPEC_CVAL (val->etype).v_fixed16x16 = (TYPE_TARGET_ULONG) -((long) SPEC_CVAL (val->etype).v_fixed16x16);
else
{
if (SPEC_LONG (val->etype))
/*------------------------------------------------------------------*/
/* valCastLiteral - casts a literal value to another type */
/*------------------------------------------------------------------*/
-#if 0
-value *
-valCastLiteral (sym_link * dtype, double fval)
-{
- value *val;
- unsigned long l = double2ul (fval);
-
- if (!dtype)
- return NULL;
-
- val = newValue ();
- 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 */
- /* it will be an unsigned long */
- if (!IS_SPEC (val->type)) {
- SPEC_CVAL (val->etype).v_ulong = l;
- return val;
- }
-
- 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_TARGET_UCHAR) l;
- else
- SPEC_CVAL (val->etype).v_int = (TYPE_TARGET_CHAR) l;
- } else {
- if (SPEC_LONG (val->etype)) {
- if (SPEC_USIGN (val->etype))
- SPEC_CVAL (val->etype).v_ulong = (TYPE_TARGET_ULONG) l;
- else
- SPEC_CVAL (val->etype).v_long = (TYPE_TARGET_LONG) l;
- } else {
- if (SPEC_USIGN (val->etype))
- SPEC_CVAL (val->etype).v_uint = (TYPE_TARGET_UINT) l;
- else
- SPEC_CVAL (val->etype).v_int = (TYPE_TARGET_INT) l;
- }
- }
- return val;
-}
-#endif
-
value *
valCastLiteral (sym_link * dtype, double fval)
{
break;
case V_BITFIELD:
- SPEC_CVAL (val->etype).v_uint = ((TYPE_TARGET_UINT) l) &
- (0xffffu >> (16 - SPEC_BLEN (val->etype)));
+ l &= (0xffffffffu >> (32 - SPEC_BLEN (val->etype)));
+ if (SPEC_USIGN (val->etype))
+ SPEC_CVAL (val->etype).v_uint = (TYPE_TARGET_UINT) l;
+ else
+ SPEC_CVAL (val->etype).v_int = (TYPE_TARGET_INT) l;
break;
case V_CHAR:
}
if (IS_ARRAY (v->type) && IS_CHAR (v->etype))
- // yep, it's a string
+ /* yep, it's a string */
{
return DCL_ELEM (v->type);
}
}
SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
- (int) AST_LIT_VALUE (arrExpr->right) * size);
+ AST_ULONG_VALUE (arrExpr->right) * size);
val->type = newLink (DECLARATOR);
if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
AST_SYMBOL (aexpr)->rname, op,
- getSize (type->next) * (int) AST_LIT_VALUE (cnst));
+ getSize (type->next) * AST_ULONG_VALUE (cnst));
val->type = type;
val->etype = getSpec (val->type);