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 -------------------------------------------------------------------------*/
35 /*-----------------------------------------------------------------*/
36 /* newValue - allocates and returns a new value */
37 /*-----------------------------------------------------------------*/
43 val = Safe_alloc (sizeof (value));
48 /*-----------------------------------------------------------------*/
49 /* newiList - new initializer list */
50 /*-----------------------------------------------------------------*/
52 newiList (int type, void *ilist)
57 nilist = Safe_alloc (sizeof (initList));
60 nilist->lineno = mylineno;
61 nilist->filename = currFname;
66 nilist->init.node = (struct ast *) ilist;
70 nilist->init.deep = (struct initList *) ilist;
77 /*------------------------------------------------------------------*/
78 /* revinit - reverses the initial values for a value chain */
79 /*------------------------------------------------------------------*/
81 revinit (initList * val)
83 initList *prev, *curr, *next;
98 val->next = (void *) NULL;
103 convertIListToConstList(initList *src, literalList **lList)
106 literalList *head, *last, *newL;
110 if (!src || src->type != INIT_DEEP)
115 iLoop = src->init.deep;
119 if (iLoop->type != INIT_NODE)
124 if (!IS_AST_LIT_VALUE(decorateType(resolveSymbols(iLoop->init.node), RESULT_TYPE_NONE)))
131 // We've now established that the initializer list contains only literal values.
133 iLoop = src->init.deep;
136 double val = AST_LIT_VALUE(iLoop->init.node);
138 if (last && last->literalValue == val)
144 newL = Safe_alloc(sizeof(literalList));
145 newL->literalValue = val;
172 copyLiteralList(literalList *src)
174 literalList *head, *prev, *newL;
180 newL = Safe_alloc(sizeof(literalList));
182 newL->literalValue = src->literalValue;
183 newL->count = src->count;
203 /*------------------------------------------------------------------*/
204 /* copyIlist - copy initializer list */
205 /*------------------------------------------------------------------*/
207 copyIlist (initList * src)
209 initList *dest = NULL;
217 dest = newiList (INIT_DEEP, copyIlist (src->init.deep));
220 dest = newiList (INIT_NODE, copyAst (src->init.node));
225 dest->next = copyIlist (src->next);
230 /*------------------------------------------------------------------*/
231 /* list2int - converts the first element of the list to value */
232 /*------------------------------------------------------------------*/
234 list2int (initList * val)
238 if (i->type == INIT_DEEP)
239 return list2int (val->init.deep);
241 return floatFromVal (constExprValue (val->init.node, TRUE));
244 /*------------------------------------------------------------------*/
245 /* list2val - converts the first element of the list to value */
246 /*------------------------------------------------------------------*/
248 list2val (initList * val)
253 if (val->type == INIT_DEEP)
254 return list2val (val->init.deep);
256 return constExprValue (val->init.node, TRUE);
259 /*------------------------------------------------------------------*/
260 /* list2expr - returns the first expression in the initializer list */
261 /*------------------------------------------------------------------*/
263 list2expr (initList * ilist)
265 if (ilist->type == INIT_DEEP)
266 return list2expr (ilist->init.deep);
267 return ilist->init.node;
270 /*------------------------------------------------------------------*/
271 /* resolveIvalSym - resolve symbols in initial values */
272 /*------------------------------------------------------------------*/
274 resolveIvalSym (initList * ilist, sym_link * type)
276 RESULT_TYPE resultType;
281 if (ilist->type == INIT_NODE)
284 resultType = RESULT_TYPE_INT;
286 resultType = getResultTypeFromType (getSpec (type));
287 ilist->init.node = decorateType (resolveSymbols (ilist->init.node),
291 if (ilist->type == INIT_DEEP)
292 resolveIvalSym (ilist->init.deep, type);
294 resolveIvalSym (ilist->next, type);
297 /*-----------------------------------------------------------------*/
298 /* symbolVal - creates a value for a symbol */
299 /*-----------------------------------------------------------------*/
301 symbolVal (symbol * sym)
313 val->type = sym->type;
314 val->etype = getSpec (val->type);
319 SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname);
323 SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name);
329 /*--------------------------------------------------------------------*/
330 /* cheapestVal - try to reduce 'signed int' to 'char' */
331 /*--------------------------------------------------------------------*/
333 cheapestVal (value *val)
335 /* only int can be reduced */
336 if (!IS_INT(val->type))
339 /* long must not be changed */
340 if (SPEC_LONG(val->type))
343 /* unsigned must not be changed */
344 if (SPEC_USIGN(val->type))
347 /* the only possible reduction is from signed int to (un)signed char,
348 because it's automatically promoted back to signed int.
350 a reduction from unsigned int to unsigned char is a bug,
351 because an _unsigned_ char is promoted to _signed_ int! */
352 if (SPEC_CVAL(val->type).v_int < -128 ||
353 SPEC_CVAL(val->type).v_int > 255)
355 /* not in the range of (un)signed char */
359 SPEC_NOUN(val->type) = V_CHAR;
361 /* 'unsigned char' promotes to 'signed int', so that we can
362 reduce it the other way */
363 if (SPEC_CVAL(val->type).v_int >= 0)
365 SPEC_USIGN(val->type) = 1;
370 /*--------------------------------------------------------------------*/
371 /* checkConstantRange - check if constant fits in numeric range of */
372 /* var type in comparisons and assignments */
373 /*--------------------------------------------------------------------*/
375 checkConstantRange (sym_link *var, sym_link *lit, int op, bool exchangeLeftRight)
381 litVal = floatFromVal (valFromType (lit));
382 varBits = bitsForType (var);
389 return CCR_ALWAYS_FALSE;
391 return CCR_ALWAYS_TRUE;
393 /* special: assignment */
399 if (getenv ("SDCC_VERY_PEDANTIC"))
401 if (SPEC_USIGN (var))
403 TYPE_UDWORD maxVal = 0xffffffffu >> (32 - varBits);
412 TYPE_DWORD minVal = 0xffffffff << (varBits - 1);
413 TYPE_DWORD maxVal = 0x7fffffff >> (32 - varBits);
423 /* ignore signedness, e.g. allow everything
424 from -127...+255 for (unsigned) char */
425 TYPE_DWORD minVal = 0xffffffff << (varBits - 1);
426 TYPE_UDWORD maxVal = 0xffffffffu >> (32 - varBits);
435 if (exchangeLeftRight)
440 case '>': op = '<'; break;
441 case GE_OP: op = LE_OP; break;
442 case '<': op = '>'; break;
443 case LE_OP: op = GE_OP; break;
444 default: return CCR_ALWAYS_FALSE;
447 reType = computeType (var, lit, RESULT_TYPE_NONE, op);
449 if (SPEC_USIGN (reType))
451 /* unsigned operation */
452 TYPE_UDWORD minValP, maxValP, minValM, maxValM;
453 TYPE_UDWORD opBitsMask = 0xffffffffu >> (32 - bitsForType (reType));
455 if (SPEC_USIGN (lit) && SPEC_USIGN (var))
457 /* both operands are unsigned, this is easy */
459 maxValP = 0xffffffffu >> (32 - varBits);
460 /* there's only range, just copy it to 2nd set */
464 else if (SPEC_USIGN (var))
466 /* lit is casted from signed to unsigned, e.g.:
472 maxValP = 0xffffffffu >> (32 - varBits);
473 /* there's only one range, just copy it to 2nd set */
477 /* it's an unsigned operation */
478 if ( IS_CHAR (reType)
481 /* make signed literal unsigned and
482 limit no of bits to size of return type */
483 litVal = (TYPE_UDWORD) litVal & opBitsMask;
486 else /* SPEC_USIGN (lit) */
488 /* var is casted from signed to unsigned, e.g.:
493 The possible values after casting var
494 split up in two, nonconsecutive ranges:
496 minValP = 0; positive range: 0...127
498 minValM = 0xff80; negative range: -128...-1
504 maxValP = 0x7fffffffu >> (32 - varBits);
507 minValM = 0xffffffff << (varBits - 1);
508 maxValM = 0xffffffffu; /* -1 */
509 /* limit no of bits to size of return type */
510 minValM &= opBitsMask;
511 maxValM &= opBitsMask;
516 case EQ_OP: /* var == lit */
517 if ( litVal <= maxValP
518 && litVal >= minValP) /* 0 */
520 if ( litVal <= maxValM
521 && litVal >= minValM)
523 return CCR_ALWAYS_FALSE;
524 case NE_OP: /* var != lit */
525 if ( litVal <= maxValP
526 && litVal >= minValP) /* 0 */
528 if ( litVal <= maxValM
529 && litVal >= minValM)
531 return CCR_ALWAYS_TRUE;
532 case '>': /* var > lit */
533 if (litVal >= maxValM)
534 return CCR_ALWAYS_FALSE;
535 if (litVal < minValP) /* 0 */
536 return CCR_ALWAYS_TRUE;
538 case GE_OP: /* var >= lit */
539 if (litVal > maxValM)
540 return CCR_ALWAYS_FALSE;
541 if (litVal <= minValP) /* 0 */
542 return CCR_ALWAYS_TRUE;
544 case '<': /* var < lit */
545 if (litVal > maxValM)
546 return CCR_ALWAYS_TRUE;
547 if (litVal <= minValP) /* 0 */
548 return CCR_ALWAYS_FALSE;
550 case LE_OP: /* var <= lit */
551 if (litVal >= maxValM)
552 return CCR_ALWAYS_TRUE;
553 if (litVal < minValP) /* 0 */
554 return CCR_ALWAYS_FALSE;
557 return CCR_ALWAYS_FALSE;
562 /* signed operation */
563 TYPE_DWORD minVal, maxVal;
565 if (SPEC_USIGN (var))
567 /* unsigned var, but signed operation. This happens
568 when var is promoted to signed int.
569 Set actual min/max values of var. */
571 maxVal = 0xffffffff >> (32 - varBits);
576 minVal = 0xffffffff << (varBits - 1);
577 maxVal = 0x7fffffff >> (32 - varBits);
582 case EQ_OP: /* var == lit */
585 return CCR_ALWAYS_FALSE;
587 case NE_OP: /* var != lit */
590 return CCR_ALWAYS_TRUE;
592 case '>': /* var > lit */
593 if (litVal >= maxVal)
594 return CCR_ALWAYS_FALSE;
596 return CCR_ALWAYS_TRUE;
598 case GE_OP: /* var >= lit */
600 return CCR_ALWAYS_FALSE;
601 if (litVal <= minVal)
602 return CCR_ALWAYS_TRUE;
604 case '<': /* var < lit */
606 return CCR_ALWAYS_TRUE;
607 if (litVal <= minVal)
608 return CCR_ALWAYS_FALSE;
610 case LE_OP: /* var <= lit */
611 if (litVal >= maxVal)
612 return CCR_ALWAYS_TRUE;
614 return CCR_ALWAYS_FALSE;
617 return CCR_ALWAYS_FALSE;
622 /*-----------------------------------------------------------------*/
623 /* valueFromLit - creates a value from a literal */
624 /*-----------------------------------------------------------------*/
626 valueFromLit (double lit)
630 if ((((TYPE_DWORD) lit) - lit) == 0)
632 SNPRINTF (buffer, sizeof(buffer), "%d", (TYPE_DWORD) lit);
633 return constVal (buffer);
636 SNPRINTF (buffer, sizeof(buffer), "%f", lit);
637 return constFloatVal (buffer);
640 /*-----------------------------------------------------------------*/
641 /* constFloatVal - converts a FLOAT constant to value */
642 /*-----------------------------------------------------------------*/
644 constFloatVal (char *s)
646 value *val = newValue ();
649 if (sscanf (s, "%lf", &sval) != 1)
651 werror (E_INVALID_FLOAT_CONST, s);
652 return constVal ("0");
655 val->type = val->etype = newLink (SPECIFIER);
656 SPEC_NOUN (val->type) = V_FLOAT;
657 SPEC_SCLS (val->type) = S_LITERAL;
658 SPEC_CVAL (val->type).v_float = sval;
663 /*-----------------------------------------------------------------*/
664 /* constFixed16x16Val - converts a FIXED16X16 constant to value */
665 /*-----------------------------------------------------------------*/
667 constFixed16x16Val (char *s)
669 value *val = newValue ();
672 if (sscanf (s, "%lf", &sval) != 1)
674 werror (E_INVALID_FLOAT_CONST, s);
675 return constVal ("0");
678 val->type = val->etype = newLink (SPECIFIER);
679 SPEC_NOUN (val->type) = V_FLOAT;
680 SPEC_SCLS (val->type) = S_LITERAL;
681 SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble ( sval );
686 /*-----------------------------------------------------------------*/
687 /* constVal - converts an INTEGER constant into a cheapest value */
688 /*-----------------------------------------------------------------*/
689 value *constVal (char *s)
692 short hex = 0, octal = 0;
695 val = newValue (); /* alloc space for value */
697 val->type = val->etype = newLink (SPECIFIER); /* create the spcifier */
698 SPEC_SCLS (val->type) = S_LITERAL;
699 // let's start with a signed char
700 SPEC_NOUN (val->type) = V_CHAR;
701 SPEC_USIGN (val->type) = 0;
703 hex = ((strchr (s, 'x') || strchr (s, 'X')) ? 1 : 0);
705 /* set the octal flag */
706 if (!hex && *s == '0' && *(s + 1))
712 sval = strtoul (s, NULL, 0);
716 werror (W_INVALID_INT_CONST, s, dval);
719 sscanf (s, "%lf", &dval);
722 /* Setup the flags first */
723 /* set the unsigned flag if 'uU' is found */
724 if (strchr (s, 'u') || strchr (s, 'U')) {
725 SPEC_USIGN (val->type) = 1;
728 /* set the b_long flag if 'lL' is found */
729 if (strchr (s, 'l') || strchr (s, 'L')) {
730 SPEC_NOUN (val->type) = V_INT;
731 SPEC_LONG (val->type) = 1;
733 if (dval<0) { // "-28u" will still be signed and negative
734 if (dval<-128) { // check if we have to promote to int
735 SPEC_NOUN (val->type) = V_INT;
737 if (dval<-32768) { // check if we have to promote to long int
738 SPEC_LONG (val->type) = 1;
741 if (dval>0xff || /* check if we have to promote to int */
742 SPEC_USIGN (val->type)) { /* if it's unsigned, we can't use unsigned
743 char. After an integral promotion it will
744 be a signed int; this certainly isn't what
745 the programer wants */
746 SPEC_NOUN (val->type) = V_INT;
748 else { /* store char's always as unsigned; this helps other optimizations */
749 SPEC_USIGN (val->type) = 1;
751 if (dval>0xffff && SPEC_USIGN (val->type)) { // check if we have to promote to long
752 SPEC_LONG (val->type) = 1;
754 else if (dval>0x7fff && !SPEC_USIGN (val->type)) { // check if we have to promote to long int
755 if ((hex || octal) && /* hex or octal constants may be stored in unsigned type */
757 SPEC_USIGN (val->type) = 1;
759 SPEC_LONG (val->type) = 1;
760 if (dval>0x7fffffff) {
761 SPEC_USIGN (val->type) = 1;
768 /* check for out of range */
769 if (dval<-2147483648.0) {
770 dval = -2147483648.0;
771 werror (W_INVALID_INT_CONST, s, dval);
773 if (dval>2147483647.0 && !SPEC_USIGN (val->type)) {
775 werror (W_INVALID_INT_CONST, s, dval);
777 if (dval>4294967295.0) {
779 werror (W_INVALID_INT_CONST, s, dval);
782 if (SPEC_LONG (val->type))
784 if (SPEC_USIGN (val->type))
786 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD)dval;
790 SPEC_CVAL (val->type).v_long = (TYPE_DWORD)dval;
795 if (SPEC_USIGN (val->type))
797 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD)dval;
801 SPEC_CVAL (val->type).v_int = (TYPE_WORD)dval;
808 /*! /fn char hexEscape(char **src)
810 /param src Pointer to 'x' from start of hex character value
813 unsigned char hexEscape(char **src)
816 unsigned long value ;
818 (*src)++ ; /* Skip over the 'x' */
819 s = *src ; /* Save for error detection */
821 value = strtol (*src, src, 16);
824 // no valid hex found
825 werror(E_INVALID_HEX);
828 werror(W_ESC_SEQ_OOR_FOR_CHAR);
834 /*------------------------------------------------------------------*/
835 /* octalEscape - process an octal constant of max three digits */
836 /* return the octal value, throw a warning for illegal octal */
837 /* adjust src to point at the last proccesed char */
838 /*------------------------------------------------------------------*/
840 unsigned char octalEscape (char **str) {
844 for (digits=0; digits<3; digits++) {
845 if (**str>='0' && **str<='7') {
846 value = value*8 + (**str-'0');
853 if (value > 255 /* || (**str>='0' && **str<='7') */ ) {
854 werror (W_ESC_SEQ_OOR_FOR_CHAR);
861 /fn int copyStr (char *dest, char *src)
863 Copies a source string to a dest buffer interpreting escape sequences
864 and special characters
866 /param dest Buffer to receive the resultant string
867 /param src Buffer containing the source string with escape sequecnes
868 /return Number of characters in output string
873 copyStr (char *dest, char *src)
876 char *OriginalDest = dest ;
882 else if (*src == '\\')
917 *dest++ = octalEscape(&src);
922 *dest++ = hexEscape(&src) ;
949 return dest - OriginalDest ;
952 /*------------------------------------------------------------------*/
953 /* strVal - converts a string constant to a value */
954 /*------------------------------------------------------------------*/
960 val = newValue (); /* get a new one */
962 /* get a declarator */
963 val->type = newLink (DECLARATOR);
964 DCL_TYPE (val->type) = ARRAY;
965 val->type->next = val->etype = newLink (SPECIFIER);
966 SPEC_NOUN (val->etype) = V_CHAR;
967 SPEC_SCLS (val->etype) = S_LITERAL;
969 SPEC_CVAL (val->etype).v_char = Safe_alloc (strlen (s) + 1);
970 DCL_ELEM (val->type) = copyStr (SPEC_CVAL (val->etype).v_char, s);
976 /*------------------------------------------------------------------*/
977 /* reverseValWithType - reverses value chain with type & etype */
978 /*------------------------------------------------------------------*/
980 reverseValWithType (value * val)
988 /* save the type * etype chains */
992 /* set the current one 2b null */
993 val->type = val->etype = NULL;
994 val = reverseVal (val);
996 /* restore type & etype */
1003 /*------------------------------------------------------------------*/
1004 /* reverseVal - reverses the values for a value chain */
1005 /*------------------------------------------------------------------*/
1007 reverseVal (value * val)
1009 value *prev, *curr, *next;
1024 val->next = (void *) NULL;
1028 /*------------------------------------------------------------------*/
1029 /* copyValueChain - will copy a chain of values */
1030 /*------------------------------------------------------------------*/
1032 copyValueChain (value * src)
1039 dest = copyValue (src);
1040 dest->next = copyValueChain (src->next);
1045 /*------------------------------------------------------------------*/
1046 /* copyValue - copies contents of a value to a fresh one */
1047 /*------------------------------------------------------------------*/
1049 copyValue (value * src)
1054 dest->sym = copySymbol (src->sym);
1055 strncpyz (dest->name, src->name, SDCC_NAME_MAX);
1056 dest->type = (src->type ? copyLinkChain (src->type) : NULL);
1057 dest->etype = (src->type ? getSpec (dest->type) : NULL);
1062 /*------------------------------------------------------------------*/
1063 /* charVal - converts a character constant to a value */
1064 /*------------------------------------------------------------------*/
1072 val->type = val->etype = newLink (SPECIFIER);
1073 SPEC_NOUN (val->type) = V_CHAR;
1074 SPEC_USIGN(val->type) = 1;
1075 SPEC_SCLS (val->type) = S_LITERAL;
1077 s++; /* get rid of quotation */
1078 /* if \ then special processing */
1081 s++; /* go beyond the backslash */
1085 SPEC_CVAL (val->type).v_uint = '\n';
1088 SPEC_CVAL (val->type).v_uint = '\t';
1091 SPEC_CVAL (val->type).v_uint = '\v';
1094 SPEC_CVAL (val->type).v_uint = '\b';
1097 SPEC_CVAL (val->type).v_uint = '\r';
1100 SPEC_CVAL (val->type).v_uint = '\f';
1103 SPEC_CVAL (val->type).v_uint = '\a';
1106 SPEC_CVAL (val->type).v_uint = '\\';
1109 SPEC_CVAL (val->type).v_uint = '\?';
1112 SPEC_CVAL (val->type).v_uint = '\'';
1115 SPEC_CVAL (val->type).v_uint = '\"';
1126 SPEC_CVAL (val->type).v_uint = octalEscape(&s);
1130 SPEC_CVAL (val->type).v_uint = hexEscape(&s) ;
1134 SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
1138 else /* not a backslash */
1139 SPEC_CVAL (val->type).v_uint = (unsigned char)*s;
1144 /*------------------------------------------------------------------*/
1145 /* valFromType - creates a value from type given */
1146 /*------------------------------------------------------------------*/
1148 valFromType (sym_link * type)
1150 value *val = newValue ();
1151 val->type = copyLinkChain (type);
1152 val->etype = getSpec (val->type);
1156 /*------------------------------------------------------------------*/
1157 /* floatFromVal - value to double float conversion */
1158 /*------------------------------------------------------------------*/
1160 floatFromVal (value * val)
1165 if (val->etype && SPEC_SCLS (val->etype) != S_LITERAL)
1167 werror (E_CONST_EXPECTED, val->name);
1171 /* if it is not a specifier then we can assume that */
1172 /* it will be an unsigned long */
1173 if (!IS_SPEC (val->type))
1174 return (double) SPEC_CVAL (val->etype).v_ulong;
1176 if (SPEC_NOUN (val->etype) == V_FLOAT)
1177 return (double) SPEC_CVAL (val->etype).v_float;
1179 if (SPEC_NOUN (val->etype) == V_FIXED16X16)
1180 return (double) doubleFromFixed16x16( SPEC_CVAL (val->etype).v_fixed16x16 );
1182 if (SPEC_LONG (val->etype))
1184 if (SPEC_USIGN (val->etype))
1185 return (double) SPEC_CVAL (val->etype).v_ulong;
1187 return (double) SPEC_CVAL (val->etype).v_long;
1190 if (SPEC_NOUN (val->etype) == V_INT) {
1191 if (SPEC_USIGN (val->etype))
1192 return (double) SPEC_CVAL (val->etype).v_uint;
1194 return (double) SPEC_CVAL (val->etype).v_int;
1197 if (SPEC_NOUN (val->etype) == V_CHAR) {
1198 if (SPEC_USIGN (val->etype))
1199 return (double) (unsigned char)SPEC_CVAL (val->etype).v_uint;
1201 return (double) (signed char)SPEC_CVAL (val->etype).v_int;
1204 if (IS_BITVAR(val->etype)) {
1205 return (double) SPEC_CVAL (val->etype).v_uint;
1208 if (SPEC_NOUN (val->etype) == V_VOID) {
1209 return (double) SPEC_CVAL (val->etype).v_ulong;
1213 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1214 "floatFromVal: unknown value");
1218 /*------------------------------------------------------------------*/
1219 /* valUnaryPM - does the unary +/- operation on a constant */
1220 /*------------------------------------------------------------------*/
1222 valUnaryPM (value * val)
1224 /* depending on type */
1225 if (SPEC_NOUN (val->etype) == V_FLOAT)
1226 SPEC_CVAL (val->etype).v_float = -1.0 * SPEC_CVAL (val->etype).v_float;
1227 else if (SPEC_NOUN (val->etype) == V_FIXED16X16)
1228 SPEC_CVAL (val->etype).v_fixed16x16 = -SPEC_CVAL (val->etype).v_fixed16x16;
1231 if (SPEC_LONG (val->etype))
1233 if (SPEC_USIGN (val->etype))
1234 SPEC_CVAL (val->etype).v_ulong = 0-SPEC_CVAL (val->etype).v_ulong;
1236 SPEC_CVAL (val->etype).v_long = -SPEC_CVAL (val->etype).v_long;
1240 if (SPEC_USIGN (val->etype))
1241 SPEC_CVAL (val->etype).v_uint = 0-SPEC_CVAL (val->etype).v_uint;
1243 SPEC_CVAL (val->etype).v_int = -SPEC_CVAL (val->etype).v_int;
1245 if (SPEC_NOUN(val->etype) == V_CHAR)
1247 /* promote to 'signed int', cheapestVal() might reduce it again */
1248 SPEC_USIGN(val->etype) = 0;
1249 SPEC_NOUN(val->etype) = V_INT;
1251 return cheapestVal (val);
1257 /*------------------------------------------------------------------*/
1258 /* valueComplement - complements a constant */
1259 /*------------------------------------------------------------------*/
1261 valComplement (value * val)
1263 /* depending on type */
1264 if (SPEC_LONG (val->etype))
1266 if (SPEC_USIGN (val->etype))
1267 SPEC_CVAL (val->etype).v_ulong = ~SPEC_CVAL (val->etype).v_ulong;
1269 SPEC_CVAL (val->etype).v_long = ~SPEC_CVAL (val->etype).v_long;
1273 if (SPEC_USIGN (val->etype))
1274 SPEC_CVAL (val->etype).v_uint = ~SPEC_CVAL (val->etype).v_uint;
1276 SPEC_CVAL (val->etype).v_int = ~SPEC_CVAL (val->etype).v_int;
1278 if (SPEC_NOUN(val->etype) == V_CHAR)
1280 /* promote to 'signed int', cheapestVal() might reduce it again */
1281 SPEC_USIGN(val->etype) = 0;
1282 SPEC_NOUN(val->etype) = V_INT;
1284 return cheapestVal (val);
1289 /*------------------------------------------------------------------*/
1290 /* valueNot - complements a constant */
1291 /*------------------------------------------------------------------*/
1293 valNot (value * val)
1295 /* depending on type */
1296 if (SPEC_LONG (val->etype))
1298 if (SPEC_USIGN (val->etype))
1299 SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_ulong;
1301 SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_long;
1305 if (SPEC_USIGN (val->etype))
1306 SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_uint;
1308 SPEC_CVAL (val->etype).v_int = !SPEC_CVAL (val->etype).v_int;
1311 /* ANSI: result type is int, value is 0 or 1 */
1312 /* sdcc will hold this in an 'unsigned char' */
1313 SPEC_USIGN(val->etype) = 1;
1314 SPEC_LONG (val->etype) = 0;
1315 SPEC_NOUN(val->etype) = V_CHAR;
1319 /*------------------------------------------------------------------*/
1320 /* valMult - multiply constants */
1321 /*------------------------------------------------------------------*/
1323 valMult (value * lval, value * rval)
1327 /* create a new value */
1329 val->type = val->etype = computeType (lval->etype,
1333 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1335 if (IS_FLOAT (val->type))
1336 SPEC_CVAL (val->type).v_float = floatFromVal (lval) * floatFromVal (rval);
1338 if (IS_FIXED16X16 (val->type))
1339 SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble(floatFromVal (lval) * floatFromVal (rval));
1340 /* signed and unsigned mul are the same, as long as the precision of the
1341 result isn't bigger than the precision of the operands. */
1342 else if (SPEC_LONG (val->type))
1343 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) *
1344 (TYPE_UDWORD) floatFromVal (rval);
1345 else if (SPEC_USIGN (val->type)) /* unsigned int */
1347 TYPE_UDWORD ul = (TYPE_UWORD) floatFromVal (lval) *
1348 (TYPE_UWORD) floatFromVal (rval);
1350 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) ul;
1351 if (ul != (TYPE_UWORD) ul)
1354 else /* signed int */
1356 TYPE_DWORD l = (TYPE_WORD) floatFromVal (lval) *
1357 (TYPE_WORD) floatFromVal (rval);
1359 SPEC_CVAL (val->type).v_int = (TYPE_WORD) l;
1360 if (l != (TYPE_WORD) l)
1363 return cheapestVal (val);
1366 /*------------------------------------------------------------------*/
1367 /* valDiv - Divide constants */
1368 /*------------------------------------------------------------------*/
1370 valDiv (value * lval, value * rval)
1374 if (floatFromVal (rval) == 0)
1376 werror (E_DIVIDE_BY_ZERO);
1380 /* create a new value */
1382 val->type = val->etype = computeType (lval->etype,
1386 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1388 if (IS_FLOAT (val->type))
1389 SPEC_CVAL (val->type).v_float = floatFromVal (lval) / floatFromVal (rval);
1391 if (IS_FIXED16X16 (val->type))
1392 SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) / floatFromVal (rval) );
1393 else if (SPEC_LONG (val->type))
1395 if (SPEC_USIGN (val->type))
1396 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) /
1397 (TYPE_UDWORD) floatFromVal (rval);
1399 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) /
1400 (TYPE_DWORD) floatFromVal (rval);
1404 if (SPEC_USIGN (val->type))
1405 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) /
1406 (TYPE_UWORD) floatFromVal (rval);
1408 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) /
1409 (TYPE_WORD) floatFromVal (rval);
1411 return cheapestVal (val);
1414 /*------------------------------------------------------------------*/
1415 /* valMod - Modulus constants */
1416 /*------------------------------------------------------------------*/
1418 valMod (value * lval, value * rval)
1422 /* create a new value */
1424 val->type = val->etype = computeType (lval->etype,
1428 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1430 if (SPEC_LONG (val->type))
1432 if (SPEC_USIGN (val->type))
1433 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) %
1434 (TYPE_UDWORD) floatFromVal (rval);
1436 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) %
1437 (TYPE_DWORD) floatFromVal (rval);
1441 if (SPEC_USIGN (val->type))
1442 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) %
1443 (TYPE_UWORD) floatFromVal (rval);
1445 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) %
1446 (TYPE_WORD) floatFromVal (rval);
1448 return cheapestVal (val);
1451 /*------------------------------------------------------------------*/
1452 /* valPlus - Addition constants */
1453 /*------------------------------------------------------------------*/
1455 valPlus (value * lval, value * rval)
1459 /* create a new value */
1461 val->type = val->etype = computeType (lval->etype,
1465 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1467 if (IS_FLOAT (val->type))
1468 SPEC_CVAL (val->type).v_float = floatFromVal (lval) + floatFromVal (rval);
1470 if (IS_FIXED16X16 (val->type))
1471 SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) + floatFromVal (rval) );
1472 else if (SPEC_LONG (val->type))
1474 if (SPEC_USIGN (val->type))
1475 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) +
1476 (TYPE_UDWORD) floatFromVal (rval);
1478 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) +
1479 (TYPE_DWORD) floatFromVal (rval);
1483 if (SPEC_USIGN (val->type))
1484 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) +
1485 (TYPE_UWORD) floatFromVal (rval);
1487 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) +
1488 (TYPE_WORD) floatFromVal (rval);
1490 return cheapestVal (val);
1493 /*------------------------------------------------------------------*/
1494 /* valMinus - Addition constants */
1495 /*------------------------------------------------------------------*/
1497 valMinus (value * lval, value * rval)
1501 /* create a new value */
1503 val->type = val->etype = computeType (lval->etype,
1507 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1509 if (IS_FLOAT (val->type))
1510 SPEC_CVAL (val->type).v_float = floatFromVal (lval) - floatFromVal (rval);
1512 if (IS_FIXED16X16 (val->type))
1513 SPEC_CVAL (val->type).v_fixed16x16 = fixed16x16FromDouble( floatFromVal (lval) - floatFromVal (rval) );
1514 else if (SPEC_LONG (val->type))
1516 if (SPEC_USIGN (val->type))
1517 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) -
1518 (TYPE_UDWORD) floatFromVal (rval);
1520 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) -
1521 (TYPE_DWORD) floatFromVal (rval);
1525 if (SPEC_USIGN (val->type))
1526 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) -
1527 (TYPE_UWORD) floatFromVal (rval);
1529 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) -
1530 (TYPE_WORD) floatFromVal (rval);
1532 return cheapestVal (val);
1535 /*------------------------------------------------------------------*/
1536 /* valShift - Shift left or right */
1537 /*------------------------------------------------------------------*/
1539 valShift (value * lval, value * rval, int lr)
1543 /* create a new value */
1545 val->type = val->etype = computeType (lval->etype,
1549 SPEC_SCLS (val->etype) = S_LITERAL; /* will remain literal */
1551 if (getSize (val->type) * 8 <= (TYPE_UDWORD) floatFromVal (rval) &&
1554 /* right shift and unsigned */
1555 (!lr && SPEC_USIGN (rval->type))))
1557 werror (W_SHIFT_CHANGED, (lr ? "left" : "right"));
1560 if (SPEC_LONG (val->type))
1562 if (SPEC_USIGN (val->type))
1564 SPEC_CVAL (val->type).v_ulong = lr ?
1565 (TYPE_UDWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1566 (TYPE_UDWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1570 SPEC_CVAL (val->type).v_long = lr ?
1571 (TYPE_DWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1572 (TYPE_DWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1577 if (SPEC_USIGN (val->type))
1579 SPEC_CVAL (val->type).v_uint = lr ?
1580 (TYPE_UWORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1581 (TYPE_UWORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1585 SPEC_CVAL (val->type).v_int = lr ?
1586 (TYPE_WORD) floatFromVal (lval) << (TYPE_UDWORD) floatFromVal (rval) : \
1587 (TYPE_WORD) floatFromVal (lval) >> (TYPE_UDWORD) floatFromVal (rval);
1590 return cheapestVal (val);
1593 /*------------------------------------------------------------------*/
1594 /* valCompare- Compares two literal */
1595 /*------------------------------------------------------------------*/
1597 valCompare (value * lval, value * rval, int ctype)
1601 /* create a new value */
1603 val->type = val->etype = newCharLink ();
1604 val->type->class = SPECIFIER;
1605 SPEC_NOUN (val->type) = V_CHAR; /* type is char */
1606 SPEC_USIGN (val->type) = 1;
1607 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1612 SPEC_CVAL (val->type).v_int = floatFromVal (lval) < floatFromVal (rval);
1616 SPEC_CVAL (val->type).v_int = floatFromVal (lval) > floatFromVal (rval);
1620 SPEC_CVAL (val->type).v_int = floatFromVal (lval) <= floatFromVal (rval);
1624 SPEC_CVAL (val->type).v_int = floatFromVal (lval) >= floatFromVal (rval);
1628 if (SPEC_NOUN(lval->type) == V_FLOAT ||
1629 SPEC_NOUN(rval->type) == V_FLOAT)
1631 SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval);
1634 if (SPEC_NOUN(lval->type) == V_FIXED16X16 ||
1635 SPEC_NOUN(rval->type) == V_FIXED16X16)
1637 SPEC_CVAL (val->type).v_int = floatFromVal (lval) == floatFromVal (rval);
1641 /* integrals: ignore signedness */
1644 l = (TYPE_UDWORD) floatFromVal (lval);
1645 r = (TYPE_UDWORD) floatFromVal (rval);
1646 /* In order to correctly compare 'signed int' and 'unsigned int' it's
1647 neccessary to strip them to 16 bit.
1648 Literals are reduced to their cheapest type, therefore left and
1649 right might have different types. It's neccessary to find a
1650 common type: int (used for char too) or long */
1651 if (!IS_LONG (lval->etype) &&
1652 !IS_LONG (rval->etype))
1657 SPEC_CVAL (val->type).v_int = l == r;
1661 if (SPEC_NOUN(lval->type) == V_FLOAT ||
1662 SPEC_NOUN(rval->type) == V_FLOAT)
1664 SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval);
1667 if (SPEC_NOUN(lval->type) == V_FIXED16X16 ||
1668 SPEC_NOUN(rval->type) == V_FIXED16X16)
1670 SPEC_CVAL (val->type).v_int = floatFromVal (lval) != floatFromVal (rval);
1674 /* integrals: ignore signedness */
1677 l = (TYPE_UDWORD) floatFromVal (lval);
1678 r = (TYPE_UDWORD) floatFromVal (rval);
1679 /* In order to correctly compare 'signed int' and 'unsigned int' it's
1680 neccessary to strip them to 16 bit.
1681 Literals are reduced to their cheapest type, therefore left and
1682 right might have different types. It's neccessary to find a
1683 common type: int (used for char too) or long */
1684 if (!IS_LONG (lval->etype) &&
1685 !IS_LONG (rval->etype))
1690 SPEC_CVAL (val->type).v_int = l != r;
1699 /*------------------------------------------------------------------*/
1700 /* valBitwise - Bitwise operation */
1701 /*------------------------------------------------------------------*/
1703 valBitwise (value * lval, value * rval, int op)
1707 /* create a new value */
1709 val->type = computeType (lval->etype, rval->etype, RESULT_TYPE_CHAR, op);
1710 val->etype = getSpec (val->type);
1711 SPEC_SCLS (val->etype) = S_LITERAL;
1716 if (SPEC_LONG (val->type))
1718 if (SPEC_USIGN (val->type))
1719 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) &
1720 (TYPE_UDWORD) floatFromVal (rval);
1722 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) &
1723 (TYPE_DWORD) floatFromVal (rval);
1727 if (SPEC_USIGN (val->type))
1728 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) &
1729 (TYPE_UWORD) floatFromVal (rval);
1731 SPEC_CVAL (val->type).v_int = (TYPE_WORD) floatFromVal (lval) & (TYPE_WORD) floatFromVal (rval);
1736 if (SPEC_LONG (val->type))
1738 if (SPEC_USIGN (val->type))
1739 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) |
1740 (TYPE_UDWORD) floatFromVal (rval);
1742 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) |
1743 (TYPE_DWORD) floatFromVal (rval);
1747 if (SPEC_USIGN (val->type))
1748 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) |
1749 (TYPE_UWORD) floatFromVal (rval);
1751 SPEC_CVAL (val->type).v_int =
1752 (TYPE_WORD) floatFromVal (lval) | (TYPE_WORD) floatFromVal (rval);
1758 if (SPEC_LONG (val->type))
1760 if (SPEC_USIGN (val->type))
1761 SPEC_CVAL (val->type).v_ulong = (TYPE_UDWORD) floatFromVal (lval) ^
1762 (TYPE_UDWORD) floatFromVal (rval);
1764 SPEC_CVAL (val->type).v_long = (TYPE_DWORD) floatFromVal (lval) ^
1765 (TYPE_DWORD) floatFromVal (rval);
1769 if (SPEC_USIGN (val->type))
1770 SPEC_CVAL (val->type).v_uint = (TYPE_UWORD) floatFromVal (lval) ^
1771 (TYPE_UWORD) floatFromVal (rval);
1773 SPEC_CVAL (val->type).v_int =
1774 (TYPE_WORD) floatFromVal (lval) ^ (TYPE_WORD) floatFromVal (rval);
1779 return cheapestVal(val);
1782 /*------------------------------------------------------------------*/
1783 /* valAndOr - Generates code for and / or operation */
1784 /*------------------------------------------------------------------*/
1786 valLogicAndOr (value * lval, value * rval, int op)
1790 /* create a new value */
1792 val->type = val->etype = newCharLink ();
1793 val->type->class = SPECIFIER;
1794 SPEC_SCLS (val->type) = S_LITERAL; /* will remain literal */
1795 SPEC_USIGN (val->type) = 1;
1800 SPEC_CVAL (val->type).v_int = floatFromVal (lval) && floatFromVal (rval);
1804 SPEC_CVAL (val->type).v_int = floatFromVal (lval) || floatFromVal (rval);
1812 /*------------------------------------------------------------------*/
1813 /* valCastLiteral - casts a literal value to another type */
1814 /*------------------------------------------------------------------*/
1816 valCastLiteral (sym_link * dtype, double fval)
1819 TYPE_UDWORD l = (TYPE_UDWORD)fval;
1826 val->etype = getSpec (val->type = copyLinkChain (dtype));
1829 val->etype = val->type = newLink (SPECIFIER);
1830 SPEC_NOUN (val->etype) = V_VOID;
1832 SPEC_SCLS (val->etype) = S_LITERAL;
1834 /* if it is not a specifier then we can assume that */
1835 /* it will be an unsigned long */
1836 if (!IS_SPEC (val->type)) {
1837 SPEC_CVAL (val->etype).v_ulong = l;
1841 if (SPEC_NOUN (val->etype) == V_FLOAT)
1842 SPEC_CVAL (val->etype).v_float = fval;
1843 else if (SPEC_NOUN (val->etype) == V_FIXED16X16)
1844 SPEC_CVAL (val->etype).v_fixed16x16 = fixed16x16FromDouble( fval );
1845 else if (SPEC_NOUN (val->etype) == V_BIT ||
1846 SPEC_NOUN (val->etype) == V_SBIT)
1847 SPEC_CVAL (val->etype).v_uint = l ? 1 : 0;
1848 else if (SPEC_NOUN (val->etype) == V_BITFIELD)
1849 SPEC_CVAL (val->etype).v_uint = l &
1850 (0xffffu >> (16 - SPEC_BLEN (val->etype)));
1851 else if (SPEC_NOUN (val->etype) == V_CHAR) {
1852 if (SPEC_USIGN (val->etype))
1853 SPEC_CVAL (val->etype).v_uint= (TYPE_UBYTE) l;
1855 SPEC_CVAL (val->etype).v_int = (TYPE_BYTE) l;
1857 if (SPEC_LONG (val->etype)) {
1858 if (SPEC_USIGN (val->etype))
1859 SPEC_CVAL (val->etype).v_ulong = (TYPE_UDWORD) l;
1861 SPEC_CVAL (val->etype).v_long = (TYPE_DWORD) l;
1863 if (SPEC_USIGN (val->etype))
1864 SPEC_CVAL (val->etype).v_uint = (TYPE_UWORD)l;
1866 SPEC_CVAL (val->etype).v_int = (TYPE_WORD)l;
1872 /*------------------------------------------------------------------*/
1873 /* getNelements - determines # of elements from init list */
1874 /*------------------------------------------------------------------*/
1876 getNelements (sym_link * type, initList * ilist)
1883 if (ilist->type == INIT_DEEP)
1884 ilist = ilist->init.deep;
1886 /* if type is a character array and there is only one
1887 (string) initialiser then get the length of the string */
1888 if (IS_ARRAY (type) && IS_CHAR (type->next) && !ilist->next)
1890 ast *iast = ilist->init.node;
1891 value *v = (iast->type == EX_VALUE ? iast->opval.val : NULL);
1894 werror (E_CONST_EXPECTED);
1898 if (IS_ARRAY (v->type) && IS_CHAR (v->etype))
1899 // yep, it's a string
1901 return DCL_ELEM (v->type);
1909 ilist = ilist->next;
1914 /*-----------------------------------------------------------------*/
1915 /* valForArray - returns a value with name of array index */
1916 /*-----------------------------------------------------------------*/
1918 valForArray (ast * arrExpr)
1920 value *val, *lval = NULL;
1922 int size = getSize (arrExpr->left->ftype->next);
1923 /* if the right or left is an array
1925 if (IS_AST_OP (arrExpr->left))
1927 if (arrExpr->left->opval.op == '[')
1928 lval = valForArray (arrExpr->left);
1929 else if (arrExpr->left->opval.op == '.')
1930 lval = valForStructElem (arrExpr->left->left,
1931 arrExpr->left->right);
1932 else if (arrExpr->left->opval.op == PTR_OP &&
1933 IS_ADDRESS_OF_OP (arrExpr->left->left))
1934 lval = valForStructElem (arrExpr->left->left->left,
1935 arrExpr->left->right);
1940 else if (!IS_AST_SYM_VALUE (arrExpr->left))
1943 if (!IS_AST_LIT_VALUE (arrExpr->right))
1949 SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname);
1953 SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
1956 SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
1957 (int) AST_LIT_VALUE (arrExpr->right) * size);
1959 val->type = newLink (DECLARATOR);
1960 if (SPEC_SCLS (arrExpr->left->etype) == S_CODE)
1961 DCL_TYPE (val->type) = CPOINTER;
1962 else if (SPEC_SCLS (arrExpr->left->etype) == S_XDATA)
1963 DCL_TYPE (val->type) = FPOINTER;
1964 else if (SPEC_SCLS (arrExpr->left->etype) == S_XSTACK)
1965 DCL_TYPE (val->type) = PPOINTER;
1966 else if (SPEC_SCLS (arrExpr->left->etype) == S_IDATA)
1967 DCL_TYPE (val->type) = IPOINTER;
1968 else if (SPEC_SCLS (arrExpr->left->etype) == S_EEPROM)
1969 DCL_TYPE (val->type) = EEPPOINTER;
1971 DCL_TYPE (val->type) = POINTER;
1972 val->type->next = arrExpr->left->ftype->next;
1973 val->etype = getSpec (val->type);
1977 /*-----------------------------------------------------------------*/
1978 /* valForStructElem - returns value with name of struct element */
1979 /*-----------------------------------------------------------------*/
1981 valForStructElem (ast * structT, ast * elemT)
1983 value *val, *lval = NULL;
1987 /* left could be furthur derefed */
1988 if (IS_AST_OP (structT))
1990 if (structT->opval.op == '[')
1991 lval = valForArray (structT);
1992 else if (structT->opval.op == '.')
1993 lval = valForStructElem (structT->left, structT->right);
1994 else if (structT->opval.op == PTR_OP &&
1995 IS_ADDRESS_OF_OP (structT->left))
1996 lval = valForStructElem (structT->left->left,
2002 if (!IS_AST_SYM_VALUE (elemT))
2005 if (!IS_STRUCT (structT->etype))
2008 if ((sym = getStructElement (SPEC_STRUCT (structT->etype),
2009 AST_SYMBOL (elemT))) == NULL)
2017 SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname);
2021 SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
2024 SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
2027 val->type = newLink (DECLARATOR);
2028 if (SPEC_SCLS (structT->etype) == S_CODE)
2029 DCL_TYPE (val->type) = CPOINTER;
2030 else if (SPEC_SCLS (structT->etype) == S_XDATA)
2031 DCL_TYPE (val->type) = FPOINTER;
2032 else if (SPEC_SCLS (structT->etype) == S_XSTACK)
2033 DCL_TYPE (val->type) = PPOINTER;
2034 else if (SPEC_SCLS (structT->etype) == S_IDATA)
2035 DCL_TYPE (val->type) = IPOINTER;
2036 else if (SPEC_SCLS (structT->etype) == S_EEPROM)
2037 DCL_TYPE (val->type) = EEPPOINTER;
2039 DCL_TYPE (val->type) = POINTER;
2040 val->type->next = sym->type;
2041 val->etype = getSpec (val->type);
2045 /*-----------------------------------------------------------------*/
2046 /* valForCastAggr - will return value for a cast of an aggregate */
2047 /* plus minus a constant */
2048 /*-----------------------------------------------------------------*/
2050 valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op)
2054 if (!IS_AST_SYM_VALUE (aexpr))
2056 if (!IS_AST_LIT_VALUE (cnst))
2061 SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
2062 AST_SYMBOL (aexpr)->rname, op,
2063 getSize (type->next) * (int) AST_LIT_VALUE (cnst));
2066 val->etype = getSpec (val->type);
2070 /*-----------------------------------------------------------------*/
2071 /* valForCastAggr - will return value for a cast of an aggregate */
2072 /* with no constant */
2073 /*-----------------------------------------------------------------*/
2075 valForCastArr (ast * aexpr, sym_link * type)
2079 if (!IS_AST_SYM_VALUE (aexpr))
2084 SNPRINTF (val->name, sizeof(val->name), "(%s)",
2085 AST_SYMBOL (aexpr)->rname);
2088 val->etype = getSpec (val->type);