1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
25 #define DEBUG_CF(x) /* puts(x); */
31 set *operKeyReset = NULL;
32 ast *staticAutos = NULL;
35 #define LRVAL(x) x->left->rvalue
36 #define RRVAL(x) x->right->rvalue
37 #define TRVAL(x) x->rvalue
38 #define LLVAL(x) x->left->lvalue
39 #define RLVAL(x) x->right->lvalue
40 #define TLVAL(x) x->lvalue
41 #define RTYPE(x) x->right->ftype
42 #define RETYPE(x) x->right->etype
43 #define LTYPE(x) x->left->ftype
44 #define LETYPE(x) x->left->etype
45 #define TTYPE(x) x->ftype
46 #define TETYPE(x) x->etype
52 symbol *currFunc=NULL;
53 static ast *createIval (ast *, sym_link *, initList *, ast *);
54 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
55 static ast *optimizeCompare (ast *);
56 ast *optimizeRRCRLC (ast *);
57 ast *optimizeSWAP (ast *);
58 ast *optimizeGetHbit (ast *, RESULT_TYPE);
59 ast *optimizeGetAbit (ast *, RESULT_TYPE);
60 ast *optimizeGetByte (ast *, RESULT_TYPE);
61 ast *optimizeGetWord (ast *, RESULT_TYPE);
62 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)
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, iloop, rast)), RESULT_TYPE_NONE);
895 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
896 W_EXCESS_INITIALIZERS, "struct",
897 sym->opval.val->sym->name);
904 /*-----------------------------------------------------------------*/
905 /* createIvalArray - generates code for array initialization */
906 /*-----------------------------------------------------------------*/
908 createIvalArray (ast * sym, sym_link * type, initList * ilist)
912 int lcnt = 0, size = 0;
913 literalList *literalL;
915 /* take care of the special case */
916 /* array of characters can be init */
918 if (IS_CHAR (type->next))
919 if ((rast = createIvalCharPtr (sym,
921 decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE))))
923 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
925 /* not the special case */
926 if (ilist->type != INIT_DEEP)
928 werror (E_INIT_STRUCT, "");
932 iloop = ilist->init.deep;
933 lcnt = DCL_ELEM (type);
935 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
939 aSym = decorateType (resolveSymbols(sym), RESULT_TYPE_NONE);
941 rast = newNode(ARRAYINIT, aSym, NULL);
942 rast->values.constlist = literalL;
944 // Make sure size is set to length of initializer list.
951 if (lcnt && size > lcnt)
953 // Array size was specified, and we have more initializers than needed.
954 char *name=sym->opval.val->sym->name;
955 int lineno=sym->opval.val->sym->lineDef;
956 char *filename=sym->opval.val->sym->fileDef;
958 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
967 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
968 aSym = decorateType (resolveSymbols (aSym), RESULT_TYPE_NONE);
969 rast = createIval (aSym, type->next, iloop, rast);
970 iloop = (iloop ? iloop->next : NULL);
976 /* no of elements given and we */
977 /* have generated for all of them */
980 // is this a better way? at least it won't crash
981 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
982 int lineno = iloop->lineno;
983 char *filename = iloop->filename;
984 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
991 /* if we have not been given a size */
992 if (!DCL_ELEM (type))
994 DCL_ELEM (type) = size;
997 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1001 /*-----------------------------------------------------------------*/
1002 /* createIvalCharPtr - generates initial values for char pointers */
1003 /*-----------------------------------------------------------------*/
1005 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr)
1009 /* if this is a pointer & right is a literal array then */
1010 /* just assignment will do */
1011 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
1012 SPEC_SCLS (iexpr->etype) == S_CODE)
1013 && IS_ARRAY (iexpr->ftype)))
1014 return newNode ('=', sym, iexpr);
1016 /* left side is an array so we have to assign each */
1018 if ((IS_LITERAL (iexpr->etype) ||
1019 SPEC_SCLS (iexpr->etype) == S_CODE)
1020 && IS_ARRAY (iexpr->ftype))
1022 /* for each character generate an assignment */
1023 /* to the array element */
1024 char *s = SPEC_CVAL (iexpr->etype).v_char;
1026 int size = getSize (iexpr->ftype);
1027 int symsize = getSize (type);
1031 if (size>(symsize+1))
1033 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
1035 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1041 for (i=0;i<size;i++)
1043 rast = newNode (NULLOP,
1047 newAst_VALUE (valueFromLit ((float) i))),
1048 newAst_VALUE (valueFromLit (*s))));
1052 // now WE don't need iexpr's symbol anymore
1053 freeStringSymbol(AST_SYMBOL(iexpr));
1055 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1061 /*-----------------------------------------------------------------*/
1062 /* createIvalPtr - generates initial value for pointers */
1063 /*-----------------------------------------------------------------*/
1065 createIvalPtr (ast * sym, sym_link * type, initList * ilist)
1071 if (ilist->type == INIT_DEEP)
1072 ilist = ilist->init.deep;
1074 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
1076 /* if character pointer */
1077 if (IS_CHAR (type->next))
1078 if ((rast = createIvalCharPtr (sym, type, iexpr)))
1081 return newNode ('=', sym, iexpr);
1084 /*-----------------------------------------------------------------*/
1085 /* createIval - generates code for initial value */
1086 /*-----------------------------------------------------------------*/
1088 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
1095 /* if structure then */
1096 if (IS_STRUCT (type))
1097 rast = createIvalStruct (sym, type, ilist);
1099 /* if this is a pointer */
1101 rast = createIvalPtr (sym, type, ilist);
1103 /* if this is an array */
1104 if (IS_ARRAY (type))
1105 rast = createIvalArray (sym, type, ilist);
1107 /* if type is SPECIFIER */
1109 rast = createIvalType (sym, type, ilist);
1112 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_TYPE_NONE);
1114 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1117 /*-----------------------------------------------------------------*/
1118 /* initAggregates - initialises aggregate variables with initv */
1119 /*-----------------------------------------------------------------*/
1120 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1121 return createIval (newAst_VALUE (symbolVal (sym)), sym->type, ival, wid);
1124 /*-----------------------------------------------------------------*/
1125 /* gatherAutoInit - creates assignment expressions for initial */
1127 /*-----------------------------------------------------------------*/
1129 gatherAutoInit (symbol * autoChain)
1136 for (sym = autoChain; sym; sym = sym->next)
1139 /* resolve the symbols in the ival */
1141 resolveIvalSym (sym->ival, sym->type);
1144 /* if we are PIC16 port,
1145 * and this is a static,
1146 * and have initial value,
1147 * and not S_CODE, don't emit in gs segment,
1148 * but allow glue.c:pic16emitRegularMap to put symbol
1149 * in idata section */
1150 if(TARGET_IS_PIC16 &&
1151 IS_STATIC (sym->etype) && sym->ival
1152 && SPEC_SCLS(sym->etype) != S_CODE) {
1153 SPEC_SCLS (sym->etype) = S_DATA;
1158 /* if this is a static variable & has an */
1159 /* initial value the code needs to be lifted */
1160 /* here to the main portion since they can be */
1161 /* initialised only once at the start */
1162 if (IS_STATIC (sym->etype) && sym->ival &&
1163 SPEC_SCLS (sym->etype) != S_CODE)
1167 /* insert the symbol into the symbol table */
1168 /* with level = 0 & name = rname */
1169 newSym = copySymbol (sym);
1170 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1172 /* now lift the code to main */
1173 if (IS_AGGREGATE (sym->type)) {
1174 work = initAggregates (sym, sym->ival, NULL);
1176 if (getNelements(sym->type, sym->ival)>1) {
1177 werrorfl (sym->fileDef, sym->lineDef,
1178 W_EXCESS_INITIALIZERS, "scalar",
1181 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1182 list2expr (sym->ival));
1185 setAstLineno (work, sym->lineDef);
1189 staticAutos = newNode (NULLOP, staticAutos, work);
1196 /* if there is an initial value */
1197 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1199 initList *ilist=sym->ival;
1201 while (ilist->type == INIT_DEEP) {
1202 ilist = ilist->init.deep;
1205 /* update lineno for error msg */
1206 lineno=sym->lineDef;
1207 setAstLineno (ilist->init.node, lineno);
1209 if (IS_AGGREGATE (sym->type)) {
1210 work = initAggregates (sym, sym->ival, NULL);
1212 if (getNelements(sym->type, sym->ival)>1) {
1213 werrorfl (sym->fileDef, sym->lineDef,
1214 W_EXCESS_INITIALIZERS, "scalar",
1217 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1218 list2expr (sym->ival));
1222 setAstLineno (work, sym->lineDef);
1226 init = newNode (NULLOP, init, work);
1235 /*-----------------------------------------------------------------*/
1236 /* freeStringSymbol - delete a literal string if no more usage */
1237 /*-----------------------------------------------------------------*/
1238 void freeStringSymbol(symbol *sym) {
1239 /* make sure this is a literal string */
1240 assert (sym->isstrlit);
1241 if (--sym->isstrlit == 0) { // lower the usage count
1242 memmap *segment=SPEC_OCLS(sym->etype);
1244 deleteSetItem(&segment->syms, sym);
1249 /*-----------------------------------------------------------------*/
1250 /* stringToSymbol - creates a symbol from a literal string */
1251 /*-----------------------------------------------------------------*/
1253 stringToSymbol (value * val)
1255 char name[SDCC_NAME_MAX + 1];
1256 static int charLbl = 0;
1261 // have we heard this before?
1262 for (sp=statsg->syms; sp; sp=sp->next) {
1264 size = getSize (sym->type);
1265 if (sym->isstrlit && size == getSize (val->type) &&
1266 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1267 // yes, this is old news. Don't publish it again.
1268 sym->isstrlit++; // but raise the usage count
1269 return symbolVal(sym);
1273 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1274 sym = newSymbol (name, 0); /* make it @ level 0 */
1275 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1277 /* copy the type from the value passed */
1278 sym->type = copyLinkChain (val->type);
1279 sym->etype = getSpec (sym->type);
1280 /* change to storage class & output class */
1281 SPEC_SCLS (sym->etype) = S_CODE;
1282 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1283 SPEC_STAT (sym->etype) = 1;
1284 /* make the level & block = 0 */
1285 sym->block = sym->level = 0;
1287 /* create an ival */
1288 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1293 allocVariables (sym);
1296 return symbolVal (sym);
1300 /*-----------------------------------------------------------------*/
1301 /* processBlockVars - will go thru the ast looking for block if */
1302 /* a block is found then will allocate the syms */
1303 /* will also gather the auto inits present */
1304 /*-----------------------------------------------------------------*/
1306 processBlockVars (ast * tree, int *stack, int action)
1311 /* if this is a block */
1312 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1316 if (action == ALLOCATE)
1318 *stack += allocVariables (tree->values.sym);
1319 autoInit = gatherAutoInit (tree->values.sym);
1321 /* if there are auto inits then do them */
1323 tree->left = newNode (NULLOP, autoInit, tree->left);
1325 else /* action is deallocate */
1326 deallocLocal (tree->values.sym);
1329 processBlockVars (tree->left, stack, action);
1330 processBlockVars (tree->right, stack, action);
1335 /*-------------------------------------------------------------*/
1336 /* constExprTree - returns TRUE if this tree is a constant */
1338 /*-------------------------------------------------------------*/
1339 bool constExprTree (ast *cexpr) {
1345 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1347 switch (cexpr->type)
1350 if (IS_AST_LIT_VALUE(cexpr)) {
1351 // this is a literal
1354 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1355 // a function's address will never change
1358 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1359 // an array's address will never change
1362 if (IS_AST_SYM_VALUE(cexpr) &&
1363 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1364 // a symbol in code space will never change
1365 // This is only for the 'char *s="hallo"' case and will have to leave
1366 //printf(" code space symbol");
1371 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1372 "unexpected link in expression tree\n");
1375 if (cexpr->opval.op==ARRAYINIT) {
1376 // this is a list of literals
1379 if (cexpr->opval.op=='=') {
1380 return constExprTree(cexpr->right);
1382 if (cexpr->opval.op==CAST) {
1383 // cast ignored, maybe we should throw a warning here?
1384 return constExprTree(cexpr->right);
1386 if (cexpr->opval.op=='&') {
1389 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1392 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1397 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1402 /*-----------------------------------------------------------------*/
1403 /* constExprValue - returns the value of a constant expression */
1404 /* or NULL if it is not a constant expression */
1405 /*-----------------------------------------------------------------*/
1407 constExprValue (ast * cexpr, int check)
1409 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1411 /* if this is not a constant then */
1412 if (!IS_LITERAL (cexpr->ftype))
1414 /* then check if this is a literal array
1416 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1417 SPEC_CVAL (cexpr->etype).v_char &&
1418 IS_ARRAY (cexpr->ftype))
1420 value *val = valFromType (cexpr->ftype);
1421 SPEC_SCLS (val->etype) = S_LITERAL;
1422 val->sym = cexpr->opval.val->sym;
1423 val->sym->type = copyLinkChain (cexpr->ftype);
1424 val->sym->etype = getSpec (val->sym->type);
1425 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1429 /* if we are casting a literal value then */
1430 if (IS_AST_OP (cexpr) &&
1431 cexpr->opval.op == CAST &&
1432 IS_LITERAL (cexpr->right->ftype))
1434 return valCastLiteral (cexpr->ftype,
1435 floatFromVal (cexpr->right->opval.val));
1438 if (IS_AST_VALUE (cexpr))
1440 return cexpr->opval.val;
1444 werror (E_CONST_EXPECTED, "found expression");
1449 /* return the value */
1450 return cexpr->opval.val;
1454 /*-----------------------------------------------------------------*/
1455 /* isLabelInAst - will return true if a given label is found */
1456 /*-----------------------------------------------------------------*/
1458 isLabelInAst (symbol * label, ast * tree)
1460 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1463 if (IS_AST_OP (tree) &&
1464 tree->opval.op == LABEL &&
1465 isSymbolEqual (AST_SYMBOL (tree->left), label))
1468 return isLabelInAst (label, tree->right) &&
1469 isLabelInAst (label, tree->left);
1473 /*-----------------------------------------------------------------*/
1474 /* isLoopCountable - return true if the loop count can be determi- */
1475 /* -ned at compile time . */
1476 /*-----------------------------------------------------------------*/
1478 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1479 symbol ** sym, ast ** init, ast ** end)
1482 /* the loop is considered countable if the following
1483 conditions are true :-
1485 a) initExpr :- <sym> = <const>
1486 b) condExpr :- <sym> < <const1>
1487 c) loopExpr :- <sym> ++
1490 /* first check the initExpr */
1491 if (IS_AST_OP (initExpr) &&
1492 initExpr->opval.op == '=' && /* is assignment */
1493 IS_AST_SYM_VALUE (initExpr->left))
1494 { /* left is a symbol */
1496 *sym = AST_SYMBOL (initExpr->left);
1497 *init = initExpr->right;
1502 /* for now the symbol has to be of
1504 if (!IS_INTEGRAL ((*sym)->type))
1507 /* now check condExpr */
1508 if (IS_AST_OP (condExpr))
1511 switch (condExpr->opval.op)
1514 if (IS_AST_SYM_VALUE (condExpr->left) &&
1515 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1516 IS_AST_LIT_VALUE (condExpr->right))
1518 *end = condExpr->right;
1524 if (IS_AST_OP (condExpr->left) &&
1525 condExpr->left->opval.op == '>' &&
1526 IS_AST_LIT_VALUE (condExpr->left->right) &&
1527 IS_AST_SYM_VALUE (condExpr->left->left) &&
1528 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1531 *end = newNode ('+', condExpr->left->right,
1532 newAst_VALUE (constVal ("1")));
1545 /* check loop expression is of the form <sym>++ */
1546 if (!IS_AST_OP (loopExpr))
1549 /* check if <sym> ++ */
1550 if (loopExpr->opval.op == INC_OP)
1556 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1557 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1564 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1565 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1573 if (loopExpr->opval.op == ADD_ASSIGN)
1576 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1577 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1578 IS_AST_LIT_VALUE (loopExpr->right) &&
1579 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1587 /*-----------------------------------------------------------------*/
1588 /* astHasVolatile - returns true if ast contains any volatile */
1589 /*-----------------------------------------------------------------*/
1591 astHasVolatile (ast * tree)
1596 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1599 if (IS_AST_OP (tree))
1600 return astHasVolatile (tree->left) ||
1601 astHasVolatile (tree->right);
1606 /*-----------------------------------------------------------------*/
1607 /* astHasPointer - return true if the ast contains any ptr variable */
1608 /*-----------------------------------------------------------------*/
1610 astHasPointer (ast * tree)
1615 if (IS_AST_LINK (tree))
1618 /* if we hit an array expression then check
1619 only the left side */
1620 if (IS_AST_OP (tree) && tree->opval.op == '[')
1621 return astHasPointer (tree->left);
1623 if (IS_AST_VALUE (tree))
1624 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1626 return astHasPointer (tree->left) ||
1627 astHasPointer (tree->right);
1631 /*-----------------------------------------------------------------*/
1632 /* astHasSymbol - return true if the ast has the given symbol */
1633 /*-----------------------------------------------------------------*/
1635 astHasSymbol (ast * tree, symbol * sym)
1637 if (!tree || IS_AST_LINK (tree))
1640 if (IS_AST_VALUE (tree))
1642 if (IS_AST_SYM_VALUE (tree))
1643 return isSymbolEqual (AST_SYMBOL (tree), sym);
1648 return astHasSymbol (tree->left, sym) ||
1649 astHasSymbol (tree->right, sym);
1652 /*-----------------------------------------------------------------*/
1653 /* astHasDeref - return true if the ast has an indirect access */
1654 /*-----------------------------------------------------------------*/
1656 astHasDeref (ast * tree)
1658 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1661 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1663 return astHasDeref (tree->left) || astHasDeref (tree->right);
1666 /*-----------------------------------------------------------------*/
1667 /* isConformingBody - the loop body has to conform to a set of rules */
1668 /* for the loop to be considered reversible read on for rules */
1669 /*-----------------------------------------------------------------*/
1671 isConformingBody (ast * pbody, symbol * sym, ast * body)
1674 /* we are going to do a pre-order traversal of the
1675 tree && check for the following conditions. (essentially
1676 a set of very shallow tests )
1677 a) the sym passed does not participate in
1678 any arithmetic operation
1679 b) There are no function calls
1680 c) all jumps are within the body
1681 d) address of loop control variable not taken
1682 e) if an assignment has a pointer on the
1683 left hand side make sure right does not have
1684 loop control variable */
1686 /* if we reach the end or a leaf then true */
1687 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1690 /* if anything else is "volatile" */
1691 if (IS_VOLATILE (TETYPE (pbody)))
1694 /* we will walk the body in a pre-order traversal for
1696 switch (pbody->opval.op)
1698 /*------------------------------------------------------------------*/
1700 // if the loopvar is used as an index
1701 /* array op is commutative -- must check both left & right */
1702 if (astHasSymbol(pbody->right, sym) || astHasSymbol(pbody->left, sym)) {
1705 return isConformingBody (pbody->right, sym, body)
1706 && isConformingBody (pbody->left, sym, body);
1708 /*------------------------------------------------------------------*/
1713 /*------------------------------------------------------------------*/
1717 /* sure we are not sym is not modified */
1719 IS_AST_SYM_VALUE (pbody->left) &&
1720 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1724 IS_AST_SYM_VALUE (pbody->right) &&
1725 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1730 /*------------------------------------------------------------------*/
1732 case '*': /* can be unary : if right is null then unary operation */
1737 /* if right is NULL then unary operation */
1738 /*------------------------------------------------------------------*/
1739 /*----------------------------*/
1741 /*----------------------------*/
1744 if (IS_AST_SYM_VALUE (pbody->left) &&
1745 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1748 return isConformingBody (pbody->left, sym, body);
1752 if (astHasSymbol (pbody->left, sym) ||
1753 astHasSymbol (pbody->right, sym))
1758 /*------------------------------------------------------------------*/
1769 if (IS_AST_SYM_VALUE (pbody->left) &&
1770 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1773 if (IS_AST_SYM_VALUE (pbody->right) &&
1774 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1777 return isConformingBody (pbody->left, sym, body) &&
1778 isConformingBody (pbody->right, sym, body);
1786 if (IS_AST_SYM_VALUE (pbody->left) &&
1787 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1789 return isConformingBody (pbody->left, sym, body);
1791 /*------------------------------------------------------------------*/
1803 case SIZEOF: /* evaluate wihout code generation */
1805 if (IS_AST_SYM_VALUE (pbody->left) &&
1806 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1809 if (IS_AST_SYM_VALUE (pbody->right) &&
1810 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1813 return isConformingBody (pbody->left, sym, body) &&
1814 isConformingBody (pbody->right, sym, body);
1816 /*------------------------------------------------------------------*/
1819 /* if left has a pointer & right has loop
1820 control variable then we cannot */
1821 if (astHasPointer (pbody->left) &&
1822 astHasSymbol (pbody->right, sym))
1824 if (astHasVolatile (pbody->left))
1827 if (IS_AST_SYM_VALUE (pbody->left)) {
1828 // if the loopvar has an assignment
1829 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1831 // if the loopvar is used in another (maybe conditional) block
1832 if (astHasSymbol (pbody->right, sym) &&
1833 (pbody->level >= body->level)) {
1838 if (astHasVolatile (pbody->left))
1841 if (astHasDeref(pbody->right)) return FALSE;
1843 return isConformingBody (pbody->left, sym, body) &&
1844 isConformingBody (pbody->right, sym, body);
1855 assert ("Parser should not have generated this\n");
1857 /*------------------------------------------------------------------*/
1858 /*----------------------------*/
1859 /* comma operator */
1860 /*----------------------------*/
1862 return isConformingBody (pbody->left, sym, body) &&
1863 isConformingBody (pbody->right, sym, body);
1865 /*------------------------------------------------------------------*/
1866 /*----------------------------*/
1868 /*----------------------------*/
1870 /* if local & not passed as paramater then ok */
1871 if (sym->level && !astHasSymbol(pbody->right,sym))
1875 /*------------------------------------------------------------------*/
1876 /*----------------------------*/
1877 /* return statement */
1878 /*----------------------------*/
1883 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1888 if (astHasSymbol (pbody->left, sym))
1895 return isConformingBody (pbody->left, sym, body) &&
1896 isConformingBody (pbody->right, sym, body);
1902 /*-----------------------------------------------------------------*/
1903 /* isLoopReversible - takes a for loop as input && returns true */
1904 /* if the for loop is reversible. If yes will set the value of */
1905 /* the loop control var & init value & termination value */
1906 /*-----------------------------------------------------------------*/
1908 isLoopReversible (ast * loop, symbol ** loopCntrl,
1909 ast ** init, ast ** end)
1911 /* if option says don't do it then don't */
1912 if (optimize.noLoopReverse)
1914 /* there are several tests to determine this */
1916 /* for loop has to be of the form
1917 for ( <sym> = <const1> ;
1918 [<sym> < <const2>] ;
1919 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1921 if (!isLoopCountable (AST_FOR (loop, initExpr),
1922 AST_FOR (loop, condExpr),
1923 AST_FOR (loop, loopExpr),
1924 loopCntrl, init, end))
1927 /* now do some serious checking on the body of the loop
1930 return isConformingBody (loop->left, *loopCntrl, loop->left);
1934 /*-----------------------------------------------------------------*/
1935 /* replLoopSym - replace the loop sym by loop sym -1 */
1936 /*-----------------------------------------------------------------*/
1938 replLoopSym (ast * body, symbol * sym)
1941 if (!body || IS_AST_LINK (body))
1944 if (IS_AST_SYM_VALUE (body))
1947 if (isSymbolEqual (AST_SYMBOL (body), sym))
1951 body->opval.op = '-';
1952 body->left = newAst_VALUE (symbolVal (sym));
1953 body->right = newAst_VALUE (constVal ("1"));
1961 replLoopSym (body->left, sym);
1962 replLoopSym (body->right, sym);
1966 /*-----------------------------------------------------------------*/
1967 /* reverseLoop - do the actual loop reversal */
1968 /*-----------------------------------------------------------------*/
1970 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1974 /* create the following tree
1979 if (sym) goto for_continue ;
1982 /* put it together piece by piece */
1983 rloop = newNode (NULLOP,
1984 createIf (newAst_VALUE (symbolVal (sym)),
1986 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
1989 newAst_VALUE (symbolVal (sym)),
1992 replLoopSym (loop->left, sym);
1993 setAstLineno (rloop, init->lineno);
1995 rloop = newNode (NULLOP,
1997 newAst_VALUE (symbolVal (sym)),
1998 newNode ('-', end, init)),
1999 createLabel (AST_FOR (loop, continueLabel),
2003 newNode (SUB_ASSIGN,
2004 newAst_VALUE (symbolVal (sym)),
2005 newAst_VALUE (constVal ("1"))),
2008 rloop->lineno=init->lineno;
2009 return decorateType (rloop, RESULT_TYPE_NONE);
2013 /*-----------------------------------------------------------------*/
2014 /* searchLitOp - search tree (*ops only) for an ast with literal */
2015 /*-----------------------------------------------------------------*/
2017 searchLitOp (ast *tree, ast **parent, const unsigned char *ops)
2021 if (tree && optimize.global_cse)
2023 /* is there a literal operand? */
2025 IS_AST_OP(tree->right) &&
2026 tree->right->right &&
2027 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
2029 if (IS_LITERAL (RTYPE (tree->right)) !=
2030 IS_LITERAL (LTYPE (tree->right)))
2032 tree->right->decorated = 0;
2033 tree->decorated = 0;
2037 ret = searchLitOp (tree->right, parent, ops);
2042 IS_AST_OP(tree->left) &&
2043 tree->left->right &&
2044 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
2046 if (IS_LITERAL (RTYPE (tree->left)) !=
2047 IS_LITERAL (LTYPE (tree->left)))
2049 tree->left->decorated = 0;
2050 tree->decorated = 0;
2054 ret = searchLitOp (tree->left, parent, ops);
2062 /*-----------------------------------------------------------------*/
2063 /* getResultFromType */
2064 /*-----------------------------------------------------------------*/
2066 getResultTypeFromType (sym_link *type)
2068 /* type = getSpec (type); */
2070 return RESULT_TYPE_BIT;
2071 if (IS_BITFIELD (type))
2073 int blen = SPEC_BLEN (type);
2076 return RESULT_TYPE_BIT;
2078 return RESULT_TYPE_CHAR;
2079 return RESULT_TYPE_INT;
2082 return RESULT_TYPE_CHAR;
2085 return RESULT_TYPE_INT;
2086 return RESULT_TYPE_OTHER;
2089 /*-----------------------------------------------------------------*/
2090 /* addCast - adds casts to a type specified by RESULT_TYPE */
2091 /*-----------------------------------------------------------------*/
2093 addCast (ast *tree, RESULT_TYPE resultType, bool promote)
2096 bool upCasted = FALSE;
2100 case RESULT_TYPE_NONE:
2101 /* if thing smaller than int must be promoted to int */
2103 getSize (tree->etype) >= INTSIZE)
2104 /* promotion not necessary or already an int */
2106 /* char and bits: promote to int */
2107 newLink = newIntLink();
2110 case RESULT_TYPE_BIT:
2112 /* already an int */
2113 bitsForType (tree->etype) >= 16 ||
2114 /* bit to bit operation: don't promote, the code generators
2115 hopefully know everything about promotion rules */
2116 bitsForType (tree->etype) == 1)
2118 newLink = newIntLink();
2121 case RESULT_TYPE_CHAR:
2122 if (IS_CHAR (tree->etype) ||
2123 IS_FLOAT(tree->etype) ||
2124 IS_FIXED(tree->etype))
2126 newLink = newCharLink();
2128 case RESULT_TYPE_INT:
2130 if (getSize (tree->etype) > INTSIZE)
2132 /* warn ("Loosing significant digits"); */
2136 /* char: promote to int */
2138 getSize (tree->etype) >= INTSIZE)
2140 newLink = newIntLink();
2143 case RESULT_TYPE_OTHER:
2146 /* return type is long, float: promote char to int */
2147 if (getSize (tree->etype) >= INTSIZE)
2149 newLink = newIntLink();
2155 tree->decorated = 0;
2156 tree = newNode (CAST, newAst_LINK (newLink), tree);
2157 tree->lineno = tree->right->lineno;
2158 /* keep unsigned type during cast to smaller type,
2159 but not when promoting from char to int */
2161 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2162 return decorateType (tree, resultType);
2165 /*-----------------------------------------------------------------*/
2166 /* resultTypePropagate - decides if resultType can be propagated */
2167 /*-----------------------------------------------------------------*/
2169 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2171 switch (tree->opval.op)
2190 return RESULT_TYPE_NONE;
2194 return RESULT_TYPE_IFX;
2196 return RESULT_TYPE_NONE;
2200 /*-----------------------------------------------------------------*/
2201 /* getLeftResultType - gets type from left branch for propagation */
2202 /*-----------------------------------------------------------------*/
2204 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2206 switch (tree->opval.op)
2210 if (IS_PTR (LTYPE (tree)))
2211 return RESULT_TYPE_NONE;
2213 return getResultTypeFromType (LETYPE (tree));
2215 if (IS_PTR (currFunc->type->next))
2216 return RESULT_TYPE_NONE;
2218 return getResultTypeFromType (currFunc->type->next);
2220 if (!IS_ARRAY (LTYPE (tree)))
2222 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 256)
2223 return RESULT_TYPE_CHAR;
2230 /*--------------------------------------------------------------------*/
2231 /* decorateType - compute type for this tree, also does type checking.*/
2232 /* This is done bottom up, since type has to flow upwards. */
2233 /* resultType flows top-down and forces e.g. char-arithmetik, if the */
2234 /* result is a char and the operand(s) are int's. */
2235 /* It also does constant folding, and parameter checking. */
2236 /*--------------------------------------------------------------------*/
2238 decorateType (ast * tree, RESULT_TYPE resultType)
2242 RESULT_TYPE resultTypeProp;
2247 /* if already has type then do nothing */
2248 if (tree->decorated)
2251 tree->decorated = 1;
2254 /* print the line */
2255 /* if not block & function */
2256 if (tree->type == EX_OP &&
2257 (tree->opval.op != FUNCTION &&
2258 tree->opval.op != BLOCK &&
2259 tree->opval.op != NULLOP))
2261 filename = tree->filename;
2262 lineno = tree->lineno;
2266 /* if any child is an error | this one is an error do nothing */
2267 if (tree->isError ||
2268 (tree->left && tree->left->isError) ||
2269 (tree->right && tree->right->isError))
2272 /*------------------------------------------------------------------*/
2273 /*----------------------------*/
2274 /* leaf has been reached */
2275 /*----------------------------*/
2276 lineno=tree->lineno;
2277 /* if this is of type value */
2278 /* just get the type */
2279 if (tree->type == EX_VALUE)
2282 if (IS_LITERAL (tree->opval.val->etype))
2285 /* if this is a character array then declare it */
2286 if (IS_ARRAY (tree->opval.val->type))
2287 tree->opval.val = stringToSymbol (tree->opval.val);
2289 /* otherwise just copy the type information */
2290 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2294 if (tree->opval.val->sym)
2296 /* if the undefined flag is set then give error message */
2297 if (tree->opval.val->sym->undefined)
2299 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2301 TTYPE (tree) = TETYPE (tree) =
2302 tree->opval.val->type = tree->opval.val->sym->type =
2303 tree->opval.val->etype = tree->opval.val->sym->etype =
2304 copyLinkChain (INTTYPE);
2309 /* if impilicit i.e. struct/union member then no type */
2310 if (tree->opval.val->sym->implicit)
2311 TTYPE (tree) = TETYPE (tree) = NULL;
2316 /* else copy the type */
2317 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2319 /* and mark it as referenced */
2320 tree->opval.val->sym->isref = 1;
2328 /* if type link for the case of cast */
2329 if (tree->type == EX_LINK)
2331 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2339 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2341 if (tree->left && tree->left->type == EX_OPERAND
2342 && (tree->left->opval.op == INC_OP
2343 || tree->left->opval.op == DEC_OP)
2344 && tree->left->left)
2346 tree->left->right = tree->left->left;
2347 tree->left->left = NULL;
2349 if (tree->right && tree->right->type == EX_OPERAND
2350 && (tree->right->opval.op == INC_OP
2351 || tree->right->opval.op == DEC_OP)
2352 && tree->right->left)
2354 tree->right->right = tree->right->left;
2355 tree->right->left = NULL;
2360 /* Before decorating the left branch we've to decide in dependence
2361 upon tree->opval.op, if resultType can be propagated */
2362 resultTypeProp = resultTypePropagate (tree, resultType);
2364 if (tree->opval.op == '?')
2365 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2367 dtl = decorateType (tree->left, resultTypeProp);
2369 /* if an array node, we may need to swap branches */
2370 if (tree->opval.op == '[')
2372 /* determine which is the array & which the index */
2373 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2374 IS_INTEGRAL (LTYPE (tree)))
2376 ast *tempTree = tree->left;
2377 tree->left = tree->right;
2378 tree->right = tempTree;
2382 /* After decorating the left branch there's type information available
2383 in tree->left->?type. If the op is e.g. '=' we extract the type
2384 information from there and propagate it to the right branch. */
2385 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2387 switch (tree->opval.op)
2390 /* delay right side for '?' operator since conditional macro
2391 expansions might rely on this */
2395 /* decorate right side for CALL (parameter list) in processParms();
2396 there is resultType available */
2400 dtr = decorateType (tree->right, resultTypeProp);
2404 /* this is to take care of situations
2405 when the tree gets rewritten */
2406 if (dtl != tree->left)
2408 if (dtr != tree->right)
2410 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2414 /* depending on type of operator do */
2416 switch (tree->opval.op)
2418 /*------------------------------------------------------------------*/
2419 /*----------------------------*/
2421 /*----------------------------*/
2424 /* first check if this is a array or a pointer */
2425 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2427 werror (E_NEED_ARRAY_PTR, "[]");
2428 goto errorTreeReturn;
2431 /* check if the type of the idx */
2432 if (!IS_INTEGRAL (RTYPE (tree)))
2434 werror (E_IDX_NOT_INT);
2435 goto errorTreeReturn;
2438 /* if the left is an rvalue then error */
2441 werror (E_LVALUE_REQUIRED, "array access");
2442 goto errorTreeReturn;
2445 if (IS_LITERAL (RTYPE (tree)))
2447 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2448 int arraySize = DCL_ELEM (LTYPE (tree));
2449 if (arraySize && arrayIndex >= arraySize)
2451 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2456 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2459 /*------------------------------------------------------------------*/
2460 /*----------------------------*/
2462 /*----------------------------*/
2464 /* if this is not a structure */
2465 if (!IS_STRUCT (LTYPE (tree)))
2467 werror (E_STRUCT_UNION, ".");
2468 goto errorTreeReturn;
2470 TTYPE (tree) = structElemType (LTYPE (tree),
2471 (tree->right->type == EX_VALUE ?
2472 tree->right->opval.val : NULL));
2473 TETYPE (tree) = getSpec (TTYPE (tree));
2476 /*------------------------------------------------------------------*/
2477 /*----------------------------*/
2478 /* struct/union pointer */
2479 /*----------------------------*/
2481 /* if not pointer to a structure */
2482 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2484 werror (E_PTR_REQD);
2485 goto errorTreeReturn;
2488 if (!IS_STRUCT (LTYPE (tree)->next))
2490 werror (E_STRUCT_UNION, "->");
2491 goto errorTreeReturn;
2494 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2495 (tree->right->type == EX_VALUE ?
2496 tree->right->opval.val : NULL));
2497 TETYPE (tree) = getSpec (TTYPE (tree));
2499 /* adjust the storage class */
2500 switch (DCL_TYPE(tree->left->ftype)) {
2502 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2505 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2508 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2511 SPEC_SCLS (TETYPE (tree)) = 0;
2514 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2517 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2520 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2523 SPEC_SCLS (TETYPE (tree)) = 0;
2530 /* This breaks with extern declarations, bitfields, and perhaps other */
2531 /* cases (gcse). Let's leave this optimization disabled for now and */
2532 /* ponder if there's a safe way to do this. -- EEP */
2534 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2535 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2537 /* If defined struct type at addr var
2538 then rewrite (&struct var)->member
2540 and define membertype at (addr+offsetof(struct var,member)) temp
2543 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2544 AST_SYMBOL(tree->right));
2546 sym = newSymbol(genSymName (0), 0);
2547 sym->type = TTYPE (tree);
2548 sym->etype = getSpec(sym->type);
2549 sym->lineDef = tree->lineno;
2552 SPEC_STAT (sym->etype) = 1;
2553 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2555 SPEC_ABSA(sym->etype) = 1;
2556 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2559 AST_VALUE (tree) = symbolVal(sym);
2562 tree->type = EX_VALUE;
2570 /*------------------------------------------------------------------*/
2571 /*----------------------------*/
2572 /* ++/-- operation */
2573 /*----------------------------*/
2577 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2578 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2579 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2580 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2589 /*------------------------------------------------------------------*/
2590 /*----------------------------*/
2592 /*----------------------------*/
2593 case '&': /* can be unary */
2594 /* if right is NULL then unary operation */
2595 if (tree->right) /* not an unary operation */
2598 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2600 werror (E_BITWISE_OP);
2601 werror (W_CONTINUE, "left & right types are ");
2602 printTypeChain (LTYPE (tree), stderr);
2603 fprintf (stderr, ",");
2604 printTypeChain (RTYPE (tree), stderr);
2605 fprintf (stderr, "\n");
2606 goto errorTreeReturn;
2609 /* if they are both literal */
2610 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2612 tree->type = EX_VALUE;
2613 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2614 valFromType (RETYPE (tree)), '&');
2616 tree->right = tree->left = NULL;
2617 TETYPE (tree) = tree->opval.val->etype;
2618 TTYPE (tree) = tree->opval.val->type;
2622 /* see if this is a GETHBIT operation if yes
2625 ast *otree = optimizeGetHbit (tree, resultType);
2628 return decorateType (otree, RESULT_TYPE_NONE);
2631 /* see if this is a GETABIT operation if yes
2634 ast *otree = optimizeGetAbit (tree, resultType);
2637 return decorateType (otree, RESULT_TYPE_NONE);
2640 /* see if this is a GETBYTE operation if yes
2643 ast *otree = optimizeGetByte (tree, resultType);
2646 return decorateType (otree, RESULT_TYPE_NONE);
2649 /* see if this is a GETWORD operation if yes
2652 ast *otree = optimizeGetWord (tree, resultType);
2655 return decorateType (otree, RESULT_TYPE_NONE);
2658 /* if left is a literal exchange left & right */
2659 if (IS_LITERAL (LTYPE (tree)))
2661 ast *tTree = tree->left;
2662 tree->left = tree->right;
2663 tree->right = tTree;
2666 /* if right is a literal and */
2667 /* we can find a 2nd literal in an and-tree then */
2668 /* rearrange the tree */
2669 if (IS_LITERAL (RTYPE (tree)))
2672 ast *litTree = searchLitOp (tree, &parent, "&");
2676 ast *tTree = litTree->left;
2677 litTree->left = tree->right;
2678 tree->right = tTree;
2679 /* both operands in litTree are literal now */
2680 decorateType (parent, resultType);
2684 LRVAL (tree) = RRVAL (tree) = 1;
2686 TTYPE (tree) = computeType (LTYPE (tree),
2690 TETYPE (tree) = getSpec (TTYPE (tree));
2695 /*------------------------------------------------------------------*/
2696 /*----------------------------*/
2698 /*----------------------------*/
2699 p = newLink (DECLARATOR);
2700 /* if bit field then error */
2701 if (IS_BITVAR (tree->left->etype))
2703 werror (E_ILLEGAL_ADDR, "address of bit variable");
2704 goto errorTreeReturn;
2707 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2709 werror (E_ILLEGAL_ADDR, "address of register variable");
2710 goto errorTreeReturn;
2713 if (IS_FUNC (LTYPE (tree)))
2715 // this ought to be ignored
2716 return (tree->left);
2719 if (IS_LITERAL(LTYPE(tree)))
2721 werror (E_ILLEGAL_ADDR, "address of literal");
2722 goto errorTreeReturn;
2727 werror (E_LVALUE_REQUIRED, "address of");
2728 goto errorTreeReturn;
2731 DCL_TYPE (p) = POINTER;
2732 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2733 DCL_TYPE (p) = CPOINTER;
2734 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2735 DCL_TYPE (p) = FPOINTER;
2736 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2737 DCL_TYPE (p) = PPOINTER;
2738 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2739 DCL_TYPE (p) = IPOINTER;
2740 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2741 DCL_TYPE (p) = EEPPOINTER;
2742 else if (SPEC_OCLS(tree->left->etype))
2743 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2745 DCL_TYPE (p) = POINTER;
2747 if (IS_AST_SYM_VALUE (tree->left))
2749 AST_SYMBOL (tree->left)->addrtaken = 1;
2750 AST_SYMBOL (tree->left)->allocreq = 1;
2753 p->next = LTYPE (tree);
2755 TETYPE (tree) = getSpec (TTYPE (tree));
2760 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2761 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2763 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2764 AST_SYMBOL(tree->left->right));
2765 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2766 valueFromLit(element->offset));
2769 tree->type = EX_VALUE;
2770 tree->values.literalFromCast = 1;
2776 /*------------------------------------------------------------------*/
2777 /*----------------------------*/
2779 /*----------------------------*/
2781 /* if the rewrite succeeds then don't go any further */
2783 ast *wtree = optimizeRRCRLC (tree);
2785 return decorateType (wtree, RESULT_TYPE_NONE);
2787 wtree = optimizeSWAP (tree);
2789 return decorateType (wtree, RESULT_TYPE_NONE);
2792 /* if left is a literal exchange left & right */
2793 if (IS_LITERAL (LTYPE (tree)))
2795 ast *tTree = tree->left;
2796 tree->left = tree->right;
2797 tree->right = tTree;
2800 /* if right is a literal and */
2801 /* we can find a 2nd literal in an or-tree then */
2802 /* rearrange the tree */
2803 if (IS_LITERAL (RTYPE (tree)))
2806 ast *litTree = searchLitOp (tree, &parent, "|");
2810 ast *tTree = litTree->left;
2811 litTree->left = tree->right;
2812 tree->right = tTree;
2813 /* both operands in tTree are literal now */
2814 decorateType (parent, resultType);
2819 /*------------------------------------------------------------------*/
2820 /*----------------------------*/
2822 /*----------------------------*/
2824 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2826 werror (E_BITWISE_OP);
2827 werror (W_CONTINUE, "left & right types are ");
2828 printTypeChain (LTYPE (tree), stderr);
2829 fprintf (stderr, ",");
2830 printTypeChain (RTYPE (tree), stderr);
2831 fprintf (stderr, "\n");
2832 goto errorTreeReturn;
2835 /* if they are both literal then rewrite the tree */
2836 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2838 tree->type = EX_VALUE;
2839 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2840 valFromType (RETYPE (tree)),
2842 tree->right = tree->left = NULL;
2843 TETYPE (tree) = tree->opval.val->etype;
2844 TTYPE (tree) = tree->opval.val->type;
2848 /* if left is a literal exchange left & right */
2849 if (IS_LITERAL (LTYPE (tree)))
2851 ast *tTree = tree->left;
2852 tree->left = tree->right;
2853 tree->right = tTree;
2856 /* if right is a literal and */
2857 /* we can find a 2nd literal in a xor-tree then */
2858 /* rearrange the tree */
2859 if (IS_LITERAL (RTYPE (tree)) &&
2860 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
2863 ast *litTree = searchLitOp (tree, &parent, "^");
2867 ast *tTree = litTree->left;
2868 litTree->left = tree->right;
2869 tree->right = tTree;
2870 /* both operands in litTree are literal now */
2871 decorateType (parent, resultType);
2875 LRVAL (tree) = RRVAL (tree) = 1;
2877 TTYPE (tree) = computeType (LTYPE (tree),
2881 TETYPE (tree) = getSpec (TTYPE (tree));
2885 /*------------------------------------------------------------------*/
2886 /*----------------------------*/
2888 /*----------------------------*/
2890 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2892 werror (E_INVALID_OP, "divide");
2893 goto errorTreeReturn;
2895 /* if they are both literal then */
2896 /* rewrite the tree */
2897 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2899 tree->type = EX_VALUE;
2900 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2901 valFromType (RETYPE (tree)));
2902 tree->right = tree->left = NULL;
2903 TETYPE (tree) = getSpec (TTYPE (tree) =
2904 tree->opval.val->type);
2908 LRVAL (tree) = RRVAL (tree) = 1;
2910 TETYPE (tree) = getSpec (TTYPE (tree) =
2911 computeType (LTYPE (tree),
2916 /* if right is a literal and */
2917 /* left is also a division by a literal then */
2918 /* rearrange the tree */
2919 if (IS_LITERAL (RTYPE (tree))
2920 /* avoid infinite loop */
2921 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
2924 ast *litTree = searchLitOp (tree, &parent, "/");
2927 if (IS_LITERAL (RTYPE (litTree)))
2931 litTree->right = newNode ('*',
2933 copyAst (tree->right));
2934 litTree->right->lineno = tree->lineno;
2936 tree->right->opval.val = constVal ("1");
2937 decorateType (parent, resultType);
2941 /* litTree->left is literal: no gcse possible.
2942 We can't call decorateType(parent, RESULT_TYPE_NONE), because
2943 this would cause an infinit loop. */
2944 parent->decorated = 1;
2945 decorateType (litTree, resultType);
2952 /*------------------------------------------------------------------*/
2953 /*----------------------------*/
2955 /*----------------------------*/
2957 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2959 werror (E_BITWISE_OP);
2960 werror (W_CONTINUE, "left & right types are ");
2961 printTypeChain (LTYPE (tree), stderr);
2962 fprintf (stderr, ",");
2963 printTypeChain (RTYPE (tree), stderr);
2964 fprintf (stderr, "\n");
2965 goto errorTreeReturn;
2967 /* if they are both literal then */
2968 /* rewrite the tree */
2969 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2971 tree->type = EX_VALUE;
2972 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2973 valFromType (RETYPE (tree)));
2974 tree->right = tree->left = NULL;
2975 TETYPE (tree) = getSpec (TTYPE (tree) =
2976 tree->opval.val->type);
2979 LRVAL (tree) = RRVAL (tree) = 1;
2980 TETYPE (tree) = getSpec (TTYPE (tree) =
2981 computeType (LTYPE (tree),
2987 /*------------------------------------------------------------------*/
2988 /*----------------------------*/
2989 /* address dereference */
2990 /*----------------------------*/
2991 case '*': /* can be unary : if right is null then unary operation */
2994 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
2996 werror (E_PTR_REQD);
2997 goto errorTreeReturn;
3002 werror (E_LVALUE_REQUIRED, "pointer deref");
3003 goto errorTreeReturn;
3005 if (IS_ADDRESS_OF_OP(tree->left))
3007 /* replace *&obj with obj */
3008 return tree->left->left;
3010 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
3011 TETYPE (tree) = getSpec (TTYPE (tree));
3012 /* adjust the storage class */
3013 switch (DCL_TYPE(tree->left->ftype)) {
3015 SPEC_SCLS(TETYPE(tree)) = S_DATA;
3018 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
3021 SPEC_SCLS(TETYPE(tree)) = S_CODE;
3024 SPEC_SCLS (TETYPE (tree)) = 0;
3027 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
3030 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
3033 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
3036 SPEC_SCLS (TETYPE (tree)) = 0;
3045 /*------------------------------------------------------------------*/
3046 /*----------------------------*/
3047 /* multiplication */
3048 /*----------------------------*/
3049 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
3051 werror (E_INVALID_OP, "multiplication");
3052 goto errorTreeReturn;
3055 /* if they are both literal then */
3056 /* rewrite the tree */
3057 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3059 tree->type = EX_VALUE;
3060 tree->opval.val = valMult (valFromType (LETYPE (tree)),
3061 valFromType (RETYPE (tree)));
3062 tree->right = tree->left = NULL;
3063 TETYPE (tree) = getSpec (TTYPE (tree) =
3064 tree->opval.val->type);
3068 /* if left is a literal exchange left & right */
3069 if (IS_LITERAL (LTYPE (tree)))
3071 ast *tTree = tree->left;
3072 tree->left = tree->right;
3073 tree->right = tTree;
3076 /* if right is a literal and */
3077 /* we can find a 2nd literal in a mul-tree then */
3078 /* rearrange the tree */
3079 if (IS_LITERAL (RTYPE (tree)))
3082 ast *litTree = searchLitOp (tree, &parent, "*");
3086 ast *tTree = litTree->left;
3087 litTree->left = tree->right;
3088 tree->right = tTree;
3089 /* both operands in litTree are literal now */
3090 decorateType (parent, resultType);
3094 LRVAL (tree) = RRVAL (tree) = 1;
3095 tree->left = addCast (tree->left, resultType, FALSE);
3096 tree->right = addCast (tree->right, resultType, FALSE);
3097 TETYPE (tree) = getSpec (TTYPE (tree) =
3098 computeType (LTYPE (tree),
3105 /*------------------------------------------------------------------*/
3106 /*----------------------------*/
3107 /* unary '+' operator */
3108 /*----------------------------*/
3113 if (!IS_ARITHMETIC (LTYPE (tree)))
3115 werror (E_UNARY_OP, '+');
3116 goto errorTreeReturn;
3119 /* if left is a literal then do it */
3120 if (IS_LITERAL (LTYPE (tree)))
3122 tree->type = EX_VALUE;
3123 tree->opval.val = valFromType (LETYPE (tree));
3125 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3129 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3133 /*------------------------------------------------------------------*/
3134 /*----------------------------*/
3136 /*----------------------------*/
3138 /* this is not a unary operation */
3139 /* if both pointers then problem */
3140 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3141 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3143 werror (E_PTR_PLUS_PTR);
3144 goto errorTreeReturn;
3147 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3148 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3150 werror (E_PLUS_INVALID, "+");
3151 goto errorTreeReturn;
3154 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3155 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3157 werror (E_PLUS_INVALID, "+");
3158 goto errorTreeReturn;
3160 /* if they are both literal then */
3161 /* rewrite the tree */
3162 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3164 tree->type = EX_VALUE;
3165 tree->left = addCast (tree->left, resultType, TRUE);
3166 tree->right = addCast (tree->right, resultType, TRUE);
3167 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3168 valFromType (RETYPE (tree)));
3169 tree->right = tree->left = NULL;
3170 TETYPE (tree) = getSpec (TTYPE (tree) =
3171 tree->opval.val->type);
3175 /* if the right is a pointer or left is a literal
3176 xchange left & right */
3177 if (IS_ARRAY (RTYPE (tree)) ||
3178 IS_PTR (RTYPE (tree)) ||
3179 IS_LITERAL (LTYPE (tree)))
3181 ast *tTree = tree->left;
3182 tree->left = tree->right;
3183 tree->right = tTree;
3186 /* if right is a literal and */
3187 /* left is also an addition/subtraction with a literal then */
3188 /* rearrange the tree */
3189 if (IS_LITERAL (RTYPE (tree)))
3191 ast *litTree, *parent;
3192 litTree = searchLitOp (tree, &parent, "+-");
3195 if (litTree->opval.op == '+')
3199 ast *tTree = litTree->left;
3200 litTree->left = tree->right;
3201 tree->right = tree->left;
3204 else if (litTree->opval.op == '-')
3206 if (IS_LITERAL (RTYPE (litTree)))
3210 ast *tTree = litTree->left;
3211 litTree->left = tree->right;
3212 tree->right = tTree;
3218 ast *tTree = litTree->right;
3219 litTree->right = tree->right;
3220 tree->right = tTree;
3221 litTree->opval.op = '+';
3222 tree->opval.op = '-';
3225 decorateType (parent, resultType);
3229 LRVAL (tree) = RRVAL (tree) = 1;
3230 /* if the left is a pointer */
3231 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3232 TETYPE (tree) = getSpec (TTYPE (tree) =
3236 tree->left = addCast (tree->left, resultType, TRUE);
3237 tree->right = addCast (tree->right, resultType, TRUE);
3238 TETYPE (tree) = getSpec (TTYPE (tree) =
3239 computeType (LTYPE (tree),
3247 /*------------------------------------------------------------------*/
3248 /*----------------------------*/
3250 /*----------------------------*/
3251 case '-': /* can be unary */
3252 /* if right is null then unary */
3256 if (!IS_ARITHMETIC (LTYPE (tree)))
3258 werror (E_UNARY_OP, tree->opval.op);
3259 goto errorTreeReturn;
3262 /* if left is a literal then do it */
3263 if (IS_LITERAL (LTYPE (tree)))
3265 tree->type = EX_VALUE;
3266 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3268 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3269 SPEC_USIGN(TETYPE(tree)) = 0;
3273 TETYPE(tree) = getSpec (TTYPE (tree) = LTYPE (tree));
3277 /*------------------------------------------------------------------*/
3278 /*----------------------------*/
3280 /*----------------------------*/
3282 if (!(IS_PTR (LTYPE (tree)) ||
3283 IS_ARRAY (LTYPE (tree)) ||
3284 IS_ARITHMETIC (LTYPE (tree))))
3286 werror (E_PLUS_INVALID, "-");
3287 goto errorTreeReturn;
3290 if (!(IS_PTR (RTYPE (tree)) ||
3291 IS_ARRAY (RTYPE (tree)) ||
3292 IS_ARITHMETIC (RTYPE (tree))))
3294 werror (E_PLUS_INVALID, "-");
3295 goto errorTreeReturn;
3298 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3299 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3300 IS_INTEGRAL (RTYPE (tree))))
3302 werror (E_PLUS_INVALID, "-");
3303 goto errorTreeReturn;
3306 /* if they are both literal then */
3307 /* rewrite the tree */
3308 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3310 tree->type = EX_VALUE;
3311 tree->left = addCast (tree->left, resultType, TRUE);
3312 tree->right = addCast (tree->right, resultType, TRUE);
3313 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3314 valFromType (RETYPE (tree)));
3315 tree->right = tree->left = NULL;
3316 TETYPE (tree) = getSpec (TTYPE (tree) =
3317 tree->opval.val->type);
3321 /* if the left & right are equal then zero */
3322 if (isAstEqual (tree->left, tree->right))
3324 tree->type = EX_VALUE;
3325 tree->left = tree->right = NULL;
3326 tree->opval.val = constVal ("0");
3327 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3331 /* if both of them are pointers or arrays then */
3332 /* the result is going to be an integer */
3333 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3334 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3335 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3337 /* if only the left is a pointer */
3338 /* then result is a pointer */
3339 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3340 TETYPE (tree) = getSpec (TTYPE (tree) =
3344 tree->left = addCast (tree->left, resultType, TRUE);
3345 tree->right = addCast (tree->right, resultType, TRUE);
3347 TETYPE (tree) = getSpec (TTYPE (tree) =
3348 computeType (LTYPE (tree),
3354 LRVAL (tree) = RRVAL (tree) = 1;
3356 /* if right is a literal and */
3357 /* left is also an addition/subtraction with a literal then */
3358 /* rearrange the tree */
3359 if (IS_LITERAL (RTYPE (tree))
3360 /* avoid infinite loop */
3361 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3363 ast *litTree, *litParent;
3364 litTree = searchLitOp (tree, &litParent, "+-");
3367 if (litTree->opval.op == '+')
3371 ast *tTree = litTree->left;
3372 litTree->left = litTree->right;
3373 litTree->right = tree->right;
3374 tree->right = tTree;
3375 tree->opval.op = '+';
3376 litTree->opval.op = '-';
3378 else if (litTree->opval.op == '-')
3380 if (IS_LITERAL (RTYPE (litTree)))
3384 ast *tTree = litTree->left;
3385 litTree->left = tree->right;
3386 tree->right = litParent->left;
3387 litParent->left = tTree;
3388 litTree->opval.op = '+';
3390 tree->decorated = 0;
3391 decorateType (tree, resultType);
3397 ast *tTree = litTree->right;
3398 litTree->right = tree->right;
3399 tree->right = tTree;
3402 decorateType (litParent, resultType);
3407 /*------------------------------------------------------------------*/
3408 /*----------------------------*/
3410 /*----------------------------*/
3412 /* can be only integral type */
3413 if (!IS_INTEGRAL (LTYPE (tree)))
3415 werror (E_UNARY_OP, tree->opval.op);
3416 goto errorTreeReturn;
3419 /* if left is a literal then do it */
3420 if (IS_LITERAL (LTYPE (tree)))
3422 tree->type = EX_VALUE;
3423 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3425 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3426 return addCast (tree, resultType, TRUE);
3429 if (resultType == RESULT_TYPE_BIT &&
3430 IS_UNSIGNED (tree->left->etype) &&
3431 getSize (tree->left->etype) < INTSIZE)
3433 /* promotion rules are responsible for this strange result:
3434 bit -> int -> ~int -> bit
3435 uchar -> int -> ~int -> bit
3437 werror(W_COMPLEMENT);
3439 /* optimize bit-result, even if we optimize a buggy source */
3440 tree->type = EX_VALUE;
3441 tree->opval.val = constVal ("1");
3444 tree->left = addCast (tree->left, resultType, TRUE);
3446 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3449 /*------------------------------------------------------------------*/
3450 /*----------------------------*/
3452 /*----------------------------*/
3454 /* can be pointer */
3455 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3456 !IS_PTR (LTYPE (tree)) &&
3457 !IS_ARRAY (LTYPE (tree)))
3459 werror (E_UNARY_OP, tree->opval.op);
3460 goto errorTreeReturn;
3463 /* if left is a literal then do it */
3464 if (IS_LITERAL (LTYPE (tree)))
3466 tree->type = EX_VALUE;
3467 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3469 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3473 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3476 /*------------------------------------------------------------------*/
3477 /*----------------------------*/
3479 /*----------------------------*/
3483 TTYPE (tree) = LTYPE (tree);
3484 TETYPE (tree) = LETYPE (tree);
3489 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3493 TTYPE (tree) = TETYPE (tree) = newCharLink();
3497 TTYPE (tree) = TETYPE (tree) = newIntLink();
3502 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3504 werror (E_SHIFT_OP_INVALID);
3505 werror (W_CONTINUE, "left & right types are ");
3506 printTypeChain (LTYPE (tree), stderr);
3507 fprintf (stderr, ",");
3508 printTypeChain (RTYPE (tree), stderr);
3509 fprintf (stderr, "\n");
3510 goto errorTreeReturn;
3513 /* make smaller type only if it's a LEFT_OP */
3514 if (tree->opval.op == LEFT_OP)
3515 tree->left = addCast (tree->left, resultType, TRUE);
3517 /* if they are both literal then */
3518 /* rewrite the tree */
3519 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3521 tree->type = EX_VALUE;
3522 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3523 valFromType (RETYPE (tree)),
3524 (tree->opval.op == LEFT_OP ? 1 : 0));
3525 tree->right = tree->left = NULL;
3526 TETYPE (tree) = getSpec (TTYPE (tree) =
3527 tree->opval.val->type);
3531 /* see if this is a GETBYTE operation if yes
3534 ast *otree = optimizeGetByte (tree, resultType);
3537 return decorateType (otree, RESULT_TYPE_NONE);
3540 /* see if this is a GETWORD operation if yes
3543 ast *otree = optimizeGetWord (tree, resultType);
3546 return decorateType (otree, RESULT_TYPE_NONE);
3549 LRVAL (tree) = RRVAL (tree) = 1;
3550 if (tree->opval.op == LEFT_OP)
3552 TETYPE (tree) = getSpec (TTYPE (tree) =
3553 computeType (LTYPE (tree),
3560 /* no promotion necessary */
3561 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3562 if (IS_LITERAL (TTYPE (tree)))
3563 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3566 /* if only the right side is a literal & we are
3567 shifting more than size of the left operand then zero */
3568 if (IS_LITERAL (RTYPE (tree)) &&
3569 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3570 (getSize (TETYPE (tree)) * 8))
3572 if (tree->opval.op==LEFT_OP ||
3573 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3575 lineno=tree->lineno;
3576 werror (W_SHIFT_CHANGED,
3577 (tree->opval.op == LEFT_OP ? "left" : "right"));
3578 tree->type = EX_VALUE;
3579 tree->left = tree->right = NULL;
3580 tree->opval.val = constVal ("0");
3581 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3588 /*------------------------------------------------------------------*/
3589 /*----------------------------*/
3591 /*----------------------------*/
3592 case CAST: /* change the type */
3593 /* cannot cast to an aggregate type */
3594 if (IS_AGGREGATE (LTYPE (tree)))
3596 werror (E_CAST_ILLEGAL);
3597 goto errorTreeReturn;
3600 /* make sure the type is complete and sane */
3601 changePointer(LTYPE(tree));
3602 checkTypeSanity(LETYPE(tree), "(cast)");
3604 /* If code memory is read only, then pointers to code memory */
3605 /* implicitly point to constants -- make this explicit */
3607 sym_link *t = LTYPE(tree);
3608 while (t && t->next)
3610 if (IS_CODEPTR(t) && port->mem.code_ro)
3612 if (IS_SPEC(t->next))
3613 SPEC_CONST (t->next) = 1;
3615 DCL_PTR_CONST (t->next) = 1;
3622 /* if the right is a literal replace the tree */
3623 if (IS_LITERAL (RETYPE (tree))) {
3624 if (!IS_PTR (LTYPE (tree))) {
3625 tree->type = EX_VALUE;
3627 valCastLiteral (LTYPE (tree),
3628 floatFromVal (valFromType (RETYPE (tree))));
3631 TTYPE (tree) = tree->opval.val->type;
3632 tree->values.literalFromCast = 1;
3633 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3634 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3635 sym_link *rest = LTYPE(tree)->next;
3636 werror(W_LITERAL_GENERIC);
3637 TTYPE(tree) = newLink(DECLARATOR);
3638 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3639 TTYPE(tree)->next = rest;
3640 tree->left->opval.lnk = TTYPE(tree);
3643 TTYPE (tree) = LTYPE (tree);
3647 TTYPE (tree) = LTYPE (tree);
3651 #if 0 // this is already checked, now this could be explicit
3652 /* if pointer to struct then check names */
3653 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3654 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3655 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3657 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3658 SPEC_STRUCT(LETYPE(tree))->tag);
3661 if (IS_ADDRESS_OF_OP(tree->right)
3662 && IS_AST_SYM_VALUE (tree->right->left)
3663 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3665 symbol * sym = AST_SYMBOL (tree->right->left);
3666 unsigned int gptype = 0;
3667 unsigned int addr = SPEC_ADDR (sym->etype);
3669 if (IS_GENPTR (LTYPE (tree)) && GPTRSIZE > FPTRSIZE)
3671 switch (SPEC_SCLS (sym->etype))
3674 gptype = GPTYPE_CODE;
3677 gptype = GPTYPE_FAR;
3681 gptype = GPTYPE_NEAR;
3684 gptype = GPTYPE_XSTACK;
3689 addr |= gptype << (8*(GPTRSIZE - 1));
3692 tree->type = EX_VALUE;
3694 valCastLiteral (LTYPE (tree), addr);
3695 TTYPE (tree) = tree->opval.val->type;
3696 TETYPE (tree) = getSpec (TTYPE (tree));
3699 tree->values.literalFromCast = 1;
3703 /* handle offsetof macro: */
3704 /* #define offsetof(TYPE, MEMBER) \ */
3705 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3706 if (IS_ADDRESS_OF_OP(tree->right)
3707 && IS_AST_OP (tree->right->left)
3708 && tree->right->left->opval.op == PTR_OP
3709 && IS_AST_OP (tree->right->left->left)
3710 && tree->right->left->left->opval.op == CAST
3711 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3713 symbol *element = getStructElement (
3714 SPEC_STRUCT (LETYPE(tree->right->left)),
3715 AST_SYMBOL(tree->right->left->right)
3719 tree->type = EX_VALUE;
3720 tree->opval.val = valCastLiteral (
3723 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3726 TTYPE (tree) = tree->opval.val->type;
3727 TETYPE (tree) = getSpec (TTYPE (tree));
3734 /* if the right is a literal replace the tree */
3735 if (IS_LITERAL (RETYPE (tree))) {
3737 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3738 /* rewrite (type *)litaddr
3740 and define type at litaddr temp
3741 (but only if type's storage class is not generic)
3743 ast *newTree = newNode ('&', NULL, NULL);
3746 TTYPE (newTree) = LTYPE (tree);
3747 TETYPE (newTree) = getSpec(LTYPE (tree));
3749 /* define a global symbol at the casted address*/
3750 sym = newSymbol(genSymName (0), 0);
3751 sym->type = LTYPE (tree)->next;
3753 sym->type = newLink (V_VOID);
3754 sym->etype = getSpec(sym->type);
3755 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3756 sym->lineDef = tree->lineno;
3759 SPEC_STAT (sym->etype) = 1;
3760 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3761 SPEC_ABSA(sym->etype) = 1;
3762 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3765 newTree->left = newAst_VALUE(symbolVal(sym));
3766 newTree->left->lineno = tree->lineno;
3767 LTYPE (newTree) = sym->type;
3768 LETYPE (newTree) = sym->etype;
3769 LLVAL (newTree) = 1;
3770 LRVAL (newTree) = 0;
3771 TLVAL (newTree) = 1;
3775 if (!IS_PTR (LTYPE (tree))) {
3776 tree->type = EX_VALUE;
3778 valCastLiteral (LTYPE (tree),
3779 floatFromVal (valFromType (RTYPE (tree))));
3780 TTYPE (tree) = tree->opval.val->type;
3783 tree->values.literalFromCast = 1;
3784 TETYPE (tree) = getSpec (TTYPE (tree));
3788 TTYPE (tree) = LTYPE (tree);
3792 TETYPE (tree) = getSpec (TTYPE (tree));
3796 /*------------------------------------------------------------------*/
3797 /*----------------------------*/
3798 /* logical &&, || */
3799 /*----------------------------*/
3802 /* each must be arithmetic type or be a pointer */
3803 if (!IS_PTR (LTYPE (tree)) &&
3804 !IS_ARRAY (LTYPE (tree)) &&
3805 !IS_INTEGRAL (LTYPE (tree)))
3807 werror (E_COMPARE_OP);
3808 goto errorTreeReturn;
3811 if (!IS_PTR (RTYPE (tree)) &&
3812 !IS_ARRAY (RTYPE (tree)) &&
3813 !IS_INTEGRAL (RTYPE (tree)))
3815 werror (E_COMPARE_OP);
3816 goto errorTreeReturn;
3818 /* if they are both literal then */
3819 /* rewrite the tree */
3820 if (IS_LITERAL (RTYPE (tree)) &&
3821 IS_LITERAL (LTYPE (tree)))
3823 tree->type = EX_VALUE;
3824 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
3825 valFromType (RTYPE (tree)),
3827 tree->right = tree->left = NULL;
3828 TETYPE (tree) = getSpec (TTYPE (tree) =
3829 tree->opval.val->type);
3832 LRVAL (tree) = RRVAL (tree) = 1;
3833 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3836 /*------------------------------------------------------------------*/
3837 /*----------------------------*/
3838 /* comparison operators */
3839 /*----------------------------*/
3847 ast *lt = optimizeCompare (tree);
3853 /* if they are pointers they must be castable */
3854 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3856 if (tree->opval.op==EQ_OP &&
3857 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
3858 // we cannot cast a gptr to a !gptr: switch the leaves
3859 struct ast *s=tree->left;
3860 tree->left=tree->right;
3863 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3865 werror (E_COMPARE_OP);
3866 fprintf (stderr, "comparing type ");
3867 printTypeChain (LTYPE (tree), stderr);
3868 fprintf (stderr, "to type ");
3869 printTypeChain (RTYPE (tree), stderr);
3870 fprintf (stderr, "\n");
3871 goto errorTreeReturn;
3874 /* else they should be promotable to one another */
3877 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
3878 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
3880 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3882 werror (E_COMPARE_OP);
3883 fprintf (stderr, "comparing type ");
3884 printTypeChain (LTYPE (tree), stderr);
3885 fprintf (stderr, "to type ");
3886 printTypeChain (RTYPE (tree), stderr);
3887 fprintf (stderr, "\n");
3888 goto errorTreeReturn;
3891 /* if unsigned value < 0 then always false */
3892 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
3893 if (SPEC_USIGN(LETYPE(tree)) &&
3894 !IS_CHAR(LETYPE(tree)) && /* promotion to signed int */
3895 IS_LITERAL(RTYPE(tree)) &&
3896 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
3898 if (tree->opval.op == '<')
3902 if (tree->opval.op == '>')
3904 if (resultType == RESULT_TYPE_IFX)
3906 /* the parent is an ifx: */
3907 /* if (unsigned value) */
3911 /* (unsigned value) ? 1 : 0 */
3912 tree->opval.op = '?';
3913 tree->right = newNode (':',
3914 newAst_VALUE (constVal ("1")),
3915 tree->right); /* val 0 */
3916 tree->right->lineno = tree->lineno;
3917 tree->right->left->lineno = tree->lineno;
3918 decorateType (tree->right, RESULT_TYPE_NONE);
3921 /* if they are both literal then */
3922 /* rewrite the tree */
3923 if (IS_LITERAL (RTYPE (tree)) &&
3924 IS_LITERAL (LTYPE (tree)))
3926 tree->type = EX_VALUE;
3927 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
3928 valFromType (RETYPE (tree)),
3930 tree->right = tree->left = NULL;
3931 TETYPE (tree) = getSpec (TTYPE (tree) =
3932 tree->opval.val->type);
3935 LRVAL (tree) = RRVAL (tree) = 1;
3936 TTYPE (tree) = TETYPE (tree) = newCharLink ();
3939 /*------------------------------------------------------------------*/
3940 /*----------------------------*/
3942 /*----------------------------*/
3943 case SIZEOF: /* evaluate wihout code generation */
3944 /* change the type to a integer */
3946 int size = getSize (tree->right->ftype);
3947 SNPRINTF(buffer, sizeof(buffer), "%d", size);
3948 if (!size && !IS_VOID(tree->right->ftype))
3949 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
3951 tree->type = EX_VALUE;
3952 tree->opval.val = constVal (buffer);
3953 tree->right = tree->left = NULL;
3954 TETYPE (tree) = getSpec (TTYPE (tree) =
3955 tree->opval.val->type);
3958 /*------------------------------------------------------------------*/
3959 /*----------------------------*/
3961 /*----------------------------*/
3963 /* return typeof enum value */
3964 tree->type = EX_VALUE;
3967 if (IS_SPEC(tree->right->ftype)) {
3968 switch (SPEC_NOUN(tree->right->ftype)) {
3970 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
3971 else typeofv = TYPEOF_INT;
3974 typeofv = TYPEOF_FLOAT;
3977 typeofv = TYPEOF_FIXED16X16;
3980 typeofv = TYPEOF_CHAR;
3983 typeofv = TYPEOF_VOID;
3986 typeofv = TYPEOF_STRUCT;
3989 typeofv = TYPEOF_BITFIELD;
3992 typeofv = TYPEOF_BIT;
3995 typeofv = TYPEOF_SBIT;
4001 switch (DCL_TYPE(tree->right->ftype)) {
4003 typeofv = TYPEOF_POINTER;
4006 typeofv = TYPEOF_FPOINTER;
4009 typeofv = TYPEOF_CPOINTER;
4012 typeofv = TYPEOF_GPOINTER;
4015 typeofv = TYPEOF_PPOINTER;
4018 typeofv = TYPEOF_IPOINTER;
4021 typeofv = TYPEOF_ARRAY;
4024 typeofv = TYPEOF_FUNCTION;
4030 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
4031 tree->opval.val = constVal (buffer);
4032 tree->right = tree->left = NULL;
4033 TETYPE (tree) = getSpec (TTYPE (tree) =
4034 tree->opval.val->type);
4037 /*------------------------------------------------------------------*/
4038 /*----------------------------*/
4039 /* conditional operator '?' */
4040 /*----------------------------*/
4042 /* the type is value of the colon operator (on the right) */
4043 assert (IS_COLON_OP (tree->right));
4044 /* if already known then replace the tree : optimizer will do it
4045 but faster to do it here */
4046 if (IS_LITERAL (LTYPE (tree)))
4048 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
4049 return decorateType (tree->right->left, resultTypeProp);
4051 return decorateType (tree->right->right, resultTypeProp);
4055 tree->right = decorateType (tree->right, resultTypeProp);
4056 TTYPE (tree) = RTYPE (tree);
4057 TETYPE (tree) = getSpec (TTYPE (tree));
4062 /* if they don't match we have a problem */
4063 if ((compareType (LTYPE (tree), RTYPE (tree)) == 0) &&
4064 (compareType (RTYPE (tree), LTYPE (tree)) == 0))
4066 werror (E_TYPE_MISMATCH, "conditional operator", " ");
4067 goto errorTreeReturn;
4070 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
4071 resultType, tree->opval.op);
4072 TETYPE (tree) = getSpec (TTYPE (tree));
4076 #if 0 // assignment operators are converted by the parser
4077 /*------------------------------------------------------------------*/
4078 /*----------------------------*/
4079 /* assignment operators */
4080 /*----------------------------*/
4083 /* for these it must be both must be integral */
4084 if (!IS_ARITHMETIC (LTYPE (tree)) ||
4085 !IS_ARITHMETIC (RTYPE (tree)))
4087 werror (E_OPS_INTEGRAL);
4088 goto errorTreeReturn;
4091 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4093 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
4094 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4098 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4099 goto errorTreeReturn;
4110 /* for these it must be both must be integral */
4111 if (!IS_INTEGRAL (LTYPE (tree)) ||
4112 !IS_INTEGRAL (RTYPE (tree)))
4114 werror (E_OPS_INTEGRAL);
4115 goto errorTreeReturn;
4118 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4120 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4121 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
4125 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
4126 goto errorTreeReturn;
4132 /*------------------------------------------------------------------*/
4133 /*----------------------------*/
4135 /*----------------------------*/
4137 if (!(IS_PTR (LTYPE (tree)) ||
4138 IS_ARITHMETIC (LTYPE (tree))))
4140 werror (E_PLUS_INVALID, "-=");
4141 goto errorTreeReturn;
4144 if (!(IS_PTR (RTYPE (tree)) ||
4145 IS_ARITHMETIC (RTYPE (tree))))
4147 werror (E_PLUS_INVALID, "-=");
4148 goto errorTreeReturn;
4151 TETYPE (tree) = getSpec (TTYPE (tree) =
4152 computeType (LTYPE (tree),
4157 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4158 werror (E_CODE_WRITE, "-=");
4162 werror (E_LVALUE_REQUIRED, "-=");
4163 goto errorTreeReturn;
4169 /*------------------------------------------------------------------*/
4170 /*----------------------------*/
4172 /*----------------------------*/
4174 /* this is not a unary operation */
4175 /* if both pointers then problem */
4176 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4178 werror (E_PTR_PLUS_PTR);
4179 goto errorTreeReturn;
4182 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
4184 werror (E_PLUS_INVALID, "+=");
4185 goto errorTreeReturn;
4188 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
4190 werror (E_PLUS_INVALID, "+=");
4191 goto errorTreeReturn;
4194 TETYPE (tree) = getSpec (TTYPE (tree) =
4195 computeType (LTYPE (tree),
4200 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4201 werror (E_CODE_WRITE, "+=");
4205 werror (E_LVALUE_REQUIRED, "+=");
4206 goto errorTreeReturn;
4209 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_TYPE_NONE);
4210 tree->opval.op = '=';
4215 /*------------------------------------------------------------------*/
4216 /*----------------------------*/
4217 /* straight assignemnt */
4218 /*----------------------------*/
4220 /* cannot be an aggregate */
4221 if (IS_AGGREGATE (LTYPE (tree)))
4223 werror (E_AGGR_ASSIGN);
4224 goto errorTreeReturn;
4227 /* they should either match or be castable */
4228 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4230 werror (E_TYPE_MISMATCH, "assignment", " ");
4231 printFromToType(RTYPE(tree),LTYPE(tree));
4234 /* if the left side of the tree is of type void
4235 then report error */
4236 if (IS_VOID (LTYPE (tree)))
4238 werror (E_CAST_ZERO);
4239 printFromToType(RTYPE(tree), LTYPE(tree));
4242 TETYPE (tree) = getSpec (TTYPE (tree) =
4246 if (!tree->initMode ) {
4247 if (IS_CONSTANT(LTYPE(tree)))
4248 werror (E_CODE_WRITE, "=");
4252 werror (E_LVALUE_REQUIRED, "=");
4253 goto errorTreeReturn;
4258 /*------------------------------------------------------------------*/
4259 /*----------------------------*/
4260 /* comma operator */
4261 /*----------------------------*/
4263 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4266 /*------------------------------------------------------------------*/
4267 /*----------------------------*/
4269 /*----------------------------*/
4272 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4273 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4275 if (tree->left->opval.op == '*' && !tree->left->right)
4276 tree->left = tree->left->left;
4279 /* require a function or pointer to function */
4280 if (!IS_FUNC (LTYPE (tree)) && !IS_FUNCPTR (LTYPE (tree)))
4282 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4283 goto errorTreeReturn;
4286 /* if there are parms, make sure that
4287 parms are decorate / process / reverse only once */
4289 !tree->right->decorated)
4294 if (IS_FUNCPTR (LTYPE (tree)))
4295 functype = LTYPE (tree)->next;
4297 functype = LTYPE (tree);
4299 if (processParms (tree->left, FUNC_ARGS(functype),
4300 &tree->right, &parmNumber, TRUE))
4302 goto errorTreeReturn;
4305 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4306 !IFFUNC_ISBUILTIN(functype))
4308 reverseParms (tree->right);
4311 TTYPE (tree) = functype->next;
4312 TETYPE (tree) = getSpec (TTYPE (tree));
4316 /*------------------------------------------------------------------*/
4317 /*----------------------------*/
4318 /* return statement */
4319 /*----------------------------*/
4324 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4326 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4327 printFromToType (RTYPE(tree), currFunc->type->next);
4328 goto errorTreeReturn;
4331 if (IS_VOID (currFunc->type->next)
4333 !IS_VOID (RTYPE (tree)))
4335 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4336 goto errorTreeReturn;
4339 /* if there is going to be a casting required then add it */
4340 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4343 decorateType (newNode (CAST,
4344 newAst_LINK (copyLinkChain (currFunc->type->next)),
4354 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4356 werror (W_VOID_FUNC, currFunc->name);
4357 goto errorTreeReturn;
4360 TTYPE (tree) = TETYPE (tree) = NULL;
4363 /*------------------------------------------------------------------*/
4364 /*----------------------------*/
4365 /* switch statement */
4366 /*----------------------------*/
4368 /* the switch value must be an integer */
4369 if (!IS_INTEGRAL (LTYPE (tree)))
4371 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4372 goto errorTreeReturn;
4375 TTYPE (tree) = TETYPE (tree) = NULL;
4378 /*------------------------------------------------------------------*/
4379 /*----------------------------*/
4381 /*----------------------------*/
4383 tree->left = backPatchLabels (tree->left,
4386 TTYPE (tree) = TETYPE (tree) = NULL;
4389 /*------------------------------------------------------------------*/
4390 /*----------------------------*/
4392 /*----------------------------*/
4395 decorateType (resolveSymbols (AST_FOR (tree, initExpr)), RESULT_TYPE_NONE);
4396 decorateType (resolveSymbols (AST_FOR (tree, condExpr)), RESULT_TYPE_NONE);
4397 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_TYPE_NONE);
4399 /* if the for loop is reversible then
4400 reverse it otherwise do what we normally
4406 if (isLoopReversible (tree, &sym, &init, &end))
4407 return reverseLoop (tree, sym, init, end);
4409 return decorateType (createFor (AST_FOR (tree, trueLabel),
4410 AST_FOR (tree, continueLabel),
4411 AST_FOR (tree, falseLabel),
4412 AST_FOR (tree, condLabel),
4413 AST_FOR (tree, initExpr),
4414 AST_FOR (tree, condExpr),
4415 AST_FOR (tree, loopExpr),
4416 tree->left), RESULT_TYPE_NONE);
4419 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4420 "node PARAM shouldn't be processed here");
4421 /* but in processParams() */
4424 TTYPE (tree) = TETYPE (tree) = NULL;
4428 /* some error found this tree will be killed */
4430 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4431 tree->opval.op = NULLOP;
4437 /*-----------------------------------------------------------------*/
4438 /* sizeofOp - processes size of operation */
4439 /*-----------------------------------------------------------------*/
4441 sizeofOp (sym_link * type)
4446 /* make sure the type is complete and sane */
4447 checkTypeSanity(type, "(sizeof)");
4449 /* get the size and convert it to character */
4450 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4451 if (!size && !IS_VOID(type))
4452 werror (E_SIZEOF_INCOMPLETE_TYPE);
4454 /* now convert into value */
4455 return constVal (buff);
4459 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4460 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4461 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4462 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4463 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4464 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4465 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4467 /*-----------------------------------------------------------------*/
4468 /* backPatchLabels - change and or not operators to flow control */
4469 /*-----------------------------------------------------------------*/
4471 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4477 if (!(IS_ANDORNOT (tree)))
4480 /* if this an and */
4483 static int localLbl = 0;
4486 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4487 localLabel = newSymbol (buffer, NestLevel);
4489 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4491 /* if left is already a IFX then just change the if true label in that */
4492 if (!IS_IFX (tree->left))
4493 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4495 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4496 /* right is a IFX then just join */
4497 if (IS_IFX (tree->right))
4498 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4500 tree->right = createLabel (localLabel, tree->right);
4501 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4503 return newNode (NULLOP, tree->left, tree->right);
4506 /* if this is an or operation */
4509 static int localLbl = 0;
4512 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4513 localLabel = newSymbol (buffer, NestLevel);
4515 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4517 /* if left is already a IFX then just change the if true label in that */
4518 if (!IS_IFX (tree->left))
4519 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4521 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4522 /* right is a IFX then just join */
4523 if (IS_IFX (tree->right))
4524 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4526 tree->right = createLabel (localLabel, tree->right);
4527 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4529 return newNode (NULLOP, tree->left, tree->right);
4535 int wasnot = IS_NOT (tree->left);
4536 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4538 /* if the left is already a IFX */
4539 if (!IS_IFX (tree->left))
4540 tree->left = newNode (IFX, tree->left, NULL);
4544 tree->left->trueLabel = trueLabel;
4545 tree->left->falseLabel = falseLabel;
4549 tree->left->trueLabel = falseLabel;
4550 tree->left->falseLabel = trueLabel;
4557 tree->trueLabel = trueLabel;
4558 tree->falseLabel = falseLabel;
4565 /*-----------------------------------------------------------------*/
4566 /* createBlock - create expression tree for block */
4567 /*-----------------------------------------------------------------*/
4569 createBlock (symbol * decl, ast * body)
4573 /* if the block has nothing */
4577 ex = newNode (BLOCK, NULL, body);
4578 ex->values.sym = decl;
4585 /*-----------------------------------------------------------------*/
4586 /* createLabel - creates the expression tree for labels */
4587 /*-----------------------------------------------------------------*/
4589 createLabel (symbol * label, ast * stmnt)
4592 char name[SDCC_NAME_MAX + 1];
4595 /* must create fresh symbol if the symbol name */
4596 /* exists in the symbol table, since there can */
4597 /* be a variable with the same name as the labl */
4598 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4599 (csym->level == label->level))
4600 label = newSymbol (label->name, label->level);
4602 /* change the name before putting it in add _ */
4603 SNPRINTF(name, sizeof(name), "%s", label->name);
4605 /* put the label in the LabelSymbol table */
4606 /* but first check if a label of the same */
4608 if ((csym = findSym (LabelTab, NULL, name)))
4609 werror (E_DUPLICATE_LABEL, label->name);
4611 addSym (LabelTab, label, name, label->level, 0, 0);
4615 label->key = labelKey++;
4616 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4622 /*-----------------------------------------------------------------*/
4623 /* createCase - generates the parsetree for a case statement */
4624 /*-----------------------------------------------------------------*/
4626 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4628 char caseLbl[SDCC_NAME_MAX + 1];
4632 /* if the switch statement does not exist */
4633 /* then case is out of context */
4636 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4640 caseVal = decorateType (resolveSymbols (caseVal), RESULT_TYPE_NONE);
4641 /* if not a constant then error */
4642 if (!IS_LITERAL (caseVal->ftype))
4644 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4648 /* if not a integer than error */
4649 if (!IS_INTEGRAL (caseVal->ftype))
4651 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4655 /* find the end of the switch values chain */
4656 if (!(val = swStat->values.switchVals.swVals))
4657 swStat->values.switchVals.swVals = caseVal->opval.val;
4660 /* also order the cases according to value */
4662 int cVal = (int) floatFromVal (caseVal->opval.val);
4663 while (val && (int) floatFromVal (val) < cVal)
4669 /* if we reached the end then */
4672 pval->next = caseVal->opval.val;
4674 else if ((int) floatFromVal (val) == cVal)
4676 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
4682 /* we found a value greater than */
4683 /* the current value we must add this */
4684 /* before the value */
4685 caseVal->opval.val->next = val;
4687 /* if this was the first in chain */
4688 if (swStat->values.switchVals.swVals == val)
4689 swStat->values.switchVals.swVals =
4692 pval->next = caseVal->opval.val;
4697 /* create the case label */
4698 SNPRINTF(caseLbl, sizeof(caseLbl),
4700 swStat->values.switchVals.swNum,
4701 (int) floatFromVal (caseVal->opval.val));
4703 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
4708 /*-----------------------------------------------------------------*/
4709 /* createDefault - creates the parse tree for the default statement */
4710 /*-----------------------------------------------------------------*/
4712 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
4714 char defLbl[SDCC_NAME_MAX + 1];
4716 /* if the switch statement does not exist */
4717 /* then case is out of context */
4720 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
4724 if (swStat->values.switchVals.swDefault)
4726 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
4731 /* turn on the default flag */
4732 swStat->values.switchVals.swDefault = 1;
4734 /* create the label */
4735 SNPRINTF (defLbl, sizeof(defLbl),
4736 "_default_%d", swStat->values.switchVals.swNum);
4737 return createLabel (newSymbol (defLbl, 0), stmnt);
4740 /*-----------------------------------------------------------------*/
4741 /* createIf - creates the parsetree for the if statement */
4742 /*-----------------------------------------------------------------*/
4744 createIf (ast * condAst, ast * ifBody, ast * elseBody)
4746 static int Lblnum = 0;
4748 symbol *ifTrue, *ifFalse, *ifEnd;
4750 /* if neither exists */
4751 if (!elseBody && !ifBody) {
4752 // if there are no side effects (i++, j() etc)
4753 if (!hasSEFcalls(condAst)) {
4758 /* create the labels */
4759 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
4760 ifFalse = newSymbol (buffer, NestLevel);
4761 /* if no else body then end == false */
4766 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
4767 ifEnd = newSymbol (buffer, NestLevel);
4770 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
4771 ifTrue = newSymbol (buffer, NestLevel);
4775 /* attach the ifTrue label to the top of it body */
4776 ifBody = createLabel (ifTrue, ifBody);
4777 /* attach a goto end to the ifBody if else is present */
4780 ifBody = newNode (NULLOP, ifBody,
4782 newAst_VALUE (symbolVal (ifEnd)),
4784 /* put the elseLabel on the else body */
4785 elseBody = createLabel (ifFalse, elseBody);
4786 /* out the end at the end of the body */
4787 elseBody = newNode (NULLOP,
4789 createLabel (ifEnd, NULL));
4793 ifBody = newNode (NULLOP, ifBody,
4794 createLabel (ifFalse, NULL));
4796 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
4797 if (IS_IFX (condAst))
4800 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
4802 return newNode (NULLOP, ifTree,
4803 newNode (NULLOP, ifBody, elseBody));
4807 /*-----------------------------------------------------------------*/
4808 /* createDo - creates parse tree for do */
4811 /* _docontinue_n: */
4812 /* condition_expression +-> trueLabel -> _dobody_n */
4814 /* +-> falseLabel-> _dobreak_n */
4816 /*-----------------------------------------------------------------*/
4818 createDo (symbol * trueLabel, symbol * continueLabel,
4819 symbol * falseLabel, ast * condAst, ast * doBody)
4824 /* if the body does not exist then it is simple */
4827 condAst = backPatchLabels (condAst, continueLabel, NULL);
4828 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
4829 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
4830 doTree->trueLabel = continueLabel;
4831 doTree->falseLabel = NULL;
4835 /* otherwise we have a body */
4836 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
4838 /* attach the body label to the top */
4839 doBody = createLabel (trueLabel, doBody);
4840 /* attach the continue label to end of body */
4841 doBody = newNode (NULLOP, doBody,
4842 createLabel (continueLabel, NULL));
4844 /* now put the break label at the end */
4845 if (IS_IFX (condAst))
4848 doTree = newIfxNode (condAst, trueLabel, falseLabel);
4850 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
4852 /* putting it together */
4853 return newNode (NULLOP, doBody, doTree);
4856 /*-----------------------------------------------------------------*/
4857 /* createFor - creates parse tree for 'for' statement */
4860 /* condExpr +-> trueLabel -> _forbody_n */
4862 /* +-> falseLabel-> _forbreak_n */
4865 /* _forcontinue_n: */
4867 /* goto _forcond_n ; */
4869 /*-----------------------------------------------------------------*/
4871 createFor (symbol * trueLabel, symbol * continueLabel,
4872 symbol * falseLabel, symbol * condLabel,
4873 ast * initExpr, ast * condExpr, ast * loopExpr,
4878 /* if loopexpression not present then we can generate it */
4879 /* the same way as a while */
4881 return newNode (NULLOP, initExpr,
4882 createWhile (trueLabel, continueLabel,
4883 falseLabel, condExpr, forBody));
4884 /* vanilla for statement */
4885 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4887 if (condExpr && !IS_IFX (condExpr))
4888 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
4891 /* attach condition label to condition */
4892 condExpr = createLabel (condLabel, condExpr);
4894 /* attach body label to body */
4895 forBody = createLabel (trueLabel, forBody);
4897 /* attach continue to forLoop expression & attach */
4898 /* goto the forcond @ and of loopExpression */
4899 loopExpr = createLabel (continueLabel,
4903 newAst_VALUE (symbolVal (condLabel)),
4905 /* now start putting them together */
4906 forTree = newNode (NULLOP, initExpr, condExpr);
4907 forTree = newNode (NULLOP, forTree, forBody);
4908 forTree = newNode (NULLOP, forTree, loopExpr);
4909 /* finally add the break label */
4910 forTree = newNode (NULLOP, forTree,
4911 createLabel (falseLabel, NULL));
4915 /*-----------------------------------------------------------------*/
4916 /* createWhile - creates parse tree for while statement */
4917 /* the while statement will be created as follows */
4919 /* _while_continue_n: */
4920 /* condition_expression +-> trueLabel -> _while_boby_n */
4922 /* +-> falseLabel -> _while_break_n */
4923 /* _while_body_n: */
4925 /* goto _while_continue_n */
4926 /* _while_break_n: */
4927 /*-----------------------------------------------------------------*/
4929 createWhile (symbol * trueLabel, symbol * continueLabel,
4930 symbol * falseLabel, ast * condExpr, ast * whileBody)
4934 /* put the continue label */
4935 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4936 condExpr = createLabel (continueLabel, condExpr);
4937 condExpr->lineno = 0;
4939 /* put the body label in front of the body */
4940 whileBody = createLabel (trueLabel, whileBody);
4941 whileBody->lineno = 0;
4942 /* put a jump to continue at the end of the body */
4943 /* and put break label at the end of the body */
4944 whileBody = newNode (NULLOP,
4947 newAst_VALUE (symbolVal (continueLabel)),
4948 createLabel (falseLabel, NULL)));
4950 /* put it all together */
4951 if (IS_IFX (condExpr))
4952 whileTree = condExpr;
4955 whileTree = newNode (IFX, condExpr, NULL);
4956 /* put the true & false labels in place */
4957 whileTree->trueLabel = trueLabel;
4958 whileTree->falseLabel = falseLabel;
4961 return newNode (NULLOP, whileTree, whileBody);
4964 /*-----------------------------------------------------------------*/
4965 /* isShiftRightLitVal _BitAndLitVal - helper function */
4966 /*-----------------------------------------------------------------*/
4968 isShiftRightLitVal_BitAndLitVal (ast * tree)
4970 /* if this is not a bit and */
4971 if (!IS_BITAND (tree))
4974 /* will look for tree of the form
4975 ( expr >> litval2) & litval1 */
4976 if (!IS_AST_LIT_VALUE (tree->right))
4979 if (!IS_RIGHT_OP (tree->left))
4982 if (!IS_AST_LIT_VALUE (tree->left->right))
4985 return tree->left->left;
4988 /*-----------------------------------------------------------------*/
4989 /* isBitAndPowOf2 - helper function */
4990 /*-----------------------------------------------------------------*/
4992 isBitAndPow2 (ast * tree)
4994 /* if this is not a bit and */
4995 if (!IS_BITAND (tree))
4998 /* will look for tree of the form
4999 ( expr & (1 << litval) */
5000 if (!IS_AST_LIT_VALUE (tree->right))
5003 return powof2 ((TYPE_UDWORD)AST_LIT_VALUE (tree->right));
5006 /*-----------------------------------------------------------------*/
5007 /* optimizeGetHbit - get highest order bit of the expression */
5008 /*-----------------------------------------------------------------*/
5010 optimizeGetHbit (ast * tree, RESULT_TYPE resultType)
5015 expr = isShiftRightLitVal_BitAndLitVal(tree);
5018 if ((AST_LIT_VALUE (tree->right) != 1) ||
5019 ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
5020 (j = (getSize (TTYPE (expr)) * 8 - 1))))
5023 if (!expr && (resultType == RESULT_TYPE_BIT))
5026 if (isBitAndPow2 (tree) != getSize (TTYPE (expr)) * 8 - 1)
5032 /* make sure the port supports GETHBIT */
5033 if (port->hasExtBitOp
5034 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (expr))))
5037 return decorateType (newNode (GETHBIT, expr, NULL), RESULT_TYPE_NONE);
5040 /*-----------------------------------------------------------------*/
5041 /* optimizeGetAbit - get a single bit of the expression */
5042 /*-----------------------------------------------------------------*/
5044 optimizeGetAbit (ast * tree, RESULT_TYPE resultType)
5049 expr = isShiftRightLitVal_BitAndLitVal(tree);
5052 if (AST_LIT_VALUE (tree->right) != 1)
5054 count = tree->left->right;
5056 if (!expr && (resultType == RESULT_TYPE_BIT))
5058 int p2 = isBitAndPow2 (tree);
5062 count = newAst_VALUE (valueFromLit (p2));
5068 /* make sure the port supports GETABIT */
5069 if (port->hasExtBitOp
5070 && !port->hasExtBitOp(GETABIT, getSize (TTYPE (expr))))
5073 return decorateType (newNode (GETABIT, expr, count), RESULT_TYPE_NONE);
5077 /*-----------------------------------------------------------------*/
5078 /* optimizeGetByte - get a byte of the expression */
5079 /*-----------------------------------------------------------------*/
5081 optimizeGetByte (ast * tree, RESULT_TYPE resultType)
5087 expr = isShiftRightLitVal_BitAndLitVal(tree);
5090 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5091 count = tree->left->right;
5092 if (AST_LIT_VALUE (tree->right) != 0xFF)
5095 if (!expr && resultType == RESULT_TYPE_CHAR)
5097 /* if this is a right shift over a multiple of 8 */
5098 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5100 i = (unsigned int) AST_LIT_VALUE (tree->right);
5101 count = tree->right;
5105 if (!expr || (i == 0) || (i % 8) || (i >= getSize (TTYPE (expr)) * 8))
5108 /* make sure the port supports GETBYTE */
5109 if (port->hasExtBitOp
5110 && !port->hasExtBitOp(GETBYTE, getSize (TTYPE (expr))))
5113 return decorateType (newNode (GETBYTE, expr, count), RESULT_TYPE_NONE);
5116 /*-----------------------------------------------------------------*/
5117 /* optimizeGetWord - get two bytes of the expression */
5118 /*-----------------------------------------------------------------*/
5120 optimizeGetWord (ast * tree, RESULT_TYPE resultType)
5126 expr = isShiftRightLitVal_BitAndLitVal(tree);
5129 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5130 count = tree->left->right;
5131 if (AST_LIT_VALUE (tree->right) != 0xFFFF)
5134 if (!expr && resultType == RESULT_TYPE_INT)
5136 /* if this is a right shift over a multiple of 8 */
5137 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5139 i = (unsigned int) AST_LIT_VALUE (tree->right);
5140 count = tree->right;
5144 if (!expr || (i == 0) || (i % 8) || (i >= (getSize (TTYPE (expr))-1) * 8))
5147 /* make sure the port supports GETWORD */
5148 if (port->hasExtBitOp
5149 && !port->hasExtBitOp(GETWORD, getSize (TTYPE (expr))))
5152 return decorateType (newNode (GETWORD, expr, count), RESULT_TYPE_NONE);
5155 /*-----------------------------------------------------------------*/
5156 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
5157 /*-----------------------------------------------------------------*/
5159 optimizeRRCRLC (ast * root)
5161 /* will look for trees of the form
5162 (?expr << 1) | (?expr >> 7) or
5163 (?expr >> 7) | (?expr << 1) will make that
5164 into a RLC : operation ..
5166 (?expr >> 1) | (?expr << 7) or
5167 (?expr << 7) | (?expr >> 1) will make that
5168 into a RRC operation
5169 note : by 7 I mean (number of bits required to hold the
5171 /* if the root operations is not a | operation the not */
5172 if (!IS_BITOR (root))
5175 /* I have to think of a better way to match patterns this sucks */
5176 /* that aside let start looking for the first case : I use a the
5177 negative check a lot to improve the efficiency */
5178 /* (?expr << 1) | (?expr >> 7) */
5179 if (IS_LEFT_OP (root->left) &&
5180 IS_RIGHT_OP (root->right))
5183 if (!SPEC_USIGN (TETYPE (root->left->left)))
5186 if (!IS_AST_LIT_VALUE (root->left->right) ||
5187 !IS_AST_LIT_VALUE (root->right->right))
5190 /* make sure it is the same expression */
5191 if (!isAstEqual (root->left->left,
5195 if (AST_LIT_VALUE (root->left->right) != 1)
5198 if (AST_LIT_VALUE (root->right->right) !=
5199 (getSize (TTYPE (root->left->left)) * 8 - 1))
5202 /* make sure the port supports RLC */
5203 if (port->hasExtBitOp
5204 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5207 /* whew got the first case : create the AST */
5208 return newNode (RLC, root->left->left, NULL);
5212 /* check for second case */
5213 /* (?expr >> 7) | (?expr << 1) */
5214 if (IS_LEFT_OP (root->right) &&
5215 IS_RIGHT_OP (root->left))
5218 if (!SPEC_USIGN (TETYPE (root->left->left)))
5221 if (!IS_AST_LIT_VALUE (root->left->right) ||
5222 !IS_AST_LIT_VALUE (root->right->right))
5225 /* make sure it is the same symbol */
5226 if (!isAstEqual (root->left->left,
5230 if (AST_LIT_VALUE (root->right->right) != 1)
5233 if (AST_LIT_VALUE (root->left->right) !=
5234 (getSize (TTYPE (root->left->left)) * 8 - 1))
5237 /* make sure the port supports RLC */
5238 if (port->hasExtBitOp
5239 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5242 /* whew got the first case : create the AST */
5243 return newNode (RLC, root->left->left, NULL);
5248 /* third case for RRC */
5249 /* (?symbol >> 1) | (?symbol << 7) */
5250 if (IS_LEFT_OP (root->right) &&
5251 IS_RIGHT_OP (root->left))
5254 if (!SPEC_USIGN (TETYPE (root->left->left)))
5257 if (!IS_AST_LIT_VALUE (root->left->right) ||
5258 !IS_AST_LIT_VALUE (root->right->right))
5261 /* make sure it is the same symbol */
5262 if (!isAstEqual (root->left->left,
5266 if (AST_LIT_VALUE (root->left->right) != 1)
5269 if (AST_LIT_VALUE (root->right->right) !=
5270 (getSize (TTYPE (root->left->left)) * 8 - 1))
5273 /* make sure the port supports RRC */
5274 if (port->hasExtBitOp
5275 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5278 /* whew got the first case : create the AST */
5279 return newNode (RRC, root->left->left, NULL);
5283 /* fourth and last case for now */
5284 /* (?symbol << 7) | (?symbol >> 1) */
5285 if (IS_RIGHT_OP (root->right) &&
5286 IS_LEFT_OP (root->left))
5289 if (!SPEC_USIGN (TETYPE (root->left->left)))
5292 if (!IS_AST_LIT_VALUE (root->left->right) ||
5293 !IS_AST_LIT_VALUE (root->right->right))
5296 /* make sure it is the same symbol */
5297 if (!isAstEqual (root->left->left,
5301 if (AST_LIT_VALUE (root->right->right) != 1)
5304 if (AST_LIT_VALUE (root->left->right) !=
5305 (getSize (TTYPE (root->left->left)) * 8 - 1))
5308 /* make sure the port supports RRC */
5309 if (port->hasExtBitOp
5310 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5313 /* whew got the first case : create the AST */
5314 return newNode (RRC, root->left->left, NULL);
5318 /* not found return root */
5322 /*-----------------------------------------------------------------*/
5323 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
5324 /*-----------------------------------------------------------------*/
5326 optimizeSWAP (ast * root)
5328 /* will look for trees of the form
5329 (?expr << 4) | (?expr >> 4) or
5330 (?expr >> 4) | (?expr << 4) will make that
5331 into a SWAP : operation ..
5332 note : by 4 I mean (number of bits required to hold the
5334 /* if the root operations is not a | operation the not */
5335 if (!IS_BITOR (root))
5338 /* (?expr << 4) | (?expr >> 4) */
5339 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
5340 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
5343 if (!SPEC_USIGN (TETYPE (root->left->left)))
5346 if (!IS_AST_LIT_VALUE (root->left->right) ||
5347 !IS_AST_LIT_VALUE (root->right->right))
5350 /* make sure it is the same expression */
5351 if (!isAstEqual (root->left->left,
5355 if (AST_LIT_VALUE (root->left->right) !=
5356 (getSize (TTYPE (root->left->left)) * 4))
5359 if (AST_LIT_VALUE (root->right->right) !=
5360 (getSize (TTYPE (root->left->left)) * 4))
5363 /* make sure the port supports SWAP */
5364 if (port->hasExtBitOp
5365 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
5368 /* found it : create the AST */
5369 return newNode (SWAP, root->left->left, NULL);
5373 /* not found return root */
5377 /*-----------------------------------------------------------------*/
5378 /* optimizeCompare - optimizes compares for bit variables */
5379 /*-----------------------------------------------------------------*/
5381 optimizeCompare (ast * root)
5383 ast *optExpr = NULL;
5386 unsigned int litValue;
5388 /* if nothing then return nothing */
5392 /* if not a compare op then do leaves */
5393 if (!IS_COMPARE_OP (root))
5395 root->left = optimizeCompare (root->left);
5396 root->right = optimizeCompare (root->right);
5400 /* if left & right are the same then depending
5401 of the operation do */
5402 if (isAstEqual (root->left, root->right))
5404 switch (root->opval.op)
5409 optExpr = newAst_VALUE (constVal ("0"));
5414 optExpr = newAst_VALUE (constVal ("1"));
5418 return decorateType (optExpr, RESULT_TYPE_NONE);
5421 vleft = (root->left->type == EX_VALUE ?
5422 root->left->opval.val : NULL);
5424 vright = (root->right->type == EX_VALUE ?
5425 root->right->opval.val : NULL);
5427 /* if left is a BITVAR in BITSPACE */
5428 /* and right is a LITERAL then opt- */
5429 /* imize else do nothing */
5430 if (vleft && vright &&
5431 IS_BITVAR (vleft->etype) &&
5432 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5433 IS_LITERAL (vright->etype))
5436 /* if right side > 1 then comparison may never succeed */
5437 if ((litValue = (int) floatFromVal (vright)) > 1)
5439 werror (W_BAD_COMPARE);
5445 switch (root->opval.op)
5447 case '>': /* bit value greater than 1 cannot be */
5448 werror (W_BAD_COMPARE);
5452 case '<': /* bit value < 1 means 0 */
5454 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5457 case LE_OP: /* bit value <= 1 means no check */
5458 optExpr = newAst_VALUE (vright);
5461 case GE_OP: /* bit value >= 1 means only check for = */
5463 optExpr = newAst_VALUE (vleft);
5468 { /* literal is zero */
5469 switch (root->opval.op)
5471 case '<': /* bit value < 0 cannot be */
5472 werror (W_BAD_COMPARE);
5476 case '>': /* bit value > 0 means 1 */
5478 optExpr = newAst_VALUE (vleft);
5481 case LE_OP: /* bit value <= 0 means no check */
5482 case GE_OP: /* bit value >= 0 means no check */
5483 werror (W_BAD_COMPARE);
5487 case EQ_OP: /* bit == 0 means ! of bit */
5488 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5492 return decorateType (resolveSymbols (optExpr), RESULT_TYPE_NONE);
5493 } /* end-of-if of BITVAR */
5498 /*-----------------------------------------------------------------*/
5499 /* addSymToBlock : adds the symbol to the first block we find */
5500 /*-----------------------------------------------------------------*/
5502 addSymToBlock (symbol * sym, ast * tree)
5504 /* reached end of tree or a leaf */
5505 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5509 if (IS_AST_OP (tree) &&
5510 tree->opval.op == BLOCK)
5513 symbol *lsym = copySymbol (sym);
5515 lsym->next = AST_VALUES (tree, sym);
5516 AST_VALUES (tree, sym) = lsym;
5520 addSymToBlock (sym, tree->left);
5521 addSymToBlock (sym, tree->right);
5524 /*-----------------------------------------------------------------*/
5525 /* processRegParms - do processing for register parameters */
5526 /*-----------------------------------------------------------------*/
5528 processRegParms (value * args, ast * body)
5532 if (IS_REGPARM (args->etype))
5533 addSymToBlock (args->sym, body);
5538 /*-----------------------------------------------------------------*/
5539 /* resetParmKey - resets the operandkeys for the symbols */
5540 /*-----------------------------------------------------------------*/
5541 DEFSETFUNC (resetParmKey)
5552 /*-----------------------------------------------------------------*/
5553 /* createFunction - This is the key node that calls the iCode for */
5554 /* generating the code for a function. Note code */
5555 /* is generated function by function, later when */
5556 /* add inter-procedural analysis this will change */
5557 /*-----------------------------------------------------------------*/
5559 createFunction (symbol * name, ast * body)
5565 iCode *piCode = NULL;
5567 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5568 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5570 /* if check function return 0 then some problem */
5571 if (checkFunction (name, NULL) == 0)
5574 /* create a dummy block if none exists */
5576 body = newNode (BLOCK, NULL, NULL);
5580 /* check if the function name already in the symbol table */
5581 if ((csym = findSym (SymbolTab, NULL, name->name)))
5584 /* special case for compiler defined functions
5585 we need to add the name to the publics list : this
5586 actually means we are now compiling the compiler
5590 addSet (&publics, name);
5595 addSymChain (&name);
5596 allocVariables (name);
5598 name->lastLine = mylineno;
5601 /* set the stack pointer */
5602 stackPtr = -port->stack.direction * port->stack.call_overhead;
5605 if (IFFUNC_ISISR (name->type))
5606 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5608 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5610 if (options.useXstack)
5611 xstackPtr -= port->stack.direction * port->stack.reent_overhead;
5613 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5616 fetype = getSpec (name->type); /* get the specifier for the function */
5617 /* if this is a reentrant function then */
5618 if (IFFUNC_ISREENT (name->type))
5621 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5623 /* do processing for parameters that are passed in registers */
5624 processRegParms (FUNC_ARGS(name->type), body);
5626 /* set the stack pointer */
5630 /* allocate & autoinit the block variables */
5631 processBlockVars (body, &stack, ALLOCATE);
5633 /* save the stack information */
5634 if (options.useXstack)
5635 name->xstack = SPEC_STAK (fetype) = stack;
5637 name->stack = SPEC_STAK (fetype) = stack;
5639 /* name needs to be mangled */
5640 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5642 body = resolveSymbols (body); /* resolve the symbols */
5643 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5646 ex = newAst_VALUE (symbolVal (name)); /* create name */
5647 ex = newNode (FUNCTION, ex, body);
5648 ex->values.args = FUNC_ARGS(name->type);
5650 if (options.dump_tree) PA(ex);
5653 werror (E_FUNC_NO_CODE, name->name);
5657 /* create the node & generate intermediate code */
5659 codeOutFile = code->oFile;
5660 piCode = iCodeFromAst (ex);
5664 werror (E_FUNC_NO_CODE, name->name);
5668 eBBlockFromiCode (piCode);
5670 /* if there are any statics then do them */
5673 GcurMemmap = statsg;
5674 codeOutFile = statsg->oFile;
5675 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_TYPE_NONE)));
5681 /* dealloc the block variables */
5682 processBlockVars (body, &stack, DEALLOCATE);
5683 outputDebugStackSymbols();
5684 /* deallocate paramaters */
5685 deallocParms (FUNC_ARGS(name->type));
5687 if (IFFUNC_ISREENT (name->type))
5690 /* we are done freeup memory & cleanup */
5692 if (port->reset_labelKey) labelKey = 1;
5694 FUNC_HASBODY(name->type) = 1;
5695 addSet (&operKeyReset, name);
5696 applyToSet (operKeyReset, resetParmKey);
5701 cleanUpLevel (LabelTab, 0);
5702 cleanUpBlock (StructTab, 1);
5703 cleanUpBlock (TypedefTab, 1);
5705 xstack->syms = NULL;
5706 istack->syms = NULL;
5711 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
5712 /*-----------------------------------------------------------------*/
5713 /* ast_print : prints the ast (for debugging purposes) */
5714 /*-----------------------------------------------------------------*/
5716 void ast_print (ast * tree, FILE *outfile, int indent)
5721 /* can print only decorated trees */
5722 if (!tree->decorated) return;
5724 /* if any child is an error | this one is an error do nothing */
5725 if (tree->isError ||
5726 (tree->left && tree->left->isError) ||
5727 (tree->right && tree->right->isError)) {
5728 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
5732 /* print the line */
5733 /* if not block & function */
5734 if (tree->type == EX_OP &&
5735 (tree->opval.op != FUNCTION &&
5736 tree->opval.op != BLOCK &&
5737 tree->opval.op != NULLOP)) {
5740 if (tree->opval.op == FUNCTION) {
5742 value *args=FUNC_ARGS(tree->left->opval.val->type);
5743 fprintf(outfile,"FUNCTION (%s=%p) type (",
5744 tree->left->opval.val->name, tree);
5745 printTypeChain (tree->left->opval.val->type->next,outfile);
5746 fprintf(outfile,") args (");
5749 fprintf (outfile, ", ");
5751 printTypeChain (args ? args->type : NULL, outfile);
5753 args= args ? args->next : NULL;
5755 fprintf(outfile,")\n");
5756 ast_print(tree->left,outfile,indent);
5757 ast_print(tree->right,outfile,indent);
5760 if (tree->opval.op == BLOCK) {
5761 symbol *decls = tree->values.sym;
5762 INDENT(indent,outfile);
5763 fprintf(outfile,"{\n");
5765 INDENT(indent+2,outfile);
5766 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
5767 decls->name, decls);
5768 printTypeChain(decls->type,outfile);
5769 fprintf(outfile,")\n");
5771 decls = decls->next;
5773 ast_print(tree->right,outfile,indent+2);
5774 INDENT(indent,outfile);
5775 fprintf(outfile,"}\n");
5778 if (tree->opval.op == NULLOP) {
5779 ast_print(tree->left,outfile,indent);
5780 ast_print(tree->right,outfile,indent);
5783 INDENT(indent,outfile);
5785 /*------------------------------------------------------------------*/
5786 /*----------------------------*/
5787 /* leaf has been reached */
5788 /*----------------------------*/
5789 /* if this is of type value */
5790 /* just get the type */
5791 if (tree->type == EX_VALUE) {
5793 if (IS_LITERAL (tree->opval.val->etype)) {
5794 fprintf(outfile,"CONSTANT (%p) value = ", tree);
5795 if (SPEC_USIGN (tree->opval.val->etype))
5796 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
5798 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
5799 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
5800 floatFromVal(tree->opval.val));
5801 } else if (tree->opval.val->sym) {
5802 /* if the undefined flag is set then give error message */
5803 if (tree->opval.val->sym->undefined) {
5804 fprintf(outfile,"UNDEFINED SYMBOL ");
5806 fprintf(outfile,"SYMBOL ");
5808 fprintf(outfile,"(%s=%p)",
5809 tree->opval.val->sym->name,tree);
5812 fprintf(outfile," type (");
5813 printTypeChain(tree->ftype,outfile);
5814 fprintf(outfile,")\n");
5816 fprintf(outfile,"\n");
5821 /* if type link for the case of cast */
5822 if (tree->type == EX_LINK) {
5823 fprintf(outfile,"TYPENODE (%p) type = (",tree);
5824 printTypeChain(tree->opval.lnk,outfile);
5825 fprintf(outfile,")\n");
5830 /* depending on type of operator do */
5832 switch (tree->opval.op) {
5833 /*------------------------------------------------------------------*/
5834 /*----------------------------*/
5836 /*----------------------------*/
5838 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
5839 printTypeChain(tree->ftype,outfile);
5840 fprintf(outfile,")\n");
5841 ast_print(tree->left,outfile,indent+2);
5842 ast_print(tree->right,outfile,indent+2);
5845 /*------------------------------------------------------------------*/
5846 /*----------------------------*/
5848 /*----------------------------*/
5850 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
5851 printTypeChain(tree->ftype,outfile);
5852 fprintf(outfile,")\n");
5853 ast_print(tree->left,outfile,indent+2);
5854 ast_print(tree->right,outfile,indent+2);
5857 /*------------------------------------------------------------------*/
5858 /*----------------------------*/
5859 /* struct/union pointer */
5860 /*----------------------------*/
5862 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
5863 printTypeChain(tree->ftype,outfile);
5864 fprintf(outfile,")\n");
5865 ast_print(tree->left,outfile,indent+2);
5866 ast_print(tree->right,outfile,indent+2);
5869 /*------------------------------------------------------------------*/
5870 /*----------------------------*/
5871 /* ++/-- operation */
5872 /*----------------------------*/
5875 fprintf(outfile,"post-");
5877 fprintf(outfile,"pre-");
5878 fprintf(outfile,"INC_OP (%p) type (",tree);
5879 printTypeChain(tree->ftype,outfile);
5880 fprintf(outfile,")\n");
5881 ast_print(tree->left,outfile,indent+2); /* postincrement case */
5882 ast_print(tree->right,outfile,indent+2); /* preincrement case */
5887 fprintf(outfile,"post-");
5889 fprintf(outfile,"pre-");
5890 fprintf(outfile,"DEC_OP (%p) type (",tree);
5891 printTypeChain(tree->ftype,outfile);
5892 fprintf(outfile,")\n");
5893 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
5894 ast_print(tree->right,outfile,indent+2); /* predecrement case */
5897 /*------------------------------------------------------------------*/
5898 /*----------------------------*/
5900 /*----------------------------*/
5903 fprintf(outfile,"& (%p) type (",tree);
5904 printTypeChain(tree->ftype,outfile);
5905 fprintf(outfile,")\n");
5906 ast_print(tree->left,outfile,indent+2);
5907 ast_print(tree->right,outfile,indent+2);
5909 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
5910 printTypeChain(tree->ftype,outfile);
5911 fprintf(outfile,")\n");
5912 ast_print(tree->left,outfile,indent+2);
5913 ast_print(tree->right,outfile,indent+2);
5916 /*----------------------------*/
5918 /*----------------------------*/
5920 fprintf(outfile,"OR (%p) type (",tree);
5921 printTypeChain(tree->ftype,outfile);
5922 fprintf(outfile,")\n");
5923 ast_print(tree->left,outfile,indent+2);
5924 ast_print(tree->right,outfile,indent+2);
5926 /*------------------------------------------------------------------*/
5927 /*----------------------------*/
5929 /*----------------------------*/
5931 fprintf(outfile,"XOR (%p) type (",tree);
5932 printTypeChain(tree->ftype,outfile);
5933 fprintf(outfile,")\n");
5934 ast_print(tree->left,outfile,indent+2);
5935 ast_print(tree->right,outfile,indent+2);
5938 /*------------------------------------------------------------------*/
5939 /*----------------------------*/
5941 /*----------------------------*/
5943 fprintf(outfile,"DIV (%p) type (",tree);
5944 printTypeChain(tree->ftype,outfile);
5945 fprintf(outfile,")\n");
5946 ast_print(tree->left,outfile,indent+2);
5947 ast_print(tree->right,outfile,indent+2);
5949 /*------------------------------------------------------------------*/
5950 /*----------------------------*/
5952 /*----------------------------*/
5954 fprintf(outfile,"MOD (%p) type (",tree);
5955 printTypeChain(tree->ftype,outfile);
5956 fprintf(outfile,")\n");
5957 ast_print(tree->left,outfile,indent+2);
5958 ast_print(tree->right,outfile,indent+2);
5961 /*------------------------------------------------------------------*/
5962 /*----------------------------*/
5963 /* address dereference */
5964 /*----------------------------*/
5965 case '*': /* can be unary : if right is null then unary operation */
5967 fprintf(outfile,"DEREF (%p) type (",tree);
5968 printTypeChain(tree->ftype,outfile);
5969 fprintf(outfile,")\n");
5970 ast_print(tree->left,outfile,indent+2);
5973 /*------------------------------------------------------------------*/
5974 /*----------------------------*/
5975 /* multiplication */
5976 /*----------------------------*/
5977 fprintf(outfile,"MULT (%p) type (",tree);
5978 printTypeChain(tree->ftype,outfile);
5979 fprintf(outfile,")\n");
5980 ast_print(tree->left,outfile,indent+2);
5981 ast_print(tree->right,outfile,indent+2);
5985 /*------------------------------------------------------------------*/
5986 /*----------------------------*/
5987 /* unary '+' operator */
5988 /*----------------------------*/
5992 fprintf(outfile,"UPLUS (%p) type (",tree);
5993 printTypeChain(tree->ftype,outfile);
5994 fprintf(outfile,")\n");
5995 ast_print(tree->left,outfile,indent+2);
5997 /*------------------------------------------------------------------*/
5998 /*----------------------------*/
6000 /*----------------------------*/
6001 fprintf(outfile,"ADD (%p) type (",tree);
6002 printTypeChain(tree->ftype,outfile);
6003 fprintf(outfile,")\n");
6004 ast_print(tree->left,outfile,indent+2);
6005 ast_print(tree->right,outfile,indent+2);
6008 /*------------------------------------------------------------------*/
6009 /*----------------------------*/
6011 /*----------------------------*/
6012 case '-': /* can be unary */
6014 fprintf(outfile,"UMINUS (%p) type (",tree);
6015 printTypeChain(tree->ftype,outfile);
6016 fprintf(outfile,")\n");
6017 ast_print(tree->left,outfile,indent+2);
6019 /*------------------------------------------------------------------*/
6020 /*----------------------------*/
6022 /*----------------------------*/
6023 fprintf(outfile,"SUB (%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);
6030 /*------------------------------------------------------------------*/
6031 /*----------------------------*/
6033 /*----------------------------*/
6035 fprintf(outfile,"COMPL (%p) type (",tree);
6036 printTypeChain(tree->ftype,outfile);
6037 fprintf(outfile,")\n");
6038 ast_print(tree->left,outfile,indent+2);
6040 /*------------------------------------------------------------------*/
6041 /*----------------------------*/
6043 /*----------------------------*/
6045 fprintf(outfile,"NOT (%p) type (",tree);
6046 printTypeChain(tree->ftype,outfile);
6047 fprintf(outfile,")\n");
6048 ast_print(tree->left,outfile,indent+2);
6050 /*------------------------------------------------------------------*/
6051 /*----------------------------*/
6053 /*----------------------------*/
6055 fprintf(outfile,"RRC (%p) type (",tree);
6056 printTypeChain(tree->ftype,outfile);
6057 fprintf(outfile,")\n");
6058 ast_print(tree->left,outfile,indent+2);
6062 fprintf(outfile,"RLC (%p) type (",tree);
6063 printTypeChain(tree->ftype,outfile);
6064 fprintf(outfile,")\n");
6065 ast_print(tree->left,outfile,indent+2);
6068 fprintf(outfile,"SWAP (%p) type (",tree);
6069 printTypeChain(tree->ftype,outfile);
6070 fprintf(outfile,")\n");
6071 ast_print(tree->left,outfile,indent+2);
6074 fprintf(outfile,"GETHBIT (%p) type (",tree);
6075 printTypeChain(tree->ftype,outfile);
6076 fprintf(outfile,")\n");
6077 ast_print(tree->left,outfile,indent+2);
6080 fprintf(outfile,"GETABIT (%p) type (",tree);
6081 printTypeChain(tree->ftype,outfile);
6082 fprintf(outfile,")\n");
6083 ast_print(tree->left,outfile,indent+2);
6084 ast_print(tree->right,outfile,indent+2);
6087 fprintf(outfile,"GETBYTE (%p) type (",tree);
6088 printTypeChain(tree->ftype,outfile);
6089 fprintf(outfile,")\n");
6090 ast_print(tree->left,outfile,indent+2);
6091 ast_print(tree->right,outfile,indent+2);
6094 fprintf(outfile,"GETWORD (%p) type (",tree);
6095 printTypeChain(tree->ftype,outfile);
6096 fprintf(outfile,")\n");
6097 ast_print(tree->left,outfile,indent+2);
6098 ast_print(tree->right,outfile,indent+2);
6101 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
6102 printTypeChain(tree->ftype,outfile);
6103 fprintf(outfile,")\n");
6104 ast_print(tree->left,outfile,indent+2);
6105 ast_print(tree->right,outfile,indent+2);
6108 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
6109 printTypeChain(tree->ftype,outfile);
6110 fprintf(outfile,")\n");
6111 ast_print(tree->left,outfile,indent+2);
6112 ast_print(tree->right,outfile,indent+2);
6114 /*------------------------------------------------------------------*/
6115 /*----------------------------*/
6117 /*----------------------------*/
6118 case CAST: /* change the type */
6119 fprintf(outfile,"CAST (%p) from type (",tree);
6120 printTypeChain(tree->right->ftype,outfile);
6121 fprintf(outfile,") to type (");
6122 printTypeChain(tree->ftype,outfile);
6123 fprintf(outfile,")\n");
6124 ast_print(tree->right,outfile,indent+2);
6128 fprintf(outfile,"ANDAND (%p) type (",tree);
6129 printTypeChain(tree->ftype,outfile);
6130 fprintf(outfile,")\n");
6131 ast_print(tree->left,outfile,indent+2);
6132 ast_print(tree->right,outfile,indent+2);
6135 fprintf(outfile,"OROR (%p) type (",tree);
6136 printTypeChain(tree->ftype,outfile);
6137 fprintf(outfile,")\n");
6138 ast_print(tree->left,outfile,indent+2);
6139 ast_print(tree->right,outfile,indent+2);
6142 /*------------------------------------------------------------------*/
6143 /*----------------------------*/
6144 /* comparison operators */
6145 /*----------------------------*/
6147 fprintf(outfile,"GT(>) (%p) type (",tree);
6148 printTypeChain(tree->ftype,outfile);
6149 fprintf(outfile,")\n");
6150 ast_print(tree->left,outfile,indent+2);
6151 ast_print(tree->right,outfile,indent+2);
6154 fprintf(outfile,"LT(<) (%p) type (",tree);
6155 printTypeChain(tree->ftype,outfile);
6156 fprintf(outfile,")\n");
6157 ast_print(tree->left,outfile,indent+2);
6158 ast_print(tree->right,outfile,indent+2);
6161 fprintf(outfile,"LE(<=) (%p) type (",tree);
6162 printTypeChain(tree->ftype,outfile);
6163 fprintf(outfile,")\n");
6164 ast_print(tree->left,outfile,indent+2);
6165 ast_print(tree->right,outfile,indent+2);
6168 fprintf(outfile,"GE(>=) (%p) type (",tree);
6169 printTypeChain(tree->ftype,outfile);
6170 fprintf(outfile,")\n");
6171 ast_print(tree->left,outfile,indent+2);
6172 ast_print(tree->right,outfile,indent+2);
6175 fprintf(outfile,"EQ(==) (%p) type (",tree);
6176 printTypeChain(tree->ftype,outfile);
6177 fprintf(outfile,")\n");
6178 ast_print(tree->left,outfile,indent+2);
6179 ast_print(tree->right,outfile,indent+2);
6182 fprintf(outfile,"NE(!=) (%p) type (",tree);
6183 printTypeChain(tree->ftype,outfile);
6184 fprintf(outfile,")\n");
6185 ast_print(tree->left,outfile,indent+2);
6186 ast_print(tree->right,outfile,indent+2);
6187 /*------------------------------------------------------------------*/
6188 /*----------------------------*/
6190 /*----------------------------*/
6191 case SIZEOF: /* evaluate wihout code generation */
6192 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
6195 /*------------------------------------------------------------------*/
6196 /*----------------------------*/
6197 /* conditional operator '?' */
6198 /*----------------------------*/
6200 fprintf(outfile,"QUEST(?) (%p) type (",tree);
6201 printTypeChain(tree->ftype,outfile);
6202 fprintf(outfile,")\n");
6203 ast_print(tree->left,outfile,indent+2);
6204 ast_print(tree->right,outfile,indent+2);
6208 fprintf(outfile,"COLON(:) (%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 /*------------------------------------------------------------------*/
6216 /*----------------------------*/
6217 /* assignment operators */
6218 /*----------------------------*/
6220 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
6221 printTypeChain(tree->ftype,outfile);
6222 fprintf(outfile,")\n");
6223 ast_print(tree->left,outfile,indent+2);
6224 ast_print(tree->right,outfile,indent+2);
6227 fprintf(outfile,"DIVASS(/=) (%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,"ANDASS(&=) (%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,"ORASS(|=) (%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,"XORASS(^=) (%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,"RSHFTASS(>>=) (%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,"LSHFTASS(<<=) (%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);
6268 /*------------------------------------------------------------------*/
6269 /*----------------------------*/
6271 /*----------------------------*/
6273 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
6274 printTypeChain(tree->ftype,outfile);
6275 fprintf(outfile,")\n");
6276 ast_print(tree->left,outfile,indent+2);
6277 ast_print(tree->right,outfile,indent+2);
6279 /*------------------------------------------------------------------*/
6280 /*----------------------------*/
6282 /*----------------------------*/
6284 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
6285 printTypeChain(tree->ftype,outfile);
6286 fprintf(outfile,")\n");
6287 ast_print(tree->left,outfile,indent+2);
6288 ast_print(tree->right,outfile,indent+2);
6290 /*------------------------------------------------------------------*/
6291 /*----------------------------*/
6292 /* straight assignemnt */
6293 /*----------------------------*/
6295 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
6296 printTypeChain(tree->ftype,outfile);
6297 fprintf(outfile,")\n");
6298 ast_print(tree->left,outfile,indent+2);
6299 ast_print(tree->right,outfile,indent+2);
6301 /*------------------------------------------------------------------*/
6302 /*----------------------------*/
6303 /* comma operator */
6304 /*----------------------------*/
6306 fprintf(outfile,"COMMA(,) (%p) type (",tree);
6307 printTypeChain(tree->ftype,outfile);
6308 fprintf(outfile,")\n");
6309 ast_print(tree->left,outfile,indent+2);
6310 ast_print(tree->right,outfile,indent+2);
6312 /*------------------------------------------------------------------*/
6313 /*----------------------------*/
6315 /*----------------------------*/
6318 fprintf(outfile,"CALL (%p) type (",tree);
6319 printTypeChain(tree->ftype,outfile);
6320 fprintf(outfile,")\n");
6321 ast_print(tree->left,outfile,indent+2);
6322 ast_print(tree->right,outfile,indent+2);
6325 fprintf(outfile,"PARMS\n");
6326 ast_print(tree->left,outfile,indent+2);
6327 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
6328 ast_print(tree->right,outfile,indent+2);
6331 /*------------------------------------------------------------------*/
6332 /*----------------------------*/
6333 /* return statement */
6334 /*----------------------------*/
6336 fprintf(outfile,"RETURN (%p) type (",tree);
6338 printTypeChain(tree->right->ftype,outfile);
6340 fprintf(outfile,")\n");
6341 ast_print(tree->right,outfile,indent+2);
6343 /*------------------------------------------------------------------*/
6344 /*----------------------------*/
6345 /* label statement */
6346 /*----------------------------*/
6348 fprintf(outfile,"LABEL (%p)\n",tree);
6349 ast_print(tree->left,outfile,indent+2);
6350 ast_print(tree->right,outfile,indent);
6352 /*------------------------------------------------------------------*/
6353 /*----------------------------*/
6354 /* switch statement */
6355 /*----------------------------*/
6359 fprintf(outfile,"SWITCH (%p) ",tree);
6360 ast_print(tree->left,outfile,0);
6361 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
6362 INDENT(indent+2,outfile);
6363 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
6364 (int) floatFromVal(val),
6365 tree->values.switchVals.swNum,
6366 (int) floatFromVal(val));
6368 ast_print(tree->right,outfile,indent);
6371 /*------------------------------------------------------------------*/
6372 /*----------------------------*/
6374 /*----------------------------*/
6376 fprintf(outfile,"IF (%p) \n",tree);
6377 ast_print(tree->left,outfile,indent+2);
6378 if (tree->trueLabel) {
6379 INDENT(indent+2,outfile);
6380 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
6382 if (tree->falseLabel) {
6383 INDENT(indent+2,outfile);
6384 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
6386 ast_print(tree->right,outfile,indent+2);
6388 /*----------------------------*/
6389 /* goto Statement */
6390 /*----------------------------*/
6392 fprintf(outfile,"GOTO (%p) \n",tree);
6393 ast_print(tree->left,outfile,indent+2);
6394 fprintf(outfile,"\n");
6396 /*------------------------------------------------------------------*/
6397 /*----------------------------*/
6399 /*----------------------------*/
6401 fprintf(outfile,"FOR (%p) \n",tree);
6402 if (AST_FOR( tree, initExpr)) {
6403 INDENT(indent+2,outfile);
6404 fprintf(outfile,"INIT EXPR ");
6405 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6407 if (AST_FOR( tree, condExpr)) {
6408 INDENT(indent+2,outfile);
6409 fprintf(outfile,"COND EXPR ");
6410 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6412 if (AST_FOR( tree, loopExpr)) {
6413 INDENT(indent+2,outfile);
6414 fprintf(outfile,"LOOP EXPR ");
6415 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6417 fprintf(outfile,"FOR LOOP BODY \n");
6418 ast_print(tree->left,outfile,indent+2);
6421 fprintf(outfile,"CRITICAL (%p) \n",tree);
6422 ast_print(tree->left,outfile,indent+2);
6430 ast_print(t,stdout,0);
6435 /*-----------------------------------------------------------------*/
6436 /* astErrors : returns non-zero if errors present in tree */
6437 /*-----------------------------------------------------------------*/
6438 int astErrors(ast *t)
6447 if (t->type == EX_VALUE
6448 && t->opval.val->sym
6449 && t->opval.val->sym->undefined)
6452 errors += astErrors(t->left);
6453 errors += astErrors(t->right);