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 -------------------------------------------------------------------------*/
25 #define DEBUG_CF(x) /* puts(x); */
31 set *operKeyReset = NULL;
32 ast *staticAutos = NULL;
35 #define LRVAL(x) x->left->rvalue
36 #define RRVAL(x) x->right->rvalue
37 #define TRVAL(x) x->rvalue
38 #define LLVAL(x) x->left->lvalue
39 #define RLVAL(x) x->right->lvalue
40 #define TLVAL(x) x->lvalue
41 #define RTYPE(x) x->right->ftype
42 #define RETYPE(x) x->right->etype
43 #define LTYPE(x) x->left->ftype
44 #define LETYPE(x) x->left->etype
45 #define TTYPE(x) x->ftype
46 #define TETYPE(x) x->etype
52 symbol *currFunc=NULL;
53 static ast *createIval (ast *, sym_link *, initList *, ast *);
54 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
55 static ast *optimizeCompare (ast *);
56 ast *optimizeRRCRLC (ast *);
57 ast *optimizeSWAP (ast *);
58 ast *optimizeGetHbit (ast *);
59 ast *backPatchLabels (ast *, symbol *, symbol *);
62 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
67 printTypeChain (tree->ftype, stdout);
72 /*-----------------------------------------------------------------*/
73 /* newAst - creates a fresh node for an expression tree */
74 /*-----------------------------------------------------------------*/
76 newAst_ (unsigned type)
79 static int oldLineno = 0;
81 ex = Safe_alloc ( sizeof (ast));
84 ex->lineno = (noLineno ? oldLineno : mylineno);
85 ex->filename = currFname;
86 ex->level = NestLevel;
87 ex->block = currBlockno;
88 ex->initMode = inInitMode;
89 ex->seqPoint = seqPointNo;
94 newAst_VALUE (value * val)
96 ast *ex = newAst_ (EX_VALUE);
102 newAst_OP (unsigned op)
104 ast *ex = newAst_ (EX_OP);
110 newAst_LINK (sym_link * val)
112 ast *ex = newAst_ (EX_LINK);
117 /*-----------------------------------------------------------------*/
118 /* newNode - creates a new node */
119 /*-----------------------------------------------------------------*/
121 newNode (long op, ast * left, ast * right)
132 /*-----------------------------------------------------------------*/
133 /* newIfxNode - creates a new Ifx Node */
134 /*-----------------------------------------------------------------*/
136 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
140 /* if this is a literal then we already know the result */
141 if (condAst->etype && IS_LITERAL (condAst->etype))
143 /* then depending on the expression value */
144 if (floatFromVal (condAst->opval.val))
145 ifxNode = newNode (GOTO,
146 newAst_VALUE (symbolVal (trueLabel)),
149 ifxNode = newNode (GOTO,
150 newAst_VALUE (symbolVal (falseLabel)),
155 ifxNode = newNode (IFX, condAst, NULL);
156 ifxNode->trueLabel = trueLabel;
157 ifxNode->falseLabel = falseLabel;
163 /*-----------------------------------------------------------------*/
164 /* copyAstValues - copies value portion of ast if needed */
165 /*-----------------------------------------------------------------*/
167 copyAstValues (ast * dest, ast * src)
169 switch (src->opval.op)
172 dest->values.sym = copySymbolChain (src->values.sym);
176 dest->values.switchVals.swVals =
177 copyValue (src->values.switchVals.swVals);
178 dest->values.switchVals.swDefault =
179 src->values.switchVals.swDefault;
180 dest->values.switchVals.swNum =
181 src->values.switchVals.swNum;
185 dest->values.inlineasm = Safe_strdup(src->values.inlineasm);
189 dest->values.constlist = copyLiteralList(src->values.constlist);
193 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
194 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
195 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
196 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
197 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
198 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
199 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
204 /*-----------------------------------------------------------------*/
205 /* copyAst - makes a copy of a given astession */
206 /*-----------------------------------------------------------------*/
215 dest = Safe_alloc ( sizeof (ast));
217 dest->type = src->type;
218 dest->lineno = src->lineno;
219 dest->level = src->level;
220 dest->funcName = src->funcName;
223 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
225 /* if this is a leaf */
227 if (src->type == EX_VALUE)
229 dest->opval.val = copyValue (src->opval.val);
234 if (src->type == EX_LINK)
236 dest->opval.lnk = copyLinkChain (src->opval.lnk);
240 dest->opval.op = src->opval.op;
242 /* if this is a node that has special values */
243 copyAstValues (dest, src);
245 dest->trueLabel = copySymbol (src->trueLabel);
246 dest->falseLabel = copySymbol (src->falseLabel);
247 dest->left = copyAst (src->left);
248 dest->right = copyAst (src->right);
254 /*-----------------------------------------------------------------*/
255 /* removeIncDecOps: remove for side effects in *_ASSIGN's */
256 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
257 /*-----------------------------------------------------------------*/
258 ast *removeIncDecOps (ast * tree) {
260 // traverse the tree and remove inc/dec ops
265 if (tree->type == EX_OP &&
266 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
273 tree->left=removeIncDecOps(tree->left);
274 tree->right=removeIncDecOps(tree->right);
279 /*-----------------------------------------------------------------*/
280 /* removePreIncDecOps: remove for side effects in *_ASSIGN's */
281 /* "*++s += 3" -> "*++s = *++s + 3" */
282 /*-----------------------------------------------------------------*/
283 ast *removePreIncDecOps (ast * tree) {
285 // traverse the tree and remove pre-inc/dec ops
290 if (tree->type == EX_OP &&
291 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
296 tree->left=removePreIncDecOps(tree->left);
297 tree->right=removePreIncDecOps(tree->right);
302 /*-----------------------------------------------------------------*/
303 /* removePostIncDecOps: remove for side effects in *_ASSIGN's */
304 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
305 /*-----------------------------------------------------------------*/
306 ast *removePostIncDecOps (ast * tree) {
308 // traverse the tree and remove pre-inc/dec ops
313 if (tree->type == EX_OP &&
314 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
319 tree->left=removePostIncDecOps(tree->left);
320 tree->right=removePostIncDecOps(tree->right);
325 /*-----------------------------------------------------------------*/
326 /* hasSEFcalls - returns TRUE if tree has a function call */
327 /*-----------------------------------------------------------------*/
329 hasSEFcalls (ast * tree)
334 if (tree->type == EX_OP &&
335 (tree->opval.op == CALL ||
336 tree->opval.op == PCALL ||
337 tree->opval.op == '=' ||
338 tree->opval.op == INC_OP ||
339 tree->opval.op == DEC_OP))
342 return (hasSEFcalls (tree->left) |
343 hasSEFcalls (tree->right));
346 /*-----------------------------------------------------------------*/
347 /* isAstEqual - compares two asts & returns 1 if they are equal */
348 /*-----------------------------------------------------------------*/
350 isAstEqual (ast * t1, ast * t2)
359 if (t1->type != t2->type)
365 if (t1->opval.op != t2->opval.op)
367 return (isAstEqual (t1->left, t2->left) &&
368 isAstEqual (t1->right, t2->right));
372 if (t1->opval.val->sym)
374 if (!t2->opval.val->sym)
377 return isSymbolEqual (t1->opval.val->sym,
382 if (t2->opval.val->sym)
385 return (floatFromVal (t1->opval.val) ==
386 floatFromVal (t2->opval.val));
390 /* only compare these two types */
398 /*-----------------------------------------------------------------*/
399 /* resolveSymbols - resolve symbols from the symbol table */
400 /*-----------------------------------------------------------------*/
402 resolveSymbols (ast * tree)
404 /* walk the entire tree and check for values */
405 /* with symbols if we find one then replace */
406 /* symbol with that from the symbol table */
413 /* if not block & function */
414 if (tree->type == EX_OP &&
415 (tree->opval.op != FUNCTION &&
416 tree->opval.op != BLOCK &&
417 tree->opval.op != NULLOP))
419 filename = tree->filename;
420 lineno = tree->lineno;
424 /* make sure we resolve the true & false labels for ifx */
425 if (tree->type == EX_OP && tree->opval.op == IFX)
431 if ((csym = findSym (LabelTab, tree->trueLabel,
432 tree->trueLabel->name)))
433 tree->trueLabel = csym;
435 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
436 tree->trueLabel->name);
439 if (tree->falseLabel)
441 if ((csym = findSym (LabelTab,
443 tree->falseLabel->name)))
444 tree->falseLabel = csym;
446 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
447 tree->falseLabel->name);
452 /* if this is a label resolve it from the labelTab */
453 if (IS_AST_VALUE (tree) &&
454 tree->opval.val->sym &&
455 tree->opval.val->sym->islbl)
458 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
459 tree->opval.val->sym->name);
462 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
463 tree->opval.val->sym->name);
465 tree->opval.val->sym = csym;
467 goto resolveChildren;
470 /* do only for leafs */
471 if (IS_AST_VALUE (tree) &&
472 tree->opval.val->sym &&
473 !tree->opval.val->sym->implicit)
476 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
478 /* if found in the symbol table & they r not the same */
479 if (csym && tree->opval.val->sym != csym)
481 tree->opval.val->sym = csym;
482 tree->opval.val->type = csym->type;
483 tree->opval.val->etype = csym->etype;
486 /* if not found in the symbol table */
487 /* mark it as undefined assume it is */
488 /* an integer in data space */
489 if (!csym && !tree->opval.val->sym->implicit)
492 /* if this is a function name then */
493 /* mark it as returning an int */
496 tree->opval.val->sym->type = newLink (DECLARATOR);
497 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
498 tree->opval.val->sym->type->next =
499 tree->opval.val->sym->etype = newIntLink ();
500 tree->opval.val->etype = tree->opval.val->etype;
501 tree->opval.val->type = tree->opval.val->sym->type;
502 werrorfl (tree->filename, tree->lineno, W_IMPLICIT_FUNC,
503 tree->opval.val->sym->name);
504 //tree->opval.val->sym->undefined = 1;
505 allocVariables (tree->opval.val->sym);
509 tree->opval.val->sym->undefined = 1;
510 tree->opval.val->type =
511 tree->opval.val->etype = newIntLink ();
512 tree->opval.val->sym->type =
513 tree->opval.val->sym->etype = newIntLink ();
519 resolveSymbols (tree->left);
520 resolveSymbols (tree->right);
525 /*-----------------------------------------------------------------*/
526 /* setAstLineno - walks a ast tree & sets the line number */
527 /*-----------------------------------------------------------------*/
528 int setAstLineno (ast * tree, int lineno)
533 tree->lineno = lineno;
534 setAstLineno (tree->left, lineno);
535 setAstLineno (tree->right, lineno);
539 /*-----------------------------------------------------------------*/
540 /* funcOfType :- function of type with name */
541 /*-----------------------------------------------------------------*/
543 funcOfType (char *name, sym_link * type, sym_link * argType,
547 /* create the symbol */
548 sym = newSymbol (name, 0);
550 /* setup return value */
551 sym->type = newLink (DECLARATOR);
552 DCL_TYPE (sym->type) = FUNCTION;
553 sym->type->next = copyLinkChain (type);
554 sym->etype = getSpec (sym->type);
555 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
557 /* if arguments required */
561 args = FUNC_ARGS(sym->type) = newValue ();
565 args->type = copyLinkChain (argType);
566 args->etype = getSpec (args->type);
567 SPEC_EXTR(args->etype)=1;
570 args = args->next = newValue ();
577 allocVariables (sym);
582 /*-----------------------------------------------------------------*/
583 /* funcOfTypeVarg :- function of type with name and argtype */
584 /*-----------------------------------------------------------------*/
586 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
591 /* create the symbol */
592 sym = newSymbol (name, 0);
594 /* setup return value */
595 sym->type = newLink (DECLARATOR);
596 DCL_TYPE (sym->type) = FUNCTION;
597 sym->type->next = typeFromStr(rtype);
598 sym->etype = getSpec (sym->type);
600 /* if arguments required */
603 args = FUNC_ARGS(sym->type) = newValue ();
605 for ( i = 0 ; i < nArgs ; i++ ) {
606 args->type = typeFromStr(atypes[i]);
607 args->etype = getSpec (args->type);
608 SPEC_EXTR(args->etype)=1;
609 if ((i + 1) == nArgs) break;
610 args = args->next = newValue ();
617 allocVariables (sym);
622 /*-----------------------------------------------------------------*/
623 /* reverseParms - will reverse a parameter tree */
624 /*-----------------------------------------------------------------*/
626 reverseParms (ast * ptree)
632 /* top down if we find a nonParm tree then quit */
633 if (ptree->type == EX_OP && ptree->opval.op == PARAM)
636 ptree->left = ptree->right;
637 ptree->right = ttree;
638 reverseParms (ptree->left);
639 reverseParms (ptree->right);
645 /*-----------------------------------------------------------------*/
646 /* processParms - makes sure the parameters are okay and do some */
647 /* processing with them */
648 /*-----------------------------------------------------------------*/
650 processParms (ast *func,
653 int *parmNumber, /* unused, although updated */
656 RESULT_TYPE resultType;
659 /* if none of them exist */
660 if (!defParm && !*actParm)
665 if (getenv("DEBUG_SANITY"))
667 fprintf (stderr, "processParms: %s ", defParm->name);
669 /* make sure the type is complete and sane */
670 checkTypeSanity(defParm->etype, defParm->name);
673 if (IS_CODEPTR (func->ftype))
674 functype = func->ftype->next;
676 functype = func->ftype;
678 /* if the function is being called via a pointer & */
679 /* it has not been defined a reentrant then we cannot */
680 /* have parameters */
681 if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
683 werror (W_NONRENT_ARGS);
688 /* if defined parameters ended but actual parameters */
689 /* exist and this is not defined as a variable arg */
690 if (!defParm && *actParm && !IFFUNC_HASVARARGS(functype))
692 werror (E_TOO_MANY_PARMS);
696 /* if defined parameters present but no actual parameters */
697 if (defParm && !*actParm)
699 werror (E_TOO_FEW_PARMS);
703 /* if this is a PARAM node then match left & right */
704 if ((*actParm)->type == EX_OP && (*actParm)->opval.op == PARAM)
706 (*actParm)->decorated = 1;
707 return (processParms (func, defParm,
708 &(*actParm)->left, parmNumber, FALSE) ||
709 processParms (func, defParm ? defParm->next : NULL,
710 &(*actParm)->right, parmNumber, rightmost));
712 else if (defParm) /* not vararg */
714 /* If we have found a value node by following only right-hand links,
715 * then we know that there are no more values after us.
717 * Therefore, if there are more defined parameters, the caller didn't
720 if (rightmost && defParm->next)
722 werror (E_TOO_FEW_PARMS);
727 /* decorate parameter */
728 resultType = defParm ? getResultTypeFromType (defParm->etype) :
730 *actParm = decorateType (*actParm, resultType);
732 if (IS_VOID((*actParm)->ftype))
734 werror (E_VOID_VALUE_USED);
738 /* If this is a varargs function... */
739 if (!defParm && *actParm && IFFUNC_HASVARARGS(functype))
744 if (IS_CAST_OP (*actParm)
745 || (IS_AST_LIT_VALUE (*actParm) && (*actParm)->values.literalFromCast))
747 /* Parameter was explicitly typecast; don't touch it. */
751 ftype = (*actParm)->ftype;
753 /* If it's a char, upcast to int. */
754 if (IS_INTEGRAL (ftype)
755 && (getSize (ftype) < (unsigned) INTSIZE))
757 newType = newAst_LINK(INTTYPE);
760 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
762 newType = newAst_LINK (copyLinkChain(ftype));
763 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
766 if (IS_AGGREGATE (ftype))
768 newType = newAst_LINK (copyLinkChain (ftype));
769 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
774 /* cast required; change this op to a cast. */
775 ast *parmCopy = resolveSymbols (copyAst (*actParm));
777 (*actParm)->type = EX_OP;
778 (*actParm)->opval.op = CAST;
779 (*actParm)->left = newType;
780 (*actParm)->right = parmCopy;
781 (*actParm)->decorated = 0; /* force typechecking */
782 decorateType (*actParm, RESULT_TYPE_NONE);
787 /* if defined parameters ended but actual has not & */
789 if (!defParm && *actParm &&
790 (options.stackAuto || IFFUNC_ISREENT (functype)))
793 resolveSymbols (*actParm);
795 /* the parameter type must be at least castable */
796 if (compareType (defParm->type, (*actParm)->ftype) == 0)
798 werror (E_INCOMPAT_TYPES);
799 printFromToType ((*actParm)->ftype, defParm->type);
803 /* if the parameter is castable then add the cast */
804 if (compareType (defParm->type, (*actParm)->ftype) < 0)
808 resultType = getResultTypeFromType (defParm->etype);
809 pTree = resolveSymbols (copyAst (*actParm));
811 /* now change the current one to a cast */
812 (*actParm)->type = EX_OP;
813 (*actParm)->opval.op = CAST;
814 (*actParm)->left = newAst_LINK (defParm->type);
815 (*actParm)->right = pTree;
816 (*actParm)->decorated = 0; /* force typechecking */
817 decorateType (*actParm, resultType);
820 /* make a copy and change the regparm type to the defined parm */
821 (*actParm)->etype = getSpec ((*actParm)->ftype = copyLinkChain ((*actParm)->ftype));
822 SPEC_REGPARM ((*actParm)->etype) = SPEC_REGPARM (defParm->etype);
823 SPEC_ARGREG ((*actParm)->etype) = SPEC_ARGREG (defParm->etype);
828 /*-----------------------------------------------------------------*/
829 /* createIvalType - generates ival for basic types */
830 /*-----------------------------------------------------------------*/
832 createIvalType (ast * sym, sym_link * type, initList * ilist)
836 /* if initList is deep */
837 if (ilist->type == INIT_DEEP)
838 ilist = ilist->init.deep;
840 iExpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK);
841 return decorateType (newNode ('=', sym, iExpr), RESULT_CHECK);
844 /*-----------------------------------------------------------------*/
845 /* createIvalStruct - generates initial value for structures */
846 /*-----------------------------------------------------------------*/
848 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
855 sflds = SPEC_STRUCT (type)->fields;
856 if (ilist->type != INIT_DEEP)
858 werror (E_INIT_STRUCT, "");
862 iloop = ilist->init.deep;
864 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
866 /* if we have come to end */
870 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
871 lAst = decorateType (resolveSymbols (lAst), RESULT_CHECK);
872 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast)), RESULT_CHECK);
876 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
877 W_EXCESS_INITIALIZERS, "struct",
878 sym->opval.val->sym->name);
885 /*-----------------------------------------------------------------*/
886 /* createIvalArray - generates code for array initialization */
887 /*-----------------------------------------------------------------*/
889 createIvalArray (ast * sym, sym_link * type, initList * ilist)
893 int lcnt = 0, size = 0;
894 literalList *literalL;
896 /* take care of the special case */
897 /* array of characters can be init */
899 if (IS_CHAR (type->next))
900 if ((rast = createIvalCharPtr (sym,
902 decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK))))
904 return decorateType (resolveSymbols (rast), RESULT_CHECK);
906 /* not the special case */
907 if (ilist->type != INIT_DEEP)
909 werror (E_INIT_STRUCT, "");
913 iloop = ilist->init.deep;
914 lcnt = DCL_ELEM (type);
916 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
920 aSym = decorateType (resolveSymbols(sym), RESULT_CHECK);
922 rast = newNode(ARRAYINIT, aSym, NULL);
923 rast->values.constlist = literalL;
925 // Make sure size is set to length of initializer list.
932 if (lcnt && size > lcnt)
934 // Array size was specified, and we have more initializers than needed.
935 char *name=sym->opval.val->sym->name;
936 int lineno=sym->opval.val->sym->lineDef;
937 char *filename=sym->opval.val->sym->fileDef;
939 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
948 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
949 aSym = decorateType (resolveSymbols (aSym), RESULT_CHECK);
950 rast = createIval (aSym, type->next, iloop, rast);
951 iloop = (iloop ? iloop->next : NULL);
957 /* no of elements given and we */
958 /* have generated for all of them */
961 // there has to be a better way
962 char *name=sym->opval.val->sym->name;
963 int lineno=sym->opval.val->sym->lineDef;
964 char *filename=sym->opval.val->sym->fileDef;
965 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
972 /* if we have not been given a size */
973 if (!DCL_ELEM (type))
975 DCL_ELEM (type) = size;
978 return decorateType (resolveSymbols (rast), RESULT_CHECK);
982 /*-----------------------------------------------------------------*/
983 /* createIvalCharPtr - generates initial values for char pointers */
984 /*-----------------------------------------------------------------*/
986 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
990 /* if this is a pointer & right is a literal array then */
991 /* just assignment will do */
992 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
993 SPEC_SCLS (iexpr->etype) == S_CODE)
994 && IS_ARRAY (iexpr->ftype)))
995 return newNode ('=', sym, iexpr);
997 /* left side is an array so we have to assign each */
999 if ((IS_LITERAL (iexpr->etype) ||
1000 SPEC_SCLS (iexpr->etype) == S_CODE)
1001 && IS_ARRAY (iexpr->ftype))
1003 /* for each character generate an assignment */
1004 /* to the array element */
1005 char *s = SPEC_CVAL (iexpr->etype).v_char;
1007 int size = getSize (iexpr->ftype);
1008 int symsize = getSize (type);
1012 if (size>(symsize+1))
1013 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1014 "string", sym->opval.val->sym->name);
1018 for (i=0;i<size;i++)
1020 rast = newNode (NULLOP,
1024 newAst_VALUE (valueFromLit ((float) i))),
1025 newAst_VALUE (valueFromLit (*s))));
1029 // now WE don't need iexpr's symbol anymore
1030 freeStringSymbol(AST_SYMBOL(iexpr));
1032 return decorateType (resolveSymbols (rast), RESULT_CHECK);
1038 /*-----------------------------------------------------------------*/
1039 /* createIvalPtr - generates initial value for pointers */
1040 /*-----------------------------------------------------------------*/
1042 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
1048 if (ilist->type == INIT_DEEP)
1049 ilist = ilist->init.deep;
1051 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK);
1053 /* if character pointer */
1054 if (IS_CHAR (type->next))
1055 if ((rast = createIvalCharPtr (sym, type, iexpr)))
1058 return newNode ('=', sym, iexpr);
1061 /*-----------------------------------------------------------------*/
1062 /* createIval - generates code for initial value */
1063 /*-----------------------------------------------------------------*/
1065 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
1072 /* if structure then */
1073 if (IS_STRUCT (type))
1074 rast = createIvalStruct (sym, type, ilist);
1076 /* if this is a pointer */
1078 rast = createIvalPtr (sym, type, ilist);
1080 /* if this is an array */
1081 if (IS_ARRAY (type))
1082 rast = createIvalArray (sym, type, ilist);
1084 /* if type is SPECIFIER */
1086 rast = createIvalType (sym, type, ilist);
1089 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_CHECK);
1091 return decorateType (resolveSymbols (rast), RESULT_CHECK);
1094 /*-----------------------------------------------------------------*/
1095 /* initAggregates - initialises aggregate variables with initv */
1096 /*-----------------------------------------------------------------*/
1097 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1098 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1101 /*-----------------------------------------------------------------*/
1102 /* gatherAutoInit - creates assignment expressions for initial */
1104 /*-----------------------------------------------------------------*/
1106 gatherAutoInit (symbol * autoChain)
1113 for (sym = autoChain; sym; sym = sym->next)
1116 /* resolve the symbols in the ival */
1118 resolveIvalSym (sym->ival, sym->type);
1120 /* if this is a static variable & has an */
1121 /* initial value the code needs to be lifted */
1122 /* here to the main portion since they can be */
1123 /* initialised only once at the start */
1124 if (IS_STATIC (sym->etype) && sym->ival &&
1125 SPEC_SCLS (sym->etype) != S_CODE)
1129 /* insert the symbol into the symbol table */
1130 /* with level = 0 & name = rname */
1131 newSym = copySymbol (sym);
1132 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1134 /* now lift the code to main */
1135 if (IS_AGGREGATE (sym->type)) {
1136 work = initAggregates (sym, sym->ival, NULL);
1138 if (getNelements(sym->type, sym->ival)>1) {
1139 werrorfl (sym->fileDef, sym->lineDef,
1140 W_EXCESS_INITIALIZERS, "scalar",
1143 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1144 list2expr (sym->ival));
1147 setAstLineno (work, sym->lineDef);
1151 staticAutos = newNode (NULLOP, staticAutos, work);
1158 /* if there is an initial value */
1159 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1161 initList *ilist=sym->ival;
1163 while (ilist->type == INIT_DEEP) {
1164 ilist = ilist->init.deep;
1167 /* update lineno for error msg */
1168 lineno=sym->lineDef;
1169 setAstLineno (ilist->init.node, lineno);
1171 if (IS_AGGREGATE (sym->type)) {
1172 work = initAggregates (sym, sym->ival, NULL);
1174 if (getNelements(sym->type, sym->ival)>1) {
1175 werrorfl (sym->fileDef, sym->lineDef,
1176 W_EXCESS_INITIALIZERS, "scalar",
1179 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1180 list2expr (sym->ival));
1184 setAstLineno (work, sym->lineDef);
1188 init = newNode (NULLOP, init, work);
1197 /*-----------------------------------------------------------------*/
1198 /* freeStringSymbol - delete a literal string if no more usage */
1199 /*-----------------------------------------------------------------*/
1200 void freeStringSymbol(symbol *sym) {
1201 /* make sure this is a literal string */
1202 assert (sym->isstrlit);
1203 if (--sym->isstrlit == 0) { // lower the usage count
1204 memmap *segment=SPEC_OCLS(sym->etype);
1206 deleteSetItem(&segment->syms, sym);
1211 /*-----------------------------------------------------------------*/
1212 /* stringToSymbol - creates a symbol from a literal string */
1213 /*-----------------------------------------------------------------*/
1215 stringToSymbol (value * val)
1217 char name[SDCC_NAME_MAX + 1];
1218 static int charLbl = 0;
1223 // have we heard this before?
1224 for (sp=statsg->syms; sp; sp=sp->next) {
1226 size = getSize (sym->type);
1227 if (sym->isstrlit && size == getSize (val->type) &&
1228 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1229 // yes, this is old news. Don't publish it again.
1230 sym->isstrlit++; // but raise the usage count
1231 return symbolVal(sym);
1235 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1236 sym = newSymbol (name, 0); /* make it @ level 0 */
1237 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1239 /* copy the type from the value passed */
1240 sym->type = copyLinkChain (val->type);
1241 sym->etype = getSpec (sym->type);
1242 /* change to storage class & output class */
1243 SPEC_SCLS (sym->etype) = S_CODE;
1244 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1245 SPEC_STAT (sym->etype) = 1;
1246 /* make the level & block = 0 */
1247 sym->block = sym->level = 0;
1249 /* create an ival */
1250 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1255 allocVariables (sym);
1258 return symbolVal (sym);
1262 /*-----------------------------------------------------------------*/
1263 /* processBlockVars - will go thru the ast looking for block if */
1264 /* a block is found then will allocate the syms */
1265 /* will also gather the auto inits present */
1266 /*-----------------------------------------------------------------*/
1268 processBlockVars (ast * tree, int *stack, int action)
1273 /* if this is a block */
1274 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1278 if (action == ALLOCATE)
1280 *stack += allocVariables (tree->values.sym);
1281 autoInit = gatherAutoInit (tree->values.sym);
1283 /* if there are auto inits then do them */
1285 tree->left = newNode (NULLOP, autoInit, tree->left);
1287 else /* action is deallocate */
1288 deallocLocal (tree->values.sym);
1291 processBlockVars (tree->left, stack, action);
1292 processBlockVars (tree->right, stack, action);
1297 /*-------------------------------------------------------------*/
1298 /* constExprTree - returns TRUE if this tree is a constant */
1300 /*-------------------------------------------------------------*/
1301 bool constExprTree (ast *cexpr) {
1307 cexpr = decorateType (resolveSymbols (cexpr), RESULT_CHECK);
1309 switch (cexpr->type)
1312 if (IS_AST_LIT_VALUE(cexpr)) {
1313 // this is a literal
1316 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1317 // a function's address will never change
1320 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1321 // an array's address will never change
1324 if (IS_AST_SYM_VALUE(cexpr) &&
1325 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1326 // a symbol in code space will never change
1327 // This is only for the 'char *s="hallo"' case and will have to leave
1328 //printf(" code space symbol");
1333 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1334 "unexpected link in expression tree\n");
1337 if (cexpr->opval.op==ARRAYINIT) {
1338 // this is a list of literals
1341 if (cexpr->opval.op=='=') {
1342 return constExprTree(cexpr->right);
1344 if (cexpr->opval.op==CAST) {
1345 // cast ignored, maybe we should throw a warning here?
1346 return constExprTree(cexpr->right);
1348 if (cexpr->opval.op=='&') {
1351 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1354 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1359 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1364 /*-----------------------------------------------------------------*/
1365 /* constExprValue - returns the value of a constant expression */
1366 /* or NULL if it is not a constant expression */
1367 /*-----------------------------------------------------------------*/
1369 constExprValue (ast * cexpr, int check)
1371 cexpr = decorateType (resolveSymbols (cexpr), RESULT_CHECK);
1373 /* if this is not a constant then */
1374 if (!IS_LITERAL (cexpr->ftype))
1376 /* then check if this is a literal array
1378 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1379 SPEC_CVAL (cexpr->etype).v_char &&
1380 IS_ARRAY (cexpr->ftype))
1382 value *val = valFromType (cexpr->ftype);
1383 SPEC_SCLS (val->etype) = S_LITERAL;
1384 val->sym = cexpr->opval.val->sym;
1385 val->sym->type = copyLinkChain (cexpr->ftype);
1386 val->sym->etype = getSpec (val->sym->type);
1387 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1391 /* if we are casting a literal value then */
1392 if (IS_AST_OP (cexpr) &&
1393 cexpr->opval.op == CAST &&
1394 IS_LITERAL (cexpr->right->ftype))
1396 return valCastLiteral (cexpr->ftype,
1397 floatFromVal (cexpr->right->opval.val));
1400 if (IS_AST_VALUE (cexpr))
1402 return cexpr->opval.val;
1406 werror (E_CONST_EXPECTED, "found expression");
1411 /* return the value */
1412 return cexpr->opval.val;
1416 /*-----------------------------------------------------------------*/
1417 /* isLabelInAst - will return true if a given label is found */
1418 /*-----------------------------------------------------------------*/
1420 isLabelInAst (symbol * label, ast * tree)
1422 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1425 if (IS_AST_OP (tree) &&
1426 tree->opval.op == LABEL &&
1427 isSymbolEqual (AST_SYMBOL (tree->left), label))
1430 return isLabelInAst (label, tree->right) &&
1431 isLabelInAst (label, tree->left);
1435 /*-----------------------------------------------------------------*/
1436 /* isLoopCountable - return true if the loop count can be determi- */
1437 /* -ned at compile time . */
1438 /*-----------------------------------------------------------------*/
1440 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1441 symbol ** sym, ast ** init, ast ** end)
1444 /* the loop is considered countable if the following
1445 conditions are true :-
1447 a) initExpr :- <sym> = <const>
1448 b) condExpr :- <sym> < <const1>
1449 c) loopExpr :- <sym> ++
1452 /* first check the initExpr */
1453 if (IS_AST_OP (initExpr) &&
1454 initExpr->opval.op == '=' && /* is assignment */
1455 IS_AST_SYM_VALUE (initExpr->left))
1456 { /* left is a symbol */
1458 *sym = AST_SYMBOL (initExpr->left);
1459 *init = initExpr->right;
1464 /* for now the symbol has to be of
1466 if (!IS_INTEGRAL ((*sym)->type))
1469 /* now check condExpr */
1470 if (IS_AST_OP (condExpr))
1473 switch (condExpr->opval.op)
1476 if (IS_AST_SYM_VALUE (condExpr->left) &&
1477 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1478 IS_AST_LIT_VALUE (condExpr->right))
1480 *end = condExpr->right;
1486 if (IS_AST_OP (condExpr->left) &&
1487 condExpr->left->opval.op == '>' &&
1488 IS_AST_LIT_VALUE (condExpr->left->right) &&
1489 IS_AST_SYM_VALUE (condExpr->left->left) &&
1490 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1493 *end = newNode ('+', condExpr->left->right,
1494 newAst_VALUE (constVal ("1")));
1505 /* check loop expression is of the form <sym>++ */
1506 if (!IS_AST_OP (loopExpr))
1509 /* check if <sym> ++ */
1510 if (loopExpr->opval.op == INC_OP)
1516 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1517 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1524 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1525 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1533 if (loopExpr->opval.op == ADD_ASSIGN)
1536 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1537 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1538 IS_AST_LIT_VALUE (loopExpr->right) &&
1539 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1547 /*-----------------------------------------------------------------*/
1548 /* astHasVolatile - returns true if ast contains any volatile */
1549 /*-----------------------------------------------------------------*/
1551 astHasVolatile (ast * tree)
1556 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1559 if (IS_AST_OP (tree))
1560 return astHasVolatile (tree->left) ||
1561 astHasVolatile (tree->right);
1566 /*-----------------------------------------------------------------*/
1567 /* astHasPointer - return true if the ast contains any ptr variable */
1568 /*-----------------------------------------------------------------*/
1570 astHasPointer (ast * tree)
1575 if (IS_AST_LINK (tree))
1578 /* if we hit an array expression then check
1579 only the left side */
1580 if (IS_AST_OP (tree) && tree->opval.op == '[')
1581 return astHasPointer (tree->left);
1583 if (IS_AST_VALUE (tree))
1584 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1586 return astHasPointer (tree->left) ||
1587 astHasPointer (tree->right);
1591 /*-----------------------------------------------------------------*/
1592 /* astHasSymbol - return true if the ast has the given symbol */
1593 /*-----------------------------------------------------------------*/
1595 astHasSymbol (ast * tree, symbol * sym)
1597 if (!tree || IS_AST_LINK (tree))
1600 if (IS_AST_VALUE (tree))
1602 if (IS_AST_SYM_VALUE (tree))
1603 return isSymbolEqual (AST_SYMBOL (tree), sym);
1608 return astHasSymbol (tree->left, sym) ||
1609 astHasSymbol (tree->right, sym);
1612 /*-----------------------------------------------------------------*/
1613 /* astHasDeref - return true if the ast has an indirect access */
1614 /*-----------------------------------------------------------------*/
1616 astHasDeref (ast * tree)
1618 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1621 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1623 return astHasDeref (tree->left) || astHasDeref (tree->right);
1626 /*-----------------------------------------------------------------*/
1627 /* isConformingBody - the loop body has to conform to a set of rules */
1628 /* for the loop to be considered reversible read on for rules */
1629 /*-----------------------------------------------------------------*/
1631 isConformingBody (ast * pbody, symbol * sym, ast * body)
1634 /* we are going to do a pre-order traversal of the
1635 tree && check for the following conditions. (essentially
1636 a set of very shallow tests )
1637 a) the sym passed does not participate in
1638 any arithmetic operation
1639 b) There are no function calls
1640 c) all jumps are within the body
1641 d) address of loop control variable not taken
1642 e) if an assignment has a pointer on the
1643 left hand side make sure right does not have
1644 loop control variable */
1646 /* if we reach the end or a leaf then true */
1647 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1650 /* if anything else is "volatile" */
1651 if (IS_VOLATILE (TETYPE (pbody)))
1654 /* we will walk the body in a pre-order traversal for
1656 switch (pbody->opval.op)
1658 /*------------------------------------------------------------------*/
1660 // if the loopvar is used as an index
1661 if (astHasSymbol(pbody->right, sym)) {
1664 return isConformingBody (pbody->right, sym, body);
1666 /*------------------------------------------------------------------*/
1671 /*------------------------------------------------------------------*/
1675 /* sure we are not sym is not modified */
1677 IS_AST_SYM_VALUE (pbody->left) &&
1678 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1682 IS_AST_SYM_VALUE (pbody->right) &&
1683 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1688 /*------------------------------------------------------------------*/
1690 case '*': /* can be unary : if right is null then unary operation */
1695 /* if right is NULL then unary operation */
1696 /*------------------------------------------------------------------*/
1697 /*----------------------------*/
1699 /*----------------------------*/
1702 if (IS_AST_SYM_VALUE (pbody->left) &&
1703 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1706 return isConformingBody (pbody->left, sym, body);
1710 if (astHasSymbol (pbody->left, sym) ||
1711 astHasSymbol (pbody->right, sym))
1716 /*------------------------------------------------------------------*/
1724 if (IS_AST_SYM_VALUE (pbody->left) &&
1725 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1728 if (IS_AST_SYM_VALUE (pbody->right) &&
1729 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1732 return isConformingBody (pbody->left, sym, body) &&
1733 isConformingBody (pbody->right, sym, body);
1741 if (IS_AST_SYM_VALUE (pbody->left) &&
1742 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1744 return isConformingBody (pbody->left, sym, body);
1746 /*------------------------------------------------------------------*/
1758 case SIZEOF: /* evaluate wihout code generation */
1760 if (IS_AST_SYM_VALUE (pbody->left) &&
1761 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1764 if (IS_AST_SYM_VALUE (pbody->right) &&
1765 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1768 return isConformingBody (pbody->left, sym, body) &&
1769 isConformingBody (pbody->right, sym, body);
1771 /*------------------------------------------------------------------*/
1774 /* if left has a pointer & right has loop
1775 control variable then we cannot */
1776 if (astHasPointer (pbody->left) &&
1777 astHasSymbol (pbody->right, sym))
1779 if (astHasVolatile (pbody->left))
1782 if (IS_AST_SYM_VALUE (pbody->left)) {
1783 // if the loopvar has an assignment
1784 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1786 // if the loopvar is used in another (maybe conditional) block
1787 if (astHasSymbol (pbody->right, sym) &&
1788 (pbody->level >= body->level)) {
1793 if (astHasVolatile (pbody->left))
1796 if (astHasDeref(pbody->right)) return FALSE;
1798 return isConformingBody (pbody->left, sym, body) &&
1799 isConformingBody (pbody->right, sym, body);
1810 assert ("Parser should not have generated this\n");
1812 /*------------------------------------------------------------------*/
1813 /*----------------------------*/
1814 /* comma operator */
1815 /*----------------------------*/
1817 return isConformingBody (pbody->left, sym, body) &&
1818 isConformingBody (pbody->right, sym, body);
1820 /*------------------------------------------------------------------*/
1821 /*----------------------------*/
1823 /*----------------------------*/
1825 /* if local & not passed as paramater then ok */
1826 if (sym->level && !astHasSymbol(pbody->right,sym))
1830 /*------------------------------------------------------------------*/
1831 /*----------------------------*/
1832 /* return statement */
1833 /*----------------------------*/
1838 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1843 if (astHasSymbol (pbody->left, sym))
1850 return isConformingBody (pbody->left, sym, body) &&
1851 isConformingBody (pbody->right, sym, body);
1857 /*-----------------------------------------------------------------*/
1858 /* isLoopReversible - takes a for loop as input && returns true */
1859 /* if the for loop is reversible. If yes will set the value of */
1860 /* the loop control var & init value & termination value */
1861 /*-----------------------------------------------------------------*/
1863 isLoopReversible (ast * loop, symbol ** loopCntrl,
1864 ast ** init, ast ** end)
1866 /* if option says don't do it then don't */
1867 if (optimize.noLoopReverse)
1869 /* there are several tests to determine this */
1871 /* for loop has to be of the form
1872 for ( <sym> = <const1> ;
1873 [<sym> < <const2>] ;
1874 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1876 if (!isLoopCountable (AST_FOR (loop, initExpr),
1877 AST_FOR (loop, condExpr),
1878 AST_FOR (loop, loopExpr),
1879 loopCntrl, init, end))
1882 /* now do some serious checking on the body of the loop
1885 return isConformingBody (loop->left, *loopCntrl, loop->left);
1889 /*-----------------------------------------------------------------*/
1890 /* replLoopSym - replace the loop sym by loop sym -1 */
1891 /*-----------------------------------------------------------------*/
1893 replLoopSym (ast * body, symbol * sym)
1896 if (!body || IS_AST_LINK (body))
1899 if (IS_AST_SYM_VALUE (body))
1902 if (isSymbolEqual (AST_SYMBOL (body), sym))
1906 body->opval.op = '-';
1907 body->left = newAst_VALUE (symbolVal (sym));
1908 body->right = newAst_VALUE (constVal ("1"));
1916 replLoopSym (body->left, sym);
1917 replLoopSym (body->right, sym);
1921 /*-----------------------------------------------------------------*/
1922 /* reverseLoop - do the actual loop reversal */
1923 /*-----------------------------------------------------------------*/
1925 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1929 /* create the following tree
1934 if (sym) goto for_continue ;
1937 /* put it together piece by piece */
1938 rloop = newNode (NULLOP,
1939 createIf (newAst_VALUE (symbolVal (sym)),
1941 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1944 newAst_VALUE (symbolVal (sym)),
1947 replLoopSym (loop->left, sym);
1948 setAstLineno (rloop, init->lineno);
1950 rloop = newNode (NULLOP,
1952 newAst_VALUE (symbolVal (sym)),
1953 newNode ('-', end, init)),
1954 createLabel (AST_FOR (loop, continueLabel),
1958 newNode (SUB_ASSIGN,
1959 newAst_VALUE (symbolVal (sym)),
1960 newAst_VALUE (constVal ("1"))),
1963 rloop->lineno=init->lineno;
1964 return decorateType (rloop, RESULT_CHECK);
1968 /*-----------------------------------------------------------------*/
1969 /* searchLitOp - search tree (*ops only) for an ast with literal */
1970 /*-----------------------------------------------------------------*/
1972 searchLitOp (ast *tree, ast **parent, const char *ops)
1976 if (tree && optimize.global_cse)
1978 /* is there a literal operand? */
1980 IS_AST_OP(tree->right) &&
1981 tree->right->right &&
1982 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
1984 if (IS_LITERAL (RTYPE (tree->right)) !=
1985 IS_LITERAL (LTYPE (tree->right)))
1987 tree->right->decorated = 0;
1988 tree->decorated = 0;
1992 ret = searchLitOp (tree->right, parent, ops);
1997 IS_AST_OP(tree->left) &&
1998 tree->left->right &&
1999 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
2001 if (IS_LITERAL (RTYPE (tree->left)) !=
2002 IS_LITERAL (LTYPE (tree->left)))
2004 tree->left->decorated = 0;
2005 tree->decorated = 0;
2009 ret = searchLitOp (tree->left, parent, ops);
2017 /*-----------------------------------------------------------------*/
2018 /* getResultFromType */
2019 /*-----------------------------------------------------------------*/
2021 getResultTypeFromType (sym_link *type)
2023 /* type = getSpec (type); */
2025 return RESULT_TYPE_BIT;
2026 if (IS_BITFIELD (type))
2028 int blen = SPEC_BLEN (type);
2031 return RESULT_TYPE_BIT;
2033 return RESULT_TYPE_CHAR;
2034 return RESULT_TYPE_INT;
2037 return RESULT_TYPE_CHAR;
2040 return RESULT_TYPE_INT;
2041 return RESULT_TYPE_OTHER;
2044 /*-----------------------------------------------------------------*/
2045 /* addCast - adds casts to a type specified by RESULT_TYPE */
2046 /*-----------------------------------------------------------------*/
2048 addCast (ast *tree, RESULT_TYPE resultType, bool upcast)
2051 bool upCasted = FALSE;
2055 case RESULT_TYPE_NONE:
2056 /* char: promote to int */
2058 getSize (tree->etype) >= INTSIZE)
2060 newLink = newIntLink();
2063 case RESULT_TYPE_CHAR:
2064 if (IS_CHAR (tree->etype))
2066 newLink = newCharLink();
2068 case RESULT_TYPE_INT:
2070 if (getSize (tree->etype) > INTSIZE)
2072 /* warn ("Loosing significant digits"); */
2076 /* char: promote to int */
2078 getSize (tree->etype) >= INTSIZE)
2080 newLink = newIntLink();
2083 case RESULT_TYPE_OTHER:
2086 /* return type is long, float: promote char to int */
2087 if (getSize (tree->etype) >= INTSIZE)
2089 newLink = newIntLink();
2095 tree->decorated = 0;
2096 tree = newNode (CAST, newAst_LINK (newLink), tree);
2097 tree->lineno = tree->right->lineno;
2098 /* keep unsigned type during cast to smaller type,
2099 but not when promoting from char to int */
2101 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2102 return decorateType (tree, resultType);
2105 /*-----------------------------------------------------------------*/
2106 /* resultTypePropagate - decides if resultType can be propagated */
2107 /*-----------------------------------------------------------------*/
2109 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2111 switch (tree->opval.op)
2127 return RESULT_TYPE_NONE;
2131 return RESULT_TYPE_IFX;
2133 return RESULT_TYPE_NONE;
2137 /*-----------------------------------------------------------------*/
2138 /* getLeftResultType - gets type from left branch for propagation */
2139 /*-----------------------------------------------------------------*/
2141 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2143 switch (tree->opval.op)
2147 if (IS_PTR (LTYPE (tree)))
2148 return RESULT_TYPE_NONE;
2150 return getResultTypeFromType (LETYPE (tree));
2152 if (IS_PTR (currFunc->type->next))
2153 return RESULT_TYPE_NONE;
2155 return getResultTypeFromType (currFunc->type->next);
2157 if (!IS_ARRAY (LTYPE (tree)))
2159 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 256)
2160 return RESULT_TYPE_CHAR;
2167 /*--------------------------------------------------------------------*/
2168 /* decorateType - compute type for this tree, also does type checking.*/
2169 /* This is done bottom up, since type has to flow upwards. */
2170 /* resultType flows top-down and forces e.g. char-arithmetik, if the */
2171 /* result is a char and the operand(s) are int's. */
2172 /* It also does constant folding, and parameter checking. */
2173 /*--------------------------------------------------------------------*/
2175 decorateType (ast * tree, RESULT_TYPE resultType)
2179 RESULT_TYPE resultTypeProp;
2184 /* if already has type then do nothing */
2185 if (tree->decorated)
2188 tree->decorated = 1;
2191 /* print the line */
2192 /* if not block & function */
2193 if (tree->type == EX_OP &&
2194 (tree->opval.op != FUNCTION &&
2195 tree->opval.op != BLOCK &&
2196 tree->opval.op != NULLOP))
2198 filename = tree->filename;
2199 lineno = tree->lineno;
2203 /* if any child is an error | this one is an error do nothing */
2204 if (tree->isError ||
2205 (tree->left && tree->left->isError) ||
2206 (tree->right && tree->right->isError))
2209 /*------------------------------------------------------------------*/
2210 /*----------------------------*/
2211 /* leaf has been reached */
2212 /*----------------------------*/
2213 lineno=tree->lineno;
2214 /* if this is of type value */
2215 /* just get the type */
2216 if (tree->type == EX_VALUE)
2219 if (IS_LITERAL (tree->opval.val->etype))
2222 /* if this is a character array then declare it */
2223 if (IS_ARRAY (tree->opval.val->type))
2224 tree->opval.val = stringToSymbol (tree->opval.val);
2226 /* otherwise just copy the type information */
2227 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2231 if (tree->opval.val->sym)
2233 /* if the undefined flag is set then give error message */
2234 if (tree->opval.val->sym->undefined)
2236 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2238 TTYPE (tree) = TETYPE (tree) =
2239 tree->opval.val->type = tree->opval.val->sym->type =
2240 tree->opval.val->etype = tree->opval.val->sym->etype =
2241 copyLinkChain (INTTYPE);
2246 /* if impilicit i.e. struct/union member then no type */
2247 if (tree->opval.val->sym->implicit)
2248 TTYPE (tree) = TETYPE (tree) = NULL;
2253 /* else copy the type */
2254 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2256 /* and mark it as referenced */
2257 tree->opval.val->sym->isref = 1;
2265 /* if type link for the case of cast */
2266 if (tree->type == EX_LINK)
2268 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2276 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2278 if (tree->left && tree->left->type == EX_OPERAND
2279 && (tree->left->opval.op == INC_OP
2280 || tree->left->opval.op == DEC_OP)
2281 && tree->left->left)
2283 tree->left->right = tree->left->left;
2284 tree->left->left = NULL;
2286 if (tree->right && tree->right->type == EX_OPERAND
2287 && (tree->right->opval.op == INC_OP
2288 || tree->right->opval.op == DEC_OP)
2289 && tree->right->left)
2291 tree->right->right = tree->right->left;
2292 tree->right->left = NULL;
2297 /* Before decorating the left branch we've to decide in dependence
2298 upon tree->opval.op, if resultType can be propagated */
2299 resultTypeProp = resultTypePropagate (tree, resultType);
2301 if (tree->opval.op == '?')
2302 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2304 dtl = decorateType (tree->left, resultTypeProp);
2306 /* if an array node, we may need to swap branches */
2307 if (tree->opval.op == '[')
2309 /* determine which is the array & which the index */
2310 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2311 IS_INTEGRAL (LTYPE (tree)))
2313 ast *tempTree = tree->left;
2314 tree->left = tree->right;
2315 tree->right = tempTree;
2319 /* After decorating the left branch there's type information available
2320 in tree->left->?type. If the op is e.g. '=' we extract the type
2321 information from there and propagate it to the right branch. */
2322 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2324 switch (tree->opval.op)
2327 /* delay right side for '?' operator since conditional macro
2328 expansions might rely on this */
2332 /* decorate right side for CALL (parameter list) in processParms();
2333 there is resultType available */
2337 dtr = decorateType (tree->right, resultTypeProp);
2341 /* this is to take care of situations
2342 when the tree gets rewritten */
2343 if (dtl != tree->left)
2345 if (dtr != tree->right)
2347 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2351 /* depending on type of operator do */
2353 switch (tree->opval.op)
2355 /*------------------------------------------------------------------*/
2356 /*----------------------------*/
2358 /*----------------------------*/
2361 /* first check if this is a array or a pointer */
2362 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2364 werror (E_NEED_ARRAY_PTR, "[]");
2365 goto errorTreeReturn;
2368 /* check if the type of the idx */
2369 if (!IS_INTEGRAL (RTYPE (tree)))
2371 werror (E_IDX_NOT_INT);
2372 goto errorTreeReturn;
2375 /* if the left is an rvalue then error */
2378 werror (E_LVALUE_REQUIRED, "array access");
2379 goto errorTreeReturn;
2382 if (IS_LITERAL (RTYPE (tree)))
2384 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2385 int arraySize = DCL_ELEM (LTYPE (tree));
2386 if (arraySize && arrayIndex >= arraySize)
2388 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2393 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2396 /*------------------------------------------------------------------*/
2397 /*----------------------------*/
2399 /*----------------------------*/
2401 /* if this is not a structure */
2402 if (!IS_STRUCT (LTYPE (tree)))
2404 werror (E_STRUCT_UNION, ".");
2405 goto errorTreeReturn;
2407 TTYPE (tree) = structElemType (LTYPE (tree),
2408 (tree->right->type == EX_VALUE ?
2409 tree->right->opval.val : NULL));
2410 TETYPE (tree) = getSpec (TTYPE (tree));
2413 /*------------------------------------------------------------------*/
2414 /*----------------------------*/
2415 /* struct/union pointer */
2416 /*----------------------------*/
2418 /* if not pointer to a structure */
2419 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2421 werror (E_PTR_REQD);
2422 goto errorTreeReturn;
2425 if (!IS_STRUCT (LTYPE (tree)->next))
2427 werror (E_STRUCT_UNION, "->");
2428 goto errorTreeReturn;
2431 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2432 (tree->right->type == EX_VALUE ?
2433 tree->right->opval.val : NULL));
2434 TETYPE (tree) = getSpec (TTYPE (tree));
2436 /* adjust the storage class */
2437 switch (DCL_TYPE(tree->left->ftype)) {
2439 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2442 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2445 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2448 SPEC_SCLS (TETYPE (tree)) = 0;
2451 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2454 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2457 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2460 SPEC_SCLS (TETYPE (tree)) = 0;
2467 /* This breaks with extern declarations, bitfields, and perhaps other */
2468 /* cases (gcse). Let's leave this optimization disabled for now and */
2469 /* ponder if there's a safe way to do this. -- EEP */
2471 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2472 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2474 /* If defined struct type at addr var
2475 then rewrite (&struct var)->member
2477 and define membertype at (addr+offsetof(struct var,member)) temp
2480 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2481 AST_SYMBOL(tree->right));
2483 sym = newSymbol(genSymName (0), 0);
2484 sym->type = TTYPE (tree);
2485 sym->etype = getSpec(sym->type);
2486 sym->lineDef = tree->lineno;
2489 SPEC_STAT (sym->etype) = 1;
2490 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2492 SPEC_ABSA(sym->etype) = 1;
2493 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2496 AST_VALUE (tree) = symbolVal(sym);
2499 tree->type = EX_VALUE;
2507 /*------------------------------------------------------------------*/
2508 /*----------------------------*/
2509 /* ++/-- operation */
2510 /*----------------------------*/
2514 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2515 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2516 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2517 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2526 /*------------------------------------------------------------------*/
2527 /*----------------------------*/
2529 /*----------------------------*/
2530 case '&': /* can be unary */
2531 /* if right is NULL then unary operation */
2532 if (tree->right) /* not an unary operation */
2535 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2537 werror (E_BITWISE_OP);
2538 werror (W_CONTINUE, "left & right types are ");
2539 printTypeChain (LTYPE (tree), stderr);
2540 fprintf (stderr, ",");
2541 printTypeChain (RTYPE (tree), stderr);
2542 fprintf (stderr, "\n");
2543 goto errorTreeReturn;
2546 /* if they are both literal */
2547 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2549 tree->type = EX_VALUE;
2550 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2551 valFromType (RETYPE (tree)), '&');
2553 tree->right = tree->left = NULL;
2554 TETYPE (tree) = tree->opval.val->etype;
2555 TTYPE (tree) = tree->opval.val->type;
2559 /* see if this is a GETHBIT operation if yes
2562 ast *otree = optimizeGetHbit (tree);
2565 return decorateType (otree, RESULT_CHECK);
2568 TTYPE (tree) = computeType (LTYPE (tree),
2572 TETYPE (tree) = getSpec (TTYPE (tree));
2574 /* if left is a literal exchange left & right */
2575 if (IS_LITERAL (LTYPE (tree)))
2577 ast *tTree = tree->left;
2578 tree->left = tree->right;
2579 tree->right = tTree;
2582 /* if right is a literal and */
2583 /* we can find a 2nd literal in a and-tree then */
2584 /* rearrange the tree */
2585 if (IS_LITERAL (RTYPE (tree)))
2588 ast *litTree = searchLitOp (tree, &parent, "&");
2592 ast *tTree = litTree->left;
2593 litTree->left = tree->right;
2594 tree->right = tTree;
2595 /* both operands in tTree are literal now */
2596 decorateType (parent, resultType);
2600 LRVAL (tree) = RRVAL (tree) = 1;
2605 /*------------------------------------------------------------------*/
2606 /*----------------------------*/
2608 /*----------------------------*/
2609 p = newLink (DECLARATOR);
2610 /* if bit field then error */
2611 if (IS_BITVAR (tree->left->etype))
2613 werror (E_ILLEGAL_ADDR, "address of bit variable");
2614 goto errorTreeReturn;
2617 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2619 werror (E_ILLEGAL_ADDR, "address of register variable");
2620 goto errorTreeReturn;
2623 if (IS_FUNC (LTYPE (tree)))
2625 // this ought to be ignored
2626 return (tree->left);
2629 if (IS_LITERAL(LTYPE(tree)))
2631 werror (E_ILLEGAL_ADDR, "address of literal");
2632 goto errorTreeReturn;
2637 werror (E_LVALUE_REQUIRED, "address of");
2638 goto errorTreeReturn;
2641 DCL_TYPE (p) = POINTER;
2642 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2643 DCL_TYPE (p) = CPOINTER;
2644 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2645 DCL_TYPE (p) = FPOINTER;
2646 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2647 DCL_TYPE (p) = PPOINTER;
2648 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2649 DCL_TYPE (p) = IPOINTER;
2650 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2651 DCL_TYPE (p) = EEPPOINTER;
2652 else if (SPEC_OCLS(tree->left->etype))
2653 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2655 DCL_TYPE (p) = POINTER;
2657 if (IS_AST_SYM_VALUE (tree->left))
2659 AST_SYMBOL (tree->left)->addrtaken = 1;
2660 AST_SYMBOL (tree->left)->allocreq = 1;
2663 p->next = LTYPE (tree);
2665 TETYPE (tree) = getSpec (TTYPE (tree));
2670 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2671 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2673 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2674 AST_SYMBOL(tree->left->right));
2675 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2676 valueFromLit(element->offset));
2679 tree->type = EX_VALUE;
2680 tree->values.literalFromCast = 1;
2686 /*------------------------------------------------------------------*/
2687 /*----------------------------*/
2689 /*----------------------------*/
2691 /* if the rewrite succeeds then don't go any furthur */
2693 ast *wtree = optimizeRRCRLC (tree);
2695 return decorateType (wtree, RESULT_CHECK);
2697 wtree = optimizeSWAP (tree);
2699 return decorateType (wtree, RESULT_CHECK);
2702 /* if left is a literal exchange left & right */
2703 if (IS_LITERAL (LTYPE (tree)))
2705 ast *tTree = tree->left;
2706 tree->left = tree->right;
2707 tree->right = tTree;
2710 /* if right is a literal and */
2711 /* we can find a 2nd literal in a or-tree then */
2712 /* rearrange the tree */
2713 if (IS_LITERAL (RTYPE (tree)))
2716 ast *litTree = searchLitOp (tree, &parent, "|");
2720 ast *tTree = litTree->left;
2721 litTree->left = tree->right;
2722 tree->right = tTree;
2723 /* both operands in tTree are literal now */
2724 decorateType (parent, resultType);
2729 /*------------------------------------------------------------------*/
2730 /*----------------------------*/
2732 /*----------------------------*/
2734 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2736 werror (E_BITWISE_OP);
2737 werror (W_CONTINUE, "left & right types are ");
2738 printTypeChain (LTYPE (tree), stderr);
2739 fprintf (stderr, ",");
2740 printTypeChain (RTYPE (tree), stderr);
2741 fprintf (stderr, "\n");
2742 goto errorTreeReturn;
2745 /* if they are both literal then */
2746 /* rewrite the tree */
2747 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2749 tree->type = EX_VALUE;
2750 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2751 valFromType (RETYPE (tree)),
2753 tree->right = tree->left = NULL;
2754 TETYPE (tree) = tree->opval.val->etype;
2755 TTYPE (tree) = tree->opval.val->type;
2759 /* if left is a literal exchange left & right */
2760 if (IS_LITERAL (LTYPE (tree)))
2762 ast *tTree = tree->left;
2763 tree->left = tree->right;
2764 tree->right = tTree;
2767 /* if right is a literal and */
2768 /* we can find a 2nd literal in a xor-tree then */
2769 /* rearrange the tree */
2770 if (IS_LITERAL (RTYPE (tree)) &&
2771 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
2774 ast *litTree = searchLitOp (tree, &parent, "^");
2778 ast *tTree = litTree->left;
2779 litTree->left = tree->right;
2780 tree->right = tTree;
2781 /* both operands in litTree are literal now */
2782 decorateType (parent, resultType);
2786 LRVAL (tree) = RRVAL (tree) = 1;
2787 TETYPE (tree) = getSpec (TTYPE (tree) =
2788 computeType (LTYPE (tree),
2795 /*------------------------------------------------------------------*/
2796 /*----------------------------*/
2798 /*----------------------------*/
2800 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2802 werror (E_INVALID_OP, "divide");
2803 goto errorTreeReturn;
2805 /* if they are both literal then */
2806 /* rewrite the tree */
2807 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2809 tree->type = EX_VALUE;
2810 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2811 valFromType (RETYPE (tree)));
2812 tree->right = tree->left = NULL;
2813 TETYPE (tree) = getSpec (TTYPE (tree) =
2814 tree->opval.val->type);
2818 LRVAL (tree) = RRVAL (tree) = 1;
2820 TETYPE (tree) = getSpec (TTYPE (tree) =
2821 computeType (LTYPE (tree),
2826 /* if right is a literal and */
2827 /* left is also a division by a literal then */
2828 /* rearrange the tree */
2829 if (IS_LITERAL (RTYPE (tree))
2830 /* avoid infinite loop */
2831 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
2834 ast *litTree = searchLitOp (tree, &parent, "/");
2837 if (IS_LITERAL (RTYPE (litTree)))
2841 litTree->right = newNode ('*',
2843 copyAst (tree->right));
2844 litTree->right->lineno = tree->lineno;
2846 tree->right->opval.val = constVal ("1");
2847 decorateType (parent, resultType);
2851 /* litTree->left is literal: no gcse possible.
2852 We can't call decorateType(parent, RESULT_CHECK), because
2853 this would cause an infinit loop. */
2854 parent->decorated = 1;
2855 decorateType (litTree, resultType);
2862 /*------------------------------------------------------------------*/
2863 /*----------------------------*/
2865 /*----------------------------*/
2867 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2869 werror (E_BITWISE_OP);
2870 werror (W_CONTINUE, "left & right types are ");
2871 printTypeChain (LTYPE (tree), stderr);
2872 fprintf (stderr, ",");
2873 printTypeChain (RTYPE (tree), stderr);
2874 fprintf (stderr, "\n");
2875 goto errorTreeReturn;
2877 /* if they are both literal then */
2878 /* rewrite the tree */
2879 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2881 tree->type = EX_VALUE;
2882 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2883 valFromType (RETYPE (tree)));
2884 tree->right = tree->left = NULL;
2885 TETYPE (tree) = getSpec (TTYPE (tree) =
2886 tree->opval.val->type);
2889 LRVAL (tree) = RRVAL (tree) = 1;
2890 TETYPE (tree) = getSpec (TTYPE (tree) =
2891 computeType (LTYPE (tree),
2897 /*------------------------------------------------------------------*/
2898 /*----------------------------*/
2899 /* address dereference */
2900 /*----------------------------*/
2901 case '*': /* can be unary : if right is null then unary operation */
2904 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2906 werror (E_PTR_REQD);
2907 goto errorTreeReturn;
2912 werror (E_LVALUE_REQUIRED, "pointer deref");
2913 goto errorTreeReturn;
2915 if (IS_ADDRESS_OF_OP(tree->left))
2917 /* replace *&obj with obj */
2918 return tree->left->left;
2920 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
2921 TETYPE (tree) = getSpec (TTYPE (tree));
2922 /* adjust the storage class */
2923 switch (DCL_TYPE(tree->left->ftype)) {
2925 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2928 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2931 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2934 SPEC_SCLS (TETYPE (tree)) = 0;
2937 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2940 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2943 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2946 SPEC_SCLS (TETYPE (tree)) = 0;
2955 /*------------------------------------------------------------------*/
2956 /*----------------------------*/
2957 /* multiplication */
2958 /*----------------------------*/
2959 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2961 werror (E_INVALID_OP, "multiplication");
2962 goto errorTreeReturn;
2965 /* if they are both literal then */
2966 /* rewrite the tree */
2967 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2969 tree->type = EX_VALUE;
2970 tree->opval.val = valMult (valFromType (LETYPE (tree)),
2971 valFromType (RETYPE (tree)));
2972 tree->right = tree->left = NULL;
2973 TETYPE (tree) = getSpec (TTYPE (tree) =
2974 tree->opval.val->type);
2978 /* if left is a literal exchange left & right */
2979 if (IS_LITERAL (LTYPE (tree)))
2981 ast *tTree = tree->left;
2982 tree->left = tree->right;
2983 tree->right = tTree;
2986 /* if right is a literal and */
2987 /* we can find a 2nd literal in a mul-tree then */
2988 /* rearrange the tree */
2989 if (IS_LITERAL (RTYPE (tree)))
2992 ast *litTree = searchLitOp (tree, &parent, "*");
2996 ast *tTree = litTree->left;
2997 litTree->left = tree->right;
2998 tree->right = tTree;
2999 /* both operands in litTree are literal now */
3000 decorateType (parent, resultType);
3004 LRVAL (tree) = RRVAL (tree) = 1;
3005 tree->left = addCast (tree->left, resultType, FALSE);
3006 tree->right = addCast (tree->right, resultType, FALSE);
3007 TETYPE (tree) = getSpec (TTYPE (tree) =
3008 computeType (LTYPE (tree),
3015 /*------------------------------------------------------------------*/
3016 /*----------------------------*/
3017 /* unary '+' operator */
3018 /*----------------------------*/
3023 if (!IS_ARITHMETIC (LTYPE (tree)))
3025 werror (E_UNARY_OP, '+');
3026 goto errorTreeReturn;
3029 /* if left is a literal then do it */
3030 if (IS_LITERAL (LTYPE (tree)))
3032 tree->type = EX_VALUE;
3033 tree->opval.val = valFromType (LETYPE (tree));
3035 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3039 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3043 /*------------------------------------------------------------------*/
3044 /*----------------------------*/
3046 /*----------------------------*/
3048 /* this is not a unary operation */
3049 /* if both pointers then problem */
3050 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3051 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3053 werror (E_PTR_PLUS_PTR);
3054 goto errorTreeReturn;
3057 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3058 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3060 werror (E_PLUS_INVALID, "+");
3061 goto errorTreeReturn;
3064 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3065 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3067 werror (E_PLUS_INVALID, "+");
3068 goto errorTreeReturn;
3070 /* if they are both literal then */
3071 /* rewrite the tree */
3072 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3074 tree->type = EX_VALUE;
3075 tree->left = addCast (tree->left, resultType, TRUE);
3076 tree->right = addCast (tree->right, resultType, TRUE);
3077 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3078 valFromType (RETYPE (tree)));
3079 tree->right = tree->left = NULL;
3080 TETYPE (tree) = getSpec (TTYPE (tree) =
3081 tree->opval.val->type);
3085 /* if the right is a pointer or left is a literal
3086 xchange left & right */
3087 if (IS_ARRAY (RTYPE (tree)) ||
3088 IS_PTR (RTYPE (tree)) ||
3089 IS_LITERAL (LTYPE (tree)))
3091 ast *tTree = tree->left;
3092 tree->left = tree->right;
3093 tree->right = tTree;
3096 /* if right is a literal and */
3097 /* left is also an addition/subtraction with a literal then */
3098 /* rearrange the tree */
3099 if (IS_LITERAL (RTYPE (tree)))
3101 ast *litTree, *parent;
3102 litTree = searchLitOp (tree, &parent, "+-");
3105 if (litTree->opval.op == '+')
3109 ast *tTree = litTree->left;
3110 litTree->left = tree->right;
3111 tree->right = tree->left;
3114 else if (litTree->opval.op == '-')
3116 if (IS_LITERAL (RTYPE (litTree)))
3120 ast *tTree = litTree->left;
3121 litTree->left = tree->right;
3122 tree->right = tTree;
3128 ast *tTree = litTree->right;
3129 litTree->right = tree->right;
3130 tree->right = tTree;
3131 litTree->opval.op = '+';
3132 tree->opval.op = '-';
3135 decorateType (parent, resultType);
3139 LRVAL (tree) = RRVAL (tree) = 1;
3140 /* if the left is a pointer */
3141 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3142 TETYPE (tree) = getSpec (TTYPE (tree) =
3146 tree->left = addCast (tree->left, resultType, TRUE);
3147 tree->right = addCast (tree->right, resultType, TRUE);
3148 TETYPE (tree) = getSpec (TTYPE (tree) =
3149 computeType (LTYPE (tree),
3157 /*------------------------------------------------------------------*/
3158 /*----------------------------*/
3160 /*----------------------------*/
3161 case '-': /* can be unary */
3162 /* if right is null then unary */
3166 if (!IS_ARITHMETIC (LTYPE (tree)))
3168 werror (E_UNARY_OP, tree->opval.op);
3169 goto errorTreeReturn;
3172 /* if left is a literal then do it */
3173 if (IS_LITERAL (LTYPE (tree)))
3175 tree->type = EX_VALUE;
3176 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3178 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3179 SPEC_USIGN(TETYPE(tree)) = 0;
3183 TETYPE(tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3187 /*------------------------------------------------------------------*/
3188 /*----------------------------*/
3190 /*----------------------------*/
3192 if (!(IS_PTR (LTYPE (tree)) ||
3193 IS_ARRAY (LTYPE (tree)) ||
3194 IS_ARITHMETIC (LTYPE (tree))))
3196 werror (E_PLUS_INVALID, "-");
3197 goto errorTreeReturn;
3200 if (!(IS_PTR (RTYPE (tree)) ||
3201 IS_ARRAY (RTYPE (tree)) ||
3202 IS_ARITHMETIC (RTYPE (tree))))
3204 werror (E_PLUS_INVALID, "-");
3205 goto errorTreeReturn;
3208 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3209 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3210 IS_INTEGRAL (RTYPE (tree))))
3212 werror (E_PLUS_INVALID, "-");
3213 goto errorTreeReturn;
3216 /* if they are both literal then */
3217 /* rewrite the tree */
3218 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3220 tree->type = EX_VALUE;
3221 tree->left = addCast (tree->left, resultType, TRUE);
3222 tree->right = addCast (tree->right, resultType, TRUE);
3223 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3224 valFromType (RETYPE (tree)));
3225 tree->right = tree->left = NULL;
3226 TETYPE (tree) = getSpec (TTYPE (tree) =
3227 tree->opval.val->type);
3231 /* if the left & right are equal then zero */
3232 if (isAstEqual (tree->left, tree->right))
3234 tree->type = EX_VALUE;
3235 tree->left = tree->right = NULL;
3236 tree->opval.val = constVal ("0");
3237 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3241 /* if both of them are pointers or arrays then */
3242 /* the result is going to be an integer */
3243 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3244 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3245 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3247 /* if only the left is a pointer */
3248 /* then result is a pointer */
3249 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3250 TETYPE (tree) = getSpec (TTYPE (tree) =
3254 tree->left = addCast (tree->left, resultType, TRUE);
3255 tree->right = addCast (tree->right, resultType, TRUE);
3257 TETYPE (tree) = getSpec (TTYPE (tree) =
3258 computeType (LTYPE (tree),
3264 LRVAL (tree) = RRVAL (tree) = 1;
3266 /* if right is a literal and */
3267 /* left is also an addition/subtraction with a literal then */
3268 /* rearrange the tree */
3269 if (IS_LITERAL (RTYPE (tree))
3270 /* avoid infinite loop */
3271 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3273 ast *litTree, *litParent;
3274 litTree = searchLitOp (tree, &litParent, "+-");
3277 if (litTree->opval.op == '+')
3281 ast *tTree = litTree->left;
3282 litTree->left = litTree->right;
3283 litTree->right = tree->right;
3284 tree->right = tTree;
3285 tree->opval.op = '+';
3286 litTree->opval.op = '-';
3288 else if (litTree->opval.op == '-')
3290 if (IS_LITERAL (RTYPE (litTree)))
3294 ast *tTree = litTree->left;
3295 litTree->left = tree->right;
3296 tree->right = litParent->left;
3297 litParent->left = tTree;
3298 litTree->opval.op = '+';
3300 tree->decorated = 0;
3301 decorateType (tree, resultType);
3307 ast *tTree = litTree->right;
3308 litTree->right = tree->right;
3309 tree->right = tTree;
3312 decorateType (litParent, resultType);
3317 /*------------------------------------------------------------------*/
3318 /*----------------------------*/
3320 /*----------------------------*/
3322 /* can be only integral type */
3323 if (!IS_INTEGRAL (LTYPE (tree)))
3325 werror (E_UNARY_OP, tree->opval.op);
3326 goto errorTreeReturn;
3329 /* if left is a literal then do it */
3330 if (IS_LITERAL (LTYPE (tree)))
3332 tree->type = EX_VALUE;
3333 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3335 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3338 tree->left = addCast (tree->left, resultType, TRUE);
3340 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3343 /*------------------------------------------------------------------*/
3344 /*----------------------------*/
3346 /*----------------------------*/
3348 /* can be pointer */
3349 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3350 !IS_PTR (LTYPE (tree)) &&
3351 !IS_ARRAY (LTYPE (tree)))
3353 werror (E_UNARY_OP, tree->opval.op);
3354 goto errorTreeReturn;
3357 /* if left is a literal then do it */
3358 if (IS_LITERAL (LTYPE (tree)))
3360 tree->type = EX_VALUE;
3361 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3363 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3367 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3370 /*------------------------------------------------------------------*/
3371 /*----------------------------*/
3373 /*----------------------------*/
3377 TTYPE (tree) = LTYPE (tree);
3378 TETYPE (tree) = LETYPE (tree);
3382 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3387 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3389 werror (E_SHIFT_OP_INVALID);
3390 werror (W_CONTINUE, "left & right types are ");
3391 printTypeChain (LTYPE (tree), stderr);
3392 fprintf (stderr, ",");
3393 printTypeChain (RTYPE (tree), stderr);
3394 fprintf (stderr, "\n");
3395 goto errorTreeReturn;
3398 /* make smaller type only if it's a LEFT_OP */
3399 if (tree->opval.op == LEFT_OP)
3400 tree->left = addCast (tree->left, resultType, TRUE);
3402 /* if they are both literal then */
3403 /* rewrite the tree */
3404 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3406 tree->type = EX_VALUE;
3407 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3408 valFromType (RETYPE (tree)),
3409 (tree->opval.op == LEFT_OP ? 1 : 0));
3410 tree->right = tree->left = NULL;
3411 TETYPE (tree) = getSpec (TTYPE (tree) =
3412 tree->opval.val->type);
3416 LRVAL (tree) = RRVAL (tree) = 1;
3417 if (tree->opval.op == LEFT_OP)
3419 TETYPE (tree) = getSpec (TTYPE (tree) =
3420 computeType (LTYPE (tree),
3427 /* no promotion necessary */
3428 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3429 if (IS_LITERAL (TTYPE (tree)))
3430 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3433 /* if only the right side is a literal & we are
3434 shifting more than size of the left operand then zero */
3435 if (IS_LITERAL (RTYPE (tree)) &&
3436 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3437 (getSize (TETYPE (tree)) * 8))
3439 if (tree->opval.op==LEFT_OP ||
3440 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3442 lineno=tree->lineno;
3443 werror (W_SHIFT_CHANGED,
3444 (tree->opval.op == LEFT_OP ? "left" : "right"));
3445 tree->type = EX_VALUE;
3446 tree->left = tree->right = NULL;
3447 tree->opval.val = constVal ("0");
3448 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3455 /*------------------------------------------------------------------*/
3456 /*----------------------------*/
3458 /*----------------------------*/
3459 case CAST: /* change the type */
3460 /* cannot cast to an aggregate type */
3461 if (IS_AGGREGATE (LTYPE (tree)))
3463 werror (E_CAST_ILLEGAL);
3464 goto errorTreeReturn;
3467 /* make sure the type is complete and sane */
3468 checkTypeSanity(LETYPE(tree), "(cast)");
3470 /* If code memory is read only, then pointers to code memory */
3471 /* implicitly point to constants -- make this explicit */
3473 sym_link *t = LTYPE(tree);
3474 while (t && t->next)
3476 if (IS_CODEPTR(t) && port->mem.code_ro)
3478 if (IS_SPEC(t->next))
3479 SPEC_CONST (t->next) = 1;
3481 DCL_PTR_CONST (t->next) = 1;
3488 /* if the right is a literal replace the tree */
3489 if (IS_LITERAL (RETYPE (tree))) {
3490 if (!IS_PTR (LTYPE (tree))) {
3491 tree->type = EX_VALUE;
3493 valCastLiteral (LTYPE (tree),
3494 floatFromVal (valFromType (RETYPE (tree))));
3497 TTYPE (tree) = tree->opval.val->type;
3498 tree->values.literalFromCast = 1;
3499 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3500 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3501 sym_link *rest = LTYPE(tree)->next;
3502 werror(W_LITERAL_GENERIC);
3503 TTYPE(tree) = newLink(DECLARATOR);
3504 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3505 TTYPE(tree)->next = rest;
3506 tree->left->opval.lnk = TTYPE(tree);
3509 TTYPE (tree) = LTYPE (tree);
3513 TTYPE (tree) = LTYPE (tree);
3517 #if 0 // this is already checked, now this could be explicit
3518 /* if pointer to struct then check names */
3519 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3520 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3521 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3523 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3524 SPEC_STRUCT(LETYPE(tree))->tag);
3527 if (IS_ADDRESS_OF_OP(tree->right)
3528 && IS_AST_SYM_VALUE (tree->right->left)
3529 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3531 tree->type = EX_VALUE;
3533 valCastLiteral (LTYPE (tree),
3534 SPEC_ADDR (AST_SYMBOL (tree->right->left)->etype));
3535 TTYPE (tree) = tree->opval.val->type;
3536 TETYPE (tree) = getSpec (TTYPE (tree));
3539 tree->values.literalFromCast = 1;
3543 /* handle offsetof macro: */
3544 /* #define offsetof(TYPE, MEMBER) \ */
3545 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3546 if (IS_ADDRESS_OF_OP(tree->right)
3547 && IS_AST_OP (tree->right->left)
3548 && tree->right->left->opval.op == PTR_OP
3549 && IS_AST_OP (tree->right->left->left)
3550 && tree->right->left->left->opval.op == CAST
3551 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3553 symbol *element = getStructElement (
3554 SPEC_STRUCT (LETYPE(tree->right->left)),
3555 AST_SYMBOL(tree->right->left->right)
3559 tree->type = EX_VALUE;
3560 tree->opval.val = valCastLiteral (
3563 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3566 TTYPE (tree) = tree->opval.val->type;
3567 TETYPE (tree) = getSpec (TTYPE (tree));
3574 /* if the right is a literal replace the tree */
3575 if (IS_LITERAL (RETYPE (tree))) {
3577 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3578 /* rewrite (type *)litaddr
3580 and define type at litaddr temp
3581 (but only if type's storage class is not generic)
3583 ast *newTree = newNode ('&', NULL, NULL);
3586 TTYPE (newTree) = LTYPE (tree);
3587 TETYPE (newTree) = getSpec(LTYPE (tree));
3589 /* define a global symbol at the casted address*/
3590 sym = newSymbol(genSymName (0), 0);
3591 sym->type = LTYPE (tree)->next;
3593 sym->type = newLink (V_VOID);
3594 sym->etype = getSpec(sym->type);
3595 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3596 sym->lineDef = tree->lineno;
3599 SPEC_STAT (sym->etype) = 1;
3600 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3601 SPEC_ABSA(sym->etype) = 1;
3602 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3605 newTree->left = newAst_VALUE(symbolVal(sym));
3606 newTree->left->lineno = tree->lineno;
3607 LTYPE (newTree) = sym->type;
3608 LETYPE (newTree) = sym->etype;
3609 LLVAL (newTree) = 1;
3610 LRVAL (newTree) = 0;
3611 TLVAL (newTree) = 1;
3615 if (!IS_PTR (LTYPE (tree))) {
3616 tree->type = EX_VALUE;
3618 valCastLiteral (LTYPE (tree),
3619 floatFromVal (valFromType (RTYPE (tree))));
3620 TTYPE (tree) = tree->opval.val->type;
3623 tree->values.literalFromCast = 1;
3624 TETYPE (tree) = getSpec (TTYPE (tree));
3628 TTYPE (tree) = LTYPE (tree);
3632 TETYPE (tree) = getSpec (TTYPE (tree));
3636 /*------------------------------------------------------------------*/
3637 /*----------------------------*/
3638 /* logical &&, || */
3639 /*----------------------------*/
3642 /* each must be arithmetic type or be a pointer */
3643 if (!IS_PTR (LTYPE (tree)) &&
3644 !IS_ARRAY (LTYPE (tree)) &&
3645 !IS_INTEGRAL (LTYPE (tree)))
3647 werror (E_COMPARE_OP);
3648 goto errorTreeReturn;
3651 if (!IS_PTR (RTYPE (tree)) &&
3652 !IS_ARRAY (RTYPE (tree)) &&
3653 !IS_INTEGRAL (RTYPE (tree)))
3655 werror (E_COMPARE_OP);
3656 goto errorTreeReturn;
3658 /* if they are both literal then */
3659 /* rewrite the tree */
3660 if (IS_LITERAL (RTYPE (tree)) &&
3661 IS_LITERAL (LTYPE (tree)))
3663 tree->type = EX_VALUE;
3664 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
3665 valFromType (RTYPE (tree)),
3667 tree->right = tree->left = NULL;
3668 TETYPE (tree) = getSpec (TTYPE (tree) =
3669 tree->opval.val->type);
3672 LRVAL (tree) = RRVAL (tree) = 1;
3673 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3676 /*------------------------------------------------------------------*/
3677 /*----------------------------*/
3678 /* comparison operators */
3679 /*----------------------------*/
3687 ast *lt = optimizeCompare (tree);
3693 /* if they are pointers they must be castable */
3694 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3696 if (tree->opval.op==EQ_OP &&
3697 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
3698 // we cannot cast a gptr to a !gptr: switch the leaves
3699 struct ast *s=tree->left;
3700 tree->left=tree->right;
3703 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3705 werror (E_COMPARE_OP);
3706 fprintf (stderr, "comparing type ");
3707 printTypeChain (LTYPE (tree), stderr);
3708 fprintf (stderr, "to type ");
3709 printTypeChain (RTYPE (tree), stderr);
3710 fprintf (stderr, "\n");
3711 goto errorTreeReturn;
3714 /* else they should be promotable to one another */
3717 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
3718 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
3720 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3722 werror (E_COMPARE_OP);
3723 fprintf (stderr, "comparing type ");
3724 printTypeChain (LTYPE (tree), stderr);
3725 fprintf (stderr, "to type ");
3726 printTypeChain (RTYPE (tree), stderr);
3727 fprintf (stderr, "\n");
3728 goto errorTreeReturn;
3731 /* if unsigned value < 0 then always false */
3732 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
3733 if (SPEC_USIGN(LETYPE(tree)) &&
3734 !IS_CHAR(LETYPE(tree)) && /* promotion to signed int */
3735 IS_LITERAL(RTYPE(tree)) &&
3736 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
3738 if (tree->opval.op == '<')
3742 if (tree->opval.op == '>')
3744 if (resultType == RESULT_TYPE_IFX)
3746 /* the parent is an ifx: */
3747 /* if (unsigned value) */
3751 /* (unsigned value) ? 1 : 0 */
3752 tree->opval.op = '?';
3753 tree->right = newNode (':',
3754 newAst_VALUE (constVal ("1")),
3755 tree->right); /* val 0 */
3756 tree->right->lineno = tree->lineno;
3757 tree->right->left->lineno = tree->lineno;
3758 decorateType (tree->right, RESULT_CHECK);
3761 /* if they are both literal then */
3762 /* rewrite the tree */
3763 if (IS_LITERAL (RTYPE (tree)) &&
3764 IS_LITERAL (LTYPE (tree)))
3766 tree->type = EX_VALUE;
3767 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
3768 valFromType (RETYPE (tree)),
3770 tree->right = tree->left = NULL;
3771 TETYPE (tree) = getSpec (TTYPE (tree) =
3772 tree->opval.val->type);
3775 LRVAL (tree) = RRVAL (tree) = 1;
3776 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3779 /*------------------------------------------------------------------*/
3780 /*----------------------------*/
3782 /*----------------------------*/
3783 case SIZEOF: /* evaluate wihout code generation */
3784 /* change the type to a integer */
3786 int size = getSize (tree->right->ftype);
3787 SNPRINTF(buffer, sizeof(buffer), "%d", size);
3788 if (!size && !IS_VOID(tree->right->ftype))
3789 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
3791 tree->type = EX_VALUE;
3792 tree->opval.val = constVal (buffer);
3793 tree->right = tree->left = NULL;
3794 TETYPE (tree) = getSpec (TTYPE (tree) =
3795 tree->opval.val->type);
3798 /*------------------------------------------------------------------*/
3799 /*----------------------------*/
3801 /*----------------------------*/
3803 /* return typeof enum value */
3804 tree->type = EX_VALUE;
3807 if (IS_SPEC(tree->right->ftype)) {
3808 switch (SPEC_NOUN(tree->right->ftype)) {
3810 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
3811 else typeofv = TYPEOF_INT;
3814 typeofv = TYPEOF_FLOAT;
3817 typeofv = TYPEOF_CHAR;
3820 typeofv = TYPEOF_VOID;
3823 typeofv = TYPEOF_STRUCT;
3826 typeofv = TYPEOF_BITFIELD;
3829 typeofv = TYPEOF_BIT;
3832 typeofv = TYPEOF_SBIT;
3838 switch (DCL_TYPE(tree->right->ftype)) {
3840 typeofv = TYPEOF_POINTER;
3843 typeofv = TYPEOF_FPOINTER;
3846 typeofv = TYPEOF_CPOINTER;
3849 typeofv = TYPEOF_GPOINTER;
3852 typeofv = TYPEOF_PPOINTER;
3855 typeofv = TYPEOF_IPOINTER;
3858 typeofv = TYPEOF_ARRAY;
3861 typeofv = TYPEOF_FUNCTION;
3867 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
3868 tree->opval.val = constVal (buffer);
3869 tree->right = tree->left = NULL;
3870 TETYPE (tree) = getSpec (TTYPE (tree) =
3871 tree->opval.val->type);
3874 /*------------------------------------------------------------------*/
3875 /*----------------------------*/
3876 /* conditional operator '?' */
3877 /*----------------------------*/
3879 /* the type is value of the colon operator (on the right) */
3880 assert (IS_COLON_OP (tree->right));
3881 /* if already known then replace the tree : optimizer will do it
3882 but faster to do it here */
3883 if (IS_LITERAL (LTYPE (tree)))
3885 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
3886 return decorateType (tree->right->left, resultTypeProp);
3888 return decorateType (tree->right->right, resultTypeProp);
3892 tree->right = decorateType (tree->right, resultTypeProp);
3893 TTYPE (tree) = RTYPE (tree);
3894 TETYPE (tree) = getSpec (TTYPE (tree));
3899 /* if they don't match we have a problem */
3900 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3902 werror (E_TYPE_MISMATCH, "conditional operator", " ");
3903 goto errorTreeReturn;
3906 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
3907 resultType, tree->opval.op);
3908 TETYPE (tree) = getSpec (TTYPE (tree));
3912 #if 0 // assignment operators are converted by the parser
3913 /*------------------------------------------------------------------*/
3914 /*----------------------------*/
3915 /* assignment operators */
3916 /*----------------------------*/
3919 /* for these it must be both must be integral */
3920 if (!IS_ARITHMETIC (LTYPE (tree)) ||
3921 !IS_ARITHMETIC (RTYPE (tree)))
3923 werror (E_OPS_INTEGRAL);
3924 goto errorTreeReturn;
3927 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3929 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
3930 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
3934 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
3935 goto errorTreeReturn;
3946 /* for these it must be both must be integral */
3947 if (!IS_INTEGRAL (LTYPE (tree)) ||
3948 !IS_INTEGRAL (RTYPE (tree)))
3950 werror (E_OPS_INTEGRAL);
3951 goto errorTreeReturn;
3954 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3956 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3957 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
3961 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
3962 goto errorTreeReturn;
3968 /*------------------------------------------------------------------*/
3969 /*----------------------------*/
3971 /*----------------------------*/
3973 if (!(IS_PTR (LTYPE (tree)) ||
3974 IS_ARITHMETIC (LTYPE (tree))))
3976 werror (E_PLUS_INVALID, "-=");
3977 goto errorTreeReturn;
3980 if (!(IS_PTR (RTYPE (tree)) ||
3981 IS_ARITHMETIC (RTYPE (tree))))
3983 werror (E_PLUS_INVALID, "-=");
3984 goto errorTreeReturn;
3987 TETYPE (tree) = getSpec (TTYPE (tree) =
3988 computeType (LTYPE (tree),
3993 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3994 werror (E_CODE_WRITE, "-=");
3998 werror (E_LVALUE_REQUIRED, "-=");
3999 goto errorTreeReturn;
4005 /*------------------------------------------------------------------*/
4006 /*----------------------------*/
4008 /*----------------------------*/
4010 /* this is not a unary operation */
4011 /* if both pointers then problem */
4012 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4014 werror (E_PTR_PLUS_PTR);
4015 goto errorTreeReturn;
4018 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
4020 werror (E_PLUS_INVALID, "+=");
4021 goto errorTreeReturn;
4024 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
4026 werror (E_PLUS_INVALID, "+=");
4027 goto errorTreeReturn;
4030 TETYPE (tree) = getSpec (TTYPE (tree) =
4031 computeType (LTYPE (tree),
4036 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4037 werror (E_CODE_WRITE, "+=");
4041 werror (E_LVALUE_REQUIRED, "+=");
4042 goto errorTreeReturn;
4045 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_CHECK);
4046 tree->opval.op = '=';
4051 /*------------------------------------------------------------------*/
4052 /*----------------------------*/
4053 /* straight assignemnt */
4054 /*----------------------------*/
4056 /* cannot be an aggregate */
4057 if (IS_AGGREGATE (LTYPE (tree)))
4059 werror (E_AGGR_ASSIGN);
4060 goto errorTreeReturn;
4063 /* they should either match or be castable */
4064 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4066 werror (E_TYPE_MISMATCH, "assignment", " ");
4067 printFromToType(RTYPE(tree),LTYPE(tree));
4070 /* if the left side of the tree is of type void
4071 then report error */
4072 if (IS_VOID (LTYPE (tree)))
4074 werror (E_CAST_ZERO);
4075 printFromToType(RTYPE(tree), LTYPE(tree));
4078 TETYPE (tree) = getSpec (TTYPE (tree) =
4082 if (!tree->initMode ) {
4083 if (IS_CONSTANT(LTYPE(tree)))
4084 werror (E_CODE_WRITE, "=");
4088 werror (E_LVALUE_REQUIRED, "=");
4089 goto errorTreeReturn;
4094 /*------------------------------------------------------------------*/
4095 /*----------------------------*/
4096 /* comma operator */
4097 /*----------------------------*/
4099 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4102 /*------------------------------------------------------------------*/
4103 /*----------------------------*/
4105 /*----------------------------*/
4108 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4109 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4111 if (tree->left->opval.op == '*' && !tree->left->right)
4112 tree->left = tree->left->left;
4115 /* require a function or pointer to function */
4116 if (!IS_FUNC (LTYPE (tree))
4117 && !(IS_CODEPTR (LTYPE (tree)) && IS_FUNC (LTYPE (tree)->next)))
4119 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4120 goto errorTreeReturn;
4127 if (IS_CODEPTR(LTYPE(tree)))
4128 functype = LTYPE (tree)->next;
4130 functype = LTYPE (tree);
4132 if (processParms (tree->left, FUNC_ARGS(functype),
4133 &tree->right, &parmNumber, TRUE)) {
4134 goto errorTreeReturn;
4137 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4138 !IFFUNC_ISBUILTIN(functype))
4140 reverseParms (tree->right);
4143 TTYPE (tree) = functype->next;
4144 TETYPE (tree) = getSpec (TTYPE (tree));
4148 /*------------------------------------------------------------------*/
4149 /*----------------------------*/
4150 /* return statement */
4151 /*----------------------------*/
4156 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4158 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4159 printFromToType (RTYPE(tree), currFunc->type->next);
4160 goto errorTreeReturn;
4163 if (IS_VOID (currFunc->type->next)
4165 !IS_VOID (RTYPE (tree)))
4167 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4168 goto errorTreeReturn;
4171 /* if there is going to be a casting required then add it */
4172 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4175 decorateType (newNode (CAST,
4176 newAst_LINK (copyLinkChain (currFunc->type->next)),
4186 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4188 werror (W_VOID_FUNC, currFunc->name);
4189 goto errorTreeReturn;
4192 TTYPE (tree) = TETYPE (tree) = NULL;
4195 /*------------------------------------------------------------------*/
4196 /*----------------------------*/
4197 /* switch statement */
4198 /*----------------------------*/
4200 /* the switch value must be an integer */
4201 if (!IS_INTEGRAL (LTYPE (tree)))
4203 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4204 goto errorTreeReturn;
4207 TTYPE (tree) = TETYPE (tree) = NULL;
4210 /*------------------------------------------------------------------*/
4211 /*----------------------------*/
4213 /*----------------------------*/
4215 tree->left = backPatchLabels (tree->left,
4218 TTYPE (tree) = TETYPE (tree) = NULL;
4221 /*------------------------------------------------------------------*/
4222 /*----------------------------*/
4224 /*----------------------------*/
4227 decorateType (resolveSymbols (AST_FOR (tree, initExpr)), RESULT_CHECK);
4228 decorateType (resolveSymbols (AST_FOR (tree, condExpr)), RESULT_CHECK);
4229 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_CHECK);
4231 /* if the for loop is reversible then
4232 reverse it otherwise do what we normally
4238 if (isLoopReversible (tree, &sym, &init, &end))
4239 return reverseLoop (tree, sym, init, end);
4241 return decorateType (createFor (AST_FOR (tree, trueLabel),
4242 AST_FOR (tree, continueLabel),
4243 AST_FOR (tree, falseLabel),
4244 AST_FOR (tree, condLabel),
4245 AST_FOR (tree, initExpr),
4246 AST_FOR (tree, condExpr),
4247 AST_FOR (tree, loopExpr),
4248 tree->left), RESULT_CHECK);
4251 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4252 "node PARAM shouldn't be processed here");
4253 /* but in processParams() */
4256 TTYPE (tree) = TETYPE (tree) = NULL;
4260 /* some error found this tree will be killed */
4262 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4263 tree->opval.op = NULLOP;
4269 /*-----------------------------------------------------------------*/
4270 /* sizeofOp - processes size of operation */
4271 /*-----------------------------------------------------------------*/
4273 sizeofOp (sym_link * type)
4278 /* make sure the type is complete and sane */
4279 checkTypeSanity(type, "(sizeof)");
4281 /* get the size and convert it to character */
4282 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4283 if (!size && !IS_VOID(type))
4284 werror (E_SIZEOF_INCOMPLETE_TYPE);
4286 /* now convert into value */
4287 return constVal (buff);
4291 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4292 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4293 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4294 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4295 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4296 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4297 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4299 /*-----------------------------------------------------------------*/
4300 /* backPatchLabels - change and or not operators to flow control */
4301 /*-----------------------------------------------------------------*/
4303 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4309 if (!(IS_ANDORNOT (tree)))
4312 /* if this an and */
4315 static int localLbl = 0;
4318 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4319 localLabel = newSymbol (buffer, NestLevel);
4321 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4323 /* if left is already a IFX then just change the if true label in that */
4324 if (!IS_IFX (tree->left))
4325 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4327 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4328 /* right is a IFX then just join */
4329 if (IS_IFX (tree->right))
4330 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4332 tree->right = createLabel (localLabel, tree->right);
4333 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4335 return newNode (NULLOP, tree->left, tree->right);
4338 /* if this is an or operation */
4341 static int localLbl = 0;
4344 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4345 localLabel = newSymbol (buffer, NestLevel);
4347 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4349 /* if left is already a IFX then just change the if true label in that */
4350 if (!IS_IFX (tree->left))
4351 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4353 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4354 /* right is a IFX then just join */
4355 if (IS_IFX (tree->right))
4356 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4358 tree->right = createLabel (localLabel, tree->right);
4359 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4361 return newNode (NULLOP, tree->left, tree->right);
4367 int wasnot = IS_NOT (tree->left);
4368 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4370 /* if the left is already a IFX */
4371 if (!IS_IFX (tree->left))
4372 tree->left = newNode (IFX, tree->left, NULL);
4376 tree->left->trueLabel = trueLabel;
4377 tree->left->falseLabel = falseLabel;
4381 tree->left->trueLabel = falseLabel;
4382 tree->left->falseLabel = trueLabel;
4389 tree->trueLabel = trueLabel;
4390 tree->falseLabel = falseLabel;
4397 /*-----------------------------------------------------------------*/
4398 /* createBlock - create expression tree for block */
4399 /*-----------------------------------------------------------------*/
4401 createBlock (symbol * decl, ast * body)
4405 /* if the block has nothing */
4409 ex = newNode (BLOCK, NULL, body);
4410 ex->values.sym = decl;
4412 ex->right = ex->right;
4418 /*-----------------------------------------------------------------*/
4419 /* createLabel - creates the expression tree for labels */
4420 /*-----------------------------------------------------------------*/
4422 createLabel (symbol * label, ast * stmnt)
4425 char name[SDCC_NAME_MAX + 1];
4428 /* must create fresh symbol if the symbol name */
4429 /* exists in the symbol table, since there can */
4430 /* be a variable with the same name as the labl */
4431 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4432 (csym->level == label->level))
4433 label = newSymbol (label->name, label->level);
4435 /* change the name before putting it in add _ */
4436 SNPRINTF(name, sizeof(name), "%s", label->name);
4438 /* put the label in the LabelSymbol table */
4439 /* but first check if a label of the same */
4441 if ((csym = findSym (LabelTab, NULL, name)))
4442 werror (E_DUPLICATE_LABEL, label->name);
4444 addSym (LabelTab, label, name, label->level, 0, 0);
4447 label->key = labelKey++;
4448 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4454 /*-----------------------------------------------------------------*/
4455 /* createCase - generates the parsetree for a case statement */
4456 /*-----------------------------------------------------------------*/
4458 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4460 char caseLbl[SDCC_NAME_MAX + 1];
4464 /* if the switch statement does not exist */
4465 /* then case is out of context */
4468 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4472 caseVal = decorateType (resolveSymbols (caseVal), RESULT_CHECK);
4473 /* if not a constant then error */
4474 if (!IS_LITERAL (caseVal->ftype))
4476 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4480 /* if not a integer than error */
4481 if (!IS_INTEGRAL (caseVal->ftype))
4483 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4487 /* find the end of the switch values chain */
4488 if (!(val = swStat->values.switchVals.swVals))
4489 swStat->values.switchVals.swVals = caseVal->opval.val;
4492 /* also order the cases according to value */
4494 int cVal = (int) floatFromVal (caseVal->opval.val);
4495 while (val && (int) floatFromVal (val) < cVal)
4501 /* if we reached the end then */
4504 pval->next = caseVal->opval.val;
4506 else if ((int) floatFromVal (val) == cVal)
4508 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
4514 /* we found a value greater than */
4515 /* the current value we must add this */
4516 /* before the value */
4517 caseVal->opval.val->next = val;
4519 /* if this was the first in chain */
4520 if (swStat->values.switchVals.swVals == val)
4521 swStat->values.switchVals.swVals =
4524 pval->next = caseVal->opval.val;
4529 /* create the case label */
4530 SNPRINTF(caseLbl, sizeof(caseLbl),
4532 swStat->values.switchVals.swNum,
4533 (int) floatFromVal (caseVal->opval.val));
4535 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
4540 /*-----------------------------------------------------------------*/
4541 /* createDefault - creates the parse tree for the default statement */
4542 /*-----------------------------------------------------------------*/
4544 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
4546 char defLbl[SDCC_NAME_MAX + 1];
4548 /* if the switch statement does not exist */
4549 /* then case is out of context */
4552 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
4556 if (swStat->values.switchVals.swDefault)
4558 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
4563 /* turn on the default flag */
4564 swStat->values.switchVals.swDefault = 1;
4566 /* create the label */
4567 SNPRINTF (defLbl, sizeof(defLbl),
4568 "_default_%d", swStat->values.switchVals.swNum);
4569 return createLabel (newSymbol (defLbl, 0), stmnt);
4572 /*-----------------------------------------------------------------*/
4573 /* createIf - creates the parsetree for the if statement */
4574 /*-----------------------------------------------------------------*/
4576 createIf (ast * condAst, ast * ifBody, ast * elseBody)
4578 static int Lblnum = 0;
4580 symbol *ifTrue, *ifFalse, *ifEnd;
4582 /* if neither exists */
4583 if (!elseBody && !ifBody) {
4584 // if there are no side effects (i++, j() etc)
4585 if (!hasSEFcalls(condAst)) {
4590 /* create the labels */
4591 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
4592 ifFalse = newSymbol (buffer, NestLevel);
4593 /* if no else body then end == false */
4598 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
4599 ifEnd = newSymbol (buffer, NestLevel);
4602 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
4603 ifTrue = newSymbol (buffer, NestLevel);
4607 /* attach the ifTrue label to the top of it body */
4608 ifBody = createLabel (ifTrue, ifBody);
4609 /* attach a goto end to the ifBody if else is present */
4612 ifBody = newNode (NULLOP, ifBody,
4614 newAst_VALUE (symbolVal (ifEnd)),
4616 /* put the elseLabel on the else body */
4617 elseBody = createLabel (ifFalse, elseBody);
4618 /* out the end at the end of the body */
4619 elseBody = newNode (NULLOP,
4621 createLabel (ifEnd, NULL));
4625 ifBody = newNode (NULLOP, ifBody,
4626 createLabel (ifFalse, NULL));
4628 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
4629 if (IS_IFX (condAst))
4632 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
4634 return newNode (NULLOP, ifTree,
4635 newNode (NULLOP, ifBody, elseBody));
4639 /*-----------------------------------------------------------------*/
4640 /* createDo - creates parse tree for do */
4643 /* _docontinue_n: */
4644 /* condition_expression +-> trueLabel -> _dobody_n */
4646 /* +-> falseLabel-> _dobreak_n */
4648 /*-----------------------------------------------------------------*/
4650 createDo (symbol * trueLabel, symbol * continueLabel,
4651 symbol * falseLabel, ast * condAst, ast * doBody)
4656 /* if the body does not exist then it is simple */
4659 condAst = backPatchLabels (condAst, continueLabel, NULL);
4660 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
4661 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
4662 doTree->trueLabel = continueLabel;
4663 doTree->falseLabel = NULL;
4667 /* otherwise we have a body */
4668 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
4670 /* attach the body label to the top */
4671 doBody = createLabel (trueLabel, doBody);
4672 /* attach the continue label to end of body */
4673 doBody = newNode (NULLOP, doBody,
4674 createLabel (continueLabel, NULL));
4676 /* now put the break label at the end */
4677 if (IS_IFX (condAst))
4680 doTree = newIfxNode (condAst, trueLabel, falseLabel);
4682 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
4684 /* putting it together */
4685 return newNode (NULLOP, doBody, doTree);
4688 /*-----------------------------------------------------------------*/
4689 /* createFor - creates parse tree for 'for' statement */
4692 /* condExpr +-> trueLabel -> _forbody_n */
4694 /* +-> falseLabel-> _forbreak_n */
4697 /* _forcontinue_n: */
4699 /* goto _forcond_n ; */
4701 /*-----------------------------------------------------------------*/
4703 createFor (symbol * trueLabel, symbol * continueLabel,
4704 symbol * falseLabel, symbol * condLabel,
4705 ast * initExpr, ast * condExpr, ast * loopExpr,
4710 /* if loopexpression not present then we can generate it */
4711 /* the same way as a while */
4713 return newNode (NULLOP, initExpr,
4714 createWhile (trueLabel, continueLabel,
4715 falseLabel, condExpr, forBody));
4716 /* vanilla for statement */
4717 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4719 if (condExpr && !IS_IFX (condExpr))
4720 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
4723 /* attach condition label to condition */
4724 condExpr = createLabel (condLabel, condExpr);
4726 /* attach body label to body */
4727 forBody = createLabel (trueLabel, forBody);
4729 /* attach continue to forLoop expression & attach */
4730 /* goto the forcond @ and of loopExpression */
4731 loopExpr = createLabel (continueLabel,
4735 newAst_VALUE (symbolVal (condLabel)),
4737 /* now start putting them together */
4738 forTree = newNode (NULLOP, initExpr, condExpr);
4739 forTree = newNode (NULLOP, forTree, forBody);
4740 forTree = newNode (NULLOP, forTree, loopExpr);
4741 /* finally add the break label */
4742 forTree = newNode (NULLOP, forTree,
4743 createLabel (falseLabel, NULL));
4747 /*-----------------------------------------------------------------*/
4748 /* createWhile - creates parse tree for while statement */
4749 /* the while statement will be created as follows */
4751 /* _while_continue_n: */
4752 /* condition_expression +-> trueLabel -> _while_boby_n */
4754 /* +-> falseLabel -> _while_break_n */
4755 /* _while_body_n: */
4757 /* goto _while_continue_n */
4758 /* _while_break_n: */
4759 /*-----------------------------------------------------------------*/
4761 createWhile (symbol * trueLabel, symbol * continueLabel,
4762 symbol * falseLabel, ast * condExpr, ast * whileBody)
4766 /* put the continue label */
4767 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4768 condExpr = createLabel (continueLabel, condExpr);
4769 condExpr->lineno = 0;
4771 /* put the body label in front of the body */
4772 whileBody = createLabel (trueLabel, whileBody);
4773 whileBody->lineno = 0;
4774 /* put a jump to continue at the end of the body */
4775 /* and put break label at the end of the body */
4776 whileBody = newNode (NULLOP,
4779 newAst_VALUE (symbolVal (continueLabel)),
4780 createLabel (falseLabel, NULL)));
4782 /* put it all together */
4783 if (IS_IFX (condExpr))
4784 whileTree = condExpr;
4787 whileTree = newNode (IFX, condExpr, NULL);
4788 /* put the true & false labels in place */
4789 whileTree->trueLabel = trueLabel;
4790 whileTree->falseLabel = falseLabel;
4793 return newNode (NULLOP, whileTree, whileBody);
4796 /*-----------------------------------------------------------------*/
4797 /* optimizeGetHbit - get highest order bit of the expression */
4798 /*-----------------------------------------------------------------*/
4800 optimizeGetHbit (ast * tree)
4803 /* if this is not a bit and */
4804 if (!IS_BITAND (tree))
4807 /* will look for tree of the form
4808 ( expr >> ((sizeof expr) -1) ) & 1 */
4809 if (!IS_AST_LIT_VALUE (tree->right))
4812 if (AST_LIT_VALUE (tree->right) != 1)
4815 if (!IS_RIGHT_OP (tree->left))
4818 if (!IS_AST_LIT_VALUE (tree->left->right))
4821 if ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
4822 (j = (getSize (TTYPE (tree->left->left)) * 8 - 1)))
4825 /* make sure the port supports GETHBIT */
4826 if (port->hasExtBitOp
4827 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (tree->left->left))))
4830 return decorateType (newNode (GETHBIT, tree->left->left, NULL), RESULT_CHECK);
4834 /*-----------------------------------------------------------------*/
4835 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
4836 /*-----------------------------------------------------------------*/
4838 optimizeRRCRLC (ast * root)
4840 /* will look for trees of the form
4841 (?expr << 1) | (?expr >> 7) or
4842 (?expr >> 7) | (?expr << 1) will make that
4843 into a RLC : operation ..
4845 (?expr >> 1) | (?expr << 7) or
4846 (?expr << 7) | (?expr >> 1) will make that
4847 into a RRC operation
4848 note : by 7 I mean (number of bits required to hold the
4850 /* if the root operations is not a | operation the not */
4851 if (!IS_BITOR (root))
4854 /* I have to think of a better way to match patterns this sucks */
4855 /* that aside let start looking for the first case : I use a the
4856 negative check a lot to improve the efficiency */
4857 /* (?expr << 1) | (?expr >> 7) */
4858 if (IS_LEFT_OP (root->left) &&
4859 IS_RIGHT_OP (root->right))
4862 if (!SPEC_USIGN (TETYPE (root->left->left)))
4865 if (!IS_AST_LIT_VALUE (root->left->right) ||
4866 !IS_AST_LIT_VALUE (root->right->right))
4869 /* make sure it is the same expression */
4870 if (!isAstEqual (root->left->left,
4874 if (AST_LIT_VALUE (root->left->right) != 1)
4877 if (AST_LIT_VALUE (root->right->right) !=
4878 (getSize (TTYPE (root->left->left)) * 8 - 1))
4881 /* make sure the port supports RLC */
4882 if (port->hasExtBitOp
4883 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
4886 /* whew got the first case : create the AST */
4887 return newNode (RLC, root->left->left, NULL);
4891 /* check for second case */
4892 /* (?expr >> 7) | (?expr << 1) */
4893 if (IS_LEFT_OP (root->right) &&
4894 IS_RIGHT_OP (root->left))
4897 if (!SPEC_USIGN (TETYPE (root->left->left)))
4900 if (!IS_AST_LIT_VALUE (root->left->right) ||
4901 !IS_AST_LIT_VALUE (root->right->right))
4904 /* make sure it is the same symbol */
4905 if (!isAstEqual (root->left->left,
4909 if (AST_LIT_VALUE (root->right->right) != 1)
4912 if (AST_LIT_VALUE (root->left->right) !=
4913 (getSize (TTYPE (root->left->left)) * 8 - 1))
4916 /* make sure the port supports RLC */
4917 if (port->hasExtBitOp
4918 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
4921 /* whew got the first case : create the AST */
4922 return newNode (RLC, root->left->left, NULL);
4927 /* third case for RRC */
4928 /* (?symbol >> 1) | (?symbol << 7) */
4929 if (IS_LEFT_OP (root->right) &&
4930 IS_RIGHT_OP (root->left))
4933 if (!SPEC_USIGN (TETYPE (root->left->left)))
4936 if (!IS_AST_LIT_VALUE (root->left->right) ||
4937 !IS_AST_LIT_VALUE (root->right->right))
4940 /* make sure it is the same symbol */
4941 if (!isAstEqual (root->left->left,
4945 if (AST_LIT_VALUE (root->left->right) != 1)
4948 if (AST_LIT_VALUE (root->right->right) !=
4949 (getSize (TTYPE (root->left->left)) * 8 - 1))
4952 /* make sure the port supports RRC */
4953 if (port->hasExtBitOp
4954 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
4957 /* whew got the first case : create the AST */
4958 return newNode (RRC, root->left->left, NULL);
4962 /* fourth and last case for now */
4963 /* (?symbol << 7) | (?symbol >> 1) */
4964 if (IS_RIGHT_OP (root->right) &&
4965 IS_LEFT_OP (root->left))
4968 if (!SPEC_USIGN (TETYPE (root->left->left)))
4971 if (!IS_AST_LIT_VALUE (root->left->right) ||
4972 !IS_AST_LIT_VALUE (root->right->right))
4975 /* make sure it is the same symbol */
4976 if (!isAstEqual (root->left->left,
4980 if (AST_LIT_VALUE (root->right->right) != 1)
4983 if (AST_LIT_VALUE (root->left->right) !=
4984 (getSize (TTYPE (root->left->left)) * 8 - 1))
4987 /* make sure the port supports RRC */
4988 if (port->hasExtBitOp
4989 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
4992 /* whew got the first case : create the AST */
4993 return newNode (RRC, root->left->left, NULL);
4997 /* not found return root */
5001 /*-----------------------------------------------------------------*/
5002 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
5003 /*-----------------------------------------------------------------*/
5005 optimizeSWAP (ast * root)
5007 /* will look for trees of the form
5008 (?expr << 4) | (?expr >> 4) or
5009 (?expr >> 4) | (?expr << 4) will make that
5010 into a SWAP : operation ..
5011 note : by 4 I mean (number of bits required to hold the
5013 /* if the root operations is not a | operation the not */
5014 if (!IS_BITOR (root))
5017 /* (?expr << 4) | (?expr >> 4) */
5018 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
5019 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
5022 if (!SPEC_USIGN (TETYPE (root->left->left)))
5025 if (!IS_AST_LIT_VALUE (root->left->right) ||
5026 !IS_AST_LIT_VALUE (root->right->right))
5029 /* make sure it is the same expression */
5030 if (!isAstEqual (root->left->left,
5034 if (AST_LIT_VALUE (root->left->right) !=
5035 (getSize (TTYPE (root->left->left)) * 4))
5038 if (AST_LIT_VALUE (root->right->right) !=
5039 (getSize (TTYPE (root->left->left)) * 4))
5042 /* make sure the port supports SWAP */
5043 if (port->hasExtBitOp
5044 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
5047 /* found it : create the AST */
5048 return newNode (SWAP, root->left->left, NULL);
5052 /* not found return root */
5056 /*-----------------------------------------------------------------*/
5057 /* optimizeCompare - otimizes compares for bit variables */
5058 /*-----------------------------------------------------------------*/
5060 optimizeCompare (ast * root)
5062 ast *optExpr = NULL;
5065 unsigned int litValue;
5067 /* if nothing then return nothing */
5071 /* if not a compare op then do leaves */
5072 if (!IS_COMPARE_OP (root))
5074 root->left = optimizeCompare (root->left);
5075 root->right = optimizeCompare (root->right);
5079 /* if left & right are the same then depending
5080 of the operation do */
5081 if (isAstEqual (root->left, root->right))
5083 switch (root->opval.op)
5088 optExpr = newAst_VALUE (constVal ("0"));
5093 optExpr = newAst_VALUE (constVal ("1"));
5097 return decorateType (optExpr, RESULT_CHECK);
5100 vleft = (root->left->type == EX_VALUE ?
5101 root->left->opval.val : NULL);
5103 vright = (root->right->type == EX_VALUE ?
5104 root->right->opval.val : NULL);
5106 /* if left is a BITVAR in BITSPACE */
5107 /* and right is a LITERAL then opt- */
5108 /* imize else do nothing */
5109 if (vleft && vright &&
5110 IS_BITVAR (vleft->etype) &&
5111 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5112 IS_LITERAL (vright->etype))
5115 /* if right side > 1 then comparison may never succeed */
5116 if ((litValue = (int) floatFromVal (vright)) > 1)
5118 werror (W_BAD_COMPARE);
5124 switch (root->opval.op)
5126 case '>': /* bit value greater than 1 cannot be */
5127 werror (W_BAD_COMPARE);
5131 case '<': /* bit value < 1 means 0 */
5133 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5136 case LE_OP: /* bit value <= 1 means no check */
5137 optExpr = newAst_VALUE (vright);
5140 case GE_OP: /* bit value >= 1 means only check for = */
5142 optExpr = newAst_VALUE (vleft);
5147 { /* literal is zero */
5148 switch (root->opval.op)
5150 case '<': /* bit value < 0 cannot be */
5151 werror (W_BAD_COMPARE);
5155 case '>': /* bit value > 0 means 1 */
5157 optExpr = newAst_VALUE (vleft);
5160 case LE_OP: /* bit value <= 0 means no check */
5161 case GE_OP: /* bit value >= 0 means no check */
5162 werror (W_BAD_COMPARE);
5166 case EQ_OP: /* bit == 0 means ! of bit */
5167 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5171 return decorateType (resolveSymbols (optExpr), RESULT_CHECK);
5172 } /* end-of-if of BITVAR */
5177 /*-----------------------------------------------------------------*/
5178 /* addSymToBlock : adds the symbol to the first block we find */
5179 /*-----------------------------------------------------------------*/
5181 addSymToBlock (symbol * sym, ast * tree)
5183 /* reached end of tree or a leaf */
5184 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5188 if (IS_AST_OP (tree) &&
5189 tree->opval.op == BLOCK)
5192 symbol *lsym = copySymbol (sym);
5194 lsym->next = AST_VALUES (tree, sym);
5195 AST_VALUES (tree, sym) = lsym;
5199 addSymToBlock (sym, tree->left);
5200 addSymToBlock (sym, tree->right);
5203 /*-----------------------------------------------------------------*/
5204 /* processRegParms - do processing for register parameters */
5205 /*-----------------------------------------------------------------*/
5207 processRegParms (value * args, ast * body)
5211 if (IS_REGPARM (args->etype))
5212 addSymToBlock (args->sym, body);
5217 /*-----------------------------------------------------------------*/
5218 /* resetParmKey - resets the operandkeys for the symbols */
5219 /*-----------------------------------------------------------------*/
5220 DEFSETFUNC (resetParmKey)
5231 /*-----------------------------------------------------------------*/
5232 /* createFunction - This is the key node that calls the iCode for */
5233 /* generating the code for a function. Note code */
5234 /* is generated function by function, later when */
5235 /* add inter-procedural analysis this will change */
5236 /*-----------------------------------------------------------------*/
5238 createFunction (symbol * name, ast * body)
5244 iCode *piCode = NULL;
5246 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5247 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5249 /* if check function return 0 then some problem */
5250 if (checkFunction (name, NULL) == 0)
5253 /* create a dummy block if none exists */
5255 body = newNode (BLOCK, NULL, NULL);
5259 /* check if the function name already in the symbol table */
5260 if ((csym = findSym (SymbolTab, NULL, name->name)))
5263 /* special case for compiler defined functions
5264 we need to add the name to the publics list : this
5265 actually means we are now compiling the compiler
5269 addSet (&publics, name);
5275 allocVariables (name);
5277 name->lastLine = mylineno;
5280 /* set the stack pointer */
5281 /* PENDING: check this for the mcs51 */
5282 stackPtr = -port->stack.direction * port->stack.call_overhead;
5283 if (IFFUNC_ISISR (name->type))
5284 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5285 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5286 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5288 xstackPtr = -port->stack.direction * port->stack.call_overhead;
5290 fetype = getSpec (name->type); /* get the specifier for the function */
5291 /* if this is a reentrant function then */
5292 if (IFFUNC_ISREENT (name->type))
5295 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5297 /* do processing for parameters that are passed in registers */
5298 processRegParms (FUNC_ARGS(name->type), body);
5300 /* set the stack pointer */
5304 /* allocate & autoinit the block variables */
5305 processBlockVars (body, &stack, ALLOCATE);
5307 /* save the stack information */
5308 if (options.useXstack)
5309 name->xstack = SPEC_STAK (fetype) = stack;
5311 name->stack = SPEC_STAK (fetype) = stack;
5313 /* name needs to be mangled */
5314 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5316 body = resolveSymbols (body); /* resolve the symbols */
5317 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5320 ex = newAst_VALUE (symbolVal (name)); /* create name */
5321 ex = newNode (FUNCTION, ex, body);
5322 ex->values.args = FUNC_ARGS(name->type);
5324 if (options.dump_tree) PA(ex);
5327 werror (E_FUNC_NO_CODE, name->name);
5331 /* create the node & generate intermediate code */
5333 codeOutFile = code->oFile;
5334 piCode = iCodeFromAst (ex);
5338 werror (E_FUNC_NO_CODE, name->name);
5342 eBBlockFromiCode (piCode);
5344 /* if there are any statics then do them */
5347 GcurMemmap = statsg;
5348 codeOutFile = statsg->oFile;
5349 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_CHECK)));
5355 /* dealloc the block variables */
5356 processBlockVars (body, &stack, DEALLOCATE);
5357 outputDebugStackSymbols();
5358 /* deallocate paramaters */
5359 deallocParms (FUNC_ARGS(name->type));
5361 if (IFFUNC_ISREENT (name->type))
5364 /* we are done freeup memory & cleanup */
5366 if (port->reset_labelKey) labelKey = 1;
5368 FUNC_HASBODY(name->type) = 1;
5369 addSet (&operKeyReset, name);
5370 applyToSet (operKeyReset, resetParmKey);
5375 cleanUpLevel (LabelTab, 0);
5376 cleanUpBlock (StructTab, 1);
5377 cleanUpBlock (TypedefTab, 1);
5379 xstack->syms = NULL;
5380 istack->syms = NULL;
5385 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
5386 /*-----------------------------------------------------------------*/
5387 /* ast_print : prints the ast (for debugging purposes) */
5388 /*-----------------------------------------------------------------*/
5390 void ast_print (ast * tree, FILE *outfile, int indent)
5395 /* can print only decorated trees */
5396 if (!tree->decorated) return;
5398 /* if any child is an error | this one is an error do nothing */
5399 if (tree->isError ||
5400 (tree->left && tree->left->isError) ||
5401 (tree->right && tree->right->isError)) {
5402 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
5406 /* print the line */
5407 /* if not block & function */
5408 if (tree->type == EX_OP &&
5409 (tree->opval.op != FUNCTION &&
5410 tree->opval.op != BLOCK &&
5411 tree->opval.op != NULLOP)) {
5414 if (tree->opval.op == FUNCTION) {
5416 value *args=FUNC_ARGS(tree->left->opval.val->type);
5417 fprintf(outfile,"FUNCTION (%s=%p) type (",
5418 tree->left->opval.val->name, tree);
5419 printTypeChain (tree->left->opval.val->type->next,outfile);
5420 fprintf(outfile,") args (");
5423 fprintf (outfile, ", ");
5425 printTypeChain (args ? args->type : NULL, outfile);
5427 args= args ? args->next : NULL;
5429 fprintf(outfile,")\n");
5430 ast_print(tree->left,outfile,indent);
5431 ast_print(tree->right,outfile,indent);
5434 if (tree->opval.op == BLOCK) {
5435 symbol *decls = tree->values.sym;
5436 INDENT(indent,outfile);
5437 fprintf(outfile,"{\n");
5439 INDENT(indent+2,outfile);
5440 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
5441 decls->name, decls);
5442 printTypeChain(decls->type,outfile);
5443 fprintf(outfile,")\n");
5445 decls = decls->next;
5447 ast_print(tree->right,outfile,indent+2);
5448 INDENT(indent,outfile);
5449 fprintf(outfile,"}\n");
5452 if (tree->opval.op == NULLOP) {
5453 ast_print(tree->left,outfile,indent);
5454 ast_print(tree->right,outfile,indent);
5457 INDENT(indent,outfile);
5459 /*------------------------------------------------------------------*/
5460 /*----------------------------*/
5461 /* leaf has been reached */
5462 /*----------------------------*/
5463 /* if this is of type value */
5464 /* just get the type */
5465 if (tree->type == EX_VALUE) {
5467 if (IS_LITERAL (tree->opval.val->etype)) {
5468 fprintf(outfile,"CONSTANT (%p) value = ", tree);
5469 if (SPEC_USIGN (tree->opval.val->etype))
5470 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
5472 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
5473 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
5474 floatFromVal(tree->opval.val));
5475 } else if (tree->opval.val->sym) {
5476 /* if the undefined flag is set then give error message */
5477 if (tree->opval.val->sym->undefined) {
5478 fprintf(outfile,"UNDEFINED SYMBOL ");
5480 fprintf(outfile,"SYMBOL ");
5482 fprintf(outfile,"(%s=%p)",
5483 tree->opval.val->sym->name,tree);
5486 fprintf(outfile," type (");
5487 printTypeChain(tree->ftype,outfile);
5488 fprintf(outfile,")\n");
5490 fprintf(outfile,"\n");
5495 /* if type link for the case of cast */
5496 if (tree->type == EX_LINK) {
5497 fprintf(outfile,"TYPENODE (%p) type = (",tree);
5498 printTypeChain(tree->opval.lnk,outfile);
5499 fprintf(outfile,")\n");
5504 /* depending on type of operator do */
5506 switch (tree->opval.op) {
5507 /*------------------------------------------------------------------*/
5508 /*----------------------------*/
5510 /*----------------------------*/
5512 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
5513 printTypeChain(tree->ftype,outfile);
5514 fprintf(outfile,")\n");
5515 ast_print(tree->left,outfile,indent+2);
5516 ast_print(tree->right,outfile,indent+2);
5519 /*------------------------------------------------------------------*/
5520 /*----------------------------*/
5522 /*----------------------------*/
5524 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
5525 printTypeChain(tree->ftype,outfile);
5526 fprintf(outfile,")\n");
5527 ast_print(tree->left,outfile,indent+2);
5528 ast_print(tree->right,outfile,indent+2);
5531 /*------------------------------------------------------------------*/
5532 /*----------------------------*/
5533 /* struct/union pointer */
5534 /*----------------------------*/
5536 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
5537 printTypeChain(tree->ftype,outfile);
5538 fprintf(outfile,")\n");
5539 ast_print(tree->left,outfile,indent+2);
5540 ast_print(tree->right,outfile,indent+2);
5543 /*------------------------------------------------------------------*/
5544 /*----------------------------*/
5545 /* ++/-- operation */
5546 /*----------------------------*/
5549 fprintf(outfile,"post-");
5551 fprintf(outfile,"pre-");
5552 fprintf(outfile,"INC_OP (%p) type (",tree);
5553 printTypeChain(tree->ftype,outfile);
5554 fprintf(outfile,")\n");
5555 ast_print(tree->left,outfile,indent+2); /* postincrement case */
5556 ast_print(tree->right,outfile,indent+2); /* preincrement case */
5561 fprintf(outfile,"post-");
5563 fprintf(outfile,"pre-");
5564 fprintf(outfile,"DEC_OP (%p) type (",tree);
5565 printTypeChain(tree->ftype,outfile);
5566 fprintf(outfile,")\n");
5567 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
5568 ast_print(tree->right,outfile,indent+2); /* predecrement case */
5571 /*------------------------------------------------------------------*/
5572 /*----------------------------*/
5574 /*----------------------------*/
5577 fprintf(outfile,"& (%p) type (",tree);
5578 printTypeChain(tree->ftype,outfile);
5579 fprintf(outfile,")\n");
5580 ast_print(tree->left,outfile,indent+2);
5581 ast_print(tree->right,outfile,indent+2);
5583 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
5584 printTypeChain(tree->ftype,outfile);
5585 fprintf(outfile,")\n");
5586 ast_print(tree->left,outfile,indent+2);
5587 ast_print(tree->right,outfile,indent+2);
5590 /*----------------------------*/
5592 /*----------------------------*/
5594 fprintf(outfile,"OR (%p) type (",tree);
5595 printTypeChain(tree->ftype,outfile);
5596 fprintf(outfile,")\n");
5597 ast_print(tree->left,outfile,indent+2);
5598 ast_print(tree->right,outfile,indent+2);
5600 /*------------------------------------------------------------------*/
5601 /*----------------------------*/
5603 /*----------------------------*/
5605 fprintf(outfile,"XOR (%p) type (",tree);
5606 printTypeChain(tree->ftype,outfile);
5607 fprintf(outfile,")\n");
5608 ast_print(tree->left,outfile,indent+2);
5609 ast_print(tree->right,outfile,indent+2);
5612 /*------------------------------------------------------------------*/
5613 /*----------------------------*/
5615 /*----------------------------*/
5617 fprintf(outfile,"DIV (%p) type (",tree);
5618 printTypeChain(tree->ftype,outfile);
5619 fprintf(outfile,")\n");
5620 ast_print(tree->left,outfile,indent+2);
5621 ast_print(tree->right,outfile,indent+2);
5623 /*------------------------------------------------------------------*/
5624 /*----------------------------*/
5626 /*----------------------------*/
5628 fprintf(outfile,"MOD (%p) type (",tree);
5629 printTypeChain(tree->ftype,outfile);
5630 fprintf(outfile,")\n");
5631 ast_print(tree->left,outfile,indent+2);
5632 ast_print(tree->right,outfile,indent+2);
5635 /*------------------------------------------------------------------*/
5636 /*----------------------------*/
5637 /* address dereference */
5638 /*----------------------------*/
5639 case '*': /* can be unary : if right is null then unary operation */
5641 fprintf(outfile,"DEREF (%p) type (",tree);
5642 printTypeChain(tree->ftype,outfile);
5643 fprintf(outfile,")\n");
5644 ast_print(tree->left,outfile,indent+2);
5647 /*------------------------------------------------------------------*/
5648 /*----------------------------*/
5649 /* multiplication */
5650 /*----------------------------*/
5651 fprintf(outfile,"MULT (%p) type (",tree);
5652 printTypeChain(tree->ftype,outfile);
5653 fprintf(outfile,")\n");
5654 ast_print(tree->left,outfile,indent+2);
5655 ast_print(tree->right,outfile,indent+2);
5659 /*------------------------------------------------------------------*/
5660 /*----------------------------*/
5661 /* unary '+' operator */
5662 /*----------------------------*/
5666 fprintf(outfile,"UPLUS (%p) type (",tree);
5667 printTypeChain(tree->ftype,outfile);
5668 fprintf(outfile,")\n");
5669 ast_print(tree->left,outfile,indent+2);
5671 /*------------------------------------------------------------------*/
5672 /*----------------------------*/
5674 /*----------------------------*/
5675 fprintf(outfile,"ADD (%p) type (",tree);
5676 printTypeChain(tree->ftype,outfile);
5677 fprintf(outfile,")\n");
5678 ast_print(tree->left,outfile,indent+2);
5679 ast_print(tree->right,outfile,indent+2);
5682 /*------------------------------------------------------------------*/
5683 /*----------------------------*/
5685 /*----------------------------*/
5686 case '-': /* can be unary */
5688 fprintf(outfile,"UMINUS (%p) type (",tree);
5689 printTypeChain(tree->ftype,outfile);
5690 fprintf(outfile,")\n");
5691 ast_print(tree->left,outfile,indent+2);
5693 /*------------------------------------------------------------------*/
5694 /*----------------------------*/
5696 /*----------------------------*/
5697 fprintf(outfile,"SUB (%p) type (",tree);
5698 printTypeChain(tree->ftype,outfile);
5699 fprintf(outfile,")\n");
5700 ast_print(tree->left,outfile,indent+2);
5701 ast_print(tree->right,outfile,indent+2);
5704 /*------------------------------------------------------------------*/
5705 /*----------------------------*/
5707 /*----------------------------*/
5709 fprintf(outfile,"COMPL (%p) type (",tree);
5710 printTypeChain(tree->ftype,outfile);
5711 fprintf(outfile,")\n");
5712 ast_print(tree->left,outfile,indent+2);
5714 /*------------------------------------------------------------------*/
5715 /*----------------------------*/
5717 /*----------------------------*/
5719 fprintf(outfile,"NOT (%p) type (",tree);
5720 printTypeChain(tree->ftype,outfile);
5721 fprintf(outfile,")\n");
5722 ast_print(tree->left,outfile,indent+2);
5724 /*------------------------------------------------------------------*/
5725 /*----------------------------*/
5727 /*----------------------------*/
5729 fprintf(outfile,"RRC (%p) type (",tree);
5730 printTypeChain(tree->ftype,outfile);
5731 fprintf(outfile,")\n");
5732 ast_print(tree->left,outfile,indent+2);
5736 fprintf(outfile,"RLC (%p) type (",tree);
5737 printTypeChain(tree->ftype,outfile);
5738 fprintf(outfile,")\n");
5739 ast_print(tree->left,outfile,indent+2);
5742 fprintf(outfile,"SWAP (%p) type (",tree);
5743 printTypeChain(tree->ftype,outfile);
5744 fprintf(outfile,")\n");
5745 ast_print(tree->left,outfile,indent+2);
5748 fprintf(outfile,"GETHBIT (%p) type (",tree);
5749 printTypeChain(tree->ftype,outfile);
5750 fprintf(outfile,")\n");
5751 ast_print(tree->left,outfile,indent+2);
5754 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
5755 printTypeChain(tree->ftype,outfile);
5756 fprintf(outfile,")\n");
5757 ast_print(tree->left,outfile,indent+2);
5758 ast_print(tree->right,outfile,indent+2);
5761 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
5762 printTypeChain(tree->ftype,outfile);
5763 fprintf(outfile,")\n");
5764 ast_print(tree->left,outfile,indent+2);
5765 ast_print(tree->right,outfile,indent+2);
5767 /*------------------------------------------------------------------*/
5768 /*----------------------------*/
5770 /*----------------------------*/
5771 case CAST: /* change the type */
5772 fprintf(outfile,"CAST (%p) from type (",tree);
5773 printTypeChain(tree->right->ftype,outfile);
5774 fprintf(outfile,") to type (");
5775 printTypeChain(tree->ftype,outfile);
5776 fprintf(outfile,")\n");
5777 ast_print(tree->right,outfile,indent+2);
5781 fprintf(outfile,"ANDAND (%p) type (",tree);
5782 printTypeChain(tree->ftype,outfile);
5783 fprintf(outfile,")\n");
5784 ast_print(tree->left,outfile,indent+2);
5785 ast_print(tree->right,outfile,indent+2);
5788 fprintf(outfile,"OROR (%p) type (",tree);
5789 printTypeChain(tree->ftype,outfile);
5790 fprintf(outfile,")\n");
5791 ast_print(tree->left,outfile,indent+2);
5792 ast_print(tree->right,outfile,indent+2);
5795 /*------------------------------------------------------------------*/
5796 /*----------------------------*/
5797 /* comparison operators */
5798 /*----------------------------*/
5800 fprintf(outfile,"GT(>) (%p) type (",tree);
5801 printTypeChain(tree->ftype,outfile);
5802 fprintf(outfile,")\n");
5803 ast_print(tree->left,outfile,indent+2);
5804 ast_print(tree->right,outfile,indent+2);
5807 fprintf(outfile,"LT(<) (%p) type (",tree);
5808 printTypeChain(tree->ftype,outfile);
5809 fprintf(outfile,")\n");
5810 ast_print(tree->left,outfile,indent+2);
5811 ast_print(tree->right,outfile,indent+2);
5814 fprintf(outfile,"LE(<=) (%p) type (",tree);
5815 printTypeChain(tree->ftype,outfile);
5816 fprintf(outfile,")\n");
5817 ast_print(tree->left,outfile,indent+2);
5818 ast_print(tree->right,outfile,indent+2);
5821 fprintf(outfile,"GE(>=) (%p) type (",tree);
5822 printTypeChain(tree->ftype,outfile);
5823 fprintf(outfile,")\n");
5824 ast_print(tree->left,outfile,indent+2);
5825 ast_print(tree->right,outfile,indent+2);
5828 fprintf(outfile,"EQ(==) (%p) type (",tree);
5829 printTypeChain(tree->ftype,outfile);
5830 fprintf(outfile,")\n");
5831 ast_print(tree->left,outfile,indent+2);
5832 ast_print(tree->right,outfile,indent+2);
5835 fprintf(outfile,"NE(!=) (%p) type (",tree);
5836 printTypeChain(tree->ftype,outfile);
5837 fprintf(outfile,")\n");
5838 ast_print(tree->left,outfile,indent+2);
5839 ast_print(tree->right,outfile,indent+2);
5840 /*------------------------------------------------------------------*/
5841 /*----------------------------*/
5843 /*----------------------------*/
5844 case SIZEOF: /* evaluate wihout code generation */
5845 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
5848 /*------------------------------------------------------------------*/
5849 /*----------------------------*/
5850 /* conditional operator '?' */
5851 /*----------------------------*/
5853 fprintf(outfile,"QUEST(?) (%p) type (",tree);
5854 printTypeChain(tree->ftype,outfile);
5855 fprintf(outfile,")\n");
5856 ast_print(tree->left,outfile,indent+2);
5857 ast_print(tree->right,outfile,indent+2);
5861 fprintf(outfile,"COLON(:) (%p) type (",tree);
5862 printTypeChain(tree->ftype,outfile);
5863 fprintf(outfile,")\n");
5864 ast_print(tree->left,outfile,indent+2);
5865 ast_print(tree->right,outfile,indent+2);
5868 /*------------------------------------------------------------------*/
5869 /*----------------------------*/
5870 /* assignment operators */
5871 /*----------------------------*/
5873 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
5874 printTypeChain(tree->ftype,outfile);
5875 fprintf(outfile,")\n");
5876 ast_print(tree->left,outfile,indent+2);
5877 ast_print(tree->right,outfile,indent+2);
5880 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
5881 printTypeChain(tree->ftype,outfile);
5882 fprintf(outfile,")\n");
5883 ast_print(tree->left,outfile,indent+2);
5884 ast_print(tree->right,outfile,indent+2);
5887 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
5888 printTypeChain(tree->ftype,outfile);
5889 fprintf(outfile,")\n");
5890 ast_print(tree->left,outfile,indent+2);
5891 ast_print(tree->right,outfile,indent+2);
5894 fprintf(outfile,"ORASS(|=) (%p) type (",tree);
5895 printTypeChain(tree->ftype,outfile);
5896 fprintf(outfile,")\n");
5897 ast_print(tree->left,outfile,indent+2);
5898 ast_print(tree->right,outfile,indent+2);
5901 fprintf(outfile,"XORASS(^=) (%p) type (",tree);
5902 printTypeChain(tree->ftype,outfile);
5903 fprintf(outfile,")\n");
5904 ast_print(tree->left,outfile,indent+2);
5905 ast_print(tree->right,outfile,indent+2);
5908 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
5909 printTypeChain(tree->ftype,outfile);
5910 fprintf(outfile,")\n");
5911 ast_print(tree->left,outfile,indent+2);
5912 ast_print(tree->right,outfile,indent+2);
5915 fprintf(outfile,"LSHFTASS(<<=) (%p) type (",tree);
5916 printTypeChain(tree->ftype,outfile);
5917 fprintf(outfile,")\n");
5918 ast_print(tree->left,outfile,indent+2);
5919 ast_print(tree->right,outfile,indent+2);
5921 /*------------------------------------------------------------------*/
5922 /*----------------------------*/
5924 /*----------------------------*/
5926 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
5927 printTypeChain(tree->ftype,outfile);
5928 fprintf(outfile,")\n");
5929 ast_print(tree->left,outfile,indent+2);
5930 ast_print(tree->right,outfile,indent+2);
5932 /*------------------------------------------------------------------*/
5933 /*----------------------------*/
5935 /*----------------------------*/
5937 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
5938 printTypeChain(tree->ftype,outfile);
5939 fprintf(outfile,")\n");
5940 ast_print(tree->left,outfile,indent+2);
5941 ast_print(tree->right,outfile,indent+2);
5943 /*------------------------------------------------------------------*/
5944 /*----------------------------*/
5945 /* straight assignemnt */
5946 /*----------------------------*/
5948 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
5949 printTypeChain(tree->ftype,outfile);
5950 fprintf(outfile,")\n");
5951 ast_print(tree->left,outfile,indent+2);
5952 ast_print(tree->right,outfile,indent+2);
5954 /*------------------------------------------------------------------*/
5955 /*----------------------------*/
5956 /* comma operator */
5957 /*----------------------------*/
5959 fprintf(outfile,"COMMA(,) (%p) type (",tree);
5960 printTypeChain(tree->ftype,outfile);
5961 fprintf(outfile,")\n");
5962 ast_print(tree->left,outfile,indent+2);
5963 ast_print(tree->right,outfile,indent+2);
5965 /*------------------------------------------------------------------*/
5966 /*----------------------------*/
5968 /*----------------------------*/
5971 fprintf(outfile,"CALL (%p) type (",tree);
5972 printTypeChain(tree->ftype,outfile);
5973 fprintf(outfile,")\n");
5974 ast_print(tree->left,outfile,indent+2);
5975 ast_print(tree->right,outfile,indent+2);
5978 fprintf(outfile,"PARMS\n");
5979 ast_print(tree->left,outfile,indent+2);
5980 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
5981 ast_print(tree->right,outfile,indent+2);
5984 /*------------------------------------------------------------------*/
5985 /*----------------------------*/
5986 /* return statement */
5987 /*----------------------------*/
5989 fprintf(outfile,"RETURN (%p) type (",tree);
5991 printTypeChain(tree->right->ftype,outfile);
5993 fprintf(outfile,")\n");
5994 ast_print(tree->right,outfile,indent+2);
5996 /*------------------------------------------------------------------*/
5997 /*----------------------------*/
5998 /* label statement */
5999 /*----------------------------*/
6001 fprintf(outfile,"LABEL (%p)\n",tree);
6002 ast_print(tree->left,outfile,indent+2);
6003 ast_print(tree->right,outfile,indent);
6005 /*------------------------------------------------------------------*/
6006 /*----------------------------*/
6007 /* switch statement */
6008 /*----------------------------*/
6012 fprintf(outfile,"SWITCH (%p) ",tree);
6013 ast_print(tree->left,outfile,0);
6014 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
6015 INDENT(indent+2,outfile);
6016 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
6017 (int) floatFromVal(val),
6018 tree->values.switchVals.swNum,
6019 (int) floatFromVal(val));
6021 ast_print(tree->right,outfile,indent);
6024 /*------------------------------------------------------------------*/
6025 /*----------------------------*/
6027 /*----------------------------*/
6029 fprintf(outfile,"IF (%p) \n",tree);
6030 ast_print(tree->left,outfile,indent+2);
6031 if (tree->trueLabel) {
6032 INDENT(indent+2,outfile);
6033 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
6035 if (tree->falseLabel) {
6036 INDENT(indent+2,outfile);
6037 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
6039 ast_print(tree->right,outfile,indent+2);
6041 /*----------------------------*/
6042 /* goto Statement */
6043 /*----------------------------*/
6045 fprintf(outfile,"GOTO (%p) \n",tree);
6046 ast_print(tree->left,outfile,indent+2);
6047 fprintf(outfile,"\n");
6049 /*------------------------------------------------------------------*/
6050 /*----------------------------*/
6052 /*----------------------------*/
6054 fprintf(outfile,"FOR (%p) \n",tree);
6055 if (AST_FOR( tree, initExpr)) {
6056 INDENT(indent+2,outfile);
6057 fprintf(outfile,"INIT EXPR ");
6058 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6060 if (AST_FOR( tree, condExpr)) {
6061 INDENT(indent+2,outfile);
6062 fprintf(outfile,"COND EXPR ");
6063 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6065 if (AST_FOR( tree, loopExpr)) {
6066 INDENT(indent+2,outfile);
6067 fprintf(outfile,"LOOP EXPR ");
6068 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6070 fprintf(outfile,"FOR LOOP BODY \n");
6071 ast_print(tree->left,outfile,indent+2);
6074 fprintf(outfile,"CRITICAL (%p) \n",tree);
6075 ast_print(tree->left,outfile,indent+2);
6083 ast_print(t,stdout,0);
6088 /*-----------------------------------------------------------------*/
6089 /* astErrors : returns non-zero if errors present in tree */
6090 /*-----------------------------------------------------------------*/
6091 int astErrors(ast *t)
6100 if (t->type == EX_VALUE
6101 && t->opval.val->sym
6102 && t->opval.val->sym->undefined)
6105 errors += astErrors(t->left);
6106 errors += astErrors(t->right);