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 -------------------------------------------------------------------------*/
29 set *operKeyReset = NULL;
30 ast *staticAutos = NULL;
33 #define LRVAL(x) x->left->rvalue
34 #define RRVAL(x) x->right->rvalue
35 #define TRVAL(x) x->rvalue
36 #define LLVAL(x) x->left->lvalue
37 #define RLVAL(x) x->right->lvalue
38 #define TLVAL(x) x->lvalue
39 #define RTYPE(x) x->right->ftype
40 #define RETYPE(x) x->right->etype
41 #define LTYPE(x) x->left->ftype
42 #define LETYPE(x) x->left->etype
43 #define TTYPE(x) x->ftype
44 #define TETYPE(x) x->etype
51 ast *createIval (ast *, sym_link *, initList *, ast *);
52 ast *createIvalCharPtr (ast *, sym_link *, ast *);
53 ast *optimizeRRCRLC (ast *);
54 ast *optimizeGetHbit (ast *);
55 ast *backPatchLabels (ast *, symbol *, symbol *);
58 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
63 printTypeChain (tree->ftype, stdout);
68 /*-----------------------------------------------------------------*/
69 /* newAst - creates a fresh node for an expression tree */
70 /*-----------------------------------------------------------------*/
73 newAst (int type, void *op)
76 static int oldLineno = 0;
78 ex = Safe_alloc ( sizeof (ast));
81 ex->lineno = (noLineno ? oldLineno : yylineno);
82 ex->filename = currFname;
83 ex->level = NestLevel;
84 ex->block = currBlockno;
85 ex->initMode = inInitMode;
87 /* depending on the type */
91 ex->opval.val = (value *) op;
94 ex->opval.op = (long) op;
97 ex->opval.lnk = (sym_link *) op;
100 ex->opval.stmnt = (unsigned) op;
108 newAst_ (unsigned type)
111 static int oldLineno = 0;
113 ex = Safe_alloc ( sizeof (ast));
116 ex->lineno = (noLineno ? oldLineno : yylineno);
117 ex->filename = currFname;
118 ex->level = NestLevel;
119 ex->block = currBlockno;
120 ex->initMode = inInitMode;
125 newAst_VALUE (value * val)
127 ast *ex = newAst_ (EX_VALUE);
133 newAst_OP (unsigned op)
135 ast *ex = newAst_ (EX_OP);
141 newAst_LINK (sym_link * val)
143 ast *ex = newAst_ (EX_LINK);
149 newAst_STMNT (unsigned val)
151 ast *ex = newAst_ (EX_STMNT);
152 ex->opval.stmnt = val;
156 /*-----------------------------------------------------------------*/
157 /* newNode - creates a new node */
158 /*-----------------------------------------------------------------*/
160 newNode (long op, ast * left, ast * right)
171 /*-----------------------------------------------------------------*/
172 /* newIfxNode - creates a new Ifx Node */
173 /*-----------------------------------------------------------------*/
175 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
179 /* if this is a literal then we already know the result */
180 if (condAst->etype && IS_LITERAL (condAst->etype))
182 /* then depending on the expression value */
183 if (floatFromVal (condAst->opval.val))
184 ifxNode = newNode (GOTO,
185 newAst_VALUE (symbolVal (trueLabel)),
188 ifxNode = newNode (GOTO,
189 newAst_VALUE (symbolVal (falseLabel)),
194 ifxNode = newNode (IFX, condAst, NULL);
195 ifxNode->trueLabel = trueLabel;
196 ifxNode->falseLabel = falseLabel;
202 /*-----------------------------------------------------------------*/
203 /* copyAstValues - copies value portion of ast if needed */
204 /*-----------------------------------------------------------------*/
206 copyAstValues (ast * dest, ast * src)
208 switch (src->opval.op)
211 dest->values.sym = copySymbolChain (src->values.sym);
215 dest->values.switchVals.swVals =
216 copyValue (src->values.switchVals.swVals);
217 dest->values.switchVals.swDefault =
218 src->values.switchVals.swDefault;
219 dest->values.switchVals.swNum =
220 src->values.switchVals.swNum;
224 dest->values.inlineasm = Safe_alloc (strlen (src->values.inlineasm) + 1);
225 strcpy (dest->values.inlineasm, src->values.inlineasm);
229 dest->values.constlist = copyLiteralList(src->values.constlist);
233 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
234 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
235 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
236 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
237 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
238 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
239 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
244 /*-----------------------------------------------------------------*/
245 /* copyAst - makes a copy of a given astession */
246 /*-----------------------------------------------------------------*/
255 dest = Safe_alloc ( sizeof (ast));
257 dest->type = src->type;
258 dest->lineno = src->lineno;
259 dest->level = src->level;
260 dest->funcName = src->funcName;
263 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
265 /* if this is a leaf */
267 if (src->type == EX_VALUE)
269 dest->opval.val = copyValue (src->opval.val);
274 if (src->type == EX_LINK)
276 dest->opval.lnk = copyLinkChain (src->opval.lnk);
280 dest->opval.op = src->opval.op;
282 /* if this is a node that has special values */
283 copyAstValues (dest, src);
285 dest->trueLabel = copySymbol (src->trueLabel);
286 dest->falseLabel = copySymbol (src->falseLabel);
287 dest->left = copyAst (src->left);
288 dest->right = copyAst (src->right);
294 /*-----------------------------------------------------------------*/
295 /* hasSEFcalls - returns TRUE if tree has a function call */
296 /*-----------------------------------------------------------------*/
298 hasSEFcalls (ast * tree)
303 if (tree->type == EX_OP &&
304 (tree->opval.op == CALL ||
305 tree->opval.op == PCALL ||
306 tree->opval.op == '=' ||
307 tree->opval.op == INC_OP ||
308 tree->opval.op == DEC_OP))
311 return (hasSEFcalls (tree->left) |
312 hasSEFcalls (tree->right));
315 /*-----------------------------------------------------------------*/
316 /* isAstEqual - compares two asts & returns 1 if they are equal */
317 /*-----------------------------------------------------------------*/
319 isAstEqual (ast * t1, ast * t2)
328 if (t1->type != t2->type)
334 if (t1->opval.op != t2->opval.op)
336 return (isAstEqual (t1->left, t2->left) &&
337 isAstEqual (t1->right, t2->right));
341 if (t1->opval.val->sym)
343 if (!t2->opval.val->sym)
346 return isSymbolEqual (t1->opval.val->sym,
351 if (t2->opval.val->sym)
354 return (floatFromVal (t1->opval.val) ==
355 floatFromVal (t2->opval.val));
359 /* only compare these two types */
367 /*-----------------------------------------------------------------*/
368 /* resolveSymbols - resolve symbols from the symbol table */
369 /*-----------------------------------------------------------------*/
371 resolveSymbols (ast * tree)
373 /* walk the entire tree and check for values */
374 /* with symbols if we find one then replace */
375 /* symbol with that from the symbol table */
381 /* if not block & function */
382 if (tree->type == EX_OP &&
383 (tree->opval.op != FUNCTION &&
384 tree->opval.op != BLOCK &&
385 tree->opval.op != NULLOP))
387 filename = tree->filename;
388 lineno = tree->lineno;
391 /* make sure we resolve the true & false labels for ifx */
392 if (tree->type == EX_OP && tree->opval.op == IFX)
398 if ((csym = findSym (LabelTab, tree->trueLabel,
399 tree->trueLabel->name)))
400 tree->trueLabel = csym;
402 werror (E_LABEL_UNDEF, tree->trueLabel->name);
405 if (tree->falseLabel)
407 if ((csym = findSym (LabelTab,
409 tree->falseLabel->name)))
410 tree->falseLabel = csym;
412 werror (E_LABEL_UNDEF, tree->falseLabel->name);
417 /* if this is a label resolve it from the labelTab */
418 if (IS_AST_VALUE (tree) &&
419 tree->opval.val->sym &&
420 tree->opval.val->sym->islbl)
423 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
424 tree->opval.val->sym->name);
427 werror (E_LABEL_UNDEF, tree->opval.val->sym->name);
429 tree->opval.val->sym = csym;
431 goto resolveChildren;
434 /* do only for leafs */
435 if (IS_AST_VALUE (tree) &&
436 tree->opval.val->sym &&
437 !tree->opval.val->sym->implicit)
440 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
442 /* if found in the symbol table & they r not the same */
443 if (csym && tree->opval.val->sym != csym)
445 tree->opval.val->sym = csym;
446 tree->opval.val->type = csym->type;
447 tree->opval.val->etype = csym->etype;
450 /* if not found in the symbol table */
451 /* mark it as undefined assume it is */
452 /* an integer in data space */
453 if (!csym && !tree->opval.val->sym->implicit)
456 /* if this is a function name then */
457 /* mark it as returning an int */
460 tree->opval.val->sym->type = newLink ();
461 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
462 tree->opval.val->sym->type->next =
463 tree->opval.val->sym->etype = newIntLink ();
464 tree->opval.val->etype = tree->opval.val->etype;
465 tree->opval.val->type = tree->opval.val->sym->type;
466 werror (W_IMPLICIT_FUNC, tree->opval.val->sym->name);
467 allocVariables (tree->opval.val->sym);
471 tree->opval.val->sym->undefined = 1;
472 tree->opval.val->type =
473 tree->opval.val->etype = newIntLink ();
474 tree->opval.val->sym->type =
475 tree->opval.val->sym->etype = newIntLink ();
481 resolveSymbols (tree->left);
482 resolveSymbols (tree->right);
487 /*-----------------------------------------------------------------*/
488 /* setAstLineno - walks a ast tree & sets the line number */
489 /*-----------------------------------------------------------------*/
491 setAstLineno (ast * tree, int lineno)
496 tree->lineno = lineno;
497 setAstLineno (tree->left, lineno);
498 setAstLineno (tree->right, lineno);
503 /* this functions seems to be superfluous?! kmh */
505 /*-----------------------------------------------------------------*/
506 /* resolveFromTable - will return the symbal table value */
507 /*-----------------------------------------------------------------*/
509 resolveFromTable (value * val)
516 csym = findSymWithLevel (SymbolTab, val->sym);
518 /* if found in the symbol table & they r not the same */
519 if (csym && val->sym != csym &&
520 csym->level == val->sym->level &&
526 val->type = csym->type;
527 val->etype = csym->etype;
534 /*-----------------------------------------------------------------*/
535 /* funcOfType :- function of type with name */
536 /*-----------------------------------------------------------------*/
538 funcOfType (char *name, sym_link * type, sym_link * argType,
542 /* create the symbol */
543 sym = newSymbol (name, 0);
545 /* setup return value */
546 sym->type = newLink ();
547 DCL_TYPE (sym->type) = FUNCTION;
548 sym->type->next = copyLinkChain (type);
549 sym->etype = getSpec (sym->type);
550 FUNC_ISREENT(sym->type) = rent;
552 /* if arguments required */
556 args = FUNC_ARGS(sym->type) = newValue ();
560 args->type = copyLinkChain (argType);
561 args->etype = getSpec (args->type);
564 args = args->next = newValue ();
571 allocVariables (sym);
576 /*-----------------------------------------------------------------*/
577 /* reverseParms - will reverse a parameter tree */
578 /*-----------------------------------------------------------------*/
580 reverseParms (ast * ptree)
586 /* top down if we find a nonParm tree then quit */
587 if (ptree->type == EX_OP && ptree->opval.op == PARAM)
590 ptree->left = ptree->right;
591 ptree->right = ttree;
592 reverseParms (ptree->left);
593 reverseParms (ptree->right);
599 /*-----------------------------------------------------------------*/
600 /* processParms - makes sure the parameters are okay and do some */
601 /* processing with them */
602 /*-----------------------------------------------------------------*/
604 processParms (ast * func,
607 int *parmNumber, // unused, although updated
610 /* if none of them exist */
611 if (!defParm && !actParm)
615 if (getenv("DEBUG_SANITY")) {
616 fprintf (stderr, "processParms: %s ", defParm->name);
618 /* make sure the type is complete and sane */
619 checkTypeSanity(defParm->etype, defParm->name);
622 /* if the function is being called via a pointer & */
623 /* it has not been defined a reentrant then we cannot */
624 /* have parameters */
625 if (func->type != EX_VALUE && !IFFUNC_ISREENT (func->ftype) && !options.stackAuto)
627 werror (W_NONRENT_ARGS);
631 /* if defined parameters ended but actual parameters */
632 /* exist and this is not defined as a variable arg */
633 if (!defParm && actParm && !IFFUNC_HASVARARGS(func->ftype))
635 werror (E_TOO_MANY_PARMS);
639 /* if defined parameters present but no actual parameters */
640 if (defParm && !actParm)
642 werror (E_TOO_FEW_PARMS);
646 if (IS_VOID(actParm->ftype)) {
647 werror (E_VOID_VALUE_USED);
651 /* If this is a varargs function... */
652 if (!defParm && actParm && IFFUNC_HASVARARGS(func->ftype))
657 if (IS_CAST_OP (actParm)
658 || (IS_AST_LIT_VALUE (actParm) && actParm->values.literalFromCast))
660 /* Parameter was explicitly typecast; don't touch it. */
664 /* The ternary ('?') operator is weird: the ftype of the
665 * operator is the type of the condition, but it will return a
666 * (possibly) different type.
668 if (IS_TERNARY_OP(actParm))
670 assert(IS_COLON_OP(actParm->right));
671 assert(actParm->right->left);
672 ftype = actParm->right->left->ftype;
676 ftype = actParm->ftype;
679 /* If it's a small integer, upcast to int. */
680 if (IS_INTEGRAL (ftype)
681 && (getSize (ftype) < (unsigned) INTSIZE))
683 newType = newAst_LINK(INTTYPE);
686 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
688 newType = newAst_LINK (copyLinkChain(ftype));
689 DCL_TYPE (newType->opval.lnk) = GPOINTER;
692 if (IS_AGGREGATE (ftype))
694 newType = newAst_LINK (copyLinkChain (ftype));
695 DCL_TYPE (newType->opval.lnk) = GPOINTER;
699 /* cast required; change this op to a cast. */
700 ast *parmCopy = decorateType(resolveSymbols (copyAst (actParm)));
702 actParm->type = EX_OP;
703 actParm->opval.op = CAST;
704 actParm->left = newType;
705 actParm->right = parmCopy;
706 decorateType (actParm);
708 else if (actParm->type == EX_OP && actParm->opval.op == PARAM)
710 return (processParms (func, NULL, actParm->left, parmNumber, FALSE) ||
711 processParms (func, NULL, actParm->right, parmNumber, rightmost));
716 /* if defined parameters ended but actual has not & */
718 if (!defParm && actParm &&
719 (options.stackAuto || IFFUNC_ISREENT (func->ftype)))
722 resolveSymbols (actParm);
723 /* if this is a PARAM node then match left & right */
724 if (actParm->type == EX_OP && actParm->opval.op == PARAM)
726 return (processParms (func, defParm, actParm->left, parmNumber, FALSE) ||
727 processParms (func, defParm->next, actParm->right, parmNumber, rightmost));
731 /* If we have found a value node by following only right-hand links,
732 * then we know that there are no more values after us.
734 * Therefore, if there are more defined parameters, the caller didn't
737 if (rightmost && defParm->next)
739 werror (E_TOO_FEW_PARMS);
744 /* the parameter type must be at least castable */
745 if (compareType (defParm->type, actParm->ftype) == 0) {
746 werror (E_INCOMPAT_TYPES);
747 printFromToType (actParm->ftype, defParm->type);
751 /* if the parameter is castable then add the cast */
752 if (compareType (defParm->type, actParm->ftype) < 0)
754 ast *pTree = decorateType(resolveSymbols (copyAst (actParm)));
756 /* now change the current one to a cast */
757 actParm->type = EX_OP;
758 actParm->opval.op = CAST;
759 actParm->left = newAst_LINK (defParm->type);
760 actParm->right = pTree;
761 actParm->etype = defParm->etype;
762 actParm->ftype = defParm->type;
765 /* make a copy and change the regparm type to the defined parm */
766 actParm->etype = getSpec (actParm->ftype = copyLinkChain (actParm->ftype));
767 SPEC_REGPARM (actParm->etype) = SPEC_REGPARM (defParm->etype);
771 /*-----------------------------------------------------------------*/
772 /* createIvalType - generates ival for basic types */
773 /*-----------------------------------------------------------------*/
775 createIvalType (ast * sym, sym_link * type, initList * ilist)
779 /* if initList is deep */
780 if (ilist->type == INIT_DEEP)
781 ilist = ilist->init.deep;
783 iExpr = decorateType (resolveSymbols (list2expr (ilist)));
784 return decorateType (newNode ('=', sym, iExpr));
787 /*-----------------------------------------------------------------*/
788 /* createIvalStruct - generates initial value for structures */
789 /*-----------------------------------------------------------------*/
791 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
798 sflds = SPEC_STRUCT (type)->fields;
799 if (ilist->type != INIT_DEEP)
801 werror (E_INIT_STRUCT, "");
805 iloop = ilist->init.deep;
807 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
809 /* if we have come to end */
813 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
814 lAst = decorateType (resolveSymbols (lAst));
815 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast)));
821 /*-----------------------------------------------------------------*/
822 /* createIvalArray - generates code for array initialization */
823 /*-----------------------------------------------------------------*/
825 createIvalArray (ast * sym, sym_link * type, initList * ilist)
829 int lcnt = 0, size = 0;
830 literalList *literalL;
832 /* take care of the special case */
833 /* array of characters can be init */
835 if (IS_CHAR (type->next))
836 if ((rast = createIvalCharPtr (sym,
838 decorateType (resolveSymbols (list2expr (ilist))))))
840 return decorateType (resolveSymbols (rast));
842 /* not the special case */
843 if (ilist->type != INIT_DEEP)
845 werror (E_INIT_STRUCT, "");
849 iloop = ilist->init.deep;
850 lcnt = DCL_ELEM (type);
852 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
856 aSym = decorateType (resolveSymbols(sym));
858 rast = newNode(ARRAYINIT, aSym, NULL);
859 rast->values.constlist = literalL;
861 // Make sure size is set to length of initializer list.
868 if (lcnt && size > lcnt)
870 // Array size was specified, and we have more initializers than needed.
871 char *name=sym->opval.val->sym->name;
872 int lineno=sym->opval.val->sym->lineDef;
874 werror (W_EXESS_ARRAY_INITIALIZERS, name, lineno);
883 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
884 aSym = decorateType (resolveSymbols (aSym));
885 rast = createIval (aSym, type->next, iloop, rast);
886 iloop = (iloop ? iloop->next : NULL);
892 /* no of elements given and we */
893 /* have generated for all of them */
896 // there has to be a better way
897 char *name=sym->opval.val->sym->name;
898 int lineno=sym->opval.val->sym->lineDef;
899 werror (W_EXESS_ARRAY_INITIALIZERS, name, lineno);
906 /* if we have not been given a size */
907 if (!DCL_ELEM (type))
909 DCL_ELEM (type) = size;
912 return decorateType (resolveSymbols (rast));
916 /*-----------------------------------------------------------------*/
917 /* createIvalCharPtr - generates initial values for char pointers */
918 /*-----------------------------------------------------------------*/
920 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
924 /* if this is a pointer & right is a literal array then */
925 /* just assignment will do */
926 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
927 SPEC_SCLS (iexpr->etype) == S_CODE)
928 && IS_ARRAY (iexpr->ftype)))
929 return newNode ('=', sym, iexpr);
931 /* left side is an array so we have to assign each */
933 if ((IS_LITERAL (iexpr->etype) ||
934 SPEC_SCLS (iexpr->etype) == S_CODE)
935 && IS_ARRAY (iexpr->ftype))
937 /* for each character generate an assignment */
938 /* to the array element */
939 char *s = SPEC_CVAL (iexpr->etype).v_char;
944 rast = newNode (NULLOP,
948 newAst_VALUE (valueFromLit ((float) i))),
949 newAst_VALUE (valueFromLit (*s))));
953 rast = newNode (NULLOP,
957 newAst_VALUE (valueFromLit ((float) i))),
958 newAst_VALUE (valueFromLit (*s))));
959 return decorateType (resolveSymbols (rast));
965 /*-----------------------------------------------------------------*/
966 /* createIvalPtr - generates initial value for pointers */
967 /*-----------------------------------------------------------------*/
969 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
975 if (ilist->type == INIT_DEEP)
976 ilist = ilist->init.deep;
978 iexpr = decorateType (resolveSymbols (list2expr (ilist)));
980 /* if character pointer */
981 if (IS_CHAR (type->next))
982 if ((rast = createIvalCharPtr (sym, type, iexpr)))
985 return newNode ('=', sym, iexpr);
988 /*-----------------------------------------------------------------*/
989 /* createIval - generates code for initial value */
990 /*-----------------------------------------------------------------*/
992 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
999 /* if structure then */
1000 if (IS_STRUCT (type))
1001 rast = createIvalStruct (sym, type, ilist);
1003 /* if this is a pointer */
1005 rast = createIvalPtr (sym, type, ilist);
1007 /* if this is an array */
1008 if (IS_ARRAY (type))
1009 rast = createIvalArray (sym, type, ilist);
1011 /* if type is SPECIFIER */
1013 rast = createIvalType (sym, type, ilist);
1016 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)));
1018 return decorateType (resolveSymbols (rast));
1021 /*-----------------------------------------------------------------*/
1022 /* initAggregates - initialises aggregate variables with initv */
1023 /*-----------------------------------------------------------------*/
1025 /* this has to go */ void printIval (symbol *, sym_link *, initList *, FILE *);
1027 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1031 if (getenv("TRY_THE_NEW_INITIALIZER")) {
1033 if (!TARGET_IS_MCS51 || !(options.model==MODEL_LARGE)) {
1034 fprintf (stderr, "Can't \"TRY_THE_NEW_INITIALIZER\" unless "
1035 "with -mmcs51 and --model-large\n");
1039 if (SPEC_OCLS(sym->etype)==xdata &&
1040 getSize(sym->type) > 16) { // else it isn't worth it: do it the old way
1043 newSym=copySymbol (sym);
1044 SPEC_OCLS(newSym->etype)=code;
1045 sprintf (newSym->name, "%s_init__", sym->name);
1046 sprintf (newSym->rname,"%s_init__", sym->rname);
1047 addSym (SymbolTab, newSym, newSym->name, 0, 0, 1);
1049 // emit it in the static segment
1050 addSet(&statsg->syms, newSym);
1052 // now memcpy() the entire array from cseg
1053 ast=newNode (ARRAYINIT, // ASSIGN_AGGREGATE
1054 newAst_VALUE (symbolVal (sym)),
1055 newAst_VALUE (symbolVal (newSym)));
1056 return decorateType(resolveSymbols(ast));
1060 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1063 /*-----------------------------------------------------------------*/
1064 /* gatherAutoInit - creates assignment expressions for initial */
1066 /*-----------------------------------------------------------------*/
1068 gatherAutoInit (symbol * autoChain)
1075 for (sym = autoChain; sym; sym = sym->next)
1078 /* resolve the symbols in the ival */
1080 resolveIvalSym (sym->ival);
1082 /* if this is a static variable & has an */
1083 /* initial value the code needs to be lifted */
1084 /* here to the main portion since they can be */
1085 /* initialised only once at the start */
1086 if (IS_STATIC (sym->etype) && sym->ival &&
1087 SPEC_SCLS (sym->etype) != S_CODE)
1091 // this can only be a constant
1092 if (!inInitMode && !IS_LITERAL(sym->ival->init.node->etype)) {
1093 werror (E_CONST_EXPECTED);
1096 /* insert the symbol into the symbol table */
1097 /* with level = 0 & name = rname */
1098 newSym = copySymbol (sym);
1099 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1101 /* now lift the code to main */
1102 if (IS_AGGREGATE (sym->type))
1103 work = initAggregates (sym, sym->ival, NULL);
1105 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1106 list2expr (sym->ival));
1108 setAstLineno (work, sym->lineDef);
1112 staticAutos = newNode (NULLOP, staticAutos, work);
1119 /* if there is an initial value */
1120 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1122 if (IS_AGGREGATE (sym->type))
1123 work = initAggregates (sym, sym->ival, NULL);
1125 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1126 list2expr (sym->ival));
1128 setAstLineno (work, sym->lineDef);
1131 init = newNode (NULLOP, init, work);
1140 /*-----------------------------------------------------------------*/
1141 /* stringToSymbol - creates a symbol from a literal string */
1142 /*-----------------------------------------------------------------*/
1144 stringToSymbol (value * val)
1146 char name[SDCC_NAME_MAX + 1];
1147 static int charLbl = 0;
1150 sprintf (name, "_str_%d", charLbl++);
1151 sym = newSymbol (name, 0); /* make it @ level 0 */
1152 strcpy (sym->rname, name);
1154 /* copy the type from the value passed */
1155 sym->type = copyLinkChain (val->type);
1156 sym->etype = getSpec (sym->type);
1157 /* change to storage class & output class */
1158 SPEC_SCLS (sym->etype) = S_CODE;
1159 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1160 SPEC_STAT (sym->etype) = 1;
1161 /* make the level & block = 0 */
1162 sym->block = sym->level = 0;
1164 /* create an ival */
1165 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1170 allocVariables (sym);
1173 return symbolVal (sym);
1177 /*-----------------------------------------------------------------*/
1178 /* processBlockVars - will go thru the ast looking for block if */
1179 /* a block is found then will allocate the syms */
1180 /* will also gather the auto inits present */
1181 /*-----------------------------------------------------------------*/
1183 processBlockVars (ast * tree, int *stack, int action)
1188 /* if this is a block */
1189 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1193 if (action == ALLOCATE)
1195 *stack += allocVariables (tree->values.sym);
1196 autoInit = gatherAutoInit (tree->values.sym);
1198 /* if there are auto inits then do them */
1200 tree->left = newNode (NULLOP, autoInit, tree->left);
1202 else /* action is deallocate */
1203 deallocLocal (tree->values.sym);
1206 processBlockVars (tree->left, stack, action);
1207 processBlockVars (tree->right, stack, action);
1211 /*-----------------------------------------------------------------*/
1212 /* constExprValue - returns the value of a constant expression */
1213 /* or NULL if it is not a constant expression */
1214 /*-----------------------------------------------------------------*/
1216 constExprValue (ast * cexpr, int check)
1218 cexpr = decorateType (resolveSymbols (cexpr));
1220 /* if this is not a constant then */
1221 if (!IS_LITERAL (cexpr->ftype))
1223 /* then check if this is a literal array
1225 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1226 SPEC_CVAL (cexpr->etype).v_char &&
1227 IS_ARRAY (cexpr->ftype))
1229 value *val = valFromType (cexpr->ftype);
1230 SPEC_SCLS (val->etype) = S_LITERAL;
1231 val->sym = cexpr->opval.val->sym;
1232 val->sym->type = copyLinkChain (cexpr->ftype);
1233 val->sym->etype = getSpec (val->sym->type);
1234 strcpy (val->name, cexpr->opval.val->sym->rname);
1238 /* if we are casting a literal value then */
1239 if (IS_AST_OP (cexpr) &&
1240 cexpr->opval.op == CAST &&
1241 IS_LITERAL (cexpr->left->ftype))
1242 return valCastLiteral (cexpr->ftype,
1243 floatFromVal (cexpr->left->opval.val));
1245 if (IS_AST_VALUE (cexpr))
1246 return cexpr->opval.val;
1249 werror (E_CONST_EXPECTED, "found expression");
1254 /* return the value */
1255 return cexpr->opval.val;
1259 /*-----------------------------------------------------------------*/
1260 /* isLabelInAst - will return true if a given label is found */
1261 /*-----------------------------------------------------------------*/
1263 isLabelInAst (symbol * label, ast * tree)
1265 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1268 if (IS_AST_OP (tree) &&
1269 tree->opval.op == LABEL &&
1270 isSymbolEqual (AST_SYMBOL (tree->left), label))
1273 return isLabelInAst (label, tree->right) &&
1274 isLabelInAst (label, tree->left);
1278 /*-----------------------------------------------------------------*/
1279 /* isLoopCountable - return true if the loop count can be determi- */
1280 /* -ned at compile time . */
1281 /*-----------------------------------------------------------------*/
1283 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1284 symbol ** sym, ast ** init, ast ** end)
1287 /* the loop is considered countable if the following
1288 conditions are true :-
1290 a) initExpr :- <sym> = <const>
1291 b) condExpr :- <sym> < <const1>
1292 c) loopExpr :- <sym> ++
1295 /* first check the initExpr */
1296 if (IS_AST_OP (initExpr) &&
1297 initExpr->opval.op == '=' && /* is assignment */
1298 IS_AST_SYM_VALUE (initExpr->left))
1299 { /* left is a symbol */
1301 *sym = AST_SYMBOL (initExpr->left);
1302 *init = initExpr->right;
1307 /* for now the symbol has to be of
1309 if (!IS_INTEGRAL ((*sym)->type))
1312 /* now check condExpr */
1313 if (IS_AST_OP (condExpr))
1316 switch (condExpr->opval.op)
1319 if (IS_AST_SYM_VALUE (condExpr->left) &&
1320 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1321 IS_AST_LIT_VALUE (condExpr->right))
1323 *end = condExpr->right;
1329 if (IS_AST_OP (condExpr->left) &&
1330 condExpr->left->opval.op == '>' &&
1331 IS_AST_LIT_VALUE (condExpr->left->right) &&
1332 IS_AST_SYM_VALUE (condExpr->left->left) &&
1333 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1336 *end = newNode ('+', condExpr->left->right,
1337 newAst_VALUE (constVal ("1")));
1348 /* check loop expression is of the form <sym>++ */
1349 if (!IS_AST_OP (loopExpr))
1352 /* check if <sym> ++ */
1353 if (loopExpr->opval.op == INC_OP)
1359 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1360 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1367 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1368 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1376 if (loopExpr->opval.op == ADD_ASSIGN)
1379 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1380 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1381 IS_AST_LIT_VALUE (loopExpr->right) &&
1382 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1390 /*-----------------------------------------------------------------*/
1391 /* astHasVolatile - returns true if ast contains any volatile */
1392 /*-----------------------------------------------------------------*/
1394 astHasVolatile (ast * tree)
1399 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1402 if (IS_AST_OP (tree))
1403 return astHasVolatile (tree->left) ||
1404 astHasVolatile (tree->right);
1409 /*-----------------------------------------------------------------*/
1410 /* astHasPointer - return true if the ast contains any ptr variable */
1411 /*-----------------------------------------------------------------*/
1413 astHasPointer (ast * tree)
1418 if (IS_AST_LINK (tree))
1421 /* if we hit an array expression then check
1422 only the left side */
1423 if (IS_AST_OP (tree) && tree->opval.op == '[')
1424 return astHasPointer (tree->left);
1426 if (IS_AST_VALUE (tree))
1427 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1429 return astHasPointer (tree->left) ||
1430 astHasPointer (tree->right);
1434 /*-----------------------------------------------------------------*/
1435 /* astHasSymbol - return true if the ast has the given symbol */
1436 /*-----------------------------------------------------------------*/
1438 astHasSymbol (ast * tree, symbol * sym)
1440 if (!tree || IS_AST_LINK (tree))
1443 if (IS_AST_VALUE (tree))
1445 if (IS_AST_SYM_VALUE (tree))
1446 return isSymbolEqual (AST_SYMBOL (tree), sym);
1451 return astHasSymbol (tree->left, sym) ||
1452 astHasSymbol (tree->right, sym);
1455 /*-----------------------------------------------------------------*/
1456 /* astHasDeref - return true if the ast has an indirect access */
1457 /*-----------------------------------------------------------------*/
1459 astHasDeref (ast * tree)
1461 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1464 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1466 return astHasDeref (tree->left) || astHasDeref (tree->right);
1469 /*-----------------------------------------------------------------*/
1470 /* isConformingBody - the loop body has to conform to a set of rules */
1471 /* for the loop to be considered reversible read on for rules */
1472 /*-----------------------------------------------------------------*/
1474 isConformingBody (ast * pbody, symbol * sym, ast * body)
1477 /* we are going to do a pre-order traversal of the
1478 tree && check for the following conditions. (essentially
1479 a set of very shallow tests )
1480 a) the sym passed does not participate in
1481 any arithmetic operation
1482 b) There are no function calls
1483 c) all jumps are within the body
1484 d) address of loop control variable not taken
1485 e) if an assignment has a pointer on the
1486 left hand side make sure right does not have
1487 loop control variable */
1489 /* if we reach the end or a leaf then true */
1490 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1494 /* if anything else is "volatile" */
1495 if (IS_VOLATILE (TETYPE (pbody)))
1498 /* we will walk the body in a pre-order traversal for
1500 switch (pbody->opval.op)
1502 /*------------------------------------------------------------------*/
1504 return isConformingBody (pbody->right, sym, body);
1506 /*------------------------------------------------------------------*/
1511 /*------------------------------------------------------------------*/
1512 case INC_OP: /* incerement operator unary so left only */
1515 /* sure we are not sym is not modified */
1517 IS_AST_SYM_VALUE (pbody->left) &&
1518 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1522 IS_AST_SYM_VALUE (pbody->right) &&
1523 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1528 /*------------------------------------------------------------------*/
1530 case '*': /* can be unary : if right is null then unary operation */
1535 /* if right is NULL then unary operation */
1536 /*------------------------------------------------------------------*/
1537 /*----------------------------*/
1539 /*----------------------------*/
1542 if (IS_AST_SYM_VALUE (pbody->left) &&
1543 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1546 return isConformingBody (pbody->left, sym, body);
1550 if (astHasSymbol (pbody->left, sym) ||
1551 astHasSymbol (pbody->right, sym))
1556 /*------------------------------------------------------------------*/
1564 if (IS_AST_SYM_VALUE (pbody->left) &&
1565 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1568 if (IS_AST_SYM_VALUE (pbody->right) &&
1569 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1572 return isConformingBody (pbody->left, sym, body) &&
1573 isConformingBody (pbody->right, sym, body);
1580 if (IS_AST_SYM_VALUE (pbody->left) &&
1581 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1583 return isConformingBody (pbody->left, sym, body);
1585 /*------------------------------------------------------------------*/
1597 case SIZEOF: /* evaluate wihout code generation */
1599 return isConformingBody (pbody->left, sym, body) &&
1600 isConformingBody (pbody->right, sym, body);
1602 /*------------------------------------------------------------------*/
1605 /* if left has a pointer & right has loop
1606 control variable then we cannot */
1607 if (astHasPointer (pbody->left) &&
1608 astHasSymbol (pbody->right, sym))
1610 if (astHasVolatile (pbody->left))
1613 if (IS_AST_SYM_VALUE (pbody->left) &&
1614 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1617 if (astHasVolatile (pbody->left))
1620 if (astHasDeref(pbody->right)) return FALSE;
1622 return isConformingBody (pbody->left, sym, body) &&
1623 isConformingBody (pbody->right, sym, body);
1634 assert ("Parser should not have generated this\n");
1636 /*------------------------------------------------------------------*/
1637 /*----------------------------*/
1638 /* comma operator */
1639 /*----------------------------*/
1641 return isConformingBody (pbody->left, sym, body) &&
1642 isConformingBody (pbody->right, sym, body);
1644 /*------------------------------------------------------------------*/
1645 /*----------------------------*/
1647 /*----------------------------*/
1651 /*------------------------------------------------------------------*/
1652 /*----------------------------*/
1653 /* return statement */
1654 /*----------------------------*/
1659 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1664 if (astHasSymbol (pbody->left, sym))
1671 return isConformingBody (pbody->left, sym, body) &&
1672 isConformingBody (pbody->right, sym, body);
1678 /*-----------------------------------------------------------------*/
1679 /* isLoopReversible - takes a for loop as input && returns true */
1680 /* if the for loop is reversible. If yes will set the value of */
1681 /* the loop control var & init value & termination value */
1682 /*-----------------------------------------------------------------*/
1684 isLoopReversible (ast * loop, symbol ** loopCntrl,
1685 ast ** init, ast ** end)
1687 /* if option says don't do it then don't */
1688 if (optimize.noLoopReverse)
1690 /* there are several tests to determine this */
1692 /* for loop has to be of the form
1693 for ( <sym> = <const1> ;
1694 [<sym> < <const2>] ;
1695 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1697 if (!isLoopCountable (AST_FOR (loop, initExpr),
1698 AST_FOR (loop, condExpr),
1699 AST_FOR (loop, loopExpr),
1700 loopCntrl, init, end))
1703 /* now do some serious checking on the body of the loop
1706 return isConformingBody (loop->left, *loopCntrl, loop->left);
1710 /*-----------------------------------------------------------------*/
1711 /* replLoopSym - replace the loop sym by loop sym -1 */
1712 /*-----------------------------------------------------------------*/
1714 replLoopSym (ast * body, symbol * sym)
1717 if (!body || IS_AST_LINK (body))
1720 if (IS_AST_SYM_VALUE (body))
1723 if (isSymbolEqual (AST_SYMBOL (body), sym))
1727 body->opval.op = '-';
1728 body->left = newAst_VALUE (symbolVal (sym));
1729 body->right = newAst_VALUE (constVal ("1"));
1737 replLoopSym (body->left, sym);
1738 replLoopSym (body->right, sym);
1742 /*-----------------------------------------------------------------*/
1743 /* reverseLoop - do the actual loop reversal */
1744 /*-----------------------------------------------------------------*/
1746 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1750 /* create the following tree
1755 if (sym) goto for_continue ;
1758 /* put it together piece by piece */
1759 rloop = newNode (NULLOP,
1760 createIf (newAst_VALUE (symbolVal (sym)),
1762 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1765 newAst_VALUE (symbolVal (sym)),
1768 replLoopSym (loop->left, sym);
1770 rloop = newNode (NULLOP,
1772 newAst_VALUE (symbolVal (sym)),
1773 newNode ('-', end, init)),
1774 createLabel (AST_FOR (loop, continueLabel),
1778 newNode (SUB_ASSIGN,
1779 newAst_VALUE (symbolVal (sym)),
1780 newAst_VALUE (constVal ("1"))),
1783 return decorateType (rloop);
1787 //#define DEMAND_INTEGER_PROMOTION
1789 #ifdef DEMAND_INTEGER_PROMOTION
1791 /*-----------------------------------------------------------------*/
1792 /* walk a tree looking for the leaves. Add a typecast to the given */
1793 /* type to each value leaf node. */
1794 /*-----------------------------------------------------------------*/
1796 pushTypeCastToLeaves (sym_link * type, ast * node, ast ** parentPtr)
1798 if (!node || IS_CALLOP(node))
1800 /* WTF? We should never get here. */
1804 if (!node->left && !node->right)
1806 /* We're at a leaf; if it's a value, apply the typecast */
1807 if (node->type == EX_VALUE && IS_INTEGRAL (TTYPE (node)))
1809 *parentPtr = decorateType (newNode (CAST,
1810 newAst_LINK (copyLinkChain (type)),
1818 pushTypeCastToLeaves (type, node->left, &(node->left));
1822 pushTypeCastToLeaves (type, node->right, &(node->right));
1829 /*-----------------------------------------------------------------*/
1830 /* decorateType - compute type for this tree also does type cheking */
1831 /* this is done bottom up, since type have to flow upwards */
1832 /* it also does constant folding, and paramater checking */
1833 /*-----------------------------------------------------------------*/
1835 decorateType (ast * tree)
1843 /* if already has type then do nothing */
1844 if (tree->decorated)
1847 tree->decorated = 1;
1849 /* print the line */
1850 /* if not block & function */
1851 if (tree->type == EX_OP &&
1852 (tree->opval.op != FUNCTION &&
1853 tree->opval.op != BLOCK &&
1854 tree->opval.op != NULLOP))
1856 filename = tree->filename;
1857 lineno = tree->lineno;
1860 /* if any child is an error | this one is an error do nothing */
1861 if (tree->isError ||
1862 (tree->left && tree->left->isError) ||
1863 (tree->right && tree->right->isError))
1866 /*------------------------------------------------------------------*/
1867 /*----------------------------*/
1868 /* leaf has been reached */
1869 /*----------------------------*/
1870 /* if this is of type value */
1871 /* just get the type */
1872 if (tree->type == EX_VALUE)
1875 if (IS_LITERAL (tree->opval.val->etype))
1878 /* if this is a character array then declare it */
1879 if (IS_ARRAY (tree->opval.val->type))
1880 tree->opval.val = stringToSymbol (tree->opval.val);
1882 /* otherwise just copy the type information */
1883 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1887 if (tree->opval.val->sym)
1889 /* if the undefined flag is set then give error message */
1890 if (tree->opval.val->sym->undefined)
1892 werror (E_ID_UNDEF, tree->opval.val->sym->name);
1894 TTYPE (tree) = TETYPE (tree) =
1895 tree->opval.val->type = tree->opval.val->sym->type =
1896 tree->opval.val->etype = tree->opval.val->sym->etype =
1897 copyLinkChain (INTTYPE);
1902 /* if impilicit i.e. struct/union member then no type */
1903 if (tree->opval.val->sym->implicit)
1904 TTYPE (tree) = TETYPE (tree) = NULL;
1909 /* else copy the type */
1910 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1912 /* and mark it as referenced */
1913 tree->opval.val->sym->isref = 1;
1921 /* if type link for the case of cast */
1922 if (tree->type == EX_LINK)
1924 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
1931 dtl = decorateType (tree->left);
1932 dtr = decorateType (tree->right);
1934 /* this is to take care of situations
1935 when the tree gets rewritten */
1936 if (dtl != tree->left)
1938 if (dtr != tree->right)
1942 /* depending on type of operator do */
1944 switch (tree->opval.op)
1946 /*------------------------------------------------------------------*/
1947 /*----------------------------*/
1949 /*----------------------------*/
1952 /* determine which is the array & which the index */
1953 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) && IS_INTEGRAL (LTYPE (tree)))
1956 ast *tempTree = tree->left;
1957 tree->left = tree->right;
1958 tree->right = tempTree;
1961 /* first check if this is a array or a pointer */
1962 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
1964 werror (E_NEED_ARRAY_PTR, "[]");
1965 goto errorTreeReturn;
1968 /* check if the type of the idx */
1969 if (!IS_INTEGRAL (RTYPE (tree)))
1971 werror (E_IDX_NOT_INT);
1972 goto errorTreeReturn;
1975 /* if the left is an rvalue then error */
1978 werror (E_LVALUE_REQUIRED, "array access");
1979 goto errorTreeReturn;
1982 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
1983 if (IS_PTR(LTYPE(tree))) {
1984 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
1988 /*------------------------------------------------------------------*/
1989 /*----------------------------*/
1991 /*----------------------------*/
1993 /* if this is not a structure */
1994 if (!IS_STRUCT (LTYPE (tree)))
1996 werror (E_STRUCT_UNION, ".");
1997 goto errorTreeReturn;
1999 TTYPE (tree) = structElemType (LTYPE (tree),
2000 (tree->right->type == EX_VALUE ?
2001 tree->right->opval.val : NULL));
2002 TETYPE (tree) = getSpec (TTYPE (tree));
2005 /*------------------------------------------------------------------*/
2006 /*----------------------------*/
2007 /* struct/union pointer */
2008 /*----------------------------*/
2010 /* if not pointer to a structure */
2011 if (!IS_PTR (LTYPE (tree)))
2013 werror (E_PTR_REQD);
2014 goto errorTreeReturn;
2017 if (!IS_STRUCT (LTYPE (tree)->next))
2019 werror (E_STRUCT_UNION, "->");
2020 goto errorTreeReturn;
2023 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2024 (tree->right->type == EX_VALUE ?
2025 tree->right->opval.val : NULL));
2026 TETYPE (tree) = getSpec (TTYPE (tree));
2029 /*------------------------------------------------------------------*/
2030 /*----------------------------*/
2031 /* ++/-- operation */
2032 /*----------------------------*/
2033 case INC_OP: /* incerement operator unary so left only */
2036 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2037 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2038 if (!tree->initMode && IS_CONSTANT(TETYPE(tree)))
2039 werror (E_CODE_WRITE, "++/--");
2048 /*------------------------------------------------------------------*/
2049 /*----------------------------*/
2051 /*----------------------------*/
2052 case '&': /* can be unary */
2053 /* if right is NULL then unary operation */
2054 if (tree->right) /* not an unary operation */
2057 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2059 werror (E_BITWISE_OP);
2060 werror (W_CONTINUE, "left & right types are ");
2061 printTypeChain (LTYPE (tree), stderr);
2062 fprintf (stderr, ",");
2063 printTypeChain (RTYPE (tree), stderr);
2064 fprintf (stderr, "\n");
2065 goto errorTreeReturn;
2068 /* if they are both literal */
2069 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2071 tree->type = EX_VALUE;
2072 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2073 valFromType (RETYPE (tree)), '&');
2075 tree->right = tree->left = NULL;
2076 TETYPE (tree) = tree->opval.val->etype;
2077 TTYPE (tree) = tree->opval.val->type;
2081 /* see if this is a GETHBIT operation if yes
2084 ast *otree = optimizeGetHbit (tree);
2087 return decorateType (otree);
2091 // we can't do this because of "(int & 0xff) << 3"
2093 /* if right or left is literal then result of that type */
2094 if (IS_LITERAL (RTYPE (tree)))
2097 TTYPE (tree) = copyLinkChain (RTYPE (tree));
2098 TETYPE (tree) = getSpec (TTYPE (tree));
2099 SPEC_SCLS (TETYPE (tree)) = S_AUTO;
2103 if (IS_LITERAL (LTYPE (tree)))
2105 TTYPE (tree) = copyLinkChain (LTYPE (tree));
2106 TETYPE (tree) = getSpec (TTYPE (tree));
2107 SPEC_SCLS (TETYPE (tree)) = S_AUTO;
2113 computeType (LTYPE (tree), RTYPE (tree));
2114 TETYPE (tree) = getSpec (TTYPE (tree));
2119 computeType (LTYPE (tree), RTYPE (tree));
2120 TETYPE (tree) = getSpec (TTYPE (tree));
2122 LRVAL (tree) = RRVAL (tree) = 1;
2126 /*------------------------------------------------------------------*/
2127 /*----------------------------*/
2129 /*----------------------------*/
2131 p->class = DECLARATOR;
2132 /* if bit field then error */
2133 if (IS_BITVAR (tree->left->etype))
2135 werror (E_ILLEGAL_ADDR, "address of bit variable");
2136 goto errorTreeReturn;
2139 if (SPEC_SCLS (tree->left->etype) == S_REGISTER)
2141 werror (E_ILLEGAL_ADDR, "address of register variable");
2142 goto errorTreeReturn;
2145 if (IS_FUNC (LTYPE (tree)))
2147 werror (E_ILLEGAL_ADDR, "address of function");
2148 goto errorTreeReturn;
2151 if (IS_LITERAL(LTYPE(tree)))
2153 werror (E_ILLEGAL_ADDR, "address of literal");
2154 goto errorTreeReturn;
2159 werror (E_LVALUE_REQUIRED, "address of");
2160 goto errorTreeReturn;
2162 if (SPEC_SCLS (tree->left->etype) == S_CODE)
2164 DCL_TYPE (p) = CPOINTER;
2165 DCL_PTR_CONST (p) = port->mem.code_ro;
2167 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2168 DCL_TYPE (p) = FPOINTER;
2169 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2170 DCL_TYPE (p) = PPOINTER;
2171 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2172 DCL_TYPE (p) = IPOINTER;
2173 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2174 DCL_TYPE (p) = EEPPOINTER;
2176 DCL_TYPE (p) = POINTER;
2178 if (IS_AST_SYM_VALUE (tree->left))
2180 AST_SYMBOL (tree->left)->addrtaken = 1;
2181 AST_SYMBOL (tree->left)->allocreq = 1;
2184 p->next = LTYPE (tree);
2186 TETYPE (tree) = getSpec (TTYPE (tree));
2187 DCL_PTR_CONST (p) = SPEC_CONST (TETYPE (tree));
2188 DCL_PTR_VOLATILE (p) = SPEC_VOLATILE (TETYPE (tree));
2193 /*------------------------------------------------------------------*/
2194 /*----------------------------*/
2196 /*----------------------------*/
2198 /* if the rewrite succeeds then don't go any furthur */
2200 ast *wtree = optimizeRRCRLC (tree);
2202 return decorateType (wtree);
2204 /*------------------------------------------------------------------*/
2205 /*----------------------------*/
2207 /*----------------------------*/
2209 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2211 werror (E_BITWISE_OP);
2212 werror (W_CONTINUE, "left & right types are ");
2213 printTypeChain (LTYPE (tree), stderr);
2214 fprintf (stderr, ",");
2215 printTypeChain (RTYPE (tree), stderr);
2216 fprintf (stderr, "\n");
2217 goto errorTreeReturn;
2220 /* if they are both literal then */
2221 /* rewrite the tree */
2222 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2224 tree->type = EX_VALUE;
2225 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2226 valFromType (RETYPE (tree)),
2228 tree->right = tree->left = NULL;
2229 TETYPE (tree) = tree->opval.val->etype;
2230 TTYPE (tree) = tree->opval.val->type;
2233 LRVAL (tree) = RRVAL (tree) = 1;
2234 TETYPE (tree) = getSpec (TTYPE (tree) =
2235 computeType (LTYPE (tree),
2238 /*------------------------------------------------------------------*/
2239 /*----------------------------*/
2241 /*----------------------------*/
2243 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2245 werror (E_INVALID_OP, "divide");
2246 goto errorTreeReturn;
2248 /* if they are both literal then */
2249 /* rewrite the tree */
2250 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2252 tree->type = EX_VALUE;
2253 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2254 valFromType (RETYPE (tree)));
2255 tree->right = tree->left = NULL;
2256 TETYPE (tree) = getSpec (TTYPE (tree) =
2257 tree->opval.val->type);
2260 LRVAL (tree) = RRVAL (tree) = 1;
2261 TETYPE (tree) = getSpec (TTYPE (tree) =
2262 computeType (LTYPE (tree),
2266 /*------------------------------------------------------------------*/
2267 /*----------------------------*/
2269 /*----------------------------*/
2271 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2273 werror (E_BITWISE_OP);
2274 werror (W_CONTINUE, "left & right types are ");
2275 printTypeChain (LTYPE (tree), stderr);
2276 fprintf (stderr, ",");
2277 printTypeChain (RTYPE (tree), stderr);
2278 fprintf (stderr, "\n");
2279 goto errorTreeReturn;
2281 /* if they are both literal then */
2282 /* rewrite the tree */
2283 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2285 tree->type = EX_VALUE;
2286 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2287 valFromType (RETYPE (tree)));
2288 tree->right = tree->left = NULL;
2289 TETYPE (tree) = getSpec (TTYPE (tree) =
2290 tree->opval.val->type);
2293 LRVAL (tree) = RRVAL (tree) = 1;
2294 TETYPE (tree) = getSpec (TTYPE (tree) =
2295 computeType (LTYPE (tree),
2299 /*------------------------------------------------------------------*/
2300 /*----------------------------*/
2301 /* address dereference */
2302 /*----------------------------*/
2303 case '*': /* can be unary : if right is null then unary operation */
2306 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2308 werror (E_PTR_REQD);
2309 goto errorTreeReturn;
2314 werror (E_LVALUE_REQUIRED, "pointer deref");
2315 goto errorTreeReturn;
2317 TTYPE (tree) = copyLinkChain ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) ?
2318 LTYPE (tree)->next : NULL);
2319 TETYPE (tree) = getSpec (TTYPE (tree));
2320 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
2324 /*------------------------------------------------------------------*/
2325 /*----------------------------*/
2326 /* multiplication */
2327 /*----------------------------*/
2328 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2330 werror (E_INVALID_OP, "multiplication");
2331 goto errorTreeReturn;
2334 /* if they are both literal then */
2335 /* rewrite the tree */
2336 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2338 tree->type = EX_VALUE;
2339 tree->opval.val = valMult (valFromType (LETYPE (tree)),
2340 valFromType (RETYPE (tree)));
2341 tree->right = tree->left = NULL;
2342 TETYPE (tree) = getSpec (TTYPE (tree) =
2343 tree->opval.val->type);
2347 /* if left is a literal exchange left & right */
2348 if (IS_LITERAL (LTYPE (tree)))
2350 ast *tTree = tree->left;
2351 tree->left = tree->right;
2352 tree->right = tTree;
2355 LRVAL (tree) = RRVAL (tree) = 1;
2356 /* promote result to int if left & right are char
2357 this will facilitate hardware multiplies 8bit x 8bit = 16bit */
2358 if (IS_CHAR(LETYPE(tree)) && IS_CHAR(RETYPE(tree))) {
2359 TETYPE (tree) = getSpec (TTYPE (tree) =
2360 computeType (LTYPE (tree),
2362 SPEC_NOUN(TETYPE(tree)) = V_INT;
2364 TETYPE (tree) = getSpec (TTYPE (tree) =
2365 computeType (LTYPE (tree),
2370 /*------------------------------------------------------------------*/
2371 /*----------------------------*/
2372 /* unary '+' operator */
2373 /*----------------------------*/
2378 if (!IS_INTEGRAL (LTYPE (tree)))
2380 werror (E_UNARY_OP, '+');
2381 goto errorTreeReturn;
2384 /* if left is a literal then do it */
2385 if (IS_LITERAL (LTYPE (tree)))
2387 tree->type = EX_VALUE;
2388 tree->opval.val = valFromType (LETYPE (tree));
2390 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2394 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2398 /*------------------------------------------------------------------*/
2399 /*----------------------------*/
2401 /*----------------------------*/
2403 /* this is not a unary operation */
2404 /* if both pointers then problem */
2405 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2406 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
2408 werror (E_PTR_PLUS_PTR);
2409 goto errorTreeReturn;
2412 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2413 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2415 werror (E_PLUS_INVALID, "+");
2416 goto errorTreeReturn;
2419 if (!IS_ARITHMETIC (RTYPE (tree)) &&
2420 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
2422 werror (E_PLUS_INVALID, "+");
2423 goto errorTreeReturn;
2425 /* if they are both literal then */
2426 /* rewrite the tree */
2427 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2429 tree->type = EX_VALUE;
2430 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
2431 valFromType (RETYPE (tree)));
2432 tree->right = tree->left = NULL;
2433 TETYPE (tree) = getSpec (TTYPE (tree) =
2434 tree->opval.val->type);
2438 /* if the right is a pointer or left is a literal
2439 xchange left & right */
2440 if (IS_ARRAY (RTYPE (tree)) ||
2441 IS_PTR (RTYPE (tree)) ||
2442 IS_LITERAL (LTYPE (tree)))
2444 ast *tTree = tree->left;
2445 tree->left = tree->right;
2446 tree->right = tTree;
2449 LRVAL (tree) = RRVAL (tree) = 1;
2450 /* if the left is a pointer */
2451 if (IS_PTR (LTYPE (tree)))
2452 TETYPE (tree) = getSpec (TTYPE (tree) =
2455 TETYPE (tree) = getSpec (TTYPE (tree) =
2456 computeType (LTYPE (tree),
2460 /*------------------------------------------------------------------*/
2461 /*----------------------------*/
2463 /*----------------------------*/
2464 case '-': /* can be unary */
2465 /* if right is null then unary */
2469 if (!IS_ARITHMETIC (LTYPE (tree)))
2471 werror (E_UNARY_OP, tree->opval.op);
2472 goto errorTreeReturn;
2475 /* if left is a literal then do it */
2476 if (IS_LITERAL (LTYPE (tree)))
2478 tree->type = EX_VALUE;
2479 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
2481 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2482 SPEC_USIGN(TETYPE(tree)) = 0;
2486 TTYPE (tree) = LTYPE (tree);
2490 /*------------------------------------------------------------------*/
2491 /*----------------------------*/
2493 /*----------------------------*/
2495 if (!(IS_PTR (LTYPE (tree)) ||
2496 IS_ARRAY (LTYPE (tree)) ||
2497 IS_ARITHMETIC (LTYPE (tree))))
2499 werror (E_PLUS_INVALID, "-");
2500 goto errorTreeReturn;
2503 if (!(IS_PTR (RTYPE (tree)) ||
2504 IS_ARRAY (RTYPE (tree)) ||
2505 IS_ARITHMETIC (RTYPE (tree))))
2507 werror (E_PLUS_INVALID, "-");
2508 goto errorTreeReturn;
2511 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2512 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
2513 IS_INTEGRAL (RTYPE (tree))))
2515 werror (E_PLUS_INVALID, "-");
2516 goto errorTreeReturn;
2519 /* if they are both literal then */
2520 /* rewrite the tree */
2521 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2523 tree->type = EX_VALUE;
2524 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
2525 valFromType (RETYPE (tree)));
2526 tree->right = tree->left = NULL;
2527 TETYPE (tree) = getSpec (TTYPE (tree) =
2528 tree->opval.val->type);
2532 /* if the left & right are equal then zero */
2533 if (isAstEqual (tree->left, tree->right))
2535 tree->type = EX_VALUE;
2536 tree->left = tree->right = NULL;
2537 tree->opval.val = constVal ("0");
2538 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2542 /* if both of them are pointers or arrays then */
2543 /* the result is going to be an integer */
2544 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
2545 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
2546 TETYPE (tree) = TTYPE (tree) = newIntLink ();
2548 /* if only the left is a pointer */
2549 /* then result is a pointer */
2550 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
2551 TETYPE (tree) = getSpec (TTYPE (tree) =
2554 TETYPE (tree) = getSpec (TTYPE (tree) =
2555 computeType (LTYPE (tree),
2557 LRVAL (tree) = RRVAL (tree) = 1;
2560 /*------------------------------------------------------------------*/
2561 /*----------------------------*/
2563 /*----------------------------*/
2565 /* can be only integral type */
2566 if (!IS_INTEGRAL (LTYPE (tree)))
2568 werror (E_UNARY_OP, tree->opval.op);
2569 goto errorTreeReturn;
2572 /* if left is a literal then do it */
2573 if (IS_LITERAL (LTYPE (tree)))
2575 tree->type = EX_VALUE;
2576 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
2578 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2582 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2585 /*------------------------------------------------------------------*/
2586 /*----------------------------*/
2588 /*----------------------------*/
2590 /* can be pointer */
2591 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2592 !IS_PTR (LTYPE (tree)) &&
2593 !IS_ARRAY (LTYPE (tree)))
2595 werror (E_UNARY_OP, tree->opval.op);
2596 goto errorTreeReturn;
2599 /* if left is a literal then do it */
2600 if (IS_LITERAL (LTYPE (tree)))
2602 tree->type = EX_VALUE;
2603 tree->opval.val = valNot (valFromType (LETYPE (tree)));
2605 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2609 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2612 /*------------------------------------------------------------------*/
2613 /*----------------------------*/
2615 /*----------------------------*/
2618 TTYPE (tree) = LTYPE (tree);
2619 TETYPE (tree) = LETYPE (tree);
2623 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2628 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
2630 werror (E_SHIFT_OP_INVALID);
2631 werror (W_CONTINUE, "left & right types are ");
2632 printTypeChain (LTYPE (tree), stderr);
2633 fprintf (stderr, ",");
2634 printTypeChain (RTYPE (tree), stderr);
2635 fprintf (stderr, "\n");
2636 goto errorTreeReturn;
2639 /* if they are both literal then */
2640 /* rewrite the tree */
2641 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2643 tree->type = EX_VALUE;
2644 tree->opval.val = valShift (valFromType (LETYPE (tree)),
2645 valFromType (RETYPE (tree)),
2646 (tree->opval.op == LEFT_OP ? 1 : 0));
2647 tree->right = tree->left = NULL;
2648 TETYPE (tree) = getSpec (TTYPE (tree) =
2649 tree->opval.val->type);
2652 /* if only the right side is a literal & we are
2653 shifting more than size of the left operand then zero */
2654 if (IS_LITERAL (RTYPE (tree)) &&
2655 ((unsigned) floatFromVal (valFromType (RETYPE (tree)))) >=
2656 (getSize (LTYPE (tree)) * 8))
2658 werror (W_SHIFT_CHANGED,
2659 (tree->opval.op == LEFT_OP ? "left" : "right"));
2660 tree->type = EX_VALUE;
2661 tree->left = tree->right = NULL;
2662 tree->opval.val = constVal ("0");
2663 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2666 LRVAL (tree) = RRVAL (tree) = 1;
2667 if (IS_LITERAL (LTYPE (tree)) && !IS_LITERAL (RTYPE (tree)))
2669 COPYTYPE (TTYPE (tree), TETYPE (tree), RTYPE (tree));
2673 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2677 /*------------------------------------------------------------------*/
2678 /*----------------------------*/
2680 /*----------------------------*/
2681 case CAST: /* change the type */
2682 /* cannot cast to an aggregate type */
2683 if (IS_AGGREGATE (LTYPE (tree)))
2685 werror (E_CAST_ILLEGAL);
2686 goto errorTreeReturn;
2689 /* make sure the type is complete and sane */
2690 checkTypeSanity(LETYPE(tree), "(cast)");
2693 /* if the right is a literal replace the tree */
2694 if (IS_LITERAL (RETYPE (tree))) {
2695 if (!IS_PTR (LTYPE (tree))) {
2696 tree->type = EX_VALUE;
2698 valCastLiteral (LTYPE (tree),
2699 floatFromVal (valFromType (RETYPE (tree))));
2702 TTYPE (tree) = tree->opval.val->type;
2703 tree->values.literalFromCast = 1;
2704 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
2705 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
2706 sym_link *rest = LTYPE(tree)->next;
2707 werror(W_LITERAL_GENERIC);
2708 TTYPE(tree) = newLink();
2709 DCL_TYPE(TTYPE(tree)) = FPOINTER;
2710 TTYPE(tree)->next = rest;
2711 tree->left->opval.lnk = TTYPE(tree);
2714 TTYPE (tree) = LTYPE (tree);
2718 TTYPE (tree) = LTYPE (tree);
2722 /* if the right is a literal replace the tree */
2723 if (IS_LITERAL (RETYPE (tree)) && !IS_PTR (LTYPE (tree))) {
2724 tree->type = EX_VALUE;
2726 valCastLiteral (LTYPE (tree),
2727 floatFromVal (valFromType (RETYPE (tree))));
2730 TTYPE (tree) = tree->opval.val->type;
2731 tree->values.literalFromCast = 1;
2733 TTYPE (tree) = LTYPE (tree);
2738 TETYPE (tree) = getSpec (TTYPE (tree));
2742 /*------------------------------------------------------------------*/
2743 /*----------------------------*/
2744 /* logical &&, || */
2745 /*----------------------------*/
2748 /* each must me arithmetic type or be a pointer */
2749 if (!IS_PTR (LTYPE (tree)) &&
2750 !IS_ARRAY (LTYPE (tree)) &&
2751 !IS_INTEGRAL (LTYPE (tree)))
2753 werror (E_COMPARE_OP);
2754 goto errorTreeReturn;
2757 if (!IS_PTR (RTYPE (tree)) &&
2758 !IS_ARRAY (RTYPE (tree)) &&
2759 !IS_INTEGRAL (RTYPE (tree)))
2761 werror (E_COMPARE_OP);
2762 goto errorTreeReturn;
2764 /* if they are both literal then */
2765 /* rewrite the tree */
2766 if (IS_LITERAL (RTYPE (tree)) &&
2767 IS_LITERAL (LTYPE (tree)))
2769 tree->type = EX_VALUE;
2770 tree->opval.val = valLogicAndOr (valFromType (LETYPE (tree)),
2771 valFromType (RETYPE (tree)),
2773 tree->right = tree->left = NULL;
2774 TETYPE (tree) = getSpec (TTYPE (tree) =
2775 tree->opval.val->type);
2778 LRVAL (tree) = RRVAL (tree) = 1;
2779 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2782 /*------------------------------------------------------------------*/
2783 /*----------------------------*/
2784 /* comparison operators */
2785 /*----------------------------*/
2793 ast *lt = optimizeCompare (tree);
2799 /* if they are pointers they must be castable */
2800 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
2802 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2804 werror (E_COMPARE_OP);
2805 fprintf (stderr, "comparing type ");
2806 printTypeChain (LTYPE (tree), stderr);
2807 fprintf (stderr, "to type ");
2808 printTypeChain (RTYPE (tree), stderr);
2809 fprintf (stderr, "\n");
2810 goto errorTreeReturn;
2813 /* else they should be promotable to one another */
2816 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
2817 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
2819 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2821 werror (E_COMPARE_OP);
2822 fprintf (stderr, "comparing type ");
2823 printTypeChain (LTYPE (tree), stderr);
2824 fprintf (stderr, "to type ");
2825 printTypeChain (RTYPE (tree), stderr);
2826 fprintf (stderr, "\n");
2827 goto errorTreeReturn;
2831 /* if they are both literal then */
2832 /* rewrite the tree */
2833 if (IS_LITERAL (RTYPE (tree)) &&
2834 IS_LITERAL (LTYPE (tree)))
2836 tree->type = EX_VALUE;
2837 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
2838 valFromType (RETYPE (tree)),
2840 tree->right = tree->left = NULL;
2841 TETYPE (tree) = getSpec (TTYPE (tree) =
2842 tree->opval.val->type);
2845 LRVAL (tree) = RRVAL (tree) = 1;
2846 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2849 /*------------------------------------------------------------------*/
2850 /*----------------------------*/
2852 /*----------------------------*/
2853 case SIZEOF: /* evaluate wihout code generation */
2854 /* change the type to a integer */
2855 tree->type = EX_VALUE;
2856 sprintf (buffer, "%d", (getSize (tree->right->ftype)));
2857 tree->opval.val = constVal (buffer);
2858 tree->right = tree->left = NULL;
2859 TETYPE (tree) = getSpec (TTYPE (tree) =
2860 tree->opval.val->type);
2863 /*------------------------------------------------------------------*/
2864 /*----------------------------*/
2865 /* conditional operator '?' */
2866 /*----------------------------*/
2868 /* the type is value of the colon operator (on the right) */
2869 assert(IS_COLON_OP(tree->right));
2870 /* if already known then replace the tree : optimizer will do it
2871 but faster to do it here */
2872 if (IS_LITERAL (LTYPE(tree))) {
2873 if ( ((int) floatFromVal (valFromType (LETYPE (tree)))) != 0) {
2874 return tree->right->left ;
2876 return tree->right->right ;
2879 TTYPE (tree) = RTYPE(tree); // #HACK LTYPE(tree).
2880 TETYPE (tree) = getSpec (TTYPE (tree));
2885 /* if they don't match we have a problem */
2886 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2888 werror (E_TYPE_MISMATCH, "conditional operator", " ");
2889 goto errorTreeReturn;
2892 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree));
2893 TETYPE (tree) = getSpec (TTYPE (tree));
2897 /*------------------------------------------------------------------*/
2898 /*----------------------------*/
2899 /* assignment operators */
2900 /*----------------------------*/
2903 /* for these it must be both must be integral */
2904 if (!IS_ARITHMETIC (LTYPE (tree)) ||
2905 !IS_ARITHMETIC (RTYPE (tree)))
2907 werror (E_OPS_INTEGRAL);
2908 goto errorTreeReturn;
2911 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
2913 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
2914 werror (E_CODE_WRITE, " ");
2918 werror (E_LVALUE_REQUIRED, "*= or /=");
2919 goto errorTreeReturn;
2930 /* for these it must be both must be integral */
2931 if (!IS_INTEGRAL (LTYPE (tree)) ||
2932 !IS_INTEGRAL (RTYPE (tree)))
2934 werror (E_OPS_INTEGRAL);
2935 goto errorTreeReturn;
2938 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
2940 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
2941 werror (E_CODE_WRITE, " ");
2945 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
2946 goto errorTreeReturn;
2952 /*------------------------------------------------------------------*/
2953 /*----------------------------*/
2955 /*----------------------------*/
2957 if (!(IS_PTR (LTYPE (tree)) ||
2958 IS_ARITHMETIC (LTYPE (tree))))
2960 werror (E_PLUS_INVALID, "-=");
2961 goto errorTreeReturn;
2964 if (!(IS_PTR (RTYPE (tree)) ||
2965 IS_ARITHMETIC (RTYPE (tree))))
2967 werror (E_PLUS_INVALID, "-=");
2968 goto errorTreeReturn;
2971 TETYPE (tree) = getSpec (TTYPE (tree) =
2972 computeType (LTYPE (tree),
2975 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
2976 werror (E_CODE_WRITE, " ");
2980 werror (E_LVALUE_REQUIRED, "-=");
2981 goto errorTreeReturn;
2987 /*------------------------------------------------------------------*/
2988 /*----------------------------*/
2990 /*----------------------------*/
2992 /* this is not a unary operation */
2993 /* if both pointers then problem */
2994 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
2996 werror (E_PTR_PLUS_PTR);
2997 goto errorTreeReturn;
3000 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
3002 werror (E_PLUS_INVALID, "+=");
3003 goto errorTreeReturn;
3006 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
3008 werror (E_PLUS_INVALID, "+=");
3009 goto errorTreeReturn;
3012 TETYPE (tree) = getSpec (TTYPE (tree) =
3013 computeType (LTYPE (tree),
3016 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3017 werror (E_CODE_WRITE, " ");
3021 werror (E_LVALUE_REQUIRED, "+=");
3022 goto errorTreeReturn;
3025 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right));
3026 tree->opval.op = '=';
3030 /*------------------------------------------------------------------*/
3031 /*----------------------------*/
3032 /* straight assignemnt */
3033 /*----------------------------*/
3035 /* cannot be an aggregate */
3036 if (IS_AGGREGATE (LTYPE (tree)))
3038 werror (E_AGGR_ASSIGN);
3039 goto errorTreeReturn;
3042 /* they should either match or be castable */
3043 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3045 werror (E_TYPE_MISMATCH, "assignment", " ");
3046 fprintf (stderr, "type --> '");
3047 printTypeChain (RTYPE (tree), stderr);
3048 fprintf (stderr, "' ");
3049 fprintf (stderr, "assigned to type --> '");
3050 printTypeChain (LTYPE (tree), stderr);
3051 fprintf (stderr, "'\n");
3052 goto errorTreeReturn;
3055 /* if the left side of the tree is of type void
3056 then report error */
3057 if (IS_VOID (LTYPE (tree)))
3059 werror (E_CAST_ZERO);
3060 printFromToType(RTYPE(tree), LTYPE(tree));
3063 TETYPE (tree) = getSpec (TTYPE (tree) =
3067 if (!tree->initMode ) {
3068 if ((IS_SPEC(LETYPE(tree)) && IS_CONSTANT (LETYPE (tree))))
3069 werror (E_CODE_WRITE, " ");
3073 werror (E_LVALUE_REQUIRED, "=");
3074 goto errorTreeReturn;
3079 /*------------------------------------------------------------------*/
3080 /*----------------------------*/
3081 /* comma operator */
3082 /*----------------------------*/
3084 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
3087 /*------------------------------------------------------------------*/
3088 /*----------------------------*/
3090 /*----------------------------*/
3094 if (processParms (tree->left,
3095 FUNC_ARGS(tree->left->ftype),
3096 tree->right, &parmNumber, TRUE)) {
3097 //fprintf (stderr, "jwk: error in processParms()\n");
3098 goto errorTreeReturn;
3101 if (options.stackAuto || IFFUNC_ISREENT (LTYPE (tree)))
3103 //FUNC_ARGS(tree->left->ftype) =
3104 //reverseVal (FUNC_ARGS(tree->left->ftype));
3105 reverseParms (tree->right);
3108 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)->next);
3111 /*------------------------------------------------------------------*/
3112 /*----------------------------*/
3113 /* return statement */
3114 /*----------------------------*/
3119 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
3121 werror (W_RETURN_MISMATCH);
3122 printFromToType (RTYPE(tree), currFunc->type->next);
3123 goto errorTreeReturn;
3126 if (IS_VOID (currFunc->type->next)
3128 !IS_VOID (RTYPE (tree)))
3130 werror (E_FUNC_VOID);
3131 goto errorTreeReturn;
3134 /* if there is going to be a casing required then add it */
3135 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
3138 decorateType (newNode (CAST,
3139 newAst_LINK (copyLinkChain (currFunc->type->next)),
3148 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
3150 werror (E_VOID_FUNC, currFunc->name);
3151 goto errorTreeReturn;
3154 TTYPE (tree) = TETYPE (tree) = NULL;
3157 /*------------------------------------------------------------------*/
3158 /*----------------------------*/
3159 /* switch statement */
3160 /*----------------------------*/
3162 /* the switch value must be an integer */
3163 if (!IS_INTEGRAL (LTYPE (tree)))
3165 werror (E_SWITCH_NON_INTEGER);
3166 goto errorTreeReturn;
3169 TTYPE (tree) = TETYPE (tree) = NULL;
3172 /*------------------------------------------------------------------*/
3173 /*----------------------------*/
3175 /*----------------------------*/
3177 tree->left = backPatchLabels (tree->left,
3180 TTYPE (tree) = TETYPE (tree) = NULL;
3183 /*------------------------------------------------------------------*/
3184 /*----------------------------*/
3186 /*----------------------------*/
3189 decorateType (resolveSymbols (AST_FOR (tree, initExpr)));
3190 decorateType (resolveSymbols (AST_FOR (tree, condExpr)));
3191 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)));
3193 /* if the for loop is reversible then
3194 reverse it otherwise do what we normally
3200 if (isLoopReversible (tree, &sym, &init, &end))
3201 return reverseLoop (tree, sym, init, end);
3203 return decorateType (createFor (AST_FOR (tree, trueLabel),
3204 AST_FOR (tree, continueLabel),
3205 AST_FOR (tree, falseLabel),
3206 AST_FOR (tree, condLabel),
3207 AST_FOR (tree, initExpr),
3208 AST_FOR (tree, condExpr),
3209 AST_FOR (tree, loopExpr),
3213 TTYPE (tree) = TETYPE (tree) = NULL;
3217 /* some error found this tree will be killed */
3219 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3220 tree->opval.op = NULLOP;
3226 /*-----------------------------------------------------------------*/
3227 /* sizeofOp - processes size of operation */
3228 /*-----------------------------------------------------------------*/
3230 sizeofOp (sym_link * type)
3234 /* make sure the type is complete and sane */
3235 checkTypeSanity(type, "(sizeof)");
3237 /* get the size and convert it to character */
3238 sprintf (buff, "%d", getSize (type));
3240 /* now convert into value */
3241 return constVal (buff);
3245 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
3246 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
3247 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
3248 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
3249 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
3250 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
3251 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
3253 /*-----------------------------------------------------------------*/
3254 /* backPatchLabels - change and or not operators to flow control */
3255 /*-----------------------------------------------------------------*/
3257 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
3263 if (!(IS_ANDORNOT (tree)))
3266 /* if this an and */
3269 static int localLbl = 0;
3272 sprintf (buffer, "_and_%d", localLbl++);
3273 localLabel = newSymbol (buffer, NestLevel);
3275 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
3277 /* if left is already a IFX then just change the if true label in that */
3278 if (!IS_IFX (tree->left))
3279 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
3281 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3282 /* right is a IFX then just join */
3283 if (IS_IFX (tree->right))
3284 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3286 tree->right = createLabel (localLabel, tree->right);
3287 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3289 return newNode (NULLOP, tree->left, tree->right);
3292 /* if this is an or operation */
3295 static int localLbl = 0;
3298 sprintf (buffer, "_or_%d", localLbl++);
3299 localLabel = newSymbol (buffer, NestLevel);
3301 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
3303 /* if left is already a IFX then just change the if true label in that */
3304 if (!IS_IFX (tree->left))
3305 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
3307 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3308 /* right is a IFX then just join */
3309 if (IS_IFX (tree->right))
3310 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3312 tree->right = createLabel (localLabel, tree->right);
3313 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3315 return newNode (NULLOP, tree->left, tree->right);
3321 int wasnot = IS_NOT (tree->left);
3322 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
3324 /* if the left is already a IFX */
3325 if (!IS_IFX (tree->left))
3326 tree->left = newNode (IFX, tree->left, NULL);
3330 tree->left->trueLabel = trueLabel;
3331 tree->left->falseLabel = falseLabel;
3335 tree->left->trueLabel = falseLabel;
3336 tree->left->falseLabel = trueLabel;
3343 tree->trueLabel = trueLabel;
3344 tree->falseLabel = falseLabel;
3351 /*-----------------------------------------------------------------*/
3352 /* createBlock - create expression tree for block */
3353 /*-----------------------------------------------------------------*/
3355 createBlock (symbol * decl, ast * body)
3359 /* if the block has nothing */
3363 ex = newNode (BLOCK, NULL, body);
3364 ex->values.sym = decl;
3366 ex->right = ex->right;
3372 /*-----------------------------------------------------------------*/
3373 /* createLabel - creates the expression tree for labels */
3374 /*-----------------------------------------------------------------*/
3376 createLabel (symbol * label, ast * stmnt)
3379 char name[SDCC_NAME_MAX + 1];
3382 /* must create fresh symbol if the symbol name */
3383 /* exists in the symbol table, since there can */
3384 /* be a variable with the same name as the labl */
3385 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
3386 (csym->level == label->level))
3387 label = newSymbol (label->name, label->level);
3389 /* change the name before putting it in add _ */
3390 sprintf (name, "%s", label->name);
3392 /* put the label in the LabelSymbol table */
3393 /* but first check if a label of the same */
3395 if ((csym = findSym (LabelTab, NULL, name)))
3396 werror (E_DUPLICATE_LABEL, label->name);
3398 addSym (LabelTab, label, name, label->level, 0, 0);
3401 label->key = labelKey++;
3402 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
3408 /*-----------------------------------------------------------------*/
3409 /* createCase - generates the parsetree for a case statement */
3410 /*-----------------------------------------------------------------*/
3412 createCase (ast * swStat, ast * caseVal, ast * stmnt)
3414 char caseLbl[SDCC_NAME_MAX + 1];
3418 /* if the switch statement does not exist */
3419 /* then case is out of context */
3422 werror (E_CASE_CONTEXT);
3426 caseVal = decorateType (resolveSymbols (caseVal));
3427 /* if not a constant then error */
3428 if (!IS_LITERAL (caseVal->ftype))
3430 werror (E_CASE_CONSTANT);
3434 /* if not a integer than error */
3435 if (!IS_INTEGRAL (caseVal->ftype))
3437 werror (E_CASE_NON_INTEGER);
3441 /* find the end of the switch values chain */
3442 if (!(val = swStat->values.switchVals.swVals))
3443 swStat->values.switchVals.swVals = caseVal->opval.val;
3446 /* also order the cases according to value */
3448 int cVal = (int) floatFromVal (caseVal->opval.val);
3449 while (val && (int) floatFromVal (val) < cVal)
3455 /* if we reached the end then */
3458 pval->next = caseVal->opval.val;
3462 /* we found a value greater than */
3463 /* the current value we must add this */
3464 /* before the value */
3465 caseVal->opval.val->next = val;
3467 /* if this was the first in chain */
3468 if (swStat->values.switchVals.swVals == val)
3469 swStat->values.switchVals.swVals =
3472 pval->next = caseVal->opval.val;
3477 /* create the case label */
3478 sprintf (caseLbl, "_case_%d_%d",
3479 swStat->values.switchVals.swNum,
3480 (int) floatFromVal (caseVal->opval.val));
3482 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
3487 /*-----------------------------------------------------------------*/
3488 /* createDefault - creates the parse tree for the default statement */
3489 /*-----------------------------------------------------------------*/
3491 createDefault (ast * swStat, ast * stmnt)
3493 char defLbl[SDCC_NAME_MAX + 1];
3495 /* if the switch statement does not exist */
3496 /* then case is out of context */
3499 werror (E_CASE_CONTEXT);
3503 /* turn on the default flag */
3504 swStat->values.switchVals.swDefault = 1;
3506 /* create the label */
3507 sprintf (defLbl, "_default_%d", swStat->values.switchVals.swNum);
3508 return createLabel (newSymbol (defLbl, 0), stmnt);
3511 /*-----------------------------------------------------------------*/
3512 /* createIf - creates the parsetree for the if statement */
3513 /*-----------------------------------------------------------------*/
3515 createIf (ast * condAst, ast * ifBody, ast * elseBody)
3517 static int Lblnum = 0;
3519 symbol *ifTrue, *ifFalse, *ifEnd;
3521 /* if neither exists */
3522 if (!elseBody && !ifBody) {
3523 // if there are no side effects (i++, j() etc)
3524 if (!hasSEFcalls(condAst)) {
3529 /* create the labels */
3530 sprintf (buffer, "_iffalse_%d", Lblnum);
3531 ifFalse = newSymbol (buffer, NestLevel);
3532 /* if no else body then end == false */
3537 sprintf (buffer, "_ifend_%d", Lblnum);
3538 ifEnd = newSymbol (buffer, NestLevel);
3541 sprintf (buffer, "_iftrue_%d", Lblnum);
3542 ifTrue = newSymbol (buffer, NestLevel);
3546 /* attach the ifTrue label to the top of it body */
3547 ifBody = createLabel (ifTrue, ifBody);
3548 /* attach a goto end to the ifBody if else is present */
3551 ifBody = newNode (NULLOP, ifBody,
3553 newAst_VALUE (symbolVal (ifEnd)),
3555 /* put the elseLabel on the else body */
3556 elseBody = createLabel (ifFalse, elseBody);
3557 /* out the end at the end of the body */
3558 elseBody = newNode (NULLOP,
3560 createLabel (ifEnd, NULL));
3564 ifBody = newNode (NULLOP, ifBody,
3565 createLabel (ifFalse, NULL));
3567 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
3568 if (IS_IFX (condAst))
3571 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
3573 return newNode (NULLOP, ifTree,
3574 newNode (NULLOP, ifBody, elseBody));
3578 /*-----------------------------------------------------------------*/
3579 /* createDo - creates parse tree for do */
3582 /* _docontinue_n: */
3583 /* condition_expression +-> trueLabel -> _dobody_n */
3585 /* +-> falseLabel-> _dobreak_n */
3587 /*-----------------------------------------------------------------*/
3589 createDo (symbol * trueLabel, symbol * continueLabel,
3590 symbol * falseLabel, ast * condAst, ast * doBody)
3595 /* if the body does not exist then it is simple */
3598 condAst = backPatchLabels (condAst, continueLabel, NULL);
3599 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
3600 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
3601 doTree->trueLabel = continueLabel;
3602 doTree->falseLabel = NULL;
3606 /* otherwise we have a body */
3607 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
3609 /* attach the body label to the top */
3610 doBody = createLabel (trueLabel, doBody);
3611 /* attach the continue label to end of body */
3612 doBody = newNode (NULLOP, doBody,
3613 createLabel (continueLabel, NULL));
3615 /* now put the break label at the end */
3616 if (IS_IFX (condAst))
3619 doTree = newIfxNode (condAst, trueLabel, falseLabel);
3621 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
3623 /* putting it together */
3624 return newNode (NULLOP, doBody, doTree);
3627 /*-----------------------------------------------------------------*/
3628 /* createFor - creates parse tree for 'for' statement */
3631 /* condExpr +-> trueLabel -> _forbody_n */
3633 /* +-> falseLabel-> _forbreak_n */
3636 /* _forcontinue_n: */
3638 /* goto _forcond_n ; */
3640 /*-----------------------------------------------------------------*/
3642 createFor (symbol * trueLabel, symbol * continueLabel,
3643 symbol * falseLabel, symbol * condLabel,
3644 ast * initExpr, ast * condExpr, ast * loopExpr,
3649 /* if loopexpression not present then we can generate it */
3650 /* the same way as a while */
3652 return newNode (NULLOP, initExpr,
3653 createWhile (trueLabel, continueLabel,
3654 falseLabel, condExpr, forBody));
3655 /* vanilla for statement */
3656 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3658 if (condExpr && !IS_IFX (condExpr))
3659 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
3662 /* attach condition label to condition */
3663 condExpr = createLabel (condLabel, condExpr);
3665 /* attach body label to body */
3666 forBody = createLabel (trueLabel, forBody);
3668 /* attach continue to forLoop expression & attach */
3669 /* goto the forcond @ and of loopExpression */
3670 loopExpr = createLabel (continueLabel,
3674 newAst_VALUE (symbolVal (condLabel)),
3676 /* now start putting them together */
3677 forTree = newNode (NULLOP, initExpr, condExpr);
3678 forTree = newNode (NULLOP, forTree, forBody);
3679 forTree = newNode (NULLOP, forTree, loopExpr);
3680 /* finally add the break label */
3681 forTree = newNode (NULLOP, forTree,
3682 createLabel (falseLabel, NULL));
3686 /*-----------------------------------------------------------------*/
3687 /* createWhile - creates parse tree for while statement */
3688 /* the while statement will be created as follows */
3690 /* _while_continue_n: */
3691 /* condition_expression +-> trueLabel -> _while_boby_n */
3693 /* +-> falseLabel -> _while_break_n */
3694 /* _while_body_n: */
3696 /* goto _while_continue_n */
3697 /* _while_break_n: */
3698 /*-----------------------------------------------------------------*/
3700 createWhile (symbol * trueLabel, symbol * continueLabel,
3701 symbol * falseLabel, ast * condExpr, ast * whileBody)
3705 /* put the continue label */
3706 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3707 condExpr = createLabel (continueLabel, condExpr);
3708 condExpr->lineno = 0;
3710 /* put the body label in front of the body */
3711 whileBody = createLabel (trueLabel, whileBody);
3712 whileBody->lineno = 0;
3713 /* put a jump to continue at the end of the body */
3714 /* and put break label at the end of the body */
3715 whileBody = newNode (NULLOP,
3718 newAst_VALUE (symbolVal (continueLabel)),
3719 createLabel (falseLabel, NULL)));
3721 /* put it all together */
3722 if (IS_IFX (condExpr))
3723 whileTree = condExpr;
3726 whileTree = newNode (IFX, condExpr, NULL);
3727 /* put the true & false labels in place */
3728 whileTree->trueLabel = trueLabel;
3729 whileTree->falseLabel = falseLabel;
3732 return newNode (NULLOP, whileTree, whileBody);
3735 /*-----------------------------------------------------------------*/
3736 /* optimizeGetHbit - get highest order bit of the expression */
3737 /*-----------------------------------------------------------------*/
3739 optimizeGetHbit (ast * tree)
3742 /* if this is not a bit and */
3743 if (!IS_BITAND (tree))
3746 /* will look for tree of the form
3747 ( expr >> ((sizeof expr) -1) ) & 1 */
3748 if (!IS_AST_LIT_VALUE (tree->right))
3751 if (AST_LIT_VALUE (tree->right) != 1)
3754 if (!IS_RIGHT_OP (tree->left))
3757 if (!IS_AST_LIT_VALUE (tree->left->right))
3760 if ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
3761 (j = (getSize (TTYPE (tree->left->left)) * 8 - 1)))
3764 return decorateType (newNode (GETHBIT, tree->left->left, NULL));
3768 /*-----------------------------------------------------------------*/
3769 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
3770 /*-----------------------------------------------------------------*/
3772 optimizeRRCRLC (ast * root)
3774 /* will look for trees of the form
3775 (?expr << 1) | (?expr >> 7) or
3776 (?expr >> 7) | (?expr << 1) will make that
3777 into a RLC : operation ..
3779 (?expr >> 1) | (?expr << 7) or
3780 (?expr << 7) | (?expr >> 1) will make that
3781 into a RRC operation
3782 note : by 7 I mean (number of bits required to hold the
3784 /* if the root operations is not a | operation the not */
3785 if (!IS_BITOR (root))
3788 /* I have to think of a better way to match patterns this sucks */
3789 /* that aside let start looking for the first case : I use a the
3790 negative check a lot to improve the efficiency */
3791 /* (?expr << 1) | (?expr >> 7) */
3792 if (IS_LEFT_OP (root->left) &&
3793 IS_RIGHT_OP (root->right))
3796 if (!SPEC_USIGN (TETYPE (root->left->left)))
3799 if (!IS_AST_LIT_VALUE (root->left->right) ||
3800 !IS_AST_LIT_VALUE (root->right->right))
3803 /* make sure it is the same expression */
3804 if (!isAstEqual (root->left->left,
3808 if (AST_LIT_VALUE (root->left->right) != 1)
3811 if (AST_LIT_VALUE (root->right->right) !=
3812 (getSize (TTYPE (root->left->left)) * 8 - 1))
3815 /* whew got the first case : create the AST */
3816 return newNode (RLC, root->left->left, NULL);
3820 /* check for second case */
3821 /* (?expr >> 7) | (?expr << 1) */
3822 if (IS_LEFT_OP (root->right) &&
3823 IS_RIGHT_OP (root->left))
3826 if (!SPEC_USIGN (TETYPE (root->left->left)))
3829 if (!IS_AST_LIT_VALUE (root->left->right) ||
3830 !IS_AST_LIT_VALUE (root->right->right))
3833 /* make sure it is the same symbol */
3834 if (!isAstEqual (root->left->left,
3838 if (AST_LIT_VALUE (root->right->right) != 1)
3841 if (AST_LIT_VALUE (root->left->right) !=
3842 (getSize (TTYPE (root->left->left)) * 8 - 1))
3845 /* whew got the first case : create the AST */
3846 return newNode (RLC, root->left->left, NULL);
3851 /* third case for RRC */
3852 /* (?symbol >> 1) | (?symbol << 7) */
3853 if (IS_LEFT_OP (root->right) &&
3854 IS_RIGHT_OP (root->left))
3857 if (!SPEC_USIGN (TETYPE (root->left->left)))
3860 if (!IS_AST_LIT_VALUE (root->left->right) ||
3861 !IS_AST_LIT_VALUE (root->right->right))
3864 /* make sure it is the same symbol */
3865 if (!isAstEqual (root->left->left,
3869 if (AST_LIT_VALUE (root->left->right) != 1)
3872 if (AST_LIT_VALUE (root->right->right) !=
3873 (getSize (TTYPE (root->left->left)) * 8 - 1))
3876 /* whew got the first case : create the AST */
3877 return newNode (RRC, root->left->left, NULL);
3881 /* fourth and last case for now */
3882 /* (?symbol << 7) | (?symbol >> 1) */
3883 if (IS_RIGHT_OP (root->right) &&
3884 IS_LEFT_OP (root->left))
3887 if (!SPEC_USIGN (TETYPE (root->left->left)))
3890 if (!IS_AST_LIT_VALUE (root->left->right) ||
3891 !IS_AST_LIT_VALUE (root->right->right))
3894 /* make sure it is the same symbol */
3895 if (!isAstEqual (root->left->left,
3899 if (AST_LIT_VALUE (root->right->right) != 1)
3902 if (AST_LIT_VALUE (root->left->right) !=
3903 (getSize (TTYPE (root->left->left)) * 8 - 1))
3906 /* whew got the first case : create the AST */
3907 return newNode (RRC, root->left->left, NULL);
3911 /* not found return root */
3915 /*-----------------------------------------------------------------*/
3916 /* optimizeCompare - otimizes compares for bit variables */
3917 /*-----------------------------------------------------------------*/
3919 optimizeCompare (ast * root)
3921 ast *optExpr = NULL;
3924 unsigned int litValue;
3926 /* if nothing then return nothing */
3930 /* if not a compare op then do leaves */
3931 if (!IS_COMPARE_OP (root))
3933 root->left = optimizeCompare (root->left);
3934 root->right = optimizeCompare (root->right);
3938 /* if left & right are the same then depending
3939 of the operation do */
3940 if (isAstEqual (root->left, root->right))
3942 switch (root->opval.op)
3947 optExpr = newAst_VALUE (constVal ("0"));
3952 optExpr = newAst_VALUE (constVal ("1"));
3956 return decorateType (optExpr);
3959 vleft = (root->left->type == EX_VALUE ?
3960 root->left->opval.val : NULL);
3962 vright = (root->right->type == EX_VALUE ?
3963 root->right->opval.val : NULL);
3965 /* if left is a BITVAR in BITSPACE */
3966 /* and right is a LITERAL then opt- */
3967 /* imize else do nothing */
3968 if (vleft && vright &&
3969 IS_BITVAR (vleft->etype) &&
3970 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
3971 IS_LITERAL (vright->etype))
3974 /* if right side > 1 then comparison may never succeed */
3975 if ((litValue = (int) floatFromVal (vright)) > 1)
3977 werror (W_BAD_COMPARE);
3983 switch (root->opval.op)
3985 case '>': /* bit value greater than 1 cannot be */
3986 werror (W_BAD_COMPARE);
3990 case '<': /* bit value < 1 means 0 */
3992 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
3995 case LE_OP: /* bit value <= 1 means no check */
3996 optExpr = newAst_VALUE (vright);
3999 case GE_OP: /* bit value >= 1 means only check for = */
4001 optExpr = newAst_VALUE (vleft);
4006 { /* literal is zero */
4007 switch (root->opval.op)
4009 case '<': /* bit value < 0 cannot be */
4010 werror (W_BAD_COMPARE);
4014 case '>': /* bit value > 0 means 1 */
4016 optExpr = newAst_VALUE (vleft);
4019 case LE_OP: /* bit value <= 0 means no check */
4020 case GE_OP: /* bit value >= 0 means no check */
4021 werror (W_BAD_COMPARE);
4025 case EQ_OP: /* bit == 0 means ! of bit */
4026 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
4030 return decorateType (resolveSymbols (optExpr));
4031 } /* end-of-if of BITVAR */
4036 /*-----------------------------------------------------------------*/
4037 /* addSymToBlock : adds the symbol to the first block we find */
4038 /*-----------------------------------------------------------------*/
4040 addSymToBlock (symbol * sym, ast * tree)
4042 /* reached end of tree or a leaf */
4043 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
4047 if (IS_AST_OP (tree) &&
4048 tree->opval.op == BLOCK)
4051 symbol *lsym = copySymbol (sym);
4053 lsym->next = AST_VALUES (tree, sym);
4054 AST_VALUES (tree, sym) = lsym;
4058 addSymToBlock (sym, tree->left);
4059 addSymToBlock (sym, tree->right);
4062 /*-----------------------------------------------------------------*/
4063 /* processRegParms - do processing for register parameters */
4064 /*-----------------------------------------------------------------*/
4066 processRegParms (value * args, ast * body)
4070 if (IS_REGPARM (args->etype))
4071 addSymToBlock (args->sym, body);
4076 /*-----------------------------------------------------------------*/
4077 /* resetParmKey - resets the operandkeys for the symbols */
4078 /*-----------------------------------------------------------------*/
4079 DEFSETFUNC (resetParmKey)
4090 /*-----------------------------------------------------------------*/
4091 /* createFunction - This is the key node that calls the iCode for */
4092 /* generating the code for a function. Note code */
4093 /* is generated function by function, later when */
4094 /* add inter-procedural analysis this will change */
4095 /*-----------------------------------------------------------------*/
4097 createFunction (symbol * name, ast * body)
4103 iCode *piCode = NULL;
4105 /* if check function return 0 then some problem */
4106 if (checkFunction (name, NULL) == 0)
4109 /* create a dummy block if none exists */
4111 body = newNode (BLOCK, NULL, NULL);
4115 /* check if the function name already in the symbol table */
4116 if ((csym = findSym (SymbolTab, NULL, name->name)))
4119 /* special case for compiler defined functions
4120 we need to add the name to the publics list : this
4121 actually means we are now compiling the compiler
4125 addSet (&publics, name);
4131 allocVariables (name);
4133 name->lastLine = yylineno;
4135 processFuncArgs (currFunc, 0);
4137 /* set the stack pointer */
4138 /* PENDING: check this for the mcs51 */
4139 stackPtr = -port->stack.direction * port->stack.call_overhead;
4140 if (IFFUNC_ISISR (name->type))
4141 stackPtr -= port->stack.direction * port->stack.isr_overhead;
4142 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
4143 stackPtr -= port->stack.direction * port->stack.reent_overhead;
4145 xstackPtr = -port->stack.direction * port->stack.call_overhead;
4147 fetype = getSpec (name->type); /* get the specifier for the function */
4148 /* if this is a reentrant function then */
4149 if (IFFUNC_ISREENT (name->type))
4152 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
4154 /* do processing for parameters that are passed in registers */
4155 processRegParms (FUNC_ARGS(name->type), body);
4157 /* set the stack pointer */
4161 /* allocate & autoinit the block variables */
4162 processBlockVars (body, &stack, ALLOCATE);
4164 /* save the stack information */
4165 if (options.useXstack)
4166 name->xstack = SPEC_STAK (fetype) = stack;
4168 name->stack = SPEC_STAK (fetype) = stack;
4170 /* name needs to be mangled */
4171 sprintf (name->rname, "%s%s", port->fun_prefix, name->name);
4173 body = resolveSymbols (body); /* resolve the symbols */
4174 body = decorateType (body); /* propagateType & do semantic checks */
4176 ex = newAst_VALUE (symbolVal (name)); /* create name */
4177 ex = newNode (FUNCTION, ex, body);
4178 ex->values.args = FUNC_ARGS(name->type);
4180 if (options.dump_tree) PA(ex);
4183 werror (E_FUNC_NO_CODE, name->name);
4187 /* create the node & generate intermediate code */
4189 codeOutFile = code->oFile;
4190 piCode = iCodeFromAst (ex);
4194 werror (E_FUNC_NO_CODE, name->name);
4198 eBBlockFromiCode (piCode);
4200 /* if there are any statics then do them */
4203 GcurMemmap = statsg;
4204 codeOutFile = statsg->oFile;
4205 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos))));
4211 /* dealloc the block variables */
4212 processBlockVars (body, &stack, DEALLOCATE);
4213 /* deallocate paramaters */
4214 deallocParms (FUNC_ARGS(name->type));
4216 if (IFFUNC_ISREENT (name->type))
4219 /* we are done freeup memory & cleanup */
4223 FUNC_HASBODY(name->type) = 1;
4224 addSet (&operKeyReset, name);
4225 applyToSet (operKeyReset, resetParmKey);
4228 cdbStructBlock (1, cdbFile);
4230 cleanUpLevel (LabelTab, 0);
4231 cleanUpBlock (StructTab, 1);
4232 cleanUpBlock (TypedefTab, 1);
4234 xstack->syms = NULL;
4235 istack->syms = NULL;
4240 #define INDENT(x,f) { int i ; for (i=0;i < x; i++) fprintf(f," "); }
4241 /*-----------------------------------------------------------------*/
4242 /* ast_print : prints the ast (for debugging purposes) */
4243 /*-----------------------------------------------------------------*/
4245 void ast_print (ast * tree, FILE *outfile, int indent)
4250 /* can print only decorated trees */
4251 if (!tree->decorated) return;
4253 /* if any child is an error | this one is an error do nothing */
4254 if (tree->isError ||
4255 (tree->left && tree->left->isError) ||
4256 (tree->right && tree->right->isError)) {
4257 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
4261 /* print the line */
4262 /* if not block & function */
4263 if (tree->type == EX_OP &&
4264 (tree->opval.op != FUNCTION &&
4265 tree->opval.op != BLOCK &&
4266 tree->opval.op != NULLOP)) {
4269 if (tree->opval.op == FUNCTION) {
4271 value *args=FUNC_ARGS(tree->left->opval.val->type);
4272 fprintf(outfile,"FUNCTION (%s=%p) type (",
4273 tree->left->opval.val->name, tree);
4274 printTypeChain (tree->ftype,outfile);
4275 fprintf(outfile,") args (");
4278 fprintf (outfile, ", ");
4280 printTypeChain (args ? args->type : NULL, outfile);
4282 args= args ? args->next : NULL;
4284 fprintf(outfile,")\n");
4285 ast_print(tree->left,outfile,indent);
4286 ast_print(tree->right,outfile,indent);
4289 if (tree->opval.op == BLOCK) {
4290 symbol *decls = tree->values.sym;
4291 INDENT(indent,outfile);
4292 fprintf(outfile,"{\n");
4294 INDENT(indent+4,outfile);
4295 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
4296 decls->name, decls);
4297 printTypeChain(decls->type,outfile);
4298 fprintf(outfile,")\n");
4300 decls = decls->next;
4302 ast_print(tree->right,outfile,indent+4);
4303 INDENT(indent,outfile);
4304 fprintf(outfile,"}\n");
4307 if (tree->opval.op == NULLOP) {
4308 fprintf(outfile,"\n");
4309 ast_print(tree->left,outfile,indent);
4310 fprintf(outfile,"\n");
4311 ast_print(tree->right,outfile,indent);
4314 INDENT(indent,outfile);
4316 /*------------------------------------------------------------------*/
4317 /*----------------------------*/
4318 /* leaf has been reached */
4319 /*----------------------------*/
4320 /* if this is of type value */
4321 /* just get the type */
4322 if (tree->type == EX_VALUE) {
4324 if (IS_LITERAL (tree->opval.val->etype)) {
4325 fprintf(outfile,"CONSTANT (%p) value = %d, 0x%x, %g", tree,
4326 (int) floatFromVal(tree->opval.val),
4327 (int) floatFromVal(tree->opval.val),
4328 floatFromVal(tree->opval.val));
4329 } else if (tree->opval.val->sym) {
4330 /* if the undefined flag is set then give error message */
4331 if (tree->opval.val->sym->undefined) {
4332 fprintf(outfile,"UNDEFINED SYMBOL ");
4334 fprintf(outfile,"SYMBOL ");
4336 fprintf(outfile,"(%s=%p)",
4337 tree->opval.val->sym->name,tree);
4340 fprintf(outfile," type (");
4341 printTypeChain(tree->ftype,outfile);
4342 fprintf(outfile,")\n");
4344 fprintf(outfile,"\n");
4349 /* if type link for the case of cast */
4350 if (tree->type == EX_LINK) {
4351 fprintf(outfile,"TYPENODE (%p) type = (",tree);
4352 printTypeChain(tree->opval.lnk,outfile);
4353 fprintf(outfile,")\n");
4358 /* depending on type of operator do */
4360 switch (tree->opval.op) {
4361 /*------------------------------------------------------------------*/
4362 /*----------------------------*/
4364 /*----------------------------*/
4366 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
4367 printTypeChain(tree->ftype,outfile);
4368 fprintf(outfile,")\n");
4369 ast_print(tree->left,outfile,indent+4);
4370 ast_print(tree->right,outfile,indent+4);
4373 /*------------------------------------------------------------------*/
4374 /*----------------------------*/
4376 /*----------------------------*/
4378 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
4379 printTypeChain(tree->ftype,outfile);
4380 fprintf(outfile,")\n");
4381 ast_print(tree->left,outfile,indent+4);
4382 ast_print(tree->right,outfile,indent+4);
4385 /*------------------------------------------------------------------*/
4386 /*----------------------------*/
4387 /* struct/union pointer */
4388 /*----------------------------*/
4390 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
4391 printTypeChain(tree->ftype,outfile);
4392 fprintf(outfile,")\n");
4393 ast_print(tree->left,outfile,indent+4);
4394 ast_print(tree->right,outfile,indent+4);
4397 /*------------------------------------------------------------------*/
4398 /*----------------------------*/
4399 /* ++/-- operation */
4400 /*----------------------------*/
4401 case INC_OP: /* incerement operator unary so left only */
4402 fprintf(outfile,"INC_OP (%p) type (",tree);
4403 printTypeChain(tree->ftype,outfile);
4404 fprintf(outfile,")\n");
4405 ast_print(tree->left,outfile,indent+4);
4409 fprintf(outfile,"DEC_OP (%p) type (",tree);
4410 printTypeChain(tree->ftype,outfile);
4411 fprintf(outfile,")\n");
4412 ast_print(tree->left,outfile,indent+4);
4415 /*------------------------------------------------------------------*/
4416 /*----------------------------*/
4418 /*----------------------------*/
4421 fprintf(outfile,"& (%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);
4427 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
4428 printTypeChain(tree->ftype,outfile);
4429 fprintf(outfile,")\n");
4430 ast_print(tree->left,outfile,indent+4);
4431 ast_print(tree->right,outfile,indent+4);
4434 /*----------------------------*/
4436 /*----------------------------*/
4438 fprintf(outfile,"OR (%p) type (",tree);
4439 printTypeChain(tree->ftype,outfile);
4440 fprintf(outfile,")\n");
4441 ast_print(tree->left,outfile,indent+4);
4442 ast_print(tree->right,outfile,indent+4);
4444 /*------------------------------------------------------------------*/
4445 /*----------------------------*/
4447 /*----------------------------*/
4449 fprintf(outfile,"XOR (%p) type (",tree);
4450 printTypeChain(tree->ftype,outfile);
4451 fprintf(outfile,")\n");
4452 ast_print(tree->left,outfile,indent+4);
4453 ast_print(tree->right,outfile,indent+4);
4456 /*------------------------------------------------------------------*/
4457 /*----------------------------*/
4459 /*----------------------------*/
4461 fprintf(outfile,"DIV (%p) type (",tree);
4462 printTypeChain(tree->ftype,outfile);
4463 fprintf(outfile,")\n");
4464 ast_print(tree->left,outfile,indent+4);
4465 ast_print(tree->right,outfile,indent+4);
4467 /*------------------------------------------------------------------*/
4468 /*----------------------------*/
4470 /*----------------------------*/
4472 fprintf(outfile,"MOD (%p) type (",tree);
4473 printTypeChain(tree->ftype,outfile);
4474 fprintf(outfile,")\n");
4475 ast_print(tree->left,outfile,indent+4);
4476 ast_print(tree->right,outfile,indent+4);
4479 /*------------------------------------------------------------------*/
4480 /*----------------------------*/
4481 /* address dereference */
4482 /*----------------------------*/
4483 case '*': /* can be unary : if right is null then unary operation */
4485 fprintf(outfile,"DEREF (%p) type (",tree);
4486 printTypeChain(tree->ftype,outfile);
4487 fprintf(outfile,")\n");
4488 ast_print(tree->left,outfile,indent+4);
4491 /*------------------------------------------------------------------*/
4492 /*----------------------------*/
4493 /* multiplication */
4494 /*----------------------------*/
4495 fprintf(outfile,"MULT (%p) type (",tree);
4496 printTypeChain(tree->ftype,outfile);
4497 fprintf(outfile,")\n");
4498 ast_print(tree->left,outfile,indent+4);
4499 ast_print(tree->right,outfile,indent+4);
4503 /*------------------------------------------------------------------*/
4504 /*----------------------------*/
4505 /* unary '+' operator */
4506 /*----------------------------*/
4510 fprintf(outfile,"UPLUS (%p) type (",tree);
4511 printTypeChain(tree->ftype,outfile);
4512 fprintf(outfile,")\n");
4513 ast_print(tree->left,outfile,indent+4);
4515 /*------------------------------------------------------------------*/
4516 /*----------------------------*/
4518 /*----------------------------*/
4519 fprintf(outfile,"ADD (%p) type (",tree);
4520 printTypeChain(tree->ftype,outfile);
4521 fprintf(outfile,")\n");
4522 ast_print(tree->left,outfile,indent+4);
4523 ast_print(tree->right,outfile,indent+4);
4526 /*------------------------------------------------------------------*/
4527 /*----------------------------*/
4529 /*----------------------------*/
4530 case '-': /* can be unary */
4532 fprintf(outfile,"UMINUS (%p) type (",tree);
4533 printTypeChain(tree->ftype,outfile);
4534 fprintf(outfile,")\n");
4535 ast_print(tree->left,outfile,indent+4);
4537 /*------------------------------------------------------------------*/
4538 /*----------------------------*/
4540 /*----------------------------*/
4541 fprintf(outfile,"SUB (%p) type (",tree);
4542 printTypeChain(tree->ftype,outfile);
4543 fprintf(outfile,")\n");
4544 ast_print(tree->left,outfile,indent+4);
4545 ast_print(tree->right,outfile,indent+4);
4548 /*------------------------------------------------------------------*/
4549 /*----------------------------*/
4551 /*----------------------------*/
4553 fprintf(outfile,"COMPL (%p) type (",tree);
4554 printTypeChain(tree->ftype,outfile);
4555 fprintf(outfile,")\n");
4556 ast_print(tree->left,outfile,indent+4);
4558 /*------------------------------------------------------------------*/
4559 /*----------------------------*/
4561 /*----------------------------*/
4563 fprintf(outfile,"NOT (%p) type (",tree);
4564 printTypeChain(tree->ftype,outfile);
4565 fprintf(outfile,")\n");
4566 ast_print(tree->left,outfile,indent+4);
4568 /*------------------------------------------------------------------*/
4569 /*----------------------------*/
4571 /*----------------------------*/
4573 fprintf(outfile,"RRC (%p) type (",tree);
4574 printTypeChain(tree->ftype,outfile);
4575 fprintf(outfile,")\n");
4576 ast_print(tree->left,outfile,indent+4);
4580 fprintf(outfile,"RLC (%p) type (",tree);
4581 printTypeChain(tree->ftype,outfile);
4582 fprintf(outfile,")\n");
4583 ast_print(tree->left,outfile,indent+4);
4586 fprintf(outfile,"GETHBIT (%p) type (",tree);
4587 printTypeChain(tree->ftype,outfile);
4588 fprintf(outfile,")\n");
4589 ast_print(tree->left,outfile,indent+4);
4592 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
4593 printTypeChain(tree->ftype,outfile);
4594 fprintf(outfile,")\n");
4595 ast_print(tree->left,outfile,indent+4);
4596 ast_print(tree->right,outfile,indent+4);
4599 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
4600 printTypeChain(tree->ftype,outfile);
4601 fprintf(outfile,")\n");
4602 ast_print(tree->left,outfile,indent+4);
4603 ast_print(tree->right,outfile,indent+4);
4605 /*------------------------------------------------------------------*/
4606 /*----------------------------*/
4608 /*----------------------------*/
4609 case CAST: /* change the type */
4610 fprintf(outfile,"CAST (%p) from type (",tree);
4611 printTypeChain(tree->right->ftype,outfile);
4612 fprintf(outfile,") to type (");
4613 printTypeChain(tree->ftype,outfile);
4614 fprintf(outfile,")\n");
4615 ast_print(tree->right,outfile,indent+4);
4619 fprintf(outfile,"ANDAND (%p) type (",tree);
4620 printTypeChain(tree->ftype,outfile);
4621 fprintf(outfile,")\n");
4622 ast_print(tree->left,outfile,indent+4);
4623 ast_print(tree->right,outfile,indent+4);
4626 fprintf(outfile,"OROR (%p) type (",tree);
4627 printTypeChain(tree->ftype,outfile);
4628 fprintf(outfile,")\n");
4629 ast_print(tree->left,outfile,indent+4);
4630 ast_print(tree->right,outfile,indent+4);
4633 /*------------------------------------------------------------------*/
4634 /*----------------------------*/
4635 /* comparison operators */
4636 /*----------------------------*/
4638 fprintf(outfile,"GT(>) (%p) type (",tree);
4639 printTypeChain(tree->ftype,outfile);
4640 fprintf(outfile,")\n");
4641 ast_print(tree->left,outfile,indent+4);
4642 ast_print(tree->right,outfile,indent+4);
4645 fprintf(outfile,"LT(<) (%p) type (",tree);
4646 printTypeChain(tree->ftype,outfile);
4647 fprintf(outfile,")\n");
4648 ast_print(tree->left,outfile,indent+4);
4649 ast_print(tree->right,outfile,indent+4);
4652 fprintf(outfile,"LE(<=) (%p) type (",tree);
4653 printTypeChain(tree->ftype,outfile);
4654 fprintf(outfile,")\n");
4655 ast_print(tree->left,outfile,indent+4);
4656 ast_print(tree->right,outfile,indent+4);
4659 fprintf(outfile,"GE(>=) (%p) type (",tree);
4660 printTypeChain(tree->ftype,outfile);
4661 fprintf(outfile,")\n");
4662 ast_print(tree->left,outfile,indent+4);
4663 ast_print(tree->right,outfile,indent+4);
4666 fprintf(outfile,"EQ(==) (%p) type (",tree);
4667 printTypeChain(tree->ftype,outfile);
4668 fprintf(outfile,")\n");
4669 ast_print(tree->left,outfile,indent+4);
4670 ast_print(tree->right,outfile,indent+4);
4673 fprintf(outfile,"NE(!=) (%p) type (",tree);
4674 printTypeChain(tree->ftype,outfile);
4675 fprintf(outfile,")\n");
4676 ast_print(tree->left,outfile,indent+4);
4677 ast_print(tree->right,outfile,indent+4);
4678 /*------------------------------------------------------------------*/
4679 /*----------------------------*/
4681 /*----------------------------*/
4682 case SIZEOF: /* evaluate wihout code generation */
4683 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
4686 /*------------------------------------------------------------------*/
4687 /*----------------------------*/
4688 /* conditional operator '?' */
4689 /*----------------------------*/
4691 fprintf(outfile,"QUEST(?) (%p) type (",tree);
4692 printTypeChain(tree->ftype,outfile);
4693 fprintf(outfile,")\n");
4694 ast_print(tree->left,outfile,indent+4);
4695 ast_print(tree->right,outfile,indent+4);
4699 fprintf(outfile,"COLON(:) (%p) type (",tree);
4700 printTypeChain(tree->ftype,outfile);
4701 fprintf(outfile,")\n");
4702 ast_print(tree->left,outfile,indent+4);
4703 ast_print(tree->right,outfile,indent+4);
4706 /*------------------------------------------------------------------*/
4707 /*----------------------------*/
4708 /* assignment operators */
4709 /*----------------------------*/
4711 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
4712 printTypeChain(tree->ftype,outfile);
4713 fprintf(outfile,")\n");
4714 ast_print(tree->left,outfile,indent+4);
4715 ast_print(tree->right,outfile,indent+4);
4718 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
4719 printTypeChain(tree->ftype,outfile);
4720 fprintf(outfile,")\n");
4721 ast_print(tree->left,outfile,indent+4);
4722 ast_print(tree->right,outfile,indent+4);
4725 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
4726 printTypeChain(tree->ftype,outfile);
4727 fprintf(outfile,")\n");
4728 ast_print(tree->left,outfile,indent+4);
4729 ast_print(tree->right,outfile,indent+4);
4732 fprintf(outfile,"ORASS(*=) (%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,"XORASS(*=) (%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 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
4747 printTypeChain(tree->ftype,outfile);
4748 fprintf(outfile,")\n");
4749 ast_print(tree->left,outfile,indent+4);
4750 ast_print(tree->right,outfile,indent+4);
4753 fprintf(outfile,"LSHFTASS(*=) (%p) type (",tree);
4754 printTypeChain(tree->ftype,outfile);
4755 fprintf(outfile,")\n");
4756 ast_print(tree->left,outfile,indent+4);
4757 ast_print(tree->right,outfile,indent+4);
4759 /*------------------------------------------------------------------*/
4760 /*----------------------------*/
4762 /*----------------------------*/
4764 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
4765 printTypeChain(tree->ftype,outfile);
4766 fprintf(outfile,")\n");
4767 ast_print(tree->left,outfile,indent+4);
4768 ast_print(tree->right,outfile,indent+4);
4770 /*------------------------------------------------------------------*/
4771 /*----------------------------*/
4773 /*----------------------------*/
4775 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
4776 printTypeChain(tree->ftype,outfile);
4777 fprintf(outfile,")\n");
4778 ast_print(tree->left,outfile,indent+4);
4779 ast_print(tree->right,outfile,indent+4);
4781 /*------------------------------------------------------------------*/
4782 /*----------------------------*/
4783 /* straight assignemnt */
4784 /*----------------------------*/
4786 fprintf(outfile,"ASSIGN(=) (%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);
4792 /*------------------------------------------------------------------*/
4793 /*----------------------------*/
4794 /* comma operator */
4795 /*----------------------------*/
4797 fprintf(outfile,"COMMA(,) (%p) type (",tree);
4798 printTypeChain(tree->ftype,outfile);
4799 fprintf(outfile,")\n");
4800 ast_print(tree->left,outfile,indent+4);
4801 ast_print(tree->right,outfile,indent+4);
4803 /*------------------------------------------------------------------*/
4804 /*----------------------------*/
4806 /*----------------------------*/
4809 fprintf(outfile,"CALL (%p) type (",tree);
4810 printTypeChain(tree->ftype,outfile);
4811 fprintf(outfile,")\n");
4812 ast_print(tree->left,outfile,indent+4);
4813 ast_print(tree->right,outfile,indent+4);
4816 fprintf(outfile,"PARMS\n");
4817 ast_print(tree->left,outfile,indent+4);
4818 if (tree->right && !IS_AST_PARAM(tree->right)) {
4819 ast_print(tree->right,outfile,indent+4);
4822 /*------------------------------------------------------------------*/
4823 /*----------------------------*/
4824 /* return statement */
4825 /*----------------------------*/
4827 fprintf(outfile,"RETURN (%p) type (",tree);
4828 printTypeChain(tree->right->ftype,outfile);
4829 fprintf(outfile,")\n");
4830 ast_print(tree->right,outfile,indent+4);
4832 /*------------------------------------------------------------------*/
4833 /*----------------------------*/
4834 /* label statement */
4835 /*----------------------------*/
4837 fprintf(outfile,"LABEL (%p)",tree);
4838 ast_print(tree->left,outfile,indent+4);
4839 ast_print(tree->right,outfile,indent);
4841 /*------------------------------------------------------------------*/
4842 /*----------------------------*/
4843 /* switch statement */
4844 /*----------------------------*/
4848 fprintf(outfile,"SWITCH (%p) ",tree);
4849 ast_print(tree->left,outfile,0);
4850 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
4851 INDENT(indent+4,outfile);
4852 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
4853 (int) floatFromVal(val),
4854 tree->values.switchVals.swNum,
4855 (int) floatFromVal(val));
4857 ast_print(tree->right,outfile,indent);
4860 /*------------------------------------------------------------------*/
4861 /*----------------------------*/
4863 /*----------------------------*/
4865 fprintf(outfile,"IF (%p) \n",tree);
4866 ast_print(tree->left,outfile,indent+4);
4867 if (tree->trueLabel) {
4868 INDENT(indent,outfile);
4869 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
4871 if (tree->falseLabel) {
4872 INDENT(indent,outfile);
4873 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
4875 ast_print(tree->right,outfile,indent+4);
4877 /*------------------------------------------------------------------*/
4878 /*----------------------------*/
4880 /*----------------------------*/
4882 fprintf(outfile,"FOR (%p) \n",tree);
4883 if (AST_FOR( tree, initExpr)) {
4884 INDENT(indent+4,outfile);
4885 fprintf(outfile,"INIT EXPR ");
4886 ast_print(AST_FOR(tree, initExpr),outfile,indent+4);
4888 if (AST_FOR( tree, condExpr)) {
4889 INDENT(indent+4,outfile);
4890 fprintf(outfile,"COND EXPR ");
4891 ast_print(AST_FOR(tree, condExpr),outfile,indent+4);
4893 if (AST_FOR( tree, loopExpr)) {
4894 INDENT(indent+4,outfile);
4895 fprintf(outfile,"LOOP EXPR ");
4896 ast_print(AST_FOR(tree, loopExpr),outfile,indent+4);
4898 fprintf(outfile,"FOR LOOP BODY \n");
4899 ast_print(tree->left,outfile,indent+4);
4908 ast_print(t,stdout,0);