/* newValue - allocates and returns a new value */
/*-----------------------------------------------------------------*/
value *
-newValue ()
+newValue (void)
{
value *val;
{
initList *nilist;
-
nilist = Safe_alloc (sizeof (initList));
nilist->type = type;
- nilist->lineno = lexLineno;
nilist->filename = lexFilename;
+ nilist->lineno = lexLineno;
switch (type)
{
}
/*------------------------------------------------------------------*/
-/* revinit - reverses the initial values for a value chain */
+/* revinit - reverses the initial values for a value chain */
/*------------------------------------------------------------------*/
initList *
revinit (initList * val)
}
bool
-convertIListToConstList(initList *src, literalList **lList)
+convertIListToConstList(initList *src, literalList **lList, int size)
{
+ int cnt = 0;
initList *iLoop;
literalList *head, *last, *newL;
head = last = NULL;
- if (!src || src->type != INIT_DEEP)
+ if (src && src->type != INIT_DEEP)
{
return FALSE;
}
- iLoop = src->init.deep;
+ iLoop = src ? src->init.deep : NULL;
while (iLoop)
{
return FALSE;
}
iLoop = iLoop->next;
+ cnt++;
+ }
+ if (!size)
+ {
+ size = cnt;
}
/* We've now established that the initializer list contains only literal values. */
- iLoop = src->init.deep;
- while (iLoop)
+ iLoop = src ? src->init.deep : NULL;
+ while (size--)
{
- double val = AST_LIT_VALUE(iLoop->init.node);
+ double val = iLoop ? AST_FLOAT_VALUE(iLoop->init.node) : 0;
if (last && last->literalValue == val)
{
}
last = newL;
}
- iLoop = iLoop->next;
+ iLoop = iLoop ? iLoop->next : NULL;
}
if (!head)
/*------------------------------------------------------------------*/
-/* copyIlist - copy initializer list */
+/* copyIlist - copy initializer list */
/*------------------------------------------------------------------*/
initList *
copyIlist (initList * src)
ast *
list2expr (initList * ilist)
{
+ if (!ilist)
+ return NULL;
if (ilist->type == INIT_DEEP)
return list2expr (ilist->init.deep);
return ilist->init.node;
/* 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;
+ char *p;
double dval;
+ bool is_integral = 0;
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 */
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;
-
errno = 0;
- if (hex || octal) {
- unsigned long sval;
- sval = strtoul (s, NULL, 0);
- dval = sval;
- if (errno) {
+ if (s[0] == '0')
+ {
+ if (s[1] == 'b' || s[1] == 'B')
+ dval = strtoul (s + 2, &p, 2);
+ else
+ dval = strtoul (s, &p, 0);
+ is_integral = 1;
+ }
+ else
+ dval = strtod (s, &p);
+
+ if (errno)
+ {
dval = 4294967295.0;
werror (W_INVALID_INT_CONST, s, dval);
}
- } 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 (is_integral && /* integral (hex, octal and binary) 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);
}
/*------------------------------------------------------------------*/
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 (IS_FLOAT (val->type))
SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval);
- else
- if (IS_FIXED16X16 (val->type))
+ else if (IS_FIXED16X16 (val->type))
SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) / floatFromVal (rval) );
else if (SPEC_LONG (val->type))
{
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:
}
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);