#include <limits.h>
#include "newalloc.h"
+#if defined(__BORLANDC__) || defined(_MSC_VER)
+#define LONG_LONG __int64
+#else
+#define LONG_LONG long long
+#endif
+
int cNestLevel;
/*-----------------------------------------------------------------*/
{
value *val;
- val = Safe_calloc (1, sizeof (value));
+ val = Safe_alloc (sizeof (value));
return val;
}
initList *nilist;
- nilist = Safe_calloc (1, sizeof (initList));
+ nilist = Safe_alloc (sizeof (initList));
nilist->type = type;
nilist->lineno = yylineno;
}
else
{
- newL = Safe_malloc(sizeof(literalList));
+ newL = Safe_alloc(sizeof(literalList));
newL->literalValue = val;
newL->count = 1;
newL->next = NULL;
while (src)
{
- newL = Safe_malloc(sizeof(literalList));
+ newL = Safe_alloc(sizeof(literalList));
newL->literalValue = src->literalValue;
newL->count = src->count;
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<=0xff) {
+ SPEC_NOUN(val->type)=V_CHAR;
+ SPEC_LONG(val->type)=0;
+ } else {
+ if (uval<=0xffff) {
+ SPEC_LONG(val->type)=0;
+ }
+ }
+ } else {
+ if (sval<0) {
+ if (sval>=-128) {
+ SPEC_NOUN(val->type)=V_CHAR;
+ SPEC_LONG(val->type)=0;
+ } else {
+ if (sval>=-32768) {
+ SPEC_LONG(val->type)=0;
+ }
+ }
+ } else {
+ if (sval<=127) {
+ SPEC_NOUN(val->type)=V_CHAR;
+ SPEC_LONG(val->type)=0;
+ } else {
+ if (sval<=32767) {
+ SPEC_LONG(val->type)=0;
+ }
+ }
+ }
+ }
+ return val;
+}
+
/*-----------------------------------------------------------------*/
/* valueFromLit - creates a value from a literal */
/*-----------------------------------------------------------------*/
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");
short hex = 0, octal = 0;
char scanFmt[10];
int scI = 0;
- unsigned long sval;
+ LONG_LONG sval;
val = newValue (); /* alloc space for value */
/* create the scan string */
scanFmt[scI++] = '%';
+ scanFmt[scI++] = 'L';
+
if (octal)
scanFmt[scI++] = 'o';
else if (hex)
else
scanFmt[scI++] = 'd';
- scanFmt[scI++] = 'L';
scanFmt[scI++] = '\0';
sscanf (s, scanFmt, &sval);
if (sval<0) { // "-28u" will still be signed and negative
SPEC_USIGN (val->type) = 0;
- if (sval<-32768) { // check if have to promote to long
+ if (sval<-32768) { // check if we have to promote to long
SPEC_NOUN (val->type) = V_INT;
SPEC_LONG (val->type) = 1;
SPEC_CVAL (val->type).v_long=sval;
/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;
SPEC_NOUN (val->etype) = V_CHAR;
SPEC_SCLS (val->etype) = S_LITERAL;
- SPEC_CVAL (val->etype).v_char = Safe_calloc (1, strlen (s) + 1);
+ 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;
}
/*------------------------------------------------------------------*/
-/* copyValue - copies contents of a vlue to a fresh one */
+/* copyValue - copies contents of a value to a fresh one */
/*------------------------------------------------------------------*/
value *
copyValue (value * src)
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_int = (unsigned char)*s;
return val;
}
/*------------------------------------------------------------------*/
-/* valUnaryPM - dones the unary +/- operation on a constant */
+/* valUnaryPM - does the unary +/- operation on a constant */
/*------------------------------------------------------------------*/
value *
valUnaryPM (value * val)
}
}
return val;
-
}
/*------------------------------------------------------------------*/
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));
SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
if (IS_FLOAT (val->type))
(int) floatFromVal (rval);
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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))
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) /
if (SPEC_USIGN (val->type)) {
SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) /
(unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_uint <=255)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
} else {
SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) /
(int) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_int >=-128) &&
- (SPEC_CVAL (val->type).v_int <=127)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
}
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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))
if (SPEC_USIGN (val->type)) {
SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) %
(unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_uint <=255)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
} else {
SPEC_CVAL (val->type).v_int = (unsigned) floatFromVal (lval) %
(unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_int >=-128) &&
- (SPEC_CVAL (val->type).v_int <=127)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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))
if (SPEC_USIGN (val->type)) {
SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) +
(unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_uint <=255)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
} else {
SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) +
(int) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_int >=-128) &&
- (SPEC_CVAL (val->type).v_int <=127)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
}
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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))
if (SPEC_USIGN (val->type)) {
SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) -
(unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_uint <=255)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
} else {
- SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) - (int) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_int >=-128) &&
- (SPEC_CVAL (val->type).v_int <=127)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
+ SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) -
+ (int) floatFromVal (rval);
}
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
val = newValue ();
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_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))
SPEC_CVAL (val->type).v_uint = lr ?
(unsigned) floatFromVal (lval) << (unsigned) floatFromVal (rval) :\
(unsigned) floatFromVal (lval) >> (unsigned) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_uint <=255)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
} else {
SPEC_CVAL (val->type).v_int = lr ?
(int) floatFromVal (lval) << (int) floatFromVal (rval) : \
(int) floatFromVal (lval) >> (int) floatFromVal (rval);
- if (/* IS_CHAR (lval->etype) && IS_CHAR (rval->etype) && */
- (SPEC_CVAL (val->type).v_int >=-128) &&
- (SPEC_CVAL (val->type).v_int <=127)) {
- SPEC_NOUN (val->type) = V_CHAR;
- }
}
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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)
break;
}
- return val;
+ return cheapestVal(val);
}
/*------------------------------------------------------------------*/
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)
{
else
{
if (SPEC_USIGN (val->etype))
- SPEC_CVAL (val->etype).v_uint = (unsigned int) fval;
+ if (SPEC_NOUN (val->etype)==V_CHAR) {
+ SPEC_CVAL (val->etype).v_uint = (unsigned char)fval;
+ } else {
+ SPEC_CVAL (val->etype).v_uint = (unsigned short)fval;
+ }
else
- SPEC_CVAL (val->etype).v_int = (int) fval;
+ if (SPEC_NOUN (val->etype)==V_CHAR) {
+ SPEC_CVAL (val->etype).v_int = (char)fval;
+ } else {
+ SPEC_CVAL (val->etype).v_int = (short)fval;
+ }
}
}
return val;