1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
25 #define DEBUG_CF(x) /* puts(x); */
31 set *operKeyReset = NULL;
32 ast *staticAutos = NULL;
35 #define LRVAL(x) x->left->rvalue
36 #define RRVAL(x) x->right->rvalue
37 #define TRVAL(x) x->rvalue
38 #define LLVAL(x) x->left->lvalue
39 #define RLVAL(x) x->right->lvalue
40 #define TLVAL(x) x->lvalue
41 #define RTYPE(x) x->right->ftype
42 #define RETYPE(x) x->right->etype
43 #define LTYPE(x) x->left->ftype
44 #define LETYPE(x) x->left->etype
45 #define TTYPE(x) x->ftype
46 #define TETYPE(x) x->etype
52 symbol *currFunc=NULL;
53 static ast *createIval (ast *, sym_link *, initList *, ast *, ast *);
54 static ast *createIvalCharPtr (ast *, sym_link *, ast *, ast *);
55 static ast *optimizeCompare (ast *);
56 ast *optimizeRRCRLC (ast *);
57 ast *optimizeSWAP (ast *);
58 ast *optimizeGetHbit (ast *, RESULT_TYPE);
59 ast *optimizeGetAbit (ast *, RESULT_TYPE);
60 ast *optimizeGetByte (ast *, RESULT_TYPE);
61 ast *optimizeGetWord (ast *, RESULT_TYPE);
62 static ast *backPatchLabels (ast *, symbol *, symbol *);
65 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
70 printTypeChain (tree->ftype, stdout);
75 /*-----------------------------------------------------------------*/
76 /* newAst - creates a fresh node for an expression tree */
77 /*-----------------------------------------------------------------*/
79 newAst_ (unsigned type)
82 static int oldLineno = 0;
84 ex = Safe_alloc ( sizeof (ast));
87 ex->lineno = (noLineno ? oldLineno : mylineno);
88 ex->filename = currFname;
89 ex->level = NestLevel;
90 ex->block = currBlockno;
91 ex->initMode = inInitMode;
92 ex->seqPoint = seqPointNo;
97 newAst_VALUE (value * val)
99 ast *ex = newAst_ (EX_VALUE);
105 newAst_OP (unsigned op)
107 ast *ex = newAst_ (EX_OP);
113 newAst_LINK (sym_link * val)
115 ast *ex = newAst_ (EX_LINK);
120 /*-----------------------------------------------------------------*/
121 /* newNode - creates a new node */
122 /*-----------------------------------------------------------------*/
124 newNode (long op, ast * left, ast * right)
135 /*-----------------------------------------------------------------*/
136 /* newIfxNode - creates a new Ifx Node */
137 /*-----------------------------------------------------------------*/
139 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
143 /* if this is a literal then we already know the result */
144 if (condAst->etype && IS_LITERAL (condAst->etype))
146 /* then depending on the expression value */
147 if (floatFromVal (condAst->opval.val))
148 ifxNode = newNode (GOTO,
149 newAst_VALUE (symbolVal (trueLabel)),
152 ifxNode = newNode (GOTO,
153 newAst_VALUE (symbolVal (falseLabel)),
158 ifxNode = newNode (IFX, condAst, NULL);
159 ifxNode->trueLabel = trueLabel;
160 ifxNode->falseLabel = falseLabel;
166 /*-----------------------------------------------------------------*/
167 /* copyAstValues - copies value portion of ast if needed */
168 /*-----------------------------------------------------------------*/
170 copyAstValues (ast * dest, ast * src)
172 switch (src->opval.op)
175 dest->values.sym = copySymbolChain (src->values.sym);
179 dest->values.switchVals.swVals =
180 copyValue (src->values.switchVals.swVals);
181 dest->values.switchVals.swDefault =
182 src->values.switchVals.swDefault;
183 dest->values.switchVals.swNum =
184 src->values.switchVals.swNum;
188 dest->values.inlineasm = Safe_strdup(src->values.inlineasm);
192 dest->values.constlist = copyLiteralList(src->values.constlist);
196 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
197 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
198 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
199 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
200 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
201 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
202 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
207 /*-----------------------------------------------------------------*/
208 /* copyAst - makes a copy of a given astession */
209 /*-----------------------------------------------------------------*/
218 dest = Safe_alloc ( sizeof (ast));
220 dest->type = src->type;
221 dest->lineno = src->lineno;
222 dest->level = src->level;
223 dest->funcName = src->funcName;
224 dest->reversed = src->reversed;
227 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
229 /* if this is a leaf */
231 if (src->type == EX_VALUE)
233 dest->opval.val = copyValue (src->opval.val);
238 if (src->type == EX_LINK)
240 dest->opval.lnk = copyLinkChain (src->opval.lnk);
244 dest->opval.op = src->opval.op;
246 /* if this is a node that has special values */
247 copyAstValues (dest, src);
249 dest->trueLabel = copySymbol (src->trueLabel);
250 dest->falseLabel = copySymbol (src->falseLabel);
251 dest->left = copyAst (src->left);
252 dest->right = copyAst (src->right);
258 /*-----------------------------------------------------------------*/
259 /* removeIncDecOps: remove for side effects in *_ASSIGN's */
260 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
261 /*-----------------------------------------------------------------*/
262 ast *removeIncDecOps (ast * tree) {
264 // traverse the tree and remove inc/dec ops
269 if (tree->type == EX_OP &&
270 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
277 tree->left=removeIncDecOps(tree->left);
278 tree->right=removeIncDecOps(tree->right);
283 /*-----------------------------------------------------------------*/
284 /* removePreIncDecOps: remove for side effects in *_ASSIGN's */
285 /* "*++s += 3" -> "*++s = *++s + 3" */
286 /*-----------------------------------------------------------------*/
287 ast *removePreIncDecOps (ast * tree) {
289 // traverse the tree and remove pre-inc/dec ops
294 if (tree->type == EX_OP &&
295 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
300 tree->left=removePreIncDecOps(tree->left);
301 tree->right=removePreIncDecOps(tree->right);
306 /*-----------------------------------------------------------------*/
307 /* removePostIncDecOps: remove for side effects in *_ASSIGN's */
308 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
309 /*-----------------------------------------------------------------*/
310 ast *removePostIncDecOps (ast * tree) {
312 // traverse the tree and remove pre-inc/dec ops
317 if (tree->type == EX_OP &&
318 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
323 tree->left=removePostIncDecOps(tree->left);
324 tree->right=removePostIncDecOps(tree->right);
329 /*-----------------------------------------------------------------*/
330 /* hasSEFcalls - returns TRUE if tree has a function call */
331 /*-----------------------------------------------------------------*/
333 hasSEFcalls (ast * tree)
338 if (tree->type == EX_OP &&
339 (tree->opval.op == CALL ||
340 tree->opval.op == PCALL ||
341 tree->opval.op == '=' ||
342 tree->opval.op == INC_OP ||
343 tree->opval.op == DEC_OP))
346 return (hasSEFcalls (tree->left) |
347 hasSEFcalls (tree->right));
350 /*-----------------------------------------------------------------*/
351 /* isAstEqual - compares two asts & returns 1 if they are equal */
352 /*-----------------------------------------------------------------*/
354 isAstEqual (ast * t1, ast * t2)
363 if (t1->type != t2->type)
369 if (t1->opval.op != t2->opval.op)
371 return (isAstEqual (t1->left, t2->left) &&
372 isAstEqual (t1->right, t2->right));
376 if (t1->opval.val->sym)
378 if (!t2->opval.val->sym)
381 return isSymbolEqual (t1->opval.val->sym,
386 if (t2->opval.val->sym)
389 return (floatFromVal (t1->opval.val) ==
390 floatFromVal (t2->opval.val));
394 /* only compare these two types */
402 /*-----------------------------------------------------------------*/
403 /* resolveSymbols - resolve symbols from the symbol table */
404 /*-----------------------------------------------------------------*/
406 resolveSymbols (ast * tree)
408 /* walk the entire tree and check for values */
409 /* with symbols if we find one then replace */
410 /* symbol with that from the symbol table */
417 /* if not block & function */
418 if (tree->type == EX_OP &&
419 (tree->opval.op != FUNCTION &&
420 tree->opval.op != BLOCK &&
421 tree->opval.op != NULLOP))
423 filename = tree->filename;
424 lineno = tree->lineno;
428 /* make sure we resolve the true & false labels for ifx */
429 if (tree->type == EX_OP && tree->opval.op == IFX)
435 if ((csym = findSym (LabelTab, tree->trueLabel,
436 tree->trueLabel->name)))
437 tree->trueLabel = csym;
439 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
440 tree->trueLabel->name);
443 if (tree->falseLabel)
445 if ((csym = findSym (LabelTab,
447 tree->falseLabel->name)))
448 tree->falseLabel = csym;
450 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
451 tree->falseLabel->name);
456 /* if this is a label resolve it from the labelTab */
457 if (IS_AST_VALUE (tree) &&
458 tree->opval.val->sym &&
459 tree->opval.val->sym->islbl)
462 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
463 tree->opval.val->sym->name);
466 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
467 tree->opval.val->sym->name);
469 tree->opval.val->sym = csym;
471 goto resolveChildren;
474 /* do only for leafs */
475 if (IS_AST_VALUE (tree) &&
476 tree->opval.val->sym &&
477 !tree->opval.val->sym->implicit)
480 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
482 /* if found in the symbol table & they r not the same */
483 if (csym && tree->opval.val->sym != csym)
485 tree->opval.val->sym = csym;
486 tree->opval.val->type = csym->type;
487 tree->opval.val->etype = csym->etype;
490 /* if not found in the symbol table */
491 /* mark it as undefined assume it is */
492 /* an integer in data space */
493 if (!csym && !tree->opval.val->sym->implicit)
496 /* if this is a function name then */
497 /* mark it as returning an int */
500 tree->opval.val->sym->type = newLink (DECLARATOR);
501 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
502 tree->opval.val->sym->type->next =
503 tree->opval.val->sym->etype = newIntLink ();
504 tree->opval.val->etype = tree->opval.val->etype;
505 tree->opval.val->type = tree->opval.val->sym->type;
506 werrorfl (tree->filename, tree->lineno, W_IMPLICIT_FUNC,
507 tree->opval.val->sym->name);
508 //tree->opval.val->sym->undefined = 1;
509 allocVariables (tree->opval.val->sym);
513 tree->opval.val->sym->undefined = 1;
514 tree->opval.val->type =
515 tree->opval.val->etype = newIntLink ();
516 tree->opval.val->sym->type =
517 tree->opval.val->sym->etype = newIntLink ();
523 resolveSymbols (tree->left);
524 resolveSymbols (tree->right);
529 /*-----------------------------------------------------------------*/
530 /* setAstLineno - walks a ast tree & sets the line number */
531 /*-----------------------------------------------------------------*/
532 int setAstLineno (ast * tree, int lineno)
537 tree->lineno = lineno;
538 setAstLineno (tree->left, lineno);
539 setAstLineno (tree->right, lineno);
543 /*-----------------------------------------------------------------*/
544 /* funcOfType :- function of type with name */
545 /*-----------------------------------------------------------------*/
547 funcOfType (char *name, sym_link * type, sym_link * argType,
551 /* create the symbol */
552 sym = newSymbol (name, 0);
554 /* setup return value */
555 sym->type = newLink (DECLARATOR);
556 DCL_TYPE (sym->type) = FUNCTION;
557 sym->type->next = copyLinkChain (type);
558 sym->etype = getSpec (sym->type);
559 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
561 /* if arguments required */
565 args = FUNC_ARGS(sym->type) = newValue ();
569 args->type = copyLinkChain (argType);
570 args->etype = getSpec (args->type);
571 SPEC_EXTR(args->etype)=1;
574 args = args->next = newValue ();
581 allocVariables (sym);
586 /*-----------------------------------------------------------------*/
587 /* funcOfTypeVarg :- function of type with name and argtype */
588 /*-----------------------------------------------------------------*/
590 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
595 /* create the symbol */
596 sym = newSymbol (name, 0);
598 /* setup return value */
599 sym->type = newLink (DECLARATOR);
600 DCL_TYPE (sym->type) = FUNCTION;
601 sym->type->next = typeFromStr(rtype);
602 sym->etype = getSpec (sym->type);
604 /* if arguments required */
607 args = FUNC_ARGS(sym->type) = newValue ();
609 for ( i = 0 ; i < nArgs ; i++ ) {
610 args->type = typeFromStr(atypes[i]);
611 args->etype = getSpec (args->type);
612 SPEC_EXTR(args->etype)=1;
613 if ((i + 1) == nArgs) break;
614 args = args->next = newValue ();
621 allocVariables (sym);
626 /*-----------------------------------------------------------------*/
627 /* reverseParms - will reverse a parameter tree */
628 /*-----------------------------------------------------------------*/
630 reverseParms (ast * ptree)
636 /* top down if we find a nonParm tree then quit */
637 if (ptree->type == EX_OP && ptree->opval.op == PARAM && !ptree->reversed)
639 /* The various functions expect the parameter tree to be right heavy. */
640 /* Rotate the tree to be left heavy so that after reversal it is */
641 /* right heavy again. */
642 while ((ttree = ptree->right) && ttree->type == EX_OP &&
643 ttree->opval.op == PARAM)
645 ptree->right = ttree->right;
646 ttree->right = ttree->left;
647 ttree->left = ptree->left;
653 ptree->left = ptree->right;
654 ptree->right = ttree;
656 reverseParms (ptree->left);
657 reverseParms (ptree->right);
663 /*-----------------------------------------------------------------*/
664 /* processParms - makes sure the parameters are okay and do some */
665 /* processing with them */
666 /*-----------------------------------------------------------------*/
668 processParms (ast *func,
671 int *parmNumber, /* unused, although updated */
674 RESULT_TYPE resultType;
677 /* if none of them exist */
678 if (!defParm && !*actParm)
683 if (getenv("DEBUG_SANITY"))
685 fprintf (stderr, "processParms: %s ", defParm->name);
687 /* make sure the type is complete and sane */
688 checkTypeSanity(defParm->etype, defParm->name);
691 if (IS_CODEPTR (func->ftype))
692 functype = func->ftype->next;
694 functype = func->ftype;
696 /* if the function is being called via a pointer & */
697 /* it has not been defined a reentrant then we cannot */
698 /* have parameters */
699 /* PIC16 port can... */
700 if (!TARGET_IS_PIC16)
702 if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
704 werror (W_NONRENT_ARGS);
710 /* if defined parameters ended but actual parameters */
711 /* exist and this is not defined as a variable arg */
712 if (!defParm && *actParm && !IFFUNC_HASVARARGS(functype))
714 werror (E_TOO_MANY_PARMS);
718 /* if defined parameters present but no actual parameters */
719 if (defParm && !*actParm)
721 werror (E_TOO_FEW_PARMS);
725 /* if this is a PARAM node then match left & right */
726 if ((*actParm)->type == EX_OP && (*actParm)->opval.op == PARAM)
728 (*actParm)->decorated = 1;
729 return (processParms (func, defParm,
730 &(*actParm)->left, parmNumber, FALSE) ||
731 processParms (func, defParm ? defParm->next : NULL,
732 &(*actParm)->right, parmNumber, rightmost));
734 else if (defParm) /* not vararg */
736 /* If we have found a value node by following only right-hand links,
737 * then we know that there are no more values after us.
739 * Therefore, if there are more defined parameters, the caller didn't
742 if (rightmost && defParm->next)
744 werror (E_TOO_FEW_PARMS);
749 /* decorate parameter */
750 resultType = defParm ? getResultTypeFromType (defParm->etype) :
752 *actParm = decorateType (*actParm, resultType);
754 if (IS_VOID((*actParm)->ftype))
756 werror (E_VOID_VALUE_USED);
760 /* If this is a varargs function... */
761 if (!defParm && *actParm && IFFUNC_HASVARARGS(functype))
766 if (IS_CAST_OP (*actParm)
767 || (IS_AST_LIT_VALUE (*actParm) && (*actParm)->values.literalFromCast))
769 /* Parameter was explicitly typecast; don't touch it. */
773 ftype = (*actParm)->ftype;
775 /* If it's a char, upcast to int. */
776 if (IS_INTEGRAL (ftype)
777 && (getSize (ftype) < (unsigned) INTSIZE))
779 newType = newAst_LINK(INTTYPE);
782 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
784 newType = newAst_LINK (copyLinkChain(ftype));
785 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
788 if (IS_AGGREGATE (ftype))
790 newType = newAst_LINK (copyLinkChain (ftype));
791 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
796 /* cast required; change this op to a cast. */
797 (*actParm)->decorated = 0;
798 *actParm = newNode (CAST, newType, *actParm);
799 (*actParm)->lineno = (*actParm)->right->lineno;
801 decorateType (*actParm, RESULT_TYPE_NONE);
806 /* if defined parameters ended but actual has not & */
808 if (!defParm && *actParm &&
809 (options.stackAuto || IFFUNC_ISREENT (functype)))
812 resolveSymbols (*actParm);
814 /* the parameter type must be at least castable */
815 if (compareType (defParm->type, (*actParm)->ftype) == 0)
817 werror (E_INCOMPAT_TYPES);
818 printFromToType ((*actParm)->ftype, defParm->type);
822 /* if the parameter is castable then add the cast */
823 if (compareType (defParm->type, (*actParm)->ftype) < 0)
827 resultType = getResultTypeFromType (defParm->etype);
828 pTree = resolveSymbols (copyAst (*actParm));
830 /* now change the current one to a cast */
831 (*actParm)->type = EX_OP;
832 (*actParm)->opval.op = CAST;
833 (*actParm)->left = newAst_LINK (defParm->type);
834 (*actParm)->right = pTree;
835 (*actParm)->decorated = 0; /* force typechecking */
836 decorateType (*actParm, resultType);
839 /* make a copy and change the regparm type to the defined parm */
840 (*actParm)->etype = getSpec ((*actParm)->ftype = copyLinkChain ((*actParm)->ftype));
841 SPEC_REGPARM ((*actParm)->etype) = SPEC_REGPARM (defParm->etype);
842 SPEC_ARGREG ((*actParm)->etype) = SPEC_ARGREG (defParm->etype);
847 /*-----------------------------------------------------------------*/
848 /* createIvalType - generates ival for basic types */
849 /*-----------------------------------------------------------------*/
851 createIvalType (ast * sym, sym_link * type, initList * ilist)
855 /* if initList is deep */
856 if (ilist->type == INIT_DEEP)
857 ilist = ilist->init.deep;
859 iExpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
860 return decorateType (newNode ('=', sym, iExpr), RESULT_TYPE_NONE);
863 /*-----------------------------------------------------------------*/
864 /* createIvalStruct - generates initial value for structures */
865 /*-----------------------------------------------------------------*/
867 createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
874 sflds = SPEC_STRUCT (type)->fields;
875 if (ilist->type != INIT_DEEP)
877 werror (E_INIT_STRUCT, "");
881 iloop = ilist->init.deep;
883 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
885 /* if we have come to end */
889 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
890 lAst = decorateType (resolveSymbols (lAst), RESULT_TYPE_NONE);
891 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type,
892 iloop, rast, rootValue)),
898 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
899 W_EXCESS_INITIALIZERS, "struct",
900 sym->opval.val->sym->name);
907 /*-----------------------------------------------------------------*/
908 /* createIvalArray - generates code for array initialization */
909 /*-----------------------------------------------------------------*/
911 createIvalArray (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
915 int lcnt = 0, size = 0;
916 literalList *literalL;
918 /* take care of the special case */
919 /* array of characters can be init */
921 if (IS_CHAR (type->next))
922 if ((rast = createIvalCharPtr (sym,
924 decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE),
927 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
929 /* not the special case */
930 if (ilist->type != INIT_DEEP)
932 werror (E_INIT_STRUCT, "");
936 iloop = ilist->init.deep;
937 lcnt = DCL_ELEM (type);
939 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
943 aSym = decorateType (resolveSymbols(sym), RESULT_TYPE_NONE);
945 rast = newNode(ARRAYINIT, aSym, NULL);
946 rast->values.constlist = literalL;
948 // Make sure size is set to length of initializer list.
955 if (lcnt && size > lcnt)
957 // Array size was specified, and we have more initializers than needed.
958 char *name=sym->opval.val->sym->name;
959 int lineno=sym->opval.val->sym->lineDef;
960 char *filename=sym->opval.val->sym->fileDef;
962 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
971 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
972 aSym = decorateType (resolveSymbols (aSym), RESULT_TYPE_NONE);
973 rast = createIval (aSym, type->next, iloop, rast, rootValue);
974 iloop = (iloop ? iloop->next : NULL);
980 /* no of elements given and we */
981 /* have generated for all of them */
984 // is this a better way? at least it won't crash
985 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
986 int lineno = iloop->lineno;
987 char *filename = iloop->filename;
988 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
995 /* if we have not been given a size */
996 if (!DCL_ELEM (type))
998 /* check, if it's a flexible array */
999 if (IS_STRUCT (AST_VALUE (rootValue)->type))
1000 AST_SYMBOL(rootValue)->flexArrayLength = size * getSize (type->next);
1002 DCL_ELEM (type) = size;
1005 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1009 /*-----------------------------------------------------------------*/
1010 /* createIvalCharPtr - generates initial values for char pointers */
1011 /*-----------------------------------------------------------------*/
1013 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr, ast *rootVal)
1018 /* if this is a pointer & right is a literal array then */
1019 /* just assignment will do */
1020 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
1021 SPEC_SCLS (iexpr->etype) == S_CODE)
1022 && IS_ARRAY (iexpr->ftype)))
1023 return newNode ('=', sym, iexpr);
1025 /* left side is an array so we have to assign each */
1027 if ((IS_LITERAL (iexpr->etype) ||
1028 SPEC_SCLS (iexpr->etype) == S_CODE)
1029 && IS_ARRAY (iexpr->ftype))
1031 /* for each character generate an assignment */
1032 /* to the array element */
1033 char *s = SPEC_CVAL (iexpr->etype).v_char;
1035 unsigned int symsize = getSize (type);
1037 size = getSize (iexpr->ftype);
1038 if (symsize && size>symsize)
1040 if (size>(symsize+1))
1042 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
1044 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1050 for (i=0;i<size;i++)
1052 rast = newNode (NULLOP,
1056 newAst_VALUE (valueFromLit ((float) i))),
1057 newAst_VALUE (valueFromLit (*s))));
1061 // now WE don't need iexpr's symbol anymore
1062 freeStringSymbol(AST_SYMBOL(iexpr));
1064 /* if we have not been given a size */
1065 if (!DCL_ELEM (type))
1067 /* check, if it's a flexible array */
1068 if (IS_STRUCT (AST_VALUE (rootVal)->type))
1069 AST_SYMBOL(rootVal)->flexArrayLength = size * getSize (type->next);
1071 DCL_ELEM (type) = size;
1074 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1080 /*-----------------------------------------------------------------*/
1081 /* createIvalPtr - generates initial value for pointers */
1082 /*-----------------------------------------------------------------*/
1084 createIvalPtr (ast * sym, sym_link * type, initList * ilist, ast *rootVal)
1090 if (ilist->type == INIT_DEEP)
1091 ilist = ilist->init.deep;
1093 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
1095 /* if character pointer */
1096 if (IS_CHAR (type->next))
1097 if ((rast = createIvalCharPtr (sym, type, iexpr, rootVal)))
1100 return newNode ('=', sym, iexpr);
1103 /*-----------------------------------------------------------------*/
1104 /* createIval - generates code for initial value */
1105 /*-----------------------------------------------------------------*/
1107 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid, ast *rootValue)
1114 /* if structure then */
1115 if (IS_STRUCT (type))
1116 rast = createIvalStruct (sym, type, ilist, rootValue);
1118 /* if this is a pointer */
1120 rast = createIvalPtr (sym, type, ilist, rootValue);
1122 /* if this is an array */
1123 if (IS_ARRAY (type))
1124 rast = createIvalArray (sym, type, ilist, rootValue);
1126 /* if type is SPECIFIER */
1128 rast = createIvalType (sym, type, ilist);
1131 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_TYPE_NONE);
1133 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1136 /*-----------------------------------------------------------------*/
1137 /* initAggregates - initialises aggregate variables with initv */
1138 /*-----------------------------------------------------------------*/
1139 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1140 ast *newAst = newAst_VALUE (symbolVal (sym));
1141 return createIval (newAst, sym->type, ival, wid, newAst);
1144 /*-----------------------------------------------------------------*/
1145 /* gatherAutoInit - creates assignment expressions for initial */
1147 /*-----------------------------------------------------------------*/
1149 gatherAutoInit (symbol * autoChain)
1156 for (sym = autoChain; sym; sym = sym->next)
1159 /* resolve the symbols in the ival */
1161 resolveIvalSym (sym->ival, sym->type);
1164 /* if we are PIC16 port,
1165 * and this is a static,
1166 * and have initial value,
1167 * and not S_CODE, don't emit in gs segment,
1168 * but allow glue.c:pic16emitRegularMap to put symbol
1169 * in idata section */
1170 if(TARGET_IS_PIC16 &&
1171 IS_STATIC (sym->etype) && sym->ival
1172 && SPEC_SCLS(sym->etype) != S_CODE) {
1173 SPEC_SCLS (sym->etype) = S_DATA;
1178 /* if this is a static variable & has an */
1179 /* initial value the code needs to be lifted */
1180 /* here to the main portion since they can be */
1181 /* initialised only once at the start */
1182 if (IS_STATIC (sym->etype) && sym->ival &&
1183 SPEC_SCLS (sym->etype) != S_CODE)
1187 /* insert the symbol into the symbol table */
1188 /* with level = 0 & name = rname */
1189 newSym = copySymbol (sym);
1190 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1192 /* now lift the code to main */
1193 if (IS_AGGREGATE (sym->type)) {
1194 work = initAggregates (sym, sym->ival, NULL);
1196 if (getNelements(sym->type, sym->ival)>1) {
1197 werrorfl (sym->fileDef, sym->lineDef,
1198 W_EXCESS_INITIALIZERS, "scalar",
1201 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1202 list2expr (sym->ival));
1205 setAstLineno (work, sym->lineDef);
1209 staticAutos = newNode (NULLOP, staticAutos, work);
1216 /* if there is an initial value */
1217 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1219 initList *ilist=sym->ival;
1221 while (ilist->type == INIT_DEEP) {
1222 ilist = ilist->init.deep;
1225 /* update lineno for error msg */
1226 lineno=sym->lineDef;
1227 setAstLineno (ilist->init.node, lineno);
1229 if (IS_AGGREGATE (sym->type)) {
1230 work = initAggregates (sym, sym->ival, NULL);
1232 if (getNelements(sym->type, sym->ival)>1) {
1233 werrorfl (sym->fileDef, sym->lineDef,
1234 W_EXCESS_INITIALIZERS, "scalar",
1237 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1238 list2expr (sym->ival));
1242 setAstLineno (work, sym->lineDef);
1246 init = newNode (NULLOP, init, work);
1255 /*-----------------------------------------------------------------*/
1256 /* freeStringSymbol - delete a literal string if no more usage */
1257 /*-----------------------------------------------------------------*/
1258 void freeStringSymbol(symbol *sym) {
1259 /* make sure this is a literal string */
1260 assert (sym->isstrlit);
1261 if (--sym->isstrlit == 0) { // lower the usage count
1262 memmap *segment=SPEC_OCLS(sym->etype);
1264 deleteSetItem(&segment->syms, sym);
1269 /*-----------------------------------------------------------------*/
1270 /* stringToSymbol - creates a symbol from a literal string */
1271 /*-----------------------------------------------------------------*/
1273 stringToSymbol (value * val)
1275 char name[SDCC_NAME_MAX + 1];
1276 static int charLbl = 0;
1281 // have we heard this before?
1282 for (sp=statsg->syms; sp; sp=sp->next) {
1284 size = getSize (sym->type);
1285 if (sym->isstrlit && size == getSize (val->type) &&
1286 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1287 // yes, this is old news. Don't publish it again.
1288 sym->isstrlit++; // but raise the usage count
1289 return symbolVal(sym);
1293 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1294 sym = newSymbol (name, 0); /* make it @ level 0 */
1295 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1297 /* copy the type from the value passed */
1298 sym->type = copyLinkChain (val->type);
1299 sym->etype = getSpec (sym->type);
1300 /* change to storage class & output class */
1301 SPEC_SCLS (sym->etype) = S_CODE;
1302 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1303 SPEC_STAT (sym->etype) = 1;
1304 /* make the level & block = 0 */
1305 sym->block = sym->level = 0;
1307 /* create an ival */
1308 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1313 allocVariables (sym);
1316 return symbolVal (sym);
1320 /*-----------------------------------------------------------------*/
1321 /* processBlockVars - will go thru the ast looking for block if */
1322 /* a block is found then will allocate the syms */
1323 /* will also gather the auto inits present */
1324 /*-----------------------------------------------------------------*/
1326 processBlockVars (ast * tree, int *stack, int action)
1331 /* if this is a block */
1332 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1336 if (action == ALLOCATE)
1338 *stack += allocVariables (tree->values.sym);
1339 autoInit = gatherAutoInit (tree->values.sym);
1341 /* if there are auto inits then do them */
1343 tree->left = newNode (NULLOP, autoInit, tree->left);
1345 else /* action is deallocate */
1346 deallocLocal (tree->values.sym);
1349 processBlockVars (tree->left, stack, action);
1350 processBlockVars (tree->right, stack, action);
1355 /*-------------------------------------------------------------*/
1356 /* constExprTree - returns TRUE if this tree is a constant */
1358 /*-------------------------------------------------------------*/
1359 bool constExprTree (ast *cexpr) {
1365 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1367 switch (cexpr->type)
1370 if (IS_AST_LIT_VALUE(cexpr)) {
1371 // this is a literal
1374 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1375 // a function's address will never change
1378 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1379 // an array's address will never change
1382 if (IS_AST_SYM_VALUE(cexpr) &&
1383 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1384 // a symbol in code space will never change
1385 // This is only for the 'char *s="hallo"' case and will have to leave
1386 //printf(" code space symbol");
1391 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1392 "unexpected link in expression tree\n");
1395 if (cexpr->opval.op==ARRAYINIT) {
1396 // this is a list of literals
1399 if (cexpr->opval.op=='=') {
1400 return constExprTree(cexpr->right);
1402 if (cexpr->opval.op==CAST) {
1403 // cast ignored, maybe we should throw a warning here?
1404 return constExprTree(cexpr->right);
1406 if (cexpr->opval.op=='&') {
1409 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1412 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1417 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1422 /*-----------------------------------------------------------------*/
1423 /* constExprValue - returns the value of a constant expression */
1424 /* or NULL if it is not a constant expression */
1425 /*-----------------------------------------------------------------*/
1427 constExprValue (ast * cexpr, int check)
1429 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1431 /* if this is not a constant then */
1432 if (!IS_LITERAL (cexpr->ftype))
1434 /* then check if this is a literal array
1436 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1437 SPEC_CVAL (cexpr->etype).v_char &&
1438 IS_ARRAY (cexpr->ftype))
1440 value *val = valFromType (cexpr->ftype);
1441 SPEC_SCLS (val->etype) = S_LITERAL;
1442 val->sym = cexpr->opval.val->sym;
1443 val->sym->type = copyLinkChain (cexpr->ftype);
1444 val->sym->etype = getSpec (val->sym->type);
1445 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1449 /* if we are casting a literal value then */
1450 if (IS_AST_OP (cexpr) &&
1451 cexpr->opval.op == CAST &&
1452 IS_LITERAL (cexpr->right->ftype))
1454 return valCastLiteral (cexpr->ftype,
1455 floatFromVal (cexpr->right->opval.val));
1458 if (IS_AST_VALUE (cexpr))
1460 return cexpr->opval.val;
1464 werror (E_CONST_EXPECTED, "found expression");
1469 /* return the value */
1470 if (IS_AST_VALUE (cexpr))
1472 return cexpr->opval.val;
1477 /*-----------------------------------------------------------------*/
1478 /* isLabelInAst - will return true if a given label is found */
1479 /*-----------------------------------------------------------------*/
1481 isLabelInAst (symbol * label, ast * tree)
1483 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1486 if (IS_AST_OP (tree) &&
1487 tree->opval.op == LABEL &&
1488 isSymbolEqual (AST_SYMBOL (tree->left), label))
1491 return isLabelInAst (label, tree->right) &&
1492 isLabelInAst (label, tree->left);
1496 /*-----------------------------------------------------------------*/
1497 /* isLoopCountable - return true if the loop count can be determi- */
1498 /* -ned at compile time . */
1499 /*-----------------------------------------------------------------*/
1501 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1502 symbol ** sym, ast ** init, ast ** end)
1505 /* the loop is considered countable if the following
1506 conditions are true :-
1508 a) initExpr :- <sym> = <const>
1509 b) condExpr :- <sym> < <const1>
1510 c) loopExpr :- <sym> ++
1513 /* first check the initExpr */
1514 if (IS_AST_OP (initExpr) &&
1515 initExpr->opval.op == '=' && /* is assignment */
1516 IS_AST_SYM_VALUE (initExpr->left))
1517 { /* left is a symbol */
1519 *sym = AST_SYMBOL (initExpr->left);
1520 *init = initExpr->right;
1525 /* don't reverse loop with volatile counter */
1526 if (IS_VOLATILE ((*sym)->type))
1529 /* for now the symbol has to be of
1531 if (!IS_INTEGRAL ((*sym)->type))
1534 /* now check condExpr */
1535 if (IS_AST_OP (condExpr))
1538 switch (condExpr->opval.op)
1541 if (IS_AST_SYM_VALUE (condExpr->left) &&
1542 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1543 IS_AST_LIT_VALUE (condExpr->right))
1545 *end = condExpr->right;
1551 if (IS_AST_OP (condExpr->left) &&
1552 condExpr->left->opval.op == '>' &&
1553 IS_AST_LIT_VALUE (condExpr->left->right) &&
1554 IS_AST_SYM_VALUE (condExpr->left->left) &&
1555 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1558 *end = newNode ('+', condExpr->left->right,
1559 newAst_VALUE (constVal ("1")));
1572 /* check loop expression is of the form <sym>++ */
1573 if (!IS_AST_OP (loopExpr))
1576 /* check if <sym> ++ */
1577 if (loopExpr->opval.op == INC_OP)
1583 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1584 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1591 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1592 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1600 if (loopExpr->opval.op == ADD_ASSIGN)
1603 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1604 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1605 IS_AST_LIT_VALUE (loopExpr->right) &&
1606 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1614 /*-----------------------------------------------------------------*/
1615 /* astHasVolatile - returns true if ast contains any volatile */
1616 /*-----------------------------------------------------------------*/
1618 astHasVolatile (ast * tree)
1623 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1626 if (IS_AST_OP (tree))
1627 return astHasVolatile (tree->left) ||
1628 astHasVolatile (tree->right);
1633 /*-----------------------------------------------------------------*/
1634 /* astHasPointer - return true if the ast contains any ptr variable */
1635 /*-----------------------------------------------------------------*/
1637 astHasPointer (ast * tree)
1642 if (IS_AST_LINK (tree))
1645 /* if we hit an array expression then check
1646 only the left side */
1647 if (IS_AST_OP (tree) && tree->opval.op == '[')
1648 return astHasPointer (tree->left);
1650 if (IS_AST_VALUE (tree))
1651 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1653 return astHasPointer (tree->left) ||
1654 astHasPointer (tree->right);
1658 /*-----------------------------------------------------------------*/
1659 /* astHasSymbol - return true if the ast has the given symbol */
1660 /*-----------------------------------------------------------------*/
1662 astHasSymbol (ast * tree, symbol * sym)
1664 if (!tree || IS_AST_LINK (tree))
1667 if (IS_AST_VALUE (tree))
1669 if (IS_AST_SYM_VALUE (tree))
1670 return isSymbolEqual (AST_SYMBOL (tree), sym);
1675 return astHasSymbol (tree->left, sym) ||
1676 astHasSymbol (tree->right, sym);
1679 /*-----------------------------------------------------------------*/
1680 /* astHasDeref - return true if the ast has an indirect access */
1681 /*-----------------------------------------------------------------*/
1683 astHasDeref (ast * tree)
1685 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1688 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1690 return astHasDeref (tree->left) || astHasDeref (tree->right);
1693 /*-----------------------------------------------------------------*/
1694 /* isConformingBody - the loop body has to conform to a set of rules */
1695 /* for the loop to be considered reversible read on for rules */
1696 /*-----------------------------------------------------------------*/
1698 isConformingBody (ast * pbody, symbol * sym, ast * body)
1701 /* we are going to do a pre-order traversal of the
1702 tree && check for the following conditions. (essentially
1703 a set of very shallow tests )
1704 a) the sym passed does not participate in
1705 any arithmetic operation
1706 b) There are no function calls
1707 c) all jumps are within the body
1708 d) address of loop control variable not taken
1709 e) if an assignment has a pointer on the
1710 left hand side make sure right does not have
1711 loop control variable */
1713 /* if we reach the end or a leaf then true */
1714 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1717 /* if anything else is "volatile" */
1718 if (IS_VOLATILE (TETYPE (pbody)))
1721 /* we will walk the body in a pre-order traversal for
1723 switch (pbody->opval.op)
1725 /*------------------------------------------------------------------*/
1727 // if the loopvar is used as an index
1728 /* array op is commutative -- must check both left & right */
1729 if (astHasSymbol(pbody->right, sym) || astHasSymbol(pbody->left, sym)) {
1732 return isConformingBody (pbody->right, sym, body)
1733 && isConformingBody (pbody->left, sym, body);
1735 /*------------------------------------------------------------------*/
1740 /*------------------------------------------------------------------*/
1744 /* sure we are not sym is not modified */
1746 IS_AST_SYM_VALUE (pbody->left) &&
1747 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1751 IS_AST_SYM_VALUE (pbody->right) &&
1752 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1757 /*------------------------------------------------------------------*/
1759 case '*': /* can be unary : if right is null then unary operation */
1764 /* if right is NULL then unary operation */
1765 /*------------------------------------------------------------------*/
1766 /*----------------------------*/
1768 /*----------------------------*/
1771 if (IS_AST_SYM_VALUE (pbody->left) &&
1772 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1775 return isConformingBody (pbody->left, sym, body);
1779 if (astHasSymbol (pbody->left, sym) ||
1780 astHasSymbol (pbody->right, sym))
1785 /*------------------------------------------------------------------*/
1796 if (IS_AST_SYM_VALUE (pbody->left) &&
1797 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1800 if (IS_AST_SYM_VALUE (pbody->right) &&
1801 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1804 return isConformingBody (pbody->left, sym, body) &&
1805 isConformingBody (pbody->right, sym, body);
1813 if (IS_AST_SYM_VALUE (pbody->left) &&
1814 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1816 return isConformingBody (pbody->left, sym, body);
1818 /*------------------------------------------------------------------*/
1830 case SIZEOF: /* evaluate wihout code generation */
1832 if (IS_AST_SYM_VALUE (pbody->left) &&
1833 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1836 if (IS_AST_SYM_VALUE (pbody->right) &&
1837 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1840 return isConformingBody (pbody->left, sym, body) &&
1841 isConformingBody (pbody->right, sym, body);
1843 /*------------------------------------------------------------------*/
1846 /* if left has a pointer & right has loop
1847 control variable then we cannot */
1848 if (astHasPointer (pbody->left) &&
1849 astHasSymbol (pbody->right, sym))
1851 if (astHasVolatile (pbody->left))
1854 if (IS_AST_SYM_VALUE (pbody->left)) {
1855 // if the loopvar has an assignment
1856 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1858 // if the loopvar is used in another (maybe conditional) block
1859 if (astHasSymbol (pbody->right, sym) &&
1860 (pbody->level >= body->level)) {
1865 if (astHasVolatile (pbody->left))
1868 if (astHasDeref(pbody->right)) return FALSE;
1870 return isConformingBody (pbody->left, sym, body) &&
1871 isConformingBody (pbody->right, sym, body);
1882 assert ("Parser should not have generated this\n");
1884 /*------------------------------------------------------------------*/
1885 /*----------------------------*/
1886 /* comma operator */
1887 /*----------------------------*/
1889 return isConformingBody (pbody->left, sym, body) &&
1890 isConformingBody (pbody->right, sym, body);
1892 /*------------------------------------------------------------------*/
1893 /*----------------------------*/
1895 /*----------------------------*/
1897 /* if local & not passed as paramater then ok */
1898 if (sym->level && !astHasSymbol(pbody->right,sym))
1902 /*------------------------------------------------------------------*/
1903 /*----------------------------*/
1904 /* return statement */
1905 /*----------------------------*/
1910 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1915 if (astHasSymbol (pbody->left, sym))
1922 return isConformingBody (pbody->left, sym, body) &&
1923 isConformingBody (pbody->right, sym, body);
1929 /*-----------------------------------------------------------------*/
1930 /* isLoopReversible - takes a for loop as input && returns true */
1931 /* if the for loop is reversible. If yes will set the value of */
1932 /* the loop control var & init value & termination value */
1933 /*-----------------------------------------------------------------*/
1935 isLoopReversible (ast * loop, symbol ** loopCntrl,
1936 ast ** init, ast ** end)
1938 /* if option says don't do it then don't */
1939 if (optimize.noLoopReverse)
1941 /* there are several tests to determine this */
1943 /* for loop has to be of the form
1944 for ( <sym> = <const1> ;
1945 [<sym> < <const2>] ;
1946 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1948 if (!isLoopCountable (AST_FOR (loop, initExpr),
1949 AST_FOR (loop, condExpr),
1950 AST_FOR (loop, loopExpr),
1951 loopCntrl, init, end))
1954 /* now do some serious checking on the body of the loop
1957 return isConformingBody (loop->left, *loopCntrl, loop->left);
1961 /*-----------------------------------------------------------------*/
1962 /* replLoopSym - replace the loop sym by loop sym -1 */
1963 /*-----------------------------------------------------------------*/
1965 replLoopSym (ast * body, symbol * sym)
1968 if (!body || IS_AST_LINK (body))
1971 if (IS_AST_SYM_VALUE (body))
1974 if (isSymbolEqual (AST_SYMBOL (body), sym))
1978 body->opval.op = '-';
1979 body->left = newAst_VALUE (symbolVal (sym));
1980 body->right = newAst_VALUE (constVal ("1"));
1988 replLoopSym (body->left, sym);
1989 replLoopSym (body->right, sym);
1993 /*-----------------------------------------------------------------*/
1994 /* reverseLoop - do the actual loop reversal */
1995 /*-----------------------------------------------------------------*/
1997 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
2001 /* create the following tree
2006 if (sym) goto for_continue ;
2009 /* put it together piece by piece */
2010 rloop = newNode (NULLOP,
2011 createIf (newAst_VALUE (symbolVal (sym)),
2013 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
2016 newAst_VALUE (symbolVal (sym)),
2019 replLoopSym (loop->left, sym);
2020 setAstLineno (rloop, init->lineno);
2022 rloop = newNode (NULLOP,
2024 newAst_VALUE (symbolVal (sym)),
2025 newNode ('-', end, init)),
2026 createLabel (AST_FOR (loop, continueLabel),
2030 newNode (SUB_ASSIGN,
2031 newAst_VALUE (symbolVal (sym)),
2032 newAst_VALUE (constVal ("1"))),
2035 rloop->lineno=init->lineno;
2036 return decorateType (rloop, RESULT_TYPE_NONE);
2040 /*-----------------------------------------------------------------*/
2041 /* searchLitOp - search tree (*ops only) for an ast with literal */
2042 /*-----------------------------------------------------------------*/
2044 searchLitOp (ast *tree, ast **parent, const unsigned char *ops)
2048 if (tree && optimize.global_cse)
2050 /* is there a literal operand? */
2052 IS_AST_OP(tree->right) &&
2053 tree->right->right &&
2054 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
2056 if (IS_LITERAL (RTYPE (tree->right)) !=
2057 IS_LITERAL (LTYPE (tree->right)))
2059 tree->right->decorated = 0;
2060 tree->decorated = 0;
2064 ret = searchLitOp (tree->right, parent, ops);
2069 IS_AST_OP(tree->left) &&
2070 tree->left->right &&
2071 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
2073 if (IS_LITERAL (RTYPE (tree->left)) !=
2074 IS_LITERAL (LTYPE (tree->left)))
2076 tree->left->decorated = 0;
2077 tree->decorated = 0;
2081 ret = searchLitOp (tree->left, parent, ops);
2089 /*-----------------------------------------------------------------*/
2090 /* getResultFromType */
2091 /*-----------------------------------------------------------------*/
2093 getResultTypeFromType (sym_link *type)
2095 /* type = getSpec (type); */
2097 return RESULT_TYPE_BIT;
2098 if (IS_BITFIELD (type))
2100 int blen = SPEC_BLEN (type);
2103 return RESULT_TYPE_BIT;
2105 return RESULT_TYPE_CHAR;
2106 return RESULT_TYPE_INT;
2109 return RESULT_TYPE_CHAR;
2112 return RESULT_TYPE_INT;
2113 return RESULT_TYPE_OTHER;
2116 /*-----------------------------------------------------------------*/
2117 /* addCast - adds casts to a type specified by RESULT_TYPE */
2118 /*-----------------------------------------------------------------*/
2120 addCast (ast *tree, RESULT_TYPE resultType, bool promote)
2123 bool upCasted = FALSE;
2127 case RESULT_TYPE_NONE:
2128 /* if thing smaller than int must be promoted to int */
2130 getSize (tree->etype) >= INTSIZE)
2131 /* promotion not necessary or already an int */
2133 /* char and bits: promote to int */
2134 newLink = newIntLink();
2137 case RESULT_TYPE_BIT:
2139 /* already an int */
2140 bitsForType (tree->etype) >= 16 ||
2141 /* bit to bit operation: don't promote, the code generators
2142 hopefully know everything about promotion rules */
2143 bitsForType (tree->etype) == 1)
2145 newLink = newIntLink();
2148 case RESULT_TYPE_CHAR:
2149 if (IS_CHAR (tree->etype) ||
2150 IS_FLOAT(tree->etype) ||
2151 IS_FIXED(tree->etype))
2153 newLink = newCharLink();
2155 case RESULT_TYPE_INT:
2157 if (getSize (tree->etype) > INTSIZE)
2159 /* warn ("Loosing significant digits"); */
2163 /* char: promote to int */
2165 getSize (tree->etype) >= INTSIZE)
2167 newLink = newIntLink();
2170 case RESULT_TYPE_OTHER:
2173 /* return type is long, float: promote char to int */
2174 if (getSize (tree->etype) >= INTSIZE)
2176 newLink = newIntLink();
2182 tree->decorated = 0;
2183 tree = newNode (CAST, newAst_LINK (newLink), tree);
2184 tree->lineno = tree->right->lineno;
2185 /* keep unsigned type during cast to smaller type,
2186 but not when promoting from char to int */
2188 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2189 return decorateType (tree, resultType);
2192 /*-----------------------------------------------------------------*/
2193 /* resultTypePropagate - decides if resultType can be propagated */
2194 /*-----------------------------------------------------------------*/
2196 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2198 switch (tree->opval.op)
2217 return RESULT_TYPE_NONE;
2221 return RESULT_TYPE_IFX;
2223 return RESULT_TYPE_NONE;
2227 /*-----------------------------------------------------------------*/
2228 /* getLeftResultType - gets type from left branch for propagation */
2229 /*-----------------------------------------------------------------*/
2231 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2233 switch (tree->opval.op)
2237 if (IS_PTR (LTYPE (tree)))
2238 return RESULT_TYPE_NONE;
2240 return getResultTypeFromType (LETYPE (tree));
2242 if (IS_PTR (currFunc->type->next))
2243 return RESULT_TYPE_NONE;
2245 return getResultTypeFromType (currFunc->type->next);
2247 if (!IS_ARRAY (LTYPE (tree)))
2249 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 255)
2250 return RESULT_TYPE_CHAR;
2257 /*--------------------------------------------------------------------*/
2258 /* decorateType - compute type for this tree, also does type checking.*/
2259 /* This is done bottom up, since type has to flow upwards. */
2260 /* resultType flows top-down and forces e.g. char-arithmetic, if the */
2261 /* result is a char and the operand(s) are int's. */
2262 /* It also does constant folding, and parameter checking. */
2263 /*--------------------------------------------------------------------*/
2265 decorateType (ast * tree, RESULT_TYPE resultType)
2269 RESULT_TYPE resultTypeProp;
2274 /* if already has type then do nothing */
2275 if (tree->decorated)
2278 tree->decorated = 1;
2281 /* print the line */
2282 /* if not block & function */
2283 if (tree->type == EX_OP &&
2284 (tree->opval.op != FUNCTION &&
2285 tree->opval.op != BLOCK &&
2286 tree->opval.op != NULLOP))
2288 filename = tree->filename;
2289 lineno = tree->lineno;
2293 /* if any child is an error | this one is an error do nothing */
2294 if (tree->isError ||
2295 (tree->left && tree->left->isError) ||
2296 (tree->right && tree->right->isError))
2299 /*------------------------------------------------------------------*/
2300 /*----------------------------*/
2301 /* leaf has been reached */
2302 /*----------------------------*/
2303 lineno=tree->lineno;
2304 /* if this is of type value */
2305 /* just get the type */
2306 if (tree->type == EX_VALUE)
2309 if (IS_LITERAL (tree->opval.val->etype))
2312 /* if this is a character array then declare it */
2313 if (IS_ARRAY (tree->opval.val->type))
2314 tree->opval.val = stringToSymbol (tree->opval.val);
2316 /* otherwise just copy the type information */
2317 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2321 if (tree->opval.val->sym)
2323 /* if the undefined flag is set then give error message */
2324 if (tree->opval.val->sym->undefined)
2326 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2328 TTYPE (tree) = TETYPE (tree) =
2329 tree->opval.val->type = tree->opval.val->sym->type =
2330 tree->opval.val->etype = tree->opval.val->sym->etype =
2331 copyLinkChain (INTTYPE);
2336 /* if impilicit i.e. struct/union member then no type */
2337 if (tree->opval.val->sym->implicit)
2338 TTYPE (tree) = TETYPE (tree) = NULL;
2343 /* else copy the type */
2344 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2346 /* and mark it as referenced */
2347 tree->opval.val->sym->isref = 1;
2355 /* if type link for the case of cast */
2356 if (tree->type == EX_LINK)
2358 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2366 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2368 if (tree->left && tree->left->type == EX_OPERAND
2369 && (tree->left->opval.op == INC_OP
2370 || tree->left->opval.op == DEC_OP)
2371 && tree->left->left)
2373 tree->left->right = tree->left->left;
2374 tree->left->left = NULL;
2376 if (tree->right && tree->right->type == EX_OPERAND
2377 && (tree->right->opval.op == INC_OP
2378 || tree->right->opval.op == DEC_OP)
2379 && tree->right->left)
2381 tree->right->right = tree->right->left;
2382 tree->right->left = NULL;
2387 /* Before decorating the left branch we've to decide in dependence
2388 upon tree->opval.op, if resultType can be propagated */
2389 resultTypeProp = resultTypePropagate (tree, resultType);
2391 if (tree->opval.op == '?')
2392 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2394 dtl = decorateType (tree->left, resultTypeProp);
2396 /* if an array node, we may need to swap branches */
2397 if (tree->opval.op == '[')
2399 /* determine which is the array & which the index */
2400 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2401 IS_INTEGRAL (LTYPE (tree)))
2403 ast *tempTree = tree->left;
2404 tree->left = tree->right;
2405 tree->right = tempTree;
2409 /* After decorating the left branch there's type information available
2410 in tree->left->?type. If the op is e.g. '=' we extract the type
2411 information from there and propagate it to the right branch. */
2412 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2414 switch (tree->opval.op)
2417 /* delay right side for '?' operator since conditional macro
2418 expansions might rely on this */
2422 /* decorate right side for CALL (parameter list) in processParms();
2423 there is resultType available */
2427 dtr = decorateType (tree->right, resultTypeProp);
2431 /* this is to take care of situations
2432 when the tree gets rewritten */
2433 if (dtl != tree->left)
2435 if (dtr != tree->right)
2437 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2441 /* depending on type of operator do */
2443 switch (tree->opval.op)
2445 /*------------------------------------------------------------------*/
2446 /*----------------------------*/
2448 /*----------------------------*/
2451 /* first check if this is a array or a pointer */
2452 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2454 werror (E_NEED_ARRAY_PTR, "[]");
2455 goto errorTreeReturn;
2458 /* check if the type of the idx */
2459 if (!IS_INTEGRAL (RTYPE (tree)))
2461 werror (E_IDX_NOT_INT);
2462 goto errorTreeReturn;
2465 /* if the left is an rvalue then error */
2468 werror (E_LVALUE_REQUIRED, "array access");
2469 goto errorTreeReturn;
2472 if (IS_LITERAL (RTYPE (tree)))
2474 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2475 int arraySize = DCL_ELEM (LTYPE (tree));
2476 if (arraySize && arrayIndex >= arraySize)
2478 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2483 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2484 SPEC_CONST (TETYPE (tree)) |= DCL_PTR_CONST (LTYPE (tree));
2487 /*------------------------------------------------------------------*/
2488 /*----------------------------*/
2490 /*----------------------------*/
2492 /* if this is not a structure */
2493 if (!IS_STRUCT (LTYPE (tree)))
2495 werror (E_STRUCT_UNION, ".");
2496 goto errorTreeReturn;
2498 TTYPE (tree) = structElemType (LTYPE (tree),
2499 (tree->right->type == EX_VALUE ?
2500 tree->right->opval.val : NULL));
2501 TETYPE (tree) = getSpec (TTYPE (tree));
2504 /*------------------------------------------------------------------*/
2505 /*----------------------------*/
2506 /* struct/union pointer */
2507 /*----------------------------*/
2509 /* if not pointer to a structure */
2510 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2512 werror (E_PTR_REQD);
2513 goto errorTreeReturn;
2516 if (!IS_STRUCT (LTYPE (tree)->next))
2518 werror (E_STRUCT_UNION, "->");
2519 goto errorTreeReturn;
2522 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2523 (tree->right->type == EX_VALUE ?
2524 tree->right->opval.val : NULL));
2525 TETYPE (tree) = getSpec (TTYPE (tree));
2527 /* adjust the storage class */
2528 switch (DCL_TYPE(tree->left->ftype)) {
2530 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2533 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2536 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2539 SPEC_SCLS (TETYPE (tree)) = 0;
2542 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2545 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2548 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2551 SPEC_SCLS (TETYPE (tree)) = 0;
2558 /* This breaks with extern declarations, bitfields, and perhaps other */
2559 /* cases (gcse). Let's leave this optimization disabled for now and */
2560 /* ponder if there's a safe way to do this. -- EEP */
2562 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2563 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2565 /* If defined struct type at addr var
2566 then rewrite (&struct var)->member
2568 and define membertype at (addr+offsetof(struct var,member)) temp
2571 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2572 AST_SYMBOL(tree->right));
2574 sym = newSymbol(genSymName (0), 0);
2575 sym->type = TTYPE (tree);
2576 sym->etype = getSpec(sym->type);
2577 sym->lineDef = tree->lineno;
2580 SPEC_STAT (sym->etype) = 1;
2581 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2583 SPEC_ABSA(sym->etype) = 1;
2584 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2587 AST_VALUE (tree) = symbolVal(sym);
2590 tree->type = EX_VALUE;
2598 /*------------------------------------------------------------------*/
2599 /*----------------------------*/
2600 /* ++/-- operation */
2601 /*----------------------------*/
2605 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2606 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2607 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2608 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2617 /*------------------------------------------------------------------*/
2618 /*----------------------------*/
2620 /*----------------------------*/
2621 case '&': /* can be unary */
2622 /* if right is NULL then unary operation */
2623 if (tree->right) /* not an unary operation */
2626 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2628 werror (E_BITWISE_OP);
2629 werror (W_CONTINUE, "left & right types are ");
2630 printTypeChain (LTYPE (tree), stderr);
2631 fprintf (stderr, ",");
2632 printTypeChain (RTYPE (tree), stderr);
2633 fprintf (stderr, "\n");
2634 goto errorTreeReturn;
2637 /* if they are both literal */
2638 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2640 tree->type = EX_VALUE;
2641 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2642 valFromType (RETYPE (tree)), '&');
2644 tree->right = tree->left = NULL;
2645 TETYPE (tree) = tree->opval.val->etype;
2646 TTYPE (tree) = tree->opval.val->type;
2650 /* see if this is a GETHBIT operation if yes
2653 ast *otree = optimizeGetHbit (tree, resultType);
2656 return decorateType (otree, RESULT_TYPE_NONE);
2659 /* see if this is a GETABIT operation if yes
2662 ast *otree = optimizeGetAbit (tree, resultType);
2665 return decorateType (otree, RESULT_TYPE_NONE);
2668 /* see if this is a GETBYTE operation if yes
2671 ast *otree = optimizeGetByte (tree, resultType);
2674 return decorateType (otree, RESULT_TYPE_NONE);
2677 /* see if this is a GETWORD operation if yes
2680 ast *otree = optimizeGetWord (tree, resultType);
2683 return decorateType (otree, RESULT_TYPE_NONE);
2686 /* if left is a literal exchange left & right */
2687 if (IS_LITERAL (LTYPE (tree)))
2689 ast *tTree = tree->left;
2690 tree->left = tree->right;
2691 tree->right = tTree;
2694 /* if right is a literal and */
2695 /* we can find a 2nd literal in an and-tree then */
2696 /* rearrange the tree */
2697 if (IS_LITERAL (RTYPE (tree)))
2700 ast *litTree = searchLitOp (tree, &parent, "&");
2704 ast *tTree = litTree->left;
2705 litTree->left = tree->right;
2706 tree->right = tTree;
2707 /* both operands in litTree are literal now */
2708 decorateType (parent, resultType);
2712 LRVAL (tree) = RRVAL (tree) = 1;
2714 TTYPE (tree) = computeType (LTYPE (tree),
2718 TETYPE (tree) = getSpec (TTYPE (tree));
2723 /*------------------------------------------------------------------*/
2724 /*----------------------------*/
2726 /*----------------------------*/
2727 p = newLink (DECLARATOR);
2728 /* if bit field then error */
2729 if (IS_BITVAR (tree->left->etype))
2731 werror (E_ILLEGAL_ADDR, "address of bit variable");
2732 goto errorTreeReturn;
2735 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2737 werror (E_ILLEGAL_ADDR, "address of register variable");
2738 goto errorTreeReturn;
2741 if (IS_FUNC (LTYPE (tree)))
2743 // this ought to be ignored
2744 return (tree->left);
2747 if (IS_LITERAL(LTYPE(tree)))
2749 werror (E_ILLEGAL_ADDR, "address of literal");
2750 goto errorTreeReturn;
2755 werror (E_LVALUE_REQUIRED, "address of");
2756 goto errorTreeReturn;
2759 DCL_TYPE (p) = POINTER;
2760 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2761 DCL_TYPE (p) = CPOINTER;
2762 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2763 DCL_TYPE (p) = FPOINTER;
2764 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2765 DCL_TYPE (p) = PPOINTER;
2766 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2767 DCL_TYPE (p) = IPOINTER;
2768 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2769 DCL_TYPE (p) = EEPPOINTER;
2770 else if (SPEC_OCLS(tree->left->etype))
2771 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2773 DCL_TYPE (p) = POINTER;
2775 if (IS_AST_SYM_VALUE (tree->left))
2777 AST_SYMBOL (tree->left)->addrtaken = 1;
2778 AST_SYMBOL (tree->left)->allocreq = 1;
2781 p->next = LTYPE (tree);
2783 TETYPE (tree) = getSpec (TTYPE (tree));
2788 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2789 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2791 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2792 AST_SYMBOL(tree->left->right));
2793 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2794 valueFromLit(element->offset));
2797 tree->type = EX_VALUE;
2798 tree->values.literalFromCast = 1;
2804 /*------------------------------------------------------------------*/
2805 /*----------------------------*/
2807 /*----------------------------*/
2809 /* if the rewrite succeeds then don't go any further */
2811 ast *wtree = optimizeRRCRLC (tree);
2813 return decorateType (wtree, RESULT_TYPE_NONE);
2815 wtree = optimizeSWAP (tree);
2817 return decorateType (wtree, RESULT_TYPE_NONE);
2820 /* if left is a literal exchange left & right */
2821 if (IS_LITERAL (LTYPE (tree)))
2823 ast *tTree = tree->left;
2824 tree->left = tree->right;
2825 tree->right = tTree;
2828 /* if right is a literal and */
2829 /* we can find a 2nd literal in an or-tree then */
2830 /* rearrange the tree */
2831 if (IS_LITERAL (RTYPE (tree)))
2834 ast *litTree = searchLitOp (tree, &parent, "|");
2838 ast *tTree = litTree->left;
2839 litTree->left = tree->right;
2840 tree->right = tTree;
2841 /* both operands in tTree are literal now */
2842 decorateType (parent, resultType);
2847 /*------------------------------------------------------------------*/
2848 /*----------------------------*/
2850 /*----------------------------*/
2852 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2854 werror (E_BITWISE_OP);
2855 werror (W_CONTINUE, "left & right types are ");
2856 printTypeChain (LTYPE (tree), stderr);
2857 fprintf (stderr, ",");
2858 printTypeChain (RTYPE (tree), stderr);
2859 fprintf (stderr, "\n");
2860 goto errorTreeReturn;
2863 /* if they are both literal then rewrite the tree */
2864 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2866 tree->type = EX_VALUE;
2867 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2868 valFromType (RETYPE (tree)),
2870 tree->right = tree->left = NULL;
2871 TETYPE (tree) = tree->opval.val->etype;
2872 TTYPE (tree) = tree->opval.val->type;
2876 /* if left is a literal exchange left & right */
2877 if (IS_LITERAL (LTYPE (tree)))
2879 ast *tTree = tree->left;
2880 tree->left = tree->right;
2881 tree->right = tTree;
2884 /* if right is a literal and */
2885 /* we can find a 2nd literal in a xor-tree then */
2886 /* rearrange the tree */
2887 if (IS_LITERAL (RTYPE (tree)) &&
2888 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
2891 ast *litTree = searchLitOp (tree, &parent, "^");
2895 ast *tTree = litTree->left;
2896 litTree->left = tree->right;
2897 tree->right = tTree;
2898 /* both operands in litTree are literal now */
2899 decorateType (parent, resultType);
2903 LRVAL (tree) = RRVAL (tree) = 1;
2905 TTYPE (tree) = computeType (LTYPE (tree),
2909 TETYPE (tree) = getSpec (TTYPE (tree));
2913 /*------------------------------------------------------------------*/
2914 /*----------------------------*/
2916 /*----------------------------*/
2918 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2920 werror (E_INVALID_OP, "divide");
2921 goto errorTreeReturn;
2923 /* if they are both literal then */
2924 /* rewrite the tree */
2925 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2927 tree->type = EX_VALUE;
2928 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2929 valFromType (RETYPE (tree)));
2930 tree->right = tree->left = NULL;
2931 TETYPE (tree) = getSpec (TTYPE (tree) =
2932 tree->opval.val->type);
2936 LRVAL (tree) = RRVAL (tree) = 1;
2938 TETYPE (tree) = getSpec (TTYPE (tree) =
2939 computeType (LTYPE (tree),
2944 /* if right is a literal and */
2945 /* left is also a division by a literal then */
2946 /* rearrange the tree */
2947 if (IS_LITERAL (RTYPE (tree))
2948 /* avoid infinite loop */
2949 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
2952 ast *litTree = searchLitOp (tree, &parent, "/");
2955 if (IS_LITERAL (RTYPE (litTree)))
2959 litTree->right = newNode ('*',
2961 copyAst (tree->right));
2962 litTree->right->lineno = tree->lineno;
2964 tree->right->opval.val = constVal ("1");
2965 decorateType (parent, resultType);
2969 /* litTree->left is literal: no gcse possible.
2970 We can't call decorateType(parent, RESULT_TYPE_NONE), because
2971 this would cause an infinit loop. */
2972 parent->decorated = 1;
2973 decorateType (litTree, resultType);
2980 /*------------------------------------------------------------------*/
2981 /*----------------------------*/
2983 /*----------------------------*/
2985 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2987 werror (E_BITWISE_OP);
2988 werror (W_CONTINUE, "left & right types are ");
2989 printTypeChain (LTYPE (tree), stderr);
2990 fprintf (stderr, ",");
2991 printTypeChain (RTYPE (tree), stderr);
2992 fprintf (stderr, "\n");
2993 goto errorTreeReturn;
2995 /* if they are both literal then */
2996 /* rewrite the tree */
2997 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2999 tree->type = EX_VALUE;
3000 tree->opval.val = valMod (valFromType (LETYPE (tree)),
3001 valFromType (RETYPE (tree)));
3002 tree->right = tree->left = NULL;
3003 TETYPE (tree) = getSpec (TTYPE (tree) =
3004 tree->opval.val->type);
3007 LRVAL (tree) = RRVAL (tree) = 1;
3008 TETYPE (tree) = getSpec (TTYPE (tree) =
3009 computeType (LTYPE (tree),
3015 /*------------------------------------------------------------------*/
3016 /*----------------------------*/
3017 /* address dereference */
3018 /*----------------------------*/
3019 case '*': /* can be unary : if right is null then unary operation */
3022 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3024 werror (E_PTR_REQD);
3025 goto errorTreeReturn;
3030 werror (E_LVALUE_REQUIRED, "pointer deref");
3031 goto errorTreeReturn;
3033 if (IS_ADDRESS_OF_OP(tree->left))
3035 /* replace *&obj with obj */
3036 return tree->left->left;
3038 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
3039 TETYPE (tree) = getSpec (TTYPE (tree));
3040 /* adjust the storage class */
3041 switch (DCL_TYPE(tree->left->ftype)) {
3043 SPEC_SCLS(TETYPE(tree)) = S_DATA;
3046 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
3049 SPEC_SCLS(TETYPE(tree)) = S_CODE;
3052 SPEC_SCLS (TETYPE (tree)) = 0;
3055 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
3058 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
3061 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
3064 SPEC_SCLS (TETYPE (tree)) = 0;
3073 /*------------------------------------------------------------------*/
3074 /*----------------------------*/
3075 /* multiplication */
3076 /*----------------------------*/
3077 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
3079 werror (E_INVALID_OP, "multiplication");
3080 goto errorTreeReturn;
3083 /* if they are both literal then */
3084 /* rewrite the tree */
3085 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3087 tree->type = EX_VALUE;
3088 tree->opval.val = valMult (valFromType (LETYPE (tree)),
3089 valFromType (RETYPE (tree)));
3090 tree->right = tree->left = NULL;
3091 TETYPE (tree) = getSpec (TTYPE (tree) =
3092 tree->opval.val->type);
3096 /* if left is a literal exchange left & right */
3097 if (IS_LITERAL (LTYPE (tree)))
3099 ast *tTree = tree->left;
3100 tree->left = tree->right;
3101 tree->right = tTree;
3104 /* if right is a literal and */
3105 /* we can find a 2nd literal in a mul-tree then */
3106 /* rearrange the tree */
3107 if (IS_LITERAL (RTYPE (tree)))
3110 ast *litTree = searchLitOp (tree, &parent, "*");
3114 ast *tTree = litTree->left;
3115 litTree->left = tree->right;
3116 tree->right = tTree;
3117 /* both operands in litTree are literal now */
3118 decorateType (parent, resultType);
3122 LRVAL (tree) = RRVAL (tree) = 1;
3123 tree->left = addCast (tree->left, resultType, FALSE);
3124 tree->right = addCast (tree->right, resultType, FALSE);
3125 TETYPE (tree) = getSpec (TTYPE (tree) =
3126 computeType (LTYPE (tree),
3133 /*------------------------------------------------------------------*/
3134 /*----------------------------*/
3135 /* unary '+' operator */
3136 /*----------------------------*/
3141 if (!IS_ARITHMETIC (LTYPE (tree)))
3143 werror (E_UNARY_OP, '+');
3144 goto errorTreeReturn;
3147 /* if left is a literal then do it */
3148 if (IS_LITERAL (LTYPE (tree)))
3150 tree->type = EX_VALUE;
3151 tree->opval.val = valFromType (LETYPE (tree));
3153 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3157 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3161 /*------------------------------------------------------------------*/
3162 /*----------------------------*/
3164 /*----------------------------*/
3166 /* this is not a unary operation */
3167 /* if both pointers then problem */
3168 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3169 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3171 werror (E_PTR_PLUS_PTR);
3172 goto errorTreeReturn;
3175 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3176 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3178 werror (E_PLUS_INVALID, "+");
3179 goto errorTreeReturn;
3182 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3183 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3185 werror (E_PLUS_INVALID, "+");
3186 goto errorTreeReturn;
3188 /* if they are both literal then */
3189 /* rewrite the tree */
3190 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3192 tree->type = EX_VALUE;
3193 tree->left = addCast (tree->left, resultType, TRUE);
3194 tree->right = addCast (tree->right, resultType, TRUE);
3195 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3196 valFromType (RETYPE (tree)));
3197 tree->right = tree->left = NULL;
3198 TETYPE (tree) = getSpec (TTYPE (tree) =
3199 tree->opval.val->type);
3203 /* if the right is a pointer or left is a literal
3204 xchange left & right */
3205 if (IS_ARRAY (RTYPE (tree)) ||
3206 IS_PTR (RTYPE (tree)) ||
3207 IS_LITERAL (LTYPE (tree)))
3209 ast *tTree = tree->left;
3210 tree->left = tree->right;
3211 tree->right = tTree;
3214 /* if right is a literal and */
3215 /* left is also an addition/subtraction with a literal then */
3216 /* rearrange the tree */
3217 if (IS_LITERAL (RTYPE (tree)))
3219 ast *litTree, *parent;
3220 litTree = searchLitOp (tree, &parent, "+-");
3223 if (litTree->opval.op == '+')
3227 ast *tTree = litTree->left;
3228 litTree->left = tree->right;
3229 tree->right = tree->left;
3232 else if (litTree->opval.op == '-')
3234 if (IS_LITERAL (RTYPE (litTree)))
3238 ast *tTree = litTree->left;
3239 litTree->left = tree->right;
3240 tree->right = tTree;
3246 ast *tTree = litTree->right;
3247 litTree->right = tree->right;
3248 tree->right = tTree;
3249 litTree->opval.op = '+';
3250 tree->opval.op = '-';
3253 decorateType (parent, resultType);
3257 LRVAL (tree) = RRVAL (tree) = 1;
3258 /* if the left is a pointer */
3259 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3260 TETYPE (tree) = getSpec (TTYPE (tree) =
3264 tree->left = addCast (tree->left, resultType, TRUE);
3265 tree->right = addCast (tree->right, resultType, TRUE);
3266 TETYPE (tree) = getSpec (TTYPE (tree) =
3267 computeType (LTYPE (tree),
3275 /*------------------------------------------------------------------*/
3276 /*----------------------------*/
3278 /*----------------------------*/
3279 case '-': /* can be unary */
3280 /* if right is null then unary */
3284 if (!IS_ARITHMETIC (LTYPE (tree)))
3286 werror (E_UNARY_OP, tree->opval.op);
3287 goto errorTreeReturn;
3290 /* if left is a literal then do it */
3291 if (IS_LITERAL (LTYPE (tree)))
3293 tree->type = EX_VALUE;
3294 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3296 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3299 tree->left = addCast (tree->left, resultType, TRUE);
3300 TETYPE (tree) = getSpec (TTYPE (tree) =
3301 computeType (LTYPE (tree),
3309 /*------------------------------------------------------------------*/
3310 /*----------------------------*/
3312 /*----------------------------*/
3314 if (!(IS_PTR (LTYPE (tree)) ||
3315 IS_ARRAY (LTYPE (tree)) ||
3316 IS_ARITHMETIC (LTYPE (tree))))
3318 werror (E_PLUS_INVALID, "-");
3319 goto errorTreeReturn;
3322 if (!(IS_PTR (RTYPE (tree)) ||
3323 IS_ARRAY (RTYPE (tree)) ||
3324 IS_ARITHMETIC (RTYPE (tree))))
3326 werror (E_PLUS_INVALID, "-");
3327 goto errorTreeReturn;
3330 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3331 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3332 IS_INTEGRAL (RTYPE (tree))))
3334 werror (E_PLUS_INVALID, "-");
3335 goto errorTreeReturn;
3338 /* if they are both literal then */
3339 /* rewrite the tree */
3340 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3342 tree->type = EX_VALUE;
3343 tree->left = addCast (tree->left, resultType, TRUE);
3344 tree->right = addCast (tree->right, resultType, TRUE);
3345 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3346 valFromType (RETYPE (tree)));
3347 tree->right = tree->left = NULL;
3348 TETYPE (tree) = getSpec (TTYPE (tree) =
3349 tree->opval.val->type);
3353 /* if the left & right are equal then zero */
3354 if (isAstEqual (tree->left, tree->right))
3356 tree->type = EX_VALUE;
3357 tree->left = tree->right = NULL;
3358 tree->opval.val = constVal ("0");
3359 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3363 /* if both of them are pointers or arrays then */
3364 /* the result is going to be an integer */
3365 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3366 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3367 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3369 /* if only the left is a pointer */
3370 /* then result is a pointer */
3371 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3372 TETYPE (tree) = getSpec (TTYPE (tree) =
3376 tree->left = addCast (tree->left, resultType, TRUE);
3377 tree->right = addCast (tree->right, resultType, TRUE);
3379 TETYPE (tree) = getSpec (TTYPE (tree) =
3380 computeType (LTYPE (tree),
3386 LRVAL (tree) = RRVAL (tree) = 1;
3388 /* if right is a literal and */
3389 /* left is also an addition/subtraction with a literal then */
3390 /* rearrange the tree */
3391 if (IS_LITERAL (RTYPE (tree))
3392 /* avoid infinite loop */
3393 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3395 ast *litTree, *litParent;
3396 litTree = searchLitOp (tree, &litParent, "+-");
3399 if (litTree->opval.op == '+')
3403 ast *tTree = litTree->left;
3404 litTree->left = litTree->right;
3405 litTree->right = tree->right;
3406 tree->right = tTree;
3407 tree->opval.op = '+';
3408 litTree->opval.op = '-';
3410 else if (litTree->opval.op == '-')
3412 if (IS_LITERAL (RTYPE (litTree)))
3416 ast *tTree = litTree->left;
3417 litTree->left = tree->right;
3418 tree->right = litParent->left;
3419 litParent->left = tTree;
3420 litTree->opval.op = '+';
3422 tree->decorated = 0;
3423 decorateType (tree, resultType);
3429 ast *tTree = litTree->right;
3430 litTree->right = tree->right;
3431 tree->right = tTree;
3434 decorateType (litParent, resultType);
3439 /*------------------------------------------------------------------*/
3440 /*----------------------------*/
3442 /*----------------------------*/
3444 /* can be only integral type */
3445 if (!IS_INTEGRAL (LTYPE (tree)))
3447 werror (E_UNARY_OP, tree->opval.op);
3448 goto errorTreeReturn;
3451 /* if left is a literal then do it */
3452 if (IS_LITERAL (LTYPE (tree)))
3454 tree->type = EX_VALUE;
3455 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3457 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3458 return addCast (tree, resultType, TRUE);
3461 if (resultType == RESULT_TYPE_BIT &&
3462 IS_UNSIGNED (tree->left->etype) &&
3463 getSize (tree->left->etype) < INTSIZE)
3465 /* promotion rules are responsible for this strange result:
3466 bit -> int -> ~int -> bit
3467 uchar -> int -> ~int -> bit
3469 werror(W_COMPLEMENT);
3471 /* optimize bit-result, even if we optimize a buggy source */
3472 tree->type = EX_VALUE;
3473 tree->opval.val = constVal ("1");
3476 tree->left = addCast (tree->left, resultType, TRUE);
3478 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3481 /*------------------------------------------------------------------*/
3482 /*----------------------------*/
3484 /*----------------------------*/
3486 /* can be pointer */
3487 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3488 !IS_PTR (LTYPE (tree)) &&
3489 !IS_ARRAY (LTYPE (tree)))
3491 werror (E_UNARY_OP, tree->opval.op);
3492 goto errorTreeReturn;
3495 /* if left is a literal then do it */
3496 if (IS_LITERAL (LTYPE (tree)))
3498 tree->type = EX_VALUE;
3499 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3501 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3505 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3508 /*------------------------------------------------------------------*/
3509 /*----------------------------*/
3511 /*----------------------------*/
3515 TTYPE (tree) = LTYPE (tree);
3516 TETYPE (tree) = LETYPE (tree);
3521 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3525 TTYPE (tree) = TETYPE (tree) = newCharLink();
3529 TTYPE (tree) = TETYPE (tree) = newIntLink();
3534 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3536 werror (E_SHIFT_OP_INVALID);
3537 werror (W_CONTINUE, "left & right types are ");
3538 printTypeChain (LTYPE (tree), stderr);
3539 fprintf (stderr, ",");
3540 printTypeChain (RTYPE (tree), stderr);
3541 fprintf (stderr, "\n");
3542 goto errorTreeReturn;
3545 /* make smaller type only if it's a LEFT_OP */
3546 if (tree->opval.op == LEFT_OP)
3547 tree->left = addCast (tree->left, resultType, TRUE);
3549 /* if they are both literal then */
3550 /* rewrite the tree */
3551 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3553 tree->type = EX_VALUE;
3554 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3555 valFromType (RETYPE (tree)),
3556 (tree->opval.op == LEFT_OP ? 1 : 0));
3557 tree->right = tree->left = NULL;
3558 TETYPE (tree) = getSpec (TTYPE (tree) =
3559 tree->opval.val->type);
3563 /* see if this is a GETBYTE operation if yes
3566 ast *otree = optimizeGetByte (tree, resultType);
3569 return decorateType (otree, RESULT_TYPE_NONE);
3572 /* see if this is a GETWORD operation if yes
3575 ast *otree = optimizeGetWord (tree, resultType);
3578 return decorateType (otree, RESULT_TYPE_NONE);
3581 LRVAL (tree) = RRVAL (tree) = 1;
3582 if (tree->opval.op == LEFT_OP)
3584 TETYPE (tree) = getSpec (TTYPE (tree) =
3585 computeType (LTYPE (tree),
3592 /* no promotion necessary */
3593 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3594 if (IS_LITERAL (TTYPE (tree)))
3595 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3598 /* if only the right side is a literal & we are
3599 shifting more than size of the left operand then zero */
3600 if (IS_LITERAL (RTYPE (tree)) &&
3601 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3602 (getSize (TETYPE (tree)) * 8))
3604 if (tree->opval.op==LEFT_OP ||
3605 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3607 lineno=tree->lineno;
3608 werror (W_SHIFT_CHANGED,
3609 (tree->opval.op == LEFT_OP ? "left" : "right"));
3610 tree->type = EX_VALUE;
3611 tree->left = tree->right = NULL;
3612 tree->opval.val = constVal ("0");
3613 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3620 /*------------------------------------------------------------------*/
3621 /*----------------------------*/
3623 /*----------------------------*/
3624 case CAST: /* change the type */
3625 /* cannot cast to an aggregate type */
3626 if (IS_AGGREGATE (LTYPE (tree)))
3628 werror (E_CAST_ILLEGAL);
3629 goto errorTreeReturn;
3632 /* make sure the type is complete and sane */
3633 changePointer(LTYPE(tree));
3634 checkTypeSanity(LETYPE(tree), "(cast)");
3636 /* if 'from' and 'to' are the same remove the superflous cast, */
3637 /* this helps other optimizations */
3638 if (compareTypeExact (LTYPE(tree), RTYPE(tree), -1) == 1)
3643 /* If code memory is read only, then pointers to code memory */
3644 /* implicitly point to constants -- make this explicit */
3646 sym_link *t = LTYPE(tree);
3647 while (t && t->next)
3649 if (IS_CODEPTR(t) && port->mem.code_ro)
3651 if (IS_SPEC(t->next))
3652 SPEC_CONST (t->next) = 1;
3654 DCL_PTR_CONST (t->next) = 1;
3661 /* if the right is a literal replace the tree */
3662 if (IS_LITERAL (RETYPE (tree))) {
3663 if (!IS_PTR (LTYPE (tree))) {
3664 tree->type = EX_VALUE;
3666 valCastLiteral (LTYPE (tree),
3667 floatFromVal (valFromType (RETYPE (tree))));
3670 TTYPE (tree) = tree->opval.val->type;
3671 tree->values.literalFromCast = 1;
3672 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3673 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3674 sym_link *rest = LTYPE(tree)->next;
3675 werror(W_LITERAL_GENERIC);
3676 TTYPE(tree) = newLink(DECLARATOR);
3677 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3678 TTYPE(tree)->next = rest;
3679 tree->left->opval.lnk = TTYPE(tree);
3682 TTYPE (tree) = LTYPE (tree);
3686 TTYPE (tree) = LTYPE (tree);
3690 #if 0 // this is already checked, now this could be explicit
3691 /* if pointer to struct then check names */
3692 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3693 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3694 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3696 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3697 SPEC_STRUCT(LETYPE(tree))->tag);
3700 if (IS_ADDRESS_OF_OP(tree->right)
3701 && IS_AST_SYM_VALUE (tree->right->left)
3702 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3704 symbol * sym = AST_SYMBOL (tree->right->left);
3705 unsigned int gptype = 0;
3706 unsigned int addr = SPEC_ADDR (sym->etype);
3708 if (IS_GENPTR (LTYPE (tree)) && GPTRSIZE > FPTRSIZE)
3710 switch (SPEC_SCLS (sym->etype))
3713 gptype = GPTYPE_CODE;
3716 gptype = GPTYPE_FAR;
3720 gptype = GPTYPE_NEAR;
3723 gptype = GPTYPE_XSTACK;
3728 addr |= gptype << (8*(GPTRSIZE - 1));
3731 tree->type = EX_VALUE;
3733 valCastLiteral (LTYPE (tree), addr);
3734 TTYPE (tree) = tree->opval.val->type;
3735 TETYPE (tree) = getSpec (TTYPE (tree));
3738 tree->values.literalFromCast = 1;
3742 /* handle offsetof macro: */
3743 /* #define offsetof(TYPE, MEMBER) \ */
3744 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3745 if (IS_ADDRESS_OF_OP(tree->right)
3746 && IS_AST_OP (tree->right->left)
3747 && tree->right->left->opval.op == PTR_OP
3748 && IS_AST_OP (tree->right->left->left)
3749 && tree->right->left->left->opval.op == CAST
3750 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3752 symbol *element = getStructElement (
3753 SPEC_STRUCT (LETYPE(tree->right->left)),
3754 AST_SYMBOL(tree->right->left->right)
3758 tree->type = EX_VALUE;
3759 tree->opval.val = valCastLiteral (
3762 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3765 TTYPE (tree) = tree->opval.val->type;
3766 TETYPE (tree) = getSpec (TTYPE (tree));
3773 /* if the right is a literal replace the tree */
3774 if (IS_LITERAL (RETYPE (tree))) {
3776 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3777 /* rewrite (type *)litaddr
3779 and define type at litaddr temp
3780 (but only if type's storage class is not generic)
3782 ast *newTree = newNode ('&', NULL, NULL);
3785 TTYPE (newTree) = LTYPE (tree);
3786 TETYPE (newTree) = getSpec(LTYPE (tree));
3788 /* define a global symbol at the casted address*/
3789 sym = newSymbol(genSymName (0), 0);
3790 sym->type = LTYPE (tree)->next;
3792 sym->type = newLink (V_VOID);
3793 sym->etype = getSpec(sym->type);
3794 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3795 sym->lineDef = tree->lineno;
3798 SPEC_STAT (sym->etype) = 1;
3799 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3800 SPEC_ABSA(sym->etype) = 1;
3801 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3804 newTree->left = newAst_VALUE(symbolVal(sym));
3805 newTree->left->lineno = tree->lineno;
3806 LTYPE (newTree) = sym->type;
3807 LETYPE (newTree) = sym->etype;
3808 LLVAL (newTree) = 1;
3809 LRVAL (newTree) = 0;
3810 TLVAL (newTree) = 1;
3814 if (!IS_PTR (LTYPE (tree))) {
3815 tree->type = EX_VALUE;
3817 valCastLiteral (LTYPE (tree),
3818 floatFromVal (valFromType (RTYPE (tree))));
3819 TTYPE (tree) = tree->opval.val->type;
3822 tree->values.literalFromCast = 1;
3823 TETYPE (tree) = getSpec (TTYPE (tree));
3827 TTYPE (tree) = LTYPE (tree);
3831 TETYPE (tree) = getSpec (TTYPE (tree));
3835 /*------------------------------------------------------------------*/
3836 /*----------------------------*/
3837 /* logical &&, || */
3838 /*----------------------------*/
3841 /* each must be arithmetic type or be a pointer */
3842 if (!IS_PTR (LTYPE (tree)) &&
3843 !IS_ARRAY (LTYPE (tree)) &&
3844 !IS_INTEGRAL (LTYPE (tree)))
3846 werror (E_COMPARE_OP);
3847 goto errorTreeReturn;
3850 if (!IS_PTR (RTYPE (tree)) &&
3851 !IS_ARRAY (RTYPE (tree)) &&
3852 !IS_INTEGRAL (RTYPE (tree)))
3854 werror (E_COMPARE_OP);
3855 goto errorTreeReturn;
3857 /* if they are both literal then */
3858 /* rewrite the tree */
3859 if (IS_LITERAL (RTYPE (tree)) &&
3860 IS_LITERAL (LTYPE (tree)))
3862 tree->type = EX_VALUE;
3863 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
3864 valFromType (RTYPE (tree)),
3866 tree->right = tree->left = NULL;
3867 TETYPE (tree) = getSpec (TTYPE (tree) =
3868 tree->opval.val->type);
3871 LRVAL (tree) = RRVAL (tree) = 1;
3872 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3875 /*------------------------------------------------------------------*/
3876 /*----------------------------*/
3877 /* comparison operators */
3878 /*----------------------------*/
3886 ast *lt = optimizeCompare (tree);
3892 /* if they are pointers they must be castable */
3893 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3895 if (tree->opval.op==EQ_OP &&
3896 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
3897 // we cannot cast a gptr to a !gptr: switch the leaves
3898 struct ast *s=tree->left;
3899 tree->left=tree->right;
3902 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3904 werror (E_COMPARE_OP);
3905 fprintf (stderr, "comparing type ");
3906 printTypeChain (LTYPE (tree), stderr);
3907 fprintf (stderr, "to type ");
3908 printTypeChain (RTYPE (tree), stderr);
3909 fprintf (stderr, "\n");
3910 goto errorTreeReturn;
3913 /* else they should be promotable to one another */
3916 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
3917 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
3919 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3921 werror (E_COMPARE_OP);
3922 fprintf (stderr, "comparing type ");
3923 printTypeChain (LTYPE (tree), stderr);
3924 fprintf (stderr, "to type ");
3925 printTypeChain (RTYPE (tree), stderr);
3926 fprintf (stderr, "\n");
3927 goto errorTreeReturn;
3931 /* if unsigned value < 0 then always false */
3932 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
3933 if (SPEC_USIGN(LETYPE(tree)) &&
3934 !IS_CHAR(LETYPE(tree)) && /* promotion to signed int */
3935 IS_LITERAL(RTYPE(tree)) &&
3936 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
3938 if (tree->opval.op == '<')
3942 if (tree->opval.op == '>')
3944 if (resultType == RESULT_TYPE_IFX)
3946 /* the parent is an ifx: */
3947 /* if (unsigned value) */
3951 /* (unsigned value) ? 1 : 0 */
3952 tree->opval.op = '?';
3953 tree->right = newNode (':',
3954 newAst_VALUE (constVal ("1")),
3955 tree->right); /* val 0 */
3956 tree->right->lineno = tree->lineno;
3957 tree->right->left->lineno = tree->lineno;
3958 tree->decorated = 0;
3959 return decorateType (tree, resultType);
3963 /* 'ifx (op == 0)' -> 'ifx (!(op))' */
3964 if (IS_LITERAL(RTYPE(tree)) &&
3965 floatFromVal (valFromType (RETYPE (tree))) == 0 &&
3966 tree->opval.op == EQ_OP &&
3967 resultType == RESULT_TYPE_IFX)
3969 tree->opval.op = '!';
3971 tree->decorated = 0;
3972 return decorateType (tree, resultType);
3975 /* if they are both literal then */
3976 /* rewrite the tree */
3977 if (IS_LITERAL (RTYPE (tree)) &&
3978 IS_LITERAL (LTYPE (tree)))
3980 tree->type = EX_VALUE;
3981 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
3982 valFromType (RETYPE (tree)),
3984 tree->right = tree->left = NULL;
3985 TETYPE (tree) = getSpec (TTYPE (tree) =
3986 tree->opval.val->type);
3989 /* if one is 'signed char ' and the other one is 'unsigned char' */
3990 /* it's necessary to promote to int */
3991 if (IS_CHAR (RTYPE (tree)) && IS_CHAR (LTYPE (tree)) &&
3992 (IS_UNSIGNED (RTYPE (tree)) != IS_UNSIGNED (LTYPE (tree))))
3994 /* Literals are 'optimized' to 'unsigned char'. Try to figure out,
3995 if it's possible to use a 'signed char' */
3997 /* is left a 'unsigned char'? */
3998 if (IS_LITERAL (RTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)) &&
3999 /* the value range of a 'unsigned char' is 0...255;
4000 if the actual value is < 128 it can be changed to signed */
4001 (int) floatFromVal (valFromType (RETYPE (tree))) < 128)
4003 /* now we've got 2 'signed char'! */
4004 SPEC_USIGN (RETYPE (tree)) = 0;
4006 /* same test for the left operand: */
4007 else if (IS_LITERAL (LTYPE (tree)) && IS_UNSIGNED (LTYPE (tree)) &&
4008 (int) floatFromVal (valFromType (LETYPE (tree))) < 128)
4010 SPEC_USIGN (LETYPE (tree)) = 0;
4014 werror (W_CMP_SU_CHAR);
4015 tree->left = addCast (tree->left , RESULT_TYPE_INT, TRUE);
4016 tree->right = addCast (tree->right, RESULT_TYPE_INT, TRUE);
4020 LRVAL (tree) = RRVAL (tree) = 1;
4021 TTYPE (tree) = TETYPE (tree) = newBoolLink ();
4024 /*------------------------------------------------------------------*/
4025 /*----------------------------*/
4027 /*----------------------------*/
4028 case SIZEOF: /* evaluate wihout code generation */
4029 /* change the type to a integer */
4031 int size = getSize (tree->right->ftype);
4032 SNPRINTF(buffer, sizeof(buffer), "%d", size);
4033 if (!size && !IS_VOID(tree->right->ftype))
4034 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
4036 tree->type = EX_VALUE;
4037 tree->opval.val = constVal (buffer);
4038 tree->right = tree->left = NULL;
4039 TETYPE (tree) = getSpec (TTYPE (tree) =
4040 tree->opval.val->type);
4043 /*------------------------------------------------------------------*/
4044 /*----------------------------*/
4046 /*----------------------------*/
4048 /* return typeof enum value */
4049 tree->type = EX_VALUE;
4052 if (IS_SPEC(tree->right->ftype)) {
4053 switch (SPEC_NOUN(tree->right->ftype)) {
4055 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
4056 else typeofv = TYPEOF_INT;
4059 typeofv = TYPEOF_FLOAT;
4062 typeofv = TYPEOF_FIXED16X16;
4065 typeofv = TYPEOF_CHAR;
4068 typeofv = TYPEOF_VOID;
4071 typeofv = TYPEOF_STRUCT;
4074 typeofv = TYPEOF_BITFIELD;
4077 typeofv = TYPEOF_BIT;
4080 typeofv = TYPEOF_SBIT;
4086 switch (DCL_TYPE(tree->right->ftype)) {
4088 typeofv = TYPEOF_POINTER;
4091 typeofv = TYPEOF_FPOINTER;
4094 typeofv = TYPEOF_CPOINTER;
4097 typeofv = TYPEOF_GPOINTER;
4100 typeofv = TYPEOF_PPOINTER;
4103 typeofv = TYPEOF_IPOINTER;
4106 typeofv = TYPEOF_ARRAY;
4109 typeofv = TYPEOF_FUNCTION;
4115 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
4116 tree->opval.val = constVal (buffer);
4117 tree->right = tree->left = NULL;
4118 TETYPE (tree) = getSpec (TTYPE (tree) =
4119 tree->opval.val->type);
4122 /*------------------------------------------------------------------*/
4123 /*----------------------------*/
4124 /* conditional operator '?' */
4125 /*----------------------------*/
4127 /* the type is value of the colon operator (on the right) */
4128 assert (IS_COLON_OP (tree->right));
4129 /* if already known then replace the tree : optimizer will do it
4130 but faster to do it here */
4131 if (IS_LITERAL (LTYPE (tree)))
4133 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
4134 return decorateType (tree->right->left, resultTypeProp);
4136 return decorateType (tree->right->right, resultTypeProp);
4140 tree->right = decorateType (tree->right, resultTypeProp);
4141 TTYPE (tree) = RTYPE (tree);
4142 TETYPE (tree) = getSpec (TTYPE (tree));
4147 /* if they don't match we have a problem */
4148 if ((compareType (LTYPE (tree), RTYPE (tree)) == 0) &&
4149 (compareType (RTYPE (tree), LTYPE (tree)) == 0))
4151 werror (E_TYPE_MISMATCH, "conditional operator", " ");
4152 goto errorTreeReturn;
4155 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
4156 resultType, tree->opval.op);
4157 TETYPE (tree) = getSpec (TTYPE (tree));
4161 #if 0 // assignment operators are converted by the parser
4162 /*------------------------------------------------------------------*/
4163 /*----------------------------*/
4164 /* assignment operators */
4165 /*----------------------------*/
4168 /* for these it must be both must be integral */
4169 if (!IS_ARITHMETIC (LTYPE (tree)) ||
4170 !IS_ARITHMETIC (RTYPE (tree)))
4172 werror (E_OPS_INTEGRAL);
4173 goto errorTreeReturn;
4176 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4178 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
4179 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4183 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4184 goto errorTreeReturn;
4195 /* for these it must be both must be integral */
4196 if (!IS_INTEGRAL (LTYPE (tree)) ||
4197 !IS_INTEGRAL (RTYPE (tree)))
4199 werror (E_OPS_INTEGRAL);
4200 goto errorTreeReturn;
4203 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4205 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4206 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
4210 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
4211 goto errorTreeReturn;
4217 /*------------------------------------------------------------------*/
4218 /*----------------------------*/
4220 /*----------------------------*/
4222 if (!(IS_PTR (LTYPE (tree)) ||
4223 IS_ARITHMETIC (LTYPE (tree))))
4225 werror (E_PLUS_INVALID, "-=");
4226 goto errorTreeReturn;
4229 if (!(IS_PTR (RTYPE (tree)) ||
4230 IS_ARITHMETIC (RTYPE (tree))))
4232 werror (E_PLUS_INVALID, "-=");
4233 goto errorTreeReturn;
4236 TETYPE (tree) = getSpec (TTYPE (tree) =
4237 computeType (LTYPE (tree),
4242 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4243 werror (E_CODE_WRITE, "-=");
4247 werror (E_LVALUE_REQUIRED, "-=");
4248 goto errorTreeReturn;
4254 /*------------------------------------------------------------------*/
4255 /*----------------------------*/
4257 /*----------------------------*/
4259 /* this is not a unary operation */
4260 /* if both pointers then problem */
4261 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4263 werror (E_PTR_PLUS_PTR);
4264 goto errorTreeReturn;
4267 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
4269 werror (E_PLUS_INVALID, "+=");
4270 goto errorTreeReturn;
4273 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
4275 werror (E_PLUS_INVALID, "+=");
4276 goto errorTreeReturn;
4279 TETYPE (tree) = getSpec (TTYPE (tree) =
4280 computeType (LTYPE (tree),
4285 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4286 werror (E_CODE_WRITE, "+=");
4290 werror (E_LVALUE_REQUIRED, "+=");
4291 goto errorTreeReturn;
4294 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_TYPE_NONE);
4295 tree->opval.op = '=';
4300 /*------------------------------------------------------------------*/
4301 /*----------------------------*/
4302 /* straight assignemnt */
4303 /*----------------------------*/
4305 /* cannot be an aggregate */
4306 if (IS_AGGREGATE (LTYPE (tree)))
4308 werror (E_AGGR_ASSIGN);
4309 goto errorTreeReturn;
4312 /* they should either match or be castable */
4313 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4315 werror (E_TYPE_MISMATCH, "assignment", " ");
4316 printFromToType(RTYPE(tree),LTYPE(tree));
4319 /* if the left side of the tree is of type void
4320 then report error */
4321 if (IS_VOID (LTYPE (tree)))
4323 werror (E_CAST_ZERO);
4324 printFromToType(RTYPE(tree), LTYPE(tree));
4327 TETYPE (tree) = getSpec (TTYPE (tree) =
4331 if (!tree->initMode ) {
4332 if (IS_CONSTANT(LTYPE(tree)))
4333 werror (E_CODE_WRITE, "=");
4337 werror (E_LVALUE_REQUIRED, "=");
4338 goto errorTreeReturn;
4343 /*------------------------------------------------------------------*/
4344 /*----------------------------*/
4345 /* comma operator */
4346 /*----------------------------*/
4348 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4351 /*------------------------------------------------------------------*/
4352 /*----------------------------*/
4354 /*----------------------------*/
4357 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4358 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4360 if (tree->left->opval.op == '*' && !tree->left->right)
4361 tree->left = tree->left->left;
4364 /* require a function or pointer to function */
4365 if (!IS_FUNC (LTYPE (tree)) && !IS_FUNCPTR (LTYPE (tree)))
4367 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4368 goto errorTreeReturn;
4371 /* if there are parms, make sure that
4372 parms are decorate / process / reverse only once */
4374 !tree->right->decorated)
4379 if (IS_FUNCPTR (LTYPE (tree)))
4381 functype = LTYPE (tree)->next;
4382 processFuncPtrArgs (functype);
4385 functype = LTYPE (tree);
4387 if (processParms (tree->left, FUNC_ARGS(functype),
4388 &tree->right, &parmNumber, TRUE))
4390 goto errorTreeReturn;
4393 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4394 !IFFUNC_ISBUILTIN(functype))
4396 reverseParms (tree->right);
4399 TTYPE (tree) = functype->next;
4400 TETYPE (tree) = getSpec (TTYPE (tree));
4404 /*------------------------------------------------------------------*/
4405 /*----------------------------*/
4406 /* return statement */
4407 /*----------------------------*/
4412 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4414 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4415 printFromToType (RTYPE(tree), currFunc->type->next);
4416 goto errorTreeReturn;
4419 if (IS_VOID (currFunc->type->next)
4421 !IS_VOID (RTYPE (tree)))
4423 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4424 goto errorTreeReturn;
4427 /* if there is going to be a casting required then add it */
4428 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4431 decorateType (newNode (CAST,
4432 newAst_LINK (copyLinkChain (currFunc->type->next)),
4442 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4444 werror (W_VOID_FUNC, currFunc->name);
4445 goto errorTreeReturn;
4448 TTYPE (tree) = TETYPE (tree) = NULL;
4451 /*------------------------------------------------------------------*/
4452 /*----------------------------*/
4453 /* switch statement */
4454 /*----------------------------*/
4456 /* the switch value must be an integer */
4457 if (!IS_INTEGRAL (LTYPE (tree)))
4459 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4460 goto errorTreeReturn;
4463 TTYPE (tree) = TETYPE (tree) = NULL;
4466 /*------------------------------------------------------------------*/
4467 /*----------------------------*/
4469 /*----------------------------*/
4471 tree->left = backPatchLabels (tree->left,
4474 TTYPE (tree) = TETYPE (tree) = NULL;
4477 /*------------------------------------------------------------------*/
4478 /*----------------------------*/
4480 /*----------------------------*/
4483 decorateType (resolveSymbols (AST_FOR (tree, initExpr)), RESULT_TYPE_NONE);
4484 decorateType (resolveSymbols (AST_FOR (tree, condExpr)), RESULT_TYPE_NONE);
4485 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_TYPE_NONE);
4487 /* if the for loop is reversible then
4488 reverse it otherwise do what we normally
4494 if (isLoopReversible (tree, &sym, &init, &end))
4495 return reverseLoop (tree, sym, init, end);
4497 return decorateType (createFor (AST_FOR (tree, trueLabel),
4498 AST_FOR (tree, continueLabel),
4499 AST_FOR (tree, falseLabel),
4500 AST_FOR (tree, condLabel),
4501 AST_FOR (tree, initExpr),
4502 AST_FOR (tree, condExpr),
4503 AST_FOR (tree, loopExpr),
4504 tree->left), RESULT_TYPE_NONE);
4507 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4508 "node PARAM shouldn't be processed here");
4509 /* but in processParams() */
4512 TTYPE (tree) = TETYPE (tree) = NULL;
4516 /* some error found this tree will be killed */
4518 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4519 tree->opval.op = NULLOP;
4525 /*-----------------------------------------------------------------*/
4526 /* sizeofOp - processes size of operation */
4527 /*-----------------------------------------------------------------*/
4529 sizeofOp (sym_link * type)
4534 /* make sure the type is complete and sane */
4535 checkTypeSanity(type, "(sizeof)");
4537 /* get the size and convert it to character */
4538 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4539 if (!size && !IS_VOID(type))
4540 werror (E_SIZEOF_INCOMPLETE_TYPE);
4542 /* now convert into value */
4543 return constVal (buff);
4547 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4548 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4549 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4550 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4551 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4552 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4553 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4555 /*-----------------------------------------------------------------*/
4556 /* backPatchLabels - change and or not operators to flow control */
4557 /*-----------------------------------------------------------------*/
4559 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4565 if (!(IS_ANDORNOT (tree)))
4568 /* if this an and */
4571 static int localLbl = 0;
4574 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4575 localLabel = newSymbol (buffer, NestLevel);
4577 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4579 /* if left is already a IFX then just change the if true label in that */
4580 if (!IS_IFX (tree->left))
4581 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4583 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4584 /* right is a IFX then just join */
4585 if (IS_IFX (tree->right))
4586 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4588 tree->right = createLabel (localLabel, tree->right);
4589 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4591 return newNode (NULLOP, tree->left, tree->right);
4594 /* if this is an or operation */
4597 static int localLbl = 0;
4600 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4601 localLabel = newSymbol (buffer, NestLevel);
4603 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4605 /* if left is already a IFX then just change the if true label in that */
4606 if (!IS_IFX (tree->left))
4607 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4609 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4610 /* right is a IFX then just join */
4611 if (IS_IFX (tree->right))
4612 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4614 tree->right = createLabel (localLabel, tree->right);
4615 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4617 return newNode (NULLOP, tree->left, tree->right);
4623 /* call with exchanged labels */
4624 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4626 /* if left isn't already a IFX */
4627 if (!IS_IFX (tree->left))
4629 tree->left = newNode (IFX, tree->left, NULL);
4630 tree->left->trueLabel = falseLabel;
4631 tree->left->falseLabel = trueLabel;
4638 tree->trueLabel = trueLabel;
4639 tree->falseLabel = falseLabel;
4646 /*-----------------------------------------------------------------*/
4647 /* createBlock - create expression tree for block */
4648 /*-----------------------------------------------------------------*/
4650 createBlock (symbol * decl, ast * body)
4654 /* if the block has nothing */
4658 ex = newNode (BLOCK, NULL, body);
4659 ex->values.sym = decl;
4666 /*-----------------------------------------------------------------*/
4667 /* createLabel - creates the expression tree for labels */
4668 /*-----------------------------------------------------------------*/
4670 createLabel (symbol * label, ast * stmnt)
4673 char name[SDCC_NAME_MAX + 1];
4676 /* must create fresh symbol if the symbol name */
4677 /* exists in the symbol table, since there can */
4678 /* be a variable with the same name as the labl */
4679 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4680 (csym->level == label->level))
4681 label = newSymbol (label->name, label->level);
4683 /* change the name before putting it in add _ */
4684 SNPRINTF(name, sizeof(name), "%s", label->name);
4686 /* put the label in the LabelSymbol table */
4687 /* but first check if a label of the same */
4689 if ((csym = findSym (LabelTab, NULL, name)))
4690 werror (E_DUPLICATE_LABEL, label->name);
4692 addSym (LabelTab, label, name, label->level, 0, 0);
4696 label->key = labelKey++;
4697 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4703 /*-----------------------------------------------------------------*/
4704 /* createCase - generates the parsetree for a case statement */
4705 /*-----------------------------------------------------------------*/
4707 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4709 char caseLbl[SDCC_NAME_MAX + 1];
4713 /* if the switch statement does not exist */
4714 /* then case is out of context */
4717 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4721 caseVal = decorateType (resolveSymbols (caseVal), RESULT_TYPE_NONE);
4722 /* if not a constant then error */
4723 if (!IS_LITERAL (caseVal->ftype))
4725 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4729 /* if not a integer than error */
4730 if (!IS_INTEGRAL (caseVal->ftype))
4732 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4736 /* find the end of the switch values chain */
4737 if (!(val = swStat->values.switchVals.swVals))
4738 swStat->values.switchVals.swVals = caseVal->opval.val;
4741 /* also order the cases according to value */
4743 int cVal = (int) floatFromVal (caseVal->opval.val);
4744 while (val && (int) floatFromVal (val) < cVal)
4750 /* if we reached the end then */
4753 pval->next = caseVal->opval.val;
4755 else if ((int) floatFromVal (val) == cVal)
4757 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
4763 /* we found a value greater than */
4764 /* the current value we must add this */
4765 /* before the value */
4766 caseVal->opval.val->next = val;
4768 /* if this was the first in chain */
4769 if (swStat->values.switchVals.swVals == val)
4770 swStat->values.switchVals.swVals =
4773 pval->next = caseVal->opval.val;
4778 /* create the case label */
4779 SNPRINTF(caseLbl, sizeof(caseLbl),
4781 swStat->values.switchVals.swNum,
4782 (int) floatFromVal (caseVal->opval.val));
4784 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
4789 /*-----------------------------------------------------------------*/
4790 /* createDefault - creates the parse tree for the default statement */
4791 /*-----------------------------------------------------------------*/
4793 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
4795 char defLbl[SDCC_NAME_MAX + 1];
4797 /* if the switch statement does not exist */
4798 /* then case is out of context */
4801 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
4805 if (swStat->values.switchVals.swDefault)
4807 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
4812 /* turn on the default flag */
4813 swStat->values.switchVals.swDefault = 1;
4815 /* create the label */
4816 SNPRINTF (defLbl, sizeof(defLbl),
4817 "_default_%d", swStat->values.switchVals.swNum);
4818 return createLabel (newSymbol (defLbl, 0), stmnt);
4821 /*-----------------------------------------------------------------*/
4822 /* createIf - creates the parsetree for the if statement */
4823 /*-----------------------------------------------------------------*/
4825 createIf (ast * condAst, ast * ifBody, ast * elseBody)
4827 static int Lblnum = 0;
4829 symbol *ifTrue, *ifFalse, *ifEnd;
4831 /* if neither exists */
4832 if (!elseBody && !ifBody) {
4833 // if there are no side effects (i++, j() etc)
4834 if (!hasSEFcalls(condAst)) {
4839 /* create the labels */
4840 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
4841 ifFalse = newSymbol (buffer, NestLevel);
4842 /* if no else body then end == false */
4847 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
4848 ifEnd = newSymbol (buffer, NestLevel);
4851 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
4852 ifTrue = newSymbol (buffer, NestLevel);
4856 /* attach the ifTrue label to the top of it body */
4857 ifBody = createLabel (ifTrue, ifBody);
4858 /* attach a goto end to the ifBody if else is present */
4861 ifBody = newNode (NULLOP, ifBody,
4863 newAst_VALUE (symbolVal (ifEnd)),
4865 /* put the elseLabel on the else body */
4866 elseBody = createLabel (ifFalse, elseBody);
4867 /* out the end at the end of the body */
4868 elseBody = newNode (NULLOP,
4870 createLabel (ifEnd, NULL));
4874 ifBody = newNode (NULLOP, ifBody,
4875 createLabel (ifFalse, NULL));
4877 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
4878 if (IS_IFX (condAst))
4881 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
4883 return newNode (NULLOP, ifTree,
4884 newNode (NULLOP, ifBody, elseBody));
4888 /*-----------------------------------------------------------------*/
4889 /* createDo - creates parse tree for do */
4892 /* _docontinue_n: */
4893 /* condition_expression +-> trueLabel -> _dobody_n */
4895 /* +-> falseLabel-> _dobreak_n */
4897 /*-----------------------------------------------------------------*/
4899 createDo (symbol * trueLabel, symbol * continueLabel,
4900 symbol * falseLabel, ast * condAst, ast * doBody)
4905 /* if the body does not exist then it is simple */
4908 condAst = backPatchLabels (condAst, continueLabel, NULL);
4909 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
4910 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
4911 doTree->trueLabel = continueLabel;
4912 doTree->falseLabel = NULL;
4916 /* otherwise we have a body */
4917 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
4919 /* attach the body label to the top */
4920 doBody = createLabel (trueLabel, doBody);
4921 /* attach the continue label to end of body */
4922 doBody = newNode (NULLOP, doBody,
4923 createLabel (continueLabel, NULL));
4925 /* now put the break label at the end */
4926 if (IS_IFX (condAst))
4929 doTree = newIfxNode (condAst, trueLabel, falseLabel);
4931 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
4933 /* putting it together */
4934 return newNode (NULLOP, doBody, doTree);
4937 /*-----------------------------------------------------------------*/
4938 /* createFor - creates parse tree for 'for' statement */
4941 /* condExpr +-> trueLabel -> _forbody_n */
4943 /* +-> falseLabel-> _forbreak_n */
4946 /* _forcontinue_n: */
4948 /* goto _forcond_n ; */
4950 /*-----------------------------------------------------------------*/
4952 createFor (symbol * trueLabel, symbol * continueLabel,
4953 symbol * falseLabel, symbol * condLabel,
4954 ast * initExpr, ast * condExpr, ast * loopExpr,
4959 /* if loopexpression not present then we can generate it */
4960 /* the same way as a while */
4962 return newNode (NULLOP, initExpr,
4963 createWhile (trueLabel, continueLabel,
4964 falseLabel, condExpr, forBody));
4965 /* vanilla for statement */
4966 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4968 if (condExpr && !IS_IFX (condExpr))
4969 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
4972 /* attach condition label to condition */
4973 condExpr = createLabel (condLabel, condExpr);
4975 /* attach body label to body */
4976 forBody = createLabel (trueLabel, forBody);
4978 /* attach continue to forLoop expression & attach */
4979 /* goto the forcond @ and of loopExpression */
4980 loopExpr = createLabel (continueLabel,
4984 newAst_VALUE (symbolVal (condLabel)),
4986 /* now start putting them together */
4987 forTree = newNode (NULLOP, initExpr, condExpr);
4988 forTree = newNode (NULLOP, forTree, forBody);
4989 forTree = newNode (NULLOP, forTree, loopExpr);
4990 /* finally add the break label */
4991 forTree = newNode (NULLOP, forTree,
4992 createLabel (falseLabel, NULL));
4996 /*-----------------------------------------------------------------*/
4997 /* createWhile - creates parse tree for while statement */
4998 /* the while statement will be created as follows */
5000 /* _while_continue_n: */
5001 /* condition_expression +-> trueLabel -> _while_boby_n */
5003 /* +-> falseLabel -> _while_break_n */
5004 /* _while_body_n: */
5006 /* goto _while_continue_n */
5007 /* _while_break_n: */
5008 /*-----------------------------------------------------------------*/
5010 createWhile (symbol * trueLabel, symbol * continueLabel,
5011 symbol * falseLabel, ast * condExpr, ast * whileBody)
5015 /* put the continue label */
5016 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
5017 condExpr = createLabel (continueLabel, condExpr);
5018 condExpr->lineno = 0;
5020 /* put the body label in front of the body */
5021 whileBody = createLabel (trueLabel, whileBody);
5022 whileBody->lineno = 0;
5023 /* put a jump to continue at the end of the body */
5024 /* and put break label at the end of the body */
5025 whileBody = newNode (NULLOP,
5028 newAst_VALUE (symbolVal (continueLabel)),
5029 createLabel (falseLabel, NULL)));
5031 /* put it all together */
5032 if (IS_IFX (condExpr))
5033 whileTree = condExpr;
5036 whileTree = newNode (IFX, condExpr, NULL);
5037 /* put the true & false labels in place */
5038 whileTree->trueLabel = trueLabel;
5039 whileTree->falseLabel = falseLabel;
5042 return newNode (NULLOP, whileTree, whileBody);
5045 /*-----------------------------------------------------------------*/
5046 /* isShiftRightLitVal _BitAndLitVal - helper function */
5047 /*-----------------------------------------------------------------*/
5049 isShiftRightLitVal_BitAndLitVal (ast * tree)
5051 /* if this is not a bit and */
5052 if (!IS_BITAND (tree))
5055 /* will look for tree of the form
5056 ( expr >> litval2) & litval1 */
5057 if (!IS_AST_LIT_VALUE (tree->right))
5060 if (!IS_RIGHT_OP (tree->left))
5063 if (!IS_AST_LIT_VALUE (tree->left->right))
5066 return tree->left->left;
5069 /*-----------------------------------------------------------------*/
5070 /* isBitAndPowOf2 - helper function */
5071 /*-----------------------------------------------------------------*/
5073 isBitAndPow2 (ast * tree)
5075 /* if this is not a bit and */
5076 if (!IS_BITAND (tree))
5079 /* will look for tree of the form
5080 ( expr & (1 << litval) */
5081 if (!IS_AST_LIT_VALUE (tree->right))
5084 return powof2 ((TYPE_UDWORD)AST_LIT_VALUE (tree->right));
5087 /*-----------------------------------------------------------------*/
5088 /* optimizeGetHbit - get highest order bit of the expression */
5089 /*-----------------------------------------------------------------*/
5091 optimizeGetHbit (ast * tree, RESULT_TYPE resultType)
5096 expr = isShiftRightLitVal_BitAndLitVal(tree);
5099 if ((AST_LIT_VALUE (tree->right) != 1) ||
5100 ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
5101 (j = (getSize (TTYPE (expr)) * 8 - 1))))
5104 if (!expr && (resultType == RESULT_TYPE_BIT))
5107 if (isBitAndPow2 (tree) != (signed)getSize (TTYPE (expr)) * 8 - 1)
5113 /* make sure the port supports GETHBIT */
5114 if (port->hasExtBitOp
5115 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (expr))))
5118 return decorateType (newNode (GETHBIT, expr, NULL), RESULT_TYPE_NONE);
5121 /*-----------------------------------------------------------------*/
5122 /* optimizeGetAbit - get a single bit of the expression */
5123 /*-----------------------------------------------------------------*/
5125 optimizeGetAbit (ast * tree, RESULT_TYPE resultType)
5130 expr = isShiftRightLitVal_BitAndLitVal(tree);
5133 if (AST_LIT_VALUE (tree->right) != 1)
5135 count = tree->left->right;
5137 if (!expr && (resultType == RESULT_TYPE_BIT))
5139 int p2 = isBitAndPow2 (tree);
5143 count = newAst_VALUE (valueFromLit (p2));
5149 /* make sure the port supports GETABIT */
5150 if (port->hasExtBitOp
5151 && !port->hasExtBitOp(GETABIT, getSize (TTYPE (expr))))
5154 return decorateType (newNode (GETABIT, expr, count), RESULT_TYPE_NONE);
5158 /*-----------------------------------------------------------------*/
5159 /* optimizeGetByte - get a byte of the expression */
5160 /*-----------------------------------------------------------------*/
5162 optimizeGetByte (ast * tree, RESULT_TYPE resultType)
5168 expr = isShiftRightLitVal_BitAndLitVal(tree);
5171 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5172 count = tree->left->right;
5173 if (AST_LIT_VALUE (tree->right) != 0xFF)
5176 if (!expr && resultType == RESULT_TYPE_CHAR)
5178 /* if this is a right shift over a multiple of 8 */
5179 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5181 i = (unsigned int) AST_LIT_VALUE (tree->right);
5182 count = tree->right;
5186 if (!expr || (i == 0) || (i % 8) || (i >= getSize (TTYPE (expr)) * 8))
5189 /* make sure the port supports GETBYTE */
5190 if (port->hasExtBitOp
5191 && !port->hasExtBitOp(GETBYTE, getSize (TTYPE (expr))))
5194 return decorateType (newNode (GETBYTE, expr, count), RESULT_TYPE_NONE);
5197 /*-----------------------------------------------------------------*/
5198 /* optimizeGetWord - get two bytes of the expression */
5199 /*-----------------------------------------------------------------*/
5201 optimizeGetWord (ast * tree, RESULT_TYPE resultType)
5207 expr = isShiftRightLitVal_BitAndLitVal(tree);
5210 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5211 count = tree->left->right;
5212 if (AST_LIT_VALUE (tree->right) != 0xFFFF)
5215 if (!expr && resultType == RESULT_TYPE_INT)
5217 /* if this is a right shift over a multiple of 8 */
5218 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5220 i = (unsigned int) AST_LIT_VALUE (tree->right);
5221 count = tree->right;
5225 if (!expr || (i == 0) || (i % 8) || (i >= (getSize (TTYPE (expr))-1) * 8))
5228 /* make sure the port supports GETWORD */
5229 if (port->hasExtBitOp
5230 && !port->hasExtBitOp(GETWORD, getSize (TTYPE (expr))))
5233 return decorateType (newNode (GETWORD, expr, count), RESULT_TYPE_NONE);
5236 /*-----------------------------------------------------------------*/
5237 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
5238 /*-----------------------------------------------------------------*/
5240 optimizeRRCRLC (ast * root)
5242 /* will look for trees of the form
5243 (?expr << 1) | (?expr >> 7) or
5244 (?expr >> 7) | (?expr << 1) will make that
5245 into a RLC : operation ..
5247 (?expr >> 1) | (?expr << 7) or
5248 (?expr << 7) | (?expr >> 1) will make that
5249 into a RRC operation
5250 note : by 7 I mean (number of bits required to hold the
5252 /* if the root operation is not a | operation then not */
5253 if (!IS_BITOR (root))
5256 /* I have to think of a better way to match patterns this sucks */
5257 /* that aside let's start looking for the first case : I use a
5258 negative check a lot to improve the efficiency */
5259 /* (?expr << 1) | (?expr >> 7) */
5260 if (IS_LEFT_OP (root->left) &&
5261 IS_RIGHT_OP (root->right))
5264 if (!SPEC_USIGN (TETYPE (root->left->left)))
5267 if (!IS_AST_LIT_VALUE (root->left->right) ||
5268 !IS_AST_LIT_VALUE (root->right->right))
5271 /* make sure it is the same expression */
5272 if (!isAstEqual (root->left->left,
5276 if (AST_LIT_VALUE (root->left->right) != 1)
5279 if (AST_LIT_VALUE (root->right->right) !=
5280 (getSize (TTYPE (root->left->left)) * 8 - 1))
5283 /* make sure the port supports RLC */
5284 if (port->hasExtBitOp
5285 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5288 /* whew got the first case : create the AST */
5289 return newNode (RLC, root->left->left, NULL);
5293 /* check for second case */
5294 /* (?expr >> 7) | (?expr << 1) */
5295 if (IS_LEFT_OP (root->right) &&
5296 IS_RIGHT_OP (root->left))
5299 if (!SPEC_USIGN (TETYPE (root->left->left)))
5302 if (!IS_AST_LIT_VALUE (root->left->right) ||
5303 !IS_AST_LIT_VALUE (root->right->right))
5306 /* make sure it is the same symbol */
5307 if (!isAstEqual (root->left->left,
5311 if (AST_LIT_VALUE (root->right->right) != 1)
5314 if (AST_LIT_VALUE (root->left->right) !=
5315 (getSize (TTYPE (root->left->left)) * 8 - 1))
5318 /* make sure the port supports RLC */
5319 if (port->hasExtBitOp
5320 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5323 /* whew got the first case : create the AST */
5324 return newNode (RLC, root->left->left, NULL);
5329 /* third case for RRC */
5330 /* (?symbol >> 1) | (?symbol << 7) */
5331 if (IS_LEFT_OP (root->right) &&
5332 IS_RIGHT_OP (root->left))
5335 if (!SPEC_USIGN (TETYPE (root->left->left)))
5338 if (!IS_AST_LIT_VALUE (root->left->right) ||
5339 !IS_AST_LIT_VALUE (root->right->right))
5342 /* make sure it is the same symbol */
5343 if (!isAstEqual (root->left->left,
5347 if (AST_LIT_VALUE (root->left->right) != 1)
5350 if (AST_LIT_VALUE (root->right->right) !=
5351 (getSize (TTYPE (root->left->left)) * 8 - 1))
5354 /* make sure the port supports RRC */
5355 if (port->hasExtBitOp
5356 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5359 /* whew got the first case : create the AST */
5360 return newNode (RRC, root->left->left, NULL);
5364 /* fourth and last case for now */
5365 /* (?symbol << 7) | (?symbol >> 1) */
5366 if (IS_RIGHT_OP (root->right) &&
5367 IS_LEFT_OP (root->left))
5370 if (!SPEC_USIGN (TETYPE (root->left->left)))
5373 if (!IS_AST_LIT_VALUE (root->left->right) ||
5374 !IS_AST_LIT_VALUE (root->right->right))
5377 /* make sure it is the same symbol */
5378 if (!isAstEqual (root->left->left,
5382 if (AST_LIT_VALUE (root->right->right) != 1)
5385 if (AST_LIT_VALUE (root->left->right) !=
5386 (getSize (TTYPE (root->left->left)) * 8 - 1))
5389 /* make sure the port supports RRC */
5390 if (port->hasExtBitOp
5391 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5394 /* whew got the first case : create the AST */
5395 return newNode (RRC, root->left->left, NULL);
5399 /* not found return root */
5403 /*-----------------------------------------------------------------*/
5404 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
5405 /*-----------------------------------------------------------------*/
5407 optimizeSWAP (ast * root)
5409 /* will look for trees of the form
5410 (?expr << 4) | (?expr >> 4) or
5411 (?expr >> 4) | (?expr << 4) will make that
5412 into a SWAP : operation ..
5413 note : by 4 I mean (number of bits required to hold the
5415 /* if the root operation is not a | operation then not */
5416 if (!IS_BITOR (root))
5419 /* (?expr << 4) | (?expr >> 4) */
5420 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
5421 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
5424 if (!SPEC_USIGN (TETYPE (root->left->left)))
5427 if (!IS_AST_LIT_VALUE (root->left->right) ||
5428 !IS_AST_LIT_VALUE (root->right->right))
5431 /* make sure it is the same expression */
5432 if (!isAstEqual (root->left->left,
5436 if (AST_LIT_VALUE (root->left->right) !=
5437 (getSize (TTYPE (root->left->left)) * 4))
5440 if (AST_LIT_VALUE (root->right->right) !=
5441 (getSize (TTYPE (root->left->left)) * 4))
5444 /* make sure the port supports SWAP */
5445 if (port->hasExtBitOp
5446 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
5449 /* found it : create the AST */
5450 return newNode (SWAP, root->left->left, NULL);
5454 /* not found return root */
5458 /*-----------------------------------------------------------------*/
5459 /* optimizeCompare - optimizes compares for bit variables */
5460 /*-----------------------------------------------------------------*/
5462 optimizeCompare (ast * root)
5464 ast *optExpr = NULL;
5467 unsigned int litValue;
5469 /* if nothing then return nothing */
5473 /* if not a compare op then do leaves */
5474 if (!IS_COMPARE_OP (root))
5476 root->left = optimizeCompare (root->left);
5477 root->right = optimizeCompare (root->right);
5481 /* if left & right are the same then depending
5482 of the operation do */
5483 if (isAstEqual (root->left, root->right))
5485 switch (root->opval.op)
5490 optExpr = newAst_VALUE (constVal ("0"));
5495 optExpr = newAst_VALUE (constVal ("1"));
5499 return decorateType (optExpr, RESULT_TYPE_NONE);
5502 vleft = (root->left->type == EX_VALUE ?
5503 root->left->opval.val : NULL);
5505 vright = (root->right->type == EX_VALUE ?
5506 root->right->opval.val : NULL);
5508 /* if left is a BITVAR in BITSPACE */
5509 /* and right is a LITERAL then opt- */
5510 /* imize else do nothing */
5511 if (vleft && vright &&
5512 IS_BITVAR (vleft->etype) &&
5513 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5514 IS_LITERAL (vright->etype))
5517 /* if right side > 1 then comparison may never succeed */
5518 if ((litValue = (int) floatFromVal (vright)) > 1)
5520 werror (W_BAD_COMPARE);
5526 switch (root->opval.op)
5528 case '>': /* bit value greater than 1 cannot be */
5529 werror (W_BAD_COMPARE);
5533 case '<': /* bit value < 1 means 0 */
5535 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5538 case LE_OP: /* bit value <= 1 means no check */
5539 optExpr = newAst_VALUE (vright);
5542 case GE_OP: /* bit value >= 1 means only check for = */
5544 optExpr = newAst_VALUE (vleft);
5549 { /* literal is zero */
5550 switch (root->opval.op)
5552 case '<': /* bit value < 0 cannot be */
5553 werror (W_BAD_COMPARE);
5557 case '>': /* bit value > 0 means 1 */
5559 optExpr = newAst_VALUE (vleft);
5562 case LE_OP: /* bit value <= 0 means no check */
5563 case GE_OP: /* bit value >= 0 means no check */
5564 werror (W_BAD_COMPARE);
5568 case EQ_OP: /* bit == 0 means ! of bit */
5569 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5573 return decorateType (resolveSymbols (optExpr), RESULT_TYPE_NONE);
5574 } /* end-of-if of BITVAR */
5579 /*-----------------------------------------------------------------*/
5580 /* addSymToBlock : adds the symbol to the first block we find */
5581 /*-----------------------------------------------------------------*/
5583 addSymToBlock (symbol * sym, ast * tree)
5585 /* reached end of tree or a leaf */
5586 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5590 if (IS_AST_OP (tree) &&
5591 tree->opval.op == BLOCK)
5594 symbol *lsym = copySymbol (sym);
5596 lsym->next = AST_VALUES (tree, sym);
5597 AST_VALUES (tree, sym) = lsym;
5601 addSymToBlock (sym, tree->left);
5602 addSymToBlock (sym, tree->right);
5605 /*-----------------------------------------------------------------*/
5606 /* processRegParms - do processing for register parameters */
5607 /*-----------------------------------------------------------------*/
5609 processRegParms (value * args, ast * body)
5613 if (IS_REGPARM (args->etype))
5614 addSymToBlock (args->sym, body);
5619 /*-----------------------------------------------------------------*/
5620 /* resetParmKey - resets the operandkeys for the symbols */
5621 /*-----------------------------------------------------------------*/
5622 DEFSETFUNC (resetParmKey)
5633 /*-----------------------------------------------------------------*/
5634 /* createFunction - This is the key node that calls the iCode for */
5635 /* generating the code for a function. Note code */
5636 /* is generated function by function, later when */
5637 /* add inter-procedural analysis this will change */
5638 /*-----------------------------------------------------------------*/
5640 createFunction (symbol * name, ast * body)
5646 iCode *piCode = NULL;
5648 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5649 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5651 /* if check function return 0 then some problem */
5652 if (checkFunction (name, NULL) == 0)
5655 /* create a dummy block if none exists */
5657 body = newNode (BLOCK, NULL, NULL);
5661 /* check if the function name already in the symbol table */
5662 if ((csym = findSym (SymbolTab, NULL, name->name)))
5665 /* special case for compiler defined functions
5666 we need to add the name to the publics list : this
5667 actually means we are now compiling the compiler
5671 addSet (&publics, name);
5676 addSymChain (&name);
5677 allocVariables (name);
5679 name->lastLine = mylineno;
5682 /* set the stack pointer */
5683 stackPtr = -port->stack.direction * port->stack.call_overhead;
5686 if (IFFUNC_ISISR (name->type))
5687 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5689 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5691 if (options.useXstack)
5692 xstackPtr -= port->stack.direction * port->stack.reent_overhead;
5694 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5697 fetype = getSpec (name->type); /* get the specifier for the function */
5698 /* if this is a reentrant function then */
5699 if (IFFUNC_ISREENT (name->type))
5702 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5704 /* do processing for parameters that are passed in registers */
5705 processRegParms (FUNC_ARGS(name->type), body);
5707 /* set the stack pointer */
5711 /* allocate & autoinit the block variables */
5712 processBlockVars (body, &stack, ALLOCATE);
5714 /* save the stack information */
5715 if (options.useXstack)
5716 name->xstack = SPEC_STAK (fetype) = stack;
5718 name->stack = SPEC_STAK (fetype) = stack;
5720 /* name needs to be mangled */
5721 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5723 body = resolveSymbols (body); /* resolve the symbols */
5724 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5726 ex = newAst_VALUE (symbolVal (name)); /* create name */
5727 ex = newNode (FUNCTION, ex, body);
5728 ex->values.args = FUNC_ARGS(name->type);
5730 if (options.dump_tree) PA(ex);
5733 werror (E_FUNC_NO_CODE, name->name);
5737 /* create the node & generate intermediate code */
5739 codeOutFile = code->oFile;
5740 piCode = iCodeFromAst (ex);
5744 werror (E_FUNC_NO_CODE, name->name);
5748 eBBlockFromiCode (piCode);
5750 /* if there are any statics then do them */
5753 GcurMemmap = statsg;
5754 codeOutFile = statsg->oFile;
5755 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_TYPE_NONE)));
5761 /* dealloc the block variables */
5762 processBlockVars (body, &stack, DEALLOCATE);
5763 outputDebugStackSymbols();
5764 /* deallocate paramaters */
5765 deallocParms (FUNC_ARGS(name->type));
5767 if (IFFUNC_ISREENT (name->type))
5770 /* we are done freeup memory & cleanup */
5772 if (port->reset_labelKey) labelKey = 1;
5774 FUNC_HASBODY(name->type) = 1;
5775 addSet (&operKeyReset, name);
5776 applyToSet (operKeyReset, resetParmKey);
5781 cleanUpLevel (LabelTab, 0);
5782 cleanUpBlock (StructTab, 1);
5783 cleanUpBlock (TypedefTab, 1);
5785 xstack->syms = NULL;
5786 istack->syms = NULL;
5791 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
5792 /*-----------------------------------------------------------------*/
5793 /* ast_print : prints the ast (for debugging purposes) */
5794 /*-----------------------------------------------------------------*/
5796 void ast_print (ast * tree, FILE *outfile, int indent)
5801 /* can print only decorated trees */
5802 if (!tree->decorated) return;
5804 /* if any child is an error | this one is an error do nothing */
5805 if (tree->isError ||
5806 (tree->left && tree->left->isError) ||
5807 (tree->right && tree->right->isError)) {
5808 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
5812 /* print the line */
5813 /* if not block & function */
5814 if (tree->type == EX_OP &&
5815 (tree->opval.op != FUNCTION &&
5816 tree->opval.op != BLOCK &&
5817 tree->opval.op != NULLOP)) {
5820 if (tree->opval.op == FUNCTION) {
5822 value *args=FUNC_ARGS(tree->left->opval.val->type);
5823 fprintf(outfile,"FUNCTION (%s=%p) type (",
5824 tree->left->opval.val->name, tree);
5825 printTypeChain (tree->left->opval.val->type->next,outfile);
5826 fprintf(outfile,") args (");
5829 fprintf (outfile, ", ");
5831 printTypeChain (args ? args->type : NULL, outfile);
5833 args= args ? args->next : NULL;
5835 fprintf(outfile,")\n");
5836 ast_print(tree->left,outfile,indent);
5837 ast_print(tree->right,outfile,indent);
5840 if (tree->opval.op == BLOCK) {
5841 symbol *decls = tree->values.sym;
5842 INDENT(indent,outfile);
5843 fprintf(outfile,"{\n");
5845 INDENT(indent+2,outfile);
5846 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
5847 decls->name, decls);
5848 printTypeChain(decls->type,outfile);
5849 fprintf(outfile,")\n");
5851 decls = decls->next;
5853 ast_print(tree->right,outfile,indent+2);
5854 INDENT(indent,outfile);
5855 fprintf(outfile,"}\n");
5858 if (tree->opval.op == NULLOP) {
5859 ast_print(tree->left,outfile,indent);
5860 ast_print(tree->right,outfile,indent);
5863 INDENT(indent,outfile);
5865 /*------------------------------------------------------------------*/
5866 /*----------------------------*/
5867 /* leaf has been reached */
5868 /*----------------------------*/
5869 /* if this is of type value */
5870 /* just get the type */
5871 if (tree->type == EX_VALUE) {
5873 if (IS_LITERAL (tree->opval.val->etype)) {
5874 fprintf(outfile,"CONSTANT (%p) value = ", tree);
5875 if (SPEC_USIGN (tree->opval.val->etype))
5876 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
5878 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
5879 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
5880 floatFromVal(tree->opval.val));
5881 } else if (tree->opval.val->sym) {
5882 /* if the undefined flag is set then give error message */
5883 if (tree->opval.val->sym->undefined) {
5884 fprintf(outfile,"UNDEFINED SYMBOL ");
5886 fprintf(outfile,"SYMBOL ");
5888 fprintf(outfile,"(%s=%p)",
5889 tree->opval.val->sym->name,tree);
5892 fprintf(outfile," type (");
5893 printTypeChain(tree->ftype,outfile);
5894 fprintf(outfile,")\n");
5896 fprintf(outfile,"\n");
5901 /* if type link for the case of cast */
5902 if (tree->type == EX_LINK) {
5903 fprintf(outfile,"TYPENODE (%p) type = (",tree);
5904 printTypeChain(tree->opval.lnk,outfile);
5905 fprintf(outfile,")\n");
5910 /* depending on type of operator do */
5912 switch (tree->opval.op) {
5913 /*------------------------------------------------------------------*/
5914 /*----------------------------*/
5916 /*----------------------------*/
5918 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
5919 printTypeChain(tree->ftype,outfile);
5920 fprintf(outfile,")\n");
5921 ast_print(tree->left,outfile,indent+2);
5922 ast_print(tree->right,outfile,indent+2);
5925 /*------------------------------------------------------------------*/
5926 /*----------------------------*/
5928 /*----------------------------*/
5930 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
5931 printTypeChain(tree->ftype,outfile);
5932 fprintf(outfile,")\n");
5933 ast_print(tree->left,outfile,indent+2);
5934 ast_print(tree->right,outfile,indent+2);
5937 /*------------------------------------------------------------------*/
5938 /*----------------------------*/
5939 /* struct/union pointer */
5940 /*----------------------------*/
5942 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
5943 printTypeChain(tree->ftype,outfile);
5944 fprintf(outfile,")\n");
5945 ast_print(tree->left,outfile,indent+2);
5946 ast_print(tree->right,outfile,indent+2);
5949 /*------------------------------------------------------------------*/
5950 /*----------------------------*/
5951 /* ++/-- operation */
5952 /*----------------------------*/
5955 fprintf(outfile,"post-");
5957 fprintf(outfile,"pre-");
5958 fprintf(outfile,"INC_OP (%p) type (",tree);
5959 printTypeChain(tree->ftype,outfile);
5960 fprintf(outfile,")\n");
5961 ast_print(tree->left,outfile,indent+2); /* postincrement case */
5962 ast_print(tree->right,outfile,indent+2); /* preincrement case */
5967 fprintf(outfile,"post-");
5969 fprintf(outfile,"pre-");
5970 fprintf(outfile,"DEC_OP (%p) type (",tree);
5971 printTypeChain(tree->ftype,outfile);
5972 fprintf(outfile,")\n");
5973 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
5974 ast_print(tree->right,outfile,indent+2); /* predecrement case */
5977 /*------------------------------------------------------------------*/
5978 /*----------------------------*/
5980 /*----------------------------*/
5983 fprintf(outfile,"& (%p) type (",tree);
5984 printTypeChain(tree->ftype,outfile);
5985 fprintf(outfile,")\n");
5986 ast_print(tree->left,outfile,indent+2);
5987 ast_print(tree->right,outfile,indent+2);
5989 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
5990 printTypeChain(tree->ftype,outfile);
5991 fprintf(outfile,")\n");
5992 ast_print(tree->left,outfile,indent+2);
5993 ast_print(tree->right,outfile,indent+2);
5996 /*----------------------------*/
5998 /*----------------------------*/
6000 fprintf(outfile,"OR (%p) type (",tree);
6001 printTypeChain(tree->ftype,outfile);
6002 fprintf(outfile,")\n");
6003 ast_print(tree->left,outfile,indent+2);
6004 ast_print(tree->right,outfile,indent+2);
6006 /*------------------------------------------------------------------*/
6007 /*----------------------------*/
6009 /*----------------------------*/
6011 fprintf(outfile,"XOR (%p) type (",tree);
6012 printTypeChain(tree->ftype,outfile);
6013 fprintf(outfile,")\n");
6014 ast_print(tree->left,outfile,indent+2);
6015 ast_print(tree->right,outfile,indent+2);
6018 /*------------------------------------------------------------------*/
6019 /*----------------------------*/
6021 /*----------------------------*/
6023 fprintf(outfile,"DIV (%p) type (",tree);
6024 printTypeChain(tree->ftype,outfile);
6025 fprintf(outfile,")\n");
6026 ast_print(tree->left,outfile,indent+2);
6027 ast_print(tree->right,outfile,indent+2);
6029 /*------------------------------------------------------------------*/
6030 /*----------------------------*/
6032 /*----------------------------*/
6034 fprintf(outfile,"MOD (%p) type (",tree);
6035 printTypeChain(tree->ftype,outfile);
6036 fprintf(outfile,")\n");
6037 ast_print(tree->left,outfile,indent+2);
6038 ast_print(tree->right,outfile,indent+2);
6041 /*------------------------------------------------------------------*/
6042 /*----------------------------*/
6043 /* address dereference */
6044 /*----------------------------*/
6045 case '*': /* can be unary : if right is null then unary operation */
6047 fprintf(outfile,"DEREF (%p) type (",tree);
6048 printTypeChain(tree->ftype,outfile);
6049 fprintf(outfile,")\n");
6050 ast_print(tree->left,outfile,indent+2);
6053 /*------------------------------------------------------------------*/
6054 /*----------------------------*/
6055 /* multiplication */
6056 /*----------------------------*/
6057 fprintf(outfile,"MULT (%p) type (",tree);
6058 printTypeChain(tree->ftype,outfile);
6059 fprintf(outfile,")\n");
6060 ast_print(tree->left,outfile,indent+2);
6061 ast_print(tree->right,outfile,indent+2);
6065 /*------------------------------------------------------------------*/
6066 /*----------------------------*/
6067 /* unary '+' operator */
6068 /*----------------------------*/
6072 fprintf(outfile,"UPLUS (%p) type (",tree);
6073 printTypeChain(tree->ftype,outfile);
6074 fprintf(outfile,")\n");
6075 ast_print(tree->left,outfile,indent+2);
6077 /*------------------------------------------------------------------*/
6078 /*----------------------------*/
6080 /*----------------------------*/
6081 fprintf(outfile,"ADD (%p) type (",tree);
6082 printTypeChain(tree->ftype,outfile);
6083 fprintf(outfile,")\n");
6084 ast_print(tree->left,outfile,indent+2);
6085 ast_print(tree->right,outfile,indent+2);
6088 /*------------------------------------------------------------------*/
6089 /*----------------------------*/
6091 /*----------------------------*/
6092 case '-': /* can be unary */
6094 fprintf(outfile,"UMINUS (%p) type (",tree);
6095 printTypeChain(tree->ftype,outfile);
6096 fprintf(outfile,")\n");
6097 ast_print(tree->left,outfile,indent+2);
6099 /*------------------------------------------------------------------*/
6100 /*----------------------------*/
6102 /*----------------------------*/
6103 fprintf(outfile,"SUB (%p) type (",tree);
6104 printTypeChain(tree->ftype,outfile);
6105 fprintf(outfile,")\n");
6106 ast_print(tree->left,outfile,indent+2);
6107 ast_print(tree->right,outfile,indent+2);
6110 /*------------------------------------------------------------------*/
6111 /*----------------------------*/
6113 /*----------------------------*/
6115 fprintf(outfile,"COMPL (%p) type (",tree);
6116 printTypeChain(tree->ftype,outfile);
6117 fprintf(outfile,")\n");
6118 ast_print(tree->left,outfile,indent+2);
6120 /*------------------------------------------------------------------*/
6121 /*----------------------------*/
6123 /*----------------------------*/
6125 fprintf(outfile,"NOT (%p) type (",tree);
6126 printTypeChain(tree->ftype,outfile);
6127 fprintf(outfile,")\n");
6128 ast_print(tree->left,outfile,indent+2);
6130 /*------------------------------------------------------------------*/
6131 /*----------------------------*/
6133 /*----------------------------*/
6135 fprintf(outfile,"RRC (%p) type (",tree);
6136 printTypeChain(tree->ftype,outfile);
6137 fprintf(outfile,")\n");
6138 ast_print(tree->left,outfile,indent+2);
6142 fprintf(outfile,"RLC (%p) type (",tree);
6143 printTypeChain(tree->ftype,outfile);
6144 fprintf(outfile,")\n");
6145 ast_print(tree->left,outfile,indent+2);
6148 fprintf(outfile,"SWAP (%p) type (",tree);
6149 printTypeChain(tree->ftype,outfile);
6150 fprintf(outfile,")\n");
6151 ast_print(tree->left,outfile,indent+2);
6154 fprintf(outfile,"GETHBIT (%p) type (",tree);
6155 printTypeChain(tree->ftype,outfile);
6156 fprintf(outfile,")\n");
6157 ast_print(tree->left,outfile,indent+2);
6160 fprintf(outfile,"GETABIT (%p) type (",tree);
6161 printTypeChain(tree->ftype,outfile);
6162 fprintf(outfile,")\n");
6163 ast_print(tree->left,outfile,indent+2);
6164 ast_print(tree->right,outfile,indent+2);
6167 fprintf(outfile,"GETBYTE (%p) type (",tree);
6168 printTypeChain(tree->ftype,outfile);
6169 fprintf(outfile,")\n");
6170 ast_print(tree->left,outfile,indent+2);
6171 ast_print(tree->right,outfile,indent+2);
6174 fprintf(outfile,"GETWORD (%p) type (",tree);
6175 printTypeChain(tree->ftype,outfile);
6176 fprintf(outfile,")\n");
6177 ast_print(tree->left,outfile,indent+2);
6178 ast_print(tree->right,outfile,indent+2);
6181 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
6182 printTypeChain(tree->ftype,outfile);
6183 fprintf(outfile,")\n");
6184 ast_print(tree->left,outfile,indent+2);
6185 ast_print(tree->right,outfile,indent+2);
6188 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
6189 printTypeChain(tree->ftype,outfile);
6190 fprintf(outfile,")\n");
6191 ast_print(tree->left,outfile,indent+2);
6192 ast_print(tree->right,outfile,indent+2);
6194 /*------------------------------------------------------------------*/
6195 /*----------------------------*/
6197 /*----------------------------*/
6198 case CAST: /* change the type */
6199 fprintf(outfile,"CAST (%p) from type (",tree);
6200 printTypeChain(tree->right->ftype,outfile);
6201 fprintf(outfile,") to type (");
6202 printTypeChain(tree->ftype,outfile);
6203 fprintf(outfile,")\n");
6204 ast_print(tree->right,outfile,indent+2);
6208 fprintf(outfile,"ANDAND (%p) type (",tree);
6209 printTypeChain(tree->ftype,outfile);
6210 fprintf(outfile,")\n");
6211 ast_print(tree->left,outfile,indent+2);
6212 ast_print(tree->right,outfile,indent+2);
6215 fprintf(outfile,"OROR (%p) type (",tree);
6216 printTypeChain(tree->ftype,outfile);
6217 fprintf(outfile,")\n");
6218 ast_print(tree->left,outfile,indent+2);
6219 ast_print(tree->right,outfile,indent+2);
6222 /*------------------------------------------------------------------*/
6223 /*----------------------------*/
6224 /* comparison operators */
6225 /*----------------------------*/
6227 fprintf(outfile,"GT(>) (%p) type (",tree);
6228 printTypeChain(tree->ftype,outfile);
6229 fprintf(outfile,")\n");
6230 ast_print(tree->left,outfile,indent+2);
6231 ast_print(tree->right,outfile,indent+2);
6234 fprintf(outfile,"LT(<) (%p) type (",tree);
6235 printTypeChain(tree->ftype,outfile);
6236 fprintf(outfile,")\n");
6237 ast_print(tree->left,outfile,indent+2);
6238 ast_print(tree->right,outfile,indent+2);
6241 fprintf(outfile,"LE(<=) (%p) type (",tree);
6242 printTypeChain(tree->ftype,outfile);
6243 fprintf(outfile,")\n");
6244 ast_print(tree->left,outfile,indent+2);
6245 ast_print(tree->right,outfile,indent+2);
6248 fprintf(outfile,"GE(>=) (%p) type (",tree);
6249 printTypeChain(tree->ftype,outfile);
6250 fprintf(outfile,")\n");
6251 ast_print(tree->left,outfile,indent+2);
6252 ast_print(tree->right,outfile,indent+2);
6255 fprintf(outfile,"EQ(==) (%p) type (",tree);
6256 printTypeChain(tree->ftype,outfile);
6257 fprintf(outfile,")\n");
6258 ast_print(tree->left,outfile,indent+2);
6259 ast_print(tree->right,outfile,indent+2);
6262 fprintf(outfile,"NE(!=) (%p) type (",tree);
6263 printTypeChain(tree->ftype,outfile);
6264 fprintf(outfile,")\n");
6265 ast_print(tree->left,outfile,indent+2);
6266 ast_print(tree->right,outfile,indent+2);
6267 /*------------------------------------------------------------------*/
6268 /*----------------------------*/
6270 /*----------------------------*/
6271 case SIZEOF: /* evaluate wihout code generation */
6272 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
6275 /*------------------------------------------------------------------*/
6276 /*----------------------------*/
6277 /* conditional operator '?' */
6278 /*----------------------------*/
6280 fprintf(outfile,"QUEST(?) (%p) type (",tree);
6281 printTypeChain(tree->ftype,outfile);
6282 fprintf(outfile,")\n");
6283 ast_print(tree->left,outfile,indent+2);
6284 ast_print(tree->right,outfile,indent+2);
6288 fprintf(outfile,"COLON(:) (%p) type (",tree);
6289 printTypeChain(tree->ftype,outfile);
6290 fprintf(outfile,")\n");
6291 ast_print(tree->left,outfile,indent+2);
6292 ast_print(tree->right,outfile,indent+2);
6295 /*------------------------------------------------------------------*/
6296 /*----------------------------*/
6297 /* assignment operators */
6298 /*----------------------------*/
6300 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
6301 printTypeChain(tree->ftype,outfile);
6302 fprintf(outfile,")\n");
6303 ast_print(tree->left,outfile,indent+2);
6304 ast_print(tree->right,outfile,indent+2);
6307 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
6308 printTypeChain(tree->ftype,outfile);
6309 fprintf(outfile,")\n");
6310 ast_print(tree->left,outfile,indent+2);
6311 ast_print(tree->right,outfile,indent+2);
6314 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
6315 printTypeChain(tree->ftype,outfile);
6316 fprintf(outfile,")\n");
6317 ast_print(tree->left,outfile,indent+2);
6318 ast_print(tree->right,outfile,indent+2);
6321 fprintf(outfile,"ORASS(|=) (%p) type (",tree);
6322 printTypeChain(tree->ftype,outfile);
6323 fprintf(outfile,")\n");
6324 ast_print(tree->left,outfile,indent+2);
6325 ast_print(tree->right,outfile,indent+2);
6328 fprintf(outfile,"XORASS(^=) (%p) type (",tree);
6329 printTypeChain(tree->ftype,outfile);
6330 fprintf(outfile,")\n");
6331 ast_print(tree->left,outfile,indent+2);
6332 ast_print(tree->right,outfile,indent+2);
6335 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
6336 printTypeChain(tree->ftype,outfile);
6337 fprintf(outfile,")\n");
6338 ast_print(tree->left,outfile,indent+2);
6339 ast_print(tree->right,outfile,indent+2);
6342 fprintf(outfile,"LSHFTASS(<<=) (%p) type (",tree);
6343 printTypeChain(tree->ftype,outfile);
6344 fprintf(outfile,")\n");
6345 ast_print(tree->left,outfile,indent+2);
6346 ast_print(tree->right,outfile,indent+2);
6348 /*------------------------------------------------------------------*/
6349 /*----------------------------*/
6351 /*----------------------------*/
6353 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
6354 printTypeChain(tree->ftype,outfile);
6355 fprintf(outfile,")\n");
6356 ast_print(tree->left,outfile,indent+2);
6357 ast_print(tree->right,outfile,indent+2);
6359 /*------------------------------------------------------------------*/
6360 /*----------------------------*/
6362 /*----------------------------*/
6364 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
6365 printTypeChain(tree->ftype,outfile);
6366 fprintf(outfile,")\n");
6367 ast_print(tree->left,outfile,indent+2);
6368 ast_print(tree->right,outfile,indent+2);
6370 /*------------------------------------------------------------------*/
6371 /*----------------------------*/
6372 /* straight assignemnt */
6373 /*----------------------------*/
6375 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
6376 printTypeChain(tree->ftype,outfile);
6377 fprintf(outfile,")\n");
6378 ast_print(tree->left,outfile,indent+2);
6379 ast_print(tree->right,outfile,indent+2);
6381 /*------------------------------------------------------------------*/
6382 /*----------------------------*/
6383 /* comma operator */
6384 /*----------------------------*/
6386 fprintf(outfile,"COMMA(,) (%p) type (",tree);
6387 printTypeChain(tree->ftype,outfile);
6388 fprintf(outfile,")\n");
6389 ast_print(tree->left,outfile,indent+2);
6390 ast_print(tree->right,outfile,indent+2);
6392 /*------------------------------------------------------------------*/
6393 /*----------------------------*/
6395 /*----------------------------*/
6398 fprintf(outfile,"CALL (%p) type (",tree);
6399 printTypeChain(tree->ftype,outfile);
6400 fprintf(outfile,")\n");
6401 ast_print(tree->left,outfile,indent+2);
6402 ast_print(tree->right,outfile,indent+2);
6405 fprintf(outfile,"PARMS\n");
6406 ast_print(tree->left,outfile,indent+2);
6407 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
6408 ast_print(tree->right,outfile,indent+2);
6411 /*------------------------------------------------------------------*/
6412 /*----------------------------*/
6413 /* return statement */
6414 /*----------------------------*/
6416 fprintf(outfile,"RETURN (%p) type (",tree);
6418 printTypeChain(tree->right->ftype,outfile);
6420 fprintf(outfile,")\n");
6421 ast_print(tree->right,outfile,indent+2);
6423 /*------------------------------------------------------------------*/
6424 /*----------------------------*/
6425 /* label statement */
6426 /*----------------------------*/
6428 fprintf(outfile,"LABEL (%p)\n",tree);
6429 ast_print(tree->left,outfile,indent+2);
6430 ast_print(tree->right,outfile,indent);
6432 /*------------------------------------------------------------------*/
6433 /*----------------------------*/
6434 /* switch statement */
6435 /*----------------------------*/
6439 fprintf(outfile,"SWITCH (%p) ",tree);
6440 ast_print(tree->left,outfile,0);
6441 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
6442 INDENT(indent+2,outfile);
6443 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
6444 (int) floatFromVal(val),
6445 tree->values.switchVals.swNum,
6446 (int) floatFromVal(val));
6448 ast_print(tree->right,outfile,indent);
6451 /*------------------------------------------------------------------*/
6452 /*----------------------------*/
6454 /*----------------------------*/
6456 fprintf(outfile,"IF (%p) \n",tree);
6457 ast_print(tree->left,outfile,indent+2);
6458 if (tree->trueLabel) {
6459 INDENT(indent+2,outfile);
6460 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
6462 if (tree->falseLabel) {
6463 INDENT(indent+2,outfile);
6464 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
6466 ast_print(tree->right,outfile,indent+2);
6468 /*----------------------------*/
6469 /* goto Statement */
6470 /*----------------------------*/
6472 fprintf(outfile,"GOTO (%p) \n",tree);
6473 ast_print(tree->left,outfile,indent+2);
6474 fprintf(outfile,"\n");
6476 /*------------------------------------------------------------------*/
6477 /*----------------------------*/
6479 /*----------------------------*/
6481 fprintf(outfile,"FOR (%p) \n",tree);
6482 if (AST_FOR( tree, initExpr)) {
6483 INDENT(indent+2,outfile);
6484 fprintf(outfile,"INIT EXPR ");
6485 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6487 if (AST_FOR( tree, condExpr)) {
6488 INDENT(indent+2,outfile);
6489 fprintf(outfile,"COND EXPR ");
6490 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6492 if (AST_FOR( tree, loopExpr)) {
6493 INDENT(indent+2,outfile);
6494 fprintf(outfile,"LOOP EXPR ");
6495 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6497 fprintf(outfile,"FOR LOOP BODY \n");
6498 ast_print(tree->left,outfile,indent+2);
6501 fprintf(outfile,"CRITICAL (%p) \n",tree);
6502 ast_print(tree->left,outfile,indent+2);
6510 ast_print(t,stdout,0);
6515 /*-----------------------------------------------------------------*/
6516 /* astErrors : returns non-zero if errors present in tree */
6517 /*-----------------------------------------------------------------*/
6518 int astErrors(ast *t)
6527 if (t->type == EX_VALUE
6528 && t->opval.val->sym
6529 && t->opval.val->sym->undefined)
6532 errors += astErrors(t->left);
6533 errors += astErrors(t->right);