1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
25 #define DEBUG_CF(x) /* puts(x); */
31 set *operKeyReset = NULL;
32 ast *staticAutos = NULL;
35 #define LRVAL(x) x->left->rvalue
36 #define RRVAL(x) x->right->rvalue
37 #define TRVAL(x) x->rvalue
38 #define LLVAL(x) x->left->lvalue
39 #define RLVAL(x) x->right->lvalue
40 #define TLVAL(x) x->lvalue
41 #define RTYPE(x) x->right->ftype
42 #define RETYPE(x) x->right->etype
43 #define LTYPE(x) x->left->ftype
44 #define LETYPE(x) x->left->etype
45 #define TTYPE(x) x->ftype
46 #define TETYPE(x) x->etype
52 symbol *currFunc=NULL;
53 static ast *createIval (ast *, sym_link *, initList *, ast *, ast *);
54 static ast *createIvalCharPtr (ast *, sym_link *, ast *, ast *);
55 static ast *optimizeCompare (ast *);
56 ast *optimizeRRCRLC (ast *);
57 ast *optimizeSWAP (ast *);
58 ast *optimizeGetHbit (ast *, RESULT_TYPE);
59 ast *optimizeGetAbit (ast *, RESULT_TYPE);
60 ast *optimizeGetByte (ast *, RESULT_TYPE);
61 ast *optimizeGetWord (ast *, RESULT_TYPE);
62 static ast *backPatchLabels (ast *, symbol *, symbol *);
65 memmap *GcurMemmap=NULL; /* points to the memmap that's currently active */
70 printTypeChain (tree->ftype, stdout);
75 /*-----------------------------------------------------------------*/
76 /* newAst - creates a fresh node for an expression tree */
77 /*-----------------------------------------------------------------*/
79 newAst_ (unsigned type)
82 static int oldLineno = 0;
84 ex = Safe_alloc ( sizeof (ast));
87 ex->lineno = (noLineno ? oldLineno : mylineno);
88 ex->filename = currFname;
89 ex->level = NestLevel;
90 ex->block = currBlockno;
91 ex->initMode = inInitMode;
92 ex->seqPoint = seqPointNo;
97 newAst_VALUE (value * val)
99 ast *ex = newAst_ (EX_VALUE);
105 newAst_OP (unsigned op)
107 ast *ex = newAst_ (EX_OP);
113 newAst_LINK (sym_link * val)
115 ast *ex = newAst_ (EX_LINK);
120 /*-----------------------------------------------------------------*/
121 /* newNode - creates a new node */
122 /*-----------------------------------------------------------------*/
124 newNode (long op, ast * left, ast * right)
135 /*-----------------------------------------------------------------*/
136 /* newIfxNode - creates a new Ifx Node */
137 /*-----------------------------------------------------------------*/
139 newIfxNode (ast * condAst, symbol * trueLabel, symbol * falseLabel)
143 /* if this is a literal then we already know the result */
144 if (condAst->etype && IS_LITERAL (condAst->etype))
146 /* then depending on the expression value */
147 if (floatFromVal (condAst->opval.val))
148 ifxNode = newNode (GOTO,
149 newAst_VALUE (symbolVal (trueLabel)),
152 ifxNode = newNode (GOTO,
153 newAst_VALUE (symbolVal (falseLabel)),
158 ifxNode = newNode (IFX, condAst, NULL);
159 ifxNode->trueLabel = trueLabel;
160 ifxNode->falseLabel = falseLabel;
166 /*-----------------------------------------------------------------*/
167 /* copyAstValues - copies value portion of ast if needed */
168 /*-----------------------------------------------------------------*/
170 copyAstValues (ast * dest, ast * src)
172 switch (src->opval.op)
175 dest->values.sym = copySymbolChain (src->values.sym);
179 dest->values.switchVals.swVals =
180 copyValue (src->values.switchVals.swVals);
181 dest->values.switchVals.swDefault =
182 src->values.switchVals.swDefault;
183 dest->values.switchVals.swNum =
184 src->values.switchVals.swNum;
188 dest->values.inlineasm = Safe_strdup(src->values.inlineasm);
192 dest->values.constlist = copyLiteralList(src->values.constlist);
196 AST_FOR (dest, trueLabel) = copySymbol (AST_FOR (src, trueLabel));
197 AST_FOR (dest, continueLabel) = copySymbol (AST_FOR (src, continueLabel));
198 AST_FOR (dest, falseLabel) = copySymbol (AST_FOR (src, falseLabel));
199 AST_FOR (dest, condLabel) = copySymbol (AST_FOR (src, condLabel));
200 AST_FOR (dest, initExpr) = copyAst (AST_FOR (src, initExpr));
201 AST_FOR (dest, condExpr) = copyAst (AST_FOR (src, condExpr));
202 AST_FOR (dest, loopExpr) = copyAst (AST_FOR (src, loopExpr));
207 /*-----------------------------------------------------------------*/
208 /* copyAst - makes a copy of a given astession */
209 /*-----------------------------------------------------------------*/
218 dest = Safe_alloc ( sizeof (ast));
220 dest->type = src->type;
221 dest->lineno = src->lineno;
222 dest->level = src->level;
223 dest->funcName = src->funcName;
224 dest->reversed = src->reversed;
227 dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
229 /* if this is a leaf */
231 if (src->type == EX_VALUE)
233 dest->opval.val = copyValue (src->opval.val);
238 if (src->type == EX_LINK)
240 dest->opval.lnk = copyLinkChain (src->opval.lnk);
244 dest->opval.op = src->opval.op;
246 /* if this is a node that has special values */
247 copyAstValues (dest, src);
249 dest->trueLabel = copySymbol (src->trueLabel);
250 dest->falseLabel = copySymbol (src->falseLabel);
251 dest->left = copyAst (src->left);
252 dest->right = copyAst (src->right);
258 /*-----------------------------------------------------------------*/
259 /* removeIncDecOps: remove for side effects in *_ASSIGN's */
260 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
261 /*-----------------------------------------------------------------*/
262 ast *removeIncDecOps (ast * tree) {
264 // traverse the tree and remove inc/dec ops
269 if (tree->type == EX_OP &&
270 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
277 tree->left=removeIncDecOps(tree->left);
278 tree->right=removeIncDecOps(tree->right);
283 /*-----------------------------------------------------------------*/
284 /* removePreIncDecOps: remove for side effects in *_ASSIGN's */
285 /* "*++s += 3" -> "*++s = *++s + 3" */
286 /*-----------------------------------------------------------------*/
287 ast *removePreIncDecOps (ast * tree) {
289 // traverse the tree and remove pre-inc/dec ops
294 if (tree->type == EX_OP &&
295 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
300 tree->left=removePreIncDecOps(tree->left);
301 tree->right=removePreIncDecOps(tree->right);
306 /*-----------------------------------------------------------------*/
307 /* removePostIncDecOps: remove for side effects in *_ASSIGN's */
308 /* "*s++ += 3" -> "*s++ = *s++ + 3" */
309 /*-----------------------------------------------------------------*/
310 ast *removePostIncDecOps (ast * tree) {
312 // traverse the tree and remove pre-inc/dec ops
317 if (tree->type == EX_OP &&
318 (tree->opval.op == INC_OP || tree->opval.op == DEC_OP)) {
323 tree->left=removePostIncDecOps(tree->left);
324 tree->right=removePostIncDecOps(tree->right);
329 /*-----------------------------------------------------------------*/
330 /* hasSEFcalls - returns TRUE if tree has a function call */
331 /*-----------------------------------------------------------------*/
333 hasSEFcalls (ast * tree)
338 if (tree->type == EX_OP &&
339 (tree->opval.op == CALL ||
340 tree->opval.op == PCALL ||
341 tree->opval.op == '=' ||
342 tree->opval.op == INC_OP ||
343 tree->opval.op == DEC_OP))
346 return (hasSEFcalls (tree->left) |
347 hasSEFcalls (tree->right));
350 /*-----------------------------------------------------------------*/
351 /* isAstEqual - compares two asts & returns 1 if they are equal */
352 /*-----------------------------------------------------------------*/
354 isAstEqual (ast * t1, ast * t2)
363 if (t1->type != t2->type)
369 if (t1->opval.op != t2->opval.op)
371 return (isAstEqual (t1->left, t2->left) &&
372 isAstEqual (t1->right, t2->right));
376 if (t1->opval.val->sym)
378 if (!t2->opval.val->sym)
381 return isSymbolEqual (t1->opval.val->sym,
386 if (t2->opval.val->sym)
389 return (floatFromVal (t1->opval.val) ==
390 floatFromVal (t2->opval.val));
394 /* only compare these two types */
402 /*-----------------------------------------------------------------*/
403 /* resolveSymbols - resolve symbols from the symbol table */
404 /*-----------------------------------------------------------------*/
406 resolveSymbols (ast * tree)
408 /* walk the entire tree and check for values */
409 /* with symbols if we find one then replace */
410 /* symbol with that from the symbol table */
417 /* if not block & function */
418 if (tree->type == EX_OP &&
419 (tree->opval.op != FUNCTION &&
420 tree->opval.op != BLOCK &&
421 tree->opval.op != NULLOP))
423 filename = tree->filename;
424 lineno = tree->lineno;
428 /* make sure we resolve the true & false labels for ifx */
429 if (tree->type == EX_OP && tree->opval.op == IFX)
435 if ((csym = findSym (LabelTab, tree->trueLabel,
436 tree->trueLabel->name)))
437 tree->trueLabel = csym;
439 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
440 tree->trueLabel->name);
443 if (tree->falseLabel)
445 if ((csym = findSym (LabelTab,
447 tree->falseLabel->name)))
448 tree->falseLabel = csym;
450 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
451 tree->falseLabel->name);
456 /* if this is a label resolve it from the labelTab */
457 if (IS_AST_VALUE (tree) &&
458 tree->opval.val->sym &&
459 tree->opval.val->sym->islbl)
462 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
463 tree->opval.val->sym->name);
466 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
467 tree->opval.val->sym->name);
469 tree->opval.val->sym = csym;
471 goto resolveChildren;
474 /* do only for leafs */
475 if (IS_AST_VALUE (tree) &&
476 tree->opval.val->sym &&
477 !tree->opval.val->sym->implicit)
480 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
482 /* if found in the symbol table & they r not the same */
483 if (csym && tree->opval.val->sym != csym)
485 tree->opval.val->sym = csym;
486 tree->opval.val->type = csym->type;
487 tree->opval.val->etype = csym->etype;
490 /* if not found in the symbol table */
491 /* mark it as undefined assume it is */
492 /* an integer in data space */
493 if (!csym && !tree->opval.val->sym->implicit)
496 /* if this is a function name then */
497 /* mark it as returning an int */
500 tree->opval.val->sym->type = newLink (DECLARATOR);
501 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
502 tree->opval.val->sym->type->next =
503 tree->opval.val->sym->etype = newIntLink ();
504 tree->opval.val->etype = tree->opval.val->etype;
505 tree->opval.val->type = tree->opval.val->sym->type;
506 werrorfl (tree->filename, tree->lineno, W_IMPLICIT_FUNC,
507 tree->opval.val->sym->name);
508 //tree->opval.val->sym->undefined = 1;
509 allocVariables (tree->opval.val->sym);
513 tree->opval.val->sym->undefined = 1;
514 tree->opval.val->type =
515 tree->opval.val->etype = newIntLink ();
516 tree->opval.val->sym->type =
517 tree->opval.val->sym->etype = newIntLink ();
523 resolveSymbols (tree->left);
524 resolveSymbols (tree->right);
529 /*-----------------------------------------------------------------*/
530 /* setAstLineno - walks a ast tree & sets the line number */
531 /*-----------------------------------------------------------------*/
532 int setAstLineno (ast * tree, int lineno)
537 tree->lineno = lineno;
538 setAstLineno (tree->left, lineno);
539 setAstLineno (tree->right, lineno);
543 /*-----------------------------------------------------------------*/
544 /* funcOfType :- function of type with name */
545 /*-----------------------------------------------------------------*/
547 funcOfType (char *name, sym_link * type, sym_link * argType,
551 /* create the symbol */
552 sym = newSymbol (name, 0);
554 /* setup return value */
555 sym->type = newLink (DECLARATOR);
556 DCL_TYPE (sym->type) = FUNCTION;
557 sym->type->next = copyLinkChain (type);
558 sym->etype = getSpec (sym->type);
559 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
561 /* if arguments required */
565 args = FUNC_ARGS(sym->type) = newValue ();
569 args->type = copyLinkChain (argType);
570 args->etype = getSpec (args->type);
571 SPEC_EXTR(args->etype)=1;
574 args = args->next = newValue ();
581 allocVariables (sym);
586 /*-----------------------------------------------------------------*/
587 /* funcOfTypeVarg :- function of type with name and argtype */
588 /*-----------------------------------------------------------------*/
590 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
595 /* create the symbol */
596 sym = newSymbol (name, 0);
598 /* setup return value */
599 sym->type = newLink (DECLARATOR);
600 DCL_TYPE (sym->type) = FUNCTION;
601 sym->type->next = typeFromStr(rtype);
602 sym->etype = getSpec (sym->type);
604 /* if arguments required */
607 args = FUNC_ARGS(sym->type) = newValue ();
609 for ( i = 0 ; i < nArgs ; i++ ) {
610 args->type = typeFromStr(atypes[i]);
611 args->etype = getSpec (args->type);
612 SPEC_EXTR(args->etype)=1;
613 if ((i + 1) == nArgs) break;
614 args = args->next = newValue ();
621 allocVariables (sym);
626 /*-----------------------------------------------------------------*/
627 /* reverseParms - will reverse a parameter tree */
628 /*-----------------------------------------------------------------*/
630 reverseParms (ast * ptree)
636 /* top down if we find a nonParm tree then quit */
637 if (ptree->type == EX_OP && ptree->opval.op == PARAM && !ptree->reversed)
639 /* The various functions expect the parameter tree to be right heavy. */
640 /* Rotate the tree to be left heavy so that after reversal it is */
641 /* right heavy again. */
642 while ((ttree = ptree->right) && ttree->type == EX_OP &&
643 ttree->opval.op == PARAM)
645 ptree->right = ttree->right;
646 ttree->right = ttree->left;
647 ttree->left = ptree->left;
653 ptree->left = ptree->right;
654 ptree->right = ttree;
656 reverseParms (ptree->left);
657 reverseParms (ptree->right);
663 /*-----------------------------------------------------------------*/
664 /* processParms - makes sure the parameters are okay and do some */
665 /* processing with them */
666 /*-----------------------------------------------------------------*/
668 processParms (ast *func,
671 int *parmNumber, /* unused, although updated */
674 RESULT_TYPE resultType;
677 /* if none of them exist */
678 if (!defParm && !*actParm)
683 if (getenv("DEBUG_SANITY"))
685 fprintf (stderr, "processParms: %s ", defParm->name);
687 /* make sure the type is complete and sane */
688 checkTypeSanity(defParm->etype, defParm->name);
691 if (IS_CODEPTR (func->ftype))
692 functype = func->ftype->next;
694 functype = func->ftype;
696 /* if the function is being called via a pointer & */
697 /* it has not been defined a reentrant then we cannot */
698 /* have parameters */
699 /* PIC16 port can... */
700 if (!TARGET_IS_PIC16)
702 if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
704 werror (W_NONRENT_ARGS);
710 /* if defined parameters ended but actual parameters */
711 /* exist and this is not defined as a variable arg */
712 if (!defParm && *actParm && !IFFUNC_HASVARARGS(functype))
714 werror (E_TOO_MANY_PARMS);
718 /* if defined parameters present but no actual parameters */
719 if (defParm && !*actParm)
721 werror (E_TOO_FEW_PARMS);
725 /* if this is a PARAM node then match left & right */
726 if ((*actParm)->type == EX_OP && (*actParm)->opval.op == PARAM)
728 (*actParm)->decorated = 1;
729 return (processParms (func, defParm,
730 &(*actParm)->left, parmNumber, FALSE) ||
731 processParms (func, defParm ? defParm->next : NULL,
732 &(*actParm)->right, parmNumber, rightmost));
734 else if (defParm) /* not vararg */
736 /* If we have found a value node by following only right-hand links,
737 * then we know that there are no more values after us.
739 * Therefore, if there are more defined parameters, the caller didn't
742 if (rightmost && defParm->next)
744 werror (E_TOO_FEW_PARMS);
749 /* decorate parameter */
750 resultType = defParm ? getResultTypeFromType (defParm->etype) :
752 *actParm = decorateType (*actParm, resultType);
754 if (IS_VOID((*actParm)->ftype))
756 werror (E_VOID_VALUE_USED);
760 /* If this is a varargs function... */
761 if (!defParm && *actParm && IFFUNC_HASVARARGS(functype))
766 if (IS_CAST_OP (*actParm)
767 || (IS_AST_LIT_VALUE (*actParm) && (*actParm)->values.literalFromCast))
769 /* Parameter was explicitly typecast; don't touch it. */
773 ftype = (*actParm)->ftype;
775 /* If it's a char, upcast to int. */
776 if (IS_INTEGRAL (ftype)
777 && (getSize (ftype) < (unsigned) INTSIZE))
779 newType = newAst_LINK(INTTYPE);
782 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
784 newType = newAst_LINK (copyLinkChain(ftype));
785 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
788 if (IS_AGGREGATE (ftype))
790 newType = newAst_LINK (copyLinkChain (ftype));
791 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
796 /* cast required; change this op to a cast. */
797 (*actParm)->decorated = 0;
798 *actParm = newNode (CAST, newType, *actParm);
799 (*actParm)->lineno = (*actParm)->right->lineno;
801 decorateType (*actParm, RESULT_TYPE_NONE);
806 /* if defined parameters ended but actual has not & */
808 if (!defParm && *actParm &&
809 (options.stackAuto || IFFUNC_ISREENT (functype)))
812 resolveSymbols (*actParm);
814 /* the parameter type must be at least castable */
815 if (compareType (defParm->type, (*actParm)->ftype) == 0)
817 werror (E_INCOMPAT_TYPES);
818 printFromToType ((*actParm)->ftype, defParm->type);
822 /* if the parameter is castable then add the cast */
823 if (compareType (defParm->type, (*actParm)->ftype) < 0)
827 resultType = getResultTypeFromType (defParm->etype);
828 pTree = resolveSymbols (copyAst (*actParm));
830 /* now change the current one to a cast */
831 (*actParm)->type = EX_OP;
832 (*actParm)->opval.op = CAST;
833 (*actParm)->left = newAst_LINK (defParm->type);
834 (*actParm)->right = pTree;
835 (*actParm)->decorated = 0; /* force typechecking */
836 decorateType (*actParm, resultType);
839 /* make a copy and change the regparm type to the defined parm */
840 (*actParm)->etype = getSpec ((*actParm)->ftype = copyLinkChain ((*actParm)->ftype));
841 SPEC_REGPARM ((*actParm)->etype) = SPEC_REGPARM (defParm->etype);
842 SPEC_ARGREG ((*actParm)->etype) = SPEC_ARGREG (defParm->etype);
847 /*-----------------------------------------------------------------*/
848 /* createIvalType - generates ival for basic types */
849 /*-----------------------------------------------------------------*/
851 createIvalType (ast * sym, sym_link * type, initList * ilist)
855 /* if initList is deep */
856 if (ilist->type == INIT_DEEP)
857 ilist = ilist->init.deep;
859 iExpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
860 return decorateType (newNode ('=', sym, iExpr), RESULT_TYPE_NONE);
863 /*-----------------------------------------------------------------*/
864 /* createIvalStruct - generates initial value for structures */
865 /*-----------------------------------------------------------------*/
867 createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
874 sflds = SPEC_STRUCT (type)->fields;
875 if (ilist->type != INIT_DEEP)
877 werror (E_INIT_STRUCT, "");
881 iloop = ilist->init.deep;
883 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
885 /* if we have come to end */
889 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
890 lAst = decorateType (resolveSymbols (lAst), RESULT_TYPE_NONE);
891 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type,
892 iloop, rast, rootValue)),
898 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
899 W_EXCESS_INITIALIZERS, "struct",
900 sym->opval.val->sym->name);
907 /*-----------------------------------------------------------------*/
908 /* createIvalArray - generates code for array initialization */
909 /*-----------------------------------------------------------------*/
911 createIvalArray (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
915 int lcnt = 0, size = 0;
916 literalList *literalL;
918 /* take care of the special case */
919 /* array of characters can be init */
921 if (IS_CHAR (type->next))
922 if ((rast = createIvalCharPtr (sym,
924 decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE),
927 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
929 /* not the special case */
930 if (ilist->type != INIT_DEEP)
932 werror (E_INIT_STRUCT, "");
936 iloop = ilist->init.deep;
937 lcnt = DCL_ELEM (type);
939 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
943 aSym = decorateType (resolveSymbols(sym), RESULT_TYPE_NONE);
945 rast = newNode(ARRAYINIT, aSym, NULL);
946 rast->values.constlist = literalL;
948 // Make sure size is set to length of initializer list.
955 if (lcnt && size > lcnt)
957 // Array size was specified, and we have more initializers than needed.
958 char *name=sym->opval.val->sym->name;
959 int lineno=sym->opval.val->sym->lineDef;
960 char *filename=sym->opval.val->sym->fileDef;
962 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
971 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
972 aSym = decorateType (resolveSymbols (aSym), RESULT_TYPE_NONE);
973 rast = createIval (aSym, type->next, iloop, rast, rootValue);
974 iloop = (iloop ? iloop->next : NULL);
980 /* no of elements given and we */
981 /* have generated for all of them */
984 // is this a better way? at least it won't crash
985 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
986 int lineno = iloop->lineno;
987 char *filename = iloop->filename;
988 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
995 /* if we have not been given a size */
996 if (!DCL_ELEM (type))
998 /* check, if it's a flexible array */
999 if (IS_STRUCT (AST_VALUE (rootValue)->type))
1000 AST_SYMBOL(rootValue)->flexArrayLength = size * getSize (type->next);
1002 DCL_ELEM (type) = size;
1005 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1009 /*-----------------------------------------------------------------*/
1010 /* createIvalCharPtr - generates initial values for char pointers */
1011 /*-----------------------------------------------------------------*/
1013 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr, ast *rootVal)
1018 /* if this is a pointer & right is a literal array then */
1019 /* just assignment will do */
1020 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
1021 SPEC_SCLS (iexpr->etype) == S_CODE)
1022 && IS_ARRAY (iexpr->ftype)))
1023 return newNode ('=', sym, iexpr);
1025 /* left side is an array so we have to assign each */
1027 if ((IS_LITERAL (iexpr->etype) ||
1028 SPEC_SCLS (iexpr->etype) == S_CODE)
1029 && IS_ARRAY (iexpr->ftype))
1031 /* for each character generate an assignment */
1032 /* to the array element */
1033 char *s = SPEC_CVAL (iexpr->etype).v_char;
1035 unsigned int symsize = getSize (type);
1037 size = getSize (iexpr->ftype);
1038 if (symsize && size>symsize)
1040 if (size>(symsize+1))
1042 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
1044 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1050 for (i=0;i<size;i++)
1052 rast = newNode (NULLOP,
1056 newAst_VALUE (valueFromLit ((float) i))),
1057 newAst_VALUE (valueFromLit (*s))));
1061 // now WE don't need iexpr's symbol anymore
1062 freeStringSymbol(AST_SYMBOL(iexpr));
1064 /* if we have not been given a size */
1065 if (!DCL_ELEM (type))
1067 /* check, if it's a flexible array */
1068 if (IS_STRUCT (AST_VALUE (rootVal)->type))
1069 AST_SYMBOL(rootVal)->flexArrayLength = size * getSize (type->next);
1071 DCL_ELEM (type) = size;
1074 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1080 /*-----------------------------------------------------------------*/
1081 /* createIvalPtr - generates initial value for pointers */
1082 /*-----------------------------------------------------------------*/
1084 createIvalPtr (ast * sym, sym_link * type, initList * ilist, ast *rootVal)
1090 if (ilist->type == INIT_DEEP)
1091 ilist = ilist->init.deep;
1093 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
1095 /* if character pointer */
1096 if (IS_CHAR (type->next))
1097 if ((rast = createIvalCharPtr (sym, type, iexpr, rootVal)))
1100 return newNode ('=', sym, iexpr);
1103 /*-----------------------------------------------------------------*/
1104 /* createIval - generates code for initial value */
1105 /*-----------------------------------------------------------------*/
1107 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid, ast *rootValue)
1114 /* if structure then */
1115 if (IS_STRUCT (type))
1116 rast = createIvalStruct (sym, type, ilist, rootValue);
1118 /* if this is a pointer */
1120 rast = createIvalPtr (sym, type, ilist, rootValue);
1122 /* if this is an array */
1123 if (IS_ARRAY (type))
1124 rast = createIvalArray (sym, type, ilist, rootValue);
1126 /* if type is SPECIFIER */
1128 rast = createIvalType (sym, type, ilist);
1131 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_TYPE_NONE);
1133 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1136 /*-----------------------------------------------------------------*/
1137 /* initAggregates - initialises aggregate variables with initv */
1138 /*-----------------------------------------------------------------*/
1139 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1140 ast *newAst = newAst_VALUE (symbolVal (sym));
1141 return createIval (newAst, sym->type, ival, wid, newAst);
1144 /*-----------------------------------------------------------------*/
1145 /* gatherAutoInit - creates assignment expressions for initial */
1147 /*-----------------------------------------------------------------*/
1149 gatherAutoInit (symbol * autoChain)
1156 for (sym = autoChain; sym; sym = sym->next)
1159 /* resolve the symbols in the ival */
1161 resolveIvalSym (sym->ival, sym->type);
1164 /* if we are PIC16 port,
1165 * and this is a static,
1166 * and have initial value,
1167 * and not S_CODE, don't emit in gs segment,
1168 * but allow glue.c:pic16emitRegularMap to put symbol
1169 * in idata section */
1170 if(TARGET_IS_PIC16 &&
1171 IS_STATIC (sym->etype) && sym->ival
1172 && SPEC_SCLS(sym->etype) != S_CODE) {
1173 SPEC_SCLS (sym->etype) = S_DATA;
1178 /* if this is a static variable & has an */
1179 /* initial value the code needs to be lifted */
1180 /* here to the main portion since they can be */
1181 /* initialised only once at the start */
1182 if (IS_STATIC (sym->etype) && sym->ival &&
1183 SPEC_SCLS (sym->etype) != S_CODE)
1187 /* insert the symbol into the symbol table */
1188 /* with level = 0 & name = rname */
1189 newSym = copySymbol (sym);
1190 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1192 /* now lift the code to main */
1193 if (IS_AGGREGATE (sym->type)) {
1194 work = initAggregates (sym, sym->ival, NULL);
1196 if (getNelements(sym->type, sym->ival)>1) {
1197 werrorfl (sym->fileDef, sym->lineDef,
1198 W_EXCESS_INITIALIZERS, "scalar",
1201 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1202 list2expr (sym->ival));
1205 setAstLineno (work, sym->lineDef);
1209 staticAutos = newNode (NULLOP, staticAutos, work);
1216 /* if there is an initial value */
1217 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1219 initList *ilist=sym->ival;
1221 while (ilist->type == INIT_DEEP) {
1222 ilist = ilist->init.deep;
1225 /* update lineno for error msg */
1226 lineno=sym->lineDef;
1227 setAstLineno (ilist->init.node, lineno);
1229 if (IS_AGGREGATE (sym->type)) {
1230 work = initAggregates (sym, sym->ival, NULL);
1232 if (getNelements(sym->type, sym->ival)>1) {
1233 werrorfl (sym->fileDef, sym->lineDef,
1234 W_EXCESS_INITIALIZERS, "scalar",
1237 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1238 list2expr (sym->ival));
1242 setAstLineno (work, sym->lineDef);
1246 init = newNode (NULLOP, init, work);
1255 /*-----------------------------------------------------------------*/
1256 /* freeStringSymbol - delete a literal string if no more usage */
1257 /*-----------------------------------------------------------------*/
1258 void freeStringSymbol(symbol *sym) {
1259 /* make sure this is a literal string */
1260 assert (sym->isstrlit);
1261 if (--sym->isstrlit == 0) { // lower the usage count
1262 memmap *segment=SPEC_OCLS(sym->etype);
1264 deleteSetItem(&segment->syms, sym);
1269 /*-----------------------------------------------------------------*/
1270 /* stringToSymbol - creates a symbol from a literal string */
1271 /*-----------------------------------------------------------------*/
1273 stringToSymbol (value * val)
1275 char name[SDCC_NAME_MAX + 1];
1276 static int charLbl = 0;
1281 // have we heard this before?
1282 for (sp=statsg->syms; sp; sp=sp->next) {
1284 size = getSize (sym->type);
1285 if (sym->isstrlit && size == getSize (val->type) &&
1286 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1287 // yes, this is old news. Don't publish it again.
1288 sym->isstrlit++; // but raise the usage count
1289 return symbolVal(sym);
1293 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1294 sym = newSymbol (name, 0); /* make it @ level 0 */
1295 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1297 /* copy the type from the value passed */
1298 sym->type = copyLinkChain (val->type);
1299 sym->etype = getSpec (sym->type);
1300 /* change to storage class & output class */
1301 SPEC_SCLS (sym->etype) = S_CODE;
1302 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1303 SPEC_STAT (sym->etype) = 1;
1304 /* make the level & block = 0 */
1305 sym->block = sym->level = 0;
1307 /* create an ival */
1308 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1313 allocVariables (sym);
1316 return symbolVal (sym);
1320 /*-----------------------------------------------------------------*/
1321 /* processBlockVars - will go thru the ast looking for block if */
1322 /* a block is found then will allocate the syms */
1323 /* will also gather the auto inits present */
1324 /*-----------------------------------------------------------------*/
1326 processBlockVars (ast * tree, int *stack, int action)
1331 /* if this is a block */
1332 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1336 if (action == ALLOCATE)
1338 *stack += allocVariables (tree->values.sym);
1339 autoInit = gatherAutoInit (tree->values.sym);
1341 /* if there are auto inits then do them */
1343 tree->left = newNode (NULLOP, autoInit, tree->left);
1345 else /* action is deallocate */
1346 deallocLocal (tree->values.sym);
1349 processBlockVars (tree->left, stack, action);
1350 processBlockVars (tree->right, stack, action);
1355 /*-------------------------------------------------------------*/
1356 /* constExprTree - returns TRUE if this tree is a constant */
1358 /*-------------------------------------------------------------*/
1359 bool constExprTree (ast *cexpr) {
1365 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1367 switch (cexpr->type)
1370 if (IS_AST_LIT_VALUE(cexpr)) {
1371 // this is a literal
1374 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1375 // a function's address will never change
1378 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1379 // an array's address will never change
1382 if (IS_AST_SYM_VALUE(cexpr) &&
1383 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1384 // a symbol in code space will never change
1385 // This is only for the 'char *s="hallo"' case and will have to leave
1386 //printf(" code space symbol");
1391 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1392 "unexpected link in expression tree\n");
1395 if (cexpr->opval.op==ARRAYINIT) {
1396 // this is a list of literals
1399 if (cexpr->opval.op=='=') {
1400 return constExprTree(cexpr->right);
1402 if (cexpr->opval.op==CAST) {
1403 // cast ignored, maybe we should throw a warning here?
1404 return constExprTree(cexpr->right);
1406 if (cexpr->opval.op=='&') {
1409 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1412 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1417 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1422 /*-----------------------------------------------------------------*/
1423 /* constExprValue - returns the value of a constant expression */
1424 /* or NULL if it is not a constant expression */
1425 /*-----------------------------------------------------------------*/
1427 constExprValue (ast * cexpr, int check)
1429 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1431 /* if this is not a constant then */
1432 if (!IS_LITERAL (cexpr->ftype))
1434 /* then check if this is a literal array
1436 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1437 SPEC_CVAL (cexpr->etype).v_char &&
1438 IS_ARRAY (cexpr->ftype))
1440 value *val = valFromType (cexpr->ftype);
1441 SPEC_SCLS (val->etype) = S_LITERAL;
1442 val->sym = cexpr->opval.val->sym;
1443 val->sym->type = copyLinkChain (cexpr->ftype);
1444 val->sym->etype = getSpec (val->sym->type);
1445 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1449 /* if we are casting a literal value then */
1450 if (IS_AST_OP (cexpr) &&
1451 cexpr->opval.op == CAST &&
1452 IS_LITERAL (cexpr->right->ftype))
1454 return valCastLiteral (cexpr->ftype,
1455 floatFromVal (cexpr->right->opval.val));
1458 if (IS_AST_VALUE (cexpr))
1460 return cexpr->opval.val;
1464 werror (E_CONST_EXPECTED, "found expression");
1469 /* return the value */
1470 if (IS_AST_VALUE (cexpr))
1472 return cexpr->opval.val;
1477 /*-----------------------------------------------------------------*/
1478 /* isLabelInAst - will return true if a given label is found */
1479 /*-----------------------------------------------------------------*/
1481 isLabelInAst (symbol * label, ast * tree)
1483 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1486 if (IS_AST_OP (tree) &&
1487 tree->opval.op == LABEL &&
1488 isSymbolEqual (AST_SYMBOL (tree->left), label))
1491 return isLabelInAst (label, tree->right) &&
1492 isLabelInAst (label, tree->left);
1496 /*-----------------------------------------------------------------*/
1497 /* isLoopCountable - return true if the loop count can be determi- */
1498 /* -ned at compile time . */
1499 /*-----------------------------------------------------------------*/
1501 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1502 symbol ** sym, ast ** init, ast ** end)
1505 /* the loop is considered countable if the following
1506 conditions are true :-
1508 a) initExpr :- <sym> = <const>
1509 b) condExpr :- <sym> < <const1>
1510 c) loopExpr :- <sym> ++
1513 /* first check the initExpr */
1514 if (IS_AST_OP (initExpr) &&
1515 initExpr->opval.op == '=' && /* is assignment */
1516 IS_AST_SYM_VALUE (initExpr->left))
1517 { /* left is a symbol */
1519 *sym = AST_SYMBOL (initExpr->left);
1520 *init = initExpr->right;
1525 /* for now the symbol has to be of
1527 if (!IS_INTEGRAL ((*sym)->type))
1530 /* now check condExpr */
1531 if (IS_AST_OP (condExpr))
1534 switch (condExpr->opval.op)
1537 if (IS_AST_SYM_VALUE (condExpr->left) &&
1538 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1539 IS_AST_LIT_VALUE (condExpr->right))
1541 *end = condExpr->right;
1547 if (IS_AST_OP (condExpr->left) &&
1548 condExpr->left->opval.op == '>' &&
1549 IS_AST_LIT_VALUE (condExpr->left->right) &&
1550 IS_AST_SYM_VALUE (condExpr->left->left) &&
1551 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1554 *end = newNode ('+', condExpr->left->right,
1555 newAst_VALUE (constVal ("1")));
1568 /* check loop expression is of the form <sym>++ */
1569 if (!IS_AST_OP (loopExpr))
1572 /* check if <sym> ++ */
1573 if (loopExpr->opval.op == INC_OP)
1579 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1580 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1587 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1588 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1596 if (loopExpr->opval.op == ADD_ASSIGN)
1599 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1600 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1601 IS_AST_LIT_VALUE (loopExpr->right) &&
1602 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1610 /*-----------------------------------------------------------------*/
1611 /* astHasVolatile - returns true if ast contains any volatile */
1612 /*-----------------------------------------------------------------*/
1614 astHasVolatile (ast * tree)
1619 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1622 if (IS_AST_OP (tree))
1623 return astHasVolatile (tree->left) ||
1624 astHasVolatile (tree->right);
1629 /*-----------------------------------------------------------------*/
1630 /* astHasPointer - return true if the ast contains any ptr variable */
1631 /*-----------------------------------------------------------------*/
1633 astHasPointer (ast * tree)
1638 if (IS_AST_LINK (tree))
1641 /* if we hit an array expression then check
1642 only the left side */
1643 if (IS_AST_OP (tree) && tree->opval.op == '[')
1644 return astHasPointer (tree->left);
1646 if (IS_AST_VALUE (tree))
1647 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1649 return astHasPointer (tree->left) ||
1650 astHasPointer (tree->right);
1654 /*-----------------------------------------------------------------*/
1655 /* astHasSymbol - return true if the ast has the given symbol */
1656 /*-----------------------------------------------------------------*/
1658 astHasSymbol (ast * tree, symbol * sym)
1660 if (!tree || IS_AST_LINK (tree))
1663 if (IS_AST_VALUE (tree))
1665 if (IS_AST_SYM_VALUE (tree))
1666 return isSymbolEqual (AST_SYMBOL (tree), sym);
1671 return astHasSymbol (tree->left, sym) ||
1672 astHasSymbol (tree->right, sym);
1675 /*-----------------------------------------------------------------*/
1676 /* astHasDeref - return true if the ast has an indirect access */
1677 /*-----------------------------------------------------------------*/
1679 astHasDeref (ast * tree)
1681 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1684 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1686 return astHasDeref (tree->left) || astHasDeref (tree->right);
1689 /*-----------------------------------------------------------------*/
1690 /* isConformingBody - the loop body has to conform to a set of rules */
1691 /* for the loop to be considered reversible read on for rules */
1692 /*-----------------------------------------------------------------*/
1694 isConformingBody (ast * pbody, symbol * sym, ast * body)
1697 /* we are going to do a pre-order traversal of the
1698 tree && check for the following conditions. (essentially
1699 a set of very shallow tests )
1700 a) the sym passed does not participate in
1701 any arithmetic operation
1702 b) There are no function calls
1703 c) all jumps are within the body
1704 d) address of loop control variable not taken
1705 e) if an assignment has a pointer on the
1706 left hand side make sure right does not have
1707 loop control variable */
1709 /* if we reach the end or a leaf then true */
1710 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1713 /* if anything else is "volatile" */
1714 if (IS_VOLATILE (TETYPE (pbody)))
1717 /* we will walk the body in a pre-order traversal for
1719 switch (pbody->opval.op)
1721 /*------------------------------------------------------------------*/
1723 // if the loopvar is used as an index
1724 /* array op is commutative -- must check both left & right */
1725 if (astHasSymbol(pbody->right, sym) || astHasSymbol(pbody->left, sym)) {
1728 return isConformingBody (pbody->right, sym, body)
1729 && isConformingBody (pbody->left, sym, body);
1731 /*------------------------------------------------------------------*/
1736 /*------------------------------------------------------------------*/
1740 /* sure we are not sym is not modified */
1742 IS_AST_SYM_VALUE (pbody->left) &&
1743 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1747 IS_AST_SYM_VALUE (pbody->right) &&
1748 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1753 /*------------------------------------------------------------------*/
1755 case '*': /* can be unary : if right is null then unary operation */
1760 /* if right is NULL then unary operation */
1761 /*------------------------------------------------------------------*/
1762 /*----------------------------*/
1764 /*----------------------------*/
1767 if (IS_AST_SYM_VALUE (pbody->left) &&
1768 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1771 return isConformingBody (pbody->left, sym, body);
1775 if (astHasSymbol (pbody->left, sym) ||
1776 astHasSymbol (pbody->right, sym))
1781 /*------------------------------------------------------------------*/
1792 if (IS_AST_SYM_VALUE (pbody->left) &&
1793 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1796 if (IS_AST_SYM_VALUE (pbody->right) &&
1797 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1800 return isConformingBody (pbody->left, sym, body) &&
1801 isConformingBody (pbody->right, sym, body);
1809 if (IS_AST_SYM_VALUE (pbody->left) &&
1810 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1812 return isConformingBody (pbody->left, sym, body);
1814 /*------------------------------------------------------------------*/
1826 case SIZEOF: /* evaluate wihout code generation */
1828 if (IS_AST_SYM_VALUE (pbody->left) &&
1829 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1832 if (IS_AST_SYM_VALUE (pbody->right) &&
1833 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1836 return isConformingBody (pbody->left, sym, body) &&
1837 isConformingBody (pbody->right, sym, body);
1839 /*------------------------------------------------------------------*/
1842 /* if left has a pointer & right has loop
1843 control variable then we cannot */
1844 if (astHasPointer (pbody->left) &&
1845 astHasSymbol (pbody->right, sym))
1847 if (astHasVolatile (pbody->left))
1850 if (IS_AST_SYM_VALUE (pbody->left)) {
1851 // if the loopvar has an assignment
1852 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1854 // if the loopvar is used in another (maybe conditional) block
1855 if (astHasSymbol (pbody->right, sym) &&
1856 (pbody->level >= body->level)) {
1861 if (astHasVolatile (pbody->left))
1864 if (astHasDeref(pbody->right)) return FALSE;
1866 return isConformingBody (pbody->left, sym, body) &&
1867 isConformingBody (pbody->right, sym, body);
1878 assert ("Parser should not have generated this\n");
1880 /*------------------------------------------------------------------*/
1881 /*----------------------------*/
1882 /* comma operator */
1883 /*----------------------------*/
1885 return isConformingBody (pbody->left, sym, body) &&
1886 isConformingBody (pbody->right, sym, body);
1888 /*------------------------------------------------------------------*/
1889 /*----------------------------*/
1891 /*----------------------------*/
1893 /* if local & not passed as paramater then ok */
1894 if (sym->level && !astHasSymbol(pbody->right,sym))
1898 /*------------------------------------------------------------------*/
1899 /*----------------------------*/
1900 /* return statement */
1901 /*----------------------------*/
1906 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1911 if (astHasSymbol (pbody->left, sym))
1918 return isConformingBody (pbody->left, sym, body) &&
1919 isConformingBody (pbody->right, sym, body);
1925 /*-----------------------------------------------------------------*/
1926 /* isLoopReversible - takes a for loop as input && returns true */
1927 /* if the for loop is reversible. If yes will set the value of */
1928 /* the loop control var & init value & termination value */
1929 /*-----------------------------------------------------------------*/
1931 isLoopReversible (ast * loop, symbol ** loopCntrl,
1932 ast ** init, ast ** end)
1934 /* if option says don't do it then don't */
1935 if (optimize.noLoopReverse)
1937 /* there are several tests to determine this */
1939 /* for loop has to be of the form
1940 for ( <sym> = <const1> ;
1941 [<sym> < <const2>] ;
1942 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1944 if (!isLoopCountable (AST_FOR (loop, initExpr),
1945 AST_FOR (loop, condExpr),
1946 AST_FOR (loop, loopExpr),
1947 loopCntrl, init, end))
1950 /* now do some serious checking on the body of the loop
1953 return isConformingBody (loop->left, *loopCntrl, loop->left);
1957 /*-----------------------------------------------------------------*/
1958 /* replLoopSym - replace the loop sym by loop sym -1 */
1959 /*-----------------------------------------------------------------*/
1961 replLoopSym (ast * body, symbol * sym)
1964 if (!body || IS_AST_LINK (body))
1967 if (IS_AST_SYM_VALUE (body))
1970 if (isSymbolEqual (AST_SYMBOL (body), sym))
1974 body->opval.op = '-';
1975 body->left = newAst_VALUE (symbolVal (sym));
1976 body->right = newAst_VALUE (constVal ("1"));
1984 replLoopSym (body->left, sym);
1985 replLoopSym (body->right, sym);
1989 /*-----------------------------------------------------------------*/
1990 /* reverseLoop - do the actual loop reversal */
1991 /*-----------------------------------------------------------------*/
1993 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
1997 /* create the following tree
2002 if (sym) goto for_continue ;
2005 /* put it together piece by piece */
2006 rloop = newNode (NULLOP,
2007 createIf (newAst_VALUE (symbolVal (sym)),
2009 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
2012 newAst_VALUE (symbolVal (sym)),
2015 replLoopSym (loop->left, sym);
2016 setAstLineno (rloop, init->lineno);
2018 rloop = newNode (NULLOP,
2020 newAst_VALUE (symbolVal (sym)),
2021 newNode ('-', end, init)),
2022 createLabel (AST_FOR (loop, continueLabel),
2026 newNode (SUB_ASSIGN,
2027 newAst_VALUE (symbolVal (sym)),
2028 newAst_VALUE (constVal ("1"))),
2031 rloop->lineno=init->lineno;
2032 return decorateType (rloop, RESULT_TYPE_NONE);
2036 /*-----------------------------------------------------------------*/
2037 /* searchLitOp - search tree (*ops only) for an ast with literal */
2038 /*-----------------------------------------------------------------*/
2040 searchLitOp (ast *tree, ast **parent, const unsigned char *ops)
2044 if (tree && optimize.global_cse)
2046 /* is there a literal operand? */
2048 IS_AST_OP(tree->right) &&
2049 tree->right->right &&
2050 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
2052 if (IS_LITERAL (RTYPE (tree->right)) !=
2053 IS_LITERAL (LTYPE (tree->right)))
2055 tree->right->decorated = 0;
2056 tree->decorated = 0;
2060 ret = searchLitOp (tree->right, parent, ops);
2065 IS_AST_OP(tree->left) &&
2066 tree->left->right &&
2067 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
2069 if (IS_LITERAL (RTYPE (tree->left)) !=
2070 IS_LITERAL (LTYPE (tree->left)))
2072 tree->left->decorated = 0;
2073 tree->decorated = 0;
2077 ret = searchLitOp (tree->left, parent, ops);
2085 /*-----------------------------------------------------------------*/
2086 /* getResultFromType */
2087 /*-----------------------------------------------------------------*/
2089 getResultTypeFromType (sym_link *type)
2091 /* type = getSpec (type); */
2093 return RESULT_TYPE_BIT;
2094 if (IS_BITFIELD (type))
2096 int blen = SPEC_BLEN (type);
2099 return RESULT_TYPE_BIT;
2101 return RESULT_TYPE_CHAR;
2102 return RESULT_TYPE_INT;
2105 return RESULT_TYPE_CHAR;
2108 return RESULT_TYPE_INT;
2109 return RESULT_TYPE_OTHER;
2112 /*-----------------------------------------------------------------*/
2113 /* addCast - adds casts to a type specified by RESULT_TYPE */
2114 /*-----------------------------------------------------------------*/
2116 addCast (ast *tree, RESULT_TYPE resultType, bool promote)
2119 bool upCasted = FALSE;
2123 case RESULT_TYPE_NONE:
2124 /* if thing smaller than int must be promoted to int */
2126 getSize (tree->etype) >= INTSIZE)
2127 /* promotion not necessary or already an int */
2129 /* char and bits: promote to int */
2130 newLink = newIntLink();
2133 case RESULT_TYPE_BIT:
2135 /* already an int */
2136 bitsForType (tree->etype) >= 16 ||
2137 /* bit to bit operation: don't promote, the code generators
2138 hopefully know everything about promotion rules */
2139 bitsForType (tree->etype) == 1)
2141 newLink = newIntLink();
2144 case RESULT_TYPE_CHAR:
2145 if (IS_CHAR (tree->etype) ||
2146 IS_FLOAT(tree->etype) ||
2147 IS_FIXED(tree->etype))
2149 newLink = newCharLink();
2151 case RESULT_TYPE_INT:
2153 if (getSize (tree->etype) > INTSIZE)
2155 /* warn ("Loosing significant digits"); */
2159 /* char: promote to int */
2161 getSize (tree->etype) >= INTSIZE)
2163 newLink = newIntLink();
2166 case RESULT_TYPE_OTHER:
2169 /* return type is long, float: promote char to int */
2170 if (getSize (tree->etype) >= INTSIZE)
2172 newLink = newIntLink();
2178 tree->decorated = 0;
2179 tree = newNode (CAST, newAst_LINK (newLink), tree);
2180 tree->lineno = tree->right->lineno;
2181 /* keep unsigned type during cast to smaller type,
2182 but not when promoting from char to int */
2184 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2185 return decorateType (tree, resultType);
2188 /*-----------------------------------------------------------------*/
2189 /* resultTypePropagate - decides if resultType can be propagated */
2190 /*-----------------------------------------------------------------*/
2192 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2194 switch (tree->opval.op)
2213 return RESULT_TYPE_NONE;
2217 return RESULT_TYPE_IFX;
2219 return RESULT_TYPE_NONE;
2223 /*-----------------------------------------------------------------*/
2224 /* getLeftResultType - gets type from left branch for propagation */
2225 /*-----------------------------------------------------------------*/
2227 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2229 switch (tree->opval.op)
2233 if (IS_PTR (LTYPE (tree)))
2234 return RESULT_TYPE_NONE;
2236 return getResultTypeFromType (LETYPE (tree));
2238 if (IS_PTR (currFunc->type->next))
2239 return RESULT_TYPE_NONE;
2241 return getResultTypeFromType (currFunc->type->next);
2243 if (!IS_ARRAY (LTYPE (tree)))
2245 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 255)
2246 return RESULT_TYPE_CHAR;
2253 /*--------------------------------------------------------------------*/
2254 /* decorateType - compute type for this tree, also does type checking.*/
2255 /* This is done bottom up, since type has to flow upwards. */
2256 /* resultType flows top-down and forces e.g. char-arithmetic, if the */
2257 /* result is a char and the operand(s) are int's. */
2258 /* It also does constant folding, and parameter checking. */
2259 /*--------------------------------------------------------------------*/
2261 decorateType (ast * tree, RESULT_TYPE resultType)
2265 RESULT_TYPE resultTypeProp;
2270 /* if already has type then do nothing */
2271 if (tree->decorated)
2274 tree->decorated = 1;
2277 /* print the line */
2278 /* if not block & function */
2279 if (tree->type == EX_OP &&
2280 (tree->opval.op != FUNCTION &&
2281 tree->opval.op != BLOCK &&
2282 tree->opval.op != NULLOP))
2284 filename = tree->filename;
2285 lineno = tree->lineno;
2289 /* if any child is an error | this one is an error do nothing */
2290 if (tree->isError ||
2291 (tree->left && tree->left->isError) ||
2292 (tree->right && tree->right->isError))
2295 /*------------------------------------------------------------------*/
2296 /*----------------------------*/
2297 /* leaf has been reached */
2298 /*----------------------------*/
2299 lineno=tree->lineno;
2300 /* if this is of type value */
2301 /* just get the type */
2302 if (tree->type == EX_VALUE)
2305 if (IS_LITERAL (tree->opval.val->etype))
2308 /* if this is a character array then declare it */
2309 if (IS_ARRAY (tree->opval.val->type))
2310 tree->opval.val = stringToSymbol (tree->opval.val);
2312 /* otherwise just copy the type information */
2313 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2317 if (tree->opval.val->sym)
2319 /* if the undefined flag is set then give error message */
2320 if (tree->opval.val->sym->undefined)
2322 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2324 TTYPE (tree) = TETYPE (tree) =
2325 tree->opval.val->type = tree->opval.val->sym->type =
2326 tree->opval.val->etype = tree->opval.val->sym->etype =
2327 copyLinkChain (INTTYPE);
2332 /* if impilicit i.e. struct/union member then no type */
2333 if (tree->opval.val->sym->implicit)
2334 TTYPE (tree) = TETYPE (tree) = NULL;
2339 /* else copy the type */
2340 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2342 /* and mark it as referenced */
2343 tree->opval.val->sym->isref = 1;
2351 /* if type link for the case of cast */
2352 if (tree->type == EX_LINK)
2354 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2362 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2364 if (tree->left && tree->left->type == EX_OPERAND
2365 && (tree->left->opval.op == INC_OP
2366 || tree->left->opval.op == DEC_OP)
2367 && tree->left->left)
2369 tree->left->right = tree->left->left;
2370 tree->left->left = NULL;
2372 if (tree->right && tree->right->type == EX_OPERAND
2373 && (tree->right->opval.op == INC_OP
2374 || tree->right->opval.op == DEC_OP)
2375 && tree->right->left)
2377 tree->right->right = tree->right->left;
2378 tree->right->left = NULL;
2383 /* Before decorating the left branch we've to decide in dependence
2384 upon tree->opval.op, if resultType can be propagated */
2385 resultTypeProp = resultTypePropagate (tree, resultType);
2387 if (tree->opval.op == '?')
2388 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2390 dtl = decorateType (tree->left, resultTypeProp);
2392 /* if an array node, we may need to swap branches */
2393 if (tree->opval.op == '[')
2395 /* determine which is the array & which the index */
2396 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2397 IS_INTEGRAL (LTYPE (tree)))
2399 ast *tempTree = tree->left;
2400 tree->left = tree->right;
2401 tree->right = tempTree;
2405 /* After decorating the left branch there's type information available
2406 in tree->left->?type. If the op is e.g. '=' we extract the type
2407 information from there and propagate it to the right branch. */
2408 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2410 switch (tree->opval.op)
2413 /* delay right side for '?' operator since conditional macro
2414 expansions might rely on this */
2418 /* decorate right side for CALL (parameter list) in processParms();
2419 there is resultType available */
2423 dtr = decorateType (tree->right, resultTypeProp);
2427 /* this is to take care of situations
2428 when the tree gets rewritten */
2429 if (dtl != tree->left)
2431 if (dtr != tree->right)
2433 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2437 /* depending on type of operator do */
2439 switch (tree->opval.op)
2441 /*------------------------------------------------------------------*/
2442 /*----------------------------*/
2444 /*----------------------------*/
2447 /* first check if this is a array or a pointer */
2448 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2450 werror (E_NEED_ARRAY_PTR, "[]");
2451 goto errorTreeReturn;
2454 /* check if the type of the idx */
2455 if (!IS_INTEGRAL (RTYPE (tree)))
2457 werror (E_IDX_NOT_INT);
2458 goto errorTreeReturn;
2461 /* if the left is an rvalue then error */
2464 werror (E_LVALUE_REQUIRED, "array access");
2465 goto errorTreeReturn;
2468 if (IS_LITERAL (RTYPE (tree)))
2470 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2471 int arraySize = DCL_ELEM (LTYPE (tree));
2472 if (arraySize && arrayIndex >= arraySize)
2474 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2479 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2480 SPEC_CONST (TETYPE (tree)) |= DCL_PTR_CONST (LTYPE (tree));
2483 /*------------------------------------------------------------------*/
2484 /*----------------------------*/
2486 /*----------------------------*/
2488 /* if this is not a structure */
2489 if (!IS_STRUCT (LTYPE (tree)))
2491 werror (E_STRUCT_UNION, ".");
2492 goto errorTreeReturn;
2494 TTYPE (tree) = structElemType (LTYPE (tree),
2495 (tree->right->type == EX_VALUE ?
2496 tree->right->opval.val : NULL));
2497 TETYPE (tree) = getSpec (TTYPE (tree));
2500 /*------------------------------------------------------------------*/
2501 /*----------------------------*/
2502 /* struct/union pointer */
2503 /*----------------------------*/
2505 /* if not pointer to a structure */
2506 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2508 werror (E_PTR_REQD);
2509 goto errorTreeReturn;
2512 if (!IS_STRUCT (LTYPE (tree)->next))
2514 werror (E_STRUCT_UNION, "->");
2515 goto errorTreeReturn;
2518 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2519 (tree->right->type == EX_VALUE ?
2520 tree->right->opval.val : NULL));
2521 TETYPE (tree) = getSpec (TTYPE (tree));
2523 /* adjust the storage class */
2524 switch (DCL_TYPE(tree->left->ftype)) {
2526 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2529 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2532 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2535 SPEC_SCLS (TETYPE (tree)) = 0;
2538 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2541 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2544 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2547 SPEC_SCLS (TETYPE (tree)) = 0;
2554 /* This breaks with extern declarations, bitfields, and perhaps other */
2555 /* cases (gcse). Let's leave this optimization disabled for now and */
2556 /* ponder if there's a safe way to do this. -- EEP */
2558 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2559 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2561 /* If defined struct type at addr var
2562 then rewrite (&struct var)->member
2564 and define membertype at (addr+offsetof(struct var,member)) temp
2567 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2568 AST_SYMBOL(tree->right));
2570 sym = newSymbol(genSymName (0), 0);
2571 sym->type = TTYPE (tree);
2572 sym->etype = getSpec(sym->type);
2573 sym->lineDef = tree->lineno;
2576 SPEC_STAT (sym->etype) = 1;
2577 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2579 SPEC_ABSA(sym->etype) = 1;
2580 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2583 AST_VALUE (tree) = symbolVal(sym);
2586 tree->type = EX_VALUE;
2594 /*------------------------------------------------------------------*/
2595 /*----------------------------*/
2596 /* ++/-- operation */
2597 /*----------------------------*/
2601 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2602 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2603 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2604 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2613 /*------------------------------------------------------------------*/
2614 /*----------------------------*/
2616 /*----------------------------*/
2617 case '&': /* can be unary */
2618 /* if right is NULL then unary operation */
2619 if (tree->right) /* not an unary operation */
2622 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2624 werror (E_BITWISE_OP);
2625 werror (W_CONTINUE, "left & right types are ");
2626 printTypeChain (LTYPE (tree), stderr);
2627 fprintf (stderr, ",");
2628 printTypeChain (RTYPE (tree), stderr);
2629 fprintf (stderr, "\n");
2630 goto errorTreeReturn;
2633 /* if they are both literal */
2634 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2636 tree->type = EX_VALUE;
2637 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2638 valFromType (RETYPE (tree)), '&');
2640 tree->right = tree->left = NULL;
2641 TETYPE (tree) = tree->opval.val->etype;
2642 TTYPE (tree) = tree->opval.val->type;
2646 /* see if this is a GETHBIT operation if yes
2649 ast *otree = optimizeGetHbit (tree, resultType);
2652 return decorateType (otree, RESULT_TYPE_NONE);
2655 /* see if this is a GETABIT operation if yes
2658 ast *otree = optimizeGetAbit (tree, resultType);
2661 return decorateType (otree, RESULT_TYPE_NONE);
2664 /* see if this is a GETBYTE operation if yes
2667 ast *otree = optimizeGetByte (tree, resultType);
2670 return decorateType (otree, RESULT_TYPE_NONE);
2673 /* see if this is a GETWORD operation if yes
2676 ast *otree = optimizeGetWord (tree, resultType);
2679 return decorateType (otree, RESULT_TYPE_NONE);
2682 /* if left is a literal exchange left & right */
2683 if (IS_LITERAL (LTYPE (tree)))
2685 ast *tTree = tree->left;
2686 tree->left = tree->right;
2687 tree->right = tTree;
2690 /* if right is a literal and */
2691 /* we can find a 2nd literal in an and-tree then */
2692 /* rearrange the tree */
2693 if (IS_LITERAL (RTYPE (tree)))
2696 ast *litTree = searchLitOp (tree, &parent, "&");
2700 ast *tTree = litTree->left;
2701 litTree->left = tree->right;
2702 tree->right = tTree;
2703 /* both operands in litTree are literal now */
2704 decorateType (parent, resultType);
2708 LRVAL (tree) = RRVAL (tree) = 1;
2710 TTYPE (tree) = computeType (LTYPE (tree),
2714 TETYPE (tree) = getSpec (TTYPE (tree));
2719 /*------------------------------------------------------------------*/
2720 /*----------------------------*/
2722 /*----------------------------*/
2723 p = newLink (DECLARATOR);
2724 /* if bit field then error */
2725 if (IS_BITVAR (tree->left->etype))
2727 werror (E_ILLEGAL_ADDR, "address of bit variable");
2728 goto errorTreeReturn;
2731 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2733 werror (E_ILLEGAL_ADDR, "address of register variable");
2734 goto errorTreeReturn;
2737 if (IS_FUNC (LTYPE (tree)))
2739 // this ought to be ignored
2740 return (tree->left);
2743 if (IS_LITERAL(LTYPE(tree)))
2745 werror (E_ILLEGAL_ADDR, "address of literal");
2746 goto errorTreeReturn;
2751 werror (E_LVALUE_REQUIRED, "address of");
2752 goto errorTreeReturn;
2755 DCL_TYPE (p) = POINTER;
2756 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2757 DCL_TYPE (p) = CPOINTER;
2758 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2759 DCL_TYPE (p) = FPOINTER;
2760 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2761 DCL_TYPE (p) = PPOINTER;
2762 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2763 DCL_TYPE (p) = IPOINTER;
2764 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2765 DCL_TYPE (p) = EEPPOINTER;
2766 else if (SPEC_OCLS(tree->left->etype))
2767 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2769 DCL_TYPE (p) = POINTER;
2771 if (IS_AST_SYM_VALUE (tree->left))
2773 AST_SYMBOL (tree->left)->addrtaken = 1;
2774 AST_SYMBOL (tree->left)->allocreq = 1;
2777 p->next = LTYPE (tree);
2779 TETYPE (tree) = getSpec (TTYPE (tree));
2784 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2785 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2787 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2788 AST_SYMBOL(tree->left->right));
2789 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2790 valueFromLit(element->offset));
2793 tree->type = EX_VALUE;
2794 tree->values.literalFromCast = 1;
2800 /*------------------------------------------------------------------*/
2801 /*----------------------------*/
2803 /*----------------------------*/
2805 /* if the rewrite succeeds then don't go any further */
2807 ast *wtree = optimizeRRCRLC (tree);
2809 return decorateType (wtree, RESULT_TYPE_NONE);
2811 wtree = optimizeSWAP (tree);
2813 return decorateType (wtree, RESULT_TYPE_NONE);
2816 /* if left is a literal exchange left & right */
2817 if (IS_LITERAL (LTYPE (tree)))
2819 ast *tTree = tree->left;
2820 tree->left = tree->right;
2821 tree->right = tTree;
2824 /* if right is a literal and */
2825 /* we can find a 2nd literal in an or-tree then */
2826 /* rearrange the tree */
2827 if (IS_LITERAL (RTYPE (tree)))
2830 ast *litTree = searchLitOp (tree, &parent, "|");
2834 ast *tTree = litTree->left;
2835 litTree->left = tree->right;
2836 tree->right = tTree;
2837 /* both operands in tTree are literal now */
2838 decorateType (parent, resultType);
2843 /*------------------------------------------------------------------*/
2844 /*----------------------------*/
2846 /*----------------------------*/
2848 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2850 werror (E_BITWISE_OP);
2851 werror (W_CONTINUE, "left & right types are ");
2852 printTypeChain (LTYPE (tree), stderr);
2853 fprintf (stderr, ",");
2854 printTypeChain (RTYPE (tree), stderr);
2855 fprintf (stderr, "\n");
2856 goto errorTreeReturn;
2859 /* if they are both literal then rewrite the tree */
2860 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2862 tree->type = EX_VALUE;
2863 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2864 valFromType (RETYPE (tree)),
2866 tree->right = tree->left = NULL;
2867 TETYPE (tree) = tree->opval.val->etype;
2868 TTYPE (tree) = tree->opval.val->type;
2872 /* if left is a literal exchange left & right */
2873 if (IS_LITERAL (LTYPE (tree)))
2875 ast *tTree = tree->left;
2876 tree->left = tree->right;
2877 tree->right = tTree;
2880 /* if right is a literal and */
2881 /* we can find a 2nd literal in a xor-tree then */
2882 /* rearrange the tree */
2883 if (IS_LITERAL (RTYPE (tree)) &&
2884 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
2887 ast *litTree = searchLitOp (tree, &parent, "^");
2891 ast *tTree = litTree->left;
2892 litTree->left = tree->right;
2893 tree->right = tTree;
2894 /* both operands in litTree are literal now */
2895 decorateType (parent, resultType);
2899 LRVAL (tree) = RRVAL (tree) = 1;
2901 TTYPE (tree) = computeType (LTYPE (tree),
2905 TETYPE (tree) = getSpec (TTYPE (tree));
2909 /*------------------------------------------------------------------*/
2910 /*----------------------------*/
2912 /*----------------------------*/
2914 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
2916 werror (E_INVALID_OP, "divide");
2917 goto errorTreeReturn;
2919 /* if they are both literal then */
2920 /* rewrite the tree */
2921 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2923 tree->type = EX_VALUE;
2924 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
2925 valFromType (RETYPE (tree)));
2926 tree->right = tree->left = NULL;
2927 TETYPE (tree) = getSpec (TTYPE (tree) =
2928 tree->opval.val->type);
2932 LRVAL (tree) = RRVAL (tree) = 1;
2934 TETYPE (tree) = getSpec (TTYPE (tree) =
2935 computeType (LTYPE (tree),
2940 /* if right is a literal and */
2941 /* left is also a division by a literal then */
2942 /* rearrange the tree */
2943 if (IS_LITERAL (RTYPE (tree))
2944 /* avoid infinite loop */
2945 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
2948 ast *litTree = searchLitOp (tree, &parent, "/");
2951 if (IS_LITERAL (RTYPE (litTree)))
2955 litTree->right = newNode ('*',
2957 copyAst (tree->right));
2958 litTree->right->lineno = tree->lineno;
2960 tree->right->opval.val = constVal ("1");
2961 decorateType (parent, resultType);
2965 /* litTree->left is literal: no gcse possible.
2966 We can't call decorateType(parent, RESULT_TYPE_NONE), because
2967 this would cause an infinit loop. */
2968 parent->decorated = 1;
2969 decorateType (litTree, resultType);
2976 /*------------------------------------------------------------------*/
2977 /*----------------------------*/
2979 /*----------------------------*/
2981 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2983 werror (E_BITWISE_OP);
2984 werror (W_CONTINUE, "left & right types are ");
2985 printTypeChain (LTYPE (tree), stderr);
2986 fprintf (stderr, ",");
2987 printTypeChain (RTYPE (tree), stderr);
2988 fprintf (stderr, "\n");
2989 goto errorTreeReturn;
2991 /* if they are both literal then */
2992 /* rewrite the tree */
2993 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2995 tree->type = EX_VALUE;
2996 tree->opval.val = valMod (valFromType (LETYPE (tree)),
2997 valFromType (RETYPE (tree)));
2998 tree->right = tree->left = NULL;
2999 TETYPE (tree) = getSpec (TTYPE (tree) =
3000 tree->opval.val->type);
3003 LRVAL (tree) = RRVAL (tree) = 1;
3004 TETYPE (tree) = getSpec (TTYPE (tree) =
3005 computeType (LTYPE (tree),
3011 /*------------------------------------------------------------------*/
3012 /*----------------------------*/
3013 /* address dereference */
3014 /*----------------------------*/
3015 case '*': /* can be unary : if right is null then unary operation */
3018 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3020 werror (E_PTR_REQD);
3021 goto errorTreeReturn;
3026 werror (E_LVALUE_REQUIRED, "pointer deref");
3027 goto errorTreeReturn;
3029 if (IS_ADDRESS_OF_OP(tree->left))
3031 /* replace *&obj with obj */
3032 return tree->left->left;
3034 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
3035 TETYPE (tree) = getSpec (TTYPE (tree));
3036 /* adjust the storage class */
3037 switch (DCL_TYPE(tree->left->ftype)) {
3039 SPEC_SCLS(TETYPE(tree)) = S_DATA;
3042 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
3045 SPEC_SCLS(TETYPE(tree)) = S_CODE;
3048 SPEC_SCLS (TETYPE (tree)) = 0;
3051 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
3054 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
3057 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
3060 SPEC_SCLS (TETYPE (tree)) = 0;
3069 /*------------------------------------------------------------------*/
3070 /*----------------------------*/
3071 /* multiplication */
3072 /*----------------------------*/
3073 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
3075 werror (E_INVALID_OP, "multiplication");
3076 goto errorTreeReturn;
3079 /* if they are both literal then */
3080 /* rewrite the tree */
3081 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3083 tree->type = EX_VALUE;
3084 tree->opval.val = valMult (valFromType (LETYPE (tree)),
3085 valFromType (RETYPE (tree)));
3086 tree->right = tree->left = NULL;
3087 TETYPE (tree) = getSpec (TTYPE (tree) =
3088 tree->opval.val->type);
3092 /* if left is a literal exchange left & right */
3093 if (IS_LITERAL (LTYPE (tree)))
3095 ast *tTree = tree->left;
3096 tree->left = tree->right;
3097 tree->right = tTree;
3100 /* if right is a literal and */
3101 /* we can find a 2nd literal in a mul-tree then */
3102 /* rearrange the tree */
3103 if (IS_LITERAL (RTYPE (tree)))
3106 ast *litTree = searchLitOp (tree, &parent, "*");
3110 ast *tTree = litTree->left;
3111 litTree->left = tree->right;
3112 tree->right = tTree;
3113 /* both operands in litTree are literal now */
3114 decorateType (parent, resultType);
3118 LRVAL (tree) = RRVAL (tree) = 1;
3119 tree->left = addCast (tree->left, resultType, FALSE);
3120 tree->right = addCast (tree->right, resultType, FALSE);
3121 TETYPE (tree) = getSpec (TTYPE (tree) =
3122 computeType (LTYPE (tree),
3129 /*------------------------------------------------------------------*/
3130 /*----------------------------*/
3131 /* unary '+' operator */
3132 /*----------------------------*/
3137 if (!IS_ARITHMETIC (LTYPE (tree)))
3139 werror (E_UNARY_OP, '+');
3140 goto errorTreeReturn;
3143 /* if left is a literal then do it */
3144 if (IS_LITERAL (LTYPE (tree)))
3146 tree->type = EX_VALUE;
3147 tree->opval.val = valFromType (LETYPE (tree));
3149 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3153 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3157 /*------------------------------------------------------------------*/
3158 /*----------------------------*/
3160 /*----------------------------*/
3162 /* this is not a unary operation */
3163 /* if both pointers then problem */
3164 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3165 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3167 werror (E_PTR_PLUS_PTR);
3168 goto errorTreeReturn;
3171 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3172 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3174 werror (E_PLUS_INVALID, "+");
3175 goto errorTreeReturn;
3178 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3179 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3181 werror (E_PLUS_INVALID, "+");
3182 goto errorTreeReturn;
3184 /* if they are both literal then */
3185 /* rewrite the tree */
3186 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3188 tree->type = EX_VALUE;
3189 tree->left = addCast (tree->left, resultType, TRUE);
3190 tree->right = addCast (tree->right, resultType, TRUE);
3191 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3192 valFromType (RETYPE (tree)));
3193 tree->right = tree->left = NULL;
3194 TETYPE (tree) = getSpec (TTYPE (tree) =
3195 tree->opval.val->type);
3199 /* if the right is a pointer or left is a literal
3200 xchange left & right */
3201 if (IS_ARRAY (RTYPE (tree)) ||
3202 IS_PTR (RTYPE (tree)) ||
3203 IS_LITERAL (LTYPE (tree)))
3205 ast *tTree = tree->left;
3206 tree->left = tree->right;
3207 tree->right = tTree;
3210 /* if right is a literal and */
3211 /* left is also an addition/subtraction with a literal then */
3212 /* rearrange the tree */
3213 if (IS_LITERAL (RTYPE (tree)))
3215 ast *litTree, *parent;
3216 litTree = searchLitOp (tree, &parent, "+-");
3219 if (litTree->opval.op == '+')
3223 ast *tTree = litTree->left;
3224 litTree->left = tree->right;
3225 tree->right = tree->left;
3228 else if (litTree->opval.op == '-')
3230 if (IS_LITERAL (RTYPE (litTree)))
3234 ast *tTree = litTree->left;
3235 litTree->left = tree->right;
3236 tree->right = tTree;
3242 ast *tTree = litTree->right;
3243 litTree->right = tree->right;
3244 tree->right = tTree;
3245 litTree->opval.op = '+';
3246 tree->opval.op = '-';
3249 decorateType (parent, resultType);
3253 LRVAL (tree) = RRVAL (tree) = 1;
3254 /* if the left is a pointer */
3255 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3256 TETYPE (tree) = getSpec (TTYPE (tree) =
3260 tree->left = addCast (tree->left, resultType, TRUE);
3261 tree->right = addCast (tree->right, resultType, TRUE);
3262 TETYPE (tree) = getSpec (TTYPE (tree) =
3263 computeType (LTYPE (tree),
3271 /*------------------------------------------------------------------*/
3272 /*----------------------------*/
3274 /*----------------------------*/
3275 case '-': /* can be unary */
3276 /* if right is null then unary */
3280 if (!IS_ARITHMETIC (LTYPE (tree)))
3282 werror (E_UNARY_OP, tree->opval.op);
3283 goto errorTreeReturn;
3286 /* if left is a literal then do it */
3287 if (IS_LITERAL (LTYPE (tree)))
3289 tree->type = EX_VALUE;
3290 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3292 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3295 tree->left = addCast (tree->left, resultType, TRUE);
3296 TETYPE (tree) = getSpec (TTYPE (tree) =
3297 computeType (LTYPE (tree),
3305 /*------------------------------------------------------------------*/
3306 /*----------------------------*/
3308 /*----------------------------*/
3310 if (!(IS_PTR (LTYPE (tree)) ||
3311 IS_ARRAY (LTYPE (tree)) ||
3312 IS_ARITHMETIC (LTYPE (tree))))
3314 werror (E_PLUS_INVALID, "-");
3315 goto errorTreeReturn;
3318 if (!(IS_PTR (RTYPE (tree)) ||
3319 IS_ARRAY (RTYPE (tree)) ||
3320 IS_ARITHMETIC (RTYPE (tree))))
3322 werror (E_PLUS_INVALID, "-");
3323 goto errorTreeReturn;
3326 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3327 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3328 IS_INTEGRAL (RTYPE (tree))))
3330 werror (E_PLUS_INVALID, "-");
3331 goto errorTreeReturn;
3334 /* if they are both literal then */
3335 /* rewrite the tree */
3336 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3338 tree->type = EX_VALUE;
3339 tree->left = addCast (tree->left, resultType, TRUE);
3340 tree->right = addCast (tree->right, resultType, TRUE);
3341 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3342 valFromType (RETYPE (tree)));
3343 tree->right = tree->left = NULL;
3344 TETYPE (tree) = getSpec (TTYPE (tree) =
3345 tree->opval.val->type);
3349 /* if the left & right are equal then zero */
3350 if (isAstEqual (tree->left, tree->right))
3352 tree->type = EX_VALUE;
3353 tree->left = tree->right = NULL;
3354 tree->opval.val = constVal ("0");
3355 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3359 /* if both of them are pointers or arrays then */
3360 /* the result is going to be an integer */
3361 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3362 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3363 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3365 /* if only the left is a pointer */
3366 /* then result is a pointer */
3367 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3368 TETYPE (tree) = getSpec (TTYPE (tree) =
3372 tree->left = addCast (tree->left, resultType, TRUE);
3373 tree->right = addCast (tree->right, resultType, TRUE);
3375 TETYPE (tree) = getSpec (TTYPE (tree) =
3376 computeType (LTYPE (tree),
3382 LRVAL (tree) = RRVAL (tree) = 1;
3384 /* if right is a literal and */
3385 /* left is also an addition/subtraction with a literal then */
3386 /* rearrange the tree */
3387 if (IS_LITERAL (RTYPE (tree))
3388 /* avoid infinite loop */
3389 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3391 ast *litTree, *litParent;
3392 litTree = searchLitOp (tree, &litParent, "+-");
3395 if (litTree->opval.op == '+')
3399 ast *tTree = litTree->left;
3400 litTree->left = litTree->right;
3401 litTree->right = tree->right;
3402 tree->right = tTree;
3403 tree->opval.op = '+';
3404 litTree->opval.op = '-';
3406 else if (litTree->opval.op == '-')
3408 if (IS_LITERAL (RTYPE (litTree)))
3412 ast *tTree = litTree->left;
3413 litTree->left = tree->right;
3414 tree->right = litParent->left;
3415 litParent->left = tTree;
3416 litTree->opval.op = '+';
3418 tree->decorated = 0;
3419 decorateType (tree, resultType);
3425 ast *tTree = litTree->right;
3426 litTree->right = tree->right;
3427 tree->right = tTree;
3430 decorateType (litParent, resultType);
3435 /*------------------------------------------------------------------*/
3436 /*----------------------------*/
3438 /*----------------------------*/
3440 /* can be only integral type */
3441 if (!IS_INTEGRAL (LTYPE (tree)))
3443 werror (E_UNARY_OP, tree->opval.op);
3444 goto errorTreeReturn;
3447 /* if left is a literal then do it */
3448 if (IS_LITERAL (LTYPE (tree)))
3450 tree->type = EX_VALUE;
3451 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3453 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3454 return addCast (tree, resultType, TRUE);
3457 if (resultType == RESULT_TYPE_BIT &&
3458 IS_UNSIGNED (tree->left->etype) &&
3459 getSize (tree->left->etype) < INTSIZE)
3461 /* promotion rules are responsible for this strange result:
3462 bit -> int -> ~int -> bit
3463 uchar -> int -> ~int -> bit
3465 werror(W_COMPLEMENT);
3467 /* optimize bit-result, even if we optimize a buggy source */
3468 tree->type = EX_VALUE;
3469 tree->opval.val = constVal ("1");
3472 tree->left = addCast (tree->left, resultType, TRUE);
3474 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3477 /*------------------------------------------------------------------*/
3478 /*----------------------------*/
3480 /*----------------------------*/
3482 /* can be pointer */
3483 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3484 !IS_PTR (LTYPE (tree)) &&
3485 !IS_ARRAY (LTYPE (tree)))
3487 werror (E_UNARY_OP, tree->opval.op);
3488 goto errorTreeReturn;
3491 /* if left is a literal then do it */
3492 if (IS_LITERAL (LTYPE (tree)))
3494 tree->type = EX_VALUE;
3495 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3497 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3501 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3504 /*------------------------------------------------------------------*/
3505 /*----------------------------*/
3507 /*----------------------------*/
3511 TTYPE (tree) = LTYPE (tree);
3512 TETYPE (tree) = LETYPE (tree);
3517 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3521 TTYPE (tree) = TETYPE (tree) = newCharLink();
3525 TTYPE (tree) = TETYPE (tree) = newIntLink();
3530 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3532 werror (E_SHIFT_OP_INVALID);
3533 werror (W_CONTINUE, "left & right types are ");
3534 printTypeChain (LTYPE (tree), stderr);
3535 fprintf (stderr, ",");
3536 printTypeChain (RTYPE (tree), stderr);
3537 fprintf (stderr, "\n");
3538 goto errorTreeReturn;
3541 /* make smaller type only if it's a LEFT_OP */
3542 if (tree->opval.op == LEFT_OP)
3543 tree->left = addCast (tree->left, resultType, TRUE);
3545 /* if they are both literal then */
3546 /* rewrite the tree */
3547 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3549 tree->type = EX_VALUE;
3550 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3551 valFromType (RETYPE (tree)),
3552 (tree->opval.op == LEFT_OP ? 1 : 0));
3553 tree->right = tree->left = NULL;
3554 TETYPE (tree) = getSpec (TTYPE (tree) =
3555 tree->opval.val->type);
3559 /* see if this is a GETBYTE operation if yes
3562 ast *otree = optimizeGetByte (tree, resultType);
3565 return decorateType (otree, RESULT_TYPE_NONE);
3568 /* see if this is a GETWORD operation if yes
3571 ast *otree = optimizeGetWord (tree, resultType);
3574 return decorateType (otree, RESULT_TYPE_NONE);
3577 LRVAL (tree) = RRVAL (tree) = 1;
3578 if (tree->opval.op == LEFT_OP)
3580 TETYPE (tree) = getSpec (TTYPE (tree) =
3581 computeType (LTYPE (tree),
3588 /* no promotion necessary */
3589 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3590 if (IS_LITERAL (TTYPE (tree)))
3591 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3594 /* if only the right side is a literal & we are
3595 shifting more than size of the left operand then zero */
3596 if (IS_LITERAL (RTYPE (tree)) &&
3597 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3598 (getSize (TETYPE (tree)) * 8))
3600 if (tree->opval.op==LEFT_OP ||
3601 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3603 lineno=tree->lineno;
3604 werror (W_SHIFT_CHANGED,
3605 (tree->opval.op == LEFT_OP ? "left" : "right"));
3606 tree->type = EX_VALUE;
3607 tree->left = tree->right = NULL;
3608 tree->opval.val = constVal ("0");
3609 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3616 /*------------------------------------------------------------------*/
3617 /*----------------------------*/
3619 /*----------------------------*/
3620 case CAST: /* change the type */
3621 /* cannot cast to an aggregate type */
3622 if (IS_AGGREGATE (LTYPE (tree)))
3624 werror (E_CAST_ILLEGAL);
3625 goto errorTreeReturn;
3628 /* make sure the type is complete and sane */
3629 changePointer(LTYPE(tree));
3630 checkTypeSanity(LETYPE(tree), "(cast)");
3632 /* if 'from' and 'to' are the same remove the superflous cast, */
3633 /* this helps other optimizations */
3634 if (compareTypeExact (LTYPE(tree), RTYPE(tree), -1) == 1)
3639 /* If code memory is read only, then pointers to code memory */
3640 /* implicitly point to constants -- make this explicit */
3642 sym_link *t = LTYPE(tree);
3643 while (t && t->next)
3645 if (IS_CODEPTR(t) && port->mem.code_ro)
3647 if (IS_SPEC(t->next))
3648 SPEC_CONST (t->next) = 1;
3650 DCL_PTR_CONST (t->next) = 1;
3657 /* if the right is a literal replace the tree */
3658 if (IS_LITERAL (RETYPE (tree))) {
3659 if (!IS_PTR (LTYPE (tree))) {
3660 tree->type = EX_VALUE;
3662 valCastLiteral (LTYPE (tree),
3663 floatFromVal (valFromType (RETYPE (tree))));
3666 TTYPE (tree) = tree->opval.val->type;
3667 tree->values.literalFromCast = 1;
3668 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3669 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3670 sym_link *rest = LTYPE(tree)->next;
3671 werror(W_LITERAL_GENERIC);
3672 TTYPE(tree) = newLink(DECLARATOR);
3673 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3674 TTYPE(tree)->next = rest;
3675 tree->left->opval.lnk = TTYPE(tree);
3678 TTYPE (tree) = LTYPE (tree);
3682 TTYPE (tree) = LTYPE (tree);
3686 #if 0 // this is already checked, now this could be explicit
3687 /* if pointer to struct then check names */
3688 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3689 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3690 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3692 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3693 SPEC_STRUCT(LETYPE(tree))->tag);
3696 if (IS_ADDRESS_OF_OP(tree->right)
3697 && IS_AST_SYM_VALUE (tree->right->left)
3698 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3700 symbol * sym = AST_SYMBOL (tree->right->left);
3701 unsigned int gptype = 0;
3702 unsigned int addr = SPEC_ADDR (sym->etype);
3704 if (IS_GENPTR (LTYPE (tree)) && GPTRSIZE > FPTRSIZE)
3706 switch (SPEC_SCLS (sym->etype))
3709 gptype = GPTYPE_CODE;
3712 gptype = GPTYPE_FAR;
3716 gptype = GPTYPE_NEAR;
3719 gptype = GPTYPE_XSTACK;
3724 addr |= gptype << (8*(GPTRSIZE - 1));
3727 tree->type = EX_VALUE;
3729 valCastLiteral (LTYPE (tree), addr);
3730 TTYPE (tree) = tree->opval.val->type;
3731 TETYPE (tree) = getSpec (TTYPE (tree));
3734 tree->values.literalFromCast = 1;
3738 /* handle offsetof macro: */
3739 /* #define offsetof(TYPE, MEMBER) \ */
3740 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3741 if (IS_ADDRESS_OF_OP(tree->right)
3742 && IS_AST_OP (tree->right->left)
3743 && tree->right->left->opval.op == PTR_OP
3744 && IS_AST_OP (tree->right->left->left)
3745 && tree->right->left->left->opval.op == CAST
3746 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3748 symbol *element = getStructElement (
3749 SPEC_STRUCT (LETYPE(tree->right->left)),
3750 AST_SYMBOL(tree->right->left->right)
3754 tree->type = EX_VALUE;
3755 tree->opval.val = valCastLiteral (
3758 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3761 TTYPE (tree) = tree->opval.val->type;
3762 TETYPE (tree) = getSpec (TTYPE (tree));
3769 /* if the right is a literal replace the tree */
3770 if (IS_LITERAL (RETYPE (tree))) {
3772 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3773 /* rewrite (type *)litaddr
3775 and define type at litaddr temp
3776 (but only if type's storage class is not generic)
3778 ast *newTree = newNode ('&', NULL, NULL);
3781 TTYPE (newTree) = LTYPE (tree);
3782 TETYPE (newTree) = getSpec(LTYPE (tree));
3784 /* define a global symbol at the casted address*/
3785 sym = newSymbol(genSymName (0), 0);
3786 sym->type = LTYPE (tree)->next;
3788 sym->type = newLink (V_VOID);
3789 sym->etype = getSpec(sym->type);
3790 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3791 sym->lineDef = tree->lineno;
3794 SPEC_STAT (sym->etype) = 1;
3795 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3796 SPEC_ABSA(sym->etype) = 1;
3797 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3800 newTree->left = newAst_VALUE(symbolVal(sym));
3801 newTree->left->lineno = tree->lineno;
3802 LTYPE (newTree) = sym->type;
3803 LETYPE (newTree) = sym->etype;
3804 LLVAL (newTree) = 1;
3805 LRVAL (newTree) = 0;
3806 TLVAL (newTree) = 1;
3810 if (!IS_PTR (LTYPE (tree))) {
3811 tree->type = EX_VALUE;
3813 valCastLiteral (LTYPE (tree),
3814 floatFromVal (valFromType (RTYPE (tree))));
3815 TTYPE (tree) = tree->opval.val->type;
3818 tree->values.literalFromCast = 1;
3819 TETYPE (tree) = getSpec (TTYPE (tree));
3823 TTYPE (tree) = LTYPE (tree);
3827 TETYPE (tree) = getSpec (TTYPE (tree));
3831 /*------------------------------------------------------------------*/
3832 /*----------------------------*/
3833 /* logical &&, || */
3834 /*----------------------------*/
3837 /* each must be arithmetic type or be a pointer */
3838 if (!IS_PTR (LTYPE (tree)) &&
3839 !IS_ARRAY (LTYPE (tree)) &&
3840 !IS_INTEGRAL (LTYPE (tree)))
3842 werror (E_COMPARE_OP);
3843 goto errorTreeReturn;
3846 if (!IS_PTR (RTYPE (tree)) &&
3847 !IS_ARRAY (RTYPE (tree)) &&
3848 !IS_INTEGRAL (RTYPE (tree)))
3850 werror (E_COMPARE_OP);
3851 goto errorTreeReturn;
3853 /* if they are both literal then */
3854 /* rewrite the tree */
3855 if (IS_LITERAL (RTYPE (tree)) &&
3856 IS_LITERAL (LTYPE (tree)))
3858 tree->type = EX_VALUE;
3859 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
3860 valFromType (RTYPE (tree)),
3862 tree->right = tree->left = NULL;
3863 TETYPE (tree) = getSpec (TTYPE (tree) =
3864 tree->opval.val->type);
3867 LRVAL (tree) = RRVAL (tree) = 1;
3868 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3871 /*------------------------------------------------------------------*/
3872 /*----------------------------*/
3873 /* comparison operators */
3874 /*----------------------------*/
3882 ast *lt = optimizeCompare (tree);
3888 /* if they are pointers they must be castable */
3889 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
3891 if (tree->opval.op==EQ_OP &&
3892 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
3893 // we cannot cast a gptr to a !gptr: switch the leaves
3894 struct ast *s=tree->left;
3895 tree->left=tree->right;
3898 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3900 werror (E_COMPARE_OP);
3901 fprintf (stderr, "comparing type ");
3902 printTypeChain (LTYPE (tree), stderr);
3903 fprintf (stderr, "to type ");
3904 printTypeChain (RTYPE (tree), stderr);
3905 fprintf (stderr, "\n");
3906 goto errorTreeReturn;
3909 /* else they should be promotable to one another */
3912 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
3913 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
3915 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
3917 werror (E_COMPARE_OP);
3918 fprintf (stderr, "comparing type ");
3919 printTypeChain (LTYPE (tree), stderr);
3920 fprintf (stderr, "to type ");
3921 printTypeChain (RTYPE (tree), stderr);
3922 fprintf (stderr, "\n");
3923 goto errorTreeReturn;
3926 /* if unsigned value < 0 then always false */
3927 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
3928 if (SPEC_USIGN(LETYPE(tree)) &&
3929 !IS_CHAR(LETYPE(tree)) && /* promotion to signed int */
3930 IS_LITERAL(RTYPE(tree)) &&
3931 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
3933 if (tree->opval.op == '<')
3937 if (tree->opval.op == '>')
3939 if (resultType == RESULT_TYPE_IFX)
3941 /* the parent is an ifx: */
3942 /* if (unsigned value) */
3946 /* (unsigned value) ? 1 : 0 */
3947 tree->opval.op = '?';
3948 tree->right = newNode (':',
3949 newAst_VALUE (constVal ("1")),
3950 tree->right); /* val 0 */
3951 tree->right->lineno = tree->lineno;
3952 tree->right->left->lineno = tree->lineno;
3953 decorateType (tree->right, RESULT_TYPE_NONE);
3956 /* if they are both literal then */
3957 /* rewrite the tree */
3958 if (IS_LITERAL (RTYPE (tree)) &&
3959 IS_LITERAL (LTYPE (tree)))
3961 tree->type = EX_VALUE;
3962 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
3963 valFromType (RETYPE (tree)),
3965 tree->right = tree->left = NULL;
3966 TETYPE (tree) = getSpec (TTYPE (tree) =
3967 tree->opval.val->type);
3970 /* if one is 'signed char ' and the other one is 'unsigned char' */
3971 /* it's necessary to promote to int */
3972 if (IS_CHAR (RTYPE (tree)) && IS_CHAR (LTYPE (tree)) &&
3973 (IS_UNSIGNED (RTYPE (tree)) != IS_UNSIGNED (LTYPE (tree))))
3975 /* Literals are 'optimized' to 'unsigned char'. Try to figure out,
3976 if it's possible to use a 'signed char' */
3978 /* is left a 'unsigned char'? */
3979 if (IS_LITERAL (RTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)) &&
3980 /* the value range of a 'unsigned char' is 0...255;
3981 if the actual value is < 128 it can be changed to signed */
3982 (int) floatFromVal (valFromType (RETYPE (tree))) < 128)
3984 /* now we've got 2 'signed char'! */
3985 SPEC_USIGN (RETYPE (tree)) = 0;
3987 /* same test for the left operand: */
3988 else if (IS_LITERAL (LTYPE (tree)) && IS_UNSIGNED (LTYPE (tree)) &&
3989 (int) floatFromVal (valFromType (LETYPE (tree))) < 128)
3991 SPEC_USIGN (LETYPE (tree)) = 0;
3995 werror (W_CMP_SU_CHAR);
3996 tree->left = addCast (tree->left , RESULT_TYPE_INT, TRUE);
3997 tree->right = addCast (tree->right, RESULT_TYPE_INT, TRUE);
4001 LRVAL (tree) = RRVAL (tree) = 1;
4002 TTYPE (tree) = TETYPE (tree) = newBoolLink ();
4005 /*------------------------------------------------------------------*/
4006 /*----------------------------*/
4008 /*----------------------------*/
4009 case SIZEOF: /* evaluate wihout code generation */
4010 /* change the type to a integer */
4012 int size = getSize (tree->right->ftype);
4013 SNPRINTF(buffer, sizeof(buffer), "%d", size);
4014 if (!size && !IS_VOID(tree->right->ftype))
4015 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
4017 tree->type = EX_VALUE;
4018 tree->opval.val = constVal (buffer);
4019 tree->right = tree->left = NULL;
4020 TETYPE (tree) = getSpec (TTYPE (tree) =
4021 tree->opval.val->type);
4024 /*------------------------------------------------------------------*/
4025 /*----------------------------*/
4027 /*----------------------------*/
4029 /* return typeof enum value */
4030 tree->type = EX_VALUE;
4033 if (IS_SPEC(tree->right->ftype)) {
4034 switch (SPEC_NOUN(tree->right->ftype)) {
4036 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
4037 else typeofv = TYPEOF_INT;
4040 typeofv = TYPEOF_FLOAT;
4043 typeofv = TYPEOF_FIXED16X16;
4046 typeofv = TYPEOF_CHAR;
4049 typeofv = TYPEOF_VOID;
4052 typeofv = TYPEOF_STRUCT;
4055 typeofv = TYPEOF_BITFIELD;
4058 typeofv = TYPEOF_BIT;
4061 typeofv = TYPEOF_SBIT;
4067 switch (DCL_TYPE(tree->right->ftype)) {
4069 typeofv = TYPEOF_POINTER;
4072 typeofv = TYPEOF_FPOINTER;
4075 typeofv = TYPEOF_CPOINTER;
4078 typeofv = TYPEOF_GPOINTER;
4081 typeofv = TYPEOF_PPOINTER;
4084 typeofv = TYPEOF_IPOINTER;
4087 typeofv = TYPEOF_ARRAY;
4090 typeofv = TYPEOF_FUNCTION;
4096 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
4097 tree->opval.val = constVal (buffer);
4098 tree->right = tree->left = NULL;
4099 TETYPE (tree) = getSpec (TTYPE (tree) =
4100 tree->opval.val->type);
4103 /*------------------------------------------------------------------*/
4104 /*----------------------------*/
4105 /* conditional operator '?' */
4106 /*----------------------------*/
4108 /* the type is value of the colon operator (on the right) */
4109 assert (IS_COLON_OP (tree->right));
4110 /* if already known then replace the tree : optimizer will do it
4111 but faster to do it here */
4112 if (IS_LITERAL (LTYPE (tree)))
4114 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
4115 return decorateType (tree->right->left, resultTypeProp);
4117 return decorateType (tree->right->right, resultTypeProp);
4121 tree->right = decorateType (tree->right, resultTypeProp);
4122 TTYPE (tree) = RTYPE (tree);
4123 TETYPE (tree) = getSpec (TTYPE (tree));
4128 /* if they don't match we have a problem */
4129 if ((compareType (LTYPE (tree), RTYPE (tree)) == 0) &&
4130 (compareType (RTYPE (tree), LTYPE (tree)) == 0))
4132 werror (E_TYPE_MISMATCH, "conditional operator", " ");
4133 goto errorTreeReturn;
4136 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
4137 resultType, tree->opval.op);
4138 TETYPE (tree) = getSpec (TTYPE (tree));
4142 #if 0 // assignment operators are converted by the parser
4143 /*------------------------------------------------------------------*/
4144 /*----------------------------*/
4145 /* assignment operators */
4146 /*----------------------------*/
4149 /* for these it must be both must be integral */
4150 if (!IS_ARITHMETIC (LTYPE (tree)) ||
4151 !IS_ARITHMETIC (RTYPE (tree)))
4153 werror (E_OPS_INTEGRAL);
4154 goto errorTreeReturn;
4157 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4159 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
4160 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4164 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4165 goto errorTreeReturn;
4176 /* for these it must be both must be integral */
4177 if (!IS_INTEGRAL (LTYPE (tree)) ||
4178 !IS_INTEGRAL (RTYPE (tree)))
4180 werror (E_OPS_INTEGRAL);
4181 goto errorTreeReturn;
4184 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4186 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4187 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
4191 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
4192 goto errorTreeReturn;
4198 /*------------------------------------------------------------------*/
4199 /*----------------------------*/
4201 /*----------------------------*/
4203 if (!(IS_PTR (LTYPE (tree)) ||
4204 IS_ARITHMETIC (LTYPE (tree))))
4206 werror (E_PLUS_INVALID, "-=");
4207 goto errorTreeReturn;
4210 if (!(IS_PTR (RTYPE (tree)) ||
4211 IS_ARITHMETIC (RTYPE (tree))))
4213 werror (E_PLUS_INVALID, "-=");
4214 goto errorTreeReturn;
4217 TETYPE (tree) = getSpec (TTYPE (tree) =
4218 computeType (LTYPE (tree),
4223 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4224 werror (E_CODE_WRITE, "-=");
4228 werror (E_LVALUE_REQUIRED, "-=");
4229 goto errorTreeReturn;
4235 /*------------------------------------------------------------------*/
4236 /*----------------------------*/
4238 /*----------------------------*/
4240 /* this is not a unary operation */
4241 /* if both pointers then problem */
4242 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4244 werror (E_PTR_PLUS_PTR);
4245 goto errorTreeReturn;
4248 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
4250 werror (E_PLUS_INVALID, "+=");
4251 goto errorTreeReturn;
4254 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
4256 werror (E_PLUS_INVALID, "+=");
4257 goto errorTreeReturn;
4260 TETYPE (tree) = getSpec (TTYPE (tree) =
4261 computeType (LTYPE (tree),
4266 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4267 werror (E_CODE_WRITE, "+=");
4271 werror (E_LVALUE_REQUIRED, "+=");
4272 goto errorTreeReturn;
4275 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_TYPE_NONE);
4276 tree->opval.op = '=';
4281 /*------------------------------------------------------------------*/
4282 /*----------------------------*/
4283 /* straight assignemnt */
4284 /*----------------------------*/
4286 /* cannot be an aggregate */
4287 if (IS_AGGREGATE (LTYPE (tree)))
4289 werror (E_AGGR_ASSIGN);
4290 goto errorTreeReturn;
4293 /* they should either match or be castable */
4294 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4296 werror (E_TYPE_MISMATCH, "assignment", " ");
4297 printFromToType(RTYPE(tree),LTYPE(tree));
4300 /* if the left side of the tree is of type void
4301 then report error */
4302 if (IS_VOID (LTYPE (tree)))
4304 werror (E_CAST_ZERO);
4305 printFromToType(RTYPE(tree), LTYPE(tree));
4308 TETYPE (tree) = getSpec (TTYPE (tree) =
4312 if (!tree->initMode ) {
4313 if (IS_CONSTANT(LTYPE(tree)))
4314 werror (E_CODE_WRITE, "=");
4318 werror (E_LVALUE_REQUIRED, "=");
4319 goto errorTreeReturn;
4324 /*------------------------------------------------------------------*/
4325 /*----------------------------*/
4326 /* comma operator */
4327 /*----------------------------*/
4329 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4332 /*------------------------------------------------------------------*/
4333 /*----------------------------*/
4335 /*----------------------------*/
4338 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4339 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4341 if (tree->left->opval.op == '*' && !tree->left->right)
4342 tree->left = tree->left->left;
4345 /* require a function or pointer to function */
4346 if (!IS_FUNC (LTYPE (tree)) && !IS_FUNCPTR (LTYPE (tree)))
4348 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4349 goto errorTreeReturn;
4352 /* if there are parms, make sure that
4353 parms are decorate / process / reverse only once */
4355 !tree->right->decorated)
4360 if (IS_FUNCPTR (LTYPE (tree)))
4362 functype = LTYPE (tree)->next;
4363 processFuncPtrArgs (functype);
4366 functype = LTYPE (tree);
4368 if (processParms (tree->left, FUNC_ARGS(functype),
4369 &tree->right, &parmNumber, TRUE))
4371 goto errorTreeReturn;
4374 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4375 !IFFUNC_ISBUILTIN(functype))
4377 reverseParms (tree->right);
4380 TTYPE (tree) = functype->next;
4381 TETYPE (tree) = getSpec (TTYPE (tree));
4385 /*------------------------------------------------------------------*/
4386 /*----------------------------*/
4387 /* return statement */
4388 /*----------------------------*/
4393 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4395 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4396 printFromToType (RTYPE(tree), currFunc->type->next);
4397 goto errorTreeReturn;
4400 if (IS_VOID (currFunc->type->next)
4402 !IS_VOID (RTYPE (tree)))
4404 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4405 goto errorTreeReturn;
4408 /* if there is going to be a casting required then add it */
4409 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4412 decorateType (newNode (CAST,
4413 newAst_LINK (copyLinkChain (currFunc->type->next)),
4423 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4425 werror (W_VOID_FUNC, currFunc->name);
4426 goto errorTreeReturn;
4429 TTYPE (tree) = TETYPE (tree) = NULL;
4432 /*------------------------------------------------------------------*/
4433 /*----------------------------*/
4434 /* switch statement */
4435 /*----------------------------*/
4437 /* the switch value must be an integer */
4438 if (!IS_INTEGRAL (LTYPE (tree)))
4440 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4441 goto errorTreeReturn;
4444 TTYPE (tree) = TETYPE (tree) = NULL;
4447 /*------------------------------------------------------------------*/
4448 /*----------------------------*/
4450 /*----------------------------*/
4452 tree->left = backPatchLabels (tree->left,
4455 TTYPE (tree) = TETYPE (tree) = NULL;
4458 /*------------------------------------------------------------------*/
4459 /*----------------------------*/
4461 /*----------------------------*/
4464 decorateType (resolveSymbols (AST_FOR (tree, initExpr)), RESULT_TYPE_NONE);
4465 decorateType (resolveSymbols (AST_FOR (tree, condExpr)), RESULT_TYPE_NONE);
4466 decorateType (resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_TYPE_NONE);
4468 /* if the for loop is reversible then
4469 reverse it otherwise do what we normally
4475 if (isLoopReversible (tree, &sym, &init, &end))
4476 return reverseLoop (tree, sym, init, end);
4478 return decorateType (createFor (AST_FOR (tree, trueLabel),
4479 AST_FOR (tree, continueLabel),
4480 AST_FOR (tree, falseLabel),
4481 AST_FOR (tree, condLabel),
4482 AST_FOR (tree, initExpr),
4483 AST_FOR (tree, condExpr),
4484 AST_FOR (tree, loopExpr),
4485 tree->left), RESULT_TYPE_NONE);
4488 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4489 "node PARAM shouldn't be processed here");
4490 /* but in processParams() */
4493 TTYPE (tree) = TETYPE (tree) = NULL;
4497 /* some error found this tree will be killed */
4499 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4500 tree->opval.op = NULLOP;
4506 /*-----------------------------------------------------------------*/
4507 /* sizeofOp - processes size of operation */
4508 /*-----------------------------------------------------------------*/
4510 sizeofOp (sym_link * type)
4515 /* make sure the type is complete and sane */
4516 checkTypeSanity(type, "(sizeof)");
4518 /* get the size and convert it to character */
4519 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4520 if (!size && !IS_VOID(type))
4521 werror (E_SIZEOF_INCOMPLETE_TYPE);
4523 /* now convert into value */
4524 return constVal (buff);
4528 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4529 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4530 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4531 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4532 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4533 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4534 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4536 /*-----------------------------------------------------------------*/
4537 /* backPatchLabels - change and or not operators to flow control */
4538 /*-----------------------------------------------------------------*/
4540 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4546 if (!(IS_ANDORNOT (tree)))
4549 /* if this an and */
4552 static int localLbl = 0;
4555 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4556 localLabel = newSymbol (buffer, NestLevel);
4558 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4560 /* if left is already a IFX then just change the if true label in that */
4561 if (!IS_IFX (tree->left))
4562 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4564 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4565 /* right is a IFX then just join */
4566 if (IS_IFX (tree->right))
4567 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4569 tree->right = createLabel (localLabel, tree->right);
4570 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4572 return newNode (NULLOP, tree->left, tree->right);
4575 /* if this is an or operation */
4578 static int localLbl = 0;
4581 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4582 localLabel = newSymbol (buffer, NestLevel);
4584 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4586 /* if left is already a IFX then just change the if true label in that */
4587 if (!IS_IFX (tree->left))
4588 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4590 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4591 /* right is a IFX then just join */
4592 if (IS_IFX (tree->right))
4593 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4595 tree->right = createLabel (localLabel, tree->right);
4596 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4598 return newNode (NULLOP, tree->left, tree->right);
4604 /* call with exchanged labels */
4605 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4607 /* if left isn't already a IFX */
4608 if (!IS_IFX (tree->left))
4610 tree->left = newNode (IFX, tree->left, NULL);
4611 tree->left->trueLabel = falseLabel;
4612 tree->left->falseLabel = trueLabel;
4619 tree->trueLabel = trueLabel;
4620 tree->falseLabel = falseLabel;
4627 /*-----------------------------------------------------------------*/
4628 /* createBlock - create expression tree for block */
4629 /*-----------------------------------------------------------------*/
4631 createBlock (symbol * decl, ast * body)
4635 /* if the block has nothing */
4639 ex = newNode (BLOCK, NULL, body);
4640 ex->values.sym = decl;
4647 /*-----------------------------------------------------------------*/
4648 /* createLabel - creates the expression tree for labels */
4649 /*-----------------------------------------------------------------*/
4651 createLabel (symbol * label, ast * stmnt)
4654 char name[SDCC_NAME_MAX + 1];
4657 /* must create fresh symbol if the symbol name */
4658 /* exists in the symbol table, since there can */
4659 /* be a variable with the same name as the labl */
4660 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4661 (csym->level == label->level))
4662 label = newSymbol (label->name, label->level);
4664 /* change the name before putting it in add _ */
4665 SNPRINTF(name, sizeof(name), "%s", label->name);
4667 /* put the label in the LabelSymbol table */
4668 /* but first check if a label of the same */
4670 if ((csym = findSym (LabelTab, NULL, name)))
4671 werror (E_DUPLICATE_LABEL, label->name);
4673 addSym (LabelTab, label, name, label->level, 0, 0);
4677 label->key = labelKey++;
4678 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4684 /*-----------------------------------------------------------------*/
4685 /* createCase - generates the parsetree for a case statement */
4686 /*-----------------------------------------------------------------*/
4688 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4690 char caseLbl[SDCC_NAME_MAX + 1];
4694 /* if the switch statement does not exist */
4695 /* then case is out of context */
4698 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4702 caseVal = decorateType (resolveSymbols (caseVal), RESULT_TYPE_NONE);
4703 /* if not a constant then error */
4704 if (!IS_LITERAL (caseVal->ftype))
4706 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4710 /* if not a integer than error */
4711 if (!IS_INTEGRAL (caseVal->ftype))
4713 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4717 /* find the end of the switch values chain */
4718 if (!(val = swStat->values.switchVals.swVals))
4719 swStat->values.switchVals.swVals = caseVal->opval.val;
4722 /* also order the cases according to value */
4724 int cVal = (int) floatFromVal (caseVal->opval.val);
4725 while (val && (int) floatFromVal (val) < cVal)
4731 /* if we reached the end then */
4734 pval->next = caseVal->opval.val;
4736 else if ((int) floatFromVal (val) == cVal)
4738 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
4744 /* we found a value greater than */
4745 /* the current value we must add this */
4746 /* before the value */
4747 caseVal->opval.val->next = val;
4749 /* if this was the first in chain */
4750 if (swStat->values.switchVals.swVals == val)
4751 swStat->values.switchVals.swVals =
4754 pval->next = caseVal->opval.val;
4759 /* create the case label */
4760 SNPRINTF(caseLbl, sizeof(caseLbl),
4762 swStat->values.switchVals.swNum,
4763 (int) floatFromVal (caseVal->opval.val));
4765 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
4770 /*-----------------------------------------------------------------*/
4771 /* createDefault - creates the parse tree for the default statement */
4772 /*-----------------------------------------------------------------*/
4774 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
4776 char defLbl[SDCC_NAME_MAX + 1];
4778 /* if the switch statement does not exist */
4779 /* then case is out of context */
4782 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
4786 if (swStat->values.switchVals.swDefault)
4788 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
4793 /* turn on the default flag */
4794 swStat->values.switchVals.swDefault = 1;
4796 /* create the label */
4797 SNPRINTF (defLbl, sizeof(defLbl),
4798 "_default_%d", swStat->values.switchVals.swNum);
4799 return createLabel (newSymbol (defLbl, 0), stmnt);
4802 /*-----------------------------------------------------------------*/
4803 /* createIf - creates the parsetree for the if statement */
4804 /*-----------------------------------------------------------------*/
4806 createIf (ast * condAst, ast * ifBody, ast * elseBody)
4808 static int Lblnum = 0;
4810 symbol *ifTrue, *ifFalse, *ifEnd;
4812 /* if neither exists */
4813 if (!elseBody && !ifBody) {
4814 // if there are no side effects (i++, j() etc)
4815 if (!hasSEFcalls(condAst)) {
4820 /* create the labels */
4821 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
4822 ifFalse = newSymbol (buffer, NestLevel);
4823 /* if no else body then end == false */
4828 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
4829 ifEnd = newSymbol (buffer, NestLevel);
4832 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
4833 ifTrue = newSymbol (buffer, NestLevel);
4837 /* attach the ifTrue label to the top of it body */
4838 ifBody = createLabel (ifTrue, ifBody);
4839 /* attach a goto end to the ifBody if else is present */
4842 ifBody = newNode (NULLOP, ifBody,
4844 newAst_VALUE (symbolVal (ifEnd)),
4846 /* put the elseLabel on the else body */
4847 elseBody = createLabel (ifFalse, elseBody);
4848 /* out the end at the end of the body */
4849 elseBody = newNode (NULLOP,
4851 createLabel (ifEnd, NULL));
4855 ifBody = newNode (NULLOP, ifBody,
4856 createLabel (ifFalse, NULL));
4858 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
4859 if (IS_IFX (condAst))
4862 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
4864 return newNode (NULLOP, ifTree,
4865 newNode (NULLOP, ifBody, elseBody));
4869 /*-----------------------------------------------------------------*/
4870 /* createDo - creates parse tree for do */
4873 /* _docontinue_n: */
4874 /* condition_expression +-> trueLabel -> _dobody_n */
4876 /* +-> falseLabel-> _dobreak_n */
4878 /*-----------------------------------------------------------------*/
4880 createDo (symbol * trueLabel, symbol * continueLabel,
4881 symbol * falseLabel, ast * condAst, ast * doBody)
4886 /* if the body does not exist then it is simple */
4889 condAst = backPatchLabels (condAst, continueLabel, NULL);
4890 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
4891 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
4892 doTree->trueLabel = continueLabel;
4893 doTree->falseLabel = NULL;
4897 /* otherwise we have a body */
4898 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
4900 /* attach the body label to the top */
4901 doBody = createLabel (trueLabel, doBody);
4902 /* attach the continue label to end of body */
4903 doBody = newNode (NULLOP, doBody,
4904 createLabel (continueLabel, NULL));
4906 /* now put the break label at the end */
4907 if (IS_IFX (condAst))
4910 doTree = newIfxNode (condAst, trueLabel, falseLabel);
4912 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
4914 /* putting it together */
4915 return newNode (NULLOP, doBody, doTree);
4918 /*-----------------------------------------------------------------*/
4919 /* createFor - creates parse tree for 'for' statement */
4922 /* condExpr +-> trueLabel -> _forbody_n */
4924 /* +-> falseLabel-> _forbreak_n */
4927 /* _forcontinue_n: */
4929 /* goto _forcond_n ; */
4931 /*-----------------------------------------------------------------*/
4933 createFor (symbol * trueLabel, symbol * continueLabel,
4934 symbol * falseLabel, symbol * condLabel,
4935 ast * initExpr, ast * condExpr, ast * loopExpr,
4940 /* if loopexpression not present then we can generate it */
4941 /* the same way as a while */
4943 return newNode (NULLOP, initExpr,
4944 createWhile (trueLabel, continueLabel,
4945 falseLabel, condExpr, forBody));
4946 /* vanilla for statement */
4947 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4949 if (condExpr && !IS_IFX (condExpr))
4950 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
4953 /* attach condition label to condition */
4954 condExpr = createLabel (condLabel, condExpr);
4956 /* attach body label to body */
4957 forBody = createLabel (trueLabel, forBody);
4959 /* attach continue to forLoop expression & attach */
4960 /* goto the forcond @ and of loopExpression */
4961 loopExpr = createLabel (continueLabel,
4965 newAst_VALUE (symbolVal (condLabel)),
4967 /* now start putting them together */
4968 forTree = newNode (NULLOP, initExpr, condExpr);
4969 forTree = newNode (NULLOP, forTree, forBody);
4970 forTree = newNode (NULLOP, forTree, loopExpr);
4971 /* finally add the break label */
4972 forTree = newNode (NULLOP, forTree,
4973 createLabel (falseLabel, NULL));
4977 /*-----------------------------------------------------------------*/
4978 /* createWhile - creates parse tree for while statement */
4979 /* the while statement will be created as follows */
4981 /* _while_continue_n: */
4982 /* condition_expression +-> trueLabel -> _while_boby_n */
4984 /* +-> falseLabel -> _while_break_n */
4985 /* _while_body_n: */
4987 /* goto _while_continue_n */
4988 /* _while_break_n: */
4989 /*-----------------------------------------------------------------*/
4991 createWhile (symbol * trueLabel, symbol * continueLabel,
4992 symbol * falseLabel, ast * condExpr, ast * whileBody)
4996 /* put the continue label */
4997 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
4998 condExpr = createLabel (continueLabel, condExpr);
4999 condExpr->lineno = 0;
5001 /* put the body label in front of the body */
5002 whileBody = createLabel (trueLabel, whileBody);
5003 whileBody->lineno = 0;
5004 /* put a jump to continue at the end of the body */
5005 /* and put break label at the end of the body */
5006 whileBody = newNode (NULLOP,
5009 newAst_VALUE (symbolVal (continueLabel)),
5010 createLabel (falseLabel, NULL)));
5012 /* put it all together */
5013 if (IS_IFX (condExpr))
5014 whileTree = condExpr;
5017 whileTree = newNode (IFX, condExpr, NULL);
5018 /* put the true & false labels in place */
5019 whileTree->trueLabel = trueLabel;
5020 whileTree->falseLabel = falseLabel;
5023 return newNode (NULLOP, whileTree, whileBody);
5026 /*-----------------------------------------------------------------*/
5027 /* isShiftRightLitVal _BitAndLitVal - helper function */
5028 /*-----------------------------------------------------------------*/
5030 isShiftRightLitVal_BitAndLitVal (ast * tree)
5032 /* if this is not a bit and */
5033 if (!IS_BITAND (tree))
5036 /* will look for tree of the form
5037 ( expr >> litval2) & litval1 */
5038 if (!IS_AST_LIT_VALUE (tree->right))
5041 if (!IS_RIGHT_OP (tree->left))
5044 if (!IS_AST_LIT_VALUE (tree->left->right))
5047 return tree->left->left;
5050 /*-----------------------------------------------------------------*/
5051 /* isBitAndPowOf2 - helper function */
5052 /*-----------------------------------------------------------------*/
5054 isBitAndPow2 (ast * tree)
5056 /* if this is not a bit and */
5057 if (!IS_BITAND (tree))
5060 /* will look for tree of the form
5061 ( expr & (1 << litval) */
5062 if (!IS_AST_LIT_VALUE (tree->right))
5065 return powof2 ((TYPE_UDWORD)AST_LIT_VALUE (tree->right));
5068 /*-----------------------------------------------------------------*/
5069 /* optimizeGetHbit - get highest order bit of the expression */
5070 /*-----------------------------------------------------------------*/
5072 optimizeGetHbit (ast * tree, RESULT_TYPE resultType)
5077 expr = isShiftRightLitVal_BitAndLitVal(tree);
5080 if ((AST_LIT_VALUE (tree->right) != 1) ||
5081 ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
5082 (j = (getSize (TTYPE (expr)) * 8 - 1))))
5085 if (!expr && (resultType == RESULT_TYPE_BIT))
5088 if (isBitAndPow2 (tree) != (signed)getSize (TTYPE (expr)) * 8 - 1)
5094 /* make sure the port supports GETHBIT */
5095 if (port->hasExtBitOp
5096 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (expr))))
5099 return decorateType (newNode (GETHBIT, expr, NULL), RESULT_TYPE_NONE);
5102 /*-----------------------------------------------------------------*/
5103 /* optimizeGetAbit - get a single bit of the expression */
5104 /*-----------------------------------------------------------------*/
5106 optimizeGetAbit (ast * tree, RESULT_TYPE resultType)
5111 expr = isShiftRightLitVal_BitAndLitVal(tree);
5114 if (AST_LIT_VALUE (tree->right) != 1)
5116 count = tree->left->right;
5118 if (!expr && (resultType == RESULT_TYPE_BIT))
5120 int p2 = isBitAndPow2 (tree);
5124 count = newAst_VALUE (valueFromLit (p2));
5130 /* make sure the port supports GETABIT */
5131 if (port->hasExtBitOp
5132 && !port->hasExtBitOp(GETABIT, getSize (TTYPE (expr))))
5135 return decorateType (newNode (GETABIT, expr, count), RESULT_TYPE_NONE);
5139 /*-----------------------------------------------------------------*/
5140 /* optimizeGetByte - get a byte of the expression */
5141 /*-----------------------------------------------------------------*/
5143 optimizeGetByte (ast * tree, RESULT_TYPE resultType)
5149 expr = isShiftRightLitVal_BitAndLitVal(tree);
5152 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5153 count = tree->left->right;
5154 if (AST_LIT_VALUE (tree->right) != 0xFF)
5157 if (!expr && resultType == RESULT_TYPE_CHAR)
5159 /* if this is a right shift over a multiple of 8 */
5160 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5162 i = (unsigned int) AST_LIT_VALUE (tree->right);
5163 count = tree->right;
5167 if (!expr || (i == 0) || (i % 8) || (i >= getSize (TTYPE (expr)) * 8))
5170 /* make sure the port supports GETBYTE */
5171 if (port->hasExtBitOp
5172 && !port->hasExtBitOp(GETBYTE, getSize (TTYPE (expr))))
5175 return decorateType (newNode (GETBYTE, expr, count), RESULT_TYPE_NONE);
5178 /*-----------------------------------------------------------------*/
5179 /* optimizeGetWord - get two bytes of the expression */
5180 /*-----------------------------------------------------------------*/
5182 optimizeGetWord (ast * tree, RESULT_TYPE resultType)
5188 expr = isShiftRightLitVal_BitAndLitVal(tree);
5191 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5192 count = tree->left->right;
5193 if (AST_LIT_VALUE (tree->right) != 0xFFFF)
5196 if (!expr && resultType == RESULT_TYPE_INT)
5198 /* if this is a right shift over a multiple of 8 */
5199 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5201 i = (unsigned int) AST_LIT_VALUE (tree->right);
5202 count = tree->right;
5206 if (!expr || (i == 0) || (i % 8) || (i >= (getSize (TTYPE (expr))-1) * 8))
5209 /* make sure the port supports GETWORD */
5210 if (port->hasExtBitOp
5211 && !port->hasExtBitOp(GETWORD, getSize (TTYPE (expr))))
5214 return decorateType (newNode (GETWORD, expr, count), RESULT_TYPE_NONE);
5217 /*-----------------------------------------------------------------*/
5218 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
5219 /*-----------------------------------------------------------------*/
5221 optimizeRRCRLC (ast * root)
5223 /* will look for trees of the form
5224 (?expr << 1) | (?expr >> 7) or
5225 (?expr >> 7) | (?expr << 1) will make that
5226 into a RLC : operation ..
5228 (?expr >> 1) | (?expr << 7) or
5229 (?expr << 7) | (?expr >> 1) will make that
5230 into a RRC operation
5231 note : by 7 I mean (number of bits required to hold the
5233 /* if the root operation is not a | operation then not */
5234 if (!IS_BITOR (root))
5237 /* I have to think of a better way to match patterns this sucks */
5238 /* that aside let's start looking for the first case : I use a
5239 negative check a lot to improve the efficiency */
5240 /* (?expr << 1) | (?expr >> 7) */
5241 if (IS_LEFT_OP (root->left) &&
5242 IS_RIGHT_OP (root->right))
5245 if (!SPEC_USIGN (TETYPE (root->left->left)))
5248 if (!IS_AST_LIT_VALUE (root->left->right) ||
5249 !IS_AST_LIT_VALUE (root->right->right))
5252 /* make sure it is the same expression */
5253 if (!isAstEqual (root->left->left,
5257 if (AST_LIT_VALUE (root->left->right) != 1)
5260 if (AST_LIT_VALUE (root->right->right) !=
5261 (getSize (TTYPE (root->left->left)) * 8 - 1))
5264 /* make sure the port supports RLC */
5265 if (port->hasExtBitOp
5266 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5269 /* whew got the first case : create the AST */
5270 return newNode (RLC, root->left->left, NULL);
5274 /* check for second case */
5275 /* (?expr >> 7) | (?expr << 1) */
5276 if (IS_LEFT_OP (root->right) &&
5277 IS_RIGHT_OP (root->left))
5280 if (!SPEC_USIGN (TETYPE (root->left->left)))
5283 if (!IS_AST_LIT_VALUE (root->left->right) ||
5284 !IS_AST_LIT_VALUE (root->right->right))
5287 /* make sure it is the same symbol */
5288 if (!isAstEqual (root->left->left,
5292 if (AST_LIT_VALUE (root->right->right) != 1)
5295 if (AST_LIT_VALUE (root->left->right) !=
5296 (getSize (TTYPE (root->left->left)) * 8 - 1))
5299 /* make sure the port supports RLC */
5300 if (port->hasExtBitOp
5301 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5304 /* whew got the first case : create the AST */
5305 return newNode (RLC, root->left->left, NULL);
5310 /* third case for RRC */
5311 /* (?symbol >> 1) | (?symbol << 7) */
5312 if (IS_LEFT_OP (root->right) &&
5313 IS_RIGHT_OP (root->left))
5316 if (!SPEC_USIGN (TETYPE (root->left->left)))
5319 if (!IS_AST_LIT_VALUE (root->left->right) ||
5320 !IS_AST_LIT_VALUE (root->right->right))
5323 /* make sure it is the same symbol */
5324 if (!isAstEqual (root->left->left,
5328 if (AST_LIT_VALUE (root->left->right) != 1)
5331 if (AST_LIT_VALUE (root->right->right) !=
5332 (getSize (TTYPE (root->left->left)) * 8 - 1))
5335 /* make sure the port supports RRC */
5336 if (port->hasExtBitOp
5337 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5340 /* whew got the first case : create the AST */
5341 return newNode (RRC, root->left->left, NULL);
5345 /* fourth and last case for now */
5346 /* (?symbol << 7) | (?symbol >> 1) */
5347 if (IS_RIGHT_OP (root->right) &&
5348 IS_LEFT_OP (root->left))
5351 if (!SPEC_USIGN (TETYPE (root->left->left)))
5354 if (!IS_AST_LIT_VALUE (root->left->right) ||
5355 !IS_AST_LIT_VALUE (root->right->right))
5358 /* make sure it is the same symbol */
5359 if (!isAstEqual (root->left->left,
5363 if (AST_LIT_VALUE (root->right->right) != 1)
5366 if (AST_LIT_VALUE (root->left->right) !=
5367 (getSize (TTYPE (root->left->left)) * 8 - 1))
5370 /* make sure the port supports RRC */
5371 if (port->hasExtBitOp
5372 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5375 /* whew got the first case : create the AST */
5376 return newNode (RRC, root->left->left, NULL);
5380 /* not found return root */
5384 /*-----------------------------------------------------------------*/
5385 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
5386 /*-----------------------------------------------------------------*/
5388 optimizeSWAP (ast * root)
5390 /* will look for trees of the form
5391 (?expr << 4) | (?expr >> 4) or
5392 (?expr >> 4) | (?expr << 4) will make that
5393 into a SWAP : operation ..
5394 note : by 4 I mean (number of bits required to hold the
5396 /* if the root operation is not a | operation then not */
5397 if (!IS_BITOR (root))
5400 /* (?expr << 4) | (?expr >> 4) */
5401 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
5402 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
5405 if (!SPEC_USIGN (TETYPE (root->left->left)))
5408 if (!IS_AST_LIT_VALUE (root->left->right) ||
5409 !IS_AST_LIT_VALUE (root->right->right))
5412 /* make sure it is the same expression */
5413 if (!isAstEqual (root->left->left,
5417 if (AST_LIT_VALUE (root->left->right) !=
5418 (getSize (TTYPE (root->left->left)) * 4))
5421 if (AST_LIT_VALUE (root->right->right) !=
5422 (getSize (TTYPE (root->left->left)) * 4))
5425 /* make sure the port supports SWAP */
5426 if (port->hasExtBitOp
5427 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
5430 /* found it : create the AST */
5431 return newNode (SWAP, root->left->left, NULL);
5435 /* not found return root */
5439 /*-----------------------------------------------------------------*/
5440 /* optimizeCompare - optimizes compares for bit variables */
5441 /*-----------------------------------------------------------------*/
5443 optimizeCompare (ast * root)
5445 ast *optExpr = NULL;
5448 unsigned int litValue;
5450 /* if nothing then return nothing */
5454 /* if not a compare op then do leaves */
5455 if (!IS_COMPARE_OP (root))
5457 root->left = optimizeCompare (root->left);
5458 root->right = optimizeCompare (root->right);
5462 /* if left & right are the same then depending
5463 of the operation do */
5464 if (isAstEqual (root->left, root->right))
5466 switch (root->opval.op)
5471 optExpr = newAst_VALUE (constVal ("0"));
5476 optExpr = newAst_VALUE (constVal ("1"));
5480 return decorateType (optExpr, RESULT_TYPE_NONE);
5483 vleft = (root->left->type == EX_VALUE ?
5484 root->left->opval.val : NULL);
5486 vright = (root->right->type == EX_VALUE ?
5487 root->right->opval.val : NULL);
5489 /* if left is a BITVAR in BITSPACE */
5490 /* and right is a LITERAL then opt- */
5491 /* imize else do nothing */
5492 if (vleft && vright &&
5493 IS_BITVAR (vleft->etype) &&
5494 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5495 IS_LITERAL (vright->etype))
5498 /* if right side > 1 then comparison may never succeed */
5499 if ((litValue = (int) floatFromVal (vright)) > 1)
5501 werror (W_BAD_COMPARE);
5507 switch (root->opval.op)
5509 case '>': /* bit value greater than 1 cannot be */
5510 werror (W_BAD_COMPARE);
5514 case '<': /* bit value < 1 means 0 */
5516 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5519 case LE_OP: /* bit value <= 1 means no check */
5520 optExpr = newAst_VALUE (vright);
5523 case GE_OP: /* bit value >= 1 means only check for = */
5525 optExpr = newAst_VALUE (vleft);
5530 { /* literal is zero */
5531 switch (root->opval.op)
5533 case '<': /* bit value < 0 cannot be */
5534 werror (W_BAD_COMPARE);
5538 case '>': /* bit value > 0 means 1 */
5540 optExpr = newAst_VALUE (vleft);
5543 case LE_OP: /* bit value <= 0 means no check */
5544 case GE_OP: /* bit value >= 0 means no check */
5545 werror (W_BAD_COMPARE);
5549 case EQ_OP: /* bit == 0 means ! of bit */
5550 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5554 return decorateType (resolveSymbols (optExpr), RESULT_TYPE_NONE);
5555 } /* end-of-if of BITVAR */
5560 /*-----------------------------------------------------------------*/
5561 /* addSymToBlock : adds the symbol to the first block we find */
5562 /*-----------------------------------------------------------------*/
5564 addSymToBlock (symbol * sym, ast * tree)
5566 /* reached end of tree or a leaf */
5567 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5571 if (IS_AST_OP (tree) &&
5572 tree->opval.op == BLOCK)
5575 symbol *lsym = copySymbol (sym);
5577 lsym->next = AST_VALUES (tree, sym);
5578 AST_VALUES (tree, sym) = lsym;
5582 addSymToBlock (sym, tree->left);
5583 addSymToBlock (sym, tree->right);
5586 /*-----------------------------------------------------------------*/
5587 /* processRegParms - do processing for register parameters */
5588 /*-----------------------------------------------------------------*/
5590 processRegParms (value * args, ast * body)
5594 if (IS_REGPARM (args->etype))
5595 addSymToBlock (args->sym, body);
5600 /*-----------------------------------------------------------------*/
5601 /* resetParmKey - resets the operandkeys for the symbols */
5602 /*-----------------------------------------------------------------*/
5603 DEFSETFUNC (resetParmKey)
5614 /*-----------------------------------------------------------------*/
5615 /* createFunction - This is the key node that calls the iCode for */
5616 /* generating the code for a function. Note code */
5617 /* is generated function by function, later when */
5618 /* add inter-procedural analysis this will change */
5619 /*-----------------------------------------------------------------*/
5621 createFunction (symbol * name, ast * body)
5627 iCode *piCode = NULL;
5629 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5630 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5632 /* if check function return 0 then some problem */
5633 if (checkFunction (name, NULL) == 0)
5636 /* create a dummy block if none exists */
5638 body = newNode (BLOCK, NULL, NULL);
5642 /* check if the function name already in the symbol table */
5643 if ((csym = findSym (SymbolTab, NULL, name->name)))
5646 /* special case for compiler defined functions
5647 we need to add the name to the publics list : this
5648 actually means we are now compiling the compiler
5652 addSet (&publics, name);
5657 addSymChain (&name);
5658 allocVariables (name);
5660 name->lastLine = mylineno;
5663 /* set the stack pointer */
5664 stackPtr = -port->stack.direction * port->stack.call_overhead;
5667 if (IFFUNC_ISISR (name->type))
5668 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5670 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5672 if (options.useXstack)
5673 xstackPtr -= port->stack.direction * port->stack.reent_overhead;
5675 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5678 fetype = getSpec (name->type); /* get the specifier for the function */
5679 /* if this is a reentrant function then */
5680 if (IFFUNC_ISREENT (name->type))
5683 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5685 /* do processing for parameters that are passed in registers */
5686 processRegParms (FUNC_ARGS(name->type), body);
5688 /* set the stack pointer */
5692 /* allocate & autoinit the block variables */
5693 processBlockVars (body, &stack, ALLOCATE);
5695 /* save the stack information */
5696 if (options.useXstack)
5697 name->xstack = SPEC_STAK (fetype) = stack;
5699 name->stack = SPEC_STAK (fetype) = stack;
5701 /* name needs to be mangled */
5702 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5704 body = resolveSymbols (body); /* resolve the symbols */
5705 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5707 ex = newAst_VALUE (symbolVal (name)); /* create name */
5708 ex = newNode (FUNCTION, ex, body);
5709 ex->values.args = FUNC_ARGS(name->type);
5711 if (options.dump_tree) PA(ex);
5714 werror (E_FUNC_NO_CODE, name->name);
5718 /* create the node & generate intermediate code */
5720 codeOutFile = code->oFile;
5721 piCode = iCodeFromAst (ex);
5725 werror (E_FUNC_NO_CODE, name->name);
5729 eBBlockFromiCode (piCode);
5731 /* if there are any statics then do them */
5734 GcurMemmap = statsg;
5735 codeOutFile = statsg->oFile;
5736 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_TYPE_NONE)));
5742 /* dealloc the block variables */
5743 processBlockVars (body, &stack, DEALLOCATE);
5744 outputDebugStackSymbols();
5745 /* deallocate paramaters */
5746 deallocParms (FUNC_ARGS(name->type));
5748 if (IFFUNC_ISREENT (name->type))
5751 /* we are done freeup memory & cleanup */
5753 if (port->reset_labelKey) labelKey = 1;
5755 FUNC_HASBODY(name->type) = 1;
5756 addSet (&operKeyReset, name);
5757 applyToSet (operKeyReset, resetParmKey);
5762 cleanUpLevel (LabelTab, 0);
5763 cleanUpBlock (StructTab, 1);
5764 cleanUpBlock (TypedefTab, 1);
5766 xstack->syms = NULL;
5767 istack->syms = NULL;
5772 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
5773 /*-----------------------------------------------------------------*/
5774 /* ast_print : prints the ast (for debugging purposes) */
5775 /*-----------------------------------------------------------------*/
5777 void ast_print (ast * tree, FILE *outfile, int indent)
5782 /* can print only decorated trees */
5783 if (!tree->decorated) return;
5785 /* if any child is an error | this one is an error do nothing */
5786 if (tree->isError ||
5787 (tree->left && tree->left->isError) ||
5788 (tree->right && tree->right->isError)) {
5789 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
5793 /* print the line */
5794 /* if not block & function */
5795 if (tree->type == EX_OP &&
5796 (tree->opval.op != FUNCTION &&
5797 tree->opval.op != BLOCK &&
5798 tree->opval.op != NULLOP)) {
5801 if (tree->opval.op == FUNCTION) {
5803 value *args=FUNC_ARGS(tree->left->opval.val->type);
5804 fprintf(outfile,"FUNCTION (%s=%p) type (",
5805 tree->left->opval.val->name, tree);
5806 printTypeChain (tree->left->opval.val->type->next,outfile);
5807 fprintf(outfile,") args (");
5810 fprintf (outfile, ", ");
5812 printTypeChain (args ? args->type : NULL, outfile);
5814 args= args ? args->next : NULL;
5816 fprintf(outfile,")\n");
5817 ast_print(tree->left,outfile,indent);
5818 ast_print(tree->right,outfile,indent);
5821 if (tree->opval.op == BLOCK) {
5822 symbol *decls = tree->values.sym;
5823 INDENT(indent,outfile);
5824 fprintf(outfile,"{\n");
5826 INDENT(indent+2,outfile);
5827 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
5828 decls->name, decls);
5829 printTypeChain(decls->type,outfile);
5830 fprintf(outfile,")\n");
5832 decls = decls->next;
5834 ast_print(tree->right,outfile,indent+2);
5835 INDENT(indent,outfile);
5836 fprintf(outfile,"}\n");
5839 if (tree->opval.op == NULLOP) {
5840 ast_print(tree->left,outfile,indent);
5841 ast_print(tree->right,outfile,indent);
5844 INDENT(indent,outfile);
5846 /*------------------------------------------------------------------*/
5847 /*----------------------------*/
5848 /* leaf has been reached */
5849 /*----------------------------*/
5850 /* if this is of type value */
5851 /* just get the type */
5852 if (tree->type == EX_VALUE) {
5854 if (IS_LITERAL (tree->opval.val->etype)) {
5855 fprintf(outfile,"CONSTANT (%p) value = ", tree);
5856 if (SPEC_USIGN (tree->opval.val->etype))
5857 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
5859 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
5860 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
5861 floatFromVal(tree->opval.val));
5862 } else if (tree->opval.val->sym) {
5863 /* if the undefined flag is set then give error message */
5864 if (tree->opval.val->sym->undefined) {
5865 fprintf(outfile,"UNDEFINED SYMBOL ");
5867 fprintf(outfile,"SYMBOL ");
5869 fprintf(outfile,"(%s=%p)",
5870 tree->opval.val->sym->name,tree);
5873 fprintf(outfile," type (");
5874 printTypeChain(tree->ftype,outfile);
5875 fprintf(outfile,")\n");
5877 fprintf(outfile,"\n");
5882 /* if type link for the case of cast */
5883 if (tree->type == EX_LINK) {
5884 fprintf(outfile,"TYPENODE (%p) type = (",tree);
5885 printTypeChain(tree->opval.lnk,outfile);
5886 fprintf(outfile,")\n");
5891 /* depending on type of operator do */
5893 switch (tree->opval.op) {
5894 /*------------------------------------------------------------------*/
5895 /*----------------------------*/
5897 /*----------------------------*/
5899 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
5900 printTypeChain(tree->ftype,outfile);
5901 fprintf(outfile,")\n");
5902 ast_print(tree->left,outfile,indent+2);
5903 ast_print(tree->right,outfile,indent+2);
5906 /*------------------------------------------------------------------*/
5907 /*----------------------------*/
5909 /*----------------------------*/
5911 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
5912 printTypeChain(tree->ftype,outfile);
5913 fprintf(outfile,")\n");
5914 ast_print(tree->left,outfile,indent+2);
5915 ast_print(tree->right,outfile,indent+2);
5918 /*------------------------------------------------------------------*/
5919 /*----------------------------*/
5920 /* struct/union pointer */
5921 /*----------------------------*/
5923 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
5924 printTypeChain(tree->ftype,outfile);
5925 fprintf(outfile,")\n");
5926 ast_print(tree->left,outfile,indent+2);
5927 ast_print(tree->right,outfile,indent+2);
5930 /*------------------------------------------------------------------*/
5931 /*----------------------------*/
5932 /* ++/-- operation */
5933 /*----------------------------*/
5936 fprintf(outfile,"post-");
5938 fprintf(outfile,"pre-");
5939 fprintf(outfile,"INC_OP (%p) type (",tree);
5940 printTypeChain(tree->ftype,outfile);
5941 fprintf(outfile,")\n");
5942 ast_print(tree->left,outfile,indent+2); /* postincrement case */
5943 ast_print(tree->right,outfile,indent+2); /* preincrement case */
5948 fprintf(outfile,"post-");
5950 fprintf(outfile,"pre-");
5951 fprintf(outfile,"DEC_OP (%p) type (",tree);
5952 printTypeChain(tree->ftype,outfile);
5953 fprintf(outfile,")\n");
5954 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
5955 ast_print(tree->right,outfile,indent+2); /* predecrement case */
5958 /*------------------------------------------------------------------*/
5959 /*----------------------------*/
5961 /*----------------------------*/
5964 fprintf(outfile,"& (%p) type (",tree);
5965 printTypeChain(tree->ftype,outfile);
5966 fprintf(outfile,")\n");
5967 ast_print(tree->left,outfile,indent+2);
5968 ast_print(tree->right,outfile,indent+2);
5970 fprintf(outfile,"ADDRESS_OF (%p) type (",tree);
5971 printTypeChain(tree->ftype,outfile);
5972 fprintf(outfile,")\n");
5973 ast_print(tree->left,outfile,indent+2);
5974 ast_print(tree->right,outfile,indent+2);
5977 /*----------------------------*/
5979 /*----------------------------*/
5981 fprintf(outfile,"OR (%p) type (",tree);
5982 printTypeChain(tree->ftype,outfile);
5983 fprintf(outfile,")\n");
5984 ast_print(tree->left,outfile,indent+2);
5985 ast_print(tree->right,outfile,indent+2);
5987 /*------------------------------------------------------------------*/
5988 /*----------------------------*/
5990 /*----------------------------*/
5992 fprintf(outfile,"XOR (%p) type (",tree);
5993 printTypeChain(tree->ftype,outfile);
5994 fprintf(outfile,")\n");
5995 ast_print(tree->left,outfile,indent+2);
5996 ast_print(tree->right,outfile,indent+2);
5999 /*------------------------------------------------------------------*/
6000 /*----------------------------*/
6002 /*----------------------------*/
6004 fprintf(outfile,"DIV (%p) type (",tree);
6005 printTypeChain(tree->ftype,outfile);
6006 fprintf(outfile,")\n");
6007 ast_print(tree->left,outfile,indent+2);
6008 ast_print(tree->right,outfile,indent+2);
6010 /*------------------------------------------------------------------*/
6011 /*----------------------------*/
6013 /*----------------------------*/
6015 fprintf(outfile,"MOD (%p) type (",tree);
6016 printTypeChain(tree->ftype,outfile);
6017 fprintf(outfile,")\n");
6018 ast_print(tree->left,outfile,indent+2);
6019 ast_print(tree->right,outfile,indent+2);
6022 /*------------------------------------------------------------------*/
6023 /*----------------------------*/
6024 /* address dereference */
6025 /*----------------------------*/
6026 case '*': /* can be unary : if right is null then unary operation */
6028 fprintf(outfile,"DEREF (%p) type (",tree);
6029 printTypeChain(tree->ftype,outfile);
6030 fprintf(outfile,")\n");
6031 ast_print(tree->left,outfile,indent+2);
6034 /*------------------------------------------------------------------*/
6035 /*----------------------------*/
6036 /* multiplication */
6037 /*----------------------------*/
6038 fprintf(outfile,"MULT (%p) type (",tree);
6039 printTypeChain(tree->ftype,outfile);
6040 fprintf(outfile,")\n");
6041 ast_print(tree->left,outfile,indent+2);
6042 ast_print(tree->right,outfile,indent+2);
6046 /*------------------------------------------------------------------*/
6047 /*----------------------------*/
6048 /* unary '+' operator */
6049 /*----------------------------*/
6053 fprintf(outfile,"UPLUS (%p) type (",tree);
6054 printTypeChain(tree->ftype,outfile);
6055 fprintf(outfile,")\n");
6056 ast_print(tree->left,outfile,indent+2);
6058 /*------------------------------------------------------------------*/
6059 /*----------------------------*/
6061 /*----------------------------*/
6062 fprintf(outfile,"ADD (%p) type (",tree);
6063 printTypeChain(tree->ftype,outfile);
6064 fprintf(outfile,")\n");
6065 ast_print(tree->left,outfile,indent+2);
6066 ast_print(tree->right,outfile,indent+2);
6069 /*------------------------------------------------------------------*/
6070 /*----------------------------*/
6072 /*----------------------------*/
6073 case '-': /* can be unary */
6075 fprintf(outfile,"UMINUS (%p) type (",tree);
6076 printTypeChain(tree->ftype,outfile);
6077 fprintf(outfile,")\n");
6078 ast_print(tree->left,outfile,indent+2);
6080 /*------------------------------------------------------------------*/
6081 /*----------------------------*/
6083 /*----------------------------*/
6084 fprintf(outfile,"SUB (%p) type (",tree);
6085 printTypeChain(tree->ftype,outfile);
6086 fprintf(outfile,")\n");
6087 ast_print(tree->left,outfile,indent+2);
6088 ast_print(tree->right,outfile,indent+2);
6091 /*------------------------------------------------------------------*/
6092 /*----------------------------*/
6094 /*----------------------------*/
6096 fprintf(outfile,"COMPL (%p) type (",tree);
6097 printTypeChain(tree->ftype,outfile);
6098 fprintf(outfile,")\n");
6099 ast_print(tree->left,outfile,indent+2);
6101 /*------------------------------------------------------------------*/
6102 /*----------------------------*/
6104 /*----------------------------*/
6106 fprintf(outfile,"NOT (%p) type (",tree);
6107 printTypeChain(tree->ftype,outfile);
6108 fprintf(outfile,")\n");
6109 ast_print(tree->left,outfile,indent+2);
6111 /*------------------------------------------------------------------*/
6112 /*----------------------------*/
6114 /*----------------------------*/
6116 fprintf(outfile,"RRC (%p) type (",tree);
6117 printTypeChain(tree->ftype,outfile);
6118 fprintf(outfile,")\n");
6119 ast_print(tree->left,outfile,indent+2);
6123 fprintf(outfile,"RLC (%p) type (",tree);
6124 printTypeChain(tree->ftype,outfile);
6125 fprintf(outfile,")\n");
6126 ast_print(tree->left,outfile,indent+2);
6129 fprintf(outfile,"SWAP (%p) type (",tree);
6130 printTypeChain(tree->ftype,outfile);
6131 fprintf(outfile,")\n");
6132 ast_print(tree->left,outfile,indent+2);
6135 fprintf(outfile,"GETHBIT (%p) type (",tree);
6136 printTypeChain(tree->ftype,outfile);
6137 fprintf(outfile,")\n");
6138 ast_print(tree->left,outfile,indent+2);
6141 fprintf(outfile,"GETABIT (%p) type (",tree);
6142 printTypeChain(tree->ftype,outfile);
6143 fprintf(outfile,")\n");
6144 ast_print(tree->left,outfile,indent+2);
6145 ast_print(tree->right,outfile,indent+2);
6148 fprintf(outfile,"GETBYTE (%p) type (",tree);
6149 printTypeChain(tree->ftype,outfile);
6150 fprintf(outfile,")\n");
6151 ast_print(tree->left,outfile,indent+2);
6152 ast_print(tree->right,outfile,indent+2);
6155 fprintf(outfile,"GETWORD (%p) type (",tree);
6156 printTypeChain(tree->ftype,outfile);
6157 fprintf(outfile,")\n");
6158 ast_print(tree->left,outfile,indent+2);
6159 ast_print(tree->right,outfile,indent+2);
6162 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
6163 printTypeChain(tree->ftype,outfile);
6164 fprintf(outfile,")\n");
6165 ast_print(tree->left,outfile,indent+2);
6166 ast_print(tree->right,outfile,indent+2);
6169 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
6170 printTypeChain(tree->ftype,outfile);
6171 fprintf(outfile,")\n");
6172 ast_print(tree->left,outfile,indent+2);
6173 ast_print(tree->right,outfile,indent+2);
6175 /*------------------------------------------------------------------*/
6176 /*----------------------------*/
6178 /*----------------------------*/
6179 case CAST: /* change the type */
6180 fprintf(outfile,"CAST (%p) from type (",tree);
6181 printTypeChain(tree->right->ftype,outfile);
6182 fprintf(outfile,") to type (");
6183 printTypeChain(tree->ftype,outfile);
6184 fprintf(outfile,")\n");
6185 ast_print(tree->right,outfile,indent+2);
6189 fprintf(outfile,"ANDAND (%p) type (",tree);
6190 printTypeChain(tree->ftype,outfile);
6191 fprintf(outfile,")\n");
6192 ast_print(tree->left,outfile,indent+2);
6193 ast_print(tree->right,outfile,indent+2);
6196 fprintf(outfile,"OROR (%p) type (",tree);
6197 printTypeChain(tree->ftype,outfile);
6198 fprintf(outfile,")\n");
6199 ast_print(tree->left,outfile,indent+2);
6200 ast_print(tree->right,outfile,indent+2);
6203 /*------------------------------------------------------------------*/
6204 /*----------------------------*/
6205 /* comparison operators */
6206 /*----------------------------*/
6208 fprintf(outfile,"GT(>) (%p) type (",tree);
6209 printTypeChain(tree->ftype,outfile);
6210 fprintf(outfile,")\n");
6211 ast_print(tree->left,outfile,indent+2);
6212 ast_print(tree->right,outfile,indent+2);
6215 fprintf(outfile,"LT(<) (%p) type (",tree);
6216 printTypeChain(tree->ftype,outfile);
6217 fprintf(outfile,")\n");
6218 ast_print(tree->left,outfile,indent+2);
6219 ast_print(tree->right,outfile,indent+2);
6222 fprintf(outfile,"LE(<=) (%p) type (",tree);
6223 printTypeChain(tree->ftype,outfile);
6224 fprintf(outfile,")\n");
6225 ast_print(tree->left,outfile,indent+2);
6226 ast_print(tree->right,outfile,indent+2);
6229 fprintf(outfile,"GE(>=) (%p) type (",tree);
6230 printTypeChain(tree->ftype,outfile);
6231 fprintf(outfile,")\n");
6232 ast_print(tree->left,outfile,indent+2);
6233 ast_print(tree->right,outfile,indent+2);
6236 fprintf(outfile,"EQ(==) (%p) type (",tree);
6237 printTypeChain(tree->ftype,outfile);
6238 fprintf(outfile,")\n");
6239 ast_print(tree->left,outfile,indent+2);
6240 ast_print(tree->right,outfile,indent+2);
6243 fprintf(outfile,"NE(!=) (%p) type (",tree);
6244 printTypeChain(tree->ftype,outfile);
6245 fprintf(outfile,")\n");
6246 ast_print(tree->left,outfile,indent+2);
6247 ast_print(tree->right,outfile,indent+2);
6248 /*------------------------------------------------------------------*/
6249 /*----------------------------*/
6251 /*----------------------------*/
6252 case SIZEOF: /* evaluate wihout code generation */
6253 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
6256 /*------------------------------------------------------------------*/
6257 /*----------------------------*/
6258 /* conditional operator '?' */
6259 /*----------------------------*/
6261 fprintf(outfile,"QUEST(?) (%p) type (",tree);
6262 printTypeChain(tree->ftype,outfile);
6263 fprintf(outfile,")\n");
6264 ast_print(tree->left,outfile,indent+2);
6265 ast_print(tree->right,outfile,indent+2);
6269 fprintf(outfile,"COLON(:) (%p) type (",tree);
6270 printTypeChain(tree->ftype,outfile);
6271 fprintf(outfile,")\n");
6272 ast_print(tree->left,outfile,indent+2);
6273 ast_print(tree->right,outfile,indent+2);
6276 /*------------------------------------------------------------------*/
6277 /*----------------------------*/
6278 /* assignment operators */
6279 /*----------------------------*/
6281 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
6282 printTypeChain(tree->ftype,outfile);
6283 fprintf(outfile,")\n");
6284 ast_print(tree->left,outfile,indent+2);
6285 ast_print(tree->right,outfile,indent+2);
6288 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
6289 printTypeChain(tree->ftype,outfile);
6290 fprintf(outfile,")\n");
6291 ast_print(tree->left,outfile,indent+2);
6292 ast_print(tree->right,outfile,indent+2);
6295 fprintf(outfile,"ANDASS(&=) (%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);
6302 fprintf(outfile,"ORASS(|=) (%p) type (",tree);
6303 printTypeChain(tree->ftype,outfile);
6304 fprintf(outfile,")\n");
6305 ast_print(tree->left,outfile,indent+2);
6306 ast_print(tree->right,outfile,indent+2);
6309 fprintf(outfile,"XORASS(^=) (%p) type (",tree);
6310 printTypeChain(tree->ftype,outfile);
6311 fprintf(outfile,")\n");
6312 ast_print(tree->left,outfile,indent+2);
6313 ast_print(tree->right,outfile,indent+2);
6316 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
6317 printTypeChain(tree->ftype,outfile);
6318 fprintf(outfile,")\n");
6319 ast_print(tree->left,outfile,indent+2);
6320 ast_print(tree->right,outfile,indent+2);
6323 fprintf(outfile,"LSHFTASS(<<=) (%p) type (",tree);
6324 printTypeChain(tree->ftype,outfile);
6325 fprintf(outfile,")\n");
6326 ast_print(tree->left,outfile,indent+2);
6327 ast_print(tree->right,outfile,indent+2);
6329 /*------------------------------------------------------------------*/
6330 /*----------------------------*/
6332 /*----------------------------*/
6334 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
6335 printTypeChain(tree->ftype,outfile);
6336 fprintf(outfile,")\n");
6337 ast_print(tree->left,outfile,indent+2);
6338 ast_print(tree->right,outfile,indent+2);
6340 /*------------------------------------------------------------------*/
6341 /*----------------------------*/
6343 /*----------------------------*/
6345 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
6346 printTypeChain(tree->ftype,outfile);
6347 fprintf(outfile,")\n");
6348 ast_print(tree->left,outfile,indent+2);
6349 ast_print(tree->right,outfile,indent+2);
6351 /*------------------------------------------------------------------*/
6352 /*----------------------------*/
6353 /* straight assignemnt */
6354 /*----------------------------*/
6356 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
6357 printTypeChain(tree->ftype,outfile);
6358 fprintf(outfile,")\n");
6359 ast_print(tree->left,outfile,indent+2);
6360 ast_print(tree->right,outfile,indent+2);
6362 /*------------------------------------------------------------------*/
6363 /*----------------------------*/
6364 /* comma operator */
6365 /*----------------------------*/
6367 fprintf(outfile,"COMMA(,) (%p) type (",tree);
6368 printTypeChain(tree->ftype,outfile);
6369 fprintf(outfile,")\n");
6370 ast_print(tree->left,outfile,indent+2);
6371 ast_print(tree->right,outfile,indent+2);
6373 /*------------------------------------------------------------------*/
6374 /*----------------------------*/
6376 /*----------------------------*/
6379 fprintf(outfile,"CALL (%p) type (",tree);
6380 printTypeChain(tree->ftype,outfile);
6381 fprintf(outfile,")\n");
6382 ast_print(tree->left,outfile,indent+2);
6383 ast_print(tree->right,outfile,indent+2);
6386 fprintf(outfile,"PARMS\n");
6387 ast_print(tree->left,outfile,indent+2);
6388 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
6389 ast_print(tree->right,outfile,indent+2);
6392 /*------------------------------------------------------------------*/
6393 /*----------------------------*/
6394 /* return statement */
6395 /*----------------------------*/
6397 fprintf(outfile,"RETURN (%p) type (",tree);
6399 printTypeChain(tree->right->ftype,outfile);
6401 fprintf(outfile,")\n");
6402 ast_print(tree->right,outfile,indent+2);
6404 /*------------------------------------------------------------------*/
6405 /*----------------------------*/
6406 /* label statement */
6407 /*----------------------------*/
6409 fprintf(outfile,"LABEL (%p)\n",tree);
6410 ast_print(tree->left,outfile,indent+2);
6411 ast_print(tree->right,outfile,indent);
6413 /*------------------------------------------------------------------*/
6414 /*----------------------------*/
6415 /* switch statement */
6416 /*----------------------------*/
6420 fprintf(outfile,"SWITCH (%p) ",tree);
6421 ast_print(tree->left,outfile,0);
6422 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
6423 INDENT(indent+2,outfile);
6424 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
6425 (int) floatFromVal(val),
6426 tree->values.switchVals.swNum,
6427 (int) floatFromVal(val));
6429 ast_print(tree->right,outfile,indent);
6432 /*------------------------------------------------------------------*/
6433 /*----------------------------*/
6435 /*----------------------------*/
6437 fprintf(outfile,"IF (%p) \n",tree);
6438 ast_print(tree->left,outfile,indent+2);
6439 if (tree->trueLabel) {
6440 INDENT(indent+2,outfile);
6441 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
6443 if (tree->falseLabel) {
6444 INDENT(indent+2,outfile);
6445 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
6447 ast_print(tree->right,outfile,indent+2);
6449 /*----------------------------*/
6450 /* goto Statement */
6451 /*----------------------------*/
6453 fprintf(outfile,"GOTO (%p) \n",tree);
6454 ast_print(tree->left,outfile,indent+2);
6455 fprintf(outfile,"\n");
6457 /*------------------------------------------------------------------*/
6458 /*----------------------------*/
6460 /*----------------------------*/
6462 fprintf(outfile,"FOR (%p) \n",tree);
6463 if (AST_FOR( tree, initExpr)) {
6464 INDENT(indent+2,outfile);
6465 fprintf(outfile,"INIT EXPR ");
6466 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6468 if (AST_FOR( tree, condExpr)) {
6469 INDENT(indent+2,outfile);
6470 fprintf(outfile,"COND EXPR ");
6471 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6473 if (AST_FOR( tree, loopExpr)) {
6474 INDENT(indent+2,outfile);
6475 fprintf(outfile,"LOOP EXPR ");
6476 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6478 fprintf(outfile,"FOR LOOP BODY \n");
6479 ast_print(tree->left,outfile,indent+2);
6482 fprintf(outfile,"CRITICAL (%p) \n",tree);
6483 ast_print(tree->left,outfile,indent+2);
6491 ast_print(t,stdout,0);
6496 /*-----------------------------------------------------------------*/
6497 /* astErrors : returns non-zero if errors present in tree */
6498 /*-----------------------------------------------------------------*/
6499 int astErrors(ast *t)
6508 if (t->type == EX_VALUE
6509 && t->opval.val->sym
6510 && t->opval.val->sym->undefined)
6513 errors += astErrors(t->left);
6514 errors += astErrors(t->right);