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 static ast *createIval (ast *, sym_link *, initList *, ast *);
52 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
53 static ast *optimizeCompare (ast *);
54 ast *optimizeRRCRLC (ast *);
55 ast *optimizeGetHbit (ast *);
56 ast *backPatchLabels (ast *, symbol *, symbol *);
59 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
64 printTypeChain (tree->ftype, stdout);
69 /*-----------------------------------------------------------------*/
70 /* newAst - creates a fresh node for an expression tree */
71 /*-----------------------------------------------------------------*/
73 newAst_ (unsigned type)
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;
90 newAst_VALUE (value * val)
92 ast *ex = newAst_ (EX_VALUE);
98 newAst_OP (unsigned op)
100 ast *ex = newAst_ (EX_OP);
106 newAst_LINK (sym_link * val)
108 ast *ex = newAst_ (EX_LINK);
114 newAst_STMNT (unsigned val)
116 ast *ex = newAst_ (EX_STMNT);
117 ex->opval.stmnt = val;
121 /*-----------------------------------------------------------------*/
122 /* newNode - creates a new node */
123 /*-----------------------------------------------------------------*/
125 newNode (long op, ast * left, ast * right)
136 /*-----------------------------------------------------------------*/
137 /* newIfxNode - creates a new Ifx Node */
138 /*-----------------------------------------------------------------*/
140 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
144 /* if this is a literal then we already know the result */
145 if (condAst->etype && IS_LITERAL (condAst->etype))
147 /* then depending on the expression value */
148 if (floatFromVal (condAst->opval.val))
149 ifxNode = newNode (GOTO,
150 newAst_VALUE (symbolVal (trueLabel)),
153 ifxNode = newNode (GOTO,
154 newAst_VALUE (symbolVal (falseLabel)),
159 ifxNode = newNode (IFX, condAst, NULL);
160 ifxNode->trueLabel = trueLabel;
161 ifxNode->falseLabel = falseLabel;
167 /*-----------------------------------------------------------------*/
168 /* copyAstValues - copies value portion of ast if needed */
169 /*-----------------------------------------------------------------*/
171 copyAstValues (ast * dest, ast * src)
173 switch (src->opval.op)
176 dest->values.sym = copySymbolChain (src->values.sym);
180 dest->values.switchVals.swVals =
181 copyValue (src->values.switchVals.swVals);
182 dest->values.switchVals.swDefault =
183 src->values.switchVals.swDefault;
184 dest->values.switchVals.swNum =
185 src->values.switchVals.swNum;
189 dest->values.inlineasm = Safe_alloc (strlen (src->values.inlineasm) + 1);
190 strcpy (dest->values.inlineasm, src->values.inlineasm);
194 dest->values.constlist = copyLiteralList(src->values.constlist);
198 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
199 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
200 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
201 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
202 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
203 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
204 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
209 /*-----------------------------------------------------------------*/
210 /* copyAst - makes a copy of a given astession */
211 /*-----------------------------------------------------------------*/
220 dest = Safe_alloc ( sizeof (ast));
222 dest->type = src->type;
223 dest->lineno = src->lineno;
224 dest->level = src->level;
225 dest->funcName = src->funcName;
228 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
230 /* if this is a leaf */
232 if (src->type == EX_VALUE)
234 dest->opval.val = copyValue (src->opval.val);
239 if (src->type == EX_LINK)
241 dest->opval.lnk = copyLinkChain (src->opval.lnk);
245 dest->opval.op = src->opval.op;
247 /* if this is a node that has special values */
248 copyAstValues (dest, src);
250 dest->trueLabel = copySymbol (src->trueLabel);
251 dest->falseLabel = copySymbol (src->falseLabel);
252 dest->left = copyAst (src->left);
253 dest->right = copyAst (src->right);
259 /*-----------------------------------------------------------------*/
260 /* removeIncDecOps: remove for side effects in *_ASSIGN's */
261 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
262 /*-----------------------------------------------------------------*/
263 ast *removeIncDecOps (ast * tree) {
265 // traverse the tree and remove inc/dec ops
270 if (tree->type == EX_OP &&
271 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
278 tree->left=removeIncDecOps(tree->left);
279 tree->right=removeIncDecOps(tree->right);
284 /*-----------------------------------------------------------------*/
285 /* hasSEFcalls - returns TRUE if tree has a function call */
286 /*-----------------------------------------------------------------*/
288 hasSEFcalls (ast * tree)
293 if (tree->type == EX_OP &&
294 (tree->opval.op == CALL ||
295 tree->opval.op == PCALL ||
296 tree->opval.op == '=' ||
297 tree->opval.op == INC_OP ||
298 tree->opval.op == DEC_OP))
301 return (hasSEFcalls (tree->left) |
302 hasSEFcalls (tree->right));
305 /*-----------------------------------------------------------------*/
306 /* isAstEqual - compares two asts & returns 1 if they are equal */
307 /*-----------------------------------------------------------------*/
309 isAstEqual (ast * t1, ast * t2)
318 if (t1->type != t2->type)
324 if (t1->opval.op != t2->opval.op)
326 return (isAstEqual (t1->left, t2->left) &&
327 isAstEqual (t1->right, t2->right));
331 if (t1->opval.val->sym)
333 if (!t2->opval.val->sym)
336 return isSymbolEqual (t1->opval.val->sym,
341 if (t2->opval.val->sym)
344 return (floatFromVal (t1->opval.val) ==
345 floatFromVal (t2->opval.val));
349 /* only compare these two types */
357 /*-----------------------------------------------------------------*/
358 /* resolveSymbols - resolve symbols from the symbol table */
359 /*-----------------------------------------------------------------*/
361 resolveSymbols (ast * tree)
363 /* walk the entire tree and check for values */
364 /* with symbols if we find one then replace */
365 /* symbol with that from the symbol table */
371 /* if not block & function */
372 if (tree->type == EX_OP &&
373 (tree->opval.op != FUNCTION &&
374 tree->opval.op != BLOCK &&
375 tree->opval.op != NULLOP))
377 filename = tree->filename;
378 lineno = tree->lineno;
381 /* make sure we resolve the true & false labels for ifx */
382 if (tree->type == EX_OP && tree->opval.op == IFX)
388 if ((csym = findSym (LabelTab, tree->trueLabel,
389 tree->trueLabel->name)))
390 tree->trueLabel = csym;
392 werror (E_LABEL_UNDEF, tree->trueLabel->name);
395 if (tree->falseLabel)
397 if ((csym = findSym (LabelTab,
399 tree->falseLabel->name)))
400 tree->falseLabel = csym;
402 werror (E_LABEL_UNDEF, tree->falseLabel->name);
407 /* if this is a label resolve it from the labelTab */
408 if (IS_AST_VALUE (tree) &&
409 tree->opval.val->sym &&
410 tree->opval.val->sym->islbl)
413 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
414 tree->opval.val->sym->name);
417 werror (E_LABEL_UNDEF, tree->opval.val->sym->name);
419 tree->opval.val->sym = csym;
421 goto resolveChildren;
424 /* do only for leafs */
425 if (IS_AST_VALUE (tree) &&
426 tree->opval.val->sym &&
427 !tree->opval.val->sym->implicit)
430 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
432 /* if found in the symbol table & they r not the same */
433 if (csym && tree->opval.val->sym != csym)
435 tree->opval.val->sym = csym;
436 tree->opval.val->type = csym->type;
437 tree->opval.val->etype = csym->etype;
440 /* if not found in the symbol table */
441 /* mark it as undefined assume it is */
442 /* an integer in data space */
443 if (!csym && !tree->opval.val->sym->implicit)
446 /* if this is a function name then */
447 /* mark it as returning an int */
450 tree->opval.val->sym->type = newLink ();
451 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
452 tree->opval.val->sym->type->next =
453 tree->opval.val->sym->etype = newIntLink ();
454 tree->opval.val->etype = tree->opval.val->etype;
455 tree->opval.val->type = tree->opval.val->sym->type;
456 werror (W_IMPLICIT_FUNC, tree->opval.val->sym->name);
457 allocVariables (tree->opval.val->sym);
461 tree->opval.val->sym->undefined = 1;
462 tree->opval.val->type =
463 tree->opval.val->etype = newIntLink ();
464 tree->opval.val->sym->type =
465 tree->opval.val->sym->etype = newIntLink ();
471 resolveSymbols (tree->left);
472 resolveSymbols (tree->right);
477 /*-----------------------------------------------------------------*/
478 /* setAstLineno - walks a ast tree & sets the line number */
479 /*-----------------------------------------------------------------*/
480 int setAstLineno (ast * tree, int lineno)
485 tree->lineno = lineno;
486 setAstLineno (tree->left, lineno);
487 setAstLineno (tree->right, lineno);
491 /*-----------------------------------------------------------------*/
492 /* funcOfType :- function of type with name */
493 /*-----------------------------------------------------------------*/
495 funcOfType (char *name, sym_link * type, sym_link * argType,
499 /* create the symbol */
500 sym = newSymbol (name, 0);
502 /* setup return value */
503 sym->type = newLink ();
504 DCL_TYPE (sym->type) = FUNCTION;
505 sym->type->next = copyLinkChain (type);
506 sym->etype = getSpec (sym->type);
507 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
509 /* if arguments required */
513 args = FUNC_ARGS(sym->type) = newValue ();
517 args->type = copyLinkChain (argType);
518 args->etype = getSpec (args->type);
519 SPEC_EXTR(args->etype)=1;
522 args = args->next = newValue ();
529 allocVariables (sym);
534 /*-----------------------------------------------------------------*/
535 /* funcOfTypeVarg :- function of type with name and argtype */
536 /*-----------------------------------------------------------------*/
538 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
543 /* create the symbol */
544 sym = newSymbol (name, 0);
546 /* setup return value */
547 sym->type = newLink ();
548 DCL_TYPE (sym->type) = FUNCTION;
549 sym->type->next = typeFromStr(rtype);
550 sym->etype = getSpec (sym->type);
552 /* if arguments required */
555 args = FUNC_ARGS(sym->type) = newValue ();
557 for ( i = 0 ; i < nArgs ; i++ ) {
558 args->type = typeFromStr(atypes[i]);
559 args->etype = getSpec (args->type);
560 SPEC_EXTR(args->etype)=1;
561 if ((i + 1) == nArgs) break;
562 args = args->next = newValue ();
569 allocVariables (sym);
574 /*-----------------------------------------------------------------*/
575 /* reverseParms - will reverse a parameter tree */
576 /*-----------------------------------------------------------------*/
578 reverseParms (ast * ptree)
584 /* top down if we find a nonParm tree then quit */
585 if (ptree->type == EX_OP && ptree->opval.op == PARAM)
588 ptree->left = ptree->right;
589 ptree->right = ttree;
590 reverseParms (ptree->left);
591 reverseParms (ptree->right);
597 /*-----------------------------------------------------------------*/
598 /* processParms - makes sure the parameters are okay and do some */
599 /* processing with them */
600 /*-----------------------------------------------------------------*/
602 processParms (ast * func,
605 int *parmNumber, // unused, although updated
608 /* if none of them exist */
609 if (!defParm && !actParm)
613 if (getenv("DEBUG_SANITY")) {
614 fprintf (stderr, "processParms: %s ", defParm->name);
616 /* make sure the type is complete and sane */
617 checkTypeSanity(defParm->etype, defParm->name);
620 /* if the function is being called via a pointer & */
621 /* it has not been defined a reentrant then we cannot */
622 /* have parameters */
623 if (func->type != EX_VALUE && !IFFUNC_ISREENT (func->ftype) && !options.stackAuto)
625 werror (W_NONRENT_ARGS);
629 /* if defined parameters ended but actual parameters */
630 /* exist and this is not defined as a variable arg */
631 if (!defParm && actParm && !IFFUNC_HASVARARGS(func->ftype))
633 werror (E_TOO_MANY_PARMS);
637 /* if defined parameters present but no actual parameters */
638 if (defParm && !actParm)
640 werror (E_TOO_FEW_PARMS);
644 if (IS_VOID(actParm->ftype)) {
645 werror (E_VOID_VALUE_USED);
649 /* If this is a varargs function... */
650 if (!defParm && actParm && IFFUNC_HASVARARGS(func->ftype))
655 if (IS_CAST_OP (actParm)
656 || (IS_AST_LIT_VALUE (actParm) && actParm->values.literalFromCast))
658 /* Parameter was explicitly typecast; don't touch it. */
662 ftype = actParm->ftype;
664 /* If it's a small integer, upcast to int. */
665 if (IS_INTEGRAL (ftype)
666 && (getSize (ftype) < (unsigned) INTSIZE))
668 newType = newAst_LINK(INTTYPE);
671 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
673 newType = newAst_LINK (copyLinkChain(ftype));
674 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
677 if (IS_AGGREGATE (ftype))
679 newType = newAst_LINK (copyLinkChain (ftype));
680 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
684 /* cast required; change this op to a cast. */
685 ast *parmCopy = decorateType(resolveSymbols (copyAst (actParm)));
687 actParm->type = EX_OP;
688 actParm->opval.op = CAST;
689 actParm->left = newType;
690 actParm->right = parmCopy;
691 decorateType (actParm);
693 else if (actParm->type == EX_OP && actParm->opval.op == PARAM)
695 return (processParms (func, NULL, actParm->left, parmNumber, FALSE) ||
696 processParms (func, NULL, actParm->right, parmNumber, rightmost));
701 /* if defined parameters ended but actual has not & */
703 if (!defParm && actParm &&
704 (options.stackAuto || IFFUNC_ISREENT (func->ftype)))
707 resolveSymbols (actParm);
708 /* if this is a PARAM node then match left & right */
709 if (actParm->type == EX_OP && actParm->opval.op == PARAM)
711 return (processParms (func, defParm, actParm->left, parmNumber, FALSE) ||
712 processParms (func, defParm->next, actParm->right, parmNumber, rightmost));
716 /* If we have found a value node by following only right-hand links,
717 * then we know that there are no more values after us.
719 * Therefore, if there are more defined parameters, the caller didn't
722 if (rightmost && defParm->next)
724 werror (E_TOO_FEW_PARMS);
729 /* the parameter type must be at least castable */
730 if (compareType (defParm->type, actParm->ftype) == 0) {
731 werror (E_INCOMPAT_TYPES);
732 printFromToType (actParm->ftype, defParm->type);
736 /* if the parameter is castable then add the cast */
737 if (compareType (defParm->type, actParm->ftype) < 0)
739 ast *pTree = decorateType(resolveSymbols (copyAst (actParm)));
741 /* now change the current one to a cast */
742 actParm->type = EX_OP;
743 actParm->opval.op = CAST;
744 actParm->left = newAst_LINK (defParm->type);
745 actParm->right = pTree;
746 actParm->etype = defParm->etype;
747 actParm->ftype = defParm->type;
748 actParm->decorated=0; /* force typechecking */
749 decorateType (actParm);
752 /* make a copy and change the regparm type to the defined parm */
753 actParm->etype = getSpec (actParm->ftype = copyLinkChain (actParm->ftype));
754 SPEC_REGPARM (actParm->etype) = SPEC_REGPARM (defParm->etype);
755 SPEC_ARGREG (actParm->etype) = SPEC_ARGREG (defParm->etype);
759 /*-----------------------------------------------------------------*/
760 /* createIvalType - generates ival for basic types */
761 /*-----------------------------------------------------------------*/
763 createIvalType (ast * sym, sym_link * type, initList * ilist)
767 /* if initList is deep */
768 if (ilist->type == INIT_DEEP)
769 ilist = ilist->init.deep;
771 iExpr = decorateType (resolveSymbols (list2expr (ilist)));
772 return decorateType (newNode ('=', sym, iExpr));
775 /*-----------------------------------------------------------------*/
776 /* createIvalStruct - generates initial value for structures */
777 /*-----------------------------------------------------------------*/
779 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
786 sflds = SPEC_STRUCT (type)->fields;
787 if (ilist->type != INIT_DEEP)
789 werror (E_INIT_STRUCT, "");
793 iloop = ilist->init.deep;
795 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
797 /* if we have come to end */
801 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
802 lAst = decorateType (resolveSymbols (lAst));
803 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast)));
807 werror (W_EXCESS_INITIALIZERS, "struct",
808 sym->opval.val->sym->name, sym->opval.val->sym->lineDef);
815 /*-----------------------------------------------------------------*/
816 /* createIvalArray - generates code for array initialization */
817 /*-----------------------------------------------------------------*/
819 createIvalArray (ast * sym, sym_link * type, initList * ilist)
823 int lcnt = 0, size = 0;
824 literalList *literalL;
826 /* take care of the special case */
827 /* array of characters can be init */
829 if (IS_CHAR (type->next))
830 if ((rast = createIvalCharPtr (sym,
832 decorateType (resolveSymbols (list2expr (ilist))))))
834 return decorateType (resolveSymbols (rast));
836 /* not the special case */
837 if (ilist->type != INIT_DEEP)
839 werror (E_INIT_STRUCT, "");
843 iloop = ilist->init.deep;
844 lcnt = DCL_ELEM (type);
846 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
850 aSym = decorateType (resolveSymbols(sym));
852 rast = newNode(ARRAYINIT, aSym, NULL);
853 rast->values.constlist = literalL;
855 // Make sure size is set to length of initializer list.
862 if (lcnt && size > lcnt)
864 // Array size was specified, and we have more initializers than needed.
865 char *name=sym->opval.val->sym->name;
866 int lineno=sym->opval.val->sym->lineDef;
868 werror (W_EXCESS_INITIALIZERS, "array", name, lineno);
877 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
878 aSym = decorateType (resolveSymbols (aSym));
879 rast = createIval (aSym, type->next, iloop, rast);
880 iloop = (iloop ? iloop->next : NULL);
886 /* no of elements given and we */
887 /* have generated for all of them */
890 // there has to be a better way
891 char *name=sym->opval.val->sym->name;
892 int lineno=sym->opval.val->sym->lineDef;
893 werror (W_EXCESS_INITIALIZERS, "array", name, lineno);
900 /* if we have not been given a size */
901 if (!DCL_ELEM (type))
903 DCL_ELEM (type) = size;
906 return decorateType (resolveSymbols (rast));
910 /*-----------------------------------------------------------------*/
911 /* createIvalCharPtr - generates initial values for char pointers */
912 /*-----------------------------------------------------------------*/
914 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
918 /* if this is a pointer & right is a literal array then */
919 /* just assignment will do */
920 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
921 SPEC_SCLS (iexpr->etype) == S_CODE)
922 && IS_ARRAY (iexpr->ftype)))
923 return newNode ('=', sym, iexpr);
925 /* left side is an array so we have to assign each */
927 if ((IS_LITERAL (iexpr->etype) ||
928 SPEC_SCLS (iexpr->etype) == S_CODE)
929 && IS_ARRAY (iexpr->ftype))
931 /* for each character generate an assignment */
932 /* to the array element */
933 char *s = SPEC_CVAL (iexpr->etype).v_char;
938 rast = newNode (NULLOP,
942 newAst_VALUE (valueFromLit ((float) i))),
943 newAst_VALUE (valueFromLit (*s))));
947 rast = newNode (NULLOP,
951 newAst_VALUE (valueFromLit ((float) i))),
952 newAst_VALUE (valueFromLit (*s))));
954 // now we don't need iexpr's symbol anymore
956 symbol *sym=AST_SYMBOL(iexpr);
957 memmap *segment=SPEC_OCLS(sym->etype);
958 deleteSetItem(&segment->syms, sym);
960 return decorateType (resolveSymbols (rast));
966 /*-----------------------------------------------------------------*/
967 /* createIvalPtr - generates initial value for pointers */
968 /*-----------------------------------------------------------------*/
970 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
976 if (ilist->type == INIT_DEEP)
977 ilist = ilist->init.deep;
979 iexpr = decorateType (resolveSymbols (list2expr (ilist)));
981 /* if character pointer */
982 if (IS_CHAR (type->next))
983 if ((rast = createIvalCharPtr (sym, type, iexpr)))
986 return newNode ('=', sym, iexpr);
989 /*-----------------------------------------------------------------*/
990 /* createIval - generates code for initial value */
991 /*-----------------------------------------------------------------*/
993 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
1000 /* if structure then */
1001 if (IS_STRUCT (type))
1002 rast = createIvalStruct (sym, type, ilist);
1004 /* if this is a pointer */
1006 rast = createIvalPtr (sym, type, ilist);
1008 /* if this is an array */
1009 if (IS_ARRAY (type))
1010 rast = createIvalArray (sym, type, ilist);
1012 /* if type is SPECIFIER */
1014 rast = createIvalType (sym, type, ilist);
1017 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)));
1019 return decorateType (resolveSymbols (rast));
1022 /*-----------------------------------------------------------------*/
1023 /* initAggregates - initialises aggregate variables with initv */
1024 /*-----------------------------------------------------------------*/
1025 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1026 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1029 /*-----------------------------------------------------------------*/
1030 /* gatherAutoInit - creates assignment expressions for initial */
1032 /*-----------------------------------------------------------------*/
1034 gatherAutoInit (symbol * autoChain)
1041 for (sym = autoChain; sym; sym = sym->next)
1044 /* resolve the symbols in the ival */
1046 resolveIvalSym (sym->ival);
1048 /* if this is a static variable & has an */
1049 /* initial value the code needs to be lifted */
1050 /* here to the main portion since they can be */
1051 /* initialised only once at the start */
1052 if (IS_STATIC (sym->etype) && sym->ival &&
1053 SPEC_SCLS (sym->etype) != S_CODE)
1057 /* insert the symbol into the symbol table */
1058 /* with level = 0 & name = rname */
1059 newSym = copySymbol (sym);
1060 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1062 /* now lift the code to main */
1063 if (IS_AGGREGATE (sym->type)) {
1064 work = initAggregates (sym, sym->ival, NULL);
1066 if (getNelements(sym->type, sym->ival)>1) {
1067 werror (W_EXCESS_INITIALIZERS, "scalar",
1068 sym->name, sym->lineDef);
1070 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1071 list2expr (sym->ival));
1074 setAstLineno (work, sym->lineDef);
1078 staticAutos = newNode (NULLOP, staticAutos, work);
1085 /* if there is an initial value */
1086 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1088 if (IS_AGGREGATE (sym->type)) {
1089 work = initAggregates (sym, sym->ival, NULL);
1091 if (getNelements(sym->type, sym->ival)>1) {
1092 werror (W_EXCESS_INITIALIZERS, "scalar",
1093 sym->name, sym->lineDef);
1095 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1096 list2expr (sym->ival));
1099 setAstLineno (work, sym->lineDef);
1102 init = newNode (NULLOP, init, work);
1111 /*-----------------------------------------------------------------*/
1112 /* stringToSymbol - creates a symbol from a literal string */
1113 /*-----------------------------------------------------------------*/
1115 stringToSymbol (value * val)
1117 char name[SDCC_NAME_MAX + 1];
1118 static int charLbl = 0;
1121 sprintf (name, "_str_%d", charLbl++);
1122 sym = newSymbol (name, 0); /* make it @ level 0 */
1123 strcpy (sym->rname, name);
1125 /* copy the type from the value passed */
1126 sym->type = copyLinkChain (val->type);
1127 sym->etype = getSpec (sym->type);
1128 /* change to storage class & output class */
1129 SPEC_SCLS (sym->etype) = S_CODE;
1130 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1131 SPEC_STAT (sym->etype) = 1;
1132 /* make the level & block = 0 */
1133 sym->block = sym->level = 0;
1135 /* create an ival */
1136 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1141 allocVariables (sym);
1144 return symbolVal (sym);
1148 /*-----------------------------------------------------------------*/
1149 /* processBlockVars - will go thru the ast looking for block if */
1150 /* a block is found then will allocate the syms */
1151 /* will also gather the auto inits present */
1152 /*-----------------------------------------------------------------*/
1154 processBlockVars (ast * tree, int *stack, int action)
1159 /* if this is a block */
1160 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1164 if (action == ALLOCATE)
1166 *stack += allocVariables (tree->values.sym);
1167 autoInit = gatherAutoInit (tree->values.sym);
1169 /* if there are auto inits then do them */
1171 tree->left = newNode (NULLOP, autoInit, tree->left);
1173 else /* action is deallocate */
1174 deallocLocal (tree->values.sym);
1177 processBlockVars (tree->left, stack, action);
1178 processBlockVars (tree->right, stack, action);
1182 /*-------------------------------------------------------------*/
1183 /* constExprTree - returns TRUE if this tree is a constant */
1185 /*-------------------------------------------------------------*/
1186 bool constExprTree (ast *cexpr) {
1192 cexpr = decorateType (resolveSymbols (cexpr));
1194 switch (cexpr->type)
1197 if (IS_AST_LIT_VALUE(cexpr)) {
1198 // this is a literal
1201 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1202 // a function's address will never change
1205 if (IS_AST_SYM_VALUE(cexpr) &&
1206 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1207 // a symbol in code space will never change
1208 // This is only for the 'char *s="hallo"' case and will have to leave
1213 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1214 "unexpected link in expression tree\n");
1217 if (cexpr->opval.op==ARRAYINIT) {
1218 // this is a list of literals
1221 if (cexpr->opval.op=='=') {
1222 return constExprTree(cexpr->right);
1224 if (cexpr->opval.op==CAST) {
1225 // jwk: cast ignored, maybe we should throw a warning here
1226 return constExprTree(cexpr->right);
1228 if (cexpr->opval.op=='&') {
1231 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1234 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1241 /*-----------------------------------------------------------------*/
1242 /* constExprValue - returns the value of a constant expression */
1243 /* or NULL if it is not a constant expression */
1244 /*-----------------------------------------------------------------*/
1246 constExprValue (ast * cexpr, int check)
1248 cexpr = decorateType (resolveSymbols (cexpr));
1250 /* if this is not a constant then */
1251 if (!IS_LITERAL (cexpr->ftype))
1253 /* then check if this is a literal array
1255 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1256 SPEC_CVAL (cexpr->etype).v_char &&
1257 IS_ARRAY (cexpr->ftype))
1259 value *val = valFromType (cexpr->ftype);
1260 SPEC_SCLS (val->etype) = S_LITERAL;
1261 val->sym = cexpr->opval.val->sym;
1262 val->sym->type = copyLinkChain (cexpr->ftype);
1263 val->sym->etype = getSpec (val->sym->type);
1264 strcpy (val->name, cexpr->opval.val->sym->rname);
1268 /* if we are casting a literal value then */
1269 if (IS_AST_OP (cexpr) &&
1270 cexpr->opval.op == CAST &&
1271 IS_LITERAL (cexpr->right->ftype))
1272 return valCastLiteral (cexpr->ftype,
1273 floatFromVal (cexpr->right->opval.val));
1275 if (IS_AST_VALUE (cexpr))
1276 return cexpr->opval.val;
1279 werror (E_CONST_EXPECTED, "found expression");
1284 /* return the value */
1285 return cexpr->opval.val;
1289 /*-----------------------------------------------------------------*/
1290 /* isLabelInAst - will return true if a given label is found */
1291 /*-----------------------------------------------------------------*/
1293 isLabelInAst (symbol * label, ast * tree)
1295 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1298 if (IS_AST_OP (tree) &&
1299 tree->opval.op == LABEL &&
1300 isSymbolEqual (AST_SYMBOL (tree->left), label))
1303 return isLabelInAst (label, tree->right) &&
1304 isLabelInAst (label, tree->left);
1308 /*-----------------------------------------------------------------*/
1309 /* isLoopCountable - return true if the loop count can be determi- */
1310 /* -ned at compile time . */
1311 /*-----------------------------------------------------------------*/
1313 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1314 symbol ** sym, ast ** init, ast ** end)
1317 /* the loop is considered countable if the following
1318 conditions are true :-
1320 a) initExpr :- <sym> = <const>
1321 b) condExpr :- <sym> < <const1>
1322 c) loopExpr :- <sym> ++
1325 /* first check the initExpr */
1326 if (IS_AST_OP (initExpr) &&
1327 initExpr->opval.op == '=' && /* is assignment */
1328 IS_AST_SYM_VALUE (initExpr->left))
1329 { /* left is a symbol */
1331 *sym = AST_SYMBOL (initExpr->left);
1332 *init = initExpr->right;
1337 /* for now the symbol has to be of
1339 if (!IS_INTEGRAL ((*sym)->type))
1342 /* now check condExpr */
1343 if (IS_AST_OP (condExpr))
1346 switch (condExpr->opval.op)
1349 if (IS_AST_SYM_VALUE (condExpr->left) &&
1350 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1351 IS_AST_LIT_VALUE (condExpr->right))
1353 *end = condExpr->right;
1359 if (IS_AST_OP (condExpr->left) &&
1360 condExpr->left->opval.op == '>' &&
1361 IS_AST_LIT_VALUE (condExpr->left->right) &&
1362 IS_AST_SYM_VALUE (condExpr->left->left) &&
1363 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1366 *end = newNode ('+', condExpr->left->right,
1367 newAst_VALUE (constVal ("1")));
1378 /* check loop expression is of the form <sym>++ */
1379 if (!IS_AST_OP (loopExpr))
1382 /* check if <sym> ++ */
1383 if (loopExpr->opval.op == INC_OP)
1389 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1390 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1397 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1398 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1406 if (loopExpr->opval.op == ADD_ASSIGN)
1409 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1410 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1411 IS_AST_LIT_VALUE (loopExpr->right) &&
1412 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1420 /*-----------------------------------------------------------------*/
1421 /* astHasVolatile - returns true if ast contains any volatile */
1422 /*-----------------------------------------------------------------*/
1424 astHasVolatile (ast * tree)
1429 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1432 if (IS_AST_OP (tree))
1433 return astHasVolatile (tree->left) ||
1434 astHasVolatile (tree->right);
1439 /*-----------------------------------------------------------------*/
1440 /* astHasPointer - return true if the ast contains any ptr variable */
1441 /*-----------------------------------------------------------------*/
1443 astHasPointer (ast * tree)
1448 if (IS_AST_LINK (tree))
1451 /* if we hit an array expression then check
1452 only the left side */
1453 if (IS_AST_OP (tree) && tree->opval.op == '[')
1454 return astHasPointer (tree->left);
1456 if (IS_AST_VALUE (tree))
1457 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1459 return astHasPointer (tree->left) ||
1460 astHasPointer (tree->right);
1464 /*-----------------------------------------------------------------*/
1465 /* astHasSymbol - return true if the ast has the given symbol */
1466 /*-----------------------------------------------------------------*/
1468 astHasSymbol (ast * tree, symbol * sym)
1470 if (!tree || IS_AST_LINK (tree))
1473 if (IS_AST_VALUE (tree))
1475 if (IS_AST_SYM_VALUE (tree))
1476 return isSymbolEqual (AST_SYMBOL (tree), sym);
1481 return astHasSymbol (tree->left, sym) ||
1482 astHasSymbol (tree->right, sym);
1485 /*-----------------------------------------------------------------*/
1486 /* astHasDeref - return true if the ast has an indirect access */
1487 /*-----------------------------------------------------------------*/
1489 astHasDeref (ast * tree)
1491 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1494 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1496 return astHasDeref (tree->left) || astHasDeref (tree->right);
1499 /*-----------------------------------------------------------------*/
1500 /* isConformingBody - the loop body has to conform to a set of rules */
1501 /* for the loop to be considered reversible read on for rules */
1502 /*-----------------------------------------------------------------*/
1504 isConformingBody (ast * pbody, symbol * sym, ast * body)
1507 /* we are going to do a pre-order traversal of the
1508 tree && check for the following conditions. (essentially
1509 a set of very shallow tests )
1510 a) the sym passed does not participate in
1511 any arithmetic operation
1512 b) There are no function calls
1513 c) all jumps are within the body
1514 d) address of loop control variable not taken
1515 e) if an assignment has a pointer on the
1516 left hand side make sure right does not have
1517 loop control variable */
1519 /* if we reach the end or a leaf then true */
1520 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1523 /* if anything else is "volatile" */
1524 if (IS_VOLATILE (TETYPE (pbody)))
1527 /* we will walk the body in a pre-order traversal for
1529 switch (pbody->opval.op)
1531 /*------------------------------------------------------------------*/
1533 // if the loopvar is used as an index
1534 if (astHasSymbol(pbody->right, sym)) {
1537 return isConformingBody (pbody->right, sym, body);
1539 /*------------------------------------------------------------------*/
1544 /*------------------------------------------------------------------*/
1545 case INC_OP: /* incerement operator unary so left only */
1548 /* sure we are not sym is not modified */
1550 IS_AST_SYM_VALUE (pbody->left) &&
1551 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1555 IS_AST_SYM_VALUE (pbody->right) &&
1556 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1561 /*------------------------------------------------------------------*/
1563 case '*': /* can be unary : if right is null then unary operation */
1568 /* if right is NULL then unary operation */
1569 /*------------------------------------------------------------------*/
1570 /*----------------------------*/
1572 /*----------------------------*/
1575 if (IS_AST_SYM_VALUE (pbody->left) &&
1576 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1579 return isConformingBody (pbody->left, sym, body);
1583 if (astHasSymbol (pbody->left, sym) ||
1584 astHasSymbol (pbody->right, sym))
1589 /*------------------------------------------------------------------*/
1597 if (IS_AST_SYM_VALUE (pbody->left) &&
1598 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1601 if (IS_AST_SYM_VALUE (pbody->right) &&
1602 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1605 return isConformingBody (pbody->left, sym, body) &&
1606 isConformingBody (pbody->right, sym, body);
1613 if (IS_AST_SYM_VALUE (pbody->left) &&
1614 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1616 return isConformingBody (pbody->left, sym, body);
1618 /*------------------------------------------------------------------*/
1630 case SIZEOF: /* evaluate wihout code generation */
1632 return isConformingBody (pbody->left, sym, body) &&
1633 isConformingBody (pbody->right, sym, body);
1635 /*------------------------------------------------------------------*/
1638 /* if left has a pointer & right has loop
1639 control variable then we cannot */
1640 if (astHasPointer (pbody->left) &&
1641 astHasSymbol (pbody->right, sym))
1643 if (astHasVolatile (pbody->left))
1646 if (IS_AST_SYM_VALUE (pbody->left)) {
1647 // if the loopvar has an assignment
1648 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1650 // if the loopvar is used in another (maybe conditional) block
1651 if (astHasSymbol (pbody->right, sym) &&
1652 (pbody->level > body->level)) {
1657 if (astHasVolatile (pbody->left))
1660 if (astHasDeref(pbody->right)) return FALSE;
1662 return isConformingBody (pbody->left, sym, body) &&
1663 isConformingBody (pbody->right, sym, body);
1674 assert ("Parser should not have generated this\n");
1676 /*------------------------------------------------------------------*/
1677 /*----------------------------*/
1678 /* comma operator */
1679 /*----------------------------*/
1681 return isConformingBody (pbody->left, sym, body) &&
1682 isConformingBody (pbody->right, sym, body);
1684 /*------------------------------------------------------------------*/
1685 /*----------------------------*/
1687 /*----------------------------*/
1689 /* if local & not passed as paramater then ok */
1690 if (sym->level && !astHasSymbol(pbody->right,sym))
1694 /*------------------------------------------------------------------*/
1695 /*----------------------------*/
1696 /* return statement */
1697 /*----------------------------*/
1702 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1707 if (astHasSymbol (pbody->left, sym))
1714 return isConformingBody (pbody->left, sym, body) &&
1715 isConformingBody (pbody->right, sym, body);
1721 /*-----------------------------------------------------------------*/
1722 /* isLoopReversible - takes a for loop as input && returns true */
1723 /* if the for loop is reversible. If yes will set the value of */
1724 /* the loop control var & init value & termination value */
1725 /*-----------------------------------------------------------------*/
1727 isLoopReversible (ast * loop, symbol ** loopCntrl,
1728 ast ** init, ast ** end)
1730 /* if option says don't do it then don't */
1731 if (optimize.noLoopReverse)
1733 /* there are several tests to determine this */
1735 /* for loop has to be of the form
1736 for ( <sym> = <const1> ;
1737 [<sym> < <const2>] ;
1738 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1740 if (!isLoopCountable (AST_FOR (loop, initExpr),
1741 AST_FOR (loop, condExpr),
1742 AST_FOR (loop, loopExpr),
1743 loopCntrl, init, end))
1746 /* now do some serious checking on the body of the loop
1749 return isConformingBody (loop->left, *loopCntrl, loop->left);
1753 /*-----------------------------------------------------------------*/
1754 /* replLoopSym - replace the loop sym by loop sym -1 */
1755 /*-----------------------------------------------------------------*/
1757 replLoopSym (ast * body, symbol * sym)
1760 if (!body || IS_AST_LINK (body))
1763 if (IS_AST_SYM_VALUE (body))
1766 if (isSymbolEqual (AST_SYMBOL (body), sym))
1770 body->opval.op = '-';
1771 body->left = newAst_VALUE (symbolVal (sym));
1772 body->right = newAst_VALUE (constVal ("1"));
1780 replLoopSym (body->left, sym);
1781 replLoopSym (body->right, sym);
1785 /*-----------------------------------------------------------------*/
1786 /* reverseLoop - do the actual loop reversal */
1787 /*-----------------------------------------------------------------*/
1789 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1793 /* create the following tree
1798 if (sym) goto for_continue ;
1801 /* put it together piece by piece */
1802 rloop = newNode (NULLOP,
1803 createIf (newAst_VALUE (symbolVal (sym)),
1805 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1808 newAst_VALUE (symbolVal (sym)),
1811 replLoopSym (loop->left, sym);
1812 setAstLineno (rloop, init->lineno);
1814 rloop = newNode (NULLOP,
1816 newAst_VALUE (symbolVal (sym)),
1817 newNode ('-', end, init)),
1818 createLabel (AST_FOR (loop, continueLabel),
1822 newNode (SUB_ASSIGN,
1823 newAst_VALUE (symbolVal (sym)),
1824 newAst_VALUE (constVal ("1"))),
1827 rloop->lineno=init->lineno;
1828 return decorateType (rloop);
1832 /*-----------------------------------------------------------------*/
1833 /* decorateType - compute type for this tree also does type cheking */
1834 /* this is done bottom up, since type have to flow upwards */
1835 /* it also does constant folding, and paramater checking */
1836 /*-----------------------------------------------------------------*/
1838 decorateType (ast * tree)
1846 /* if already has type then do nothing */
1847 if (tree->decorated)
1850 tree->decorated = 1;
1852 /* print the line */
1853 /* if not block & function */
1854 if (tree->type == EX_OP &&
1855 (tree->opval.op != FUNCTION &&
1856 tree->opval.op != BLOCK &&
1857 tree->opval.op != NULLOP))
1859 filename = tree->filename;
1860 lineno = tree->lineno;
1863 /* if any child is an error | this one is an error do nothing */
1864 if (tree->isError ||
1865 (tree->left && tree->left->isError) ||
1866 (tree->right && tree->right->isError))
1869 /*------------------------------------------------------------------*/
1870 /*----------------------------*/
1871 /* leaf has been reached */
1872 /*----------------------------*/
1873 /* if this is of type value */
1874 /* just get the type */
1875 if (tree->type == EX_VALUE)
1878 if (IS_LITERAL (tree->opval.val->etype))
1881 /* if this is a character array then declare it */
1882 if (IS_ARRAY (tree->opval.val->type))
1883 tree->opval.val = stringToSymbol (tree->opval.val);
1885 /* otherwise just copy the type information */
1886 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1890 if (tree->opval.val->sym)
1892 /* if the undefined flag is set then give error message */
1893 if (tree->opval.val->sym->undefined)
1895 werror (E_ID_UNDEF, tree->opval.val->sym->name);
1897 TTYPE (tree) = TETYPE (tree) =
1898 tree->opval.val->type = tree->opval.val->sym->type =
1899 tree->opval.val->etype = tree->opval.val->sym->etype =
1900 copyLinkChain (INTTYPE);
1905 /* if impilicit i.e. struct/union member then no type */
1906 if (tree->opval.val->sym->implicit)
1907 TTYPE (tree) = TETYPE (tree) = NULL;
1912 /* else copy the type */
1913 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
1915 /* and mark it as referenced */
1916 tree->opval.val->sym->isref = 1;
1924 /* if type link for the case of cast */
1925 if (tree->type == EX_LINK)
1927 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
1934 dtl = decorateType (tree->left);
1935 /* delay right side for '?' operator since conditional macro expansions might
1937 dtr = (tree->opval.op == '?' ? tree->right : decorateType (tree->right));
1939 /* this is to take care of situations
1940 when the tree gets rewritten */
1941 if (dtl != tree->left)
1943 if (dtr != tree->right)
1947 /* depending on type of operator do */
1949 switch (tree->opval.op)
1951 /*------------------------------------------------------------------*/
1952 /*----------------------------*/
1954 /*----------------------------*/
1957 /* determine which is the array & which the index */
1958 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) && IS_INTEGRAL (LTYPE (tree)))
1961 ast *tempTree = tree->left;
1962 tree->left = tree->right;
1963 tree->right = tempTree;
1966 /* first check if this is a array or a pointer */
1967 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
1969 werror (E_NEED_ARRAY_PTR, "[]");
1970 goto errorTreeReturn;
1973 /* check if the type of the idx */
1974 if (!IS_INTEGRAL (RTYPE (tree)))
1976 werror (E_IDX_NOT_INT);
1977 goto errorTreeReturn;
1980 /* if the left is an rvalue then error */
1983 werror (E_LVALUE_REQUIRED, "array access");
1984 goto errorTreeReturn;
1987 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
1988 if (IS_PTR(LTYPE(tree))) {
1989 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
1993 /*------------------------------------------------------------------*/
1994 /*----------------------------*/
1996 /*----------------------------*/
1998 /* if this is not a structure */
1999 if (!IS_STRUCT (LTYPE (tree)))
2001 werror (E_STRUCT_UNION, ".");
2002 goto errorTreeReturn;
2004 TTYPE (tree) = structElemType (LTYPE (tree),
2005 (tree->right->type == EX_VALUE ?
2006 tree->right->opval.val : NULL));
2007 TETYPE (tree) = getSpec (TTYPE (tree));
2010 /*------------------------------------------------------------------*/
2011 /*----------------------------*/
2012 /* struct/union pointer */
2013 /*----------------------------*/
2015 /* if not pointer to a structure */
2016 if (!IS_PTR (LTYPE (tree)))
2018 werror (E_PTR_REQD);
2019 goto errorTreeReturn;
2022 if (!IS_STRUCT (LTYPE (tree)->next))
2024 werror (E_STRUCT_UNION, "->");
2025 goto errorTreeReturn;
2028 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2029 (tree->right->type == EX_VALUE ?
2030 tree->right->opval.val : NULL));
2031 TETYPE (tree) = getSpec (TTYPE (tree));
2033 /* adjust the storage class */
2034 switch (DCL_TYPE(tree->left->ftype)) {
2038 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2041 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2046 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2049 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2052 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2061 /*------------------------------------------------------------------*/
2062 /*----------------------------*/
2063 /* ++/-- operation */
2064 /*----------------------------*/
2065 case INC_OP: /* incerement operator unary so left only */
2068 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2069 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2070 if (!tree->initMode && IS_CONSTANT(TETYPE(tree)))
2071 werror (E_CODE_WRITE, "++/--");
2080 /*------------------------------------------------------------------*/
2081 /*----------------------------*/
2083 /*----------------------------*/
2084 case '&': /* can be unary */
2085 /* if right is NULL then unary operation */
2086 if (tree->right) /* not an unary operation */
2089 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2091 werror (E_BITWISE_OP);
2092 werror (W_CONTINUE, "left & right types are ");
2093 printTypeChain (LTYPE (tree), stderr);
2094 fprintf (stderr, ",");
2095 printTypeChain (RTYPE (tree), stderr);
2096 fprintf (stderr, "\n");
2097 goto errorTreeReturn;
2100 /* if they are both literal */
2101 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2103 tree->type = EX_VALUE;
2104 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2105 valFromType (RETYPE (tree)), '&');
2107 tree->right = tree->left = NULL;
2108 TETYPE (tree) = tree->opval.val->etype;
2109 TTYPE (tree) = tree->opval.val->type;
2113 /* see if this is a GETHBIT operation if yes
2116 ast *otree = optimizeGetHbit (tree);
2119 return decorateType (otree);
2123 computeType (LTYPE (tree), RTYPE (tree));
2124 TETYPE (tree) = getSpec (TTYPE (tree));
2126 LRVAL (tree) = RRVAL (tree) = 1;
2130 /*------------------------------------------------------------------*/
2131 /*----------------------------*/
2133 /*----------------------------*/
2135 p->class = DECLARATOR;
2136 /* if bit field then error */
2137 if (IS_BITVAR (tree->left->etype))
2139 werror (E_ILLEGAL_ADDR, "address of bit variable");
2140 goto errorTreeReturn;
2143 if (SPEC_SCLS (tree->left->etype) == S_REGISTER)
2145 werror (E_ILLEGAL_ADDR, "address of register variable");
2146 goto errorTreeReturn;
2149 if (IS_FUNC (LTYPE (tree)))
2151 werror (E_ILLEGAL_ADDR, "address of function");
2152 goto errorTreeReturn;
2155 if (IS_LITERAL(LTYPE(tree)))
2157 werror (E_ILLEGAL_ADDR, "address of literal");
2158 goto errorTreeReturn;
2163 werror (E_LVALUE_REQUIRED, "address of");
2164 goto errorTreeReturn;
2166 if (SPEC_SCLS (tree->left->etype) == S_CODE)
2168 DCL_TYPE (p) = CPOINTER;
2169 DCL_PTR_CONST (p) = port->mem.code_ro;
2171 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2172 DCL_TYPE (p) = FPOINTER;
2173 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2174 DCL_TYPE (p) = PPOINTER;
2175 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2176 DCL_TYPE (p) = IPOINTER;
2177 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2178 DCL_TYPE (p) = EEPPOINTER;
2179 else if (SPEC_OCLS(tree->left->etype))
2180 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2182 DCL_TYPE (p) = POINTER;
2184 if (IS_AST_SYM_VALUE (tree->left))
2186 AST_SYMBOL (tree->left)->addrtaken = 1;
2187 AST_SYMBOL (tree->left)->allocreq = 1;
2190 p->next = LTYPE (tree);
2192 TETYPE (tree) = getSpec (TTYPE (tree));
2193 DCL_PTR_CONST (p) = SPEC_CONST (TETYPE (tree));
2194 DCL_PTR_VOLATILE (p) = SPEC_VOLATILE (TETYPE (tree));
2199 /*------------------------------------------------------------------*/
2200 /*----------------------------*/
2202 /*----------------------------*/
2204 /* if the rewrite succeeds then don't go any furthur */
2206 ast *wtree = optimizeRRCRLC (tree);
2208 return decorateType (wtree);
2210 /*------------------------------------------------------------------*/
2211 /*----------------------------*/
2213 /*----------------------------*/
2215 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2217 werror (E_BITWISE_OP);
2218 werror (W_CONTINUE, "left & right types are ");
2219 printTypeChain (LTYPE (tree), stderr);
2220 fprintf (stderr, ",");
2221 printTypeChain (RTYPE (tree), stderr);
2222 fprintf (stderr, "\n");
2223 goto errorTreeReturn;
2226 /* if they are both literal then */
2227 /* rewrite the tree */
2228 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2230 tree->type = EX_VALUE;
2231 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2232 valFromType (RETYPE (tree)),
2234 tree->right = tree->left = NULL;
2235 TETYPE (tree) = tree->opval.val->etype;
2236 TTYPE (tree) = tree->opval.val->type;
2239 LRVAL (tree) = RRVAL (tree) = 1;
2240 TETYPE (tree) = getSpec (TTYPE (tree) =
2241 computeType (LTYPE (tree),
2244 /*------------------------------------------------------------------*/
2245 /*----------------------------*/
2247 /*----------------------------*/
2249 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2251 werror (E_INVALID_OP, "divide");
2252 goto errorTreeReturn;
2254 /* if they are both literal then */
2255 /* rewrite the tree */
2256 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2258 tree->type = EX_VALUE;
2259 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2260 valFromType (RETYPE (tree)));
2261 tree->right = tree->left = NULL;
2262 TETYPE (tree) = getSpec (TTYPE (tree) =
2263 tree->opval.val->type);
2266 LRVAL (tree) = RRVAL (tree) = 1;
2267 TETYPE (tree) = getSpec (TTYPE (tree) =
2268 computeType (LTYPE (tree),
2272 /*------------------------------------------------------------------*/
2273 /*----------------------------*/
2275 /*----------------------------*/
2277 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2279 werror (E_BITWISE_OP);
2280 werror (W_CONTINUE, "left & right types are ");
2281 printTypeChain (LTYPE (tree), stderr);
2282 fprintf (stderr, ",");
2283 printTypeChain (RTYPE (tree), stderr);
2284 fprintf (stderr, "\n");
2285 goto errorTreeReturn;
2287 /* if they are both literal then */
2288 /* rewrite the tree */
2289 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2291 tree->type = EX_VALUE;
2292 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2293 valFromType (RETYPE (tree)));
2294 tree->right = tree->left = NULL;
2295 TETYPE (tree) = getSpec (TTYPE (tree) =
2296 tree->opval.val->type);
2299 LRVAL (tree) = RRVAL (tree) = 1;
2300 TETYPE (tree) = getSpec (TTYPE (tree) =
2301 computeType (LTYPE (tree),
2305 /*------------------------------------------------------------------*/
2306 /*----------------------------*/
2307 /* address dereference */
2308 /*----------------------------*/
2309 case '*': /* can be unary : if right is null then unary operation */
2312 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2314 werror (E_PTR_REQD);
2315 goto errorTreeReturn;
2320 werror (E_LVALUE_REQUIRED, "pointer deref");
2321 goto errorTreeReturn;
2323 TTYPE (tree) = copyLinkChain ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) ?
2324 LTYPE (tree)->next : NULL);
2325 TETYPE (tree) = getSpec (TTYPE (tree));
2326 SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
2330 /*------------------------------------------------------------------*/
2331 /*----------------------------*/
2332 /* multiplication */
2333 /*----------------------------*/
2334 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2336 werror (E_INVALID_OP, "multiplication");
2337 goto errorTreeReturn;
2340 /* if they are both literal then */
2341 /* rewrite the tree */
2342 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2344 tree->type = EX_VALUE;
2345 tree->opval.val = valMult (valFromType (LETYPE (tree)),
2346 valFromType (RETYPE (tree)));
2347 tree->right = tree->left = NULL;
2348 TETYPE (tree) = getSpec (TTYPE (tree) =
2349 tree->opval.val->type);
2353 /* if left is a literal exchange left & right */
2354 if (IS_LITERAL (LTYPE (tree)))
2356 ast *tTree = tree->left;
2357 tree->left = tree->right;
2358 tree->right = tTree;
2361 LRVAL (tree) = RRVAL (tree) = 1;
2362 /* promote result to int if left & right are char
2363 this will facilitate hardware multiplies 8bit x 8bit = 16bit */
2364 if (IS_CHAR(LETYPE(tree)) && IS_CHAR(RETYPE(tree))) {
2365 TETYPE (tree) = getSpec (TTYPE (tree) =
2366 computeType (LTYPE (tree),
2368 SPEC_NOUN(TETYPE(tree)) = V_INT;
2370 TETYPE (tree) = getSpec (TTYPE (tree) =
2371 computeType (LTYPE (tree),
2376 /*------------------------------------------------------------------*/
2377 /*----------------------------*/
2378 /* unary '+' operator */
2379 /*----------------------------*/
2384 if (!IS_INTEGRAL (LTYPE (tree)))
2386 werror (E_UNARY_OP, '+');
2387 goto errorTreeReturn;
2390 /* if left is a literal then do it */
2391 if (IS_LITERAL (LTYPE (tree)))
2393 tree->type = EX_VALUE;
2394 tree->opval.val = valFromType (LETYPE (tree));
2396 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2400 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2404 /*------------------------------------------------------------------*/
2405 /*----------------------------*/
2407 /*----------------------------*/
2409 /* this is not a unary operation */
2410 /* if both pointers then problem */
2411 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2412 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
2414 werror (E_PTR_PLUS_PTR);
2415 goto errorTreeReturn;
2418 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2419 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2421 werror (E_PLUS_INVALID, "+");
2422 goto errorTreeReturn;
2425 if (!IS_ARITHMETIC (RTYPE (tree)) &&
2426 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
2428 werror (E_PLUS_INVALID, "+");
2429 goto errorTreeReturn;
2431 /* if they are both literal then */
2432 /* rewrite the tree */
2433 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2435 tree->type = EX_VALUE;
2436 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
2437 valFromType (RETYPE (tree)));
2438 tree->right = tree->left = NULL;
2439 TETYPE (tree) = getSpec (TTYPE (tree) =
2440 tree->opval.val->type);
2444 /* if the right is a pointer or left is a literal
2445 xchange left & right */
2446 if (IS_ARRAY (RTYPE (tree)) ||
2447 IS_PTR (RTYPE (tree)) ||
2448 IS_LITERAL (LTYPE (tree)))
2450 ast *tTree = tree->left;
2451 tree->left = tree->right;
2452 tree->right = tTree;
2455 LRVAL (tree) = RRVAL (tree) = 1;
2456 /* if the left is a pointer */
2457 if (IS_PTR (LTYPE (tree)))
2458 TETYPE (tree) = getSpec (TTYPE (tree) =
2461 TETYPE (tree) = getSpec (TTYPE (tree) =
2462 computeType (LTYPE (tree),
2466 /*------------------------------------------------------------------*/
2467 /*----------------------------*/
2469 /*----------------------------*/
2470 case '-': /* can be unary */
2471 /* if right is null then unary */
2475 if (!IS_ARITHMETIC (LTYPE (tree)))
2477 werror (E_UNARY_OP, tree->opval.op);
2478 goto errorTreeReturn;
2481 /* if left is a literal then do it */
2482 if (IS_LITERAL (LTYPE (tree)))
2484 tree->type = EX_VALUE;
2485 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
2487 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2488 SPEC_USIGN(TETYPE(tree)) = 0;
2492 TTYPE (tree) = LTYPE (tree);
2496 /*------------------------------------------------------------------*/
2497 /*----------------------------*/
2499 /*----------------------------*/
2501 if (!(IS_PTR (LTYPE (tree)) ||
2502 IS_ARRAY (LTYPE (tree)) ||
2503 IS_ARITHMETIC (LTYPE (tree))))
2505 werror (E_PLUS_INVALID, "-");
2506 goto errorTreeReturn;
2509 if (!(IS_PTR (RTYPE (tree)) ||
2510 IS_ARRAY (RTYPE (tree)) ||
2511 IS_ARITHMETIC (RTYPE (tree))))
2513 werror (E_PLUS_INVALID, "-");
2514 goto errorTreeReturn;
2517 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
2518 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
2519 IS_INTEGRAL (RTYPE (tree))))
2521 werror (E_PLUS_INVALID, "-");
2522 goto errorTreeReturn;
2525 /* if they are both literal then */
2526 /* rewrite the tree */
2527 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2529 tree->type = EX_VALUE;
2530 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
2531 valFromType (RETYPE (tree)));
2532 tree->right = tree->left = NULL;
2533 TETYPE (tree) = getSpec (TTYPE (tree) =
2534 tree->opval.val->type);
2538 /* if the left & right are equal then zero */
2539 if (isAstEqual (tree->left, tree->right))
2541 tree->type = EX_VALUE;
2542 tree->left = tree->right = NULL;
2543 tree->opval.val = constVal ("0");
2544 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2548 /* if both of them are pointers or arrays then */
2549 /* the result is going to be an integer */
2550 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
2551 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
2552 TETYPE (tree) = TTYPE (tree) = newIntLink ();
2554 /* if only the left is a pointer */
2555 /* then result is a pointer */
2556 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
2557 TETYPE (tree) = getSpec (TTYPE (tree) =
2560 TETYPE (tree) = getSpec (TTYPE (tree) =
2561 computeType (LTYPE (tree),
2563 LRVAL (tree) = RRVAL (tree) = 1;
2566 /*------------------------------------------------------------------*/
2567 /*----------------------------*/
2569 /*----------------------------*/
2571 /* can be only integral type */
2572 if (!IS_INTEGRAL (LTYPE (tree)))
2574 werror (E_UNARY_OP, tree->opval.op);
2575 goto errorTreeReturn;
2578 /* if left is a literal then do it */
2579 if (IS_LITERAL (LTYPE (tree)))
2581 tree->type = EX_VALUE;
2582 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
2584 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2588 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2591 /*------------------------------------------------------------------*/
2592 /*----------------------------*/
2594 /*----------------------------*/
2596 /* can be pointer */
2597 if (!IS_ARITHMETIC (LTYPE (tree)) &&
2598 !IS_PTR (LTYPE (tree)) &&
2599 !IS_ARRAY (LTYPE (tree)))
2601 werror (E_UNARY_OP, tree->opval.op);
2602 goto errorTreeReturn;
2605 /* if left is a literal then do it */
2606 if (IS_LITERAL (LTYPE (tree)))
2608 tree->type = EX_VALUE;
2609 tree->opval.val = valNot (valFromType (LETYPE (tree)));
2611 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2615 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2618 /*------------------------------------------------------------------*/
2619 /*----------------------------*/
2621 /*----------------------------*/
2624 TTYPE (tree) = LTYPE (tree);
2625 TETYPE (tree) = LETYPE (tree);
2629 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2634 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
2636 werror (E_SHIFT_OP_INVALID);
2637 werror (W_CONTINUE, "left & right types are ");
2638 printTypeChain (LTYPE (tree), stderr);
2639 fprintf (stderr, ",");
2640 printTypeChain (RTYPE (tree), stderr);
2641 fprintf (stderr, "\n");
2642 goto errorTreeReturn;
2645 /* if they are both literal then */
2646 /* rewrite the tree */
2647 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2649 tree->type = EX_VALUE;
2650 tree->opval.val = valShift (valFromType (LETYPE (tree)),
2651 valFromType (RETYPE (tree)),
2652 (tree->opval.op == LEFT_OP ? 1 : 0));
2653 tree->right = tree->left = NULL;
2654 TETYPE (tree) = getSpec (TTYPE (tree) =
2655 tree->opval.val->type);
2658 /* a left shift must be done with at least 16bits */
2659 if ((tree->opval.op==LEFT_OP) && (getSize(LTYPE(tree))<2)) {
2662 decorateType (newNode (CAST,
2663 newAst_LINK(copyLinkChain(LTYPE(tree))),
2665 SPEC_NOUN(tree->left->left->ftype)=V_INT;
2667 /* if only the right side is a literal & we are
2668 shifting more than size of the left operand then zero */
2669 if (IS_LITERAL (RTYPE (tree)) &&
2670 ((unsigned) floatFromVal (valFromType (RETYPE (tree)))) >=
2671 (getSize (LTYPE (tree)) * 8))
2673 werror (W_SHIFT_CHANGED,
2674 (tree->opval.op == LEFT_OP ? "left" : "right"));
2675 tree->type = EX_VALUE;
2676 tree->left = tree->right = NULL;
2677 tree->opval.val = constVal ("0");
2678 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
2681 LRVAL (tree) = RRVAL (tree) = 1;
2682 if (IS_LITERAL (LTYPE (tree)) && !IS_LITERAL (RTYPE (tree)))
2684 COPYTYPE (TTYPE (tree), TETYPE (tree), RTYPE (tree));
2688 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
2692 /*------------------------------------------------------------------*/
2693 /*----------------------------*/
2695 /*----------------------------*/
2696 case CAST: /* change the type */
2697 /* cannot cast to an aggregate type */
2698 if (IS_AGGREGATE (LTYPE (tree)))
2700 werror (E_CAST_ILLEGAL);
2701 goto errorTreeReturn;
2704 /* make sure the type is complete and sane */
2705 checkTypeSanity(LETYPE(tree), "(cast)");
2708 /* if the right is a literal replace the tree */
2709 if (IS_LITERAL (RETYPE (tree))) {
2710 if (!IS_PTR (LTYPE (tree))) {
2711 tree->type = EX_VALUE;
2713 valCastLiteral (LTYPE (tree),
2714 floatFromVal (valFromType (RETYPE (tree))));
2717 TTYPE (tree) = tree->opval.val->type;
2718 tree->values.literalFromCast = 1;
2719 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
2720 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
2721 sym_link *rest = LTYPE(tree)->next;
2722 werror(W_LITERAL_GENERIC);
2723 TTYPE(tree) = newLink();
2724 DCL_TYPE(TTYPE(tree)) = FPOINTER;
2725 TTYPE(tree)->next = rest;
2726 tree->left->opval.lnk = TTYPE(tree);
2729 TTYPE (tree) = LTYPE (tree);
2733 TTYPE (tree) = LTYPE (tree);
2737 /* if pointer to struct then check names */
2738 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
2739 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
2740 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag)) {
2741 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,SPEC_STRUCT(LETYPE(tree))->tag);
2743 /* if the right is a literal replace the tree */
2744 if (IS_LITERAL (RETYPE (tree)) && !IS_PTR (LTYPE (tree))) {
2745 tree->type = EX_VALUE;
2747 valCastLiteral (LTYPE (tree),
2748 floatFromVal (valFromType (RETYPE (tree))));
2751 TTYPE (tree) = tree->opval.val->type;
2752 tree->values.literalFromCast = 1;
2754 TTYPE (tree) = LTYPE (tree);
2758 TETYPE (tree) = getSpec (TTYPE (tree));
2762 /*------------------------------------------------------------------*/
2763 /*----------------------------*/
2764 /* logical &&, || */
2765 /*----------------------------*/
2768 /* each must me arithmetic type or be a pointer */
2769 if (!IS_PTR (LTYPE (tree)) &&
2770 !IS_ARRAY (LTYPE (tree)) &&
2771 !IS_INTEGRAL (LTYPE (tree)))
2773 werror (E_COMPARE_OP);
2774 goto errorTreeReturn;
2777 if (!IS_PTR (RTYPE (tree)) &&
2778 !IS_ARRAY (RTYPE (tree)) &&
2779 !IS_INTEGRAL (RTYPE (tree)))
2781 werror (E_COMPARE_OP);
2782 goto errorTreeReturn;
2784 /* if they are both literal then */
2785 /* rewrite the tree */
2786 if (IS_LITERAL (RTYPE (tree)) &&
2787 IS_LITERAL (LTYPE (tree)))
2789 tree->type = EX_VALUE;
2790 tree->opval.val = valLogicAndOr (valFromType (LETYPE (tree)),
2791 valFromType (RETYPE (tree)),
2793 tree->right = tree->left = NULL;
2794 TETYPE (tree) = getSpec (TTYPE (tree) =
2795 tree->opval.val->type);
2798 LRVAL (tree) = RRVAL (tree) = 1;
2799 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2802 /*------------------------------------------------------------------*/
2803 /*----------------------------*/
2804 /* comparison operators */
2805 /*----------------------------*/
2813 ast *lt = optimizeCompare (tree);
2819 /* if they are pointers they must be castable */
2820 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
2822 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2824 werror (E_COMPARE_OP);
2825 fprintf (stderr, "comparing type ");
2826 printTypeChain (LTYPE (tree), stderr);
2827 fprintf (stderr, "to type ");
2828 printTypeChain (RTYPE (tree), stderr);
2829 fprintf (stderr, "\n");
2830 goto errorTreeReturn;
2833 /* else they should be promotable to one another */
2836 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
2837 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
2839 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2841 werror (E_COMPARE_OP);
2842 fprintf (stderr, "comparing type ");
2843 printTypeChain (LTYPE (tree), stderr);
2844 fprintf (stderr, "to type ");
2845 printTypeChain (RTYPE (tree), stderr);
2846 fprintf (stderr, "\n");
2847 goto errorTreeReturn;
2850 /* if unsigned value < 0 then always false */
2851 /* if (unsigned value) > 0 then (unsigned value) */
2852 if (SPEC_USIGN(LETYPE(tree)) && IS_LITERAL(RTYPE(tree)) &&
2853 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0) {
2855 if (tree->opval.op == '<') {
2858 if (tree->opval.op == '>') {
2862 /* if they are both literal then */
2863 /* rewrite the tree */
2864 if (IS_LITERAL (RTYPE (tree)) &&
2865 IS_LITERAL (LTYPE (tree)))
2867 tree->type = EX_VALUE;
2868 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
2869 valFromType (RETYPE (tree)),
2871 tree->right = tree->left = NULL;
2872 TETYPE (tree) = getSpec (TTYPE (tree) =
2873 tree->opval.val->type);
2876 LRVAL (tree) = RRVAL (tree) = 1;
2877 TTYPE (tree) = TETYPE (tree) = newCharLink ();
2880 /*------------------------------------------------------------------*/
2881 /*----------------------------*/
2883 /*----------------------------*/
2884 case SIZEOF: /* evaluate wihout code generation */
2885 /* change the type to a integer */
2886 tree->type = EX_VALUE;
2887 sprintf (buffer, "%d", (getSize (tree->right->ftype)));
2888 tree->opval.val = constVal (buffer);
2889 tree->right = tree->left = NULL;
2890 TETYPE (tree) = getSpec (TTYPE (tree) =
2891 tree->opval.val->type);
2894 /*------------------------------------------------------------------*/
2895 /*----------------------------*/
2897 /*----------------------------*/
2899 /* return typeof enum value */
2900 tree->type = EX_VALUE;
2903 if (IS_SPEC(tree->right->ftype)) {
2904 switch (SPEC_NOUN(tree->right->ftype)) {
2906 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
2907 else typeofv = TYPEOF_INT;
2910 typeofv = TYPEOF_FLOAT;
2913 typeofv = TYPEOF_CHAR;
2916 typeofv = TYPEOF_VOID;
2919 typeofv = TYPEOF_STRUCT;
2922 typeofv = TYPEOF_BIT;
2925 typeofv = TYPEOF_SBIT;
2931 switch (DCL_TYPE(tree->right->ftype)) {
2933 typeofv = TYPEOF_POINTER;
2936 typeofv = TYPEOF_FPOINTER;
2939 typeofv = TYPEOF_CPOINTER;
2942 typeofv = TYPEOF_GPOINTER;
2945 typeofv = TYPEOF_PPOINTER;
2948 typeofv = TYPEOF_IPOINTER;
2951 typeofv = TYPEOF_ARRAY;
2954 typeofv = TYPEOF_FUNCTION;
2960 sprintf (buffer, "%d", typeofv);
2961 tree->opval.val = constVal (buffer);
2962 tree->right = tree->left = NULL;
2963 TETYPE (tree) = getSpec (TTYPE (tree) =
2964 tree->opval.val->type);
2967 /*------------------------------------------------------------------*/
2968 /*----------------------------*/
2969 /* conditional operator '?' */
2970 /*----------------------------*/
2972 /* the type is value of the colon operator (on the right) */
2973 assert(IS_COLON_OP(tree->right));
2974 /* if already known then replace the tree : optimizer will do it
2975 but faster to do it here */
2976 if (IS_LITERAL (LTYPE(tree))) {
2977 if ( ((int) floatFromVal (valFromType (LETYPE (tree)))) != 0) {
2978 return decorateType(tree->right->left) ;
2980 return decorateType(tree->right->right) ;
2983 tree->right = decorateType(tree->right);
2984 TTYPE (tree) = RTYPE(tree);
2985 TETYPE (tree) = getSpec (TTYPE (tree));
2990 /* if they don't match we have a problem */
2991 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
2993 werror (E_TYPE_MISMATCH, "conditional operator", " ");
2994 goto errorTreeReturn;
2997 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree));
2998 TETYPE (tree) = getSpec (TTYPE (tree));
3002 #if 0 // assignment operators are converted by the parser
3003 /*------------------------------------------------------------------*/
3004 /*----------------------------*/
3005 /* assignment operators */
3006 /*----------------------------*/
3009 /* for these it must be both must be integral */
3010 if (!IS_ARITHMETIC (LTYPE (tree)) ||
3011 !IS_ARITHMETIC (RTYPE (tree)))
3013 werror (E_OPS_INTEGRAL);
3014 goto errorTreeReturn;
3017 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3019 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3020 werror (E_CODE_WRITE, " ");
3024 werror (E_LVALUE_REQUIRED, "*= or /=");
3025 goto errorTreeReturn;
3036 /* for these it must be both must be integral */
3037 if (!IS_INTEGRAL (LTYPE (tree)) ||
3038 !IS_INTEGRAL (RTYPE (tree)))
3040 werror (E_OPS_INTEGRAL);
3041 goto errorTreeReturn;
3044 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3046 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3047 werror (E_CODE_WRITE, " ");
3051 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
3052 goto errorTreeReturn;
3058 /*------------------------------------------------------------------*/
3059 /*----------------------------*/
3061 /*----------------------------*/
3063 if (!(IS_PTR (LTYPE (tree)) ||
3064 IS_ARITHMETIC (LTYPE (tree))))
3066 werror (E_PLUS_INVALID, "-=");
3067 goto errorTreeReturn;
3070 if (!(IS_PTR (RTYPE (tree)) ||
3071 IS_ARITHMETIC (RTYPE (tree))))
3073 werror (E_PLUS_INVALID, "-=");
3074 goto errorTreeReturn;
3077 TETYPE (tree) = getSpec (TTYPE (tree) =
3078 computeType (LTYPE (tree),
3081 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3082 werror (E_CODE_WRITE, " ");
3086 werror (E_LVALUE_REQUIRED, "-=");
3087 goto errorTreeReturn;
3093 /*------------------------------------------------------------------*/
3094 /*----------------------------*/
3096 /*----------------------------*/
3098 /* this is not a unary operation */
3099 /* if both pointers then problem */
3100 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3102 werror (E_PTR_PLUS_PTR);
3103 goto errorTreeReturn;
3106 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
3108 werror (E_PLUS_INVALID, "+=");
3109 goto errorTreeReturn;
3112 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
3114 werror (E_PLUS_INVALID, "+=");
3115 goto errorTreeReturn;
3118 TETYPE (tree) = getSpec (TTYPE (tree) =
3119 computeType (LTYPE (tree),
3122 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3123 werror (E_CODE_WRITE, " ");
3127 werror (E_LVALUE_REQUIRED, "+=");
3128 goto errorTreeReturn;
3131 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right));
3132 tree->opval.op = '=';
3137 /*------------------------------------------------------------------*/
3138 /*----------------------------*/
3139 /* straight assignemnt */
3140 /*----------------------------*/
3142 /* cannot be an aggregate */
3143 if (IS_AGGREGATE (LTYPE (tree)))
3145 werror (E_AGGR_ASSIGN);
3146 goto errorTreeReturn;
3149 /* they should either match or be castable */
3150 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3152 werror (E_TYPE_MISMATCH, "assignment", " ");
3153 printFromToType(RTYPE(tree),LTYPE(tree));
3154 //goto errorTreeReturn;
3157 /* if the left side of the tree is of type void
3158 then report error */
3159 if (IS_VOID (LTYPE (tree)))
3161 werror (E_CAST_ZERO);
3162 printFromToType(RTYPE(tree), LTYPE(tree));
3165 TETYPE (tree) = getSpec (TTYPE (tree) =
3169 if (!tree->initMode ) {
3170 if ((IS_SPEC(LETYPE(tree)) && IS_CONSTANT (LETYPE (tree))))
3171 werror (E_CODE_WRITE, " ");
3175 werror (E_LVALUE_REQUIRED, "=");
3176 goto errorTreeReturn;
3181 /*------------------------------------------------------------------*/
3182 /*----------------------------*/
3183 /* comma operator */
3184 /*----------------------------*/
3186 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
3189 /*------------------------------------------------------------------*/
3190 /*----------------------------*/
3192 /*----------------------------*/
3196 if (processParms (tree->left,
3197 FUNC_ARGS(tree->left->ftype),
3198 tree->right, &parmNumber, TRUE)) {
3199 goto errorTreeReturn;
3202 if ((options.stackAuto || IFFUNC_ISREENT (LTYPE (tree))) &&
3203 !IFFUNC_ISBUILTIN(LTYPE(tree)))
3205 //FUNC_ARGS(tree->left->ftype) =
3206 //reverseVal (FUNC_ARGS(tree->left->ftype));
3207 reverseParms (tree->right);
3210 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)->next);
3213 /*------------------------------------------------------------------*/
3214 /*----------------------------*/
3215 /* return statement */
3216 /*----------------------------*/
3221 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
3223 werror (W_RETURN_MISMATCH);
3224 printFromToType (RTYPE(tree), currFunc->type->next);
3225 goto errorTreeReturn;
3228 if (IS_VOID (currFunc->type->next)
3230 !IS_VOID (RTYPE (tree)))
3232 werror (E_FUNC_VOID);
3233 goto errorTreeReturn;
3236 /* if there is going to be a casing required then add it */
3237 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
3240 decorateType (newNode (CAST,
3241 newAst_LINK (copyLinkChain (currFunc->type->next)),
3250 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
3252 werror (E_VOID_FUNC, currFunc->name);
3253 goto errorTreeReturn;
3256 TTYPE (tree) = TETYPE (tree) = NULL;
3259 /*------------------------------------------------------------------*/
3260 /*----------------------------*/
3261 /* switch statement */
3262 /*----------------------------*/
3264 /* the switch value must be an integer */
3265 if (!IS_INTEGRAL (LTYPE (tree)))
3267 werror (E_SWITCH_NON_INTEGER);
3268 goto errorTreeReturn;
3271 TTYPE (tree) = TETYPE (tree) = NULL;
3274 /*------------------------------------------------------------------*/
3275 /*----------------------------*/
3277 /*----------------------------*/
3279 tree->left = backPatchLabels (tree->left,
3282 TTYPE (tree) = TETYPE (tree) = NULL;
3285 /*------------------------------------------------------------------*/
3286 /*----------------------------*/
3288 /*----------------------------*/
3291 decorateType (resolveSymbols (AST_FOR (tree, initExpr)));
3292 decorateType (resolveSymbols (AST_FOR (tree, condExpr)));
3293 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)));
3295 /* if the for loop is reversible then
3296 reverse it otherwise do what we normally
3302 if (isLoopReversible (tree, &sym, &init, &end))
3303 return reverseLoop (tree, sym, init, end);
3305 return decorateType (createFor (AST_FOR (tree, trueLabel),
3306 AST_FOR (tree, continueLabel),
3307 AST_FOR (tree, falseLabel),
3308 AST_FOR (tree, condLabel),
3309 AST_FOR (tree, initExpr),
3310 AST_FOR (tree, condExpr),
3311 AST_FOR (tree, loopExpr),
3315 TTYPE (tree) = TETYPE (tree) = NULL;
3319 /* some error found this tree will be killed */
3321 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3322 tree->opval.op = NULLOP;
3328 /*-----------------------------------------------------------------*/
3329 /* sizeofOp - processes size of operation */
3330 /*-----------------------------------------------------------------*/
3332 sizeofOp (sym_link * type)
3336 /* make sure the type is complete and sane */
3337 checkTypeSanity(type, "(sizeof)");
3339 /* get the size and convert it to character */
3340 sprintf (buff, "%d", getSize (type));
3342 /* now convert into value */
3343 return constVal (buff);
3347 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
3348 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
3349 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
3350 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
3351 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
3352 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
3353 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
3355 /*-----------------------------------------------------------------*/
3356 /* backPatchLabels - change and or not operators to flow control */
3357 /*-----------------------------------------------------------------*/
3359 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
3365 if (!(IS_ANDORNOT (tree)))
3368 /* if this an and */
3371 static int localLbl = 0;
3374 sprintf (buffer, "_and_%d", localLbl++);
3375 localLabel = newSymbol (buffer, NestLevel);
3377 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
3379 /* if left is already a IFX then just change the if true label in that */
3380 if (!IS_IFX (tree->left))
3381 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
3383 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3384 /* right is a IFX then just join */
3385 if (IS_IFX (tree->right))
3386 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3388 tree->right = createLabel (localLabel, tree->right);
3389 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3391 return newNode (NULLOP, tree->left, tree->right);
3394 /* if this is an or operation */
3397 static int localLbl = 0;
3400 sprintf (buffer, "_or_%d", localLbl++);
3401 localLabel = newSymbol (buffer, NestLevel);
3403 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
3405 /* if left is already a IFX then just change the if true label in that */
3406 if (!IS_IFX (tree->left))
3407 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
3409 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
3410 /* right is a IFX then just join */
3411 if (IS_IFX (tree->right))
3412 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
3414 tree->right = createLabel (localLabel, tree->right);
3415 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
3417 return newNode (NULLOP, tree->left, tree->right);
3423 int wasnot = IS_NOT (tree->left);
3424 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
3426 /* if the left is already a IFX */
3427 if (!IS_IFX (tree->left))
3428 tree->left = newNode (IFX, tree->left, NULL);
3432 tree->left->trueLabel = trueLabel;
3433 tree->left->falseLabel = falseLabel;
3437 tree->left->trueLabel = falseLabel;
3438 tree->left->falseLabel = trueLabel;
3445 tree->trueLabel = trueLabel;
3446 tree->falseLabel = falseLabel;
3453 /*-----------------------------------------------------------------*/
3454 /* createBlock - create expression tree for block */
3455 /*-----------------------------------------------------------------*/
3457 createBlock (symbol * decl, ast * body)
3461 /* if the block has nothing */
3465 ex = newNode (BLOCK, NULL, body);
3466 ex->values.sym = decl;
3468 ex->right = ex->right;
3474 /*-----------------------------------------------------------------*/
3475 /* createLabel - creates the expression tree for labels */
3476 /*-----------------------------------------------------------------*/
3478 createLabel (symbol * label, ast * stmnt)
3481 char name[SDCC_NAME_MAX + 1];
3484 /* must create fresh symbol if the symbol name */
3485 /* exists in the symbol table, since there can */
3486 /* be a variable with the same name as the labl */
3487 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
3488 (csym->level == label->level))
3489 label = newSymbol (label->name, label->level);
3491 /* change the name before putting it in add _ */
3492 sprintf (name, "%s", label->name);
3494 /* put the label in the LabelSymbol table */
3495 /* but first check if a label of the same */
3497 if ((csym = findSym (LabelTab, NULL, name)))
3498 werror (E_DUPLICATE_LABEL, label->name);
3500 addSym (LabelTab, label, name, label->level, 0, 0);
3503 label->key = labelKey++;
3504 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
3510 /*-----------------------------------------------------------------*/
3511 /* createCase - generates the parsetree for a case statement */
3512 /*-----------------------------------------------------------------*/
3514 createCase (ast * swStat, ast * caseVal, ast * stmnt)
3516 char caseLbl[SDCC_NAME_MAX + 1];
3520 /* if the switch statement does not exist */
3521 /* then case is out of context */
3524 werror (E_CASE_CONTEXT);
3528 caseVal = decorateType (resolveSymbols (caseVal));
3529 /* if not a constant then error */
3530 if (!IS_LITERAL (caseVal->ftype))
3532 werror (E_CASE_CONSTANT);
3536 /* if not a integer than error */
3537 if (!IS_INTEGRAL (caseVal->ftype))
3539 werror (E_CASE_NON_INTEGER);
3543 /* find the end of the switch values chain */
3544 if (!(val = swStat->values.switchVals.swVals))
3545 swStat->values.switchVals.swVals = caseVal->opval.val;
3548 /* also order the cases according to value */
3550 int cVal = (int) floatFromVal (caseVal->opval.val);
3551 while (val && (int) floatFromVal (val) < cVal)
3557 /* if we reached the end then */
3560 pval->next = caseVal->opval.val;
3564 /* we found a value greater than */
3565 /* the current value we must add this */
3566 /* before the value */
3567 caseVal->opval.val->next = val;
3569 /* if this was the first in chain */
3570 if (swStat->values.switchVals.swVals == val)
3571 swStat->values.switchVals.swVals =
3574 pval->next = caseVal->opval.val;
3579 /* create the case label */
3580 sprintf (caseLbl, "_case_%d_%d",
3581 swStat->values.switchVals.swNum,
3582 (int) floatFromVal (caseVal->opval.val));
3584 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
3589 /*-----------------------------------------------------------------*/
3590 /* createDefault - creates the parse tree for the default statement */
3591 /*-----------------------------------------------------------------*/
3593 createDefault (ast * swStat, ast * stmnt)
3595 char defLbl[SDCC_NAME_MAX + 1];
3597 /* if the switch statement does not exist */
3598 /* then case is out of context */
3601 werror (E_CASE_CONTEXT);
3605 /* turn on the default flag */
3606 swStat->values.switchVals.swDefault = 1;
3608 /* create the label */
3609 sprintf (defLbl, "_default_%d", swStat->values.switchVals.swNum);
3610 return createLabel (newSymbol (defLbl, 0), stmnt);
3613 /*-----------------------------------------------------------------*/
3614 /* createIf - creates the parsetree for the if statement */
3615 /*-----------------------------------------------------------------*/
3617 createIf (ast * condAst, ast * ifBody, ast * elseBody)
3619 static int Lblnum = 0;
3621 symbol *ifTrue, *ifFalse, *ifEnd;
3623 /* if neither exists */
3624 if (!elseBody && !ifBody) {
3625 // if there are no side effects (i++, j() etc)
3626 if (!hasSEFcalls(condAst)) {
3631 /* create the labels */
3632 sprintf (buffer, "_iffalse_%d", Lblnum);
3633 ifFalse = newSymbol (buffer, NestLevel);
3634 /* if no else body then end == false */
3639 sprintf (buffer, "_ifend_%d", Lblnum);
3640 ifEnd = newSymbol (buffer, NestLevel);
3643 sprintf (buffer, "_iftrue_%d", Lblnum);
3644 ifTrue = newSymbol (buffer, NestLevel);
3648 /* attach the ifTrue label to the top of it body */
3649 ifBody = createLabel (ifTrue, ifBody);
3650 /* attach a goto end to the ifBody if else is present */
3653 ifBody = newNode (NULLOP, ifBody,
3655 newAst_VALUE (symbolVal (ifEnd)),
3657 /* put the elseLabel on the else body */
3658 elseBody = createLabel (ifFalse, elseBody);
3659 /* out the end at the end of the body */
3660 elseBody = newNode (NULLOP,
3662 createLabel (ifEnd, NULL));
3666 ifBody = newNode (NULLOP, ifBody,
3667 createLabel (ifFalse, NULL));
3669 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
3670 if (IS_IFX (condAst))
3673 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
3675 return newNode (NULLOP, ifTree,
3676 newNode (NULLOP, ifBody, elseBody));
3680 /*-----------------------------------------------------------------*/
3681 /* createDo - creates parse tree for do */
3684 /* _docontinue_n: */
3685 /* condition_expression +-> trueLabel -> _dobody_n */
3687 /* +-> falseLabel-> _dobreak_n */
3689 /*-----------------------------------------------------------------*/
3691 createDo (symbol * trueLabel, symbol * continueLabel,
3692 symbol * falseLabel, ast * condAst, ast * doBody)
3697 /* if the body does not exist then it is simple */
3700 condAst = backPatchLabels (condAst, continueLabel, NULL);
3701 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
3702 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
3703 doTree->trueLabel = continueLabel;
3704 doTree->falseLabel = NULL;
3708 /* otherwise we have a body */
3709 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
3711 /* attach the body label to the top */
3712 doBody = createLabel (trueLabel, doBody);
3713 /* attach the continue label to end of body */
3714 doBody = newNode (NULLOP, doBody,
3715 createLabel (continueLabel, NULL));
3717 /* now put the break label at the end */
3718 if (IS_IFX (condAst))
3721 doTree = newIfxNode (condAst, trueLabel, falseLabel);
3723 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
3725 /* putting it together */
3726 return newNode (NULLOP, doBody, doTree);
3729 /*-----------------------------------------------------------------*/
3730 /* createFor - creates parse tree for 'for' statement */
3733 /* condExpr +-> trueLabel -> _forbody_n */
3735 /* +-> falseLabel-> _forbreak_n */
3738 /* _forcontinue_n: */
3740 /* goto _forcond_n ; */
3742 /*-----------------------------------------------------------------*/
3744 createFor (symbol * trueLabel, symbol * continueLabel,
3745 symbol * falseLabel, symbol * condLabel,
3746 ast * initExpr, ast * condExpr, ast * loopExpr,
3751 /* if loopexpression not present then we can generate it */
3752 /* the same way as a while */
3754 return newNode (NULLOP, initExpr,
3755 createWhile (trueLabel, continueLabel,
3756 falseLabel, condExpr, forBody));
3757 /* vanilla for statement */
3758 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3760 if (condExpr && !IS_IFX (condExpr))
3761 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
3764 /* attach condition label to condition */
3765 condExpr = createLabel (condLabel, condExpr);
3767 /* attach body label to body */
3768 forBody = createLabel (trueLabel, forBody);
3770 /* attach continue to forLoop expression & attach */
3771 /* goto the forcond @ and of loopExpression */
3772 loopExpr = createLabel (continueLabel,
3776 newAst_VALUE (symbolVal (condLabel)),
3778 /* now start putting them together */
3779 forTree = newNode (NULLOP, initExpr, condExpr);
3780 forTree = newNode (NULLOP, forTree, forBody);
3781 forTree = newNode (NULLOP, forTree, loopExpr);
3782 /* finally add the break label */
3783 forTree = newNode (NULLOP, forTree,
3784 createLabel (falseLabel, NULL));
3788 /*-----------------------------------------------------------------*/
3789 /* createWhile - creates parse tree for while statement */
3790 /* the while statement will be created as follows */
3792 /* _while_continue_n: */
3793 /* condition_expression +-> trueLabel -> _while_boby_n */
3795 /* +-> falseLabel -> _while_break_n */
3796 /* _while_body_n: */
3798 /* goto _while_continue_n */
3799 /* _while_break_n: */
3800 /*-----------------------------------------------------------------*/
3802 createWhile (symbol * trueLabel, symbol * continueLabel,
3803 symbol * falseLabel, ast * condExpr, ast * whileBody)
3807 /* put the continue label */
3808 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
3809 condExpr = createLabel (continueLabel, condExpr);
3810 condExpr->lineno = 0;
3812 /* put the body label in front of the body */
3813 whileBody = createLabel (trueLabel, whileBody);
3814 whileBody->lineno = 0;
3815 /* put a jump to continue at the end of the body */
3816 /* and put break label at the end of the body */
3817 whileBody = newNode (NULLOP,
3820 newAst_VALUE (symbolVal (continueLabel)),
3821 createLabel (falseLabel, NULL)));
3823 /* put it all together */
3824 if (IS_IFX (condExpr))
3825 whileTree = condExpr;
3828 whileTree = newNode (IFX, condExpr, NULL);
3829 /* put the true & false labels in place */
3830 whileTree->trueLabel = trueLabel;
3831 whileTree->falseLabel = falseLabel;
3834 return newNode (NULLOP, whileTree, whileBody);
3837 /*-----------------------------------------------------------------*/
3838 /* optimizeGetHbit - get highest order bit of the expression */
3839 /*-----------------------------------------------------------------*/
3841 optimizeGetHbit (ast * tree)
3844 /* if this is not a bit and */
3845 if (!IS_BITAND (tree))
3848 /* will look for tree of the form
3849 ( expr >> ((sizeof expr) -1) ) & 1 */
3850 if (!IS_AST_LIT_VALUE (tree->right))
3853 if (AST_LIT_VALUE (tree->right) != 1)
3856 if (!IS_RIGHT_OP (tree->left))
3859 if (!IS_AST_LIT_VALUE (tree->left->right))
3862 if ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
3863 (j = (getSize (TTYPE (tree->left->left)) * 8 - 1)))
3866 return decorateType (newNode (GETHBIT, tree->left->left, NULL));
3870 /*-----------------------------------------------------------------*/
3871 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
3872 /*-----------------------------------------------------------------*/
3874 optimizeRRCRLC (ast * root)
3876 /* will look for trees of the form
3877 (?expr << 1) | (?expr >> 7) or
3878 (?expr >> 7) | (?expr << 1) will make that
3879 into a RLC : operation ..
3881 (?expr >> 1) | (?expr << 7) or
3882 (?expr << 7) | (?expr >> 1) will make that
3883 into a RRC operation
3884 note : by 7 I mean (number of bits required to hold the
3886 /* if the root operations is not a | operation the not */
3887 if (!IS_BITOR (root))
3890 /* I have to think of a better way to match patterns this sucks */
3891 /* that aside let start looking for the first case : I use a the
3892 negative check a lot to improve the efficiency */
3893 /* (?expr << 1) | (?expr >> 7) */
3894 if (IS_LEFT_OP (root->left) &&
3895 IS_RIGHT_OP (root->right))
3898 if (!SPEC_USIGN (TETYPE (root->left->left)))
3901 if (!IS_AST_LIT_VALUE (root->left->right) ||
3902 !IS_AST_LIT_VALUE (root->right->right))
3905 /* make sure it is the same expression */
3906 if (!isAstEqual (root->left->left,
3910 if (AST_LIT_VALUE (root->left->right) != 1)
3913 if (AST_LIT_VALUE (root->right->right) !=
3914 (getSize (TTYPE (root->left->left)) * 8 - 1))
3917 /* whew got the first case : create the AST */
3918 return newNode (RLC, root->left->left, NULL);
3922 /* check for second case */
3923 /* (?expr >> 7) | (?expr << 1) */
3924 if (IS_LEFT_OP (root->right) &&
3925 IS_RIGHT_OP (root->left))
3928 if (!SPEC_USIGN (TETYPE (root->left->left)))
3931 if (!IS_AST_LIT_VALUE (root->left->right) ||
3932 !IS_AST_LIT_VALUE (root->right->right))
3935 /* make sure it is the same symbol */
3936 if (!isAstEqual (root->left->left,
3940 if (AST_LIT_VALUE (root->right->right) != 1)
3943 if (AST_LIT_VALUE (root->left->right) !=
3944 (getSize (TTYPE (root->left->left)) * 8 - 1))
3947 /* whew got the first case : create the AST */
3948 return newNode (RLC, root->left->left, NULL);
3953 /* third case for RRC */
3954 /* (?symbol >> 1) | (?symbol << 7) */
3955 if (IS_LEFT_OP (root->right) &&
3956 IS_RIGHT_OP (root->left))
3959 if (!SPEC_USIGN (TETYPE (root->left->left)))
3962 if (!IS_AST_LIT_VALUE (root->left->right) ||
3963 !IS_AST_LIT_VALUE (root->right->right))
3966 /* make sure it is the same symbol */
3967 if (!isAstEqual (root->left->left,
3971 if (AST_LIT_VALUE (root->left->right) != 1)
3974 if (AST_LIT_VALUE (root->right->right) !=
3975 (getSize (TTYPE (root->left->left)) * 8 - 1))
3978 /* whew got the first case : create the AST */
3979 return newNode (RRC, root->left->left, NULL);
3983 /* fourth and last case for now */
3984 /* (?symbol << 7) | (?symbol >> 1) */
3985 if (IS_RIGHT_OP (root->right) &&
3986 IS_LEFT_OP (root->left))
3989 if (!SPEC_USIGN (TETYPE (root->left->left)))
3992 if (!IS_AST_LIT_VALUE (root->left->right) ||
3993 !IS_AST_LIT_VALUE (root->right->right))
3996 /* make sure it is the same symbol */
3997 if (!isAstEqual (root->left->left,
4001 if (AST_LIT_VALUE (root->right->right) != 1)
4004 if (AST_LIT_VALUE (root->left->right) !=
4005 (getSize (TTYPE (root->left->left)) * 8 - 1))
4008 /* whew got the first case : create the AST */
4009 return newNode (RRC, root->left->left, NULL);
4013 /* not found return root */
4017 /*-----------------------------------------------------------------*/
4018 /* optimizeCompare - otimizes compares for bit variables */
4019 /*-----------------------------------------------------------------*/
4021 optimizeCompare (ast * root)
4023 ast *optExpr = NULL;
4026 unsigned int litValue;
4028 /* if nothing then return nothing */
4032 /* if not a compare op then do leaves */
4033 if (!IS_COMPARE_OP (root))
4035 root->left = optimizeCompare (root->left);
4036 root->right = optimizeCompare (root->right);
4040 /* if left & right are the same then depending
4041 of the operation do */
4042 if (isAstEqual (root->left, root->right))
4044 switch (root->opval.op)
4049 optExpr = newAst_VALUE (constVal ("0"));
4054 optExpr = newAst_VALUE (constVal ("1"));
4058 return decorateType (optExpr);
4061 vleft = (root->left->type == EX_VALUE ?
4062 root->left->opval.val : NULL);
4064 vright = (root->right->type == EX_VALUE ?
4065 root->right->opval.val : NULL);
4067 /* if left is a BITVAR in BITSPACE */
4068 /* and right is a LITERAL then opt- */
4069 /* imize else do nothing */
4070 if (vleft && vright &&
4071 IS_BITVAR (vleft->etype) &&
4072 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
4073 IS_LITERAL (vright->etype))
4076 /* if right side > 1 then comparison may never succeed */
4077 if ((litValue = (int) floatFromVal (vright)) > 1)
4079 werror (W_BAD_COMPARE);
4085 switch (root->opval.op)
4087 case '>': /* bit value greater than 1 cannot be */
4088 werror (W_BAD_COMPARE);
4092 case '<': /* bit value < 1 means 0 */
4094 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
4097 case LE_OP: /* bit value <= 1 means no check */
4098 optExpr = newAst_VALUE (vright);
4101 case GE_OP: /* bit value >= 1 means only check for = */
4103 optExpr = newAst_VALUE (vleft);
4108 { /* literal is zero */
4109 switch (root->opval.op)
4111 case '<': /* bit value < 0 cannot be */
4112 werror (W_BAD_COMPARE);
4116 case '>': /* bit value > 0 means 1 */
4118 optExpr = newAst_VALUE (vleft);
4121 case LE_OP: /* bit value <= 0 means no check */
4122 case GE_OP: /* bit value >= 0 means no check */
4123 werror (W_BAD_COMPARE);
4127 case EQ_OP: /* bit == 0 means ! of bit */
4128 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
4132 return decorateType (resolveSymbols (optExpr));
4133 } /* end-of-if of BITVAR */
4138 /*-----------------------------------------------------------------*/
4139 /* addSymToBlock : adds the symbol to the first block we find */
4140 /*-----------------------------------------------------------------*/
4142 addSymToBlock (symbol * sym, ast * tree)
4144 /* reached end of tree or a leaf */
4145 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
4149 if (IS_AST_OP (tree) &&
4150 tree->opval.op == BLOCK)
4153 symbol *lsym = copySymbol (sym);
4155 lsym->next = AST_VALUES (tree, sym);
4156 AST_VALUES (tree, sym) = lsym;
4160 addSymToBlock (sym, tree->left);
4161 addSymToBlock (sym, tree->right);
4164 /*-----------------------------------------------------------------*/
4165 /* processRegParms - do processing for register parameters */
4166 /*-----------------------------------------------------------------*/
4168 processRegParms (value * args, ast * body)
4172 if (IS_REGPARM (args->etype))
4173 addSymToBlock (args->sym, body);
4178 /*-----------------------------------------------------------------*/
4179 /* resetParmKey - resets the operandkeys for the symbols */
4180 /*-----------------------------------------------------------------*/
4181 DEFSETFUNC (resetParmKey)
4192 /*-----------------------------------------------------------------*/
4193 /* createFunction - This is the key node that calls the iCode for */
4194 /* generating the code for a function. Note code */
4195 /* is generated function by function, later when */
4196 /* add inter-procedural analysis this will change */
4197 /*-----------------------------------------------------------------*/
4199 createFunction (symbol * name, ast * body)
4205 iCode *piCode = NULL;
4207 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
4208 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
4210 /* if check function return 0 then some problem */
4211 if (checkFunction (name, NULL) == 0)
4214 /* create a dummy block if none exists */
4216 body = newNode (BLOCK, NULL, NULL);
4220 /* check if the function name already in the symbol table */
4221 if ((csym = findSym (SymbolTab, NULL, name->name)))
4224 /* special case for compiler defined functions
4225 we need to add the name to the publics list : this
4226 actually means we are now compiling the compiler
4230 addSet (&publics, name);
4236 allocVariables (name);
4238 name->lastLine = yylineno;
4241 /* set the stack pointer */
4242 /* PENDING: check this for the mcs51 */
4243 stackPtr = -port->stack.direction * port->stack.call_overhead;
4244 if (IFFUNC_ISISR (name->type))
4245 stackPtr -= port->stack.direction * port->stack.isr_overhead;
4246 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
4247 stackPtr -= port->stack.direction * port->stack.reent_overhead;
4249 xstackPtr = -port->stack.direction * port->stack.call_overhead;
4251 fetype = getSpec (name->type); /* get the specifier for the function */
4252 /* if this is a reentrant function then */
4253 if (IFFUNC_ISREENT (name->type))
4256 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
4258 /* do processing for parameters that are passed in registers */
4259 processRegParms (FUNC_ARGS(name->type), body);
4261 /* set the stack pointer */
4265 /* allocate & autoinit the block variables */
4266 processBlockVars (body, &stack, ALLOCATE);
4268 /* save the stack information */
4269 if (options.useXstack)
4270 name->xstack = SPEC_STAK (fetype) = stack;
4272 name->stack = SPEC_STAK (fetype) = stack;
4274 /* name needs to be mangled */
4275 sprintf (name->rname, "%s%s", port->fun_prefix, name->name);
4277 body = resolveSymbols (body); /* resolve the symbols */
4278 body = decorateType (body); /* propagateType & do semantic checks */
4280 ex = newAst_VALUE (symbolVal (name)); /* create name */
4281 ex = newNode (FUNCTION, ex, body);
4282 ex->values.args = FUNC_ARGS(name->type);
4284 if (options.dump_tree) PA(ex);
4287 werror (E_FUNC_NO_CODE, name->name);
4291 /* create the node & generate intermediate code */
4293 codeOutFile = code->oFile;
4294 piCode = iCodeFromAst (ex);
4298 werror (E_FUNC_NO_CODE, name->name);
4302 eBBlockFromiCode (piCode);
4304 /* if there are any statics then do them */
4307 GcurMemmap = statsg;
4308 codeOutFile = statsg->oFile;
4309 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos))));
4315 /* dealloc the block variables */
4316 processBlockVars (body, &stack, DEALLOCATE);
4317 /* deallocate paramaters */
4318 deallocParms (FUNC_ARGS(name->type));
4320 if (IFFUNC_ISREENT (name->type))
4323 /* we are done freeup memory & cleanup */
4325 if (port->reset_labelKey) labelKey = 1;
4327 FUNC_HASBODY(name->type) = 1;
4328 addSet (&operKeyReset, name);
4329 applyToSet (operKeyReset, resetParmKey);
4332 cdbStructBlock (1, cdbFile);
4334 cleanUpLevel (LabelTab, 0);
4335 cleanUpBlock (StructTab, 1);
4336 cleanUpBlock (TypedefTab, 1);
4338 xstack->syms = NULL;
4339 istack->syms = NULL;
4344 #define INDENT(x,f) { int i ; for (i=0;i < x; i++) fprintf(f," "); }
4345 /*-----------------------------------------------------------------*/
4346 /* ast_print : prints the ast (for debugging purposes) */
4347 /*-----------------------------------------------------------------*/
4349 void ast_print (ast * tree, FILE *outfile, int indent)
4354 /* can print only decorated trees */
4355 if (!tree->decorated) return;
4357 /* if any child is an error | this one is an error do nothing */
4358 if (tree->isError ||
4359 (tree->left && tree->left->isError) ||
4360 (tree->right && tree->right->isError)) {
4361 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
4365 /* print the line */
4366 /* if not block & function */
4367 if (tree->type == EX_OP &&
4368 (tree->opval.op != FUNCTION &&
4369 tree->opval.op != BLOCK &&
4370 tree->opval.op != NULLOP)) {
4373 if (tree->opval.op == FUNCTION) {
4375 value *args=FUNC_ARGS(tree->left->opval.val->type);
4376 fprintf(outfile,"FUNCTION (%s=%p) type (",
4377 tree->left->opval.val->name, tree);
4378 printTypeChain (tree->ftype,outfile);
4379 fprintf(outfile,") args (");
4382 fprintf (outfile, ", ");
4384 printTypeChain (args ? args->type : NULL, outfile);
4386 args= args ? args->next : NULL;
4388 fprintf(outfile,")\n");
4389 ast_print(tree->left,outfile,indent);
4390 ast_print(tree->right,outfile,indent);
4393 if (tree->opval.op == BLOCK) {
4394 symbol *decls = tree->values.sym;
4395 INDENT(indent,outfile);
4396 fprintf(outfile,"{\n");
4398 INDENT(indent+2,outfile);
4399 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
4400 decls->name, decls);
4401 printTypeChain(decls->type,outfile);
4402 fprintf(outfile,")\n");
4404 decls = decls->next;
4406 ast_print(tree->right,outfile,indent+2);
4407 INDENT(indent,outfile);
4408 fprintf(outfile,"}\n");
4411 if (tree->opval.op == NULLOP) {
4412 fprintf(outfile,"\n");
4413 ast_print(tree->left,outfile,indent);
4414 fprintf(outfile,"\n");
4415 ast_print(tree->right,outfile,indent);
4418 INDENT(indent,outfile);
4420 /*------------------------------------------------------------------*/
4421 /*----------------------------*/
4422 /* leaf has been reached */
4423 /*----------------------------*/
4424 /* if this is of type value */
4425 /* just get the type */
4426 if (tree->type == EX_VALUE) {
4428 if (IS_LITERAL (tree->opval.val->etype)) {
4429 fprintf(outfile,"CONSTANT (%p) value = %d, 0x%x, %g", tree,
4430 (int) floatFromVal(tree->opval.val),
4431 (int) floatFromVal(tree->opval.val),
4432 floatFromVal(tree->opval.val));
4433 } else if (tree->opval.val->sym) {
4434 /* if the undefined flag is set then give error message */
4435 if (tree->opval.val->sym->undefined) {
4436 fprintf(outfile,"UNDEFINED SYMBOL ");
4438 fprintf(outfile,"SYMBOL ");
4440 fprintf(outfile,"(%s=%p)",
4441 tree->opval.val->sym->name,tree);
4444 fprintf(outfile," type (");
4445 printTypeChain(tree->ftype,outfile);
4446 fprintf(outfile,")\n");
4448 fprintf(outfile,"\n");
4453 /* if type link for the case of cast */
4454 if (tree->type == EX_LINK) {
4455 fprintf(outfile,"TYPENODE (%p) type = (",tree);
4456 printTypeChain(tree->opval.lnk,outfile);
4457 fprintf(outfile,")\n");
4462 /* depending on type of operator do */
4464 switch (tree->opval.op) {
4465 /*------------------------------------------------------------------*/
4466 /*----------------------------*/
4468 /*----------------------------*/
4470 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
4471 printTypeChain(tree->ftype,outfile);
4472 fprintf(outfile,")\n");
4473 ast_print(tree->left,outfile,indent+2);
4474 ast_print(tree->right,outfile,indent+2);
4477 /*------------------------------------------------------------------*/
4478 /*----------------------------*/
4480 /*----------------------------*/
4482 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
4483 printTypeChain(tree->ftype,outfile);
4484 fprintf(outfile,")\n");
4485 ast_print(tree->left,outfile,indent+2);
4486 ast_print(tree->right,outfile,indent+2);
4489 /*------------------------------------------------------------------*/
4490 /*----------------------------*/
4491 /* struct/union pointer */
4492 /*----------------------------*/
4494 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
4495 printTypeChain(tree->ftype,outfile);
4496 fprintf(outfile,")\n");
4497 ast_print(tree->left,outfile,indent+2);
4498 ast_print(tree->right,outfile,indent+2);
4501 /*------------------------------------------------------------------*/
4502 /*----------------------------*/
4503 /* ++/-- operation */
4504 /*----------------------------*/
4505 case INC_OP: /* incerement operator unary so left only */
4506 fprintf(outfile,"INC_OP (%p) type (",tree);
4507 printTypeChain(tree->ftype,outfile);
4508 fprintf(outfile,")\n");
4509 ast_print(tree->left,outfile,indent+2);
4513 fprintf(outfile,"DEC_OP (%p) type (",tree);
4514 printTypeChain(tree->ftype,outfile);
4515 fprintf(outfile,")\n");
4516 ast_print(tree->left,outfile,indent+2);
4519 /*------------------------------------------------------------------*/
4520 /*----------------------------*/
4522 /*----------------------------*/
4525 fprintf(outfile,"& (%p) type (",tree);
4526 printTypeChain(tree->ftype,outfile);
4527 fprintf(outfile,")\n");
4528 ast_print(tree->left,outfile,indent+2);
4529 ast_print(tree->right,outfile,indent+2);
4531 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
4532 printTypeChain(tree->ftype,outfile);
4533 fprintf(outfile,")\n");
4534 ast_print(tree->left,outfile,indent+2);
4535 ast_print(tree->right,outfile,indent+2);
4538 /*----------------------------*/
4540 /*----------------------------*/
4542 fprintf(outfile,"OR (%p) type (",tree);
4543 printTypeChain(tree->ftype,outfile);
4544 fprintf(outfile,")\n");
4545 ast_print(tree->left,outfile,indent+2);
4546 ast_print(tree->right,outfile,indent+2);
4548 /*------------------------------------------------------------------*/
4549 /*----------------------------*/
4551 /*----------------------------*/
4553 fprintf(outfile,"XOR (%p) type (",tree);
4554 printTypeChain(tree->ftype,outfile);
4555 fprintf(outfile,")\n");
4556 ast_print(tree->left,outfile,indent+2);
4557 ast_print(tree->right,outfile,indent+2);
4560 /*------------------------------------------------------------------*/
4561 /*----------------------------*/
4563 /*----------------------------*/
4565 fprintf(outfile,"DIV (%p) type (",tree);
4566 printTypeChain(tree->ftype,outfile);
4567 fprintf(outfile,")\n");
4568 ast_print(tree->left,outfile,indent+2);
4569 ast_print(tree->right,outfile,indent+2);
4571 /*------------------------------------------------------------------*/
4572 /*----------------------------*/
4574 /*----------------------------*/
4576 fprintf(outfile,"MOD (%p) type (",tree);
4577 printTypeChain(tree->ftype,outfile);
4578 fprintf(outfile,")\n");
4579 ast_print(tree->left,outfile,indent+2);
4580 ast_print(tree->right,outfile,indent+2);
4583 /*------------------------------------------------------------------*/
4584 /*----------------------------*/
4585 /* address dereference */
4586 /*----------------------------*/
4587 case '*': /* can be unary : if right is null then unary operation */
4589 fprintf(outfile,"DEREF (%p) type (",tree);
4590 printTypeChain(tree->ftype,outfile);
4591 fprintf(outfile,")\n");
4592 ast_print(tree->left,outfile,indent+2);
4595 /*------------------------------------------------------------------*/
4596 /*----------------------------*/
4597 /* multiplication */
4598 /*----------------------------*/
4599 fprintf(outfile,"MULT (%p) type (",tree);
4600 printTypeChain(tree->ftype,outfile);
4601 fprintf(outfile,")\n");
4602 ast_print(tree->left,outfile,indent+2);
4603 ast_print(tree->right,outfile,indent+2);
4607 /*------------------------------------------------------------------*/
4608 /*----------------------------*/
4609 /* unary '+' operator */
4610 /*----------------------------*/
4614 fprintf(outfile,"UPLUS (%p) type (",tree);
4615 printTypeChain(tree->ftype,outfile);
4616 fprintf(outfile,")\n");
4617 ast_print(tree->left,outfile,indent+2);
4619 /*------------------------------------------------------------------*/
4620 /*----------------------------*/
4622 /*----------------------------*/
4623 fprintf(outfile,"ADD (%p) type (",tree);
4624 printTypeChain(tree->ftype,outfile);
4625 fprintf(outfile,")\n");
4626 ast_print(tree->left,outfile,indent+2);
4627 ast_print(tree->right,outfile,indent+2);
4630 /*------------------------------------------------------------------*/
4631 /*----------------------------*/
4633 /*----------------------------*/
4634 case '-': /* can be unary */
4636 fprintf(outfile,"UMINUS (%p) type (",tree);
4637 printTypeChain(tree->ftype,outfile);
4638 fprintf(outfile,")\n");
4639 ast_print(tree->left,outfile,indent+2);
4641 /*------------------------------------------------------------------*/
4642 /*----------------------------*/
4644 /*----------------------------*/
4645 fprintf(outfile,"SUB (%p) type (",tree);
4646 printTypeChain(tree->ftype,outfile);
4647 fprintf(outfile,")\n");
4648 ast_print(tree->left,outfile,indent+2);
4649 ast_print(tree->right,outfile,indent+2);
4652 /*------------------------------------------------------------------*/
4653 /*----------------------------*/
4655 /*----------------------------*/
4657 fprintf(outfile,"COMPL (%p) type (",tree);
4658 printTypeChain(tree->ftype,outfile);
4659 fprintf(outfile,")\n");
4660 ast_print(tree->left,outfile,indent+2);
4662 /*------------------------------------------------------------------*/
4663 /*----------------------------*/
4665 /*----------------------------*/
4667 fprintf(outfile,"NOT (%p) type (",tree);
4668 printTypeChain(tree->ftype,outfile);
4669 fprintf(outfile,")\n");
4670 ast_print(tree->left,outfile,indent+2);
4672 /*------------------------------------------------------------------*/
4673 /*----------------------------*/
4675 /*----------------------------*/
4677 fprintf(outfile,"RRC (%p) type (",tree);
4678 printTypeChain(tree->ftype,outfile);
4679 fprintf(outfile,")\n");
4680 ast_print(tree->left,outfile,indent+2);
4684 fprintf(outfile,"RLC (%p) type (",tree);
4685 printTypeChain(tree->ftype,outfile);
4686 fprintf(outfile,")\n");
4687 ast_print(tree->left,outfile,indent+2);
4690 fprintf(outfile,"GETHBIT (%p) type (",tree);
4691 printTypeChain(tree->ftype,outfile);
4692 fprintf(outfile,")\n");
4693 ast_print(tree->left,outfile,indent+2);
4696 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
4697 printTypeChain(tree->ftype,outfile);
4698 fprintf(outfile,")\n");
4699 ast_print(tree->left,outfile,indent+2);
4700 ast_print(tree->right,outfile,indent+2);
4703 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
4704 printTypeChain(tree->ftype,outfile);
4705 fprintf(outfile,")\n");
4706 ast_print(tree->left,outfile,indent+2);
4707 ast_print(tree->right,outfile,indent+2);
4709 /*------------------------------------------------------------------*/
4710 /*----------------------------*/
4712 /*----------------------------*/
4713 case CAST: /* change the type */
4714 fprintf(outfile,"CAST (%p) from type (",tree);
4715 printTypeChain(tree->right->ftype,outfile);
4716 fprintf(outfile,") to type (");
4717 printTypeChain(tree->ftype,outfile);
4718 fprintf(outfile,")\n");
4719 ast_print(tree->right,outfile,indent+2);
4723 fprintf(outfile,"ANDAND (%p) type (",tree);
4724 printTypeChain(tree->ftype,outfile);
4725 fprintf(outfile,")\n");
4726 ast_print(tree->left,outfile,indent+2);
4727 ast_print(tree->right,outfile,indent+2);
4730 fprintf(outfile,"OROR (%p) type (",tree);
4731 printTypeChain(tree->ftype,outfile);
4732 fprintf(outfile,")\n");
4733 ast_print(tree->left,outfile,indent+2);
4734 ast_print(tree->right,outfile,indent+2);
4737 /*------------------------------------------------------------------*/
4738 /*----------------------------*/
4739 /* comparison operators */
4740 /*----------------------------*/
4742 fprintf(outfile,"GT(>) (%p) type (",tree);
4743 printTypeChain(tree->ftype,outfile);
4744 fprintf(outfile,")\n");
4745 ast_print(tree->left,outfile,indent+2);
4746 ast_print(tree->right,outfile,indent+2);
4749 fprintf(outfile,"LT(<) (%p) type (",tree);
4750 printTypeChain(tree->ftype,outfile);
4751 fprintf(outfile,")\n");
4752 ast_print(tree->left,outfile,indent+2);
4753 ast_print(tree->right,outfile,indent+2);
4756 fprintf(outfile,"LE(<=) (%p) type (",tree);
4757 printTypeChain(tree->ftype,outfile);
4758 fprintf(outfile,")\n");
4759 ast_print(tree->left,outfile,indent+2);
4760 ast_print(tree->right,outfile,indent+2);
4763 fprintf(outfile,"GE(>=) (%p) type (",tree);
4764 printTypeChain(tree->ftype,outfile);
4765 fprintf(outfile,")\n");
4766 ast_print(tree->left,outfile,indent+2);
4767 ast_print(tree->right,outfile,indent+2);
4770 fprintf(outfile,"EQ(==) (%p) type (",tree);
4771 printTypeChain(tree->ftype,outfile);
4772 fprintf(outfile,")\n");
4773 ast_print(tree->left,outfile,indent+2);
4774 ast_print(tree->right,outfile,indent+2);
4777 fprintf(outfile,"NE(!=) (%p) type (",tree);
4778 printTypeChain(tree->ftype,outfile);
4779 fprintf(outfile,")\n");
4780 ast_print(tree->left,outfile,indent+2);
4781 ast_print(tree->right,outfile,indent+2);
4782 /*------------------------------------------------------------------*/
4783 /*----------------------------*/
4785 /*----------------------------*/
4786 case SIZEOF: /* evaluate wihout code generation */
4787 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
4790 /*------------------------------------------------------------------*/
4791 /*----------------------------*/
4792 /* conditional operator '?' */
4793 /*----------------------------*/
4795 fprintf(outfile,"QUEST(?) (%p) type (",tree);
4796 printTypeChain(tree->ftype,outfile);
4797 fprintf(outfile,")\n");
4798 ast_print(tree->left,outfile,indent+2);
4799 ast_print(tree->right,outfile,indent+2);
4803 fprintf(outfile,"COLON(:) (%p) type (",tree);
4804 printTypeChain(tree->ftype,outfile);
4805 fprintf(outfile,")\n");
4806 ast_print(tree->left,outfile,indent+2);
4807 ast_print(tree->right,outfile,indent+2);
4810 /*------------------------------------------------------------------*/
4811 /*----------------------------*/
4812 /* assignment operators */
4813 /*----------------------------*/
4815 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
4816 printTypeChain(tree->ftype,outfile);
4817 fprintf(outfile,")\n");
4818 ast_print(tree->left,outfile,indent+2);
4819 ast_print(tree->right,outfile,indent+2);
4822 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
4823 printTypeChain(tree->ftype,outfile);
4824 fprintf(outfile,")\n");
4825 ast_print(tree->left,outfile,indent+2);
4826 ast_print(tree->right,outfile,indent+2);
4829 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
4830 printTypeChain(tree->ftype,outfile);
4831 fprintf(outfile,")\n");
4832 ast_print(tree->left,outfile,indent+2);
4833 ast_print(tree->right,outfile,indent+2);
4836 fprintf(outfile,"ORASS(*=) (%p) type (",tree);
4837 printTypeChain(tree->ftype,outfile);
4838 fprintf(outfile,")\n");
4839 ast_print(tree->left,outfile,indent+2);
4840 ast_print(tree->right,outfile,indent+2);
4843 fprintf(outfile,"XORASS(*=) (%p) type (",tree);
4844 printTypeChain(tree->ftype,outfile);
4845 fprintf(outfile,")\n");
4846 ast_print(tree->left,outfile,indent+2);
4847 ast_print(tree->right,outfile,indent+2);
4850 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
4851 printTypeChain(tree->ftype,outfile);
4852 fprintf(outfile,")\n");
4853 ast_print(tree->left,outfile,indent+2);
4854 ast_print(tree->right,outfile,indent+2);
4857 fprintf(outfile,"LSHFTASS(*=) (%p) type (",tree);
4858 printTypeChain(tree->ftype,outfile);
4859 fprintf(outfile,")\n");
4860 ast_print(tree->left,outfile,indent+2);
4861 ast_print(tree->right,outfile,indent+2);
4863 /*------------------------------------------------------------------*/
4864 /*----------------------------*/
4866 /*----------------------------*/
4868 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
4869 printTypeChain(tree->ftype,outfile);
4870 fprintf(outfile,")\n");
4871 ast_print(tree->left,outfile,indent+2);
4872 ast_print(tree->right,outfile,indent+2);
4874 /*------------------------------------------------------------------*/
4875 /*----------------------------*/
4877 /*----------------------------*/
4879 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
4880 printTypeChain(tree->ftype,outfile);
4881 fprintf(outfile,")\n");
4882 ast_print(tree->left,outfile,indent+2);
4883 ast_print(tree->right,outfile,indent+2);
4885 /*------------------------------------------------------------------*/
4886 /*----------------------------*/
4887 /* straight assignemnt */
4888 /*----------------------------*/
4890 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
4891 printTypeChain(tree->ftype,outfile);
4892 fprintf(outfile,")\n");
4893 ast_print(tree->left,outfile,indent+2);
4894 ast_print(tree->right,outfile,indent+2);
4896 /*------------------------------------------------------------------*/
4897 /*----------------------------*/
4898 /* comma operator */
4899 /*----------------------------*/
4901 fprintf(outfile,"COMMA(,) (%p) type (",tree);
4902 printTypeChain(tree->ftype,outfile);
4903 fprintf(outfile,")\n");
4904 ast_print(tree->left,outfile,indent+2);
4905 ast_print(tree->right,outfile,indent+2);
4907 /*------------------------------------------------------------------*/
4908 /*----------------------------*/
4910 /*----------------------------*/
4913 fprintf(outfile,"CALL (%p) type (",tree);
4914 printTypeChain(tree->ftype,outfile);
4915 fprintf(outfile,")\n");
4916 ast_print(tree->left,outfile,indent+2);
4917 ast_print(tree->right,outfile,indent+2);
4920 fprintf(outfile,"PARMS\n");
4921 ast_print(tree->left,outfile,indent+2);
4922 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
4923 ast_print(tree->right,outfile,indent+2);
4926 /*------------------------------------------------------------------*/
4927 /*----------------------------*/
4928 /* return statement */
4929 /*----------------------------*/
4931 fprintf(outfile,"RETURN (%p) type (",tree);
4933 printTypeChain(tree->right->ftype,outfile);
4935 fprintf(outfile,")\n");
4936 ast_print(tree->right,outfile,indent+2);
4938 /*------------------------------------------------------------------*/
4939 /*----------------------------*/
4940 /* label statement */
4941 /*----------------------------*/
4943 fprintf(outfile,"LABEL (%p)\n",tree);
4944 ast_print(tree->left,outfile,indent+2);
4945 ast_print(tree->right,outfile,indent);
4947 /*------------------------------------------------------------------*/
4948 /*----------------------------*/
4949 /* switch statement */
4950 /*----------------------------*/
4954 fprintf(outfile,"SWITCH (%p) ",tree);
4955 ast_print(tree->left,outfile,0);
4956 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
4957 INDENT(indent+2,outfile);
4958 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
4959 (int) floatFromVal(val),
4960 tree->values.switchVals.swNum,
4961 (int) floatFromVal(val));
4963 ast_print(tree->right,outfile,indent);
4966 /*------------------------------------------------------------------*/
4967 /*----------------------------*/
4969 /*----------------------------*/
4971 fprintf(outfile,"IF (%p) \n",tree);
4972 ast_print(tree->left,outfile,indent+2);
4973 if (tree->trueLabel) {
4974 INDENT(indent,outfile);
4975 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
4977 if (tree->falseLabel) {
4978 INDENT(indent,outfile);
4979 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
4981 ast_print(tree->right,outfile,indent+2);
4983 /*------------------------------------------------------------------*/
4984 /*----------------------------*/
4986 /*----------------------------*/
4988 fprintf(outfile,"FOR (%p) \n",tree);
4989 if (AST_FOR( tree, initExpr)) {
4990 INDENT(indent+2,outfile);
4991 fprintf(outfile,"INIT EXPR ");
4992 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
4994 if (AST_FOR( tree, condExpr)) {
4995 INDENT(indent+2,outfile);
4996 fprintf(outfile,"COND EXPR ");
4997 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
4999 if (AST_FOR( tree, loopExpr)) {
5000 INDENT(indent+2,outfile);
5001 fprintf(outfile,"LOOP EXPR ");
5002 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
5004 fprintf(outfile,"FOR LOOP BODY \n");
5005 ast_print(tree->left,outfile,indent+2);
5014 ast_print(t,stdout,0);