1 /*----------------------------------------------------------------------
2 SDCCval.c :- has routine to do all kinds of fun stuff with the
3 value wrapper & with initialiser lists.
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
34 /*-----------------------------------------------------------------*/
35 /* newValue - allocates and returns a new value */
36 /*-----------------------------------------------------------------*/
42 val = Safe_alloc (sizeof (value));
47 /*-----------------------------------------------------------------*/
48 /* newiList - new initializer list */
49 /*-----------------------------------------------------------------*/
51 newiList (int type, void *ilist)
56 nilist = Safe_alloc (sizeof (initList));
59 nilist->lineno = mylineno;
64 nilist->init.node = (struct ast *) ilist;
68 nilist->init.deep = (struct initList *) ilist;
75 /*------------------------------------------------------------------*/
76 /* revinit - reverses the initial values for a value chain */
77 /*------------------------------------------------------------------*/
79 revinit (initList * val)
81 initList *prev, *curr, *next;
96 val->next = (void *) NULL;
101 convertIListToConstList(initList *src, literalList **lList)
104 literalList *head, *last, *newL;
108 if (!src || src->type != INIT_DEEP)
113 iLoop = src->init.deep;
117 if (iLoop->type != INIT_NODE)
122 if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node))))
129 // We've now established that the initializer list contains only literal values.
131 iLoop = src->init.deep;
134 double val = AST_LIT_VALUE(iLoop->init.node);
136 if (last && last->literalValue == val)
142 newL = Safe_alloc(sizeof(literalList));
143 newL->literalValue = val;
170 copyLiteralList(literalList *src)
172 literalList *head, *prev, *newL;
178 newL = Safe_alloc(sizeof(literalList));
180 newL->literalValue = src->literalValue;
181 newL->count = src->count;
201 /*------------------------------------------------------------------*/
202 /* copyIlist - copy initializer list */
203 /*------------------------------------------------------------------*/
205 copyIlist (initList * src)
207 initList *dest = NULL;
215 dest = newiList (INIT_DEEP, copyIlist (src->init.deep));
218 dest = newiList (INIT_NODE, copyAst (src->init.node));
223 dest->next = copyIlist (src->next);
228 /*------------------------------------------------------------------*/
229 /* list2int - converts the first element of the list to value */
230 /*------------------------------------------------------------------*/
232 list2int (initList * val)
236 if (i->type == INIT_DEEP)
237 return list2int (val->init.deep);
239 return floatFromVal (constExprValue (val->init.node, TRUE));
242 /*------------------------------------------------------------------*/
243 /* list2val - converts the first element of the list to value */
244 /*------------------------------------------------------------------*/
246 list2val (initList * val)
251 if (val->type == INIT_DEEP)
252 return list2val (val->init.deep);
254 return constExprValue (val->init.node, TRUE);
257 /*------------------------------------------------------------------*/
258 /* list2expr - returns the first expression in the initializer list */
259 /*------------------------------------------------------------------*/
261 list2expr (initList * ilist)
263 if (ilist->type == INIT_DEEP)
264 return list2expr (ilist->init.deep);
265 return ilist->init.node;
268 /*------------------------------------------------------------------*/
269 /* resolveIvalSym - resolve symbols in initial values */
270 /*------------------------------------------------------------------*/
272 resolveIvalSym (initList * ilist)
277 if (ilist->type == INIT_NODE)
278 ilist->init.node = decorateType (resolveSymbols (ilist->init.node));
280 if (ilist->type == INIT_DEEP)
281 resolveIvalSym (ilist->init.deep);
283 resolveIvalSym (ilist->next);
286 /*-----------------------------------------------------------------*/
287 /* symbolVal - creates a value for a symbol */
288 /*-----------------------------------------------------------------*/
290 symbolVal (symbol * sym)
302 val->type = sym->type;
303 val->etype = getSpec (val->type);
308 SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname);
312 SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name);
318 #if defined(REDUCE_LITERALS)
319 /*--------------------------------------------------------------------*/
320 /* cheapestVal - convert a val to the cheapest as possible value */
321 /*--------------------------------------------------------------------*/
322 static value *cheapestVal (value *val) {
326 if (IS_FLOAT(val->type) || IS_CHAR(val->type))
329 if (SPEC_LONG(val->type)) {
330 if (SPEC_USIGN(val->type)) {
331 uval=SPEC_CVAL(val->type).v_ulong;
333 sval=SPEC_CVAL(val->type).v_long;
336 if (SPEC_USIGN(val->type)) {
337 uval=SPEC_CVAL(val->type).v_uint;
339 sval=SPEC_CVAL(val->type).v_int;
343 if (SPEC_USIGN(val->type)) {
345 SPEC_LONG(val->type)=0;
346 SPEC_CVAL(val->type).v_uint = (TYPE_UWORD)uval;
348 SPEC_NOUN(val->type)=V_CHAR;
351 } else { // not unsigned
354 SPEC_LONG(val->type)=0;
355 SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
357 SPEC_NOUN(val->type)=V_CHAR;
362 SPEC_LONG(val->type)=0;
363 SPEC_CVAL(val->type).v_int = (TYPE_WORD)sval;
365 SPEC_NOUN(val->type)=V_CHAR;
375 static value *cheapestVal (value *val)
377 /* - signed/unsigned must no be changed.
378 - long must not be changed.
380 the only possible reduction is from signed int to signed char,
381 because it's automatically promoted back to signed int.
383 a reduction from unsigned int to unsigned char is a bug,
384 because an _unsigned_ char is promoted to _signed_ int! */
385 if (IS_INT(val->type) &&
386 !SPEC_USIGN(val->type) &&
387 !SPEC_LONG(val->type) &&
388 SPEC_CVAL(val->type).v_int >= -128 &&
389 SPEC_CVAL(val->type).v_int <= 127)
392 SPEC_NOUN(val->type) = V_CHAR;
394 /* this could be too aggressive:
395 'unsigned char' promotes to 'signed int', so that we can
396 reduce it the other way */
397 if (IS_INT(val->type) &&
398 !SPEC_USIGN(val->type) &&
399 !SPEC_LONG(val->type) &&
400 SPEC_CVAL(val->type).v_int >= 128 &&
401 SPEC_CVAL(val->type).v_int <= 255)
404 SPEC_NOUN(val->type) = V_CHAR;
405 SPEC_USIGN(val->type) = 1;
411 /*-----------------------------------------------------------------*/
412 /* valueFromLit - creates a value from a literal */
413 /*-----------------------------------------------------------------*/
415 valueFromLit (double lit)
419 if ((((TYPE_DWORD) lit) - lit) == 0)
421 SNPRINTF (buffer, sizeof(buffer), "%d", (TYPE_DWORD) lit);
422 return constVal (buffer);
425 SNPRINTF (buffer, sizeof(buffer), "%f", lit);
426 return constFloatVal (buffer);
429 /*-----------------------------------------------------------------*/
430 /* constFloatVal - converts a FLOAT constant to value */
431 /*-----------------------------------------------------------------*/
433 constFloatVal (char *s)
435 value *val = newValue ();
438 if (sscanf (s, "%lf", &sval) != 1)
440 werror (E_INVALID_FLOAT_CONST, s);
441 return constVal ("0");
444 val->type = val->etype = newLink (SPECIFIER);
445 SPEC_NOUN (val->type) = V_FLOAT;
446 SPEC_SCLS (val->type) = S_LITERAL;
447 SPEC_CVAL (val->type).v_float = sval;
452 /*-----------------------------------------------------------------*/
453 /* constVal - converts an INTEGER constant into a cheapest value */
454 /*-----------------------------------------------------------------*/
455 value *constVal (char *s)
458 short hex = 0, octal = 0;
463 val = newValue (); /* alloc space for value */
465 val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */
466 SPEC_SCLS (val->type) = S_LITERAL;
467 // let's start with a signed char
468 SPEC_NOUN (val->type) = V_CHAR;
469 SPEC_USIGN (val->type) = 0;
471 hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
473 /* set the octal flag */
474 if (!hex && *s == '0' && *(s + 1))
477 /* create the scan string */
478 scanFmt[scI++] = '%';
480 scanFmt[scI++] = 'l';
483 scanFmt[scI++] = 'o';
485 scanFmt[scI++] = 'x';
487 scanFmt[scI++] = 'f';
489 scanFmt[scI++] = '\0';
493 sscanf (s, scanFmt, &sval);
496 sscanf (s, scanFmt, &dval);
499 /* Setup the flags first */
500 /* set the _long flag if 'lL' is found */
501 if (strchr (s, 'l') || strchr (s, 'L')) {
502 SPEC_NOUN (val->type) = V_INT;
503 SPEC_LONG (val->type) = 1;
506 /* set the unsigned flag if 'uU' is found */
507 if (strchr (s, 'u') || strchr (s, 'U')) {
508 SPEC_USIGN (val->type) = 1;
511 if (dval<0) { // "-28u" will still be signed and negative
512 if (dval<-128) { // check if we have to promote to int
513 SPEC_NOUN (val->type) = V_INT;
515 if (dval<-32768) { // check if we have to promote to long int
516 SPEC_LONG (val->type) = 1;
519 if (dval>0xff || /* check if we have to promote to int */
520 SPEC_USIGN (val->type)) { /* if it's unsigned, we can't use unsigned
521 char. After an integral promotion it will
522 be a signed int; this certainly isn't what
523 the programer wants */
524 SPEC_NOUN (val->type) = V_INT;
526 else if (dval>0x7f && !SPEC_USIGN (val->type)) { // check if we have to promote to int
528 if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */
530 SPEC_USIGN (val->type) = 1;
532 SPEC_NOUN (val->type) = V_INT;
535 /* this is quite agressive: 'unsigned char' will be promoted to 'signed int',
536 so that the signedness of a char shouldn't matter */
537 SPEC_USIGN (val->type) = 1;
540 if (dval>0xffff && SPEC_USIGN (val->type)) { // check if we have to promote to long
541 SPEC_LONG (val->type) = 1;
543 else if (dval>0x7fff && !SPEC_USIGN (val->type)) { // check if we have to promote to long int
544 if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */
546 SPEC_USIGN (val->type) = 1;
548 SPEC_LONG (val->type) = 1;
549 if (dval>0x7fffffff) {
550 SPEC_USIGN (val->type) = 1;
556 if (SPEC_LONG (val->type))
558 if (SPEC_USIGN (val->type))
560 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD)dval;
564 SPEC_CVAL (val->type).v_long = (TYPE_DWORD)dval;
569 if (SPEC_USIGN (val->type))
571 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD)dval;
575 SPEC_CVAL (val->type).v_int = (TYPE_WORD)dval;
582 /*! /fn char hexEscape(char **src)
584 /param src Pointer to 'x' from start of hex character value
587 unsigned char hexEscape(char **src)
590 unsigned long value ;
592 (*src)++ ; /* Skip over the 'x' */
593 s = *src ; /* Save for error detection */
595 value = strtol (*src, src, 16);
598 // no valid hex found
599 werror(E_INVALID_HEX);
602 werror(W_ESC_SEQ_OOR_FOR_CHAR);
608 /*------------------------------------------------------------------*/
609 /* octalEscape - process an octal constant of max three digits */
610 /* return the octal value, throw a warning for illegal octal */
611 /* adjust src to point at the last proccesed char */
612 /*------------------------------------------------------------------*/
614 unsigned char octalEscape (char **str) {
618 for (digits=0; digits<3; digits++) {
619 if (**str>='0' && **str<='7') {
620 value = value*8 + (**str-'0');
627 if (value > 255 /* || (**str>='0' && **str<='7') */ ) {
628 werror (W_ESC_SEQ_OOR_FOR_CHAR);
635 /fn int copyStr (char *dest, char *src)
637 Copies a source string to a dest buffer interpreting escape sequences
638 and special characters
640 /param dest Buffer to receive the resultant string
641 /param src Buffer containing the source string with escape sequecnes
642 /return Number of characters in output string
647 copyStr (char *dest, char *src)
650 char *OriginalDest = dest ;
656 else if (*src == '\\')
691 *dest++ = octalEscape(&src);
696 *dest++ = hexEscape(&src) ;
723 return dest - OriginalDest ;
726 /*------------------------------------------------------------------*/
727 /* strVal - converts a string constant to a value */
728 /*------------------------------------------------------------------*/
734 val = newValue (); /* get a new one */
736 /* get a declarator */
737 val->type = newLink (DECLARATOR);
738 DCL_TYPE (val->type) = ARRAY;
739 val->type->next = val->etype = newLink (SPECIFIER);
740 SPEC_NOUN (val->etype) = V_CHAR;
741 SPEC_SCLS (val->etype) = S_LITERAL;
743 SPEC_CVAL (val->etype).v_char = Safe_alloc (strlen (s) + 1);
744 DCL_ELEM (val->type) = copyStr (SPEC_CVAL (val->etype).v_char, s);
750 /*------------------------------------------------------------------*/
751 /* reverseValWithType - reverses value chain with type & etype */
752 /*------------------------------------------------------------------*/
754 reverseValWithType (value * val)
762 /* save the type * etype chains */
766 /* set the current one 2b null */
767 val->type = val->etype = NULL;
768 val = reverseVal (val);
770 /* restore type & etype */
777 /*------------------------------------------------------------------*/
778 /* reverseVal - reverses the values for a value chain */
779 /*------------------------------------------------------------------*/
781 reverseVal (value * val)
783 value *prev, *curr, *next;
798 val->next = (void *) NULL;
802 /*------------------------------------------------------------------*/
803 /* copyValueChain - will copy a chain of values */
804 /*------------------------------------------------------------------*/
806 copyValueChain (value * src)
813 dest = copyValue (src);
814 dest->next = copyValueChain (src->next);
819 /*------------------------------------------------------------------*/
820 /* copyValue - copies contents of a value to a fresh one */
821 /*------------------------------------------------------------------*/
823 copyValue (value * src)
828 dest->sym = copySymbol (src->sym);
829 strncpyz (dest->name, src->name, SDCC_NAME_MAX);
830 dest->type = (src->type ? copyLinkChain (src->type) : NULL);
831 dest->etype = (src->type ? getSpec (dest->type) : NULL);
836 /*------------------------------------------------------------------*/
837 /* charVal - converts a character constant to a value */
838 /*------------------------------------------------------------------*/
846 val->type = val->etype = newLink (SPECIFIER);
847 SPEC_NOUN (val->type) = V_CHAR;
848 SPEC_USIGN(val->type) = 1;
849 SPEC_SCLS (val->type) = S_LITERAL;
851 s++; /* get rid of quotation */
852 /* if \ then special processing */
855 s++; /* go beyond the backslash */
859 SPEC_CVAL (val->type).v_uint = '\n';
862 SPEC_CVAL (val->type).v_uint = '\t';
865 SPEC_CVAL (val->type).v_uint = '\v';
868 SPEC_CVAL (val->type).v_uint = '\b';
871 SPEC_CVAL (val->type).v_uint = '\r';
874 SPEC_CVAL (val->type).v_uint = '\f';
877 SPEC_CVAL (val->type).v_uint = '\a';
880 SPEC_CVAL (val->type).v_uint = '\\';
883 SPEC_CVAL (val->type).v_uint = '\?';
886 SPEC_CVAL (val->type).v_uint = '\'';
889 SPEC_CVAL (val->type).v_uint = '\"';
900 SPEC_CVAL (val->type).v_uint = octalEscape(&s);
904 SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
908 SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
912 else /* not a backslash */
913 SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
918 /*------------------------------------------------------------------*/
919 /* valFromType - creates a value from type given */
920 /*------------------------------------------------------------------*/
922 valFromType (sym_link * type)
924 value *val = newValue ();
925 val->type = copyLinkChain (type);
926 val->etype = getSpec (val->type);
930 /*------------------------------------------------------------------*/
931 /* floatFromVal - value to double float conversion */
932 /*------------------------------------------------------------------*/
934 floatFromVal (value * val)
939 if (val->etype && SPEC_SCLS (val->etype) != S_LITERAL)
941 werror (E_CONST_EXPECTED, val->name);
945 /* if it is not a specifier then we can assume that */
946 /* it will be an unsigned long */
947 if (!IS_SPEC (val->type))
948 return (double) SPEC_CVAL (val->etype).v_ulong;
950 if (SPEC_NOUN (val->etype) == V_FLOAT)
951 return (double) SPEC_CVAL (val->etype).v_float;
953 if (SPEC_LONG (val->etype))
955 if (SPEC_USIGN (val->etype))
956 return (double) SPEC_CVAL (val->etype).v_ulong;
958 return (double) SPEC_CVAL (val->etype).v_long;
961 if (SPEC_NOUN (val->etype) == V_INT) {
962 if (SPEC_USIGN (val->etype))
963 return (double) SPEC_CVAL (val->etype).v_uint;
965 return (double) SPEC_CVAL (val->etype).v_int;
968 if (SPEC_NOUN (val->etype) == V_CHAR) {
969 if (SPEC_USIGN (val->etype))
970 return (double) (unsigned char)SPEC_CVAL (val->etype).v_uint;
972 return (double) (signed char)SPEC_CVAL (val->etype).v_int;
975 if (IS_BITVAR(val->etype)) {
976 return (double) SPEC_CVAL (val->etype).v_uint;
979 if (SPEC_NOUN (val->etype) == V_VOID) {
980 return (double) SPEC_CVAL (val->etype).v_ulong;
984 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
985 "floatFromVal: unknown value");
989 /*------------------------------------------------------------------*/
990 /* valUnaryPM - does the unary +/- operation on a constant */
991 /*------------------------------------------------------------------*/
993 valUnaryPM (value * val)
995 /* depending on type */
996 if (SPEC_NOUN (val->etype) == V_FLOAT)
997 SPEC_CVAL (val->etype).v_float = -1.0 * SPEC_CVAL (val->etype).v_float;
1000 if (SPEC_LONG (val->etype))
1002 if (SPEC_USIGN (val->etype))
1003 SPEC_CVAL (val->etype).v_ulong = 0-SPEC_CVAL (val->etype).v_ulong;
1005 SPEC_CVAL (val->etype).v_long = -SPEC_CVAL (val->etype).v_long;
1009 if (SPEC_USIGN (val->etype))
1010 SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint;
1012 SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int;
1015 // -(unsigned 3) now really is signed
1016 SPEC_USIGN(val->etype)=0;
1017 // -(unsigned char)135 now really is an int
1018 if (SPEC_NOUN(val->etype) == V_CHAR) {
1019 if (SPEC_CVAL(val->etype).v_int < -128) {
1020 SPEC_NOUN(val->etype) = V_INT;
1026 /*------------------------------------------------------------------*/
1027 /* valueComplement - complements a constant */
1028 /*------------------------------------------------------------------*/
1030 valComplement (value * val)
1032 /* depending on type */
1033 if (SPEC_LONG (val->etype))
1035 if (SPEC_USIGN (val->etype))
1036 SPEC_CVAL (val->etype).v_ulong = ~SPEC_CVAL (val->etype).v_ulong;
1038 SPEC_CVAL (val->etype).v_long = ~SPEC_CVAL (val->etype).v_long;
1042 if (SPEC_USIGN (val->etype))
1043 SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint;
1045 SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int;
1047 // ~(unsigned 3) now really is signed
1048 SPEC_USIGN(val->etype)=0;
1052 /*------------------------------------------------------------------*/
1053 /* valueNot - complements a constant */
1054 /*------------------------------------------------------------------*/
1056 valNot (value * val)
1058 /* depending on type */
1059 if (SPEC_LONG (val->etype))
1061 if (SPEC_USIGN (val->etype))
1062 SPEC_CVAL (val->etype).v_ulong = !SPEC_CVAL (val->etype).v_ulong;
1064 SPEC_CVAL (val->etype).v_long = !SPEC_CVAL (val->etype).v_long;
1068 if (SPEC_USIGN (val->etype))
1069 SPEC_CVAL (val->etype).v_uint = !SPEC_CVAL (val->etype).v_uint;
1071 SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int;
1076 /*------------------------------------------------------------------*/
1077 /* valMult - multiply constants */
1078 /*------------------------------------------------------------------*/
1080 valMult (value * lval, value * rval)
1084 /* create a new value */
1086 val->type = val->etype = newLink (SPECIFIER);
1087 SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
1088 IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
1089 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1090 SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
1091 /* both signed char and unsigned char are promoted to signed int */
1092 if (IS_CHAR (lval->etype))
1094 SPEC_USIGN (lval->etype) = 0;
1095 SPEC_NOUN (lval->etype) = V_INT;
1097 if (IS_CHAR (rval->etype))
1099 SPEC_USIGN (rval->etype) = 0;
1100 SPEC_NOUN (rval->etype) = V_INT;
1102 SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
1103 if (IS_FLOAT (val->type))
1104 SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval);
1105 /* signed and unsigned mul are the same, as long as the precision of the
1106 result isn't bigger than the precision of the operands. */
1107 else if (SPEC_LONG (val->type))
1108 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) *
1109 (TYPE_UDWORD) floatFromVal (rval);
1110 else if (SPEC_USIGN (val->type)) /* unsigned */
1112 TYPE_UDWORD ul = (TYPE_UWORD) floatFromVal (lval) *
1113 (TYPE_UWORD) floatFromVal (rval);
1115 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) ul;
1116 if (!options.lessPedantic &&
1117 ul != (TYPE_UWORD) ul)
1122 TYPE_DWORD l = (TYPE_WORD) floatFromVal (lval) *
1123 (TYPE_WORD) floatFromVal (rval);
1125 SPEC_CVAL (val->type).v_int = (TYPE_WORD) l;
1126 if (!options.lessPedantic &&
1130 return cheapestVal(val);
1133 /*------------------------------------------------------------------*/
1134 /* valDiv - Divide constants */
1135 /*------------------------------------------------------------------*/
1137 valDiv (value * lval, value * rval)
1141 if (floatFromVal (rval) == 0)
1143 werror (E_DIVIDE_BY_ZERO);
1147 /* create a new value */
1149 val->type = val->etype = newLink(SPECIFIER);
1150 SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
1151 IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
1152 SPEC_SCLS (val->etype) = S_LITERAL;
1153 /* both signed char and unsigned char are promoted to signed int */
1154 if (IS_CHAR (lval->etype))
1156 SPEC_USIGN (lval->etype) = 0;
1157 SPEC_NOUN (lval->etype) = V_INT;
1159 if (IS_CHAR (rval->etype))
1161 SPEC_USIGN (rval->etype) = 0;
1162 SPEC_NOUN (rval->etype) = V_INT;
1164 SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
1165 SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
1167 if (IS_FLOAT (val->type))
1168 SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval);
1171 if (SPEC_LONG (val->type))
1173 if (SPEC_USIGN (val->type))
1174 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) /
1175 (TYPE_UDWORD) floatFromVal (rval);
1177 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) /
1178 (TYPE_DWORD) floatFromVal (rval);
1182 if (SPEC_USIGN (val->type)) {
1183 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) /
1184 (TYPE_UWORD) floatFromVal (rval);
1186 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) /
1187 (TYPE_WORD) floatFromVal (rval);
1191 return cheapestVal(val);
1194 /*------------------------------------------------------------------*/
1195 /* valMod - Modulus constants */
1196 /*------------------------------------------------------------------*/
1198 valMod (value * lval, value * rval)
1202 /* create a new value */
1204 val->type = val->etype = newLink (SPECIFIER);
1205 SPEC_NOUN (val->type) = V_INT; /* type is int */
1206 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1207 SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
1208 SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
1210 if (SPEC_LONG (val->type))
1212 if (SPEC_USIGN (val->type))
1213 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) %
1214 (TYPE_UDWORD) floatFromVal (rval);
1216 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) %
1217 (TYPE_DWORD) floatFromVal (rval);
1221 if (SPEC_USIGN (val->type)) {
1222 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) %
1223 (TYPE_UWORD) floatFromVal (rval);
1225 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) %
1226 (TYPE_WORD) floatFromVal (rval);
1230 return cheapestVal(val);
1233 /*------------------------------------------------------------------*/
1234 /* valPlus - Addition constants */
1235 /*------------------------------------------------------------------*/
1237 valPlus (value * lval, value * rval)
1241 /* create a new value */
1243 val->type = val->etype = newLink (SPECIFIER);
1244 SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
1245 IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
1246 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1247 SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
1248 /* both signed char and unsigned char are promoted to signed int */
1249 if (IS_CHAR (lval->etype))
1251 SPEC_USIGN (lval->etype) = 0;
1252 SPEC_NOUN (lval->etype) = V_INT;
1254 if (IS_CHAR (rval->etype))
1256 SPEC_USIGN (rval->etype) = 0;
1257 SPEC_NOUN (rval->etype) = V_INT;
1259 SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
1260 if (IS_FLOAT (val->type))
1261 SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
1264 if (SPEC_USIGN (val->type))
1265 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) +
1266 (TYPE_UDWORD) floatFromVal (rval);
1268 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) +
1269 (TYPE_DWORD) floatFromVal (rval);
1271 return cheapestVal(val);
1274 /*------------------------------------------------------------------*/
1275 /* valMinus - Addition constants */
1276 /*------------------------------------------------------------------*/
1278 valMinus (value * lval, value * rval)
1282 /* create a new value */
1284 val->type = val->etype = newLink (SPECIFIER);
1285 SPEC_NOUN (val->type) = (IS_FLOAT (lval->etype) ||
1286 IS_FLOAT (rval->etype) ? V_FLOAT : V_INT);
1287 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1288 SPEC_LONG (val->type) = (SPEC_LONG (lval->etype) | SPEC_LONG (rval->etype));
1289 /* both signed char and unsigned char are promoted to signed int */
1290 if (IS_CHAR (lval->etype))
1292 SPEC_USIGN (lval->etype) = 0;
1293 SPEC_NOUN (lval->etype) = V_INT;
1295 if (IS_CHAR (rval->etype))
1297 SPEC_USIGN (rval->etype) = 0;
1298 SPEC_NOUN (rval->etype) = V_INT;
1300 SPEC_USIGN (val->type) = (SPEC_USIGN (lval->etype) | SPEC_USIGN (rval->etype));
1301 if (IS_FLOAT (val->type))
1302 SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
1305 if (SPEC_USIGN (val->type))
1306 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) -
1307 (TYPE_UDWORD) floatFromVal (rval);
1309 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) -
1310 (TYPE_DWORD) floatFromVal (rval);
1312 return cheapestVal(val);
1315 /*------------------------------------------------------------------*/
1316 /* valShift - Shift left or right */
1317 /*------------------------------------------------------------------*/
1319 valShift (value * lval, value * rval, int lr)
1323 /* create a new value */
1325 val->type = val->etype = newIntLink ();
1326 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1327 SPEC_NOUN (val->etype) = V_INT;
1328 /* 'unsigned char' promotes to 'signed int' */
1329 if (!IS_CHAR (lval->etype))
1330 SPEC_USIGN (val->type) = SPEC_USIGN (lval->etype);
1331 SPEC_LONG (val->type) = SPEC_LONG (lval->etype);
1333 if (SPEC_USIGN (val->type))
1335 SPEC_CVAL (val->type).v_ulong = lr ?
1336 (TYPE_UDWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1337 (TYPE_UDWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1341 SPEC_CVAL (val->type).v_long = lr ?
1342 (TYPE_DWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1343 (TYPE_DWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1345 return cheapestVal(val);
1348 /*------------------------------------------------------------------*/
1349 /* valCompare- Compares two literal */
1350 /*------------------------------------------------------------------*/
1352 valCompare (value * lval, value * rval, int ctype)
1356 /* create a new value */
1358 val->type = val->etype = newCharLink ();
1359 val->type->class = SPECIFIER;
1360 SPEC_NOUN (val->type) = V_CHAR; /* type is char */
1361 SPEC_USIGN (val->type) = 1;
1362 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1367 SPEC_CVAL (val->type).v_int = floatFromVal (lval) < floatFromVal (rval);
1371 SPEC_CVAL (val->type).v_int = floatFromVal (lval) > floatFromVal (rval);
1375 SPEC_CVAL (val->type).v_int = floatFromVal (lval) <= floatFromVal (rval);
1379 SPEC_CVAL (val->type).v_int = floatFromVal (lval) >= floatFromVal (rval);
1383 if (SPEC_NOUN(lval->type) == V_FLOAT ||
1384 SPEC_NOUN(rval->type) == V_FLOAT)
1386 SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval);
1390 /* integrals: ignore signedness */
1393 l = (TYPE_UDWORD) floatFromVal (lval);
1394 if (SPEC_NOUN(lval->type) == V_CHAR)
1395 l &= 0xffff; /* promote to int */
1396 else if (!SPEC_LONG (lval->type))
1399 r = (TYPE_UDWORD) floatFromVal (rval);
1400 if (SPEC_NOUN(rval->type) == V_CHAR)
1401 r &= 0xffff; /* promote to int */
1402 else if (!SPEC_LONG (rval->type))
1405 SPEC_CVAL (val->type).v_int = l == r;
1409 if (SPEC_NOUN(lval->type) == V_FLOAT ||
1410 SPEC_NOUN(rval->type) == V_FLOAT)
1412 SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval);
1416 /* integrals: ignore signedness */
1419 l = (TYPE_UDWORD) floatFromVal (lval);
1420 if (SPEC_NOUN(lval->type) == V_CHAR)
1421 l &= 0xffff; /* promote to int */
1422 else if (!SPEC_LONG (lval->type))
1425 r = (TYPE_UDWORD) floatFromVal (rval);
1426 if (SPEC_NOUN(rval->type) == V_CHAR)
1427 r &= 0xffff; /* promote to int */
1428 else if (!SPEC_LONG (rval->type))
1431 SPEC_CVAL (val->type).v_int = l != r;
1440 /*------------------------------------------------------------------*/
1441 /* valBitwise - Bitwise operation */
1442 /*------------------------------------------------------------------*/
1444 valBitwise (value * lval, value * rval, int op)
1448 /* create a new value */
1450 val->type = copyLinkChain (getSize(rval->type) > getSize(lval->type) ?
1451 rval->type : lval->type);
1452 val->etype = getSpec (val->type);
1457 if (SPEC_LONG (val->type))
1459 if (SPEC_USIGN (val->type))
1460 SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) &
1461 (unsigned long) floatFromVal (rval);
1463 SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) &
1464 (long) floatFromVal (rval);
1468 if (SPEC_USIGN (val->type))
1469 SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) &
1470 (unsigned) floatFromVal (rval);
1472 SPEC_CVAL (val->type).v_int = (int) floatFromVal (lval) & (int) floatFromVal (rval);
1477 if (SPEC_LONG (val->type))
1479 if (SPEC_USIGN (val->type))
1480 SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) |
1481 (unsigned long) floatFromVal (rval);
1483 SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) |
1484 (long) floatFromVal (rval);
1488 if (SPEC_USIGN (val->type))
1489 SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) |
1490 (unsigned) floatFromVal (rval);
1492 SPEC_CVAL (val->type).v_int =
1493 (int) floatFromVal (lval) | (int) floatFromVal (rval);
1499 if (SPEC_LONG (val->type))
1501 if (SPEC_USIGN (val->type))
1502 SPEC_CVAL (val->type).v_ulong = (unsigned long) floatFromVal (lval) ^
1503 (unsigned long) floatFromVal (rval);
1505 SPEC_CVAL (val->type).v_long = (long) floatFromVal (lval) ^
1506 (long) floatFromVal (rval);
1510 if (SPEC_USIGN (val->type))
1511 SPEC_CVAL (val->type).v_uint = (unsigned) floatFromVal (lval) ^
1512 (unsigned) floatFromVal (rval);
1514 SPEC_CVAL (val->type).v_int =
1515 (int) floatFromVal (lval) ^ (int) floatFromVal (rval);
1520 return cheapestVal(val);
1523 /*------------------------------------------------------------------*/
1524 /* valAndOr - Generates code for and / or operation */
1525 /*------------------------------------------------------------------*/
1527 valLogicAndOr (value * lval, value * rval, int op)
1531 /* create a new value */
1533 val->type = val->etype = newCharLink ();
1534 val->type->class = SPECIFIER;
1535 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1536 SPEC_USIGN (val->type) = 0;
1541 SPEC_CVAL (val->type).v_int = floatFromVal (lval) && floatFromVal (rval);
1545 SPEC_CVAL (val->type).v_int = floatFromVal (lval) || floatFromVal (rval);
1553 /*------------------------------------------------------------------*/
1554 /* valCastLiteral - casts a literal value to another type */
1555 /*------------------------------------------------------------------*/
1557 valCastLiteral (sym_link * dtype, double fval)
1560 TYPE_UDWORD l = (TYPE_UDWORD)fval;
1566 val->etype = getSpec (val->type = copyLinkChain (dtype));
1567 SPEC_SCLS (val->etype) = S_LITERAL;
1569 /* if it is not a specifier then we can assume that */
1570 /* it will be an unsigned long */
1571 if (!IS_SPEC (val->type)) {
1572 SPEC_CVAL (val->etype).v_ulong = l;
1576 if (SPEC_NOUN (val->etype) == V_FLOAT)
1577 SPEC_CVAL (val->etype).v_float = fval;
1578 else if (SPEC_NOUN (val->etype) == V_CHAR) {
1579 if (SPEC_USIGN (val->etype))
1580 SPEC_CVAL (val->etype).v_uint= (TYPE_UBYTE) l;
1582 SPEC_CVAL (val->etype).v_int = (TYPE_BYTE) l;
1584 if (SPEC_LONG (val->etype)) {
1585 if (SPEC_USIGN (val->etype))
1586 SPEC_CVAL (val->etype).v_ulong = (TYPE_UDWORD) l;
1588 SPEC_CVAL (val->etype).v_long = (TYPE_DWORD) l;
1590 if (SPEC_USIGN (val->etype))
1591 SPEC_CVAL (val->etype).v_uint = (TYPE_UWORD)l;
1593 SPEC_CVAL (val->etype).v_int = (TYPE_WORD)l;
1599 /*------------------------------------------------------------------*/
1600 /* getNelements - determines # of elements from init list */
1601 /*------------------------------------------------------------------*/
1603 getNelements (sym_link * type, initList * ilist)
1610 if (ilist->type == INIT_DEEP)
1611 ilist = ilist->init.deep;
1613 /* if type is a character array and there is only one
1614 (string) initialiser then get the length of the string */
1615 if (IS_ARRAY (type) && IS_CHAR (type->next) && !ilist->next)
1617 ast *iast = ilist->init.node;
1618 value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL);
1621 werror (E_CONST_EXPECTED);
1625 if (IS_ARRAY (v->type) && IS_CHAR (v->etype))
1626 // yep, it's a string
1628 return DCL_ELEM (v->type);
1636 ilist = ilist->next;
1641 /*-----------------------------------------------------------------*/
1642 /* valForArray - returns a value with name of array index */
1643 /*-----------------------------------------------------------------*/
1645 valForArray (ast * arrExpr)
1647 value *val, *lval = NULL;
1649 int size = getSize (arrExpr->left->ftype->next);
1650 /* if the right or left is an array
1652 if (IS_AST_OP (arrExpr->left))
1654 if (arrExpr->left->opval.op == '[')
1655 lval = valForArray (arrExpr->left);
1656 else if (arrExpr->left->opval.op == '.')
1657 lval = valForStructElem (arrExpr->left->left,
1658 arrExpr->left->right);
1659 else if (arrExpr->left->opval.op == PTR_OP &&
1660 IS_ADDRESS_OF_OP (arrExpr->left->left))
1661 lval = valForStructElem (arrExpr->left->left->left,
1662 arrExpr->left->right);
1667 else if (!IS_AST_SYM_VALUE (arrExpr->left))
1670 if (!IS_AST_LIT_VALUE (arrExpr->right))
1676 SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname);
1680 SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
1683 SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
1684 (int) AST_LIT_VALUE (arrExpr->right) * size);
1686 val->type = newLink (DECLARATOR);
1687 if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
1688 DCL_TYPE (val->type) = CPOINTER;
1689 else if (SPEC_SCLS (arrExpr->left->etype) == S_XDATA)
1690 DCL_TYPE (val->type) = FPOINTER;
1691 else if (SPEC_SCLS (arrExpr->left->etype) == S_XSTACK)
1692 DCL_TYPE (val->type) = PPOINTER;
1693 else if (SPEC_SCLS (arrExpr->left->etype) == S_IDATA)
1694 DCL_TYPE (val->type) = IPOINTER;
1695 else if (SPEC_SCLS (arrExpr->left->etype) == S_EEPROM)
1696 DCL_TYPE (val->type) = EEPPOINTER;
1698 DCL_TYPE (val->type) = POINTER;
1699 val->type->next = arrExpr->left->ftype;
1700 val->etype = getSpec (val->type);
1704 /*-----------------------------------------------------------------*/
1705 /* valForStructElem - returns value with name of struct element */
1706 /*-----------------------------------------------------------------*/
1708 valForStructElem (ast * structT, ast * elemT)
1710 value *val, *lval = NULL;
1714 /* left could be furthur derefed */
1715 if (IS_AST_OP (structT))
1717 if (structT->opval.op == '[')
1718 lval = valForArray (structT);
1719 else if (structT->opval.op == '.')
1720 lval = valForStructElem (structT->left, structT->right);
1721 else if (structT->opval.op == PTR_OP &&
1722 IS_ADDRESS_OF_OP (structT->left))
1723 lval = valForStructElem (structT->left->left,
1729 if (!IS_AST_SYM_VALUE (elemT))
1732 if (!IS_STRUCT (structT->etype))
1735 if ((sym = getStructElement (SPEC_STRUCT (structT->etype),
1736 AST_SYMBOL (elemT))) == NULL)
1744 SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname);
1748 SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
1751 SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
1754 val->type = newLink (DECLARATOR);
1755 if (SPEC_SCLS (structT->etype) == S_CODE)
1756 DCL_TYPE (val->type) = CPOINTER;
1757 else if (SPEC_SCLS (structT->etype) == S_XDATA)
1758 DCL_TYPE (val->type) = FPOINTER;
1759 else if (SPEC_SCLS (structT->etype) == S_XSTACK)
1760 DCL_TYPE (val->type) = PPOINTER;
1761 else if (SPEC_SCLS (structT->etype) == S_IDATA)
1762 DCL_TYPE (val->type) = IPOINTER;
1763 else if (SPEC_SCLS (structT->etype) == S_EEPROM)
1764 DCL_TYPE (val->type) = EEPPOINTER;
1766 DCL_TYPE (val->type) = POINTER;
1767 val->type->next = sym->type;
1768 val->etype = getSpec (val->type);
1772 /*-----------------------------------------------------------------*/
1773 /* valForCastAggr - will return value for a cast of an aggregate */
1774 /* plus minus a constant */
1775 /*-----------------------------------------------------------------*/
1777 valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op)
1781 if (!IS_AST_SYM_VALUE (aexpr))
1783 if (!IS_AST_LIT_VALUE (cnst))
1788 SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
1789 AST_SYMBOL (aexpr)->rname, op,
1790 getSize (type->next) * (int) AST_LIT_VALUE (cnst));
1793 val->etype = getSpec (val->type);
1797 /*-----------------------------------------------------------------*/
1798 /* valForCastAggr - will return value for a cast of an aggregate */
1799 /* with no constant */
1800 /*-----------------------------------------------------------------*/
1802 valForCastArr (ast * aexpr, sym_link * type)
1806 if (!IS_AST_SYM_VALUE (aexpr))
1811 SNPRINTF (val->name, sizeof(val->name), "(%s)",
1812 AST_SYMBOL (aexpr)->rname);
1815 val->etype = getSpec (val->type);