1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
30 set *operKeyReset = NULL;
31 ast *staticAutos = NULL;
34 #define LRVAL(x) x->left->rvalue
35 #define RRVAL(x) x->right->rvalue
36 #define TRVAL(x) x->rvalue
37 #define LLVAL(x) x->left->lvalue
38 #define RLVAL(x) x->right->lvalue
39 #define TLVAL(x) x->lvalue
40 #define RTYPE(x) x->right->ftype
41 #define RETYPE(x) x->right->etype
42 #define LTYPE(x) x->left->ftype
43 #define LETYPE(x) x->left->etype
44 #define TTYPE(x) x->ftype
45 #define TETYPE(x) x->etype
53 ast *createIval (ast *, sym_link *, initList *, ast *);
54 ast *createIvalCharPtr (ast *, sym_link *, ast *);
55 ast *optimizeRRCRLC (ast *);
56 ast *optimizeGetHbit (ast *);
57 ast *backPatchLabels (ast *, symbol *, symbol *);
59 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
64 printTypeChain (tree->ftype, stdout);
69 /*-----------------------------------------------------------------*/
70 /* newAst - creates a fresh node for an expression tree */
71 /*-----------------------------------------------------------------*/
74 newAst (int type, void *op)
77 static int oldLineno = 0;
79 Safe_calloc (1, ex, sizeof (ast));
82 ex->lineno = (noLineno ? oldLineno : yylineno);
83 ex->filename = currFname;
84 ex->level = NestLevel;
85 ex->block = currBlockno;
86 ex->initMode = inInitMode;
88 /* depending on the type */
92 ex->opval.val = (value *) op;
95 ex->opval.op = (long) op;
98 ex->opval.lnk = (sym_link *) op;
101 ex->opval.stmnt = (unsigned) op;
109 newAst_ (unsigned type)
112 static int oldLineno = 0;
114 ex = Safe_calloc (1, sizeof (ast));
117 ex->lineno = (noLineno ? oldLineno : yylineno);
118 ex->filename = currFname;
119 ex->level = NestLevel;
120 ex->block = currBlockno;
121 ex->initMode = inInitMode;
126 newAst_VALUE (value * val)
128 ast *ex = newAst_ (EX_VALUE);
134 newAst_OP (unsigned op)
136 ast *ex = newAst_ (EX_OP);
142 newAst_LINK (sym_link * val)
144 ast *ex = newAst_ (EX_LINK);
150 newAst_STMNT (unsigned val)
152 ast *ex = newAst_ (EX_STMNT);
153 ex->opval.stmnt = val;
157 /*-----------------------------------------------------------------*/
158 /* newNode - creates a new node */
159 /*-----------------------------------------------------------------*/
161 newNode (long op, ast * left, ast * right)
172 /*-----------------------------------------------------------------*/
173 /* newIfxNode - creates a new Ifx Node */
174 /*-----------------------------------------------------------------*/
176 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
180 /* if this is a literal then we already know the result */
181 if (condAst->etype && IS_LITERAL (condAst->etype))
183 /* then depending on the expression value */
184 if (floatFromVal (condAst->opval.val))
185 ifxNode = newNode (GOTO,
186 newAst_VALUE (symbolVal (trueLabel)),
189 ifxNode = newNode (GOTO,
190 newAst_VALUE (symbolVal (falseLabel)),
195 ifxNode = newNode (IFX, condAst, NULL);
196 ifxNode->trueLabel = trueLabel;
197 ifxNode->falseLabel = falseLabel;
203 /*-----------------------------------------------------------------*/
204 /* copyAstValues - copies value portion of ast if needed */
205 /*-----------------------------------------------------------------*/
207 copyAstValues (ast * dest, ast * src)
209 switch (src->opval.op)
212 dest->values.sym = copySymbolChain (src->values.sym);
216 dest->values.switchVals.swVals =
217 copyValue (src->values.switchVals.swVals);
218 dest->values.switchVals.swDefault =
219 src->values.switchVals.swDefault;
220 dest->values.switchVals.swNum =
221 src->values.switchVals.swNum;
225 dest->values.inlineasm = Safe_calloc (1, strlen (src->values.inlineasm) + 1);
226 strcpy (dest->values.inlineasm, src->values.inlineasm);
230 dest->values.constlist = copyLiteralList(src->values.constlist);
234 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
235 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
236 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
237 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
238 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
239 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
240 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
245 /*-----------------------------------------------------------------*/
246 /* copyAst - makes a copy of a given astession */
247 /*-----------------------------------------------------------------*/
256 dest = Safe_calloc (1, sizeof (ast));
258 dest->type = src->type;
259 dest->lineno = src->lineno;
260 dest->level = src->level;
261 dest->funcName = src->funcName;
262 dest->argSym = src->argSym;
264 /* if this is a leaf */
266 if (src->type == EX_VALUE)
268 dest->opval.val = copyValue (src->opval.val);
273 if (src->type == EX_LINK)
275 dest->opval.lnk = copyLinkChain (src->opval.lnk);
279 dest->opval.op = src->opval.op;
281 /* if this is a node that has special values */
282 copyAstValues (dest, src);
285 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
287 dest->trueLabel = copySymbol (src->trueLabel);
288 dest->falseLabel = copySymbol (src->falseLabel);
289 dest->left = copyAst (src->left);
290 dest->right = copyAst (src->right);
296 /*-----------------------------------------------------------------*/
297 /* hasSEFcalls - returns TRUE if tree has a function call */
298 /*-----------------------------------------------------------------*/
300 hasSEFcalls (ast * tree)
305 if (tree->type == EX_OP &&
306 (tree->opval.op == CALL ||
307 tree->opval.op == PCALL ||
308 tree->opval.op == '=' ||
309 tree->opval.op == INC_OP ||
310 tree->opval.op == DEC_OP))
313 return (hasSEFcalls (tree->left) |
314 hasSEFcalls (tree->right));
317 /*-----------------------------------------------------------------*/
318 /* isAstEqual - compares two asts & returns 1 if they are equal */
319 /*-----------------------------------------------------------------*/
321 isAstEqual (ast * t1, ast * t2)
330 if (t1->type != t2->type)
336 if (t1->opval.op != t2->opval.op)
338 return (isAstEqual (t1->left, t2->left) &&
339 isAstEqual (t1->right, t2->right));
343 if (t1->opval.val->sym)
345 if (!t2->opval.val->sym)
348 return isSymbolEqual (t1->opval.val->sym,
353 if (t2->opval.val->sym)
356 return (floatFromVal (t1->opval.val) ==
357 floatFromVal (t2->opval.val));
361 /* only compare these two types */
369 /*-----------------------------------------------------------------*/
370 /* resolveSymbols - resolve symbols from the symbol table */
371 /*-----------------------------------------------------------------*/
373 resolveSymbols (ast * tree)
375 /* walk the entire tree and check for values */
376 /* with symbols if we find one then replace */
377 /* symbol with that from the symbol table */
383 /* if not block & function */
384 if (tree->type == EX_OP &&
385 (tree->opval.op != FUNCTION &&
386 tree->opval.op != BLOCK &&
387 tree->opval.op != NULLOP))
389 filename = tree->filename;
390 lineno = tree->lineno;
393 /* make sure we resolve the true & false labels for ifx */
394 if (tree->type == EX_OP && tree->opval.op == IFX)
400 if ((csym = findSym (LabelTab, tree->trueLabel,
401 tree->trueLabel->name)))
402 tree->trueLabel = csym;
404 werror (E_LABEL_UNDEF, tree->trueLabel->name);
407 if (tree->falseLabel)
409 if ((csym = findSym (LabelTab,
411 tree->falseLabel->name)))
412 tree->falseLabel = csym;
414 werror (E_LABEL_UNDEF, tree->falseLabel->name);
419 /* if this is a label resolve it from the labelTab */
420 if (IS_AST_VALUE (tree) &&
421 tree->opval.val->sym &&
422 tree->opval.val->sym->islbl)
425 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
426 tree->opval.val->sym->name);
429 werror (E_LABEL_UNDEF, tree->opval.val->sym->name);
431 tree->opval.val->sym = csym;
433 goto resolveChildren;
436 /* do only for leafs */
437 if (IS_AST_VALUE (tree) &&
438 tree->opval.val->sym &&
439 !tree->opval.val->sym->implicit)
442 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
444 /* if found in the symbol table & they r not the same */
445 if (csym && tree->opval.val->sym != csym)
447 tree->opval.val->sym = csym;
448 tree->opval.val->type = csym->type;
449 tree->opval.val->etype = csym->etype;
452 /* if not found in the symbol table */
453 /* mark it as undefined assume it is */
454 /* an integer in data space */
455 if (!csym && !tree->opval.val->sym->implicit)
458 /* if this is a function name then */
459 /* mark it as returning an int */
462 tree->opval.val->sym->type = newLink ();
463 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
464 tree->opval.val->sym->type->next =
465 tree->opval.val->sym->etype = newIntLink ();
466 tree->opval.val->etype = tree->opval.val->etype;
467 tree->opval.val->type = tree->opval.val->sym->type;
468 werror (W_IMPLICIT_FUNC, tree->opval.val->sym->name);
469 allocVariables (tree->opval.val->sym);
473 tree->opval.val->sym->undefined = 1;
474 tree->opval.val->type =
475 tree->opval.val->etype = newIntLink ();
476 tree->opval.val->sym->type =
477 tree->opval.val->sym->etype = newIntLink ();
483 resolveSymbols (tree->left);
484 resolveSymbols (tree->right);
489 /*-----------------------------------------------------------------*/
490 /* setAstLineno - walks a ast tree & sets the line number */
491 /*-----------------------------------------------------------------*/
493 setAstLineno (ast * tree, int lineno)
498 tree->lineno = lineno;
499 setAstLineno (tree->left, lineno);
500 setAstLineno (tree->right, lineno);
505 /* this functions seems to be superfluous?! kmh */
507 /*-----------------------------------------------------------------*/
508 /* resolveFromTable - will return the symbal table value */
509 /*-----------------------------------------------------------------*/
511 resolveFromTable (value * val)
518 csym = findSymWithLevel (SymbolTab, val->sym);
520 /* if found in the symbol table & they r not the same */
521 if (csym && val->sym != csym &&
522 csym->level == val->sym->level &&
528 val->type = csym->type;
529 val->etype = csym->etype;
536 /*-----------------------------------------------------------------*/
537 /* funcOfType :- function of type with name */
538 /*-----------------------------------------------------------------*/
540 funcOfType (char *name, sym_link * type, sym_link * argType,
544 /* create the symbol */
545 sym = newSymbol (name, 0);
547 /* if arguments required */
552 args = sym->args = newValue ();
556 args->type = copyLinkChain (argType);
557 args->etype = getSpec (args->type);
560 args = args->next = newValue ();
564 /* setup return value */
565 sym->type = newLink ();
566 DCL_TYPE (sym->type) = FUNCTION;
567 sym->type->next = copyLinkChain (type);
568 sym->etype = getSpec (sym->type);
569 SPEC_RENT (sym->etype) = rent;
574 allocVariables (sym);
579 /*-----------------------------------------------------------------*/
580 /* reverseParms - will reverse a parameter tree */
581 /*-----------------------------------------------------------------*/
583 reverseParms (ast * ptree)
589 /* top down if we find a nonParm tree then quit */
590 if (ptree->type == EX_OP && ptree->opval.op == PARAM)
593 ptree->left = ptree->right;
594 ptree->right = ttree;
595 reverseParms (ptree->left);
596 reverseParms (ptree->right);
602 /*-----------------------------------------------------------------*/
603 /* processParms - makes sure the parameters are okay and do some */
604 /* processing with them */
605 /*-----------------------------------------------------------------*/
607 processParms (ast * func,
613 sym_link *fetype = func->etype;
615 /* if none of them exist */
616 if (!defParm && !actParm)
620 if (getenv("DEBUG_SANITY")) {
621 fprintf (stderr, "addSym: %s ", defParm->name);
623 /* make sure the type is complete and sane */
624 checkTypeSanity(defParm->etype, defParm->name);
627 /* if the function is being called via a pointer & */
628 /* it has not been defined a reentrant then we cannot */
629 /* have parameters */
630 if (func->type != EX_VALUE && !IS_RENT (fetype) && !options.stackAuto)
632 werror (W_NONRENT_ARGS);
636 /* if defined parameters ended but actual parameters */
637 /* exist and this is not defined as a variable arg */
638 /* also check if statckAuto option is specified */
639 if ((!defParm) && actParm && (!func->hasVargs) &&
640 !options.stackAuto && !IS_RENT (fetype))
642 werror (E_TOO_MANY_PARMS);
646 /* if defined parameters present but no actual parameters */
647 if (defParm && !actParm)
649 werror (E_TOO_FEW_PARMS);
653 /* If this is a varargs function... */
654 if (!defParm && actParm && func->hasVargs)
659 if (IS_CAST_OP (actParm)
660 || (IS_AST_LIT_VALUE (actParm) && actParm->values.literalFromCast))
662 /* Parameter was explicitly typecast; don't touch it. */
666 /* The ternary ('?') operator is weird: the ftype of the
667 * operator is the type of the condition, but it will return a
668 * (possibly) different type.
670 if (IS_TERNARY_OP(actParm))
672 assert(IS_COLON_OP(actParm->right));
673 assert(actParm->right->left);
674 ftype = actParm->right->left->ftype;
678 ftype = actParm->ftype;
681 /* If it's a small integer, upcast to int. */
682 if (IS_INTEGRAL (ftype)
683 && (getSize (ftype) < (unsigned) INTSIZE))
685 newType = newAst_LINK(INTTYPE);
688 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
690 newType = newAst_LINK (copyLinkChain(ftype));
691 DCL_TYPE (newType->opval.lnk) = GPOINTER;
694 if (IS_AGGREGATE (ftype))
696 newType = newAst_LINK (copyLinkChain (ftype));
697 DCL_TYPE (newType->opval.lnk) = GPOINTER;
701 /* cast required; change this op to a cast. */
702 ast *parmCopy = resolveSymbols (copyAst (actParm));
704 actParm->type = EX_OP;
705 actParm->opval.op = CAST;
706 actParm->left = newType;
707 actParm->right = parmCopy;
708 decorateType (actParm);
710 else if (actParm->type == EX_OP && actParm->opval.op == PARAM)
712 return (processParms (func, NULL, actParm->left, parmNumber, FALSE) ||
713 processParms (func, NULL, actParm->right, parmNumber, rightmost));
718 /* if defined parameters ended but actual has not & */
720 if (!defParm && actParm &&
721 (options.stackAuto || IS_RENT (fetype)))
724 resolveSymbols (actParm);
725 /* if this is a PARAM node then match left & right */
726 if (actParm->type == EX_OP && actParm->opval.op == PARAM)
728 return (processParms (func, defParm, actParm->left, parmNumber, FALSE) ||
729 processParms (func, defParm->next, actParm->right, parmNumber, rightmost));
733 /* If we have found a value node by following only right-hand links,
734 * then we know that there are no more values after us.
736 * Therefore, if there are more defined parameters, the caller didn't
739 if (rightmost && defParm->next)
741 werror (E_TOO_FEW_PARMS);
747 /* the parameter type must be at least castable */
748 if (compareType (defParm->type, actParm->ftype) == 0) {
749 werror (W_INCOMPAT_CAST);
750 fprintf (stderr, "type --> '");
751 printTypeChain (actParm->ftype, stderr);
752 fprintf (stderr, "' ");
753 fprintf (stderr, "assigned to type --> '");
754 printTypeChain (defParm->type, stderr);
755 fprintf (stderr, "'\n");
758 /* if the parameter is castable then add the cast */
759 if (compareType (defParm->type, actParm->ftype) < 0)
761 ast *pTree = resolveSymbols (copyAst (actParm));
763 /* now change the current one to a cast */
764 actParm->type = EX_OP;
765 actParm->opval.op = CAST;
766 actParm->left = newAst_LINK (defParm->type);
767 actParm->right = pTree;
768 actParm->etype = defParm->etype;
769 actParm->ftype = defParm->type;
772 /* actParm->argSym = resolveFromTable(defParm)->sym ; */
774 actParm->argSym = defParm->sym;
775 /* make a copy and change the regparm type to the defined parm */
776 actParm->etype = getSpec (actParm->ftype = copyLinkChain (actParm->ftype));
777 SPEC_REGPARM (actParm->etype) = SPEC_REGPARM (defParm->etype);
781 /*-----------------------------------------------------------------*/
782 /* createIvalType - generates ival for basic types */
783 /*-----------------------------------------------------------------*/
785 createIvalType (ast * sym, sym_link * type, initList * ilist)
789 /* if initList is deep */
790 if (ilist->type == INIT_DEEP)
791 ilist = ilist->init.deep;
793 iExpr = decorateType (resolveSymbols (list2expr (ilist)));
794 return decorateType (newNode ('=', sym, iExpr));
797 /*-----------------------------------------------------------------*/
798 /* createIvalStruct - generates initial value for structures */
799 /*-----------------------------------------------------------------*/
801 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
807 sflds = SPEC_STRUCT (type)->fields;
808 if (ilist->type != INIT_DEEP)
810 werror (E_INIT_STRUCT, "");
814 iloop = ilist->init.deep;
816 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
820 /* if we have come to end */
824 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
825 lAst = decorateType (resolveSymbols (lAst));
826 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast)));
832 /*-----------------------------------------------------------------*/
833 /* createIvalArray - generates code for array initialization */
834 /*-----------------------------------------------------------------*/
836 createIvalArray (ast * sym, sym_link * type, initList * ilist)
840 int lcnt = 0, size = 0;
841 literalList *literalL;
843 /* take care of the special case */
844 /* array of characters can be init */
846 if (IS_CHAR (type->next))
847 if ((rast = createIvalCharPtr (sym,
849 decorateType (resolveSymbols (list2expr (ilist))))))
851 return decorateType (resolveSymbols (rast));
853 /* not the special case */
854 if (ilist->type != INIT_DEEP)
856 werror (E_INIT_STRUCT, "");
860 iloop = ilist->init.deep;
861 lcnt = DCL_ELEM (type);
863 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
867 aSym = decorateType (resolveSymbols(sym));
869 rast = newNode(ARRAYINIT, aSym, NULL);
870 rast->values.constlist = literalL;
872 // Make sure size is set to length of initializer list.
879 if (lcnt && size > lcnt)
881 // Array size was specified, and we have more initializers than needed.
882 char *name=sym->opval.val->sym->name;
883 int lineno=sym->opval.val->sym->lineDef;
885 werror (W_EXESS_ARRAY_INITIALIZERS, name, lineno);
894 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
895 aSym = decorateType (resolveSymbols (aSym));
896 rast = createIval (aSym, type->next, iloop, rast);
897 iloop = (iloop ? iloop->next : NULL);
903 /* no of elements given and we */
904 /* have generated for all of them */
907 // there has to be a better way
908 char *name=sym->opval.val->sym->name;
909 int lineno=sym->opval.val->sym->lineDef;
910 werror (W_EXESS_ARRAY_INITIALIZERS, name, lineno);
917 /* if we have not been given a size */
918 if (!DCL_ELEM (type))
920 DCL_ELEM (type) = size;
923 return decorateType (resolveSymbols (rast));
927 /*-----------------------------------------------------------------*/
928 /* createIvalCharPtr - generates initial values for char pointers */
929 /*-----------------------------------------------------------------*/
931 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
935 /* if this is a pointer & right is a literal array then */
936 /* just assignment will do */
937 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
938 SPEC_SCLS (iexpr->etype) == S_CODE)
939 && IS_ARRAY (iexpr->ftype)))
940 return newNode ('=', sym, iexpr);
942 /* left side is an array so we have to assign each */
944 if ((IS_LITERAL (iexpr->etype) ||
945 SPEC_SCLS (iexpr->etype) == S_CODE)
946 && IS_ARRAY (iexpr->ftype))
948 /* for each character generate an assignment */
949 /* to the array element */
950 char *s = SPEC_CVAL (iexpr->etype).v_char;
955 rast = newNode (NULLOP,
959 newAst_VALUE (valueFromLit ((float) i))),
960 newAst_VALUE (valueFromLit (*s))));
964 rast = newNode (NULLOP,
968 newAst_VALUE (valueFromLit ((float) i))),
969 newAst_VALUE (valueFromLit (*s))));
970 return decorateType (resolveSymbols (rast));
976 /*-----------------------------------------------------------------*/
977 /* createIvalPtr - generates initial value for pointers */
978 /*-----------------------------------------------------------------*/
980 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
986 if (ilist->type == INIT_DEEP)
987 ilist = ilist->init.deep;
989 iexpr = decorateType (resolveSymbols (list2expr (ilist)));
991 /* if character pointer */
992 if (IS_CHAR (type->next))
993 if ((rast = createIvalCharPtr (sym, type, iexpr)))
996 return newNode ('=', sym, iexpr);
999 /*-----------------------------------------------------------------*/
1000 /* createIval - generates code for initial value */
1001 /*-----------------------------------------------------------------*/
1003 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
1010 /* if structure then */
1011 if (IS_STRUCT (type))
1012 rast = createIvalStruct (sym, type, ilist);
1014 /* if this is a pointer */
1016 rast = createIvalPtr (sym, type, ilist);
1018 /* if this is an array */
1019 if (IS_ARRAY (type))
1020 rast = createIvalArray (sym, type, ilist);
1022 /* if type is SPECIFIER */
1024 rast = createIvalType (sym, type, ilist);
1027 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)));
1029 return decorateType (resolveSymbols (rast));
1032 /*-----------------------------------------------------------------*/
1033 /* initAggregates - initialises aggregate variables with initv */
1034 /*-----------------------------------------------------------------*/
1036 /* this has to go */ void printIval (symbol *, sym_link *, initList *, FILE *);
1038 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1042 if (getenv("TRY_THE_NEW_INITIALIZER")) {
1044 if (!TARGET_IS_MCS51 || !(options.model==MODEL_LARGE)) {
1045 fprintf (stderr, "Can't \"TRY_THE_NEW_INITIALIZER\" unless "
1046 "with -mmcs51 and --model-large");
1050 if (SPEC_OCLS(sym->etype)==xdata &&
1051 getSize(sym->type) > 16) { // else it isn't worth it: do it the old way
1054 newSym=copySymbol (sym);
1055 SPEC_OCLS(newSym->etype)=code;
1056 sprintf (newSym->name, "%s_init__", sym->name);
1057 sprintf (newSym->rname,"%s_init__", sym->rname);
1058 addSym (SymbolTab, newSym, newSym->name, 0, 0, 1);
1060 // emit it in the static segment
1061 addSet(&statsg->syms, newSym);
1063 // now memcpy() the entire array from cseg
1064 ast=newNode (ARRAYINIT, // ASSIGN_AGGREGATE
1065 newAst_VALUE (symbolVal (sym)),
1066 newAst_VALUE (symbolVal (newSym)));
1067 return decorateType(resolveSymbols(ast));
1071 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1074 /*-----------------------------------------------------------------*/
1075 /* gatherAutoInit - creates assignment expressions for initial */
1077 /*-----------------------------------------------------------------*/
1079 gatherAutoInit (symbol * autoChain)
1086 for (sym = autoChain; sym; sym = sym->next)
1089 /* resolve the symbols in the ival */
1091 resolveIvalSym (sym->ival);
1093 /* if this is a static variable & has an */
1094 /* initial value the code needs to be lifted */
1095 /* here to the main portion since they can be */
1096 /* initialised only once at the start */
1097 if (IS_STATIC (sym->etype) && sym->ival &&
1098 SPEC_SCLS (sym->etype) != S_CODE)
1102 // this can only be a constant
1103 if (!inInitMode && !IS_LITERAL(sym->ival->init.node->etype)) {
1104 werror (E_CONST_EXPECTED);
1107 /* insert the symbol into the symbol table */
1108 /* with level = 0 & name = rname */
1109 newSym = copySymbol (sym);
1110 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1112 /* now lift the code to main */
1113 if (IS_AGGREGATE (sym->type))
1114 work = initAggregates (sym, sym->ival, NULL);
1116 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1117 list2expr (sym->ival));
1119 setAstLineno (work, sym->lineDef);
1123 staticAutos = newNode (NULLOP, staticAutos, work);
1130 /* if there is an initial value */
1131 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1133 if (IS_AGGREGATE (sym->type))
1134 work = initAggregates (sym, sym->ival, NULL);
1136 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1137 list2expr (sym->ival));
1139 setAstLineno (work, sym->lineDef);
1142 init = newNode (NULLOP, init, work);
1151 /*-----------------------------------------------------------------*/
1152 /* stringToSymbol - creates a symbol from a literal string */
1153 /*-----------------------------------------------------------------*/
1155 stringToSymbol (value * val)
1157 char name[SDCC_NAME_MAX + 1];
1158 static int charLbl = 0;
1161 sprintf (name, "_str_%d", charLbl++);
1162 sym = newSymbol (name, 0); /* make it @ level 0 */
1163 strcpy (sym->rname, name);
1165 /* copy the type from the value passed */
1166 sym->type = copyLinkChain (val->type);
1167 sym->etype = getSpec (sym->type);
1168 /* change to storage class & output class */
1169 SPEC_SCLS (sym->etype) = S_CODE;
1170 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1171 SPEC_STAT (sym->etype) = 1;
1172 /* make the level & block = 0 */
1173 sym->block = sym->level = 0;
1175 /* create an ival */
1176 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1181 allocVariables (sym);
1184 return symbolVal (sym);
1188 /*-----------------------------------------------------------------*/
1189 /* processBlockVars - will go thru the ast looking for block if */
1190 /* a block is found then will allocate the syms */
1191 /* will also gather the auto inits present */
1192 /*-----------------------------------------------------------------*/
1194 processBlockVars (ast * tree, int *stack, int action)
1199 /* if this is a block */
1200 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1204 if (action == ALLOCATE)
1206 *stack += allocVariables (tree->values.sym);
1207 autoInit = gatherAutoInit (tree->values.sym);
1209 /* if there are auto inits then do them */
1211 tree->left = newNode (NULLOP, autoInit, tree->left);
1213 else /* action is deallocate */
1214 deallocLocal (tree->values.sym);
1217 processBlockVars (tree->left, stack, action);
1218 processBlockVars (tree->right, stack, action);
1222 /*-----------------------------------------------------------------*/
1223 /* constExprValue - returns the value of a constant expression */
1224 /* or NULL if it is not a constant expression */
1225 /*-----------------------------------------------------------------*/
1227 constExprValue (ast * cexpr, int check)
1229 cexpr = decorateType (resolveSymbols (cexpr));
1231 /* if this is not a constant then */
1232 if (!IS_LITERAL (cexpr->ftype))
1234 /* then check if this is a literal array
1236 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1237 SPEC_CVAL (cexpr->etype).v_char &&
1238 IS_ARRAY (cexpr->ftype))
1240 value *val = valFromType (cexpr->ftype);
1241 SPEC_SCLS (val->etype) = S_LITERAL;
1242 val->sym = cexpr->opval.val->sym;
1243 val->sym->type = copyLinkChain (cexpr->ftype);
1244 val->sym->etype = getSpec (val->sym->type);
1245 strcpy (val->name, cexpr->opval.val->sym->rname);
1249 /* if we are casting a literal value then */
1250 if (IS_AST_OP (cexpr) &&
1251 cexpr->opval.op == CAST &&
1252 IS_LITERAL (cexpr->left->ftype))
1253 return valCastLiteral (cexpr->ftype,
1254 floatFromVal (cexpr->left->opval.val));
1256 if (IS_AST_VALUE (cexpr))
1257 return cexpr->opval.val;
1260 werror (E_CONST_EXPECTED, "found expression");
1265 /* return the value */
1266 return cexpr->opval.val;
1270 /*-----------------------------------------------------------------*/
1271 /* isLabelInAst - will return true if a given label is found */
1272 /*-----------------------------------------------------------------*/
1274 isLabelInAst (symbol * label, ast * tree)
1276 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1279 if (IS_AST_OP (tree) &&
1280 tree->opval.op == LABEL &&
1281 isSymbolEqual (AST_SYMBOL (tree->left), label))
1284 return isLabelInAst (label, tree->right) &&
1285 isLabelInAst (label, tree->left);
1289 /*-----------------------------------------------------------------*/
1290 /* isLoopCountable - return true if the loop count can be determi- */
1291 /* -ned at compile time . */
1292 /*-----------------------------------------------------------------*/
1294 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1295 symbol ** sym, ast ** init, ast ** end)
1298 /* the loop is considered countable if the following
1299 conditions are true :-
1301 a) initExpr :- <sym> = <const>
1302 b) condExpr :- <sym> < <const1>
1303 c) loopExpr :- <sym> ++
1306 /* first check the initExpr */
1307 if (IS_AST_OP (initExpr) &&
1308 initExpr->opval.op == '=' && /* is assignment */
1309 IS_AST_SYM_VALUE (initExpr->left))
1310 { /* left is a symbol */
1312 *sym = AST_SYMBOL (initExpr->left);
1313 *init = initExpr->right;
1318 /* for now the symbol has to be of
1320 if (!IS_INTEGRAL ((*sym)->type))
1323 /* now check condExpr */
1324 if (IS_AST_OP (condExpr))
1327 switch (condExpr->opval.op)
1330 if (IS_AST_SYM_VALUE (condExpr->left) &&
1331 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1332 IS_AST_LIT_VALUE (condExpr->right))
1334 *end = condExpr->right;
1340 if (IS_AST_OP (condExpr->left) &&
1341 condExpr->left->opval.op == '>' &&
1342 IS_AST_LIT_VALUE (condExpr->left->right) &&
1343 IS_AST_SYM_VALUE (condExpr->left->left) &&
1344 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1347 *end = newNode ('+', condExpr->left->right,
1348 newAst_VALUE (constVal ("1")));
1359 /* check loop expression is of the form <sym>++ */
1360 if (!IS_AST_OP (loopExpr))
1363 /* check if <sym> ++ */
1364 if (loopExpr->opval.op == INC_OP)
1370 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1371 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1378 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1379 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1387 if (loopExpr->opval.op == ADD_ASSIGN)
1390 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1391 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1392 IS_AST_LIT_VALUE (loopExpr->right) &&
1393 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1401 /*-----------------------------------------------------------------*/
1402 /* astHasVolatile - returns true if ast contains any volatile */
1403 /*-----------------------------------------------------------------*/
1405 astHasVolatile (ast * tree)
1410 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1413 if (IS_AST_OP (tree))
1414 return astHasVolatile (tree->left) ||
1415 astHasVolatile (tree->right);
1420 /*-----------------------------------------------------------------*/
1421 /* astHasPointer - return true if the ast contains any ptr variable */
1422 /*-----------------------------------------------------------------*/
1424 astHasPointer (ast * tree)
1429 if (IS_AST_LINK (tree))
1432 /* if we hit an array expression then check
1433 only the left side */
1434 if (IS_AST_OP (tree) && tree->opval.op == '[')
1435 return astHasPointer (tree->left);
1437 if (IS_AST_VALUE (tree))
1438 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1440 return astHasPointer (tree->left) ||
1441 astHasPointer (tree->right);
1445 /*-----------------------------------------------------------------*/
1446 /* astHasSymbol - return true if the ast has the given symbol */
1447 /*-----------------------------------------------------------------*/
1449 astHasSymbol (ast * tree, symbol * sym)
1451 if (!tree || IS_AST_LINK (tree))
1454 if (IS_AST_VALUE (tree))
1456 if (IS_AST_SYM_VALUE (tree))
1457 return isSymbolEqual (AST_SYMBOL (tree), sym);
1462 return astHasSymbol (tree->left, sym) ||
1463 astHasSymbol (tree->right, sym);
1466 /*-----------------------------------------------------------------*/
1467 /* astHasDeref - return true if the ast has an indirect access */
1468 /*-----------------------------------------------------------------*/
1470 astHasDeref (ast * tree)
1472 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1475 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1477 return astHasDeref (tree->left) || astHasDeref (tree->right);
1480 /*-----------------------------------------------------------------*/
1481 /* isConformingBody - the loop body has to conform to a set of rules */
1482 /* for the loop to be considered reversible read on for rules */
1483 /*-----------------------------------------------------------------*/
1485 isConformingBody (ast * pbody, symbol * sym, ast * body)
1488 /* we are going to do a pre-order traversal of the
1489 tree && check for the following conditions. (essentially
1490 a set of very shallow tests )
1491 a) the sym passed does not participate in
1492 any arithmetic operation
1493 b) There are no function calls
1494 c) all jumps are within the body
1495 d) address of loop control variable not taken
1496 e) if an assignment has a pointer on the
1497 left hand side make sure right does not have
1498 loop control variable */
1500 /* if we reach the end or a leaf then true */
1501 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1505 /* if anything else is "volatile" */
1506 if (IS_VOLATILE (TETYPE (pbody)))
1509 /* we will walk the body in a pre-order traversal for
1511 switch (pbody->opval.op)
1513 /*------------------------------------------------------------------*/
1515 return isConformingBody (pbody->right, sym, body);
1517 /*------------------------------------------------------------------*/
1522 /*------------------------------------------------------------------*/
1523 case INC_OP: /* incerement operator unary so left only */
1526 /* sure we are not sym is not modified */
1528 IS_AST_SYM_VALUE (pbody->left) &&
1529 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1533 IS_AST_SYM_VALUE (pbody->right) &&
1534 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1539 /*------------------------------------------------------------------*/
1541 case '*': /* can be unary : if right is null then unary operation */
1546 /* if right is NULL then unary operation */
1547 /*------------------------------------------------------------------*/
1548 /*----------------------------*/
1550 /*----------------------------*/
1553 if (IS_AST_SYM_VALUE (pbody->left) &&
1554 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1557 return isConformingBody (pbody->left, sym, body);
1561 if (astHasSymbol (pbody->left, sym) ||
1562 astHasSymbol (pbody->right, sym))
1567 /*------------------------------------------------------------------*/
1575 if (IS_AST_SYM_VALUE (pbody->left) &&
1576 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1579 if (IS_AST_SYM_VALUE (pbody->right) &&
1580 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1583 return isConformingBody (pbody->left, sym, body) &&
1584 isConformingBody (pbody->right, sym, body);
1591 if (IS_AST_SYM_VALUE (pbody->left) &&
1592 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1594 return isConformingBody (pbody->left, sym, body);
1596 /*------------------------------------------------------------------*/
1608 case SIZEOF: /* evaluate wihout code generation */
1610 return isConformingBody (pbody->left, sym, body) &&
1611 isConformingBody (pbody->right, sym, body);
1613 /*------------------------------------------------------------------*/
1616 /* if left has a pointer & right has loop
1617 control variable then we cannot */
1618 if (astHasPointer (pbody->left) &&
1619 astHasSymbol (pbody->right, sym))
1621 if (astHasVolatile (pbody->left))
1624 if (IS_AST_SYM_VALUE (pbody->left) &&
1625 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1628 if (astHasVolatile (pbody->left))
1631 if (astHasDeref(pbody->right)) return FALSE;
1633 return isConformingBody (pbody->left, sym, body) &&
1634 isConformingBody (pbody->right, sym, body);
1645 assert ("Parser should not have generated this\n");
1647 /*------------------------------------------------------------------*/
1648 /*----------------------------*/
1649 /* comma operator */
1650 /*----------------------------*/
1652 return isConformingBody (pbody->left, sym, body) &&
1653 isConformingBody (pbody->right, sym, body);
1655 /*------------------------------------------------------------------*/
1656 /*----------------------------*/
1658 /*----------------------------*/
1662 /*------------------------------------------------------------------*/
1663 /*----------------------------*/
1664 /* return statement */
1665 /*----------------------------*/
1670 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1675 if (astHasSymbol (pbody->left, sym))
1682 return isConformingBody (pbody->left, sym, body) &&
1683 isConformingBody (pbody->right, sym, body);
1689 /*-----------------------------------------------------------------*/
1690 /* isLoopReversible - takes a for loop as input && returns true */
1691 /* if the for loop is reversible. If yes will set the value of */
1692 /* the loop control var & init value & termination value */
1693 /*-----------------------------------------------------------------*/
1695 isLoopReversible (ast * loop, symbol ** loopCntrl,
1696 ast ** init, ast ** end)
1698 /* if option says don't do it then don't */
1699 if (optimize.noLoopReverse)
1701 /* there are several tests to determine this */
1703 /* for loop has to be of the form
1704 for ( <sym> = <const1> ;
1705 [<sym> < <const2>] ;
1706 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1708 if (!isLoopCountable (AST_FOR (loop, initExpr),
1709 AST_FOR (loop, condExpr),
1710 AST_FOR (loop, loopExpr),
1711 loopCntrl, init, end))
1714 /* now do some serious checking on the body of the loop
1717 return isConformingBody (loop->left, *loopCntrl, loop->left);
1721 /*-----------------------------------------------------------------*/
1722 /* replLoopSym - replace the loop sym by loop sym -1 */
1723 /*-----------------------------------------------------------------*/
1725 replLoopSym (ast * body, symbol * sym)
1728 if (!body || IS_AST_LINK (body))
1731 if (IS_AST_SYM_VALUE (body))
1734 if (isSymbolEqual (AST_SYMBOL (body), sym))
1738 body->opval.op = '-';
1739 body->left = newAst_VALUE (symbolVal (sym));
1740 body->right = newAst_VALUE (constVal ("1"));
1748 replLoopSym (body->left, sym);
1749 replLoopSym (body->right, sym);
1753 /*-----------------------------------------------------------------*/
1754 /* reverseLoop - do the actual loop reversal */
1755 /*-----------------------------------------------------------------*/
1757 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1761 /* create the following tree
1766 if (sym) goto for_continue ;
1769 /* put it together piece by piece */
1770 rloop = newNode (NULLOP,
1771 createIf (newAst_VALUE (symbolVal (sym)),
1773 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1776 newAst_VALUE (symbolVal (sym)),
1779 replLoopSym (loop->left, sym);
1781 rloop = newNode (NULLOP,
1783 newAst_VALUE (symbolVal (sym)),
1784 newNode ('-', end, init)),
1785 createLabel (AST_FOR (loop, continueLabel),
1789 newNode (SUB_ASSIGN,
1790 newAst_VALUE (symbolVal (sym)),
1791 newAst_VALUE (constVal ("1"))),
1794 return decorateType (rloop);
1798 //#define DEMAND_INTEGER_PROMOTION
1800 #ifdef DEMAND_INTEGER_PROMOTION
1802 /*-----------------------------------------------------------------*/
1803 /* walk a tree looking for the leaves. Add a typecast to the given */
1804 /* type to each value leaf node. */
1805 /*-----------------------------------------------------------------*/
1807 pushTypeCastToLeaves (sym_link * type, ast * node, ast ** parentPtr)
1809 if (!node || IS_CALLOP(node))
1811 /* WTF? We should never get here. */
1815 if (!node->left && !node->right)
1817 /* We're at a leaf; if it's a value, apply the typecast */
1818 if (node->type == EX_VALUE && IS_INTEGRAL (TTYPE (node)))
1820 *parentPtr = decorateType (newNode (CAST,
1821 newAst_LINK (copyLinkChain (type)),
1829 pushTypeCastToLeaves (type, node->left, &(node->left));
1833 pushTypeCastToLeaves (type, node->right, &(node->right));
1840 /*-----------------------------------------------------------------*/
1841 /* Given an assignment operation in a tree, determine if the LHS */
1842 /* (the result) has a different (integer) type than the RHS. */
1843 /* If so, walk the RHS and add a typecast to the type of the LHS */
1844 /* to all leaf nodes. */
1845 /*-----------------------------------------------------------------*/
1847 propAsgType (ast * tree)
1849 #ifdef DEMAND_INTEGER_PROMOTION
1850 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
1852 /* Nothing to do here... */
1856 if (getSize (LTYPE (tree)) > getSize (RTYPE (tree)))
1858 pushTypeCastToLeaves (LTYPE (tree), tree->right, &(tree->right));
1865 /*-----------------------------------------------------------------*/
1866 /* decorateType - compute type for this tree also does type cheking */
1867 /* this is done bottom up, since type have to flow upwards */
1868 /* it also does constant folding, and paramater checking */
1869 /*-----------------------------------------------------------------*/
1871 decorateType (ast * tree)
1879 /* if already has type then do nothing */
1880 if (tree->decorated)
1883 tree->decorated = 1;
1885 /* print the line */
1886 /* if not block & function */
1887 if (tree->type == EX_OP &&
1888 (tree->opval.op != FUNCTION &&
1889 tree->opval.op != BLOCK &&
1890 tree->opval.op != NULLOP))
1892 filename = tree->filename;
1893 lineno = tree->lineno;
1896 /* if any child is an error | this one is an error do nothing */
1897 if (tree->isError ||
1898 (tree->left && tree->left->isError) ||
1899 (tree->right && tree->right->isError))
1902 /*------------------------------------------------------------------*/
1903 /*----------------------------*/
1904 /* leaf has been reached */
1905 /*----------------------------*/
1906 /* if this is of type value */
1907 /* just get the type */
1908 if (tree->type == EX_VALUE)
1911 if (IS_LITERAL (tree->opval.val->etype))
1914 /* if this is a character array then declare it */
1915 if (IS_ARRAY (tree->opval.val->type))
1916 tree->opval.val = stringToSymbol (tree->opval.val);
1918 /* otherwise just copy the type information */
1919 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1920 if (funcInChain (tree->opval.val->type))
1922 tree->hasVargs = tree->opval.val->sym->hasVargs;
1923 tree->args = copyValueChain (tree->opval.val->sym->args);
1928 if (tree->opval.val->sym)
1930 /* if the undefined flag is set then give error message */
1931 if (tree->opval.val->sym->undefined)
1933 werror (E_ID_UNDEF, tree->opval.val->sym->name);
1935 TTYPE (tree) = TETYPE (tree) =
1936 tree->opval.val->type = tree->opval.val->sym->type =
1937 tree->opval.val->etype = tree->opval.val->sym->etype =
1938 copyLinkChain (INTTYPE);
1943 /* if impilicit i.e. struct/union member then no type */
1944 if (tree->opval.val->sym->implicit)
1945 TTYPE (tree) = TETYPE (tree) = NULL;
1950 /* else copy the type */
1951 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1953 /* and mark it as referenced */
1954 tree->opval.val->sym->isref = 1;
1955 /* if this is of type function or function pointer */
1956 if (funcInChain (tree->opval.val->type))
1958 tree->hasVargs = tree->opval.val->sym->hasVargs;
1959 tree->args = copyValueChain (tree->opval.val->sym->args);
1969 /* if type link for the case of cast */
1970 if (tree->type == EX_LINK)
1972 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
1979 dtl = decorateType (tree->left);
1980 dtr = decorateType (tree->right);
1982 /* this is to take care of situations
1983 when the tree gets rewritten */
1984 if (dtl != tree->left)
1986 if (dtr != tree->right)
1990 /* depending on type of operator do */
1992 switch (tree->opval.op)
1994 /*------------------------------------------------------------------*/
1995 /*----------------------------*/
1997 /*----------------------------*/
2000 /* determine which is the array & which the index */
2001 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) && IS_INTEGRAL (LTYPE (tree)))
2004 ast *tempTree = tree->left;
2005 tree->left = tree->right;
2006 tree->right = tempTree;
2009 /* first check if this is a array or a pointer */
2010 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2012 werror (E_NEED_ARRAY_PTR, "[]");
2013 goto errorTreeReturn;
2016 /* check if the type of the idx */
2017 if (!IS_INTEGRAL (RTYPE (tree)))
2019 werror (E_IDX_NOT_INT);
2020 goto errorTreeReturn;
2023 /* if the left is an rvalue then error */
2026 werror (E_LVALUE_REQUIRED, "array access");
2027 goto errorTreeReturn;
2030 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2031 if (IS_PTR(LTYPE(tree))) {
2032 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
2036 /*------------------------------------------------------------------*/
2037 /*----------------------------*/
2039 /*----------------------------*/
2041 /* if this is not a structure */
2042 if (!IS_STRUCT (LTYPE (tree)))
2044 werror (E_STRUCT_UNION, ".");
2045 goto errorTreeReturn;
2047 TTYPE (tree) = structElemType (LTYPE (tree),
2048 (tree->right->type == EX_VALUE ?
2049 tree->right->opval.val : NULL), &tree->args);
2050 TETYPE (tree) = getSpec (TTYPE (tree));
2053 /*------------------------------------------------------------------*/
2054 /*----------------------------*/
2055 /* struct/union pointer */
2056 /*----------------------------*/
2058 /* if not pointer to a structure */
2059 if (!IS_PTR (LTYPE (tree)))
2061 werror (E_PTR_REQD);
2062 goto errorTreeReturn;
2065 if (!IS_STRUCT (LTYPE (tree)->next))
2067 werror (E_STRUCT_UNION, "->");
2068 goto errorTreeReturn;
2071 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2072 (tree->right->type == EX_VALUE ?
2073 tree->right->opval.val : NULL), &tree->args);
2074 TETYPE (tree) = getSpec (TTYPE (tree));
2077 /*------------------------------------------------------------------*/
2078 /*----------------------------*/
2079 /* ++/-- operation */
2080 /*----------------------------*/
2081 case INC_OP: /* incerement operator unary so left only */
2084 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2085 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2086 if (!tree->initMode && IS_CONSTANT (TETYPE (tree)))
2087 werror (E_CODE_WRITE, "++/--");
2096 /*------------------------------------------------------------------*/
2097 /*----------------------------*/
2099 /*----------------------------*/
2100 case '&': /* can be unary */
2101 /* if right is NULL then unary operation */
2102 if (tree->right) /* not an unary operation */
2105 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2107 werror (E_BITWISE_OP);
2108 werror (W_CONTINUE, "left & right types are ");
2109 printTypeChain (LTYPE (tree), stderr);
2110 fprintf (stderr, ",");
2111 printTypeChain (RTYPE (tree), stderr);
2112 fprintf (stderr, "\n");
2113 goto errorTreeReturn;
2116 /* if they are both literal */
2117 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2119 tree->type = EX_VALUE;
2120 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2121 valFromType (RETYPE (tree)), '&');
2123 tree->right = tree->left = NULL;
2124 TETYPE (tree) = tree->opval.val->etype;
2125 TTYPE (tree) = tree->opval.val->type;
2129 /* see if this is a GETHBIT operation if yes
2132 ast *otree = optimizeGetHbit (tree);
2135 return decorateType (otree);
2139 // we can't do this because of "(int & 0xff) << 3"
2141 /* if right or left is literal then result of that type */
2142 if (IS_LITERAL (RTYPE (tree)))
2145 TTYPE (tree) = copyLinkChain (RTYPE (tree));
2146 TETYPE (tree) = getSpec (TTYPE (tree));
2147 SPEC_SCLS (TETYPE (tree)) = S_AUTO;
2151 if (IS_LITERAL (LTYPE (tree)))
2153 TTYPE (tree) = copyLinkChain (LTYPE (tree));
2154 TETYPE (tree) = getSpec (TTYPE (tree));
2155 SPEC_SCLS (TETYPE (tree)) = S_AUTO;
2161 computeType (LTYPE (tree), RTYPE (tree));
2162 TETYPE (tree) = getSpec (TTYPE (tree));
2167 computeType (LTYPE (tree), RTYPE (tree));
2168 TETYPE (tree) = getSpec (TTYPE (tree));
2170 LRVAL (tree) = RRVAL (tree) = 1;
2174 /*------------------------------------------------------------------*/
2175 /*----------------------------*/
2177 /*----------------------------*/
2179 p->class = DECLARATOR;
2180 /* if bit field then error */
2181 if (IS_BITVAR (tree->left->etype))
2183 werror (E_ILLEGAL_ADDR, "addrress of bit variable");
2184 goto errorTreeReturn;
2187 if (SPEC_SCLS (tree->left->etype) == S_REGISTER)
2189 werror (E_ILLEGAL_ADDR, "address of register variable");
2190 goto errorTreeReturn;
2193 if (IS_FUNC (LTYPE (tree)))
2195 werror (E_ILLEGAL_ADDR, "address of function");
2196 goto errorTreeReturn;
2201 werror (E_LVALUE_REQUIRED, "address of");
2202 goto errorTreeReturn;
2204 if (SPEC_SCLS (tree->left->etype) == S_CODE)
2206 DCL_TYPE (p) = CPOINTER;
2207 DCL_PTR_CONST (p) = port->mem.code_ro;
2209 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2210 DCL_TYPE (p) = FPOINTER;
2211 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2212 DCL_TYPE (p) = PPOINTER;
2213 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2214 DCL_TYPE (p) = IPOINTER;
2215 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2216 DCL_TYPE (p) = EEPPOINTER;
2218 DCL_TYPE (p) = POINTER;
2220 if (IS_AST_SYM_VALUE (tree->left))
2222 AST_SYMBOL (tree->left)->addrtaken = 1;
2223 AST_SYMBOL (tree->left)->allocreq = 1;
2226 p->next = LTYPE (tree);
2228 TETYPE (tree) = getSpec (TTYPE (tree));
2229 DCL_PTR_CONST (p) = SPEC_CONST (TETYPE (tree));
2230 DCL_PTR_VOLATILE (p) = SPEC_VOLATILE (TETYPE (tree));
2235 /*------------------------------------------------------------------*/
2236 /*----------------------------*/
2238 /*----------------------------*/
2240 /* if the rewrite succeeds then don't go any furthur */
2242 ast *wtree = optimizeRRCRLC (tree);
2244 return decorateType (wtree);
2246 /*------------------------------------------------------------------*/
2247 /*----------------------------*/
2249 /*----------------------------*/
2251 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2253 werror (E_BITWISE_OP);
2254 werror (W_CONTINUE, "left & right types are ");
2255 printTypeChain (LTYPE (tree), stderr);
2256 fprintf (stderr, ",");
2257 printTypeChain (RTYPE (tree), stderr);
2258 fprintf (stderr, "\n");
2259 goto errorTreeReturn;
2262 /* if they are both literal then */
2263 /* rewrite the tree */
2264 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2266 tree->type = EX_VALUE;
2267 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2268 valFromType (RETYPE (tree)),
2270 tree->right = tree->left = NULL;
2271 TETYPE (tree) = tree->opval.val->etype;
2272 TTYPE (tree) = tree->opval.val->type;
2275 LRVAL (tree) = RRVAL (tree) = 1;
2276 TETYPE (tree) = getSpec (TTYPE (tree) =
2277 computeType (LTYPE (tree),
2280 /*------------------------------------------------------------------*/
2281 /*----------------------------*/
2283 /*----------------------------*/
2285 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2287 werror (E_INVALID_OP, "divide");
2288 goto errorTreeReturn;
2290 /* if they are both literal then */
2291 /* rewrite the tree */
2292 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2294 tree->type = EX_VALUE;
2295 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2296 valFromType (RETYPE (tree)));
2297 tree->right = tree->left = NULL;
2298 TETYPE (tree) = getSpec (TTYPE (tree) =
2299 tree->opval.val->type);
2302 LRVAL (tree) = RRVAL (tree) = 1;
2303 TETYPE (tree) = getSpec (TTYPE (tree) =
2304 computeType (LTYPE (tree),
2308 /*------------------------------------------------------------------*/
2309 /*----------------------------*/
2311 /*----------------------------*/
2313 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2315 werror (E_BITWISE_OP);
2316 werror (W_CONTINUE, "left & right types are ");
2317 printTypeChain (LTYPE (tree), stderr);
2318 fprintf (stderr, ",");
2319 printTypeChain (RTYPE (tree), stderr);
2320 fprintf (stderr, "\n");
2321 goto errorTreeReturn;
2323 /* if they are both literal then */
2324 /* rewrite the tree */
2325 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2327 tree->type = EX_VALUE;
2328 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2329 valFromType (RETYPE (tree)));
2330 tree->right = tree->left = NULL;
2331 TETYPE (tree) = getSpec (TTYPE (tree) =
2332 tree->opval.val->type);
2335 LRVAL (tree) = RRVAL (tree) = 1;
2336 TETYPE (tree) = getSpec (TTYPE (tree) =
2337 computeType (LTYPE (tree),
2341 /*------------------------------------------------------------------*/
2342 /*----------------------------*/
2343 /* address dereference */
2344 /*----------------------------*/
2345 case '*': /* can be unary : if right is null then unary operation */
2348 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2350 werror (E_PTR_REQD);
2351 goto errorTreeReturn;
2356 werror (E_LVALUE_REQUIRED, "pointer deref");
2357 goto errorTreeReturn;
2359 TTYPE (tree) = copyLinkChain ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) ?
2360 LTYPE (tree)->next : NULL);
2361 TETYPE (tree) = getSpec (TTYPE (tree));
2362 tree->args = tree->left->args;
2363 tree->hasVargs = tree->left->hasVargs;
2364 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
2368 /*------------------------------------------------------------------*/
2369 /*----------------------------*/
2370 /* multiplication */
2371 /*----------------------------*/
2372 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2374 werror (E_INVALID_OP, "multiplication");
2375 goto errorTreeReturn;
2378 /* if they are both literal then */
2379 /* rewrite the tree */
2380 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2382 tree->type = EX_VALUE;
2383 tree->opval.val = valMult (valFromType (LETYPE (tree)),
2384 valFromType (RETYPE (tree)));
2385 tree->right = tree->left = NULL;
2386 TETYPE (tree) = getSpec (TTYPE (tree) =
2387 tree->opval.val->type);
2391 /* if left is a literal exchange left & right */
2392 if (IS_LITERAL (LTYPE (tree)))
2394 ast *tTree = tree->left;
2395 tree->left = tree->right;
2396 tree->right = tTree;
2399 LRVAL (tree) = RRVAL (tree) = 1;
2400 /* promote result to int if left & right are char
2401 this will facilitate hardware multiplies 8bit x 8bit = 16bit */
2402 if (IS_CHAR(LETYPE(tree)) && IS_CHAR(RETYPE(tree))) {
2403 TETYPE (tree) = getSpec (TTYPE (tree) =
2404 computeType (LTYPE (tree),
2406 SPEC_NOUN(TETYPE(tree)) = V_INT;
2408 TETYPE (tree) = getSpec (TTYPE (tree) =
2409 computeType (LTYPE (tree),
2414 /*------------------------------------------------------------------*/
2415 /*----------------------------*/
2416 /* unary '+' operator */
2417 /*----------------------------*/
2422 if (!IS_INTEGRAL (LTYPE (tree)))
2424 werror (E_UNARY_OP, '+');
2425 goto errorTreeReturn;
2428 /* if left is a literal then do it */
2429 if (IS_LITERAL (LTYPE (tree)))
2431 tree->type = EX_VALUE;
2432 tree->opval.val = valFromType (LETYPE (tree));
2434 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2438 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2442 /*------------------------------------------------------------------*/
2443 /*----------------------------*/
2445 /*----------------------------*/
2447 /* this is not a unary operation */
2448 /* if both pointers then problem */
2449 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2450 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
2452 werror (E_PTR_PLUS_PTR);
2453 goto errorTreeReturn;
2456 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2457 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2459 werror (E_PLUS_INVALID, "+");
2460 goto errorTreeReturn;
2463 if (!IS_ARITHMETIC (RTYPE (tree)) &&
2464 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
2466 werror (E_PLUS_INVALID, "+");
2467 goto errorTreeReturn;
2469 /* if they are both literal then */
2470 /* rewrite the tree */
2471 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2473 tree->type = EX_VALUE;
2474 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
2475 valFromType (RETYPE (tree)));
2476 tree->right = tree->left = NULL;
2477 TETYPE (tree) = getSpec (TTYPE (tree) =
2478 tree->opval.val->type);
2482 /* if the right is a pointer or left is a literal
2483 xchange left & right */
2484 if (IS_ARRAY (RTYPE (tree)) ||
2485 IS_PTR (RTYPE (tree)) ||
2486 IS_LITERAL (LTYPE (tree)))
2488 ast *tTree = tree->left;
2489 tree->left = tree->right;
2490 tree->right = tTree;
2493 LRVAL (tree) = RRVAL (tree) = 1;
2494 /* if the left is a pointer */
2495 if (IS_PTR (LTYPE (tree)))
2496 TETYPE (tree) = getSpec (TTYPE (tree) =
2499 TETYPE (tree) = getSpec (TTYPE (tree) =
2500 computeType (LTYPE (tree),
2504 /*------------------------------------------------------------------*/
2505 /*----------------------------*/
2507 /*----------------------------*/
2508 case '-': /* can be unary */
2509 /* if right is null then unary */
2513 if (!IS_ARITHMETIC (LTYPE (tree)))
2515 werror (E_UNARY_OP, tree->opval.op);
2516 goto errorTreeReturn;
2519 /* if left is a literal then do it */
2520 if (IS_LITERAL (LTYPE (tree)))
2522 tree->type = EX_VALUE;
2523 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
2525 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2526 SPEC_USIGN(TETYPE(tree)) = 0;
2530 TTYPE (tree) = LTYPE (tree);
2534 /*------------------------------------------------------------------*/
2535 /*----------------------------*/
2537 /*----------------------------*/
2539 if (!(IS_PTR (LTYPE (tree)) ||
2540 IS_ARRAY (LTYPE (tree)) ||
2541 IS_ARITHMETIC (LTYPE (tree))))
2543 werror (E_PLUS_INVALID, "-");
2544 goto errorTreeReturn;
2547 if (!(IS_PTR (RTYPE (tree)) ||
2548 IS_ARRAY (RTYPE (tree)) ||
2549 IS_ARITHMETIC (RTYPE (tree))))
2551 werror (E_PLUS_INVALID, "-");
2552 goto errorTreeReturn;
2555 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2556 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
2557 IS_INTEGRAL (RTYPE (tree))))
2559 werror (E_PLUS_INVALID, "-");
2560 goto errorTreeReturn;
2563 /* if they are both literal then */
2564 /* rewrite the tree */
2565 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2567 tree->type = EX_VALUE;
2568 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
2569 valFromType (RETYPE (tree)));
2570 tree->right = tree->left = NULL;
2571 TETYPE (tree) = getSpec (TTYPE (tree) =
2572 tree->opval.val->type);
2576 /* if the left & right are equal then zero */
2577 if (isAstEqual (tree->left, tree->right))
2579 tree->type = EX_VALUE;
2580 tree->left = tree->right = NULL;
2581 tree->opval.val = constVal ("0");
2582 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2586 /* if both of them are pointers or arrays then */
2587 /* the result is going to be an integer */
2588 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
2589 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
2590 TETYPE (tree) = TTYPE (tree) = newIntLink ();
2592 /* if only the left is a pointer */
2593 /* then result is a pointer */
2594 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
2595 TETYPE (tree) = getSpec (TTYPE (tree) =
2598 TETYPE (tree) = getSpec (TTYPE (tree) =
2599 computeType (LTYPE (tree),
2601 LRVAL (tree) = RRVAL (tree) = 1;
2604 /*------------------------------------------------------------------*/
2605 /*----------------------------*/
2607 /*----------------------------*/
2609 /* can be only integral type */
2610 if (!IS_INTEGRAL (LTYPE (tree)))
2612 werror (E_UNARY_OP, tree->opval.op);
2613 goto errorTreeReturn;
2616 /* if left is a literal then do it */
2617 if (IS_LITERAL (LTYPE (tree)))
2619 tree->type = EX_VALUE;
2620 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
2622 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2626 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2629 /*------------------------------------------------------------------*/
2630 /*----------------------------*/
2632 /*----------------------------*/
2634 /* can be pointer */
2635 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2636 !IS_PTR (LTYPE (tree)) &&
2637 !IS_ARRAY (LTYPE (tree)))
2639 werror (E_UNARY_OP, tree->opval.op);
2640 goto errorTreeReturn;
2643 /* if left is a literal then do it */
2644 if (IS_LITERAL (LTYPE (tree)))
2646 tree->type = EX_VALUE;
2647 tree->opval.val = valNot (valFromType (LETYPE (tree)));
2649 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2653 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2656 /*------------------------------------------------------------------*/
2657 /*----------------------------*/
2659 /*----------------------------*/
2662 TTYPE (tree) = LTYPE (tree);
2663 TETYPE (tree) = LETYPE (tree);
2667 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2672 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
2674 werror (E_SHIFT_OP_INVALID);
2675 werror (W_CONTINUE, "left & right types are ");
2676 printTypeChain (LTYPE (tree), stderr);
2677 fprintf (stderr, ",");
2678 printTypeChain (RTYPE (tree), stderr);
2679 fprintf (stderr, "\n");
2680 goto errorTreeReturn;
2683 /* if they are both literal then */
2684 /* rewrite the tree */
2685 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2687 tree->type = EX_VALUE;
2688 tree->opval.val = valShift (valFromType (LETYPE (tree)),
2689 valFromType (RETYPE (tree)),
2690 (tree->opval.op == LEFT_OP ? 1 : 0));
2691 tree->right = tree->left = NULL;
2692 TETYPE (tree) = getSpec (TTYPE (tree) =
2693 tree->opval.val->type);
2696 /* if only the right side is a literal & we are
2697 shifting more than size of the left operand then zero */
2698 if (IS_LITERAL (RTYPE (tree)) &&
2699 ((unsigned) floatFromVal (valFromType (RETYPE (tree)))) >=
2700 (getSize (LTYPE (tree)) * 8))
2702 werror (W_SHIFT_CHANGED,
2703 (tree->opval.op == LEFT_OP ? "left" : "right"));
2704 tree->type = EX_VALUE;
2705 tree->left = tree->right = NULL;
2706 tree->opval.val = constVal ("0");
2707 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2710 LRVAL (tree) = RRVAL (tree) = 1;
2711 if (IS_LITERAL (LTYPE (tree)) && !IS_LITERAL (RTYPE (tree)))
2713 COPYTYPE (TTYPE (tree), TETYPE (tree), RTYPE (tree));
2717 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2721 /*------------------------------------------------------------------*/
2722 /*----------------------------*/
2724 /*----------------------------*/
2725 case CAST: /* change the type */
2726 /* cannot cast to an aggregate type */
2727 if (IS_AGGREGATE (LTYPE (tree)))
2729 werror (E_CAST_ILLEGAL);
2730 goto errorTreeReturn;
2733 /* make sure the type is complete and sane */
2734 checkTypeSanity(LETYPE(tree), "(cast)");
2737 /* if the right is a literal replace the tree */
2738 if (IS_LITERAL (RETYPE (tree))) {
2739 if (!IS_PTR (LTYPE (tree))) {
2740 tree->type = EX_VALUE;
2742 valCastLiteral (LTYPE (tree),
2743 floatFromVal (valFromType (RETYPE (tree))));
2746 TTYPE (tree) = tree->opval.val->type;
2747 tree->values.literalFromCast = 1;
2748 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
2749 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
2750 sym_link *rest = LTYPE(tree)->next;
2751 werror(W_LITERAL_GENERIC);
2752 TTYPE(tree) = newLink();
2753 DCL_TYPE(TTYPE(tree)) = FPOINTER;
2754 TTYPE(tree)->next = rest;
2755 tree->left->opval.lnk = TTYPE(tree);
2758 TTYPE (tree) = LTYPE (tree);
2762 TTYPE (tree) = LTYPE (tree);
2766 /* if the right is a literal replace the tree */
2767 if (IS_LITERAL (RETYPE (tree)) && !IS_PTR (LTYPE (tree))) {
2768 tree->type = EX_VALUE;
2770 valCastLiteral (LTYPE (tree),
2771 floatFromVal (valFromType (RETYPE (tree))));
2774 TTYPE (tree) = tree->opval.val->type;
2775 tree->values.literalFromCast = 1;
2777 TTYPE (tree) = LTYPE (tree);
2782 TETYPE (tree) = getSpec (TTYPE (tree));
2786 /*------------------------------------------------------------------*/
2787 /*----------------------------*/
2788 /* logical &&, || */
2789 /*----------------------------*/
2792 /* each must me arithmetic type or be a pointer */
2793 if (!IS_PTR (LTYPE (tree)) &&
2794 !IS_ARRAY (LTYPE (tree)) &&
2795 !IS_INTEGRAL (LTYPE (tree)))
2797 werror (E_COMPARE_OP);
2798 goto errorTreeReturn;
2801 if (!IS_PTR (RTYPE (tree)) &&
2802 !IS_ARRAY (RTYPE (tree)) &&
2803 !IS_INTEGRAL (RTYPE (tree)))
2805 werror (E_COMPARE_OP);
2806 goto errorTreeReturn;
2808 /* if they are both literal then */
2809 /* rewrite the tree */
2810 if (IS_LITERAL (RTYPE (tree)) &&
2811 IS_LITERAL (LTYPE (tree)))
2813 tree->type = EX_VALUE;
2814 tree->opval.val = valLogicAndOr (valFromType (LETYPE (tree)),
2815 valFromType (RETYPE (tree)),
2817 tree->right = tree->left = NULL;
2818 TETYPE (tree) = getSpec (TTYPE (tree) =
2819 tree->opval.val->type);
2822 LRVAL (tree) = RRVAL (tree) = 1;
2823 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2826 /*------------------------------------------------------------------*/
2827 /*----------------------------*/
2828 /* comparison operators */
2829 /*----------------------------*/
2837 ast *lt = optimizeCompare (tree);
2843 /* if they are pointers they must be castable */
2844 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
2846 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2848 werror (E_COMPARE_OP);
2849 fprintf (stderr, "comparing type ");
2850 printTypeChain (LTYPE (tree), stderr);
2851 fprintf (stderr, "to type ");
2852 printTypeChain (RTYPE (tree), stderr);
2853 fprintf (stderr, "\n");
2854 goto errorTreeReturn;
2857 /* else they should be promotable to one another */
2860 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
2861 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
2863 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2865 werror (E_COMPARE_OP);
2866 fprintf (stderr, "comparing type ");
2867 printTypeChain (LTYPE (tree), stderr);
2868 fprintf (stderr, "to type ");
2869 printTypeChain (RTYPE (tree), stderr);
2870 fprintf (stderr, "\n");
2871 goto errorTreeReturn;
2875 /* if they are both literal then */
2876 /* rewrite the tree */
2877 if (IS_LITERAL (RTYPE (tree)) &&
2878 IS_LITERAL (LTYPE (tree)))
2880 tree->type = EX_VALUE;
2881 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
2882 valFromType (RETYPE (tree)),
2884 tree->right = tree->left = NULL;
2885 TETYPE (tree) = getSpec (TTYPE (tree) =
2886 tree->opval.val->type);
2889 LRVAL (tree) = RRVAL (tree) = 1;
2890 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2893 /*------------------------------------------------------------------*/
2894 /*----------------------------*/
2896 /*----------------------------*/
2897 case SIZEOF: /* evaluate wihout code generation */
2898 /* change the type to a integer */
2899 tree->type = EX_VALUE;
2900 sprintf (buffer, "%d", (getSize (tree->right->ftype)));
2901 tree->opval.val = constVal (buffer);
2902 tree->right = tree->left = NULL;
2903 TETYPE (tree) = getSpec (TTYPE (tree) =
2904 tree->opval.val->type);
2907 /*------------------------------------------------------------------*/
2908 /*----------------------------*/
2909 /* conditional operator '?' */
2910 /*----------------------------*/
2912 /* the type is value of the colon operator (on the right) */
2913 assert(IS_COLON_OP(tree->right));
2914 TTYPE (tree) = RTYPE(tree); // #HACK LTYPE(tree).
2915 TETYPE (tree) = getSpec (TTYPE (tree));
2919 /* if they don't match we have a problem */
2920 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2922 werror (E_TYPE_MISMATCH, "conditional operator", " ");
2923 goto errorTreeReturn;
2926 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree));
2927 TETYPE (tree) = getSpec (TTYPE (tree));
2931 /*------------------------------------------------------------------*/
2932 /*----------------------------*/
2933 /* assignment operators */
2934 /*----------------------------*/
2937 /* for these it must be both must be integral */
2938 if (!IS_ARITHMETIC (LTYPE (tree)) ||
2939 !IS_ARITHMETIC (RTYPE (tree)))
2941 werror (E_OPS_INTEGRAL);
2942 goto errorTreeReturn;
2945 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
2947 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
2948 werror (E_CODE_WRITE, " ");
2952 werror (E_LVALUE_REQUIRED, "*= or /=");
2953 goto errorTreeReturn;
2966 /* for these it must be both must be integral */
2967 if (!IS_INTEGRAL (LTYPE (tree)) ||
2968 !IS_INTEGRAL (RTYPE (tree)))
2970 werror (E_OPS_INTEGRAL);
2971 goto errorTreeReturn;
2974 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
2976 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
2977 werror (E_CODE_WRITE, " ");
2981 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
2982 goto errorTreeReturn;
2990 /*------------------------------------------------------------------*/
2991 /*----------------------------*/
2993 /*----------------------------*/
2995 if (!(IS_PTR (LTYPE (tree)) ||
2996 IS_ARITHMETIC (LTYPE (tree))))
2998 werror (E_PLUS_INVALID, "-=");
2999 goto errorTreeReturn;
3002 if (!(IS_PTR (RTYPE (tree)) ||
3003 IS_ARITHMETIC (RTYPE (tree))))
3005 werror (E_PLUS_INVALID, "-=");
3006 goto errorTreeReturn;
3009 TETYPE (tree) = getSpec (TTYPE (tree) =
3010 computeType (LTYPE (tree),
3013 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3014 werror (E_CODE_WRITE, " ");
3018 werror (E_LVALUE_REQUIRED, "-=");
3019 goto errorTreeReturn;
3027 /*------------------------------------------------------------------*/
3028 /*----------------------------*/
3030 /*----------------------------*/
3032 /* this is not a unary operation */
3033 /* if both pointers then problem */
3034 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3036 werror (E_PTR_PLUS_PTR);
3037 goto errorTreeReturn;
3040 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
3042 werror (E_PLUS_INVALID, "+=");
3043 goto errorTreeReturn;
3046 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
3048 werror (E_PLUS_INVALID, "+=");
3049 goto errorTreeReturn;
3052 TETYPE (tree) = getSpec (TTYPE (tree) =
3053 computeType (LTYPE (tree),
3056 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3057 werror (E_CODE_WRITE, " ");
3061 werror (E_LVALUE_REQUIRED, "+=");
3062 goto errorTreeReturn;
3065 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right));
3066 tree->opval.op = '=';
3072 /*------------------------------------------------------------------*/
3073 /*----------------------------*/
3074 /* straight assignemnt */
3075 /*----------------------------*/
3077 /* cannot be an aggregate */
3078 if (IS_AGGREGATE (LTYPE (tree)))
3080 werror (E_AGGR_ASSIGN);
3081 goto errorTreeReturn;
3084 /* they should either match or be castable */
3085 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3087 werror (E_TYPE_MISMATCH, "assignment", " ");
3088 fprintf (stderr, "type --> '");
3089 printTypeChain (RTYPE (tree), stderr);
3090 fprintf (stderr, "' ");
3091 fprintf (stderr, "assigned to type --> '");
3092 printTypeChain (LTYPE (tree), stderr);
3093 fprintf (stderr, "'\n");
3094 goto errorTreeReturn;
3097 /* if the left side of the tree is of type void
3098 then report error */
3099 if (IS_VOID (LTYPE (tree)))
3101 werror (E_CAST_ZERO);
3102 fprintf (stderr, "type --> '");
3103 printTypeChain (RTYPE (tree), stderr);
3104 fprintf (stderr, "' ");
3105 fprintf (stderr, "assigned to type --> '");
3106 printTypeChain (LTYPE (tree), stderr);
3107 fprintf (stderr, "'\n");
3110 /* extra checks for pointer types */
3111 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)) &&
3112 !IS_GENPTR (LTYPE (tree)))
3114 if (DCL_TYPE (LTYPE (tree)) != DCL_TYPE (RTYPE (tree)))
3115 werror (W_PTR_ASSIGN);
3118 TETYPE (tree) = getSpec (TTYPE (tree) =
3122 if (!tree->initMode ) {
3123 if (IS_CONSTANT (LETYPE (tree))) {
3124 werror (E_CODE_WRITE, " ");
3129 werror (E_LVALUE_REQUIRED, "=");
3130 goto errorTreeReturn;
3137 /*------------------------------------------------------------------*/
3138 /*----------------------------*/
3139 /* comma operator */
3140 /*----------------------------*/
3142 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
3145 /*------------------------------------------------------------------*/
3146 /*----------------------------*/
3148 /*----------------------------*/
3152 if (processParms (tree->left,
3154 tree->right, &parmNumber, TRUE))
3155 goto errorTreeReturn;
3157 if (options.stackAuto || IS_RENT (LETYPE (tree)))
3159 tree->left->args = reverseVal (tree->left->args);
3160 reverseParms (tree->right);
3163 tree->args = tree->left->args;
3164 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)->next);
3167 /*------------------------------------------------------------------*/
3168 /*----------------------------*/
3169 /* return statement */
3170 /*----------------------------*/
3175 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
3177 werror (W_RETURN_MISMATCH);
3178 goto errorTreeReturn;
3181 if (IS_VOID (currFunc->type->next)
3183 !IS_VOID (RTYPE (tree)))
3185 werror (E_FUNC_VOID);
3186 goto errorTreeReturn;
3189 /* if there is going to be a casing required then add it */
3190 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
3192 #if 0 && defined DEMAND_INTEGER_PROMOTION
3193 if (IS_INTEGRAL (currFunc->type->next))
3195 pushTypeCastToLeaves (currFunc->type->next, tree->right, &(tree->right));
3201 decorateType (newNode (CAST,
3202 newAst_LINK (copyLinkChain (currFunc->type->next)),
3212 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
3214 werror (E_VOID_FUNC, currFunc->name);
3215 goto errorTreeReturn;
3218 TTYPE (tree) = TETYPE (tree) = NULL;
3221 /*------------------------------------------------------------------*/
3222 /*----------------------------*/
3223 /* switch statement */
3224 /*----------------------------*/
3226 /* the switch value must be an integer */
3227 if (!IS_INTEGRAL (LTYPE (tree)))
3229 werror (E_SWITCH_NON_INTEGER);
3230 goto errorTreeReturn;
3233 TTYPE (tree) = TETYPE (tree) = NULL;
3236 /*------------------------------------------------------------------*/
3237 /*----------------------------*/
3239 /*----------------------------*/
3241 tree->left = backPatchLabels (tree->left,
3244 TTYPE (tree) = TETYPE (tree) = NULL;
3247 /*------------------------------------------------------------------*/
3248 /*----------------------------*/
3250 /*----------------------------*/
3253 decorateType (resolveSymbols (AST_FOR (tree, initExpr)));
3254 decorateType (resolveSymbols (AST_FOR (tree, condExpr)));
3255 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)));
3257 /* if the for loop is reversible then
3258 reverse it otherwise do what we normally
3264 if (isLoopReversible (tree, &sym, &init, &end))
3265 return reverseLoop (tree, sym, init, end);
3267 return decorateType (createFor (AST_FOR (tree, trueLabel),
3268 AST_FOR (tree, continueLabel),
3269 AST_FOR (tree, falseLabel),
3270 AST_FOR (tree, condLabel),
3271 AST_FOR (tree, initExpr),
3272 AST_FOR (tree, condExpr),
3273 AST_FOR (tree, loopExpr),
3277 TTYPE (tree) = TETYPE (tree) = NULL;
3281 /* some error found this tree will be killed */
3283 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3284 tree->opval.op = NULLOP;
3290 /*-----------------------------------------------------------------*/
3291 /* sizeofOp - processes size of operation */
3292 /*-----------------------------------------------------------------*/
3294 sizeofOp (sym_link * type)
3298 /* make sure the type is complete and sane */
3299 checkTypeSanity(type, "(sizeof)");
3301 /* get the size and convert it to character */
3302 sprintf (buff, "%d", getSize (type));
3304 /* now convert into value */
3305 return constVal (buff);
3309 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
3310 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
3311 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
3312 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
3313 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
3314 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
3315 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
3317 /*-----------------------------------------------------------------*/
3318 /* backPatchLabels - change and or not operators to flow control */
3319 /*-----------------------------------------------------------------*/
3321 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
3327 if (!(IS_ANDORNOT (tree)))
3330 /* if this an and */
3333 static int localLbl = 0;
3336 sprintf (buffer, "_and_%d", localLbl++);
3337 localLabel = newSymbol (buffer, NestLevel);
3339 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
3341 /* if left is already a IFX then just change the if true label in that */
3342 if (!IS_IFX (tree->left))
3343 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
3345 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3346 /* right is a IFX then just join */
3347 if (IS_IFX (tree->right))
3348 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3350 tree->right = createLabel (localLabel, tree->right);
3351 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3353 return newNode (NULLOP, tree->left, tree->right);
3356 /* if this is an or operation */
3359 static int localLbl = 0;
3362 sprintf (buffer, "_or_%d", localLbl++);
3363 localLabel = newSymbol (buffer, NestLevel);
3365 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
3367 /* if left is already a IFX then just change the if true label in that */
3368 if (!IS_IFX (tree->left))
3369 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
3371 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3372 /* right is a IFX then just join */
3373 if (IS_IFX (tree->right))
3374 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3376 tree->right = createLabel (localLabel, tree->right);
3377 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3379 return newNode (NULLOP, tree->left, tree->right);
3385 int wasnot = IS_NOT (tree->left);
3386 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
3388 /* if the left is already a IFX */
3389 if (!IS_IFX (tree->left))
3390 tree->left = newNode (IFX, tree->left, NULL);
3394 tree->left->trueLabel = trueLabel;
3395 tree->left->falseLabel = falseLabel;
3399 tree->left->trueLabel = falseLabel;
3400 tree->left->falseLabel = trueLabel;
3407 tree->trueLabel = trueLabel;
3408 tree->falseLabel = falseLabel;
3415 /*-----------------------------------------------------------------*/
3416 /* createBlock - create expression tree for block */
3417 /*-----------------------------------------------------------------*/
3419 createBlock (symbol * decl, ast * body)
3423 /* if the block has nothing */
3427 ex = newNode (BLOCK, NULL, body);
3428 ex->values.sym = decl;
3430 ex->right = ex->right;
3436 /*-----------------------------------------------------------------*/
3437 /* createLabel - creates the expression tree for labels */
3438 /*-----------------------------------------------------------------*/
3440 createLabel (symbol * label, ast * stmnt)
3443 char name[SDCC_NAME_MAX + 1];
3446 /* must create fresh symbol if the symbol name */
3447 /* exists in the symbol table, since there can */
3448 /* be a variable with the same name as the labl */
3449 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
3450 (csym->level == label->level))
3451 label = newSymbol (label->name, label->level);
3453 /* change the name before putting it in add _ */
3454 sprintf (name, "%s", label->name);
3456 /* put the label in the LabelSymbol table */
3457 /* but first check if a label of the same */
3459 if ((csym = findSym (LabelTab, NULL, name)))
3460 werror (E_DUPLICATE_LABEL, label->name);
3462 addSym (LabelTab, label, name, label->level, 0, 0);
3465 label->key = labelKey++;
3466 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
3472 /*-----------------------------------------------------------------*/
3473 /* createCase - generates the parsetree for a case statement */
3474 /*-----------------------------------------------------------------*/
3476 createCase (ast * swStat, ast * caseVal, ast * stmnt)
3478 char caseLbl[SDCC_NAME_MAX + 1];
3482 /* if the switch statement does not exist */
3483 /* then case is out of context */
3486 werror (E_CASE_CONTEXT);
3490 caseVal = decorateType (resolveSymbols (caseVal));
3491 /* if not a constant then error */
3492 if (!IS_LITERAL (caseVal->ftype))
3494 werror (E_CASE_CONSTANT);
3498 /* if not a integer than error */
3499 if (!IS_INTEGRAL (caseVal->ftype))
3501 werror (E_CASE_NON_INTEGER);
3505 /* find the end of the switch values chain */
3506 if (!(val = swStat->values.switchVals.swVals))
3507 swStat->values.switchVals.swVals = caseVal->opval.val;
3510 /* also order the cases according to value */
3512 int cVal = (int) floatFromVal (caseVal->opval.val);
3513 while (val && (int) floatFromVal (val) < cVal)
3519 /* if we reached the end then */
3522 pval->next = caseVal->opval.val;
3526 /* we found a value greater than */
3527 /* the current value we must add this */
3528 /* before the value */
3529 caseVal->opval.val->next = val;
3531 /* if this was the first in chain */
3532 if (swStat->values.switchVals.swVals == val)
3533 swStat->values.switchVals.swVals =
3536 pval->next = caseVal->opval.val;
3541 /* create the case label */
3542 sprintf (caseLbl, "_case_%d_%d",
3543 swStat->values.switchVals.swNum,
3544 (int) floatFromVal (caseVal->opval.val));
3546 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
3551 /*-----------------------------------------------------------------*/
3552 /* createDefault - creates the parse tree for the default statement */
3553 /*-----------------------------------------------------------------*/
3555 createDefault (ast * swStat, ast * stmnt)
3557 char defLbl[SDCC_NAME_MAX + 1];
3559 /* if the switch statement does not exist */
3560 /* then case is out of context */
3563 werror (E_CASE_CONTEXT);
3567 /* turn on the default flag */
3568 swStat->values.switchVals.swDefault = 1;
3570 /* create the label */
3571 sprintf (defLbl, "_default_%d", swStat->values.switchVals.swNum);
3572 return createLabel (newSymbol (defLbl, 0), stmnt);
3575 /*-----------------------------------------------------------------*/
3576 /* createIf - creates the parsetree for the if statement */
3577 /*-----------------------------------------------------------------*/
3579 createIf (ast * condAst, ast * ifBody, ast * elseBody)
3581 static int Lblnum = 0;
3583 symbol *ifTrue, *ifFalse, *ifEnd;
3585 /* if neither exists */
3586 if (!elseBody && !ifBody)
3589 /* create the labels */
3590 sprintf (buffer, "_iffalse_%d", Lblnum);
3591 ifFalse = newSymbol (buffer, NestLevel);
3592 /* if no else body then end == false */
3597 sprintf (buffer, "_ifend_%d", Lblnum);
3598 ifEnd = newSymbol (buffer, NestLevel);
3601 sprintf (buffer, "_iftrue_%d", Lblnum);
3602 ifTrue = newSymbol (buffer, NestLevel);
3606 /* attach the ifTrue label to the top of it body */
3607 ifBody = createLabel (ifTrue, ifBody);
3608 /* attach a goto end to the ifBody if else is present */
3611 ifBody = newNode (NULLOP, ifBody,
3613 newAst_VALUE (symbolVal (ifEnd)),
3615 /* put the elseLabel on the else body */
3616 elseBody = createLabel (ifFalse, elseBody);
3617 /* out the end at the end of the body */
3618 elseBody = newNode (NULLOP,
3620 createLabel (ifEnd, NULL));
3624 ifBody = newNode (NULLOP, ifBody,
3625 createLabel (ifFalse, NULL));
3627 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
3628 if (IS_IFX (condAst))
3631 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
3633 return newNode (NULLOP, ifTree,
3634 newNode (NULLOP, ifBody, elseBody));
3638 /*-----------------------------------------------------------------*/
3639 /* createDo - creates parse tree for do */
3642 /* _docontinue_n: */
3643 /* condition_expression +-> trueLabel -> _dobody_n */
3645 /* +-> falseLabel-> _dobreak_n */
3647 /*-----------------------------------------------------------------*/
3649 createDo (symbol * trueLabel, symbol * continueLabel,
3650 symbol * falseLabel, ast * condAst, ast * doBody)
3655 /* if the body does not exist then it is simple */
3658 condAst = backPatchLabels (condAst, continueLabel, NULL);
3659 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
3660 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
3661 doTree->trueLabel = continueLabel;
3662 doTree->falseLabel = NULL;
3666 /* otherwise we have a body */
3667 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
3669 /* attach the body label to the top */
3670 doBody = createLabel (trueLabel, doBody);
3671 /* attach the continue label to end of body */
3672 doBody = newNode (NULLOP, doBody,
3673 createLabel (continueLabel, NULL));
3675 /* now put the break label at the end */
3676 if (IS_IFX (condAst))
3679 doTree = newIfxNode (condAst, trueLabel, falseLabel);
3681 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
3683 /* putting it together */
3684 return newNode (NULLOP, doBody, doTree);
3687 /*-----------------------------------------------------------------*/
3688 /* createFor - creates parse tree for 'for' statement */
3691 /* condExpr +-> trueLabel -> _forbody_n */
3693 /* +-> falseLabel-> _forbreak_n */
3696 /* _forcontinue_n: */
3698 /* goto _forcond_n ; */
3700 /*-----------------------------------------------------------------*/
3702 createFor (symbol * trueLabel, symbol * continueLabel,
3703 symbol * falseLabel, symbol * condLabel,
3704 ast * initExpr, ast * condExpr, ast * loopExpr,
3709 /* if loopexpression not present then we can generate it */
3710 /* the same way as a while */
3712 return newNode (NULLOP, initExpr,
3713 createWhile (trueLabel, continueLabel,
3714 falseLabel, condExpr, forBody));
3715 /* vanilla for statement */
3716 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3718 if (condExpr && !IS_IFX (condExpr))
3719 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
3722 /* attach condition label to condition */
3723 condExpr = createLabel (condLabel, condExpr);
3725 /* attach body label to body */
3726 forBody = createLabel (trueLabel, forBody);
3728 /* attach continue to forLoop expression & attach */
3729 /* goto the forcond @ and of loopExpression */
3730 loopExpr = createLabel (continueLabel,
3734 newAst_VALUE (symbolVal (condLabel)),
3736 /* now start putting them together */
3737 forTree = newNode (NULLOP, initExpr, condExpr);
3738 forTree = newNode (NULLOP, forTree, forBody);
3739 forTree = newNode (NULLOP, forTree, loopExpr);
3740 /* finally add the break label */
3741 forTree = newNode (NULLOP, forTree,
3742 createLabel (falseLabel, NULL));
3746 /*-----------------------------------------------------------------*/
3747 /* createWhile - creates parse tree for while statement */
3748 /* the while statement will be created as follows */
3750 /* _while_continue_n: */
3751 /* condition_expression +-> trueLabel -> _while_boby_n */
3753 /* +-> falseLabel -> _while_break_n */
3754 /* _while_body_n: */
3756 /* goto _while_continue_n */
3757 /* _while_break_n: */
3758 /*-----------------------------------------------------------------*/
3760 createWhile (symbol * trueLabel, symbol * continueLabel,
3761 symbol * falseLabel, ast * condExpr, ast * whileBody)
3765 /* put the continue label */
3766 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3767 condExpr = createLabel (continueLabel, condExpr);
3768 condExpr->lineno = 0;
3770 /* put the body label in front of the body */
3771 whileBody = createLabel (trueLabel, whileBody);
3772 whileBody->lineno = 0;
3773 /* put a jump to continue at the end of the body */
3774 /* and put break label at the end of the body */
3775 whileBody = newNode (NULLOP,
3778 newAst_VALUE (symbolVal (continueLabel)),
3779 createLabel (falseLabel, NULL)));
3781 /* put it all together */
3782 if (IS_IFX (condExpr))
3783 whileTree = condExpr;
3786 whileTree = newNode (IFX, condExpr, NULL);
3787 /* put the true & false labels in place */
3788 whileTree->trueLabel = trueLabel;
3789 whileTree->falseLabel = falseLabel;
3792 return newNode (NULLOP, whileTree, whileBody);
3795 /*-----------------------------------------------------------------*/
3796 /* optimizeGetHbit - get highest order bit of the expression */
3797 /*-----------------------------------------------------------------*/
3799 optimizeGetHbit (ast * tree)
3802 /* if this is not a bit and */
3803 if (!IS_BITAND (tree))
3806 /* will look for tree of the form
3807 ( expr >> ((sizeof expr) -1) ) & 1 */
3808 if (!IS_AST_LIT_VALUE (tree->right))
3811 if (AST_LIT_VALUE (tree->right) != 1)
3814 if (!IS_RIGHT_OP (tree->left))
3817 if (!IS_AST_LIT_VALUE (tree->left->right))
3820 if ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
3821 (j = (getSize (TTYPE (tree->left->left)) * 8 - 1)))
3824 return decorateType (newNode (GETHBIT, tree->left->left, NULL));
3828 /*-----------------------------------------------------------------*/
3829 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
3830 /*-----------------------------------------------------------------*/
3832 optimizeRRCRLC (ast * root)
3834 /* will look for trees of the form
3835 (?expr << 1) | (?expr >> 7) or
3836 (?expr >> 7) | (?expr << 1) will make that
3837 into a RLC : operation ..
3839 (?expr >> 1) | (?expr << 7) or
3840 (?expr << 7) | (?expr >> 1) will make that
3841 into a RRC operation
3842 note : by 7 I mean (number of bits required to hold the
3844 /* if the root operations is not a | operation the not */
3845 if (!IS_BITOR (root))
3848 /* I have to think of a better way to match patterns this sucks */
3849 /* that aside let start looking for the first case : I use a the
3850 negative check a lot to improve the efficiency */
3851 /* (?expr << 1) | (?expr >> 7) */
3852 if (IS_LEFT_OP (root->left) &&
3853 IS_RIGHT_OP (root->right))
3856 if (!SPEC_USIGN (TETYPE (root->left->left)))
3859 if (!IS_AST_LIT_VALUE (root->left->right) ||
3860 !IS_AST_LIT_VALUE (root->right->right))
3863 /* make sure it is the same expression */
3864 if (!isAstEqual (root->left->left,
3868 if (AST_LIT_VALUE (root->left->right) != 1)
3871 if (AST_LIT_VALUE (root->right->right) !=
3872 (getSize (TTYPE (root->left->left)) * 8 - 1))
3875 /* whew got the first case : create the AST */
3876 return newNode (RLC, root->left->left, NULL);
3880 /* check for second case */
3881 /* (?expr >> 7) | (?expr << 1) */
3882 if (IS_LEFT_OP (root->right) &&
3883 IS_RIGHT_OP (root->left))
3886 if (!SPEC_USIGN (TETYPE (root->left->left)))
3889 if (!IS_AST_LIT_VALUE (root->left->right) ||
3890 !IS_AST_LIT_VALUE (root->right->right))
3893 /* make sure it is the same symbol */
3894 if (!isAstEqual (root->left->left,
3898 if (AST_LIT_VALUE (root->right->right) != 1)
3901 if (AST_LIT_VALUE (root->left->right) !=
3902 (getSize (TTYPE (root->left->left)) * 8 - 1))
3905 /* whew got the first case : create the AST */
3906 return newNode (RLC, root->left->left, NULL);
3911 /* third case for RRC */
3912 /* (?symbol >> 1) | (?symbol << 7) */
3913 if (IS_LEFT_OP (root->right) &&
3914 IS_RIGHT_OP (root->left))
3917 if (!SPEC_USIGN (TETYPE (root->left->left)))
3920 if (!IS_AST_LIT_VALUE (root->left->right) ||
3921 !IS_AST_LIT_VALUE (root->right->right))
3924 /* make sure it is the same symbol */
3925 if (!isAstEqual (root->left->left,
3929 if (AST_LIT_VALUE (root->left->right) != 1)
3932 if (AST_LIT_VALUE (root->right->right) !=
3933 (getSize (TTYPE (root->left->left)) * 8 - 1))
3936 /* whew got the first case : create the AST */
3937 return newNode (RRC, root->left->left, NULL);
3941 /* fourth and last case for now */
3942 /* (?symbol << 7) | (?symbol >> 1) */
3943 if (IS_RIGHT_OP (root->right) &&
3944 IS_LEFT_OP (root->left))
3947 if (!SPEC_USIGN (TETYPE (root->left->left)))
3950 if (!IS_AST_LIT_VALUE (root->left->right) ||
3951 !IS_AST_LIT_VALUE (root->right->right))
3954 /* make sure it is the same symbol */
3955 if (!isAstEqual (root->left->left,
3959 if (AST_LIT_VALUE (root->right->right) != 1)
3962 if (AST_LIT_VALUE (root->left->right) !=
3963 (getSize (TTYPE (root->left->left)) * 8 - 1))
3966 /* whew got the first case : create the AST */
3967 return newNode (RRC, root->left->left, NULL);
3971 /* not found return root */
3975 /*-----------------------------------------------------------------*/
3976 /* optimizeCompare - otimizes compares for bit variables */
3977 /*-----------------------------------------------------------------*/
3979 optimizeCompare (ast * root)
3981 ast *optExpr = NULL;
3984 unsigned int litValue;
3986 /* if nothing then return nothing */
3990 /* if not a compare op then do leaves */
3991 if (!IS_COMPARE_OP (root))
3993 root->left = optimizeCompare (root->left);
3994 root->right = optimizeCompare (root->right);
3998 /* if left & right are the same then depending
3999 of the operation do */
4000 if (isAstEqual (root->left, root->right))
4002 switch (root->opval.op)
4007 optExpr = newAst_VALUE (constVal ("0"));
4012 optExpr = newAst_VALUE (constVal ("1"));
4016 return decorateType (optExpr);
4019 vleft = (root->left->type == EX_VALUE ?
4020 root->left->opval.val : NULL);
4022 vright = (root->right->type == EX_VALUE ?
4023 root->right->opval.val : NULL);
4025 /* if left is a BITVAR in BITSPACE */
4026 /* and right is a LITERAL then opt- */
4027 /* imize else do nothing */
4028 if (vleft && vright &&
4029 IS_BITVAR (vleft->etype) &&
4030 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
4031 IS_LITERAL (vright->etype))
4034 /* if right side > 1 then comparison may never succeed */
4035 if ((litValue = (int) floatFromVal (vright)) > 1)
4037 werror (W_BAD_COMPARE);
4043 switch (root->opval.op)
4045 case '>': /* bit value greater than 1 cannot be */
4046 werror (W_BAD_COMPARE);
4050 case '<': /* bit value < 1 means 0 */
4052 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
4055 case LE_OP: /* bit value <= 1 means no check */
4056 optExpr = newAst_VALUE (vright);
4059 case GE_OP: /* bit value >= 1 means only check for = */
4061 optExpr = newAst_VALUE (vleft);
4066 { /* literal is zero */
4067 switch (root->opval.op)
4069 case '<': /* bit value < 0 cannot be */
4070 werror (W_BAD_COMPARE);
4074 case '>': /* bit value > 0 means 1 */
4076 optExpr = newAst_VALUE (vleft);
4079 case LE_OP: /* bit value <= 0 means no check */
4080 case GE_OP: /* bit value >= 0 means no check */
4081 werror (W_BAD_COMPARE);
4085 case EQ_OP: /* bit == 0 means ! of bit */
4086 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
4090 return decorateType (resolveSymbols (optExpr));
4091 } /* end-of-if of BITVAR */
4096 /*-----------------------------------------------------------------*/
4097 /* addSymToBlock : adds the symbol to the first block we find */
4098 /*-----------------------------------------------------------------*/
4100 addSymToBlock (symbol * sym, ast * tree)
4102 /* reached end of tree or a leaf */
4103 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
4107 if (IS_AST_OP (tree) &&
4108 tree->opval.op == BLOCK)
4111 symbol *lsym = copySymbol (sym);
4113 lsym->next = AST_VALUES (tree, sym);
4114 AST_VALUES (tree, sym) = lsym;
4118 addSymToBlock (sym, tree->left);
4119 addSymToBlock (sym, tree->right);
4122 /*-----------------------------------------------------------------*/
4123 /* processRegParms - do processing for register parameters */
4124 /*-----------------------------------------------------------------*/
4126 processRegParms (value * args, ast * body)
4130 if (IS_REGPARM (args->etype))
4131 addSymToBlock (args->sym, body);
4136 /*-----------------------------------------------------------------*/
4137 /* resetParmKey - resets the operandkeys for the symbols */
4138 /*-----------------------------------------------------------------*/
4139 DEFSETFUNC (resetParmKey)
4150 /*-----------------------------------------------------------------*/
4151 /* createFunction - This is the key node that calls the iCode for */
4152 /* generating the code for a function. Note code */
4153 /* is generated function by function, later when */
4154 /* add inter-procedural analysis this will change */
4155 /*-----------------------------------------------------------------*/
4157 createFunction (symbol * name, ast * body)
4163 iCode *piCode = NULL;
4165 /* if check function return 0 then some problem */
4166 if (checkFunction (name) == 0)
4169 /* create a dummy block if none exists */
4171 body = newNode (BLOCK, NULL, NULL);
4175 /* check if the function name already in the symbol table */
4176 if ((csym = findSym (SymbolTab, NULL, name->name)))
4179 /* special case for compiler defined functions
4180 we need to add the name to the publics list : this
4181 actually means we are now compiling the compiler
4185 addSet (&publics, name);
4191 allocVariables (name);
4193 name->lastLine = yylineno;
4195 processFuncArgs (currFunc, 0);
4197 /* set the stack pointer */
4198 /* PENDING: check this for the mcs51 */
4199 stackPtr = -port->stack.direction * port->stack.call_overhead;
4200 if (IS_ISR (name->etype))
4201 stackPtr -= port->stack.direction * port->stack.isr_overhead;
4202 if (IS_RENT (name->etype) || options.stackAuto)
4203 stackPtr -= port->stack.direction * port->stack.reent_overhead;
4205 xstackPtr = -port->stack.direction * port->stack.call_overhead;
4207 fetype = getSpec (name->type); /* get the specifier for the function */
4208 /* if this is a reentrant function then */
4209 if (IS_RENT (fetype))
4212 allocParms (name->args); /* allocate the parameters */
4214 /* do processing for parameters that are passed in registers */
4215 processRegParms (name->args, body);
4217 /* set the stack pointer */
4221 /* allocate & autoinit the block variables */
4222 processBlockVars (body, &stack, ALLOCATE);
4224 /* save the stack information */
4225 if (options.useXstack)
4226 name->xstack = SPEC_STAK (fetype) = stack;
4228 name->stack = SPEC_STAK (fetype) = stack;
4230 /* name needs to be mangled */
4231 sprintf (name->rname, "%s%s", port->fun_prefix, name->name);
4233 body = resolveSymbols (body); /* resolve the symbols */
4234 body = decorateType (body); /* propagateType & do semantic checks */
4236 ex = newAst_VALUE (symbolVal (name)); /* create name */
4237 ex = newNode (FUNCTION, ex, body);
4238 ex->values.args = name->args;
4242 werror (E_FUNC_NO_CODE, name->name);
4246 /* create the node & generate intermediate code */
4248 codeOutFile = code->oFile;
4249 piCode = iCodeFromAst (ex);
4253 werror (E_FUNC_NO_CODE, name->name);
4257 eBBlockFromiCode (piCode);
4259 /* if there are any statics then do them */
4262 GcurMemmap = statsg;
4263 codeOutFile = statsg->oFile;
4264 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos))));
4270 /* dealloc the block variables */
4271 processBlockVars (body, &stack, DEALLOCATE);
4272 /* deallocate paramaters */
4273 deallocParms (name->args);
4275 if (IS_RENT (fetype))
4278 /* we are done freeup memory & cleanup */
4283 addSet (&operKeyReset, name);
4284 applyToSet (operKeyReset, resetParmKey);
4287 cdbStructBlock (1, cdbFile);
4289 cleanUpLevel (LabelTab, 0);
4290 cleanUpBlock (StructTab, 1);
4291 cleanUpBlock (TypedefTab, 1);
4293 xstack->syms = NULL;
4294 istack->syms = NULL;
4299 #define INDENT(x,f) { int i ; for (i=0;i < x; i++) fprintf(f," "); }
4300 /*-----------------------------------------------------------------*/
4301 /* ast_print : prints the ast (for debugging purposes) */
4302 /*-----------------------------------------------------------------*/
4304 void ast_print (ast * tree, FILE *outfile, int indent)
4309 /* can print only decorated trees */
4310 if (!tree->decorated) return;
4312 /* if any child is an error | this one is an error do nothing */
4313 if (tree->isError ||
4314 (tree->left && tree->left->isError) ||
4315 (tree->right && tree->right->isError)) {
4316 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
4320 /* print the line */
4321 /* if not block & function */
4322 if (tree->type == EX_OP &&
4323 (tree->opval.op != FUNCTION &&
4324 tree->opval.op != BLOCK &&
4325 tree->opval.op != NULLOP)) {
4328 if (tree->opval.op == FUNCTION) {
4329 fprintf(outfile,"FUNCTION (%p) type (",tree);
4330 printTypeChain (tree->ftype,outfile);
4331 fprintf(outfile,")\n");
4332 ast_print(tree->left,outfile,indent+4);
4333 ast_print(tree->right,outfile,indent+4);
4336 if (tree->opval.op == BLOCK) {
4337 symbol *decls = tree->values.sym;
4338 fprintf(outfile,"{\n");
4340 INDENT(indent+4,outfile);
4341 fprintf(outfile,"DECLARE SYMBOL %s, type(",decls->name);
4342 printTypeChain(decls->type,outfile);
4343 fprintf(outfile,")\n");
4345 decls = decls->next;
4347 ast_print(tree->right,outfile,indent+4);
4348 fprintf(outfile,"}\n");
4351 if (tree->opval.op == NULLOP) {
4352 fprintf(outfile,"\n");
4353 ast_print(tree->left,outfile,indent);
4354 fprintf(outfile,"\n");
4355 ast_print(tree->right,outfile,indent);
4358 INDENT(indent,outfile);
4360 /*------------------------------------------------------------------*/
4361 /*----------------------------*/
4362 /* leaf has been reached */
4363 /*----------------------------*/
4364 /* if this is of type value */
4365 /* just get the type */
4366 if (tree->type == EX_VALUE) {
4368 if (IS_LITERAL (tree->opval.val->etype)) {
4369 fprintf(outfile,"CONSTANT (%p) value = %d, 0x%x, %g", tree,
4370 (int) floatFromVal(tree->opval.val),
4371 (int) floatFromVal(tree->opval.val),
4372 floatFromVal(tree->opval.val));
4373 } else if (tree->opval.val->sym) {
4374 /* if the undefined flag is set then give error message */
4375 if (tree->opval.val->sym->undefined) {
4376 fprintf(outfile,"UNDEFINED SYMBOL ");
4378 fprintf(outfile,"SYMBOL ");
4380 fprintf(outfile,"(%p) name= %s ",tree,tree->opval.val->sym->name);
4383 fprintf(outfile," type (");
4384 printTypeChain(tree->ftype,outfile);
4385 fprintf(outfile,")\n");
4387 fprintf(outfile,"\n");
4392 /* if type link for the case of cast */
4393 if (tree->type == EX_LINK) {
4394 fprintf(outfile,"TYPENODE (%p) type = (",tree);
4395 printTypeChain(tree->opval.lnk,outfile);
4396 fprintf(outfile,")\n");
4401 /* depending on type of operator do */
4403 switch (tree->opval.op) {
4404 /*------------------------------------------------------------------*/
4405 /*----------------------------*/
4407 /*----------------------------*/
4409 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
4410 printTypeChain(tree->ftype,outfile);
4411 fprintf(outfile,")\n");
4412 ast_print(tree->left,outfile,indent+4);
4413 ast_print(tree->right,outfile,indent+4);
4416 /*------------------------------------------------------------------*/
4417 /*----------------------------*/
4419 /*----------------------------*/
4421 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
4422 printTypeChain(tree->ftype,outfile);
4423 fprintf(outfile,")\n");
4424 ast_print(tree->left,outfile,indent+4);
4425 ast_print(tree->right,outfile,indent+4);
4428 /*------------------------------------------------------------------*/
4429 /*----------------------------*/
4430 /* struct/union pointer */
4431 /*----------------------------*/
4433 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
4434 printTypeChain(tree->ftype,outfile);
4435 fprintf(outfile,")\n");
4436 ast_print(tree->left,outfile,indent+4);
4437 ast_print(tree->right,outfile,indent+4);
4440 /*------------------------------------------------------------------*/
4441 /*----------------------------*/
4442 /* ++/-- operation */
4443 /*----------------------------*/
4444 case INC_OP: /* incerement operator unary so left only */
4445 fprintf(outfile,"INC_OP (%p) type (",tree);
4446 printTypeChain(tree->ftype,outfile);
4447 fprintf(outfile,")\n");
4448 ast_print(tree->left,outfile,indent+4);
4452 fprintf(outfile,"DEC_OP (%p) type (",tree);
4453 printTypeChain(tree->ftype,outfile);
4454 fprintf(outfile,")\n");
4455 ast_print(tree->left,outfile,indent+4);
4458 /*------------------------------------------------------------------*/
4459 /*----------------------------*/
4461 /*----------------------------*/
4464 fprintf(outfile,"& (%p) type (",tree);
4465 printTypeChain(tree->ftype,outfile);
4466 fprintf(outfile,")\n");
4467 ast_print(tree->left,outfile,indent+4);
4468 ast_print(tree->right,outfile,indent+4);
4470 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
4471 printTypeChain(tree->ftype,outfile);
4472 fprintf(outfile,")\n");
4473 ast_print(tree->left,outfile,indent+4);
4474 ast_print(tree->right,outfile,indent+4);
4477 /*----------------------------*/
4479 /*----------------------------*/
4481 fprintf(outfile,"OR (%p) type (",tree);
4482 printTypeChain(tree->ftype,outfile);
4483 fprintf(outfile,")\n");
4484 ast_print(tree->left,outfile,indent+4);
4485 ast_print(tree->right,outfile,indent+4);
4487 /*------------------------------------------------------------------*/
4488 /*----------------------------*/
4490 /*----------------------------*/
4492 fprintf(outfile,"XOR (%p) type (",tree);
4493 printTypeChain(tree->ftype,outfile);
4494 fprintf(outfile,")\n");
4495 ast_print(tree->left,outfile,indent+4);
4496 ast_print(tree->right,outfile,indent+4);
4499 /*------------------------------------------------------------------*/
4500 /*----------------------------*/
4502 /*----------------------------*/
4504 fprintf(outfile,"DIV (%p) type (",tree);
4505 printTypeChain(tree->ftype,outfile);
4506 fprintf(outfile,")\n");
4507 ast_print(tree->left,outfile,indent+4);
4508 ast_print(tree->right,outfile,indent+4);
4510 /*------------------------------------------------------------------*/
4511 /*----------------------------*/
4513 /*----------------------------*/
4515 fprintf(outfile,"MOD (%p) type (",tree);
4516 printTypeChain(tree->ftype,outfile);
4517 fprintf(outfile,")\n");
4518 ast_print(tree->left,outfile,indent+4);
4519 ast_print(tree->right,outfile,indent+4);
4522 /*------------------------------------------------------------------*/
4523 /*----------------------------*/
4524 /* address dereference */
4525 /*----------------------------*/
4526 case '*': /* can be unary : if right is null then unary operation */
4528 fprintf(outfile,"DEREF (%p) type (",tree);
4529 printTypeChain(tree->ftype,outfile);
4530 fprintf(outfile,")\n");
4531 ast_print(tree->left,outfile,indent+4);
4534 /*------------------------------------------------------------------*/
4535 /*----------------------------*/
4536 /* multiplication */
4537 /*----------------------------*/
4538 fprintf(outfile,"MULT (%p) type (",tree);
4539 printTypeChain(tree->ftype,outfile);
4540 fprintf(outfile,")\n");
4541 ast_print(tree->left,outfile,indent+4);
4542 ast_print(tree->right,outfile,indent+4);
4546 /*------------------------------------------------------------------*/
4547 /*----------------------------*/
4548 /* unary '+' operator */
4549 /*----------------------------*/
4553 fprintf(outfile,"UPLUS (%p) type (",tree);
4554 printTypeChain(tree->ftype,outfile);
4555 fprintf(outfile,")\n");
4556 ast_print(tree->left,outfile,indent+4);
4558 /*------------------------------------------------------------------*/
4559 /*----------------------------*/
4561 /*----------------------------*/
4562 fprintf(outfile,"ADD (%p) type (",tree);
4563 printTypeChain(tree->ftype,outfile);
4564 fprintf(outfile,")\n");
4565 ast_print(tree->left,outfile,indent+4);
4566 ast_print(tree->right,outfile,indent+4);
4569 /*------------------------------------------------------------------*/
4570 /*----------------------------*/
4572 /*----------------------------*/
4573 case '-': /* can be unary */
4575 fprintf(outfile,"UMINUS (%p) type (",tree);
4576 printTypeChain(tree->ftype,outfile);
4577 fprintf(outfile,")\n");
4578 ast_print(tree->left,outfile,indent+4);
4580 /*------------------------------------------------------------------*/
4581 /*----------------------------*/
4583 /*----------------------------*/
4584 fprintf(outfile,"SUB (%p) type (",tree);
4585 printTypeChain(tree->ftype,outfile);
4586 fprintf(outfile,")\n");
4587 ast_print(tree->left,outfile,indent+4);
4588 ast_print(tree->right,outfile,indent+4);
4591 /*------------------------------------------------------------------*/
4592 /*----------------------------*/
4594 /*----------------------------*/
4596 fprintf(outfile,"COMPL (%p) type (",tree);
4597 printTypeChain(tree->ftype,outfile);
4598 fprintf(outfile,")\n");
4599 ast_print(tree->left,outfile,indent+4);
4601 /*------------------------------------------------------------------*/
4602 /*----------------------------*/
4604 /*----------------------------*/
4606 fprintf(outfile,"NOT (%p) type (",tree);
4607 printTypeChain(tree->ftype,outfile);
4608 fprintf(outfile,")\n");
4609 ast_print(tree->left,outfile,indent+4);
4611 /*------------------------------------------------------------------*/
4612 /*----------------------------*/
4614 /*----------------------------*/
4616 fprintf(outfile,"RRC (%p) type (",tree);
4617 printTypeChain(tree->ftype,outfile);
4618 fprintf(outfile,")\n");
4619 ast_print(tree->left,outfile,indent+4);
4623 fprintf(outfile,"RLC (%p) type (",tree);
4624 printTypeChain(tree->ftype,outfile);
4625 fprintf(outfile,")\n");
4626 ast_print(tree->left,outfile,indent+4);
4629 fprintf(outfile,"GETHBIT (%p) type (",tree);
4630 printTypeChain(tree->ftype,outfile);
4631 fprintf(outfile,")\n");
4632 ast_print(tree->left,outfile,indent+4);
4635 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
4636 printTypeChain(tree->ftype,outfile);
4637 fprintf(outfile,")\n");
4638 ast_print(tree->left,outfile,indent+4);
4639 ast_print(tree->right,outfile,indent+4);
4642 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
4643 printTypeChain(tree->ftype,outfile);
4644 fprintf(outfile,")\n");
4645 ast_print(tree->left,outfile,indent+4);
4646 ast_print(tree->right,outfile,indent+4);
4648 /*------------------------------------------------------------------*/
4649 /*----------------------------*/
4651 /*----------------------------*/
4652 case CAST: /* change the type */
4653 fprintf(outfile,"CAST (%p) type (",tree);
4654 printTypeChain(tree->ftype,outfile);
4655 fprintf(outfile,")\n");
4656 ast_print(tree->right,outfile,indent+4);
4660 fprintf(outfile,"ANDAND (%p) type (",tree);
4661 printTypeChain(tree->ftype,outfile);
4662 fprintf(outfile,")\n");
4663 ast_print(tree->left,outfile,indent+4);
4664 ast_print(tree->right,outfile,indent+4);
4667 fprintf(outfile,"OROR (%p) type (",tree);
4668 printTypeChain(tree->ftype,outfile);
4669 fprintf(outfile,")\n");
4670 ast_print(tree->left,outfile,indent+4);
4671 ast_print(tree->right,outfile,indent+4);
4674 /*------------------------------------------------------------------*/
4675 /*----------------------------*/
4676 /* comparison operators */
4677 /*----------------------------*/
4679 fprintf(outfile,"GT(>) (%p) type (",tree);
4680 printTypeChain(tree->ftype,outfile);
4681 fprintf(outfile,")\n");
4682 ast_print(tree->left,outfile,indent+4);
4683 ast_print(tree->right,outfile,indent+4);
4686 fprintf(outfile,"LT(<) (%p) type (",tree);
4687 printTypeChain(tree->ftype,outfile);
4688 fprintf(outfile,")\n");
4689 ast_print(tree->left,outfile,indent+4);
4690 ast_print(tree->right,outfile,indent+4);
4693 fprintf(outfile,"LE(<=) (%p) type (",tree);
4694 printTypeChain(tree->ftype,outfile);
4695 fprintf(outfile,")\n");
4696 ast_print(tree->left,outfile,indent+4);
4697 ast_print(tree->right,outfile,indent+4);
4700 fprintf(outfile,"GE(>=) (%p) type (",tree);
4701 printTypeChain(tree->ftype,outfile);
4702 fprintf(outfile,")\n");
4703 ast_print(tree->left,outfile,indent+4);
4704 ast_print(tree->right,outfile,indent+4);
4707 fprintf(outfile,"EQ(==) (%p) type (",tree);
4708 printTypeChain(tree->ftype,outfile);
4709 fprintf(outfile,")\n");
4710 ast_print(tree->left,outfile,indent+4);
4711 ast_print(tree->right,outfile,indent+4);
4714 fprintf(outfile,"NE(!=) (%p) type (",tree);
4715 printTypeChain(tree->ftype,outfile);
4716 fprintf(outfile,")\n");
4717 ast_print(tree->left,outfile,indent+4);
4718 ast_print(tree->right,outfile,indent+4);
4719 /*------------------------------------------------------------------*/
4720 /*----------------------------*/
4722 /*----------------------------*/
4723 case SIZEOF: /* evaluate wihout code generation */
4724 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
4727 /*------------------------------------------------------------------*/
4728 /*----------------------------*/
4729 /* conditional operator '?' */
4730 /*----------------------------*/
4732 fprintf(outfile,"QUEST(?) (%p) type (",tree);
4733 printTypeChain(tree->ftype,outfile);
4734 fprintf(outfile,")\n");
4735 ast_print(tree->left,outfile,indent+4);
4736 ast_print(tree->right,outfile,indent+4);
4739 fprintf(outfile,"COLON(:) (%p) type (",tree);
4740 printTypeChain(tree->ftype,outfile);
4741 fprintf(outfile,")\n");
4742 ast_print(tree->left,outfile,indent+4);
4743 ast_print(tree->right,outfile,indent+4);
4746 /*------------------------------------------------------------------*/
4747 /*----------------------------*/
4748 /* assignment operators */
4749 /*----------------------------*/
4751 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
4752 printTypeChain(tree->ftype,outfile);
4753 fprintf(outfile,")\n");
4754 ast_print(tree->left,outfile,indent+4);
4755 ast_print(tree->right,outfile,indent+4);
4758 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
4759 printTypeChain(tree->ftype,outfile);
4760 fprintf(outfile,")\n");
4761 ast_print(tree->left,outfile,indent+4);
4762 ast_print(tree->right,outfile,indent+4);
4765 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
4766 printTypeChain(tree->ftype,outfile);
4767 fprintf(outfile,")\n");
4768 ast_print(tree->left,outfile,indent+4);
4769 ast_print(tree->right,outfile,indent+4);
4772 fprintf(outfile,"ORASS(*=) (%p) type (",tree);
4773 printTypeChain(tree->ftype,outfile);
4774 fprintf(outfile,")\n");
4775 ast_print(tree->left,outfile,indent+4);
4776 ast_print(tree->right,outfile,indent+4);
4779 fprintf(outfile,"XORASS(*=) (%p) type (",tree);
4780 printTypeChain(tree->ftype,outfile);
4781 fprintf(outfile,")\n");
4782 ast_print(tree->left,outfile,indent+4);
4783 ast_print(tree->right,outfile,indent+4);
4786 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
4787 printTypeChain(tree->ftype,outfile);
4788 fprintf(outfile,")\n");
4789 ast_print(tree->left,outfile,indent+4);
4790 ast_print(tree->right,outfile,indent+4);
4793 fprintf(outfile,"LSHFTASS(*=) (%p) type (",tree);
4794 printTypeChain(tree->ftype,outfile);
4795 fprintf(outfile,")\n");
4796 ast_print(tree->left,outfile,indent+4);
4797 ast_print(tree->right,outfile,indent+4);
4799 /*------------------------------------------------------------------*/
4800 /*----------------------------*/
4802 /*----------------------------*/
4804 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
4805 printTypeChain(tree->ftype,outfile);
4806 fprintf(outfile,")\n");
4807 ast_print(tree->left,outfile,indent+4);
4808 ast_print(tree->right,outfile,indent+4);
4810 /*------------------------------------------------------------------*/
4811 /*----------------------------*/
4813 /*----------------------------*/
4815 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
4816 printTypeChain(tree->ftype,outfile);
4817 fprintf(outfile,")\n");
4818 ast_print(tree->left,outfile,indent+4);
4819 ast_print(tree->right,outfile,indent+4);
4821 /*------------------------------------------------------------------*/
4822 /*----------------------------*/
4823 /* straight assignemnt */
4824 /*----------------------------*/
4826 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
4827 printTypeChain(tree->ftype,outfile);
4828 fprintf(outfile,")\n");
4829 ast_print(tree->left,outfile,indent+4);
4830 ast_print(tree->right,outfile,indent+4);
4832 /*------------------------------------------------------------------*/
4833 /*----------------------------*/
4834 /* comma operator */
4835 /*----------------------------*/
4837 fprintf(outfile,"COMMA(,) (%p) type (",tree);
4838 printTypeChain(tree->ftype,outfile);
4839 fprintf(outfile,")\n");
4840 ast_print(tree->left,outfile,indent+4);
4841 ast_print(tree->right,outfile,indent+4);
4843 /*------------------------------------------------------------------*/
4844 /*----------------------------*/
4846 /*----------------------------*/
4849 fprintf(outfile,"CALL (%p) type (",tree);
4850 printTypeChain(tree->ftype,outfile);
4851 fprintf(outfile,")\n");
4852 ast_print(tree->left,outfile,indent+4);
4853 ast_print(tree->right,outfile,indent+4);
4856 fprintf(outfile,"PARM ");
4857 ast_print(tree->left,outfile,indent+4);
4858 if (tree->right && !IS_AST_PARAM(tree->right)) {
4859 fprintf(outfile,"PARM ");
4860 ast_print(tree->right,outfile,indent+4);
4863 /*------------------------------------------------------------------*/
4864 /*----------------------------*/
4865 /* return statement */
4866 /*----------------------------*/
4868 fprintf(outfile,"RETURN (%p) type (",tree);
4869 printTypeChain(tree->right->ftype,outfile);
4870 fprintf(outfile,")\n");
4871 ast_print(tree->right,outfile,indent+4);
4873 /*------------------------------------------------------------------*/
4874 /*----------------------------*/
4875 /* label statement */
4876 /*----------------------------*/
4878 fprintf(outfile,"LABEL (%p)",tree);
4879 ast_print(tree->left,outfile,indent+4);
4880 ast_print(tree->right,outfile,indent);
4882 /*------------------------------------------------------------------*/
4883 /*----------------------------*/
4884 /* switch statement */
4885 /*----------------------------*/
4889 fprintf(outfile,"SWITCH (%p) ",tree);
4890 ast_print(tree->left,outfile,0);
4891 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
4892 INDENT(indent+4,outfile);
4893 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
4894 (int) floatFromVal(val),
4895 tree->values.switchVals.swNum,
4896 (int) floatFromVal(val));
4898 ast_print(tree->right,outfile,indent);
4901 /*------------------------------------------------------------------*/
4902 /*----------------------------*/
4904 /*----------------------------*/
4906 ast_print(tree->left,outfile,indent);
4907 INDENT(indent,outfile);
4908 fprintf(outfile,"IF (%p) \n",tree);
4909 if (tree->trueLabel) {
4910 INDENT(indent,outfile);
4911 fprintf(outfile,"NE(==) 0 goto %s\n",tree->trueLabel->name);
4913 if (tree->falseLabel) {
4914 INDENT(indent,outfile);
4915 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
4917 ast_print(tree->right,outfile,indent);
4919 /*------------------------------------------------------------------*/
4920 /*----------------------------*/
4922 /*----------------------------*/
4924 fprintf(outfile,"FOR (%p) \n",tree);
4925 if (AST_FOR( tree, initExpr)) {
4926 INDENT(indent+4,outfile);
4927 fprintf(outfile,"INIT EXPR ");
4928 ast_print(AST_FOR(tree, initExpr),outfile,indent+4);
4930 if (AST_FOR( tree, condExpr)) {
4931 INDENT(indent+4,outfile);
4932 fprintf(outfile,"COND EXPR ");
4933 ast_print(AST_FOR(tree, condExpr),outfile,indent+4);
4935 if (AST_FOR( tree, loopExpr)) {
4936 INDENT(indent+4,outfile);
4937 fprintf(outfile,"LOOP EXPR ");
4938 ast_print(AST_FOR(tree, loopExpr),outfile,indent+4);
4940 fprintf(outfile,"FOR LOOP BODY \n");
4941 ast_print(tree->left,outfile,indent+4);
4950 ast_print(t,stdout,1);