1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
29 set *operKeyReset = NULL;
30 ast *staticAutos = NULL;
33 #define LRVAL(x) x->left->rvalue
34 #define RRVAL(x) x->right->rvalue
35 #define TRVAL(x) x->rvalue
36 #define LLVAL(x) x->left->lvalue
37 #define RLVAL(x) x->right->lvalue
38 #define TLVAL(x) x->lvalue
39 #define RTYPE(x) x->right->ftype
40 #define RETYPE(x) x->right->etype
41 #define LTYPE(x) x->left->ftype
42 #define LETYPE(x) x->left->etype
43 #define TTYPE(x) x->ftype
44 #define TETYPE(x) x->etype
50 symbol *currFunc=NULL;
51 static ast *createIval (ast *, sym_link *, initList *, ast *);
52 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
53 static ast *optimizeCompare (ast *);
54 ast *optimizeRRCRLC (ast *);
55 ast *optimizeSWAP (ast *);
56 ast *optimizeGetHbit (ast *);
57 ast *backPatchLabels (ast *, symbol *, symbol *);
60 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
65 printTypeChain (tree->ftype, stdout);
70 /*-----------------------------------------------------------------*/
71 /* newAst - creates a fresh node for an expression tree */
72 /*-----------------------------------------------------------------*/
74 newAst_ (unsigned type)
77 static int oldLineno = 0;
79 ex = Safe_alloc ( sizeof (ast));
82 ex->lineno = (noLineno ? oldLineno : mylineno);
83 ex->filename = currFname;
84 ex->level = NestLevel;
85 ex->block = currBlockno;
86 ex->initMode = inInitMode;
87 ex->seqPoint = seqPointNo;
92 newAst_VALUE (value * val)
94 ast *ex = newAst_ (EX_VALUE);
100 newAst_OP (unsigned op)
102 ast *ex = newAst_ (EX_OP);
108 newAst_LINK (sym_link * val)
110 ast *ex = newAst_ (EX_LINK);
115 /*-----------------------------------------------------------------*/
116 /* newNode - creates a new node */
117 /*-----------------------------------------------------------------*/
119 newNode (long op, ast * left, ast * right)
130 /*-----------------------------------------------------------------*/
131 /* newIfxNode - creates a new Ifx Node */
132 /*-----------------------------------------------------------------*/
134 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
138 /* if this is a literal then we already know the result */
139 if (condAst->etype && IS_LITERAL (condAst->etype))
141 /* then depending on the expression value */
142 if (floatFromVal (condAst->opval.val))
143 ifxNode = newNode (GOTO,
144 newAst_VALUE (symbolVal (trueLabel)),
147 ifxNode = newNode (GOTO,
148 newAst_VALUE (symbolVal (falseLabel)),
153 ifxNode = newNode (IFX, condAst, NULL);
154 ifxNode->trueLabel = trueLabel;
155 ifxNode->falseLabel = falseLabel;
161 /*-----------------------------------------------------------------*/
162 /* copyAstValues - copies value portion of ast if needed */
163 /*-----------------------------------------------------------------*/
165 copyAstValues (ast * dest, ast * src)
167 switch (src->opval.op)
170 dest->values.sym = copySymbolChain (src->values.sym);
174 dest->values.switchVals.swVals =
175 copyValue (src->values.switchVals.swVals);
176 dest->values.switchVals.swDefault =
177 src->values.switchVals.swDefault;
178 dest->values.switchVals.swNum =
179 src->values.switchVals.swNum;
183 dest->values.inlineasm = Safe_strdup(src->values.inlineasm);
187 dest->values.constlist = copyLiteralList(src->values.constlist);
191 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
192 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
193 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
194 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
195 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
196 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
197 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
202 /*-----------------------------------------------------------------*/
203 /* copyAst - makes a copy of a given astession */
204 /*-----------------------------------------------------------------*/
213 dest = Safe_alloc ( sizeof (ast));
215 dest->type = src->type;
216 dest->lineno = src->lineno;
217 dest->level = src->level;
218 dest->funcName = src->funcName;
221 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
223 /* if this is a leaf */
225 if (src->type == EX_VALUE)
227 dest->opval.val = copyValue (src->opval.val);
232 if (src->type == EX_LINK)
234 dest->opval.lnk = copyLinkChain (src->opval.lnk);
238 dest->opval.op = src->opval.op;
240 /* if this is a node that has special values */
241 copyAstValues (dest, src);
243 dest->trueLabel = copySymbol (src->trueLabel);
244 dest->falseLabel = copySymbol (src->falseLabel);
245 dest->left = copyAst (src->left);
246 dest->right = copyAst (src->right);
252 /*-----------------------------------------------------------------*/
253 /* removeIncDecOps: remove for side effects in *_ASSIGN's */
254 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
255 /*-----------------------------------------------------------------*/
256 ast *removeIncDecOps (ast * tree) {
258 // traverse the tree and remove inc/dec ops
263 if (tree->type == EX_OP &&
264 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
271 tree->left=removeIncDecOps(tree->left);
272 tree->right=removeIncDecOps(tree->right);
277 /*-----------------------------------------------------------------*/
278 /* removePreIncDecOps: remove for side effects in *_ASSIGN's */
279 /* "*++s += 3" -> "*++s = *++s + 3" */
280 /*-----------------------------------------------------------------*/
281 ast *removePreIncDecOps (ast * tree) {
283 // traverse the tree and remove pre-inc/dec ops
288 if (tree->type == EX_OP &&
289 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
294 tree->left=removePreIncDecOps(tree->left);
295 tree->right=removePreIncDecOps(tree->right);
300 /*-----------------------------------------------------------------*/
301 /* removePostIncDecOps: remove for side effects in *_ASSIGN's */
302 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
303 /*-----------------------------------------------------------------*/
304 ast *removePostIncDecOps (ast * tree) {
306 // traverse the tree and remove pre-inc/dec ops
311 if (tree->type == EX_OP &&
312 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
317 tree->left=removePostIncDecOps(tree->left);
318 tree->right=removePostIncDecOps(tree->right);
323 /*-----------------------------------------------------------------*/
324 /* hasSEFcalls - returns TRUE if tree has a function call */
325 /*-----------------------------------------------------------------*/
327 hasSEFcalls (ast * tree)
332 if (tree->type == EX_OP &&
333 (tree->opval.op == CALL ||
334 tree->opval.op == PCALL ||
335 tree->opval.op == '=' ||
336 tree->opval.op == INC_OP ||
337 tree->opval.op == DEC_OP))
340 return (hasSEFcalls (tree->left) |
341 hasSEFcalls (tree->right));
344 /*-----------------------------------------------------------------*/
345 /* isAstEqual - compares two asts & returns 1 if they are equal */
346 /*-----------------------------------------------------------------*/
348 isAstEqual (ast * t1, ast * t2)
357 if (t1->type != t2->type)
363 if (t1->opval.op != t2->opval.op)
365 return (isAstEqual (t1->left, t2->left) &&
366 isAstEqual (t1->right, t2->right));
370 if (t1->opval.val->sym)
372 if (!t2->opval.val->sym)
375 return isSymbolEqual (t1->opval.val->sym,
380 if (t2->opval.val->sym)
383 return (floatFromVal (t1->opval.val) ==
384 floatFromVal (t2->opval.val));
388 /* only compare these two types */
396 /*-----------------------------------------------------------------*/
397 /* resolveSymbols - resolve symbols from the symbol table */
398 /*-----------------------------------------------------------------*/
400 resolveSymbols (ast * tree)
402 /* walk the entire tree and check for values */
403 /* with symbols if we find one then replace */
404 /* symbol with that from the symbol table */
411 /* if not block & function */
412 if (tree->type == EX_OP &&
413 (tree->opval.op != FUNCTION &&
414 tree->opval.op != BLOCK &&
415 tree->opval.op != NULLOP))
417 filename = tree->filename;
418 lineno = tree->lineno;
422 /* make sure we resolve the true & false labels for ifx */
423 if (tree->type == EX_OP && tree->opval.op == IFX)
429 if ((csym = findSym (LabelTab, tree->trueLabel,
430 tree->trueLabel->name)))
431 tree->trueLabel = csym;
433 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
434 tree->trueLabel->name);
437 if (tree->falseLabel)
439 if ((csym = findSym (LabelTab,
441 tree->falseLabel->name)))
442 tree->falseLabel = csym;
444 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
445 tree->falseLabel->name);
450 /* if this is a label resolve it from the labelTab */
451 if (IS_AST_VALUE (tree) &&
452 tree->opval.val->sym &&
453 tree->opval.val->sym->islbl)
456 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
457 tree->opval.val->sym->name);
460 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
461 tree->opval.val->sym->name);
463 tree->opval.val->sym = csym;
465 goto resolveChildren;
468 /* do only for leafs */
469 if (IS_AST_VALUE (tree) &&
470 tree->opval.val->sym &&
471 !tree->opval.val->sym->implicit)
474 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
476 /* if found in the symbol table & they r not the same */
477 if (csym && tree->opval.val->sym != csym)
479 tree->opval.val->sym = csym;
480 tree->opval.val->type = csym->type;
481 tree->opval.val->etype = csym->etype;
484 /* if not found in the symbol table */
485 /* mark it as undefined assume it is */
486 /* an integer in data space */
487 if (!csym && !tree->opval.val->sym->implicit)
490 /* if this is a function name then */
491 /* mark it as returning an int */
494 tree->opval.val->sym->type = newLink (DECLARATOR);
495 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
496 tree->opval.val->sym->type->next =
497 tree->opval.val->sym->etype = newIntLink ();
498 tree->opval.val->etype = tree->opval.val->etype;
499 tree->opval.val->type = tree->opval.val->sym->type;
500 werrorfl (tree->filename, tree->lineno, W_IMPLICIT_FUNC,
501 tree->opval.val->sym->name);
502 //tree->opval.val->sym->undefined = 1;
503 allocVariables (tree->opval.val->sym);
507 tree->opval.val->sym->undefined = 1;
508 tree->opval.val->type =
509 tree->opval.val->etype = newIntLink ();
510 tree->opval.val->sym->type =
511 tree->opval.val->sym->etype = newIntLink ();
517 resolveSymbols (tree->left);
518 resolveSymbols (tree->right);
523 /*-----------------------------------------------------------------*/
524 /* setAstLineno - walks a ast tree & sets the line number */
525 /*-----------------------------------------------------------------*/
526 int setAstLineno (ast * tree, int lineno)
531 tree->lineno = lineno;
532 setAstLineno (tree->left, lineno);
533 setAstLineno (tree->right, lineno);
537 /*-----------------------------------------------------------------*/
538 /* funcOfType :- function of type with name */
539 /*-----------------------------------------------------------------*/
541 funcOfType (char *name, sym_link * type, sym_link * argType,
545 /* create the symbol */
546 sym = newSymbol (name, 0);
548 /* setup return value */
549 sym->type = newLink (DECLARATOR);
550 DCL_TYPE (sym->type) = FUNCTION;
551 sym->type->next = copyLinkChain (type);
552 sym->etype = getSpec (sym->type);
553 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
555 /* if arguments required */
559 args = FUNC_ARGS(sym->type) = newValue ();
563 args->type = copyLinkChain (argType);
564 args->etype = getSpec (args->type);
565 SPEC_EXTR(args->etype)=1;
568 args = args->next = newValue ();
575 allocVariables (sym);
580 /*-----------------------------------------------------------------*/
581 /* funcOfTypeVarg :- function of type with name and argtype */
582 /*-----------------------------------------------------------------*/
584 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
589 /* create the symbol */
590 sym = newSymbol (name, 0);
592 /* setup return value */
593 sym->type = newLink (DECLARATOR);
594 DCL_TYPE (sym->type) = FUNCTION;
595 sym->type->next = typeFromStr(rtype);
596 sym->etype = getSpec (sym->type);
598 /* if arguments required */
601 args = FUNC_ARGS(sym->type) = newValue ();
603 for ( i = 0 ; i < nArgs ; i++ ) {
604 args->type = typeFromStr(atypes[i]);
605 args->etype = getSpec (args->type);
606 SPEC_EXTR(args->etype)=1;
607 if ((i + 1) == nArgs) break;
608 args = args->next = newValue ();
615 allocVariables (sym);
620 /*-----------------------------------------------------------------*/
621 /* reverseParms - will reverse a parameter tree */
622 /*-----------------------------------------------------------------*/
624 reverseParms (ast * ptree)
630 /* top down if we find a nonParm tree then quit */
631 if (ptree->type == EX_OP && ptree->opval.op == PARAM)
634 ptree->left = ptree->right;
635 ptree->right = ttree;
636 reverseParms (ptree->left);
637 reverseParms (ptree->right);
643 /*-----------------------------------------------------------------*/
644 /* processParms - makes sure the parameters are okay and do some */
645 /* processing with them */
646 /*-----------------------------------------------------------------*/
648 processParms (ast *func,
651 int *parmNumber, /* unused, although updated */
654 RESULT_TYPE resultType;
657 /* if none of them exist */
658 if (!defParm && !actParm)
663 if (getenv("DEBUG_SANITY"))
665 fprintf (stderr, "processParms: %s ", defParm->name);
667 /* make sure the type is complete and sane */
668 checkTypeSanity(defParm->etype, defParm->name);
671 if (IS_CODEPTR (func->ftype))
672 functype = func->ftype->next;
674 functype = func->ftype;
676 /* if the function is being called via a pointer & */
677 /* it has not been defined a reentrant then we cannot */
678 /* have parameters */
679 if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
681 werror (W_NONRENT_ARGS);
686 /* if defined parameters ended but actual parameters */
687 /* exist and this is not defined as a variable arg */
688 if (!defParm && actParm && !IFFUNC_HASVARARGS(functype))
690 werror (E_TOO_MANY_PARMS);
694 /* if defined parameters present but no actual parameters */
695 if (defParm && !actParm)
697 werror (E_TOO_FEW_PARMS);
701 /* if this is a PARAM node then match left & right */
702 if (actParm->type == EX_OP && actParm->opval.op == PARAM)
704 actParm->decorated = 1;
705 return (processParms (func, defParm,
706 actParm->left, parmNumber, FALSE) ||
707 processParms (func, defParm ? defParm->next : NULL,
708 actParm->right, parmNumber, rightmost));
710 else if (defParm) /* not vararg */
712 /* If we have found a value node by following only right-hand links,
713 * then we know that there are no more values after us.
715 * Therefore, if there are more defined parameters, the caller didn't
718 if (rightmost && defParm->next)
720 werror (E_TOO_FEW_PARMS);
725 /* decorate parameter */
726 resultType = defParm ? getResultTypeFromType (defParm->etype) :
728 actParm = decorateType (actParm, resultType);
730 if (IS_VOID(actParm->ftype))
732 werror (E_VOID_VALUE_USED);
736 /* If this is a varargs function... */
737 if (!defParm && actParm && IFFUNC_HASVARARGS(functype))
742 if (IS_CAST_OP (actParm)
743 || (IS_AST_LIT_VALUE (actParm) && actParm->values.literalFromCast))
745 /* Parameter was explicitly typecast; don't touch it. */
749 ftype = actParm->ftype;
751 /* If it's a char, upcast to int. */
752 if (IS_INTEGRAL (ftype)
753 && (getSize (ftype) < (unsigned) INTSIZE))
755 newType = newAst_LINK(INTTYPE);
758 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
760 newType = newAst_LINK (copyLinkChain(ftype));
761 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
764 if (IS_AGGREGATE (ftype))
766 newType = newAst_LINK (copyLinkChain (ftype));
767 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
772 /* cast required; change this op to a cast. */
773 ast *parmCopy = resolveSymbols (copyAst (actParm));
775 actParm->type = EX_OP;
776 actParm->opval.op = CAST;
777 actParm->left = newType;
778 actParm->right = parmCopy;
779 actParm->decorated = 0; /* force typechecking */
780 decorateType (actParm, RESULT_TYPE_NONE);
785 /* if defined parameters ended but actual has not & */
787 if (!defParm && actParm &&
788 (options.stackAuto || IFFUNC_ISREENT (functype)))
791 resolveSymbols (actParm);
793 /* the parameter type must be at least castable */
794 if (compareType (defParm->type, actParm->ftype) == 0)
796 werror (E_INCOMPAT_TYPES);
797 printFromToType (actParm->ftype, defParm->type);
801 /* if the parameter is castable then add the cast */
802 if (compareType (defParm->type, actParm->ftype) < 0)
806 resultType = getResultTypeFromType (defParm->etype);
807 pTree = resolveSymbols (copyAst (actParm));
809 /* now change the current one to a cast */
810 actParm->type = EX_OP;
811 actParm->opval.op = CAST;
812 actParm->left = newAst_LINK (defParm->type);
813 actParm->right = pTree;
814 actParm->decorated = 0; /* force typechecking */
815 decorateType (actParm, resultType);
818 /* make a copy and change the regparm type to the defined parm */
819 actParm->etype = getSpec (actParm->ftype = copyLinkChain (actParm->ftype));
820 SPEC_REGPARM (actParm->etype) = SPEC_REGPARM (defParm->etype);
821 SPEC_ARGREG (actParm->etype) = SPEC_ARGREG (defParm->etype);
826 /*-----------------------------------------------------------------*/
827 /* createIvalType - generates ival for basic types */
828 /*-----------------------------------------------------------------*/
830 createIvalType (ast * sym, sym_link * type, initList * ilist)
834 /* if initList is deep */
835 if (ilist->type == INIT_DEEP)
836 ilist = ilist->init.deep;
838 iExpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK);
839 return decorateType (newNode ('=', sym, iExpr), RESULT_CHECK);
842 /*-----------------------------------------------------------------*/
843 /* createIvalStruct - generates initial value for structures */
844 /*-----------------------------------------------------------------*/
846 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
853 sflds = SPEC_STRUCT (type)->fields;
854 if (ilist->type != INIT_DEEP)
856 werror (E_INIT_STRUCT, "");
860 iloop = ilist->init.deep;
862 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
864 /* if we have come to end */
868 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
869 lAst = decorateType (resolveSymbols (lAst), RESULT_CHECK);
870 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type, iloop, rast)), RESULT_CHECK);
874 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
875 W_EXCESS_INITIALIZERS, "struct",
876 sym->opval.val->sym->name);
883 /*-----------------------------------------------------------------*/
884 /* createIvalArray - generates code for array initialization */
885 /*-----------------------------------------------------------------*/
887 createIvalArray (ast * sym, sym_link * type, initList * ilist)
891 int lcnt = 0, size = 0;
892 literalList *literalL;
894 /* take care of the special case */
895 /* array of characters can be init */
897 if (IS_CHAR (type->next))
898 if ((rast = createIvalCharPtr (sym,
900 decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK))))
902 return decorateType (resolveSymbols (rast), RESULT_CHECK);
904 /* not the special case */
905 if (ilist->type != INIT_DEEP)
907 werror (E_INIT_STRUCT, "");
911 iloop = ilist->init.deep;
912 lcnt = DCL_ELEM (type);
914 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
918 aSym = decorateType (resolveSymbols(sym), RESULT_CHECK);
920 rast = newNode(ARRAYINIT, aSym, NULL);
921 rast->values.constlist = literalL;
923 // Make sure size is set to length of initializer list.
930 if (lcnt && size > lcnt)
932 // Array size was specified, and we have more initializers than needed.
933 char *name=sym->opval.val->sym->name;
934 int lineno=sym->opval.val->sym->lineDef;
935 char *filename=sym->opval.val->sym->fileDef;
937 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
946 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
947 aSym = decorateType (resolveSymbols (aSym), RESULT_CHECK);
948 rast = createIval (aSym, type->next, iloop, rast);
949 iloop = (iloop ? iloop->next : NULL);
955 /* no of elements given and we */
956 /* have generated for all of them */
959 // there has to be a better way
960 char *name=sym->opval.val->sym->name;
961 int lineno=sym->opval.val->sym->lineDef;
962 char *filename=sym->opval.val->sym->fileDef;
963 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
970 /* if we have not been given a size */
971 if (!DCL_ELEM (type))
973 DCL_ELEM (type) = size;
976 return decorateType (resolveSymbols (rast), RESULT_CHECK);
980 /*-----------------------------------------------------------------*/
981 /* createIvalCharPtr - generates initial values for char pointers */
982 /*-----------------------------------------------------------------*/
984 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
988 /* if this is a pointer & right is a literal array then */
989 /* just assignment will do */
990 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
991 SPEC_SCLS (iexpr->etype) == S_CODE)
992 && IS_ARRAY (iexpr->ftype)))
993 return newNode ('=', sym, iexpr);
995 /* left side is an array so we have to assign each */
997 if ((IS_LITERAL (iexpr->etype) ||
998 SPEC_SCLS (iexpr->etype) == S_CODE)
999 && IS_ARRAY (iexpr->ftype))
1001 /* for each character generate an assignment */
1002 /* to the array element */
1003 char *s = SPEC_CVAL (iexpr->etype).v_char;
1005 int size = getSize (iexpr->ftype);
1006 int symsize = getSize (type);
1010 if (size>(symsize+1))
1011 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1012 "string", sym->opval.val->sym->name);
1016 for (i=0;i<size;i++)
1018 rast = newNode (NULLOP,
1022 newAst_VALUE (valueFromLit ((float) i))),
1023 newAst_VALUE (valueFromLit (*s))));
1027 // now WE don't need iexpr's symbol anymore
1028 freeStringSymbol(AST_SYMBOL(iexpr));
1030 return decorateType (resolveSymbols (rast), RESULT_CHECK);
1036 /*-----------------------------------------------------------------*/
1037 /* createIvalPtr - generates initial value for pointers */
1038 /*-----------------------------------------------------------------*/
1040 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
1046 if (ilist->type == INIT_DEEP)
1047 ilist = ilist->init.deep;
1049 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_CHECK);
1051 /* if character pointer */
1052 if (IS_CHAR (type->next))
1053 if ((rast = createIvalCharPtr (sym, type, iexpr)))
1056 return newNode ('=', sym, iexpr);
1059 /*-----------------------------------------------------------------*/
1060 /* createIval - generates code for initial value */
1061 /*-----------------------------------------------------------------*/
1063 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
1070 /* if structure then */
1071 if (IS_STRUCT (type))
1072 rast = createIvalStruct (sym, type, ilist);
1074 /* if this is a pointer */
1076 rast = createIvalPtr (sym, type, ilist);
1078 /* if this is an array */
1079 if (IS_ARRAY (type))
1080 rast = createIvalArray (sym, type, ilist);
1082 /* if type is SPECIFIER */
1084 rast = createIvalType (sym, type, ilist);
1087 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_CHECK);
1089 return decorateType (resolveSymbols (rast), RESULT_CHECK);
1092 /*-----------------------------------------------------------------*/
1093 /* initAggregates - initialises aggregate variables with initv */
1094 /*-----------------------------------------------------------------*/
1095 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1096 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1099 /*-----------------------------------------------------------------*/
1100 /* gatherAutoInit - creates assignment expressions for initial */
1102 /*-----------------------------------------------------------------*/
1104 gatherAutoInit (symbol * autoChain)
1111 for (sym = autoChain; sym; sym = sym->next)
1114 /* resolve the symbols in the ival */
1116 resolveIvalSym (sym->ival, sym->type);
1118 /* if this is a static variable & has an */
1119 /* initial value the code needs to be lifted */
1120 /* here to the main portion since they can be */
1121 /* initialised only once at the start */
1122 if (IS_STATIC (sym->etype) && sym->ival &&
1123 SPEC_SCLS (sym->etype) != S_CODE)
1127 /* insert the symbol into the symbol table */
1128 /* with level = 0 & name = rname */
1129 newSym = copySymbol (sym);
1130 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1132 /* now lift the code to main */
1133 if (IS_AGGREGATE (sym->type)) {
1134 work = initAggregates (sym, sym->ival, NULL);
1136 if (getNelements(sym->type, sym->ival)>1) {
1137 werrorfl (sym->fileDef, sym->lineDef,
1138 W_EXCESS_INITIALIZERS, "scalar",
1141 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1142 list2expr (sym->ival));
1145 setAstLineno (work, sym->lineDef);
1149 staticAutos = newNode (NULLOP, staticAutos, work);
1156 /* if there is an initial value */
1157 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1159 initList *ilist=sym->ival;
1161 while (ilist->type == INIT_DEEP) {
1162 ilist = ilist->init.deep;
1165 /* update lineno for error msg */
1166 lineno=sym->lineDef;
1167 setAstLineno (ilist->init.node, lineno);
1169 if (IS_AGGREGATE (sym->type)) {
1170 work = initAggregates (sym, sym->ival, NULL);
1172 if (getNelements(sym->type, sym->ival)>1) {
1173 werrorfl (sym->fileDef, sym->lineDef,
1174 W_EXCESS_INITIALIZERS, "scalar",
1177 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1178 list2expr (sym->ival));
1182 setAstLineno (work, sym->lineDef);
1186 init = newNode (NULLOP, init, work);
1195 /*-----------------------------------------------------------------*/
1196 /* freeStringSymbol - delete a literal string if no more usage */
1197 /*-----------------------------------------------------------------*/
1198 void freeStringSymbol(symbol *sym) {
1199 /* make sure this is a literal string */
1200 assert (sym->isstrlit);
1201 if (--sym->isstrlit == 0) { // lower the usage count
1202 memmap *segment=SPEC_OCLS(sym->etype);
1204 deleteSetItem(&segment->syms, sym);
1209 /*-----------------------------------------------------------------*/
1210 /* stringToSymbol - creates a symbol from a literal string */
1211 /*-----------------------------------------------------------------*/
1213 stringToSymbol (value * val)
1215 char name[SDCC_NAME_MAX + 1];
1216 static int charLbl = 0;
1221 // have we heard this before?
1222 for (sp=statsg->syms; sp; sp=sp->next) {
1224 size = getSize (sym->type);
1225 if (sym->isstrlit && size == getSize (val->type) &&
1226 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1227 // yes, this is old news. Don't publish it again.
1228 sym->isstrlit++; // but raise the usage count
1229 return symbolVal(sym);
1233 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1234 sym = newSymbol (name, 0); /* make it @ level 0 */
1235 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1237 /* copy the type from the value passed */
1238 sym->type = copyLinkChain (val->type);
1239 sym->etype = getSpec (sym->type);
1240 /* change to storage class & output class */
1241 SPEC_SCLS (sym->etype) = S_CODE;
1242 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1243 SPEC_STAT (sym->etype) = 1;
1244 /* make the level & block = 0 */
1245 sym->block = sym->level = 0;
1247 /* create an ival */
1248 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1253 allocVariables (sym);
1256 return symbolVal (sym);
1260 /*-----------------------------------------------------------------*/
1261 /* processBlockVars - will go thru the ast looking for block if */
1262 /* a block is found then will allocate the syms */
1263 /* will also gather the auto inits present */
1264 /*-----------------------------------------------------------------*/
1266 processBlockVars (ast * tree, int *stack, int action)
1271 /* if this is a block */
1272 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1276 if (action == ALLOCATE)
1278 *stack += allocVariables (tree->values.sym);
1279 autoInit = gatherAutoInit (tree->values.sym);
1281 /* if there are auto inits then do them */
1283 tree->left = newNode (NULLOP, autoInit, tree->left);
1285 else /* action is deallocate */
1286 deallocLocal (tree->values.sym);
1289 processBlockVars (tree->left, stack, action);
1290 processBlockVars (tree->right, stack, action);
1295 /*-------------------------------------------------------------*/
1296 /* constExprTree - returns TRUE if this tree is a constant */
1298 /*-------------------------------------------------------------*/
1299 bool constExprTree (ast *cexpr) {
1305 cexpr = decorateType (resolveSymbols (cexpr), RESULT_CHECK);
1307 switch (cexpr->type)
1310 if (IS_AST_LIT_VALUE(cexpr)) {
1311 // this is a literal
1314 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1315 // a function's address will never change
1318 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1319 // an array's address will never change
1322 if (IS_AST_SYM_VALUE(cexpr) &&
1323 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1324 // a symbol in code space will never change
1325 // This is only for the 'char *s="hallo"' case and will have to leave
1326 //printf(" code space symbol");
1331 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1332 "unexpected link in expression tree\n");
1335 if (cexpr->opval.op==ARRAYINIT) {
1336 // this is a list of literals
1339 if (cexpr->opval.op=='=') {
1340 return constExprTree(cexpr->right);
1342 if (cexpr->opval.op==CAST) {
1343 // cast ignored, maybe we should throw a warning here?
1344 return constExprTree(cexpr->right);
1346 if (cexpr->opval.op=='&') {
1349 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1352 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1357 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1362 /*-----------------------------------------------------------------*/
1363 /* constExprValue - returns the value of a constant expression */
1364 /* or NULL if it is not a constant expression */
1365 /*-----------------------------------------------------------------*/
1367 constExprValue (ast * cexpr, int check)
1369 cexpr = decorateType (resolveSymbols (cexpr), RESULT_CHECK);
1371 /* if this is not a constant then */
1372 if (!IS_LITERAL (cexpr->ftype))
1374 /* then check if this is a literal array
1376 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1377 SPEC_CVAL (cexpr->etype).v_char &&
1378 IS_ARRAY (cexpr->ftype))
1380 value *val = valFromType (cexpr->ftype);
1381 SPEC_SCLS (val->etype) = S_LITERAL;
1382 val->sym = cexpr->opval.val->sym;
1383 val->sym->type = copyLinkChain (cexpr->ftype);
1384 val->sym->etype = getSpec (val->sym->type);
1385 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1389 /* if we are casting a literal value then */
1390 if (IS_AST_OP (cexpr) &&
1391 cexpr->opval.op == CAST &&
1392 IS_LITERAL (cexpr->right->ftype))
1394 return valCastLiteral (cexpr->ftype,
1395 floatFromVal (cexpr->right->opval.val));
1398 if (IS_AST_VALUE (cexpr))
1400 return cexpr->opval.val;
1404 werror (E_CONST_EXPECTED, "found expression");
1409 /* return the value */
1410 return cexpr->opval.val;
1414 /*-----------------------------------------------------------------*/
1415 /* isLabelInAst - will return true if a given label is found */
1416 /*-----------------------------------------------------------------*/
1418 isLabelInAst (symbol * label, ast * tree)
1420 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1423 if (IS_AST_OP (tree) &&
1424 tree->opval.op == LABEL &&
1425 isSymbolEqual (AST_SYMBOL (tree->left), label))
1428 return isLabelInAst (label, tree->right) &&
1429 isLabelInAst (label, tree->left);
1433 /*-----------------------------------------------------------------*/
1434 /* isLoopCountable - return true if the loop count can be determi- */
1435 /* -ned at compile time . */
1436 /*-----------------------------------------------------------------*/
1438 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1439 symbol ** sym, ast ** init, ast ** end)
1442 /* the loop is considered countable if the following
1443 conditions are true :-
1445 a) initExpr :- <sym> = <const>
1446 b) condExpr :- <sym> < <const1>
1447 c) loopExpr :- <sym> ++
1450 /* first check the initExpr */
1451 if (IS_AST_OP (initExpr) &&
1452 initExpr->opval.op == '=' && /* is assignment */
1453 IS_AST_SYM_VALUE (initExpr->left))
1454 { /* left is a symbol */
1456 *sym = AST_SYMBOL (initExpr->left);
1457 *init = initExpr->right;
1462 /* for now the symbol has to be of
1464 if (!IS_INTEGRAL ((*sym)->type))
1467 /* now check condExpr */
1468 if (IS_AST_OP (condExpr))
1471 switch (condExpr->opval.op)
1474 if (IS_AST_SYM_VALUE (condExpr->left) &&
1475 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1476 IS_AST_LIT_VALUE (condExpr->right))
1478 *end = condExpr->right;
1484 if (IS_AST_OP (condExpr->left) &&
1485 condExpr->left->opval.op == '>' &&
1486 IS_AST_LIT_VALUE (condExpr->left->right) &&
1487 IS_AST_SYM_VALUE (condExpr->left->left) &&
1488 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1491 *end = newNode ('+', condExpr->left->right,
1492 newAst_VALUE (constVal ("1")));
1503 /* check loop expression is of the form <sym>++ */
1504 if (!IS_AST_OP (loopExpr))
1507 /* check if <sym> ++ */
1508 if (loopExpr->opval.op == INC_OP)
1514 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1515 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1522 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1523 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1531 if (loopExpr->opval.op == ADD_ASSIGN)
1534 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1535 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1536 IS_AST_LIT_VALUE (loopExpr->right) &&
1537 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1545 /*-----------------------------------------------------------------*/
1546 /* astHasVolatile - returns true if ast contains any volatile */
1547 /*-----------------------------------------------------------------*/
1549 astHasVolatile (ast * tree)
1554 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1557 if (IS_AST_OP (tree))
1558 return astHasVolatile (tree->left) ||
1559 astHasVolatile (tree->right);
1564 /*-----------------------------------------------------------------*/
1565 /* astHasPointer - return true if the ast contains any ptr variable */
1566 /*-----------------------------------------------------------------*/
1568 astHasPointer (ast * tree)
1573 if (IS_AST_LINK (tree))
1576 /* if we hit an array expression then check
1577 only the left side */
1578 if (IS_AST_OP (tree) && tree->opval.op == '[')
1579 return astHasPointer (tree->left);
1581 if (IS_AST_VALUE (tree))
1582 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1584 return astHasPointer (tree->left) ||
1585 astHasPointer (tree->right);
1589 /*-----------------------------------------------------------------*/
1590 /* astHasSymbol - return true if the ast has the given symbol */
1591 /*-----------------------------------------------------------------*/
1593 astHasSymbol (ast * tree, symbol * sym)
1595 if (!tree || IS_AST_LINK (tree))
1598 if (IS_AST_VALUE (tree))
1600 if (IS_AST_SYM_VALUE (tree))
1601 return isSymbolEqual (AST_SYMBOL (tree), sym);
1606 return astHasSymbol (tree->left, sym) ||
1607 astHasSymbol (tree->right, sym);
1610 /*-----------------------------------------------------------------*/
1611 /* astHasDeref - return true if the ast has an indirect access */
1612 /*-----------------------------------------------------------------*/
1614 astHasDeref (ast * tree)
1616 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1619 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1621 return astHasDeref (tree->left) || astHasDeref (tree->right);
1624 /*-----------------------------------------------------------------*/
1625 /* isConformingBody - the loop body has to conform to a set of rules */
1626 /* for the loop to be considered reversible read on for rules */
1627 /*-----------------------------------------------------------------*/
1629 isConformingBody (ast * pbody, symbol * sym, ast * body)
1632 /* we are going to do a pre-order traversal of the
1633 tree && check for the following conditions. (essentially
1634 a set of very shallow tests )
1635 a) the sym passed does not participate in
1636 any arithmetic operation
1637 b) There are no function calls
1638 c) all jumps are within the body
1639 d) address of loop control variable not taken
1640 e) if an assignment has a pointer on the
1641 left hand side make sure right does not have
1642 loop control variable */
1644 /* if we reach the end or a leaf then true */
1645 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1648 /* if anything else is "volatile" */
1649 if (IS_VOLATILE (TETYPE (pbody)))
1652 /* we will walk the body in a pre-order traversal for
1654 switch (pbody->opval.op)
1656 /*------------------------------------------------------------------*/
1658 // if the loopvar is used as an index
1659 if (astHasSymbol(pbody->right, sym)) {
1662 return isConformingBody (pbody->right, sym, body);
1664 /*------------------------------------------------------------------*/
1669 /*------------------------------------------------------------------*/
1673 /* sure we are not sym is not modified */
1675 IS_AST_SYM_VALUE (pbody->left) &&
1676 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1680 IS_AST_SYM_VALUE (pbody->right) &&
1681 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1686 /*------------------------------------------------------------------*/
1688 case '*': /* can be unary : if right is null then unary operation */
1693 /* if right is NULL then unary operation */
1694 /*------------------------------------------------------------------*/
1695 /*----------------------------*/
1697 /*----------------------------*/
1700 if (IS_AST_SYM_VALUE (pbody->left) &&
1701 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1704 return isConformingBody (pbody->left, sym, body);
1708 if (astHasSymbol (pbody->left, sym) ||
1709 astHasSymbol (pbody->right, sym))
1714 /*------------------------------------------------------------------*/
1722 if (IS_AST_SYM_VALUE (pbody->left) &&
1723 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1726 if (IS_AST_SYM_VALUE (pbody->right) &&
1727 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1730 return isConformingBody (pbody->left, sym, body) &&
1731 isConformingBody (pbody->right, sym, body);
1739 if (IS_AST_SYM_VALUE (pbody->left) &&
1740 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1742 return isConformingBody (pbody->left, sym, body);
1744 /*------------------------------------------------------------------*/
1756 case SIZEOF: /* evaluate wihout code generation */
1758 if (IS_AST_SYM_VALUE (pbody->left) &&
1759 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1762 if (IS_AST_SYM_VALUE (pbody->right) &&
1763 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1766 return isConformingBody (pbody->left, sym, body) &&
1767 isConformingBody (pbody->right, sym, body);
1769 /*------------------------------------------------------------------*/
1772 /* if left has a pointer & right has loop
1773 control variable then we cannot */
1774 if (astHasPointer (pbody->left) &&
1775 astHasSymbol (pbody->right, sym))
1777 if (astHasVolatile (pbody->left))
1780 if (IS_AST_SYM_VALUE (pbody->left)) {
1781 // if the loopvar has an assignment
1782 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1784 // if the loopvar is used in another (maybe conditional) block
1785 if (astHasSymbol (pbody->right, sym) &&
1786 (pbody->level >= body->level)) {
1791 if (astHasVolatile (pbody->left))
1794 if (astHasDeref(pbody->right)) return FALSE;
1796 return isConformingBody (pbody->left, sym, body) &&
1797 isConformingBody (pbody->right, sym, body);
1808 assert ("Parser should not have generated this\n");
1810 /*------------------------------------------------------------------*/
1811 /*----------------------------*/
1812 /* comma operator */
1813 /*----------------------------*/
1815 return isConformingBody (pbody->left, sym, body) &&
1816 isConformingBody (pbody->right, sym, body);
1818 /*------------------------------------------------------------------*/
1819 /*----------------------------*/
1821 /*----------------------------*/
1823 /* if local & not passed as paramater then ok */
1824 if (sym->level && !astHasSymbol(pbody->right,sym))
1828 /*------------------------------------------------------------------*/
1829 /*----------------------------*/
1830 /* return statement */
1831 /*----------------------------*/
1836 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1841 if (astHasSymbol (pbody->left, sym))
1848 return isConformingBody (pbody->left, sym, body) &&
1849 isConformingBody (pbody->right, sym, body);
1855 /*-----------------------------------------------------------------*/
1856 /* isLoopReversible - takes a for loop as input && returns true */
1857 /* if the for loop is reversible. If yes will set the value of */
1858 /* the loop control var & init value & termination value */
1859 /*-----------------------------------------------------------------*/
1861 isLoopReversible (ast * loop, symbol ** loopCntrl,
1862 ast ** init, ast ** end)
1864 /* if option says don't do it then don't */
1865 if (optimize.noLoopReverse)
1867 /* there are several tests to determine this */
1869 /* for loop has to be of the form
1870 for ( <sym> = <const1> ;
1871 [<sym> < <const2>] ;
1872 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1874 if (!isLoopCountable (AST_FOR (loop, initExpr),
1875 AST_FOR (loop, condExpr),
1876 AST_FOR (loop, loopExpr),
1877 loopCntrl, init, end))
1880 /* now do some serious checking on the body of the loop
1883 return isConformingBody (loop->left, *loopCntrl, loop->left);
1887 /*-----------------------------------------------------------------*/
1888 /* replLoopSym - replace the loop sym by loop sym -1 */
1889 /*-----------------------------------------------------------------*/
1891 replLoopSym (ast * body, symbol * sym)
1894 if (!body || IS_AST_LINK (body))
1897 if (IS_AST_SYM_VALUE (body))
1900 if (isSymbolEqual (AST_SYMBOL (body), sym))
1904 body->opval.op = '-';
1905 body->left = newAst_VALUE (symbolVal (sym));
1906 body->right = newAst_VALUE (constVal ("1"));
1914 replLoopSym (body->left, sym);
1915 replLoopSym (body->right, sym);
1919 /*-----------------------------------------------------------------*/
1920 /* reverseLoop - do the actual loop reversal */
1921 /*-----------------------------------------------------------------*/
1923 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1927 /* create the following tree
1932 if (sym) goto for_continue ;
1935 /* put it together piece by piece */
1936 rloop = newNode (NULLOP,
1937 createIf (newAst_VALUE (symbolVal (sym)),
1939 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1942 newAst_VALUE (symbolVal (sym)),
1945 replLoopSym (loop->left, sym);
1946 setAstLineno (rloop, init->lineno);
1948 rloop = newNode (NULLOP,
1950 newAst_VALUE (symbolVal (sym)),
1951 newNode ('-', end, init)),
1952 createLabel (AST_FOR (loop, continueLabel),
1956 newNode (SUB_ASSIGN,
1957 newAst_VALUE (symbolVal (sym)),
1958 newAst_VALUE (constVal ("1"))),
1961 rloop->lineno=init->lineno;
1962 return decorateType (rloop, RESULT_CHECK);
1966 /*-----------------------------------------------------------------*/
1967 /* searchLitOp - search tree (*ops only) for an ast with literal */
1968 /*-----------------------------------------------------------------*/
1970 searchLitOp (ast *tree, ast **parent, const char *ops)
1974 if (tree && optimize.global_cse)
1976 /* is there a literal operand? */
1978 IS_AST_OP(tree->right) &&
1979 tree->right->right &&
1980 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
1982 if (IS_LITERAL (RTYPE (tree->right)) ^
1983 IS_LITERAL (LTYPE (tree->right)))
1985 tree->right->decorated = 0;
1986 tree->decorated = 0;
1990 ret = searchLitOp (tree->right, parent, ops);
1995 IS_AST_OP(tree->left) &&
1996 tree->left->right &&
1997 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
1999 if (IS_LITERAL (RTYPE (tree->left)) ^
2000 IS_LITERAL (LTYPE (tree->left)))
2002 tree->left->decorated = 0;
2003 tree->decorated = 0;
2007 ret = searchLitOp (tree->left, parent, ops);
2015 /*-----------------------------------------------------------------*/
2016 /* getResultFromType */
2017 /*-----------------------------------------------------------------*/
2019 getResultTypeFromType (sym_link *type)
2021 /* type = getSpec (type); */
2022 if (IS_BITVAR (type))
2023 return RESULT_TYPE_BIT;
2024 if (IS_BITFIELD (type))
2025 return RESULT_TYPE_CHAR;
2027 return RESULT_TYPE_CHAR;
2030 return RESULT_TYPE_INT;
2031 return RESULT_TYPE_OTHER;
2034 /*-----------------------------------------------------------------*/
2035 /* addCast - adds casts to a type specified by RESULT_TYPE */
2036 /*-----------------------------------------------------------------*/
2038 addCast (ast *tree, RESULT_TYPE resultType, bool upcast)
2041 bool upCasted = FALSE;
2045 case RESULT_TYPE_NONE:
2046 /* char: promote to int */
2048 getSize (tree->etype) >= INTSIZE)
2050 newLink = newIntLink();
2053 case RESULT_TYPE_CHAR:
2054 if (getSize (tree->etype) <= 1)
2056 newLink = newCharLink();
2058 case RESULT_TYPE_INT:
2060 if (getSize (tree->etype) > INTSIZE)
2062 /* warn ("Loosing significant digits"); */
2066 /* char: promote to int */
2068 getSize (tree->etype) >= INTSIZE)
2070 newLink = newIntLink();
2073 case RESULT_TYPE_OTHER:
2076 /* return type is long, float: promote char to int */
2077 if (getSize (tree->etype) >= INTSIZE)
2079 newLink = newIntLink();
2085 tree->decorated = 0;
2086 tree = newNode (CAST, newAst_LINK (newLink), tree);
2087 tree->lineno = tree->right->lineno;
2088 /* keep unsigned type during cast to smaller type,
2089 but not when promoting from char to int */
2091 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2092 return decorateType (tree, resultType);
2095 /*-----------------------------------------------------------------*/
2096 /* resultTypePropagate - decides if resultType can be propagated */
2097 /*-----------------------------------------------------------------*/
2099 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2101 switch (tree->opval.op)
2116 return RESULT_TYPE_NONE;
2120 return RESULT_TYPE_IFX;
2122 return RESULT_TYPE_NONE;
2126 /*-----------------------------------------------------------------*/
2127 /* getLeftResultType - gets type from left branch for propagation */
2128 /*-----------------------------------------------------------------*/
2130 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2132 switch (tree->opval.op)
2136 if (IS_PTR (LTYPE (tree)))
2137 return RESULT_TYPE_NONE;
2139 return getResultTypeFromType (LETYPE (tree));
2141 if (IS_PTR (currFunc->type->next))
2142 return RESULT_TYPE_NONE;
2144 return getResultTypeFromType (currFunc->type->next);
2146 if (!IS_ARRAY (LTYPE (tree)))
2148 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 256)
2149 return RESULT_TYPE_CHAR;
2156 /*--------------------------------------------------------------------*/
2157 /* decorateType - compute type for this tree, also does type checking.*/
2158 /* This is done bottom up, since type has to flow upwards. */
2159 /* resultType flows top-down and forces e.g. char-arithmetik, if the */
2160 /* result is a char and the operand(s) are int's. */
2161 /* It also does constant folding, and parameter checking. */
2162 /*--------------------------------------------------------------------*/
2164 decorateType (ast * tree, RESULT_TYPE resultType)
2168 RESULT_TYPE resultTypeProp;
2173 /* if already has type then do nothing */
2174 if (tree->decorated)
2177 tree->decorated = 1;
2180 /* print the line */
2181 /* if not block & function */
2182 if (tree->type == EX_OP &&
2183 (tree->opval.op != FUNCTION &&
2184 tree->opval.op != BLOCK &&
2185 tree->opval.op != NULLOP))
2187 filename = tree->filename;
2188 lineno = tree->lineno;
2192 /* if any child is an error | this one is an error do nothing */
2193 if (tree->isError ||
2194 (tree->left && tree->left->isError) ||
2195 (tree->right && tree->right->isError))
2198 /*------------------------------------------------------------------*/
2199 /*----------------------------*/
2200 /* leaf has been reached */
2201 /*----------------------------*/
2202 lineno=tree->lineno;
2203 /* if this is of type value */
2204 /* just get the type */
2205 if (tree->type == EX_VALUE)
2208 if (IS_LITERAL (tree->opval.val->etype))
2211 /* if this is a character array then declare it */
2212 if (IS_ARRAY (tree->opval.val->type))
2213 tree->opval.val = stringToSymbol (tree->opval.val);
2215 /* otherwise just copy the type information */
2216 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2220 if (tree->opval.val->sym)
2222 /* if the undefined flag is set then give error message */
2223 if (tree->opval.val->sym->undefined)
2225 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2227 TTYPE (tree) = TETYPE (tree) =
2228 tree->opval.val->type = tree->opval.val->sym->type =
2229 tree->opval.val->etype = tree->opval.val->sym->etype =
2230 copyLinkChain (INTTYPE);
2235 /* if impilicit i.e. struct/union member then no type */
2236 if (tree->opval.val->sym->implicit)
2237 TTYPE (tree) = TETYPE (tree) = NULL;
2242 /* else copy the type */
2243 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2245 /* and mark it as referenced */
2246 tree->opval.val->sym->isref = 1;
2254 /* if type link for the case of cast */
2255 if (tree->type == EX_LINK)
2257 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2265 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2267 if (tree->left && tree->left->type == EX_OPERAND
2268 && (tree->left->opval.op == INC_OP
2269 || tree->left->opval.op == DEC_OP)
2270 && tree->left->left)
2272 tree->left->right = tree->left->left;
2273 tree->left->left = NULL;
2275 if (tree->right && tree->right->type == EX_OPERAND
2276 && (tree->right->opval.op == INC_OP
2277 || tree->right->opval.op == DEC_OP)
2278 && tree->right->left)
2280 tree->right->right = tree->right->left;
2281 tree->right->left = NULL;
2286 /* Before decorating the left branch we've to decide in dependence
2287 upon tree->opval.op, if resultType can be propagated */
2288 resultTypeProp = resultTypePropagate (tree, resultType);
2290 if (tree->opval.op == '?')
2291 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2293 dtl = decorateType (tree->left, resultTypeProp);
2295 /* if an array node, we may need to swap branches */
2296 if (tree->opval.op == '[')
2298 /* determine which is the array & which the index */
2299 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2300 IS_INTEGRAL (LTYPE (tree)))
2302 ast *tempTree = tree->left;
2303 tree->left = tree->right;
2304 tree->right = tempTree;
2308 /* After decorating the left branch there's type information available
2309 in tree->left->?type. If the op is e.g. '=' we extract the type
2310 information from there and propagate it to the right branch. */
2311 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2313 switch (tree->opval.op)
2316 /* delay right side for '?' operator since conditional macro
2317 expansions might rely on this */
2321 /* decorate right side for CALL (parameter list) in processParms();
2322 there is resultType available */
2326 dtr = decorateType (tree->right, resultTypeProp);
2330 /* this is to take care of situations
2331 when the tree gets rewritten */
2332 if (dtl != tree->left)
2334 if (dtr != tree->right)
2336 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2340 /* depending on type of operator do */
2342 switch (tree->opval.op)
2344 /*------------------------------------------------------------------*/
2345 /*----------------------------*/
2347 /*----------------------------*/
2350 /* first check if this is a array or a pointer */
2351 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2353 werror (E_NEED_ARRAY_PTR, "[]");
2354 goto errorTreeReturn;
2357 /* check if the type of the idx */
2358 if (!IS_INTEGRAL (RTYPE (tree)))
2360 werror (E_IDX_NOT_INT);
2361 goto errorTreeReturn;
2364 /* if the left is an rvalue then error */
2367 werror (E_LVALUE_REQUIRED, "array access");
2368 goto errorTreeReturn;
2371 if (IS_LITERAL (RTYPE (tree)))
2373 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2374 int arraySize = DCL_ELEM (LTYPE (tree));
2375 if (arraySize && arrayIndex >= arraySize)
2377 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2382 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2385 /*------------------------------------------------------------------*/
2386 /*----------------------------*/
2388 /*----------------------------*/
2390 /* if this is not a structure */
2391 if (!IS_STRUCT (LTYPE (tree)))
2393 werror (E_STRUCT_UNION, ".");
2394 goto errorTreeReturn;
2396 TTYPE (tree) = structElemType (LTYPE (tree),
2397 (tree->right->type == EX_VALUE ?
2398 tree->right->opval.val : NULL));
2399 TETYPE (tree) = getSpec (TTYPE (tree));
2402 /*------------------------------------------------------------------*/
2403 /*----------------------------*/
2404 /* struct/union pointer */
2405 /*----------------------------*/
2407 /* if not pointer to a structure */
2408 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2410 werror (E_PTR_REQD);
2411 goto errorTreeReturn;
2414 if (!IS_STRUCT (LTYPE (tree)->next))
2416 werror (E_STRUCT_UNION, "->");
2417 goto errorTreeReturn;
2420 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2421 (tree->right->type == EX_VALUE ?
2422 tree->right->opval.val : NULL));
2423 TETYPE (tree) = getSpec (TTYPE (tree));
2425 /* adjust the storage class */
2426 switch (DCL_TYPE(tree->left->ftype)) {
2428 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2431 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2434 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2437 SPEC_SCLS (TETYPE (tree)) = 0;
2440 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2443 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2446 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2449 SPEC_SCLS (TETYPE (tree)) = 0;
2456 /* This breaks with extern declarations, bitfields, and perhaps other */
2457 /* cases (gcse). Let's leave this optimization disabled for now and */
2458 /* ponder if there's a safe way to do this. -- EEP */
2460 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2461 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2463 /* If defined struct type at addr var
2464 then rewrite (&struct var)->member
2466 and define membertype at (addr+offsetof(struct var,member)) temp
2469 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2470 AST_SYMBOL(tree->right));
2472 sym = newSymbol(genSymName (0), 0);
2473 sym->type = TTYPE (tree);
2474 sym->etype = getSpec(sym->type);
2475 sym->lineDef = tree->lineno;
2478 SPEC_STAT (sym->etype) = 1;
2479 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2481 SPEC_ABSA(sym->etype) = 1;
2482 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2485 AST_VALUE (tree) = symbolVal(sym);
2488 tree->type = EX_VALUE;
2496 /*------------------------------------------------------------------*/
2497 /*----------------------------*/
2498 /* ++/-- operation */
2499 /*----------------------------*/
2503 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2504 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2505 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2506 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2515 /*------------------------------------------------------------------*/
2516 /*----------------------------*/
2518 /*----------------------------*/
2519 case '&': /* can be unary */
2520 /* if right is NULL then unary operation */
2521 if (tree->right) /* not an unary operation */
2524 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2526 werror (E_BITWISE_OP);
2527 werror (W_CONTINUE, "left & right types are ");
2528 printTypeChain (LTYPE (tree), stderr);
2529 fprintf (stderr, ",");
2530 printTypeChain (RTYPE (tree), stderr);
2531 fprintf (stderr, "\n");
2532 goto errorTreeReturn;
2535 /* if they are both literal */
2536 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2538 tree->type = EX_VALUE;
2539 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2540 valFromType (RETYPE (tree)), '&');
2542 tree->right = tree->left = NULL;
2543 TETYPE (tree) = tree->opval.val->etype;
2544 TTYPE (tree) = tree->opval.val->type;
2548 /* see if this is a GETHBIT operation if yes
2551 ast *otree = optimizeGetHbit (tree);
2554 return decorateType (otree, RESULT_CHECK);
2557 tree->left = addCast (tree->left, resultType, FALSE);
2558 tree->right = addCast (tree->right, resultType, FALSE);
2559 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
2560 TETYPE (tree) = getSpec (TTYPE (tree));
2562 /* if left is a literal exchange left & right */
2563 if (IS_LITERAL (LTYPE (tree)))
2565 ast *tTree = tree->left;
2566 tree->left = tree->right;
2567 tree->right = tTree;
2570 /* if right is a literal and */
2571 /* we can find a 2nd literal in a and-tree then */
2572 /* rearrange the tree */
2573 if (IS_LITERAL (RTYPE (tree)))
2576 ast *litTree = searchLitOp (tree, &parent, "&");
2579 ast *tTree = litTree->left;
2580 litTree->left = tree->right;
2581 tree->right = tTree;
2582 /* both operands in tTree are literal now */
2583 decorateType (parent, RESULT_CHECK);
2587 LRVAL (tree) = RRVAL (tree) = 1;
2592 /*------------------------------------------------------------------*/
2593 /*----------------------------*/
2595 /*----------------------------*/
2596 p = newLink (DECLARATOR);
2597 /* if bit field then error */
2598 if (IS_BITVAR (tree->left->etype))
2600 werror (E_ILLEGAL_ADDR, "address of bit variable");
2601 goto errorTreeReturn;
2604 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2606 werror (E_ILLEGAL_ADDR, "address of register variable");
2607 goto errorTreeReturn;
2610 if (IS_FUNC (LTYPE (tree)))
2612 // this ought to be ignored
2613 return (tree->left);
2616 if (IS_LITERAL(LTYPE(tree)))
2618 werror (E_ILLEGAL_ADDR, "address of literal");
2619 goto errorTreeReturn;
2624 werror (E_LVALUE_REQUIRED, "address of");
2625 goto errorTreeReturn;
2628 DCL_TYPE (p) = POINTER;
2629 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2630 DCL_TYPE (p) = CPOINTER;
2631 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2632 DCL_TYPE (p) = FPOINTER;
2633 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2634 DCL_TYPE (p) = PPOINTER;
2635 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2636 DCL_TYPE (p) = IPOINTER;
2637 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2638 DCL_TYPE (p) = EEPPOINTER;
2639 else if (SPEC_OCLS(tree->left->etype))
2640 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2642 DCL_TYPE (p) = POINTER;
2644 if (IS_AST_SYM_VALUE (tree->left))
2646 AST_SYMBOL (tree->left)->addrtaken = 1;
2647 AST_SYMBOL (tree->left)->allocreq = 1;
2650 p->next = LTYPE (tree);
2652 TETYPE (tree) = getSpec (TTYPE (tree));
2657 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2658 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2660 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2661 AST_SYMBOL(tree->left->right));
2662 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2663 valueFromLit(element->offset));
2666 tree->type = EX_VALUE;
2667 tree->values.literalFromCast = 1;
2673 /*------------------------------------------------------------------*/
2674 /*----------------------------*/
2676 /*----------------------------*/
2678 /* if the rewrite succeeds then don't go any furthur */
2680 ast *wtree = optimizeRRCRLC (tree);
2682 return decorateType (wtree, RESULT_CHECK);
2684 wtree = optimizeSWAP (tree);
2686 return decorateType (wtree, RESULT_CHECK);
2689 /* if left is a literal exchange left & right */
2690 if (IS_LITERAL (LTYPE (tree)))
2692 ast *tTree = tree->left;
2693 tree->left = tree->right;
2694 tree->right = tTree;
2697 /* if right is a literal and */
2698 /* we can find a 2nd literal in a or-tree then */
2699 /* rearrange the tree */
2700 if (IS_LITERAL (RTYPE (tree)))
2703 ast *litTree = searchLitOp (tree, &parent, "|");
2706 ast *tTree = litTree->left;
2707 litTree->left = tree->right;
2708 tree->right = tTree;
2709 /* both operands in tTree are literal now */
2710 decorateType (parent, RESULT_CHECK);
2715 /*------------------------------------------------------------------*/
2716 /*----------------------------*/
2718 /*----------------------------*/
2720 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2722 werror (E_BITWISE_OP);
2723 werror (W_CONTINUE, "left & right types are ");
2724 printTypeChain (LTYPE (tree), stderr);
2725 fprintf (stderr, ",");
2726 printTypeChain (RTYPE (tree), stderr);
2727 fprintf (stderr, "\n");
2728 goto errorTreeReturn;
2731 /* if they are both literal then */
2732 /* rewrite the tree */
2733 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2735 tree->type = EX_VALUE;
2736 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2737 valFromType (RETYPE (tree)),
2739 tree->right = tree->left = NULL;
2740 TETYPE (tree) = tree->opval.val->etype;
2741 TTYPE (tree) = tree->opval.val->type;
2745 /* if left is a literal exchange left & right */
2746 if (IS_LITERAL (LTYPE (tree)))
2748 ast *tTree = tree->left;
2749 tree->left = tree->right;
2750 tree->right = tTree;
2753 /* if right is a literal and */
2754 /* we can find a 2nd literal in a xor-tree then */
2755 /* rearrange the tree */
2756 if (IS_LITERAL (RTYPE (tree)) &&
2757 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
2760 ast *litTree = searchLitOp (tree, &parent, "^");
2763 ast *tTree = litTree->left;
2764 litTree->left = tree->right;
2765 tree->right = tTree;
2766 /* both operands in litTree are literal now */
2767 decorateType (parent, RESULT_CHECK);
2771 LRVAL (tree) = RRVAL (tree) = 1;
2772 tree->left = addCast (tree->left, resultType, FALSE);
2773 tree->right = addCast (tree->right, resultType, FALSE);
2774 TETYPE (tree) = getSpec (TTYPE (tree) =
2775 computeType (LTYPE (tree),
2781 /*------------------------------------------------------------------*/
2782 /*----------------------------*/
2784 /*----------------------------*/
2786 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2788 werror (E_INVALID_OP, "divide");
2789 goto errorTreeReturn;
2791 /* if they are both literal then */
2792 /* rewrite the tree */
2793 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2795 tree->type = EX_VALUE;
2796 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2797 valFromType (RETYPE (tree)));
2798 tree->right = tree->left = NULL;
2799 TETYPE (tree) = getSpec (TTYPE (tree) =
2800 tree->opval.val->type);
2804 LRVAL (tree) = RRVAL (tree) = 1;
2806 TETYPE (tree) = getSpec (TTYPE (tree) =
2807 computeType (LTYPE (tree),
2809 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
2811 /* if right is a literal and */
2812 /* left is also a division by a literal then */
2813 /* rearrange the tree */
2814 if (IS_LITERAL (RTYPE (tree))
2815 /* avoid infinite loop */
2816 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
2819 ast *litTree = searchLitOp (tree, &parent, "/");
2822 if (IS_LITERAL (RTYPE (litTree)))
2825 litTree->right = newNode ('*', litTree->right, tree->right);
2826 litTree->right->lineno = tree->lineno;
2828 tree->right->opval.val = constVal ("1");
2829 decorateType (parent, RESULT_CHECK);
2833 /* litTree->left is literal: no gcse possible.
2834 We can't call decorateType(parent, RESULT_CHECK), because
2835 this would cause an infinit loop. */
2836 parent->decorated = 1;
2837 decorateType (litTree, RESULT_CHECK);
2844 /*------------------------------------------------------------------*/
2845 /*----------------------------*/
2847 /*----------------------------*/
2849 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2851 werror (E_BITWISE_OP);
2852 werror (W_CONTINUE, "left & right types are ");
2853 printTypeChain (LTYPE (tree), stderr);
2854 fprintf (stderr, ",");
2855 printTypeChain (RTYPE (tree), stderr);
2856 fprintf (stderr, "\n");
2857 goto errorTreeReturn;
2859 /* if they are both literal then */
2860 /* rewrite the tree */
2861 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2863 tree->type = EX_VALUE;
2864 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2865 valFromType (RETYPE (tree)));
2866 tree->right = tree->left = NULL;
2867 TETYPE (tree) = getSpec (TTYPE (tree) =
2868 tree->opval.val->type);
2871 LRVAL (tree) = RRVAL (tree) = 1;
2872 TETYPE (tree) = getSpec (TTYPE (tree) =
2873 computeType (LTYPE (tree),
2875 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
2878 /*------------------------------------------------------------------*/
2879 /*----------------------------*/
2880 /* address dereference */
2881 /*----------------------------*/
2882 case '*': /* can be unary : if right is null then unary operation */
2885 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2887 werror (E_PTR_REQD);
2888 goto errorTreeReturn;
2893 werror (E_LVALUE_REQUIRED, "pointer deref");
2894 goto errorTreeReturn;
2896 if (IS_ADDRESS_OF_OP(tree->left))
2898 /* replace *&obj with obj */
2899 return tree->left->left;
2901 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
2902 TETYPE (tree) = getSpec (TTYPE (tree));
2903 /* adjust the storage class */
2904 switch (DCL_TYPE(tree->left->ftype)) {
2906 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2909 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2912 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2915 SPEC_SCLS (TETYPE (tree)) = 0;
2918 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2921 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2924 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2927 SPEC_SCLS (TETYPE (tree)) = 0;
2936 /*------------------------------------------------------------------*/
2937 /*----------------------------*/
2938 /* multiplication */
2939 /*----------------------------*/
2940 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2942 werror (E_INVALID_OP, "multiplication");
2943 goto errorTreeReturn;
2946 /* if they are both literal then */
2947 /* rewrite the tree */
2948 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2950 tree->type = EX_VALUE;
2951 tree->opval.val = valMult (valFromType (LETYPE (tree)),
2952 valFromType (RETYPE (tree)));
2953 tree->right = tree->left = NULL;
2954 TETYPE (tree) = getSpec (TTYPE (tree) =
2955 tree->opval.val->type);
2959 /* if left is a literal exchange left & right */
2960 if (IS_LITERAL (LTYPE (tree)))
2962 ast *tTree = tree->left;
2963 tree->left = tree->right;
2964 tree->right = tTree;
2967 /* if right is a literal and */
2968 /* we can find a 2nd literal in a mul-tree then */
2969 /* rearrange the tree */
2970 if (IS_LITERAL (RTYPE (tree)))
2973 ast *litTree = searchLitOp (tree, &parent, "*");
2976 ast *tTree = litTree->left;
2977 litTree->left = tree->right;
2978 tree->right = tTree;
2979 /* both operands in litTree are literal now */
2980 decorateType (parent, RESULT_CHECK);
2984 LRVAL (tree) = RRVAL (tree) = 1;
2985 tree->left = addCast (tree->left, resultType, FALSE);
2986 tree->right = addCast (tree->right, resultType, FALSE);
2987 TETYPE (tree) = getSpec (TTYPE (tree) =
2988 computeType (LTYPE (tree),
2990 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
2994 /*------------------------------------------------------------------*/
2995 /*----------------------------*/
2996 /* unary '+' operator */
2997 /*----------------------------*/
3002 if (!IS_ARITHMETIC (LTYPE (tree)))
3004 werror (E_UNARY_OP, '+');
3005 goto errorTreeReturn;
3008 /* if left is a literal then do it */
3009 if (IS_LITERAL (LTYPE (tree)))
3011 tree->type = EX_VALUE;
3012 tree->opval.val = valFromType (LETYPE (tree));
3014 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3018 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3022 /*------------------------------------------------------------------*/
3023 /*----------------------------*/
3025 /*----------------------------*/
3027 /* this is not a unary operation */
3028 /* if both pointers then problem */
3029 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3030 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3032 werror (E_PTR_PLUS_PTR);
3033 goto errorTreeReturn;
3036 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3037 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3039 werror (E_PLUS_INVALID, "+");
3040 goto errorTreeReturn;
3043 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3044 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3046 werror (E_PLUS_INVALID, "+");
3047 goto errorTreeReturn;
3049 /* if they are both literal then */
3050 /* rewrite the tree */
3051 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3053 tree->type = EX_VALUE;
3054 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3055 valFromType (RETYPE (tree)));
3056 tree->right = tree->left = NULL;
3057 TETYPE (tree) = getSpec (TTYPE (tree) =
3058 tree->opval.val->type);
3062 /* if the right is a pointer or left is a literal
3063 xchange left & right */
3064 if (IS_ARRAY (RTYPE (tree)) ||
3065 IS_PTR (RTYPE (tree)) ||
3066 IS_LITERAL (LTYPE (tree)))
3068 ast *tTree = tree->left;
3069 tree->left = tree->right;
3070 tree->right = tTree;
3073 /* if right is a literal and */
3074 /* left is also an addition/subtraction with a literal then */
3075 /* rearrange the tree */
3076 if (IS_LITERAL (RTYPE (tree)))
3078 ast *litTree, *parent;
3079 litTree = searchLitOp (tree, &parent, "+-");
3082 if (litTree->opval.op == '+')
3085 ast *tTree = litTree->left;
3086 litTree->left = tree->right;
3087 tree->right = tree->left;
3090 else if (litTree->opval.op == '-')
3092 if (IS_LITERAL (RTYPE (litTree)))
3095 ast *tTree = litTree->left;
3096 litTree->left = tree->right;
3097 tree->right = tTree;
3102 ast *tTree = litTree->right;
3103 litTree->right = tree->right;
3104 tree->right = tTree;
3105 litTree->opval.op = '+';
3106 tree->opval.op = '-';
3109 decorateType (parent, RESULT_CHECK);
3113 LRVAL (tree) = RRVAL (tree) = 1;
3114 /* if the left is a pointer */
3115 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3116 TETYPE (tree) = getSpec (TTYPE (tree) =
3120 tree->left = addCast (tree->left, resultType, TRUE);
3121 tree->right = addCast (tree->right, resultType, TRUE);
3122 TETYPE (tree) = getSpec (TTYPE (tree) =
3123 computeType (LTYPE (tree),
3125 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
3130 /*------------------------------------------------------------------*/
3131 /*----------------------------*/
3133 /*----------------------------*/
3134 case '-': /* can be unary */
3135 /* if right is null then unary */
3139 if (!IS_ARITHMETIC (LTYPE (tree)))
3141 werror (E_UNARY_OP, tree->opval.op);
3142 goto errorTreeReturn;
3145 /* if left is a literal then do it */
3146 if (IS_LITERAL (LTYPE (tree)))
3148 tree->type = EX_VALUE;
3149 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3151 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3152 SPEC_USIGN(TETYPE(tree)) = 0;
3156 TETYPE(tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3160 /*------------------------------------------------------------------*/
3161 /*----------------------------*/
3163 /*----------------------------*/
3165 if (!(IS_PTR (LTYPE (tree)) ||
3166 IS_ARRAY (LTYPE (tree)) ||
3167 IS_ARITHMETIC (LTYPE (tree))))
3169 werror (E_PLUS_INVALID, "-");
3170 goto errorTreeReturn;
3173 if (!(IS_PTR (RTYPE (tree)) ||
3174 IS_ARRAY (RTYPE (tree)) ||
3175 IS_ARITHMETIC (RTYPE (tree))))
3177 werror (E_PLUS_INVALID, "-");
3178 goto errorTreeReturn;
3181 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3182 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3183 IS_INTEGRAL (RTYPE (tree))))
3185 werror (E_PLUS_INVALID, "-");
3186 goto errorTreeReturn;
3189 /* if they are both literal then */
3190 /* rewrite the tree */
3191 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3193 tree->type = EX_VALUE;
3194 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3195 valFromType (RETYPE (tree)));
3196 tree->right = tree->left = NULL;
3197 TETYPE (tree) = getSpec (TTYPE (tree) =
3198 tree->opval.val->type);
3202 /* if the left & right are equal then zero */
3203 if (isAstEqual (tree->left, tree->right))
3205 tree->type = EX_VALUE;
3206 tree->left = tree->right = NULL;
3207 tree->opval.val = constVal ("0");
3208 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3212 /* if both of them are pointers or arrays then */
3213 /* the result is going to be an integer */
3214 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3215 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3216 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3218 /* if only the left is a pointer */
3219 /* then result is a pointer */
3220 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3221 TETYPE (tree) = getSpec (TTYPE (tree) =
3225 tree->left = addCast (tree->left, resultType, TRUE);
3226 tree->right = addCast (tree->right, resultType, TRUE);
3227 TETYPE (tree) = getSpec (TTYPE (tree) =
3228 computeType (LTYPE (tree),
3230 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
3233 LRVAL (tree) = RRVAL (tree) = 1;
3235 /* if right is a literal and */
3236 /* left is also an addition/subtraction with a literal then */
3237 /* rearrange the tree */
3238 if (IS_LITERAL (RTYPE (tree))
3239 /* avoid infinite loop */
3240 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3242 ast *litTree, *litParent;
3243 litTree = searchLitOp (tree, &litParent, "+-");
3246 if (litTree->opval.op == '+')
3249 litTree->right = newNode ('-', litTree->right, tree->right);
3250 litTree->right->lineno = tree->lineno;
3252 tree->right->opval.val = constVal ("0");
3254 else if (litTree->opval.op == '-')
3256 if (IS_LITERAL (RTYPE (litTree)))
3259 litTree->right = newNode ('+', tree->right, litTree->right);
3260 litTree->right->lineno = tree->lineno;
3262 tree->right->opval.val = constVal ("0");
3267 ast *tTree = litTree->right;
3268 litTree->right = tree->right;
3269 tree->right = tTree;
3272 decorateType (litParent, RESULT_CHECK);
3277 /*------------------------------------------------------------------*/
3278 /*----------------------------*/
3280 /*----------------------------*/
3282 /* can be only integral type */
3283 if (!IS_INTEGRAL (LTYPE (tree)))
3285 werror (E_UNARY_OP, tree->opval.op);
3286 goto errorTreeReturn;
3289 /* if left is a literal then do it */
3290 if (IS_LITERAL (LTYPE (tree)))
3292 tree->type = EX_VALUE;
3293 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3295 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3299 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3302 /*------------------------------------------------------------------*/
3303 /*----------------------------*/
3305 /*----------------------------*/
3307 /* can be pointer */
3308 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3309 !IS_PTR (LTYPE (tree)) &&
3310 !IS_ARRAY (LTYPE (tree)))
3312 werror (E_UNARY_OP, tree->opval.op);
3313 goto errorTreeReturn;
3316 /* if left is a literal then do it */
3317 if (IS_LITERAL (LTYPE (tree)))
3319 tree->type = EX_VALUE;
3320 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3322 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3326 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3329 /*------------------------------------------------------------------*/
3330 /*----------------------------*/
3332 /*----------------------------*/
3336 TTYPE (tree) = LTYPE (tree);
3337 TETYPE (tree) = LETYPE (tree);
3341 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3346 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3348 werror (E_SHIFT_OP_INVALID);
3349 werror (W_CONTINUE, "left & right types are ");
3350 printTypeChain (LTYPE (tree), stderr);
3351 fprintf (stderr, ",");
3352 printTypeChain (RTYPE (tree), stderr);
3353 fprintf (stderr, "\n");
3354 goto errorTreeReturn;
3357 /* if they are both literal then */
3358 /* rewrite the tree */
3359 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3361 tree->type = EX_VALUE;
3362 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3363 valFromType (RETYPE (tree)),
3364 (tree->opval.op == LEFT_OP ? 1 : 0));
3365 tree->right = tree->left = NULL;
3366 TETYPE (tree) = getSpec (TTYPE (tree) =
3367 tree->opval.val->type);
3371 LRVAL (tree) = RRVAL (tree) = 1;
3372 if (tree->opval.op == LEFT_OP)
3374 tree->left = addCast (tree->left, resultType, TRUE);
3375 TETYPE (tree) = getSpec (TTYPE (tree) =
3376 computeType (LTYPE (tree),
3378 resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
3382 /* no promotion necessary */
3383 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3384 if (IS_LITERAL (TTYPE (tree)))
3385 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3388 /* if only the right side is a literal & we are
3389 shifting more than size of the left operand then zero */
3390 if (IS_LITERAL (RTYPE (tree)) &&
3391 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3392 (getSize (TETYPE (tree)) * 8))
3394 if (tree->opval.op==LEFT_OP ||
3395 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3397 lineno=tree->lineno;
3398 werror (W_SHIFT_CHANGED,
3399 (tree->opval.op == LEFT_OP ? "left" : "right"));
3400 tree->type = EX_VALUE;
3401 tree->left = tree->right = NULL;
3402 tree->opval.val = constVal ("0");
3403 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3410 /*------------------------------------------------------------------*/
3411 /*----------------------------*/
3413 /*----------------------------*/
3414 case CAST: /* change the type */
3415 /* cannot cast to an aggregate type */
3416 if (IS_AGGREGATE (LTYPE (tree)))
3418 werror (E_CAST_ILLEGAL);
3419 goto errorTreeReturn;
3422 /* make sure the type is complete and sane */
3423 checkTypeSanity(LETYPE(tree), "(cast)");
3425 /* If code memory is read only, then pointers to code memory */
3426 /* implicitly point to constants -- make this explicit */
3428 sym_link *t = LTYPE(tree);
3429 while (t && t->next)
3431 if (IS_CODEPTR(t) && port->mem.code_ro)
3433 if (IS_SPEC(t->next))
3434 SPEC_CONST (t->next) = 1;
3436 DCL_PTR_CONST (t->next) = 1;
3443 /* if the right is a literal replace the tree */
3444 if (IS_LITERAL (RETYPE (tree))) {
3445 if (!IS_PTR (LTYPE (tree))) {
3446 tree->type = EX_VALUE;
3448 valCastLiteral (LTYPE (tree),
3449 floatFromVal (valFromType (RETYPE (tree))));
3452 TTYPE (tree) = tree->opval.val->type;
3453 tree->values.literalFromCast = 1;
3454 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3455 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3456 sym_link *rest = LTYPE(tree)->next;
3457 werror(W_LITERAL_GENERIC);
3458 TTYPE(tree) = newLink(DECLARATOR);
3459 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3460 TTYPE(tree)->next = rest;
3461 tree->left->opval.lnk = TTYPE(tree);
3464 TTYPE (tree) = LTYPE (tree);
3468 TTYPE (tree) = LTYPE (tree);
3472 #if 0 // this is already checked, now this could be explicit
3473 /* if pointer to struct then check names */
3474 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3475 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3476 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3478 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3479 SPEC_STRUCT(LETYPE(tree))->tag);
3482 if (IS_ADDRESS_OF_OP(tree->right)
3483 && IS_AST_SYM_VALUE (tree->right->left)
3484 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3486 tree->type = EX_VALUE;
3488 valCastLiteral (LTYPE (tree),
3489 SPEC_ADDR (AST_SYMBOL (tree->right->left)->etype));
3490 TTYPE (tree) = tree->opval.val->type;
3491 TETYPE (tree) = getSpec (TTYPE (tree));
3494 tree->values.literalFromCast = 1;
3498 /* handle offsetof macro: */
3499 /* #define offsetof(TYPE, MEMBER) \ */
3500 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3501 if (IS_ADDRESS_OF_OP(tree->right)
3502 && IS_AST_OP (tree->right->left)
3503 && tree->right->left->opval.op == PTR_OP
3504 && IS_AST_OP (tree->right->left->left)
3505 && tree->right->left->left->opval.op == CAST
3506 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3508 symbol *element = getStructElement (
3509 SPEC_STRUCT (LETYPE(tree->right->left)),
3510 AST_SYMBOL(tree->right->left->right)
3514 tree->type = EX_VALUE;
3515 tree->opval.val = valCastLiteral (
3518 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3521 TTYPE (tree) = tree->opval.val->type;
3522 TETYPE (tree) = getSpec (TTYPE (tree));
3529 /* if the right is a literal replace the tree */
3530 if (IS_LITERAL (RETYPE (tree))) {
3532 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3533 /* rewrite (type *)litaddr
3535 and define type at litaddr temp
3536 (but only if type's storage class is not generic)
3538 ast *newTree = newNode ('&', NULL, NULL);
3541 TTYPE (newTree) = LTYPE (tree);
3542 TETYPE (newTree) = getSpec(LTYPE (tree));
3544 /* define a global symbol at the casted address*/
3545 sym = newSymbol(genSymName (0), 0);
3546 sym->type = LTYPE (tree)->next;
3548 sym->type = newLink (V_VOID);
3549 sym->etype = getSpec(sym->type);
3550 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3551 sym->lineDef = tree->lineno;
3554 SPEC_STAT (sym->etype) = 1;
3555 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3556 SPEC_ABSA(sym->etype) = 1;
3557 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3560 newTree->left = newAst_VALUE(symbolVal(sym));
3561 newTree->left->lineno = tree->lineno;
3562 LTYPE (newTree) = sym->type;
3563 LETYPE (newTree) = sym->etype;
3564 LLVAL (newTree) = 1;
3565 LRVAL (newTree) = 0;
3566 TLVAL (newTree) = 1;
3570 if (!IS_PTR (LTYPE (tree))) {
3571 tree->type = EX_VALUE;
3573 valCastLiteral (LTYPE (tree),
3574 floatFromVal (valFromType (RTYPE (tree))));
3575 TTYPE (tree) = tree->opval.val->type;
3578 tree->values.literalFromCast = 1;
3579 TETYPE (tree) = getSpec (TTYPE (tree));
3583 TTYPE (tree) = LTYPE (tree);
3587 TETYPE (tree) = getSpec (TTYPE (tree));
3591 /*------------------------------------------------------------------*/
3592 /*----------------------------*/
3593 /* logical &&, || */
3594 /*----------------------------*/
3597 /* each must me arithmetic type or be a pointer */
3598 if (!IS_PTR (LTYPE (tree)) &&
3599 !IS_ARRAY (LTYPE (tree)) &&
3600 !IS_INTEGRAL (LTYPE (tree)))
3602 werror (E_COMPARE_OP);
3603 goto errorTreeReturn;
3606 if (!IS_PTR (RTYPE (tree)) &&
3607 !IS_ARRAY (RTYPE (tree)) &&
3608 !IS_INTEGRAL (RTYPE (tree)))
3610 werror (E_COMPARE_OP);
3611 goto errorTreeReturn;
3613 /* if they are both literal then */
3614 /* rewrite the tree */
3615 if (IS_LITERAL (RTYPE (tree)) &&
3616 IS_LITERAL (LTYPE (tree)))
3618 tree->type = EX_VALUE;
3619 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
3620 valFromType (RTYPE (tree)),
3622 tree->right = tree->left = NULL;
3623 TETYPE (tree) = getSpec (TTYPE (tree) =
3624 tree->opval.val->type);
3627 LRVAL (tree) = RRVAL (tree) = 1;
3628 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3631 /*------------------------------------------------------------------*/
3632 /*----------------------------*/
3633 /* comparison operators */
3634 /*----------------------------*/
3642 ast *lt = optimizeCompare (tree);
3648 /* if they are pointers they must be castable */
3649 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3651 if (tree->opval.op==EQ_OP &&
3652 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
3653 // we cannot cast a gptr to a !gptr: switch the leaves
3654 struct ast *s=tree->left;
3655 tree->left=tree->right;
3658 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3660 werror (E_COMPARE_OP);
3661 fprintf (stderr, "comparing type ");
3662 printTypeChain (LTYPE (tree), stderr);
3663 fprintf (stderr, "to type ");
3664 printTypeChain (RTYPE (tree), stderr);
3665 fprintf (stderr, "\n");
3666 goto errorTreeReturn;
3669 /* else they should be promotable to one another */
3672 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
3673 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
3675 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3677 werror (E_COMPARE_OP);
3678 fprintf (stderr, "comparing type ");
3679 printTypeChain (LTYPE (tree), stderr);
3680 fprintf (stderr, "to type ");
3681 printTypeChain (RTYPE (tree), stderr);
3682 fprintf (stderr, "\n");
3683 goto errorTreeReturn;
3686 /* if unsigned value < 0 then always false */
3687 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
3688 if (SPEC_USIGN(LETYPE(tree)) &&
3689 !IS_CHAR(LETYPE(tree)) && /* promotion to signed int */
3690 IS_LITERAL(RTYPE(tree)) &&
3691 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
3693 if (tree->opval.op == '<')
3697 if (tree->opval.op == '>')
3699 if (resultType == RESULT_TYPE_IFX)
3701 /* the parent is an ifx: */
3702 /* if (unsigned value) */
3706 /* (unsigned value) ? 1 : 0 */
3707 tree->opval.op = '?';
3708 tree->right = newNode (':',
3709 newAst_VALUE (constVal ("1")),
3710 tree->right); /* val 0 */
3711 tree->right->lineno = tree->lineno;
3712 tree->right->left->lineno = tree->lineno;
3713 decorateType (tree->right, RESULT_CHECK);
3716 /* if they are both literal then */
3717 /* rewrite the tree */
3718 if (IS_LITERAL (RTYPE (tree)) &&
3719 IS_LITERAL (LTYPE (tree)))
3721 tree->type = EX_VALUE;
3722 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
3723 valFromType (RETYPE (tree)),
3725 tree->right = tree->left = NULL;
3726 TETYPE (tree) = getSpec (TTYPE (tree) =
3727 tree->opval.val->type);
3730 LRVAL (tree) = RRVAL (tree) = 1;
3731 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3734 /*------------------------------------------------------------------*/
3735 /*----------------------------*/
3737 /*----------------------------*/
3738 case SIZEOF: /* evaluate wihout code generation */
3739 /* change the type to a integer */
3741 int size = getSize (tree->right->ftype);
3742 SNPRINTF(buffer, sizeof(buffer), "%d", size);
3743 if (!size && !IS_VOID(tree->right->ftype))
3744 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
3746 tree->type = EX_VALUE;
3747 tree->opval.val = constVal (buffer);
3748 tree->right = tree->left = NULL;
3749 TETYPE (tree) = getSpec (TTYPE (tree) =
3750 tree->opval.val->type);
3753 /*------------------------------------------------------------------*/
3754 /*----------------------------*/
3756 /*----------------------------*/
3758 /* return typeof enum value */
3759 tree->type = EX_VALUE;
3762 if (IS_SPEC(tree->right->ftype)) {
3763 switch (SPEC_NOUN(tree->right->ftype)) {
3765 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
3766 else typeofv = TYPEOF_INT;
3769 typeofv = TYPEOF_FLOAT;
3772 typeofv = TYPEOF_CHAR;
3775 typeofv = TYPEOF_VOID;
3778 typeofv = TYPEOF_STRUCT;
3781 typeofv = TYPEOF_BITFIELD;
3784 typeofv = TYPEOF_BIT;
3787 typeofv = TYPEOF_SBIT;
3793 switch (DCL_TYPE(tree->right->ftype)) {
3795 typeofv = TYPEOF_POINTER;
3798 typeofv = TYPEOF_FPOINTER;
3801 typeofv = TYPEOF_CPOINTER;
3804 typeofv = TYPEOF_GPOINTER;
3807 typeofv = TYPEOF_PPOINTER;
3810 typeofv = TYPEOF_IPOINTER;
3813 typeofv = TYPEOF_ARRAY;
3816 typeofv = TYPEOF_FUNCTION;
3822 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
3823 tree->opval.val = constVal (buffer);
3824 tree->right = tree->left = NULL;
3825 TETYPE (tree) = getSpec (TTYPE (tree) =
3826 tree->opval.val->type);
3829 /*------------------------------------------------------------------*/
3830 /*----------------------------*/
3831 /* conditional operator '?' */
3832 /*----------------------------*/
3834 /* the type is value of the colon operator (on the right) */
3835 assert (IS_COLON_OP (tree->right));
3836 /* if already known then replace the tree : optimizer will do it
3837 but faster to do it here */
3838 if (IS_LITERAL (LTYPE (tree)))
3840 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
3841 return decorateType (tree->right->left, resultTypeProp);
3843 return decorateType (tree->right->right, resultTypeProp);
3847 tree->right = decorateType (tree->right, resultTypeProp);
3848 TTYPE (tree) = RTYPE (tree);
3849 TETYPE (tree) = getSpec (TTYPE (tree));
3854 /* if they don't match we have a problem */
3855 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3857 werror (E_TYPE_MISMATCH, "conditional operator", " ");
3858 goto errorTreeReturn;
3861 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
3862 TETYPE (tree) = getSpec (TTYPE (tree));
3866 #if 0 // assignment operators are converted by the parser
3867 /*------------------------------------------------------------------*/
3868 /*----------------------------*/
3869 /* assignment operators */
3870 /*----------------------------*/
3873 /* for these it must be both must be integral */
3874 if (!IS_ARITHMETIC (LTYPE (tree)) ||
3875 !IS_ARITHMETIC (RTYPE (tree)))
3877 werror (E_OPS_INTEGRAL);
3878 goto errorTreeReturn;
3881 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3883 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
3884 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
3888 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
3889 goto errorTreeReturn;
3900 /* for these it must be both must be integral */
3901 if (!IS_INTEGRAL (LTYPE (tree)) ||
3902 !IS_INTEGRAL (RTYPE (tree)))
3904 werror (E_OPS_INTEGRAL);
3905 goto errorTreeReturn;
3908 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3910 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3911 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
3915 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
3916 goto errorTreeReturn;
3922 /*------------------------------------------------------------------*/
3923 /*----------------------------*/
3925 /*----------------------------*/
3927 if (!(IS_PTR (LTYPE (tree)) ||
3928 IS_ARITHMETIC (LTYPE (tree))))
3930 werror (E_PLUS_INVALID, "-=");
3931 goto errorTreeReturn;
3934 if (!(IS_PTR (RTYPE (tree)) ||
3935 IS_ARITHMETIC (RTYPE (tree))))
3937 werror (E_PLUS_INVALID, "-=");
3938 goto errorTreeReturn;
3941 TETYPE (tree) = getSpec (TTYPE (tree) =
3942 computeType (LTYPE (tree),
3946 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3947 werror (E_CODE_WRITE, "-=");
3951 werror (E_LVALUE_REQUIRED, "-=");
3952 goto errorTreeReturn;
3958 /*------------------------------------------------------------------*/
3959 /*----------------------------*/
3961 /*----------------------------*/
3963 /* this is not a unary operation */
3964 /* if both pointers then problem */
3965 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3967 werror (E_PTR_PLUS_PTR);
3968 goto errorTreeReturn;
3971 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
3973 werror (E_PLUS_INVALID, "+=");
3974 goto errorTreeReturn;
3977 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
3979 werror (E_PLUS_INVALID, "+=");
3980 goto errorTreeReturn;
3983 TETYPE (tree) = getSpec (TTYPE (tree) =
3984 computeType (LTYPE (tree),
3988 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
3989 werror (E_CODE_WRITE, "+=");
3993 werror (E_LVALUE_REQUIRED, "+=");
3994 goto errorTreeReturn;
3997 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_CHECK);
3998 tree->opval.op = '=';
4003 /*------------------------------------------------------------------*/
4004 /*----------------------------*/
4005 /* straight assignemnt */
4006 /*----------------------------*/
4008 /* cannot be an aggregate */
4009 if (IS_AGGREGATE (LTYPE (tree)))
4011 werror (E_AGGR_ASSIGN);
4012 goto errorTreeReturn;
4015 /* they should either match or be castable */
4016 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4018 werror (E_TYPE_MISMATCH, "assignment", " ");
4019 printFromToType(RTYPE(tree),LTYPE(tree));
4022 /* if the left side of the tree is of type void
4023 then report error */
4024 if (IS_VOID (LTYPE (tree)))
4026 werror (E_CAST_ZERO);
4027 printFromToType(RTYPE(tree), LTYPE(tree));
4030 TETYPE (tree) = getSpec (TTYPE (tree) =
4034 if (!tree->initMode ) {
4035 if (IS_CONSTANT(LTYPE(tree)))
4036 werror (E_CODE_WRITE, "=");
4040 werror (E_LVALUE_REQUIRED, "=");
4041 goto errorTreeReturn;
4046 /*------------------------------------------------------------------*/
4047 /*----------------------------*/
4048 /* comma operator */
4049 /*----------------------------*/
4051 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4054 /*------------------------------------------------------------------*/
4055 /*----------------------------*/
4057 /*----------------------------*/
4060 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4061 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4063 if (tree->left->opval.op == '*' && !tree->left->right)
4064 tree->left = tree->left->left;
4067 /* require a function or pointer to function */
4068 if (!IS_FUNC (LTYPE (tree))
4069 && !(IS_CODEPTR (LTYPE (tree)) && IS_FUNC (LTYPE (tree)->next)))
4071 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4072 goto errorTreeReturn;
4079 if (IS_CODEPTR(LTYPE(tree)))
4080 functype = LTYPE (tree)->next;
4082 functype = LTYPE (tree);
4084 if (processParms (tree->left, FUNC_ARGS(functype),
4085 tree->right, &parmNumber, TRUE)) {
4086 goto errorTreeReturn;
4089 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4090 !IFFUNC_ISBUILTIN(functype))
4092 reverseParms (tree->right);
4095 TTYPE (tree) = functype->next;
4096 TETYPE (tree) = getSpec (TTYPE (tree));
4100 /*------------------------------------------------------------------*/
4101 /*----------------------------*/
4102 /* return statement */
4103 /*----------------------------*/
4108 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4110 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4111 printFromToType (RTYPE(tree), currFunc->type->next);
4112 goto errorTreeReturn;
4115 if (IS_VOID (currFunc->type->next)
4117 !IS_VOID (RTYPE (tree)))
4119 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4120 goto errorTreeReturn;
4123 /* if there is going to be a casting required then add it */
4124 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4127 decorateType (newNode (CAST,
4128 newAst_LINK (copyLinkChain (currFunc->type->next)),
4138 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4140 werror (W_VOID_FUNC, currFunc->name);
4141 goto errorTreeReturn;
4144 TTYPE (tree) = TETYPE (tree) = NULL;
4147 /*------------------------------------------------------------------*/
4148 /*----------------------------*/
4149 /* switch statement */
4150 /*----------------------------*/
4152 /* the switch value must be an integer */
4153 if (!IS_INTEGRAL (LTYPE (tree)))
4155 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4156 goto errorTreeReturn;
4159 TTYPE (tree) = TETYPE (tree) = NULL;
4162 /*------------------------------------------------------------------*/
4163 /*----------------------------*/
4165 /*----------------------------*/
4167 tree->left = backPatchLabels (tree->left,
4170 TTYPE (tree) = TETYPE (tree) = NULL;
4173 /*------------------------------------------------------------------*/
4174 /*----------------------------*/
4176 /*----------------------------*/
4179 decorateType (resolveSymbols (AST_FOR (tree, initExpr)), RESULT_CHECK);
4180 decorateType (resolveSymbols (AST_FOR (tree, condExpr)), RESULT_CHECK);
4181 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_CHECK);
4183 /* if the for loop is reversible then
4184 reverse it otherwise do what we normally
4190 if (isLoopReversible (tree, &sym, &init, &end))
4191 return reverseLoop (tree, sym, init, end);
4193 return decorateType (createFor (AST_FOR (tree, trueLabel),
4194 AST_FOR (tree, continueLabel),
4195 AST_FOR (tree, falseLabel),
4196 AST_FOR (tree, condLabel),
4197 AST_FOR (tree, initExpr),
4198 AST_FOR (tree, condExpr),
4199 AST_FOR (tree, loopExpr),
4200 tree->left), RESULT_CHECK);
4203 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4204 "node PARAM shouldn't be processed here");
4205 /* but in processParams() */
4208 TTYPE (tree) = TETYPE (tree) = NULL;
4212 /* some error found this tree will be killed */
4214 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4215 tree->opval.op = NULLOP;
4221 /*-----------------------------------------------------------------*/
4222 /* sizeofOp - processes size of operation */
4223 /*-----------------------------------------------------------------*/
4225 sizeofOp (sym_link * type)
4230 /* make sure the type is complete and sane */
4231 checkTypeSanity(type, "(sizeof)");
4233 /* get the size and convert it to character */
4234 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4235 if (!size && !IS_VOID(type))
4236 werror (E_SIZEOF_INCOMPLETE_TYPE);
4238 /* now convert into value */
4239 return constVal (buff);
4243 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4244 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4245 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4246 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4247 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4248 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4249 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4251 /*-----------------------------------------------------------------*/
4252 /* backPatchLabels - change and or not operators to flow control */
4253 /*-----------------------------------------------------------------*/
4255 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4261 if (!(IS_ANDORNOT (tree)))
4264 /* if this an and */
4267 static int localLbl = 0;
4270 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4271 localLabel = newSymbol (buffer, NestLevel);
4273 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4275 /* if left is already a IFX then just change the if true label in that */
4276 if (!IS_IFX (tree->left))
4277 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4279 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4280 /* right is a IFX then just join */
4281 if (IS_IFX (tree->right))
4282 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4284 tree->right = createLabel (localLabel, tree->right);
4285 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4287 return newNode (NULLOP, tree->left, tree->right);
4290 /* if this is an or operation */
4293 static int localLbl = 0;
4296 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4297 localLabel = newSymbol (buffer, NestLevel);
4299 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4301 /* if left is already a IFX then just change the if true label in that */
4302 if (!IS_IFX (tree->left))
4303 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4305 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4306 /* right is a IFX then just join */
4307 if (IS_IFX (tree->right))
4308 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4310 tree->right = createLabel (localLabel, tree->right);
4311 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4313 return newNode (NULLOP, tree->left, tree->right);
4319 int wasnot = IS_NOT (tree->left);
4320 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4322 /* if the left is already a IFX */
4323 if (!IS_IFX (tree->left))
4324 tree->left = newNode (IFX, tree->left, NULL);
4328 tree->left->trueLabel = trueLabel;
4329 tree->left->falseLabel = falseLabel;
4333 tree->left->trueLabel = falseLabel;
4334 tree->left->falseLabel = trueLabel;
4341 tree->trueLabel = trueLabel;
4342 tree->falseLabel = falseLabel;
4349 /*-----------------------------------------------------------------*/
4350 /* createBlock - create expression tree for block */
4351 /*-----------------------------------------------------------------*/
4353 createBlock (symbol * decl, ast * body)
4357 /* if the block has nothing */
4361 ex = newNode (BLOCK, NULL, body);
4362 ex->values.sym = decl;
4364 ex->right = ex->right;
4370 /*-----------------------------------------------------------------*/
4371 /* createLabel - creates the expression tree for labels */
4372 /*-----------------------------------------------------------------*/
4374 createLabel (symbol * label, ast * stmnt)
4377 char name[SDCC_NAME_MAX + 1];
4380 /* must create fresh symbol if the symbol name */
4381 /* exists in the symbol table, since there can */
4382 /* be a variable with the same name as the labl */
4383 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4384 (csym->level == label->level))
4385 label = newSymbol (label->name, label->level);
4387 /* change the name before putting it in add _ */
4388 SNPRINTF(name, sizeof(name), "%s", label->name);
4390 /* put the label in the LabelSymbol table */
4391 /* but first check if a label of the same */
4393 if ((csym = findSym (LabelTab, NULL, name)))
4394 werror (E_DUPLICATE_LABEL, label->name);
4396 addSym (LabelTab, label, name, label->level, 0, 0);
4399 label->key = labelKey++;
4400 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4406 /*-----------------------------------------------------------------*/
4407 /* createCase - generates the parsetree for a case statement */
4408 /*-----------------------------------------------------------------*/
4410 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4412 char caseLbl[SDCC_NAME_MAX + 1];
4416 /* if the switch statement does not exist */
4417 /* then case is out of context */
4420 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4424 caseVal = decorateType (resolveSymbols (caseVal), RESULT_CHECK);
4425 /* if not a constant then error */
4426 if (!IS_LITERAL (caseVal->ftype))
4428 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4432 /* if not a integer than error */
4433 if (!IS_INTEGRAL (caseVal->ftype))
4435 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4439 /* find the end of the switch values chain */
4440 if (!(val = swStat->values.switchVals.swVals))
4441 swStat->values.switchVals.swVals = caseVal->opval.val;
4444 /* also order the cases according to value */
4446 int cVal = (int) floatFromVal (caseVal->opval.val);
4447 while (val && (int) floatFromVal (val) < cVal)
4453 /* if we reached the end then */
4456 pval->next = caseVal->opval.val;
4458 else if ((int) floatFromVal (val) == cVal)
4460 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
4466 /* we found a value greater than */
4467 /* the current value we must add this */
4468 /* before the value */
4469 caseVal->opval.val->next = val;
4471 /* if this was the first in chain */
4472 if (swStat->values.switchVals.swVals == val)
4473 swStat->values.switchVals.swVals =
4476 pval->next = caseVal->opval.val;
4481 /* create the case label */
4482 SNPRINTF(caseLbl, sizeof(caseLbl),
4484 swStat->values.switchVals.swNum,
4485 (int) floatFromVal (caseVal->opval.val));
4487 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
4492 /*-----------------------------------------------------------------*/
4493 /* createDefault - creates the parse tree for the default statement */
4494 /*-----------------------------------------------------------------*/
4496 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
4498 char defLbl[SDCC_NAME_MAX + 1];
4500 /* if the switch statement does not exist */
4501 /* then case is out of context */
4504 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
4508 if (swStat->values.switchVals.swDefault)
4510 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
4515 /* turn on the default flag */
4516 swStat->values.switchVals.swDefault = 1;
4518 /* create the label */
4519 SNPRINTF (defLbl, sizeof(defLbl),
4520 "_default_%d", swStat->values.switchVals.swNum);
4521 return createLabel (newSymbol (defLbl, 0), stmnt);
4524 /*-----------------------------------------------------------------*/
4525 /* createIf - creates the parsetree for the if statement */
4526 /*-----------------------------------------------------------------*/
4528 createIf (ast * condAst, ast * ifBody, ast * elseBody)
4530 static int Lblnum = 0;
4532 symbol *ifTrue, *ifFalse, *ifEnd;
4534 /* if neither exists */
4535 if (!elseBody && !ifBody) {
4536 // if there are no side effects (i++, j() etc)
4537 if (!hasSEFcalls(condAst)) {
4542 /* create the labels */
4543 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
4544 ifFalse = newSymbol (buffer, NestLevel);
4545 /* if no else body then end == false */
4550 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
4551 ifEnd = newSymbol (buffer, NestLevel);
4554 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
4555 ifTrue = newSymbol (buffer, NestLevel);
4559 /* attach the ifTrue label to the top of it body */
4560 ifBody = createLabel (ifTrue, ifBody);
4561 /* attach a goto end to the ifBody if else is present */
4564 ifBody = newNode (NULLOP, ifBody,
4566 newAst_VALUE (symbolVal (ifEnd)),
4568 /* put the elseLabel on the else body */
4569 elseBody = createLabel (ifFalse, elseBody);
4570 /* out the end at the end of the body */
4571 elseBody = newNode (NULLOP,
4573 createLabel (ifEnd, NULL));
4577 ifBody = newNode (NULLOP, ifBody,
4578 createLabel (ifFalse, NULL));
4580 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
4581 if (IS_IFX (condAst))
4584 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
4586 return newNode (NULLOP, ifTree,
4587 newNode (NULLOP, ifBody, elseBody));
4591 /*-----------------------------------------------------------------*/
4592 /* createDo - creates parse tree for do */
4595 /* _docontinue_n: */
4596 /* condition_expression +-> trueLabel -> _dobody_n */
4598 /* +-> falseLabel-> _dobreak_n */
4600 /*-----------------------------------------------------------------*/
4602 createDo (symbol * trueLabel, symbol * continueLabel,
4603 symbol * falseLabel, ast * condAst, ast * doBody)
4608 /* if the body does not exist then it is simple */
4611 condAst = backPatchLabels (condAst, continueLabel, NULL);
4612 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
4613 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
4614 doTree->trueLabel = continueLabel;
4615 doTree->falseLabel = NULL;
4619 /* otherwise we have a body */
4620 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
4622 /* attach the body label to the top */
4623 doBody = createLabel (trueLabel, doBody);
4624 /* attach the continue label to end of body */
4625 doBody = newNode (NULLOP, doBody,
4626 createLabel (continueLabel, NULL));
4628 /* now put the break label at the end */
4629 if (IS_IFX (condAst))
4632 doTree = newIfxNode (condAst, trueLabel, falseLabel);
4634 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
4636 /* putting it together */
4637 return newNode (NULLOP, doBody, doTree);
4640 /*-----------------------------------------------------------------*/
4641 /* createFor - creates parse tree for 'for' statement */
4644 /* condExpr +-> trueLabel -> _forbody_n */
4646 /* +-> falseLabel-> _forbreak_n */
4649 /* _forcontinue_n: */
4651 /* goto _forcond_n ; */
4653 /*-----------------------------------------------------------------*/
4655 createFor (symbol * trueLabel, symbol * continueLabel,
4656 symbol * falseLabel, symbol * condLabel,
4657 ast * initExpr, ast * condExpr, ast * loopExpr,
4662 /* if loopexpression not present then we can generate it */
4663 /* the same way as a while */
4665 return newNode (NULLOP, initExpr,
4666 createWhile (trueLabel, continueLabel,
4667 falseLabel, condExpr, forBody));
4668 /* vanilla for statement */
4669 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4671 if (condExpr && !IS_IFX (condExpr))
4672 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
4675 /* attach condition label to condition */
4676 condExpr = createLabel (condLabel, condExpr);
4678 /* attach body label to body */
4679 forBody = createLabel (trueLabel, forBody);
4681 /* attach continue to forLoop expression & attach */
4682 /* goto the forcond @ and of loopExpression */
4683 loopExpr = createLabel (continueLabel,
4687 newAst_VALUE (symbolVal (condLabel)),
4689 /* now start putting them together */
4690 forTree = newNode (NULLOP, initExpr, condExpr);
4691 forTree = newNode (NULLOP, forTree, forBody);
4692 forTree = newNode (NULLOP, forTree, loopExpr);
4693 /* finally add the break label */
4694 forTree = newNode (NULLOP, forTree,
4695 createLabel (falseLabel, NULL));
4699 /*-----------------------------------------------------------------*/
4700 /* createWhile - creates parse tree for while statement */
4701 /* the while statement will be created as follows */
4703 /* _while_continue_n: */
4704 /* condition_expression +-> trueLabel -> _while_boby_n */
4706 /* +-> falseLabel -> _while_break_n */
4707 /* _while_body_n: */
4709 /* goto _while_continue_n */
4710 /* _while_break_n: */
4711 /*-----------------------------------------------------------------*/
4713 createWhile (symbol * trueLabel, symbol * continueLabel,
4714 symbol * falseLabel, ast * condExpr, ast * whileBody)
4718 /* put the continue label */
4719 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4720 condExpr = createLabel (continueLabel, condExpr);
4721 condExpr->lineno = 0;
4723 /* put the body label in front of the body */
4724 whileBody = createLabel (trueLabel, whileBody);
4725 whileBody->lineno = 0;
4726 /* put a jump to continue at the end of the body */
4727 /* and put break label at the end of the body */
4728 whileBody = newNode (NULLOP,
4731 newAst_VALUE (symbolVal (continueLabel)),
4732 createLabel (falseLabel, NULL)));
4734 /* put it all together */
4735 if (IS_IFX (condExpr))
4736 whileTree = condExpr;
4739 whileTree = newNode (IFX, condExpr, NULL);
4740 /* put the true & false labels in place */
4741 whileTree->trueLabel = trueLabel;
4742 whileTree->falseLabel = falseLabel;
4745 return newNode (NULLOP, whileTree, whileBody);
4748 /*-----------------------------------------------------------------*/
4749 /* optimizeGetHbit - get highest order bit of the expression */
4750 /*-----------------------------------------------------------------*/
4752 optimizeGetHbit (ast * tree)
4755 /* if this is not a bit and */
4756 if (!IS_BITAND (tree))
4759 /* will look for tree of the form
4760 ( expr >> ((sizeof expr) -1) ) & 1 */
4761 if (!IS_AST_LIT_VALUE (tree->right))
4764 if (AST_LIT_VALUE (tree->right) != 1)
4767 if (!IS_RIGHT_OP (tree->left))
4770 if (!IS_AST_LIT_VALUE (tree->left->right))
4773 if ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
4774 (j = (getSize (TTYPE (tree->left->left)) * 8 - 1)))
4777 /* make sure the port supports GETHBIT */
4778 if (port->hasExtBitOp
4779 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (tree->left->left))))
4782 return decorateType (newNode (GETHBIT, tree->left->left, NULL), RESULT_CHECK);
4786 /*-----------------------------------------------------------------*/
4787 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
4788 /*-----------------------------------------------------------------*/
4790 optimizeRRCRLC (ast * root)
4792 /* will look for trees of the form
4793 (?expr << 1) | (?expr >> 7) or
4794 (?expr >> 7) | (?expr << 1) will make that
4795 into a RLC : operation ..
4797 (?expr >> 1) | (?expr << 7) or
4798 (?expr << 7) | (?expr >> 1) will make that
4799 into a RRC operation
4800 note : by 7 I mean (number of bits required to hold the
4802 /* if the root operations is not a | operation the not */
4803 if (!IS_BITOR (root))
4806 /* I have to think of a better way to match patterns this sucks */
4807 /* that aside let start looking for the first case : I use a the
4808 negative check a lot to improve the efficiency */
4809 /* (?expr << 1) | (?expr >> 7) */
4810 if (IS_LEFT_OP (root->left) &&
4811 IS_RIGHT_OP (root->right))
4814 if (!SPEC_USIGN (TETYPE (root->left->left)))
4817 if (!IS_AST_LIT_VALUE (root->left->right) ||
4818 !IS_AST_LIT_VALUE (root->right->right))
4821 /* make sure it is the same expression */
4822 if (!isAstEqual (root->left->left,
4826 if (AST_LIT_VALUE (root->left->right) != 1)
4829 if (AST_LIT_VALUE (root->right->right) !=
4830 (getSize (TTYPE (root->left->left)) * 8 - 1))
4833 /* make sure the port supports RLC */
4834 if (port->hasExtBitOp
4835 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
4838 /* whew got the first case : create the AST */
4839 return newNode (RLC, root->left->left, NULL);
4843 /* check for second case */
4844 /* (?expr >> 7) | (?expr << 1) */
4845 if (IS_LEFT_OP (root->right) &&
4846 IS_RIGHT_OP (root->left))
4849 if (!SPEC_USIGN (TETYPE (root->left->left)))
4852 if (!IS_AST_LIT_VALUE (root->left->right) ||
4853 !IS_AST_LIT_VALUE (root->right->right))
4856 /* make sure it is the same symbol */
4857 if (!isAstEqual (root->left->left,
4861 if (AST_LIT_VALUE (root->right->right) != 1)
4864 if (AST_LIT_VALUE (root->left->right) !=
4865 (getSize (TTYPE (root->left->left)) * 8 - 1))
4868 /* make sure the port supports RLC */
4869 if (port->hasExtBitOp
4870 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
4873 /* whew got the first case : create the AST */
4874 return newNode (RLC, root->left->left, NULL);
4879 /* third case for RRC */
4880 /* (?symbol >> 1) | (?symbol << 7) */
4881 if (IS_LEFT_OP (root->right) &&
4882 IS_RIGHT_OP (root->left))
4885 if (!SPEC_USIGN (TETYPE (root->left->left)))
4888 if (!IS_AST_LIT_VALUE (root->left->right) ||
4889 !IS_AST_LIT_VALUE (root->right->right))
4892 /* make sure it is the same symbol */
4893 if (!isAstEqual (root->left->left,
4897 if (AST_LIT_VALUE (root->left->right) != 1)
4900 if (AST_LIT_VALUE (root->right->right) !=
4901 (getSize (TTYPE (root->left->left)) * 8 - 1))
4904 /* make sure the port supports RRC */
4905 if (port->hasExtBitOp
4906 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
4909 /* whew got the first case : create the AST */
4910 return newNode (RRC, root->left->left, NULL);
4914 /* fourth and last case for now */
4915 /* (?symbol << 7) | (?symbol >> 1) */
4916 if (IS_RIGHT_OP (root->right) &&
4917 IS_LEFT_OP (root->left))
4920 if (!SPEC_USIGN (TETYPE (root->left->left)))
4923 if (!IS_AST_LIT_VALUE (root->left->right) ||
4924 !IS_AST_LIT_VALUE (root->right->right))
4927 /* make sure it is the same symbol */
4928 if (!isAstEqual (root->left->left,
4932 if (AST_LIT_VALUE (root->right->right) != 1)
4935 if (AST_LIT_VALUE (root->left->right) !=
4936 (getSize (TTYPE (root->left->left)) * 8 - 1))
4939 /* make sure the port supports RRC */
4940 if (port->hasExtBitOp
4941 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
4944 /* whew got the first case : create the AST */
4945 return newNode (RRC, root->left->left, NULL);
4949 /* not found return root */
4953 /*-----------------------------------------------------------------*/
4954 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
4955 /*-----------------------------------------------------------------*/
4957 optimizeSWAP (ast * root)
4959 /* will look for trees of the form
4960 (?expr << 4) | (?expr >> 4) or
4961 (?expr >> 4) | (?expr << 4) will make that
4962 into a SWAP : operation ..
4963 note : by 4 I mean (number of bits required to hold the
4965 /* if the root operations is not a | operation the not */
4966 if (!IS_BITOR (root))
4969 /* (?expr << 4) | (?expr >> 4) */
4970 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
4971 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
4974 if (!SPEC_USIGN (TETYPE (root->left->left)))
4977 if (!IS_AST_LIT_VALUE (root->left->right) ||
4978 !IS_AST_LIT_VALUE (root->right->right))
4981 /* make sure it is the same expression */
4982 if (!isAstEqual (root->left->left,
4986 if (AST_LIT_VALUE (root->left->right) !=
4987 (getSize (TTYPE (root->left->left)) * 4))
4990 if (AST_LIT_VALUE (root->right->right) !=
4991 (getSize (TTYPE (root->left->left)) * 4))
4994 /* make sure the port supports SWAP */
4995 if (port->hasExtBitOp
4996 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
4999 /* found it : create the AST */
5000 return newNode (SWAP, root->left->left, NULL);
5004 /* not found return root */
5008 /*-----------------------------------------------------------------*/
5009 /* optimizeCompare - otimizes compares for bit variables */
5010 /*-----------------------------------------------------------------*/
5012 optimizeCompare (ast * root)
5014 ast *optExpr = NULL;
5017 unsigned int litValue;
5019 /* if nothing then return nothing */
5023 /* if not a compare op then do leaves */
5024 if (!IS_COMPARE_OP (root))
5026 root->left = optimizeCompare (root->left);
5027 root->right = optimizeCompare (root->right);
5031 /* if left & right are the same then depending
5032 of the operation do */
5033 if (isAstEqual (root->left, root->right))
5035 switch (root->opval.op)
5040 optExpr = newAst_VALUE (constVal ("0"));
5045 optExpr = newAst_VALUE (constVal ("1"));
5049 return decorateType (optExpr, RESULT_CHECK);
5052 vleft = (root->left->type == EX_VALUE ?
5053 root->left->opval.val : NULL);
5055 vright = (root->right->type == EX_VALUE ?
5056 root->right->opval.val : NULL);
5058 /* if left is a BITVAR in BITSPACE */
5059 /* and right is a LITERAL then opt- */
5060 /* imize else do nothing */
5061 if (vleft && vright &&
5062 IS_BITVAR (vleft->etype) &&
5063 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5064 IS_LITERAL (vright->etype))
5067 /* if right side > 1 then comparison may never succeed */
5068 if ((litValue = (int) floatFromVal (vright)) > 1)
5070 werror (W_BAD_COMPARE);
5076 switch (root->opval.op)
5078 case '>': /* bit value greater than 1 cannot be */
5079 werror (W_BAD_COMPARE);
5083 case '<': /* bit value < 1 means 0 */
5085 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5088 case LE_OP: /* bit value <= 1 means no check */
5089 optExpr = newAst_VALUE (vright);
5092 case GE_OP: /* bit value >= 1 means only check for = */
5094 optExpr = newAst_VALUE (vleft);
5099 { /* literal is zero */
5100 switch (root->opval.op)
5102 case '<': /* bit value < 0 cannot be */
5103 werror (W_BAD_COMPARE);
5107 case '>': /* bit value > 0 means 1 */
5109 optExpr = newAst_VALUE (vleft);
5112 case LE_OP: /* bit value <= 0 means no check */
5113 case GE_OP: /* bit value >= 0 means no check */
5114 werror (W_BAD_COMPARE);
5118 case EQ_OP: /* bit == 0 means ! of bit */
5119 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5123 return decorateType (resolveSymbols (optExpr), RESULT_CHECK);
5124 } /* end-of-if of BITVAR */
5129 /*-----------------------------------------------------------------*/
5130 /* addSymToBlock : adds the symbol to the first block we find */
5131 /*-----------------------------------------------------------------*/
5133 addSymToBlock (symbol * sym, ast * tree)
5135 /* reached end of tree or a leaf */
5136 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5140 if (IS_AST_OP (tree) &&
5141 tree->opval.op == BLOCK)
5144 symbol *lsym = copySymbol (sym);
5146 lsym->next = AST_VALUES (tree, sym);
5147 AST_VALUES (tree, sym) = lsym;
5151 addSymToBlock (sym, tree->left);
5152 addSymToBlock (sym, tree->right);
5155 /*-----------------------------------------------------------------*/
5156 /* processRegParms - do processing for register parameters */
5157 /*-----------------------------------------------------------------*/
5159 processRegParms (value * args, ast * body)
5163 if (IS_REGPARM (args->etype))
5164 addSymToBlock (args->sym, body);
5169 /*-----------------------------------------------------------------*/
5170 /* resetParmKey - resets the operandkeys for the symbols */
5171 /*-----------------------------------------------------------------*/
5172 DEFSETFUNC (resetParmKey)
5183 /*-----------------------------------------------------------------*/
5184 /* createFunction - This is the key node that calls the iCode for */
5185 /* generating the code for a function. Note code */
5186 /* is generated function by function, later when */
5187 /* add inter-procedural analysis this will change */
5188 /*-----------------------------------------------------------------*/
5190 createFunction (symbol * name, ast * body)
5196 iCode *piCode = NULL;
5198 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5199 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5201 /* if check function return 0 then some problem */
5202 if (checkFunction (name, NULL) == 0)
5205 /* create a dummy block if none exists */
5207 body = newNode (BLOCK, NULL, NULL);
5211 /* check if the function name already in the symbol table */
5212 if ((csym = findSym (SymbolTab, NULL, name->name)))
5215 /* special case for compiler defined functions
5216 we need to add the name to the publics list : this
5217 actually means we are now compiling the compiler
5221 addSet (&publics, name);
5227 allocVariables (name);
5229 name->lastLine = mylineno;
5232 /* set the stack pointer */
5233 /* PENDING: check this for the mcs51 */
5234 stackPtr = -port->stack.direction * port->stack.call_overhead;
5235 if (IFFUNC_ISISR (name->type))
5236 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5237 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5238 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5240 xstackPtr = -port->stack.direction * port->stack.call_overhead;
5242 fetype = getSpec (name->type); /* get the specifier for the function */
5243 /* if this is a reentrant function then */
5244 if (IFFUNC_ISREENT (name->type))
5247 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5249 /* do processing for parameters that are passed in registers */
5250 processRegParms (FUNC_ARGS(name->type), body);
5252 /* set the stack pointer */
5256 /* allocate & autoinit the block variables */
5257 processBlockVars (body, &stack, ALLOCATE);
5259 /* save the stack information */
5260 if (options.useXstack)
5261 name->xstack = SPEC_STAK (fetype) = stack;
5263 name->stack = SPEC_STAK (fetype) = stack;
5265 /* name needs to be mangled */
5266 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5268 body = resolveSymbols (body); /* resolve the symbols */
5269 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5272 ex = newAst_VALUE (symbolVal (name)); /* create name */
5273 ex = newNode (FUNCTION, ex, body);
5274 ex->values.args = FUNC_ARGS(name->type);
5276 if (options.dump_tree) PA(ex);
5279 werror (E_FUNC_NO_CODE, name->name);
5283 /* create the node & generate intermediate code */
5285 codeOutFile = code->oFile;
5286 piCode = iCodeFromAst (ex);
5290 werror (E_FUNC_NO_CODE, name->name);
5294 eBBlockFromiCode (piCode);
5296 /* if there are any statics then do them */
5299 GcurMemmap = statsg;
5300 codeOutFile = statsg->oFile;
5301 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_CHECK)));
5307 /* dealloc the block variables */
5308 processBlockVars (body, &stack, DEALLOCATE);
5309 outputDebugStackSymbols();
5310 /* deallocate paramaters */
5311 deallocParms (FUNC_ARGS(name->type));
5313 if (IFFUNC_ISREENT (name->type))
5316 /* we are done freeup memory & cleanup */
5318 if (port->reset_labelKey) labelKey = 1;
5320 FUNC_HASBODY(name->type) = 1;
5321 addSet (&operKeyReset, name);
5322 applyToSet (operKeyReset, resetParmKey);
5327 cleanUpLevel (LabelTab, 0);
5328 cleanUpBlock (StructTab, 1);
5329 cleanUpBlock (TypedefTab, 1);
5331 xstack->syms = NULL;
5332 istack->syms = NULL;
5337 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
5338 /*-----------------------------------------------------------------*/
5339 /* ast_print : prints the ast (for debugging purposes) */
5340 /*-----------------------------------------------------------------*/
5342 void ast_print (ast * tree, FILE *outfile, int indent)
5347 /* can print only decorated trees */
5348 if (!tree->decorated) return;
5350 /* if any child is an error | this one is an error do nothing */
5351 if (tree->isError ||
5352 (tree->left && tree->left->isError) ||
5353 (tree->right && tree->right->isError)) {
5354 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
5358 /* print the line */
5359 /* if not block & function */
5360 if (tree->type == EX_OP &&
5361 (tree->opval.op != FUNCTION &&
5362 tree->opval.op != BLOCK &&
5363 tree->opval.op != NULLOP)) {
5366 if (tree->opval.op == FUNCTION) {
5368 value *args=FUNC_ARGS(tree->left->opval.val->type);
5369 fprintf(outfile,"FUNCTION (%s=%p) type (",
5370 tree->left->opval.val->name, tree);
5371 printTypeChain (tree->left->opval.val->type->next,outfile);
5372 fprintf(outfile,") args (");
5375 fprintf (outfile, ", ");
5377 printTypeChain (args ? args->type : NULL, outfile);
5379 args= args ? args->next : NULL;
5381 fprintf(outfile,")\n");
5382 ast_print(tree->left,outfile,indent);
5383 ast_print(tree->right,outfile,indent);
5386 if (tree->opval.op == BLOCK) {
5387 symbol *decls = tree->values.sym;
5388 INDENT(indent,outfile);
5389 fprintf(outfile,"{\n");
5391 INDENT(indent+2,outfile);
5392 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
5393 decls->name, decls);
5394 printTypeChain(decls->type,outfile);
5395 fprintf(outfile,")\n");
5397 decls = decls->next;
5399 ast_print(tree->right,outfile,indent+2);
5400 INDENT(indent,outfile);
5401 fprintf(outfile,"}\n");
5404 if (tree->opval.op == NULLOP) {
5405 ast_print(tree->left,outfile,indent);
5406 ast_print(tree->right,outfile,indent);
5409 INDENT(indent,outfile);
5411 /*------------------------------------------------------------------*/
5412 /*----------------------------*/
5413 /* leaf has been reached */
5414 /*----------------------------*/
5415 /* if this is of type value */
5416 /* just get the type */
5417 if (tree->type == EX_VALUE) {
5419 if (IS_LITERAL (tree->opval.val->etype)) {
5420 fprintf(outfile,"CONSTANT (%p) value = ", tree);
5421 if (SPEC_USIGN (tree->opval.val->etype))
5422 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
5424 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
5425 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
5426 floatFromVal(tree->opval.val));
5427 } else if (tree->opval.val->sym) {
5428 /* if the undefined flag is set then give error message */
5429 if (tree->opval.val->sym->undefined) {
5430 fprintf(outfile,"UNDEFINED SYMBOL ");
5432 fprintf(outfile,"SYMBOL ");
5434 fprintf(outfile,"(%s=%p)",
5435 tree->opval.val->sym->name,tree);
5438 fprintf(outfile," type (");
5439 printTypeChain(tree->ftype,outfile);
5440 fprintf(outfile,")\n");
5442 fprintf(outfile,"\n");
5447 /* if type link for the case of cast */
5448 if (tree->type == EX_LINK) {
5449 fprintf(outfile,"TYPENODE (%p) type = (",tree);
5450 printTypeChain(tree->opval.lnk,outfile);
5451 fprintf(outfile,")\n");
5456 /* depending on type of operator do */
5458 switch (tree->opval.op) {
5459 /*------------------------------------------------------------------*/
5460 /*----------------------------*/
5462 /*----------------------------*/
5464 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
5465 printTypeChain(tree->ftype,outfile);
5466 fprintf(outfile,")\n");
5467 ast_print(tree->left,outfile,indent+2);
5468 ast_print(tree->right,outfile,indent+2);
5471 /*------------------------------------------------------------------*/
5472 /*----------------------------*/
5474 /*----------------------------*/
5476 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
5477 printTypeChain(tree->ftype,outfile);
5478 fprintf(outfile,")\n");
5479 ast_print(tree->left,outfile,indent+2);
5480 ast_print(tree->right,outfile,indent+2);
5483 /*------------------------------------------------------------------*/
5484 /*----------------------------*/
5485 /* struct/union pointer */
5486 /*----------------------------*/
5488 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
5489 printTypeChain(tree->ftype,outfile);
5490 fprintf(outfile,")\n");
5491 ast_print(tree->left,outfile,indent+2);
5492 ast_print(tree->right,outfile,indent+2);
5495 /*------------------------------------------------------------------*/
5496 /*----------------------------*/
5497 /* ++/-- operation */
5498 /*----------------------------*/
5501 fprintf(outfile,"post-");
5503 fprintf(outfile,"pre-");
5504 fprintf(outfile,"INC_OP (%p) type (",tree);
5505 printTypeChain(tree->ftype,outfile);
5506 fprintf(outfile,")\n");
5507 ast_print(tree->left,outfile,indent+2); /* postincrement case */
5508 ast_print(tree->right,outfile,indent+2); /* preincrement case */
5513 fprintf(outfile,"post-");
5515 fprintf(outfile,"pre-");
5516 fprintf(outfile,"DEC_OP (%p) type (",tree);
5517 printTypeChain(tree->ftype,outfile);
5518 fprintf(outfile,")\n");
5519 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
5520 ast_print(tree->right,outfile,indent+2); /* predecrement case */
5523 /*------------------------------------------------------------------*/
5524 /*----------------------------*/
5526 /*----------------------------*/
5529 fprintf(outfile,"& (%p) type (",tree);
5530 printTypeChain(tree->ftype,outfile);
5531 fprintf(outfile,")\n");
5532 ast_print(tree->left,outfile,indent+2);
5533 ast_print(tree->right,outfile,indent+2);
5535 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
5536 printTypeChain(tree->ftype,outfile);
5537 fprintf(outfile,")\n");
5538 ast_print(tree->left,outfile,indent+2);
5539 ast_print(tree->right,outfile,indent+2);
5542 /*----------------------------*/
5544 /*----------------------------*/
5546 fprintf(outfile,"OR (%p) type (",tree);
5547 printTypeChain(tree->ftype,outfile);
5548 fprintf(outfile,")\n");
5549 ast_print(tree->left,outfile,indent+2);
5550 ast_print(tree->right,outfile,indent+2);
5552 /*------------------------------------------------------------------*/
5553 /*----------------------------*/
5555 /*----------------------------*/
5557 fprintf(outfile,"XOR (%p) type (",tree);
5558 printTypeChain(tree->ftype,outfile);
5559 fprintf(outfile,")\n");
5560 ast_print(tree->left,outfile,indent+2);
5561 ast_print(tree->right,outfile,indent+2);
5564 /*------------------------------------------------------------------*/
5565 /*----------------------------*/
5567 /*----------------------------*/
5569 fprintf(outfile,"DIV (%p) type (",tree);
5570 printTypeChain(tree->ftype,outfile);
5571 fprintf(outfile,")\n");
5572 ast_print(tree->left,outfile,indent+2);
5573 ast_print(tree->right,outfile,indent+2);
5575 /*------------------------------------------------------------------*/
5576 /*----------------------------*/
5578 /*----------------------------*/
5580 fprintf(outfile,"MOD (%p) type (",tree);
5581 printTypeChain(tree->ftype,outfile);
5582 fprintf(outfile,")\n");
5583 ast_print(tree->left,outfile,indent+2);
5584 ast_print(tree->right,outfile,indent+2);
5587 /*------------------------------------------------------------------*/
5588 /*----------------------------*/
5589 /* address dereference */
5590 /*----------------------------*/
5591 case '*': /* can be unary : if right is null then unary operation */
5593 fprintf(outfile,"DEREF (%p) type (",tree);
5594 printTypeChain(tree->ftype,outfile);
5595 fprintf(outfile,")\n");
5596 ast_print(tree->left,outfile,indent+2);
5599 /*------------------------------------------------------------------*/
5600 /*----------------------------*/
5601 /* multiplication */
5602 /*----------------------------*/
5603 fprintf(outfile,"MULT (%p) type (",tree);
5604 printTypeChain(tree->ftype,outfile);
5605 fprintf(outfile,")\n");
5606 ast_print(tree->left,outfile,indent+2);
5607 ast_print(tree->right,outfile,indent+2);
5611 /*------------------------------------------------------------------*/
5612 /*----------------------------*/
5613 /* unary '+' operator */
5614 /*----------------------------*/
5618 fprintf(outfile,"UPLUS (%p) type (",tree);
5619 printTypeChain(tree->ftype,outfile);
5620 fprintf(outfile,")\n");
5621 ast_print(tree->left,outfile,indent+2);
5623 /*------------------------------------------------------------------*/
5624 /*----------------------------*/
5626 /*----------------------------*/
5627 fprintf(outfile,"ADD (%p) type (",tree);
5628 printTypeChain(tree->ftype,outfile);
5629 fprintf(outfile,")\n");
5630 ast_print(tree->left,outfile,indent+2);
5631 ast_print(tree->right,outfile,indent+2);
5634 /*------------------------------------------------------------------*/
5635 /*----------------------------*/
5637 /*----------------------------*/
5638 case '-': /* can be unary */
5640 fprintf(outfile,"UMINUS (%p) type (",tree);
5641 printTypeChain(tree->ftype,outfile);
5642 fprintf(outfile,")\n");
5643 ast_print(tree->left,outfile,indent+2);
5645 /*------------------------------------------------------------------*/
5646 /*----------------------------*/
5648 /*----------------------------*/
5649 fprintf(outfile,"SUB (%p) type (",tree);
5650 printTypeChain(tree->ftype,outfile);
5651 fprintf(outfile,")\n");
5652 ast_print(tree->left,outfile,indent+2);
5653 ast_print(tree->right,outfile,indent+2);
5656 /*------------------------------------------------------------------*/
5657 /*----------------------------*/
5659 /*----------------------------*/
5661 fprintf(outfile,"COMPL (%p) type (",tree);
5662 printTypeChain(tree->ftype,outfile);
5663 fprintf(outfile,")\n");
5664 ast_print(tree->left,outfile,indent+2);
5666 /*------------------------------------------------------------------*/
5667 /*----------------------------*/
5669 /*----------------------------*/
5671 fprintf(outfile,"NOT (%p) type (",tree);
5672 printTypeChain(tree->ftype,outfile);
5673 fprintf(outfile,")\n");
5674 ast_print(tree->left,outfile,indent+2);
5676 /*------------------------------------------------------------------*/
5677 /*----------------------------*/
5679 /*----------------------------*/
5681 fprintf(outfile,"RRC (%p) type (",tree);
5682 printTypeChain(tree->ftype,outfile);
5683 fprintf(outfile,")\n");
5684 ast_print(tree->left,outfile,indent+2);
5688 fprintf(outfile,"RLC (%p) type (",tree);
5689 printTypeChain(tree->ftype,outfile);
5690 fprintf(outfile,")\n");
5691 ast_print(tree->left,outfile,indent+2);
5694 fprintf(outfile,"SWAP (%p) type (",tree);
5695 printTypeChain(tree->ftype,outfile);
5696 fprintf(outfile,")\n");
5697 ast_print(tree->left,outfile,indent+2);
5700 fprintf(outfile,"GETHBIT (%p) type (",tree);
5701 printTypeChain(tree->ftype,outfile);
5702 fprintf(outfile,")\n");
5703 ast_print(tree->left,outfile,indent+2);
5706 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
5707 printTypeChain(tree->ftype,outfile);
5708 fprintf(outfile,")\n");
5709 ast_print(tree->left,outfile,indent+2);
5710 ast_print(tree->right,outfile,indent+2);
5713 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
5714 printTypeChain(tree->ftype,outfile);
5715 fprintf(outfile,")\n");
5716 ast_print(tree->left,outfile,indent+2);
5717 ast_print(tree->right,outfile,indent+2);
5719 /*------------------------------------------------------------------*/
5720 /*----------------------------*/
5722 /*----------------------------*/
5723 case CAST: /* change the type */
5724 fprintf(outfile,"CAST (%p) from type (",tree);
5725 printTypeChain(tree->right->ftype,outfile);
5726 fprintf(outfile,") to type (");
5727 printTypeChain(tree->ftype,outfile);
5728 fprintf(outfile,")\n");
5729 ast_print(tree->right,outfile,indent+2);
5733 fprintf(outfile,"ANDAND (%p) type (",tree);
5734 printTypeChain(tree->ftype,outfile);
5735 fprintf(outfile,")\n");
5736 ast_print(tree->left,outfile,indent+2);
5737 ast_print(tree->right,outfile,indent+2);
5740 fprintf(outfile,"OROR (%p) type (",tree);
5741 printTypeChain(tree->ftype,outfile);
5742 fprintf(outfile,")\n");
5743 ast_print(tree->left,outfile,indent+2);
5744 ast_print(tree->right,outfile,indent+2);
5747 /*------------------------------------------------------------------*/
5748 /*----------------------------*/
5749 /* comparison operators */
5750 /*----------------------------*/
5752 fprintf(outfile,"GT(>) (%p) type (",tree);
5753 printTypeChain(tree->ftype,outfile);
5754 fprintf(outfile,")\n");
5755 ast_print(tree->left,outfile,indent+2);
5756 ast_print(tree->right,outfile,indent+2);
5759 fprintf(outfile,"LT(<) (%p) type (",tree);
5760 printTypeChain(tree->ftype,outfile);
5761 fprintf(outfile,")\n");
5762 ast_print(tree->left,outfile,indent+2);
5763 ast_print(tree->right,outfile,indent+2);
5766 fprintf(outfile,"LE(<=) (%p) type (",tree);
5767 printTypeChain(tree->ftype,outfile);
5768 fprintf(outfile,")\n");
5769 ast_print(tree->left,outfile,indent+2);
5770 ast_print(tree->right,outfile,indent+2);
5773 fprintf(outfile,"GE(>=) (%p) type (",tree);
5774 printTypeChain(tree->ftype,outfile);
5775 fprintf(outfile,")\n");
5776 ast_print(tree->left,outfile,indent+2);
5777 ast_print(tree->right,outfile,indent+2);
5780 fprintf(outfile,"EQ(==) (%p) type (",tree);
5781 printTypeChain(tree->ftype,outfile);
5782 fprintf(outfile,")\n");
5783 ast_print(tree->left,outfile,indent+2);
5784 ast_print(tree->right,outfile,indent+2);
5787 fprintf(outfile,"NE(!=) (%p) type (",tree);
5788 printTypeChain(tree->ftype,outfile);
5789 fprintf(outfile,")\n");
5790 ast_print(tree->left,outfile,indent+2);
5791 ast_print(tree->right,outfile,indent+2);
5792 /*------------------------------------------------------------------*/
5793 /*----------------------------*/
5795 /*----------------------------*/
5796 case SIZEOF: /* evaluate wihout code generation */
5797 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
5800 /*------------------------------------------------------------------*/
5801 /*----------------------------*/
5802 /* conditional operator '?' */
5803 /*----------------------------*/
5805 fprintf(outfile,"QUEST(?) (%p) type (",tree);
5806 printTypeChain(tree->ftype,outfile);
5807 fprintf(outfile,")\n");
5808 ast_print(tree->left,outfile,indent+2);
5809 ast_print(tree->right,outfile,indent+2);
5813 fprintf(outfile,"COLON(:) (%p) type (",tree);
5814 printTypeChain(tree->ftype,outfile);
5815 fprintf(outfile,")\n");
5816 ast_print(tree->left,outfile,indent+2);
5817 ast_print(tree->right,outfile,indent+2);
5820 /*------------------------------------------------------------------*/
5821 /*----------------------------*/
5822 /* assignment operators */
5823 /*----------------------------*/
5825 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
5826 printTypeChain(tree->ftype,outfile);
5827 fprintf(outfile,")\n");
5828 ast_print(tree->left,outfile,indent+2);
5829 ast_print(tree->right,outfile,indent+2);
5832 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
5833 printTypeChain(tree->ftype,outfile);
5834 fprintf(outfile,")\n");
5835 ast_print(tree->left,outfile,indent+2);
5836 ast_print(tree->right,outfile,indent+2);
5839 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
5840 printTypeChain(tree->ftype,outfile);
5841 fprintf(outfile,")\n");
5842 ast_print(tree->left,outfile,indent+2);
5843 ast_print(tree->right,outfile,indent+2);
5846 fprintf(outfile,"ORASS(|=) (%p) type (",tree);
5847 printTypeChain(tree->ftype,outfile);
5848 fprintf(outfile,")\n");
5849 ast_print(tree->left,outfile,indent+2);
5850 ast_print(tree->right,outfile,indent+2);
5853 fprintf(outfile,"XORASS(^=) (%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);
5860 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
5861 printTypeChain(tree->ftype,outfile);
5862 fprintf(outfile,")\n");
5863 ast_print(tree->left,outfile,indent+2);
5864 ast_print(tree->right,outfile,indent+2);
5867 fprintf(outfile,"LSHFTASS(<<=) (%p) type (",tree);
5868 printTypeChain(tree->ftype,outfile);
5869 fprintf(outfile,")\n");
5870 ast_print(tree->left,outfile,indent+2);
5871 ast_print(tree->right,outfile,indent+2);
5873 /*------------------------------------------------------------------*/
5874 /*----------------------------*/
5876 /*----------------------------*/
5878 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
5879 printTypeChain(tree->ftype,outfile);
5880 fprintf(outfile,")\n");
5881 ast_print(tree->left,outfile,indent+2);
5882 ast_print(tree->right,outfile,indent+2);
5884 /*------------------------------------------------------------------*/
5885 /*----------------------------*/
5887 /*----------------------------*/
5889 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
5890 printTypeChain(tree->ftype,outfile);
5891 fprintf(outfile,")\n");
5892 ast_print(tree->left,outfile,indent+2);
5893 ast_print(tree->right,outfile,indent+2);
5895 /*------------------------------------------------------------------*/
5896 /*----------------------------*/
5897 /* straight assignemnt */
5898 /*----------------------------*/
5900 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
5901 printTypeChain(tree->ftype,outfile);
5902 fprintf(outfile,")\n");
5903 ast_print(tree->left,outfile,indent+2);
5904 ast_print(tree->right,outfile,indent+2);
5906 /*------------------------------------------------------------------*/
5907 /*----------------------------*/
5908 /* comma operator */
5909 /*----------------------------*/
5911 fprintf(outfile,"COMMA(,) (%p) type (",tree);
5912 printTypeChain(tree->ftype,outfile);
5913 fprintf(outfile,")\n");
5914 ast_print(tree->left,outfile,indent+2);
5915 ast_print(tree->right,outfile,indent+2);
5917 /*------------------------------------------------------------------*/
5918 /*----------------------------*/
5920 /*----------------------------*/
5923 fprintf(outfile,"CALL (%p) type (",tree);
5924 printTypeChain(tree->ftype,outfile);
5925 fprintf(outfile,")\n");
5926 ast_print(tree->left,outfile,indent+2);
5927 ast_print(tree->right,outfile,indent+2);
5930 fprintf(outfile,"PARMS\n");
5931 ast_print(tree->left,outfile,indent+2);
5932 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
5933 ast_print(tree->right,outfile,indent+2);
5936 /*------------------------------------------------------------------*/
5937 /*----------------------------*/
5938 /* return statement */
5939 /*----------------------------*/
5941 fprintf(outfile,"RETURN (%p) type (",tree);
5943 printTypeChain(tree->right->ftype,outfile);
5945 fprintf(outfile,")\n");
5946 ast_print(tree->right,outfile,indent+2);
5948 /*------------------------------------------------------------------*/
5949 /*----------------------------*/
5950 /* label statement */
5951 /*----------------------------*/
5953 fprintf(outfile,"LABEL (%p)\n",tree);
5954 ast_print(tree->left,outfile,indent+2);
5955 ast_print(tree->right,outfile,indent);
5957 /*------------------------------------------------------------------*/
5958 /*----------------------------*/
5959 /* switch statement */
5960 /*----------------------------*/
5964 fprintf(outfile,"SWITCH (%p) ",tree);
5965 ast_print(tree->left,outfile,0);
5966 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
5967 INDENT(indent+2,outfile);
5968 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
5969 (int) floatFromVal(val),
5970 tree->values.switchVals.swNum,
5971 (int) floatFromVal(val));
5973 ast_print(tree->right,outfile,indent);
5976 /*------------------------------------------------------------------*/
5977 /*----------------------------*/
5979 /*----------------------------*/
5981 fprintf(outfile,"IF (%p) \n",tree);
5982 ast_print(tree->left,outfile,indent+2);
5983 if (tree->trueLabel) {
5984 INDENT(indent+2,outfile);
5985 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
5987 if (tree->falseLabel) {
5988 INDENT(indent+2,outfile);
5989 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
5991 ast_print(tree->right,outfile,indent+2);
5993 /*----------------------------*/
5994 /* goto Statement */
5995 /*----------------------------*/
5997 fprintf(outfile,"GOTO (%p) \n",tree);
5998 ast_print(tree->left,outfile,indent+2);
5999 fprintf(outfile,"\n");
6001 /*------------------------------------------------------------------*/
6002 /*----------------------------*/
6004 /*----------------------------*/
6006 fprintf(outfile,"FOR (%p) \n",tree);
6007 if (AST_FOR( tree, initExpr)) {
6008 INDENT(indent+2,outfile);
6009 fprintf(outfile,"INIT EXPR ");
6010 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6012 if (AST_FOR( tree, condExpr)) {
6013 INDENT(indent+2,outfile);
6014 fprintf(outfile,"COND EXPR ");
6015 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6017 if (AST_FOR( tree, loopExpr)) {
6018 INDENT(indent+2,outfile);
6019 fprintf(outfile,"LOOP EXPR ");
6020 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6022 fprintf(outfile,"FOR LOOP BODY \n");
6023 ast_print(tree->left,outfile,indent+2);
6026 fprintf(outfile,"CRITICAL (%p) \n",tree);
6027 ast_print(tree->left,outfile,indent+2);
6035 ast_print(t,stdout,0);
6040 /*-----------------------------------------------------------------*/
6041 /* astErrors : returns non-zero if errors present in tree */
6042 /*-----------------------------------------------------------------*/
6043 int astErrors(ast *t)
6052 if (t->type == EX_VALUE
6053 && t->opval.val->sym
6054 && t->opval.val->sym->undefined)
6057 errors += astErrors(t->left);
6058 errors += astErrors(t->right);