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 /* replaceAstWithTemporary: Replace the AST pointed to by the arg */
331 /* with a reference to a new temporary variable. Returns*/
332 /* an AST which assigns the original value to the */
334 /*-----------------------------------------------------------------*/
335 static ast *replaceAstWithTemporary(ast **treeptr)
337 symbol *sym = newSymbol (genSymName(NestLevel), NestLevel );
340 /* Tell gatherImplicitVariables() to automatically give the
341 symbol the correct type */
346 tempvar = newNode('=', newAst_VALUE(symbolVal(sym)), *treeptr);
347 *treeptr = newAst_VALUE(symbolVal(sym));
354 /*-----------------------------------------------------------------*/
355 /* createRMW: Create a read-modify-write expression, using a */
356 /* temporary variable if necessary to avoid duplicating */
357 /* any side effects, for use in e.g. */
358 /* foo()->count += 5; becomes */
359 /* tmp = foo(); tmp->count = tmp->count + 5; */
360 /*-----------------------------------------------------------------*/
361 ast * createRMW (ast *target, unsigned op, ast *operand)
363 ast *readval, *writeval;
364 ast *tempvar1 = NULL;
365 ast *tempvar2 = NULL;
368 if (!target || !operand) {
372 /* we need to create two copies of target: one to read from and
373 one to write to. but we need to do this without duplicating
374 any side effects that may be contained in the tree. */
376 if (IS_AST_OP(target)) {
377 /* if this is a dereference, put the referenced item in the temporary */
378 if (IS_DEREF_OP(target) || target->opval.op == PTR_OP) {
379 /* create a new temporary containing the item being dereferenced */
380 if (hasSEFcalls(target->left))
381 tempvar1 = replaceAstWithTemporary(&(target->left));
382 } else if (target->opval.op == '[') {
383 /* Array access is similar, but we have to avoid side effects in
384 both values [WIML: Why not transform a[b] to *(a+b) in parser?] */
385 if (hasSEFcalls(target->left))
386 tempvar1 = replaceAstWithTemporary(&(target->left));
387 if (hasSEFcalls(target->right))
388 tempvar2 = replaceAstWithTemporary(&(target->right));
390 /* we would have to handle '.', but it is not generated any more */
391 wassertl(target->opval.op != '.', "obsolete opcode in tree");
393 /* no other kinds of ASTs are lvalues and can contain side effects */
398 writeval = copyAst(target);
400 result = newNode('=', writeval, newNode(op, readval, operand));
402 result = newNode(',', tempvar2, result);
404 result = newNode(',', tempvar1, result);
410 /*-----------------------------------------------------------------*/
411 /* hasSEFcalls - returns TRUE if tree has a function call, */
412 /* inc/decrement, or other side effect */
413 /*-----------------------------------------------------------------*/
415 hasSEFcalls (ast * tree)
420 if (tree->type == EX_OP &&
421 (tree->opval.op == CALL ||
422 tree->opval.op == PCALL ||
423 tree->opval.op == '=' ||
424 tree->opval.op == INC_OP ||
425 tree->opval.op == DEC_OP))
428 return (hasSEFcalls (tree->left) |
429 hasSEFcalls (tree->right));
432 /*-----------------------------------------------------------------*/
433 /* isAstEqual - compares two asts & returns 1 if they are equal */
434 /*-----------------------------------------------------------------*/
436 isAstEqual (ast * t1, ast * t2)
445 if (t1->type != t2->type)
451 if (t1->opval.op != t2->opval.op)
453 return (isAstEqual (t1->left, t2->left) &&
454 isAstEqual (t1->right, t2->right));
458 if (t1->opval.val->sym)
460 if (!t2->opval.val->sym)
463 return isSymbolEqual (t1->opval.val->sym,
468 if (t2->opval.val->sym)
471 return (floatFromVal (t1->opval.val) ==
472 floatFromVal (t2->opval.val));
476 /* only compare these two types */
484 /*-----------------------------------------------------------------*/
485 /* resolveSymbols - resolve symbols from the symbol table */
486 /*-----------------------------------------------------------------*/
488 resolveSymbols (ast * tree)
490 /* walk the entire tree and check for values */
491 /* with symbols if we find one then replace */
492 /* symbol with that from the symbol table */
499 /* if not block & function */
500 if (tree->type == EX_OP &&
501 (tree->opval.op != FUNCTION &&
502 tree->opval.op != BLOCK &&
503 tree->opval.op != NULLOP))
505 filename = tree->filename;
506 lineno = tree->lineno;
510 /* make sure we resolve the true & false labels for ifx */
511 if (tree->type == EX_OP && tree->opval.op == IFX)
517 if ((csym = findSym (LabelTab, tree->trueLabel,
518 tree->trueLabel->name)))
519 tree->trueLabel = csym;
521 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
522 tree->trueLabel->name);
525 if (tree->falseLabel)
527 if ((csym = findSym (LabelTab,
529 tree->falseLabel->name)))
530 tree->falseLabel = csym;
532 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
533 tree->falseLabel->name);
538 /* if this is a label resolve it from the labelTab */
539 if (IS_AST_VALUE (tree) &&
540 tree->opval.val->sym &&
541 tree->opval.val->sym->islbl)
544 symbol *csym = findSym (LabelTab, tree->opval.val->sym,
545 tree->opval.val->sym->name);
548 werrorfl (tree->filename, tree->lineno, E_LABEL_UNDEF,
549 tree->opval.val->sym->name);
551 tree->opval.val->sym = csym;
553 goto resolveChildren;
556 /* do only for leafs */
557 if (IS_AST_VALUE (tree) &&
558 tree->opval.val->sym &&
559 !tree->opval.val->sym->implicit)
562 symbol *csym = findSymWithLevel (SymbolTab, tree->opval.val->sym);
564 /* if found in the symbol table & they r not the same */
565 if (csym && tree->opval.val->sym != csym)
567 tree->opval.val->sym = csym;
568 tree->opval.val->type = csym->type;
569 tree->opval.val->etype = csym->etype;
572 /* if not found in the symbol table */
573 /* mark it as undefined assume it is */
574 /* an integer in data space */
575 if (!csym && !tree->opval.val->sym->implicit)
578 /* if this is a function name then */
579 /* mark it as returning an int */
582 tree->opval.val->sym->type = newLink (DECLARATOR);
583 DCL_TYPE (tree->opval.val->sym->type) = FUNCTION;
584 tree->opval.val->sym->type->next =
585 tree->opval.val->sym->etype = newIntLink ();
586 tree->opval.val->etype = tree->opval.val->etype;
587 tree->opval.val->type = tree->opval.val->sym->type;
588 werrorfl (tree->filename, tree->lineno, W_IMPLICIT_FUNC,
589 tree->opval.val->sym->name);
590 //tree->opval.val->sym->undefined = 1;
591 allocVariables (tree->opval.val->sym);
595 tree->opval.val->sym->undefined = 1;
596 tree->opval.val->type =
597 tree->opval.val->etype = newIntLink ();
598 tree->opval.val->sym->type =
599 tree->opval.val->sym->etype = newIntLink ();
605 resolveSymbols (tree->left);
606 resolveSymbols (tree->right);
611 /*-----------------------------------------------------------------*/
612 /* setAstLineno - walks a ast tree & sets the line number */
613 /*-----------------------------------------------------------------*/
614 int setAstLineno (ast * tree, int lineno)
619 tree->lineno = lineno;
620 setAstLineno (tree->left, lineno);
621 setAstLineno (tree->right, lineno);
625 /*-----------------------------------------------------------------*/
626 /* funcOfType :- function of type with name */
627 /*-----------------------------------------------------------------*/
629 funcOfType (char *name, sym_link * type, sym_link * argType,
633 /* create the symbol */
634 sym = newSymbol (name, 0);
636 /* setup return value */
637 sym->type = newLink (DECLARATOR);
638 DCL_TYPE (sym->type) = FUNCTION;
639 sym->type->next = copyLinkChain (type);
640 sym->etype = getSpec (sym->type);
641 FUNC_ISREENT(sym->type) = rent ? 1 : 0;
643 /* if arguments required */
647 args = FUNC_ARGS(sym->type) = newValue ();
651 args->type = copyLinkChain (argType);
652 args->etype = getSpec (args->type);
653 SPEC_EXTR(args->etype)=1;
656 args = args->next = newValue ();
663 allocVariables (sym);
668 /*-----------------------------------------------------------------*/
669 /* funcOfTypeVarg :- function of type with name and argtype */
670 /*-----------------------------------------------------------------*/
672 funcOfTypeVarg (char *name, char * rtype, int nArgs , char **atypes)
677 /* create the symbol */
678 sym = newSymbol (name, 0);
680 /* setup return value */
681 sym->type = newLink (DECLARATOR);
682 DCL_TYPE (sym->type) = FUNCTION;
683 sym->type->next = typeFromStr(rtype);
684 sym->etype = getSpec (sym->type);
686 /* if arguments required */
689 args = FUNC_ARGS(sym->type) = newValue ();
691 for ( i = 0 ; i < nArgs ; i++ ) {
692 args->type = typeFromStr(atypes[i]);
693 args->etype = getSpec (args->type);
694 SPEC_EXTR(args->etype)=1;
695 if ((i + 1) == nArgs) break;
696 args = args->next = newValue ();
703 allocVariables (sym);
708 /*-----------------------------------------------------------------*/
709 /* reverseParms - will reverse a parameter tree */
710 /*-----------------------------------------------------------------*/
712 reverseParms (ast * ptree)
718 /* top down if we find a nonParm tree then quit */
719 if (ptree->type == EX_OP && ptree->opval.op == PARAM && !ptree->reversed)
721 /* The various functions expect the parameter tree to be right heavy. */
722 /* Rotate the tree to be left heavy so that after reversal it is */
723 /* right heavy again. */
724 while ((ttree = ptree->right) && ttree->type == EX_OP &&
725 ttree->opval.op == PARAM)
727 ptree->right = ttree->right;
728 ttree->right = ttree->left;
729 ttree->left = ptree->left;
735 ptree->left = ptree->right;
736 ptree->right = ttree;
738 reverseParms (ptree->left);
739 reverseParms (ptree->right);
745 /*-----------------------------------------------------------------*/
746 /* processParms - makes sure the parameters are okay and do some */
747 /* processing with them */
748 /*-----------------------------------------------------------------*/
750 processParms (ast *func,
753 int *parmNumber, /* unused, although updated */
756 RESULT_TYPE resultType;
759 /* if none of them exist */
760 if (!defParm && !*actParm)
765 if (getenv("DEBUG_SANITY"))
767 fprintf (stderr, "processParms: %s ", defParm->name);
769 /* make sure the type is complete and sane */
770 checkTypeSanity(defParm->etype, defParm->name);
773 if (IS_CODEPTR (func->ftype))
774 functype = func->ftype->next;
776 functype = func->ftype;
778 /* if the function is being called via a pointer & */
779 /* it has not been defined a reentrant then we cannot */
780 /* have parameters */
781 /* PIC16 port can... */
782 if (!TARGET_IS_PIC16)
784 if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
786 werror (W_NONRENT_ARGS);
792 /* if defined parameters ended but actual parameters */
793 /* exist and this is not defined as a variable arg */
794 if (!defParm && *actParm && !IFFUNC_HASVARARGS(functype))
796 werror (E_TOO_MANY_PARMS);
800 /* if defined parameters present but no actual parameters */
801 if (defParm && !*actParm)
803 werror (E_TOO_FEW_PARMS);
807 /* if this is a PARAM node then match left & right */
808 if ((*actParm)->type == EX_OP && (*actParm)->opval.op == PARAM)
810 (*actParm)->decorated = 1;
811 return (processParms (func, defParm,
812 &(*actParm)->left, parmNumber, FALSE) ||
813 processParms (func, defParm ? defParm->next : NULL,
814 &(*actParm)->right, parmNumber, rightmost));
816 else if (defParm) /* not vararg */
818 /* If we have found a value node by following only right-hand links,
819 * then we know that there are no more values after us.
821 * Therefore, if there are more defined parameters, the caller didn't
824 if (rightmost && defParm->next)
826 werror (E_TOO_FEW_PARMS);
831 /* decorate parameter */
832 resultType = defParm ? getResultTypeFromType (defParm->type) :
834 *actParm = decorateType (*actParm, resultType);
836 if (IS_VOID((*actParm)->ftype))
838 werror (E_VOID_VALUE_USED);
842 /* If this is a varargs function... */
843 if (!defParm && *actParm && IFFUNC_HASVARARGS(functype))
848 if (IS_CAST_OP (*actParm)
849 || (IS_AST_LIT_VALUE (*actParm) && (*actParm)->values.literalFromCast))
851 /* Parameter was explicitly typecast; don't touch it. */
855 ftype = (*actParm)->ftype;
857 /* If it's a char, upcast to int. */
858 if (IS_INTEGRAL (ftype)
859 && (getSize (ftype) < (unsigned) INTSIZE))
861 newType = newAst_LINK(INTTYPE);
864 if (IS_PTR(ftype) && !IS_GENPTR(ftype))
866 newType = newAst_LINK (copyLinkChain(ftype));
867 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
870 if (IS_AGGREGATE (ftype))
872 newType = newAst_LINK (copyLinkChain (ftype));
873 DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
878 /* cast required; change this op to a cast. */
879 (*actParm)->decorated = 0;
880 *actParm = newNode (CAST, newType, *actParm);
881 (*actParm)->lineno = (*actParm)->right->lineno;
883 decorateType (*actParm, RESULT_TYPE_NONE);
888 /* if defined parameters ended but actual has not & */
890 if (!defParm && *actParm &&
891 (options.stackAuto || IFFUNC_ISREENT (functype)))
894 resolveSymbols (*actParm);
896 /* the parameter type must be at least castable */
897 if (compareType (defParm->type, (*actParm)->ftype) == 0)
899 werror (E_INCOMPAT_TYPES);
900 printFromToType ((*actParm)->ftype, defParm->type);
904 /* if the parameter is castable then add the cast */
905 if (compareType (defParm->type, (*actParm)->ftype) < 0)
909 resultType = getResultTypeFromType (defParm->etype);
910 pTree = resolveSymbols (copyAst (*actParm));
912 /* now change the current one to a cast */
913 (*actParm)->type = EX_OP;
914 (*actParm)->opval.op = CAST;
915 (*actParm)->left = newAst_LINK (defParm->type);
916 (*actParm)->right = pTree;
917 (*actParm)->decorated = 0; /* force typechecking */
918 decorateType (*actParm, resultType);
921 /* make a copy and change the regparm type to the defined parm */
922 (*actParm)->etype = getSpec ((*actParm)->ftype = copyLinkChain ((*actParm)->ftype));
923 SPEC_REGPARM ((*actParm)->etype) = SPEC_REGPARM (defParm->etype);
924 SPEC_ARGREG ((*actParm)->etype) = SPEC_ARGREG (defParm->etype);
929 /*-----------------------------------------------------------------*/
930 /* createIvalType - generates ival for basic types */
931 /*-----------------------------------------------------------------*/
933 createIvalType (ast * sym, sym_link * type, initList * ilist)
937 /* if initList is deep */
938 if (ilist->type == INIT_DEEP)
939 ilist = ilist->init.deep;
941 iExpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
942 return decorateType (newNode ('=', sym, iExpr), RESULT_TYPE_NONE);
945 /*-----------------------------------------------------------------*/
946 /* createIvalStruct - generates initial value for structures */
947 /*-----------------------------------------------------------------*/
949 createIvalStruct (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
956 sflds = SPEC_STRUCT (type)->fields;
957 if (ilist->type != INIT_DEEP)
959 werror (E_INIT_STRUCT, "");
963 iloop = ilist->init.deep;
965 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
967 /* if we have come to end */
971 lAst = newNode (PTR_OP, newNode ('&', sym, NULL), newAst_VALUE (symbolVal (sflds)));
972 lAst = decorateType (resolveSymbols (lAst), RESULT_TYPE_NONE);
973 rast = decorateType (resolveSymbols (createIval (lAst, sflds->type,
974 iloop, rast, rootValue)),
980 werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
981 W_EXCESS_INITIALIZERS, "struct",
982 sym->opval.val->sym->name);
989 /*-----------------------------------------------------------------*/
990 /* createIvalArray - generates code for array initialization */
991 /*-----------------------------------------------------------------*/
993 createIvalArray (ast * sym, sym_link * type, initList * ilist, ast *rootValue)
997 int lcnt = 0, size = 0;
998 literalList *literalL;
1000 /* take care of the special case */
1001 /* array of characters can be init */
1003 if (IS_CHAR (type->next))
1004 if ((rast = createIvalCharPtr (sym,
1006 decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE),
1009 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1011 /* not the special case */
1012 if (ilist->type != INIT_DEEP)
1014 werror (E_INIT_STRUCT, "");
1018 iloop = ilist->init.deep;
1019 lcnt = DCL_ELEM (type);
1021 if (port->arrayInitializerSuppported && convertIListToConstList(ilist, &literalL))
1025 aSym = decorateType (resolveSymbols(sym), RESULT_TYPE_NONE);
1027 rast = newNode(ARRAYINIT, aSym, NULL);
1028 rast->values.constlist = literalL;
1030 // Make sure size is set to length of initializer list.
1034 iloop = iloop->next;
1037 if (lcnt && size > lcnt)
1039 // Array size was specified, and we have more initializers than needed.
1040 char *name=sym->opval.val->sym->name;
1041 int lineno=sym->opval.val->sym->lineDef;
1042 char *filename=sym->opval.val->sym->fileDef;
1044 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
1053 aSym = newNode ('[', sym, newAst_VALUE (valueFromLit ((float) (size++))));
1054 aSym = decorateType (resolveSymbols (aSym), RESULT_TYPE_NONE);
1055 rast = createIval (aSym, type->next, iloop, rast, rootValue);
1056 iloop = (iloop ? iloop->next : NULL);
1062 /* no of elements given and we */
1063 /* have generated for all of them */
1066 // is this a better way? at least it won't crash
1067 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
1068 int lineno = iloop->lineno;
1069 char *filename = iloop->filename;
1070 werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
1077 /* if we have not been given a size */
1078 if (!DCL_ELEM (type))
1080 /* check, if it's a flexible array */
1081 if (IS_STRUCT (AST_VALUE (rootValue)->type))
1082 AST_SYMBOL(rootValue)->flexArrayLength = size * getSize (type->next);
1084 DCL_ELEM (type) = size;
1087 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1091 /*-----------------------------------------------------------------*/
1092 /* createIvalCharPtr - generates initial values for char pointers */
1093 /*-----------------------------------------------------------------*/
1095 createIvalCharPtr (ast * sym, sym_link * type, ast * iexpr, ast *rootVal)
1100 /* if this is a pointer & right is a literal array then */
1101 /* just assignment will do */
1102 if (IS_PTR (type) && ((IS_LITERAL (iexpr->etype) ||
1103 SPEC_SCLS (iexpr->etype) == S_CODE)
1104 && IS_ARRAY (iexpr->ftype)))
1105 return newNode ('=', sym, iexpr);
1107 /* left side is an array so we have to assign each */
1109 if ((IS_LITERAL (iexpr->etype) ||
1110 SPEC_SCLS (iexpr->etype) == S_CODE)
1111 && IS_ARRAY (iexpr->ftype))
1113 /* for each character generate an assignment */
1114 /* to the array element */
1115 char *s = SPEC_CVAL (iexpr->etype).v_char;
1117 unsigned int symsize = getSize (type);
1119 size = getSize (iexpr->ftype);
1120 if (symsize && size>symsize)
1122 if (size>(symsize+1))
1124 char *name = (IS_AST_SYM_VALUE(sym)) ? AST_SYMBOL(sym)->name : "";
1126 werrorfl (iexpr->filename, iexpr->lineno, W_EXCESS_INITIALIZERS,
1132 for (i=0;i<size;i++)
1134 rast = newNode (NULLOP,
1138 newAst_VALUE (valueFromLit ((float) i))),
1139 newAst_VALUE (valueFromLit (*s))));
1143 // now WE don't need iexpr's symbol anymore
1144 freeStringSymbol(AST_SYMBOL(iexpr));
1146 /* if we have not been given a size */
1147 if (!DCL_ELEM (type))
1149 /* check, if it's a flexible array */
1150 if (IS_STRUCT (AST_VALUE (rootVal)->type))
1151 AST_SYMBOL(rootVal)->flexArrayLength = size * getSize (type->next);
1153 DCL_ELEM (type) = size;
1156 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1162 /*-----------------------------------------------------------------*/
1163 /* createIvalPtr - generates initial value for pointers */
1164 /*-----------------------------------------------------------------*/
1166 createIvalPtr (ast * sym, sym_link * type, initList * ilist, ast *rootVal)
1172 if (ilist->type == INIT_DEEP)
1173 ilist = ilist->init.deep;
1175 iexpr = decorateType (resolveSymbols (list2expr (ilist)), RESULT_TYPE_NONE);
1177 /* if character pointer */
1178 if (IS_CHAR (type->next))
1179 if ((rast = createIvalCharPtr (sym, type, iexpr, rootVal)))
1182 return newNode ('=', sym, iexpr);
1185 /*-----------------------------------------------------------------*/
1186 /* createIval - generates code for initial value */
1187 /*-----------------------------------------------------------------*/
1189 createIval (ast * sym, sym_link * type, initList * ilist, ast * wid, ast *rootValue)
1196 /* if structure then */
1197 if (IS_STRUCT (type))
1198 rast = createIvalStruct (sym, type, ilist, rootValue);
1200 /* if this is a pointer */
1202 rast = createIvalPtr (sym, type, ilist, rootValue);
1204 /* if this is an array */
1205 if (IS_ARRAY (type))
1206 rast = createIvalArray (sym, type, ilist, rootValue);
1208 /* if type is SPECIFIER */
1210 rast = createIvalType (sym, type, ilist);
1213 return decorateType (resolveSymbols (newNode (NULLOP, wid, rast)), RESULT_TYPE_NONE);
1215 return decorateType (resolveSymbols (rast), RESULT_TYPE_NONE);
1218 /*-----------------------------------------------------------------*/
1219 /* initAggregates - initialises aggregate variables with initv */
1220 /*-----------------------------------------------------------------*/
1221 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
1222 ast *newAst = newAst_VALUE (symbolVal (sym));
1223 return createIval (newAst, sym->type, ival, wid, newAst);
1226 /*-----------------------------------------------------------------*/
1227 /* gatherAutoInit - creates assignment expressions for initial */
1229 /*-----------------------------------------------------------------*/
1231 gatherAutoInit (symbol * autoChain)
1238 for (sym = autoChain; sym; sym = sym->next)
1241 /* resolve the symbols in the ival */
1243 resolveIvalSym (sym->ival, sym->type);
1246 /* if we are PIC16 port,
1247 * and this is a static,
1248 * and have initial value,
1249 * and not S_CODE, don't emit in gs segment,
1250 * but allow glue.c:pic16emitRegularMap to put symbol
1251 * in idata section */
1252 if(TARGET_IS_PIC16 &&
1253 IS_STATIC (sym->etype) && sym->ival
1254 && SPEC_SCLS(sym->etype) != S_CODE) {
1255 SPEC_SCLS (sym->etype) = S_DATA;
1260 /* if this is a static variable & has an */
1261 /* initial value the code needs to be lifted */
1262 /* here to the main portion since they can be */
1263 /* initialised only once at the start */
1264 if (IS_STATIC (sym->etype) && sym->ival &&
1265 SPEC_SCLS (sym->etype) != S_CODE)
1269 /* insert the symbol into the symbol table */
1270 /* with level = 0 & name = rname */
1271 newSym = copySymbol (sym);
1272 addSym (SymbolTab, newSym, newSym->rname, 0, 0, 1);
1274 /* now lift the code to main */
1275 if (IS_AGGREGATE (sym->type)) {
1276 work = initAggregates (sym, sym->ival, NULL);
1278 if (getNelements(sym->type, sym->ival)>1) {
1279 werrorfl (sym->fileDef, sym->lineDef,
1280 W_EXCESS_INITIALIZERS, "scalar",
1283 work = newNode ('=', newAst_VALUE (symbolVal (newSym)),
1284 list2expr (sym->ival));
1287 setAstLineno (work, sym->lineDef);
1291 staticAutos = newNode (NULLOP, staticAutos, work);
1298 /* if there is an initial value */
1299 if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
1301 initList *ilist=sym->ival;
1303 while (ilist->type == INIT_DEEP) {
1304 ilist = ilist->init.deep;
1307 /* update lineno for error msg */
1308 lineno=sym->lineDef;
1309 setAstLineno (ilist->init.node, lineno);
1311 if (IS_AGGREGATE (sym->type)) {
1312 work = initAggregates (sym, sym->ival, NULL);
1314 if (getNelements(sym->type, sym->ival)>1) {
1315 werrorfl (sym->fileDef, sym->lineDef,
1316 W_EXCESS_INITIALIZERS, "scalar",
1319 work = newNode ('=', newAst_VALUE (symbolVal (sym)),
1320 list2expr (sym->ival));
1324 setAstLineno (work, sym->lineDef);
1328 init = newNode (NULLOP, init, work);
1337 /*-----------------------------------------------------------------*/
1338 /* freeStringSymbol - delete a literal string if no more usage */
1339 /*-----------------------------------------------------------------*/
1340 void freeStringSymbol(symbol *sym) {
1341 /* make sure this is a literal string */
1342 assert (sym->isstrlit);
1343 if (--sym->isstrlit == 0) { // lower the usage count
1344 memmap *segment=SPEC_OCLS(sym->etype);
1346 deleteSetItem(&segment->syms, sym);
1351 /*-----------------------------------------------------------------*/
1352 /* stringToSymbol - creates a symbol from a literal string */
1353 /*-----------------------------------------------------------------*/
1355 stringToSymbol (value * val)
1357 char name[SDCC_NAME_MAX + 1];
1358 static int charLbl = 0;
1363 // have we heard this before?
1364 for (sp=statsg->syms; sp; sp=sp->next) {
1366 size = getSize (sym->type);
1367 if (sym->isstrlit && size == getSize (val->type) &&
1368 !memcmp(SPEC_CVAL(sym->etype).v_char, SPEC_CVAL(val->etype).v_char, size)) {
1369 // yes, this is old news. Don't publish it again.
1370 sym->isstrlit++; // but raise the usage count
1371 return symbolVal(sym);
1375 SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
1376 sym = newSymbol (name, 0); /* make it @ level 0 */
1377 strncpyz (sym->rname, name, SDCC_NAME_MAX);
1379 /* copy the type from the value passed */
1380 sym->type = copyLinkChain (val->type);
1381 sym->etype = getSpec (sym->type);
1382 /* change to storage class & output class */
1383 SPEC_SCLS (sym->etype) = S_CODE;
1384 SPEC_CVAL (sym->etype).v_char = SPEC_CVAL (val->etype).v_char;
1385 SPEC_STAT (sym->etype) = 1;
1386 /* make the level & block = 0 */
1387 sym->block = sym->level = 0;
1389 /* create an ival */
1390 sym->ival = newiList (INIT_NODE, newAst_VALUE (val));
1395 allocVariables (sym);
1398 return symbolVal (sym);
1402 /*-----------------------------------------------------------------*/
1403 /* processBlockVars - will go thru the ast looking for block if */
1404 /* a block is found then will allocate the syms */
1405 /* will also gather the auto inits present */
1406 /*-----------------------------------------------------------------*/
1408 processBlockVars (ast * tree, int *stack, int action)
1413 /* if this is a block */
1414 if (tree->type == EX_OP && tree->opval.op == BLOCK)
1418 if (action == ALLOCATE)
1420 *stack += allocVariables (tree->values.sym);
1421 autoInit = gatherAutoInit (tree->values.sym);
1423 /* if there are auto inits then do them */
1425 tree->left = newNode (NULLOP, autoInit, tree->left);
1427 else /* action is deallocate */
1428 deallocLocal (tree->values.sym);
1431 processBlockVars (tree->left, stack, action);
1432 processBlockVars (tree->right, stack, action);
1437 /*-------------------------------------------------------------*/
1438 /* constExprTree - returns TRUE if this tree is a constant */
1440 /*-------------------------------------------------------------*/
1441 bool constExprTree (ast *cexpr) {
1447 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1449 switch (cexpr->type)
1452 if (IS_AST_LIT_VALUE(cexpr)) {
1453 // this is a literal
1456 if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
1457 // a function's address will never change
1460 if (IS_AST_SYM_VALUE(cexpr) && IS_ARRAY(AST_SYMBOL(cexpr)->type)) {
1461 // an array's address will never change
1464 if (IS_AST_SYM_VALUE(cexpr) &&
1465 IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
1466 // a symbol in code space will never change
1467 // This is only for the 'char *s="hallo"' case and will have to leave
1468 //printf(" code space symbol");
1473 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1474 "unexpected link in expression tree\n");
1477 if (cexpr->opval.op==ARRAYINIT) {
1478 // this is a list of literals
1481 if (cexpr->opval.op=='=') {
1482 return constExprTree(cexpr->right);
1484 if (cexpr->opval.op==CAST) {
1485 // cast ignored, maybe we should throw a warning here?
1486 return constExprTree(cexpr->right);
1488 if (cexpr->opval.op=='&') {
1491 if (cexpr->opval.op==CALL || cexpr->opval.op==PCALL) {
1494 if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
1499 return IS_CONSTANT(operandType(cexpr->opval.oprnd));
1504 /*-----------------------------------------------------------------*/
1505 /* constExprValue - returns the value of a constant expression */
1506 /* or NULL if it is not a constant expression */
1507 /*-----------------------------------------------------------------*/
1509 constExprValue (ast * cexpr, int check)
1511 cexpr = decorateType (resolveSymbols (cexpr), RESULT_TYPE_NONE);
1513 /* if this is not a constant then */
1514 if (!IS_LITERAL (cexpr->ftype))
1516 /* then check if this is a literal array
1518 if (SPEC_SCLS (cexpr->etype) == S_CODE &&
1519 SPEC_CVAL (cexpr->etype).v_char &&
1520 IS_ARRAY (cexpr->ftype))
1522 value *val = valFromType (cexpr->ftype);
1523 SPEC_SCLS (val->etype) = S_LITERAL;
1524 val->sym = cexpr->opval.val->sym;
1525 val->sym->type = copyLinkChain (cexpr->ftype);
1526 val->sym->etype = getSpec (val->sym->type);
1527 strncpyz (val->name, cexpr->opval.val->sym->rname, SDCC_NAME_MAX);
1531 /* if we are casting a literal value then */
1532 if (IS_AST_OP (cexpr) &&
1533 cexpr->opval.op == CAST &&
1534 IS_LITERAL (cexpr->right->ftype))
1536 return valCastLiteral (cexpr->ftype,
1537 floatFromVal (cexpr->right->opval.val));
1540 if (IS_AST_VALUE (cexpr))
1542 return cexpr->opval.val;
1546 werror (E_CONST_EXPECTED, "found expression");
1551 /* return the value */
1552 if (IS_AST_VALUE (cexpr))
1554 return cexpr->opval.val;
1559 /*-----------------------------------------------------------------*/
1560 /* isLabelInAst - will return true if a given label is found */
1561 /*-----------------------------------------------------------------*/
1563 isLabelInAst (symbol * label, ast * tree)
1565 if (!tree || IS_AST_VALUE (tree) || IS_AST_LINK (tree))
1568 if (IS_AST_OP (tree) &&
1569 tree->opval.op == LABEL &&
1570 isSymbolEqual (AST_SYMBOL (tree->left), label))
1573 return isLabelInAst (label, tree->right) &&
1574 isLabelInAst (label, tree->left);
1578 /*-----------------------------------------------------------------*/
1579 /* isLoopCountable - return true if the loop count can be determi- */
1580 /* -ned at compile time . */
1581 /*-----------------------------------------------------------------*/
1583 isLoopCountable (ast * initExpr, ast * condExpr, ast * loopExpr,
1584 symbol ** sym, ast ** init, ast ** end)
1587 /* the loop is considered countable if the following
1588 conditions are true :-
1590 a) initExpr :- <sym> = <const>
1591 b) condExpr :- <sym> < <const1>
1592 c) loopExpr :- <sym> ++
1595 /* first check the initExpr */
1596 if (IS_AST_OP (initExpr) &&
1597 initExpr->opval.op == '=' && /* is assignment */
1598 IS_AST_SYM_VALUE (initExpr->left))
1599 { /* left is a symbol */
1601 *sym = AST_SYMBOL (initExpr->left);
1602 *init = initExpr->right;
1607 /* don't reverse loop with volatile counter */
1608 if (IS_VOLATILE ((*sym)->type))
1611 /* for now the symbol has to be of
1613 if (!IS_INTEGRAL ((*sym)->type))
1616 /* now check condExpr */
1617 if (IS_AST_OP (condExpr))
1620 switch (condExpr->opval.op)
1623 if (IS_AST_SYM_VALUE (condExpr->left) &&
1624 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left)) &&
1625 IS_AST_LIT_VALUE (condExpr->right))
1627 *end = condExpr->right;
1633 if (IS_AST_OP (condExpr->left) &&
1634 condExpr->left->opval.op == '>' &&
1635 IS_AST_LIT_VALUE (condExpr->left->right) &&
1636 IS_AST_SYM_VALUE (condExpr->left->left) &&
1637 isSymbolEqual (*sym, AST_SYMBOL (condExpr->left->left)))
1640 *end = newNode ('+', condExpr->left->right,
1641 newAst_VALUE (constVal ("1")));
1654 /* check loop expression is of the form <sym>++ */
1655 if (!IS_AST_OP (loopExpr))
1658 /* check if <sym> ++ */
1659 if (loopExpr->opval.op == INC_OP)
1665 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1666 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)))
1673 if (IS_AST_SYM_VALUE (loopExpr->right) &&
1674 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->right)))
1682 if (loopExpr->opval.op == ADD_ASSIGN)
1685 if (IS_AST_SYM_VALUE (loopExpr->left) &&
1686 isSymbolEqual (*sym, AST_SYMBOL (loopExpr->left)) &&
1687 IS_AST_LIT_VALUE (loopExpr->right) &&
1688 (int) AST_LIT_VALUE (loopExpr->right) != 1)
1696 /*-----------------------------------------------------------------*/
1697 /* astHasVolatile - returns true if ast contains any volatile */
1698 /*-----------------------------------------------------------------*/
1700 astHasVolatile (ast * tree)
1705 if (TETYPE (tree) && IS_VOLATILE (TETYPE (tree)))
1708 if (IS_AST_OP (tree))
1709 return astHasVolatile (tree->left) ||
1710 astHasVolatile (tree->right);
1715 /*-----------------------------------------------------------------*/
1716 /* astHasPointer - return true if the ast contains any ptr variable */
1717 /*-----------------------------------------------------------------*/
1719 astHasPointer (ast * tree)
1724 if (IS_AST_LINK (tree))
1727 /* if we hit an array expression then check
1728 only the left side */
1729 if (IS_AST_OP (tree) && tree->opval.op == '[')
1730 return astHasPointer (tree->left);
1732 if (IS_AST_VALUE (tree))
1733 return IS_PTR (tree->ftype) || IS_ARRAY (tree->ftype);
1735 return astHasPointer (tree->left) ||
1736 astHasPointer (tree->right);
1740 /*-----------------------------------------------------------------*/
1741 /* astHasSymbol - return true if the ast has the given symbol */
1742 /*-----------------------------------------------------------------*/
1744 astHasSymbol (ast * tree, symbol * sym)
1746 if (!tree || IS_AST_LINK (tree))
1749 if (IS_AST_VALUE (tree))
1751 if (IS_AST_SYM_VALUE (tree))
1752 return isSymbolEqual (AST_SYMBOL (tree), sym);
1757 return astHasSymbol (tree->left, sym) ||
1758 astHasSymbol (tree->right, sym);
1761 /*-----------------------------------------------------------------*/
1762 /* astHasDeref - return true if the ast has an indirect access */
1763 /*-----------------------------------------------------------------*/
1765 astHasDeref (ast * tree)
1767 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE(tree))
1770 if (tree->opval.op == '*' && tree->right == NULL) return TRUE;
1772 return astHasDeref (tree->left) || astHasDeref (tree->right);
1775 /*-----------------------------------------------------------------*/
1776 /* isConformingBody - the loop body has to conform to a set of rules */
1777 /* for the loop to be considered reversible read on for rules */
1778 /*-----------------------------------------------------------------*/
1780 isConformingBody (ast * pbody, symbol * sym, ast * body)
1783 /* we are going to do a pre-order traversal of the
1784 tree && check for the following conditions. (essentially
1785 a set of very shallow tests )
1786 a) the sym passed does not participate in
1787 any arithmetic operation
1788 b) There are no function calls
1789 c) all jumps are within the body
1790 d) address of loop control variable not taken
1791 e) if an assignment has a pointer on the
1792 left hand side make sure right does not have
1793 loop control variable */
1795 /* if we reach the end or a leaf then true */
1796 if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
1799 /* if anything else is "volatile" */
1800 if (IS_VOLATILE (TETYPE (pbody)))
1803 /* we will walk the body in a pre-order traversal for
1805 switch (pbody->opval.op)
1807 /*------------------------------------------------------------------*/
1809 // if the loopvar is used as an index
1810 /* array op is commutative -- must check both left & right */
1811 if (astHasSymbol(pbody->right, sym) || astHasSymbol(pbody->left, sym)) {
1814 return isConformingBody (pbody->right, sym, body)
1815 && isConformingBody (pbody->left, sym, body);
1817 /*------------------------------------------------------------------*/
1822 /*------------------------------------------------------------------*/
1826 /* sure we are not sym is not modified */
1828 IS_AST_SYM_VALUE (pbody->left) &&
1829 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1833 IS_AST_SYM_VALUE (pbody->right) &&
1834 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1839 /*------------------------------------------------------------------*/
1841 case '*': /* can be unary : if right is null then unary operation */
1846 /* if right is NULL then unary operation */
1847 /*------------------------------------------------------------------*/
1848 /*----------------------------*/
1850 /*----------------------------*/
1853 if (IS_AST_SYM_VALUE (pbody->left) &&
1854 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1857 return isConformingBody (pbody->left, sym, body);
1861 if (astHasSymbol (pbody->left, sym) ||
1862 astHasSymbol (pbody->right, sym))
1867 /*------------------------------------------------------------------*/
1878 if (IS_AST_SYM_VALUE (pbody->left) &&
1879 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1882 if (IS_AST_SYM_VALUE (pbody->right) &&
1883 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1886 return isConformingBody (pbody->left, sym, body) &&
1887 isConformingBody (pbody->right, sym, body);
1895 if (IS_AST_SYM_VALUE (pbody->left) &&
1896 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1898 return isConformingBody (pbody->left, sym, body);
1900 /*------------------------------------------------------------------*/
1912 case SIZEOF: /* evaluate wihout code generation */
1914 if (IS_AST_SYM_VALUE (pbody->left) &&
1915 isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1918 if (IS_AST_SYM_VALUE (pbody->right) &&
1919 isSymbolEqual (AST_SYMBOL (pbody->right), sym))
1922 return isConformingBody (pbody->left, sym, body) &&
1923 isConformingBody (pbody->right, sym, body);
1925 /*------------------------------------------------------------------*/
1928 /* if left has a pointer & right has loop
1929 control variable then we cannot */
1930 if (astHasPointer (pbody->left) &&
1931 astHasSymbol (pbody->right, sym))
1933 if (astHasVolatile (pbody->left))
1936 if (IS_AST_SYM_VALUE (pbody->left)) {
1937 // if the loopvar has an assignment
1938 if (isSymbolEqual (AST_SYMBOL (pbody->left), sym))
1940 // if the loopvar is used in another (maybe conditional) block
1941 if (astHasSymbol (pbody->right, sym) &&
1942 (pbody->level >= body->level)) {
1947 if (astHasVolatile (pbody->left))
1950 if (astHasDeref(pbody->right)) return FALSE;
1952 return isConformingBody (pbody->left, sym, body) &&
1953 isConformingBody (pbody->right, sym, body);
1964 assert ("Parser should not have generated this\n");
1966 /*------------------------------------------------------------------*/
1967 /*----------------------------*/
1968 /* comma operator */
1969 /*----------------------------*/
1971 return isConformingBody (pbody->left, sym, body) &&
1972 isConformingBody (pbody->right, sym, body);
1974 /*------------------------------------------------------------------*/
1975 /*----------------------------*/
1977 /*----------------------------*/
1979 /* if local & not passed as paramater then ok */
1980 if (sym->level && !astHasSymbol(pbody->right,sym))
1984 /*------------------------------------------------------------------*/
1985 /*----------------------------*/
1986 /* return statement */
1987 /*----------------------------*/
1992 if (isLabelInAst (AST_SYMBOL (pbody->left), body))
1997 if (astHasSymbol (pbody->left, sym))
2004 return isConformingBody (pbody->left, sym, body) &&
2005 isConformingBody (pbody->right, sym, body);
2011 /*-----------------------------------------------------------------*/
2012 /* isLoopReversible - takes a for loop as input && returns true */
2013 /* if the for loop is reversible. If yes will set the value of */
2014 /* the loop control var & init value & termination value */
2015 /*-----------------------------------------------------------------*/
2017 isLoopReversible (ast * loop, symbol ** loopCntrl,
2018 ast ** init, ast ** end)
2020 /* if option says don't do it then don't */
2021 if (optimize.noLoopReverse)
2023 /* there are several tests to determine this */
2025 /* for loop has to be of the form
2026 for ( <sym> = <const1> ;
2027 [<sym> < <const2>] ;
2028 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
2030 if (!isLoopCountable (AST_FOR (loop, initExpr),
2031 AST_FOR (loop, condExpr),
2032 AST_FOR (loop, loopExpr),
2033 loopCntrl, init, end))
2036 /* now do some serious checking on the body of the loop
2039 return isConformingBody (loop->left, *loopCntrl, loop->left);
2043 /*-----------------------------------------------------------------*/
2044 /* replLoopSym - replace the loop sym by loop sym -1 */
2045 /*-----------------------------------------------------------------*/
2047 replLoopSym (ast * body, symbol * sym)
2050 if (!body || IS_AST_LINK (body))
2053 if (IS_AST_SYM_VALUE (body))
2056 if (isSymbolEqual (AST_SYMBOL (body), sym))
2060 body->opval.op = '-';
2061 body->left = newAst_VALUE (symbolVal (sym));
2062 body->right = newAst_VALUE (constVal ("1"));
2070 replLoopSym (body->left, sym);
2071 replLoopSym (body->right, sym);
2075 /*-----------------------------------------------------------------*/
2076 /* reverseLoop - do the actual loop reversal */
2077 /*-----------------------------------------------------------------*/
2079 reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
2083 /* create the following tree
2088 if (sym) goto for_continue ;
2091 /* put it together piece by piece */
2092 rloop = newNode (NULLOP,
2093 createIf (newAst_VALUE (symbolVal (sym)),
2095 newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
2098 newAst_VALUE (symbolVal (sym)),
2101 replLoopSym (loop->left, sym);
2102 setAstLineno (rloop, init->lineno);
2104 rloop = newNode (NULLOP,
2106 newAst_VALUE (symbolVal (sym)),
2107 newNode ('-', end, init)),
2108 createLabel (AST_FOR (loop, continueLabel),
2112 newNode (SUB_ASSIGN,
2113 newAst_VALUE (symbolVal (sym)),
2114 newAst_VALUE (constVal ("1"))),
2117 rloop->lineno=init->lineno;
2118 return decorateType (rloop, RESULT_TYPE_NONE);
2122 /*-----------------------------------------------------------------*/
2123 /* searchLitOp - search tree (*ops only) for an ast with literal */
2124 /*-----------------------------------------------------------------*/
2126 searchLitOp (ast *tree, ast **parent, const unsigned char *ops)
2130 if (tree && optimize.global_cse)
2132 /* is there a literal operand? */
2134 IS_AST_OP(tree->right) &&
2135 tree->right->right &&
2136 (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
2138 if (IS_LITERAL (RTYPE (tree->right)) !=
2139 IS_LITERAL (LTYPE (tree->right)))
2141 tree->right->decorated = 0;
2142 tree->decorated = 0;
2146 ret = searchLitOp (tree->right, parent, ops);
2151 IS_AST_OP(tree->left) &&
2152 tree->left->right &&
2153 (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
2155 if (IS_LITERAL (RTYPE (tree->left)) !=
2156 IS_LITERAL (LTYPE (tree->left)))
2158 tree->left->decorated = 0;
2159 tree->decorated = 0;
2163 ret = searchLitOp (tree->left, parent, ops);
2171 /*-----------------------------------------------------------------*/
2172 /* getResultFromType */
2173 /*-----------------------------------------------------------------*/
2175 getResultTypeFromType (sym_link *type)
2177 /* type = getSpec (type); */
2179 return RESULT_TYPE_BIT;
2180 if (IS_BITFIELD (type))
2182 int blen = SPEC_BLEN (type);
2185 return RESULT_TYPE_BIT;
2187 return RESULT_TYPE_CHAR;
2188 return RESULT_TYPE_INT;
2191 return RESULT_TYPE_CHAR;
2194 return RESULT_TYPE_INT;
2195 return RESULT_TYPE_OTHER;
2198 /*-----------------------------------------------------------------*/
2199 /* addCast - adds casts to a type specified by RESULT_TYPE */
2200 /*-----------------------------------------------------------------*/
2202 addCast (ast *tree, RESULT_TYPE resultType, bool promote)
2205 bool upCasted = FALSE;
2209 case RESULT_TYPE_NONE:
2210 /* if thing smaller than int must be promoted to int */
2212 getSize (tree->etype) >= INTSIZE)
2213 /* promotion not necessary or already an int */
2215 /* char and bits: promote to int */
2216 newLink = newIntLink();
2219 case RESULT_TYPE_BIT:
2221 /* already an int */
2222 bitsForType (tree->etype) >= 16 ||
2223 /* bit to bit operation: don't promote, the code generators
2224 hopefully know everything about promotion rules */
2225 bitsForType (tree->etype) == 1)
2227 newLink = newIntLink();
2230 case RESULT_TYPE_CHAR:
2231 if (IS_CHAR (tree->etype) ||
2232 IS_FLOAT(tree->etype) ||
2233 IS_FIXED(tree->etype))
2235 newLink = newCharLink();
2237 case RESULT_TYPE_INT:
2239 if (getSize (tree->etype) > INTSIZE)
2241 /* warn ("Loosing significant digits"); */
2245 /* char: promote to int */
2247 getSize (tree->etype) >= INTSIZE)
2249 newLink = newIntLink();
2252 case RESULT_TYPE_OTHER:
2255 /* return type is long, float: promote char to int */
2256 if (getSize (tree->etype) >= INTSIZE)
2258 newLink = newIntLink();
2264 tree->decorated = 0;
2265 tree = newNode (CAST, newAst_LINK (newLink), tree);
2266 tree->lineno = tree->right->lineno;
2267 /* keep unsigned type during cast to smaller type,
2268 but not when promoting from char to int */
2270 SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
2271 return decorateType (tree, resultType);
2274 /*-----------------------------------------------------------------*/
2275 /* resultTypePropagate - decides if resultType can be propagated */
2276 /*-----------------------------------------------------------------*/
2278 resultTypePropagate (ast *tree, RESULT_TYPE resultType)
2280 switch (tree->opval.op)
2299 return RESULT_TYPE_NONE;
2303 return RESULT_TYPE_IFX;
2305 return RESULT_TYPE_NONE;
2309 /*-----------------------------------------------------------------*/
2310 /* getLeftResultType - gets type from left branch for propagation */
2311 /*-----------------------------------------------------------------*/
2313 getLeftResultType (ast *tree, RESULT_TYPE resultType)
2315 switch (tree->opval.op)
2319 if (IS_PTR (LTYPE (tree)))
2320 return RESULT_TYPE_NONE;
2322 return getResultTypeFromType (LETYPE (tree));
2324 if (IS_PTR (currFunc->type->next))
2325 return RESULT_TYPE_NONE;
2327 return getResultTypeFromType (currFunc->type->next);
2329 if (!IS_ARRAY (LTYPE (tree)))
2331 if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 255)
2332 return RESULT_TYPE_CHAR;
2339 /*------------------------------------------------------------------*/
2340 /* gatherImplicitVariables: assigns correct type information to */
2341 /* symbols and values created by replaceAstWithTemporary */
2342 /* and adds the symbols to the declarations list of the */
2343 /* innermost block that contains them */
2344 /*------------------------------------------------------------------*/
2346 gatherImplicitVariables (ast * tree, ast * block)
2351 if (tree->type == EX_OP && tree->opval.op == BLOCK)
2353 /* keep track of containing scope */
2356 if (tree->type == EX_OP && tree->opval.op == '=' &&
2357 tree->left->type == EX_VALUE && tree->left->opval.val->sym)
2359 symbol *assignee = tree->left->opval.val->sym;
2361 /* special case for assignment to compiler-generated temporary variable:
2362 compute type of RHS, and set the symbol's type to match */
2363 if (assignee->type == NULL && assignee->infertype) {
2364 ast *dtr = decorateType (resolveSymbols(tree->right), RESULT_TYPE_NONE);
2366 if (dtr != tree->right)
2369 assignee->type = copyLinkChain(TTYPE(dtr));
2370 assignee->etype = getSpec(assignee->type);
2371 SPEC_SCLS (assignee->etype) = S_AUTO;
2372 SPEC_OCLS (assignee->etype) = NULL;
2373 SPEC_EXTR (assignee->etype) = 0;
2374 SPEC_STAT (assignee->etype) = 0;
2375 SPEC_VOLATILE (assignee->etype) = 0;
2376 SPEC_ABSA (assignee->etype) = 0;
2378 wassertl(block != NULL, "implicit variable not contained in block");
2379 wassert(assignee->next == NULL);
2380 if (block != NULL) {
2381 symbol **decl = &(block->values.sym);
2384 wassert(*decl != assignee); /* should not already be in list */
2385 decl = &( (*decl)->next );
2392 if (tree->type == EX_VALUE && !(IS_LITERAL(tree->opval.val->etype)) &&
2393 tree->opval.val->type == NULL &&
2394 tree->opval.val->sym &&
2395 tree->opval.val->sym->infertype)
2397 /* fixup type of value for compiler-inferred temporary var */
2398 tree->opval.val->type = tree->opval.val->sym->type;
2399 tree->opval.val->etype = tree->opval.val->sym->etype;
2402 gatherImplicitVariables(tree->left, block);
2403 gatherImplicitVariables(tree->right, block);
2406 /*--------------------------------------------------------------------*/
2407 /* decorateType - compute type for this tree, also does type checking.*/
2408 /* This is done bottom up, since type has to flow upwards. */
2409 /* resultType flows top-down and forces e.g. char-arithmetic, if the */
2410 /* result is a char and the operand(s) are int's. */
2411 /* It also does constant folding, and parameter checking. */
2412 /*--------------------------------------------------------------------*/
2414 decorateType (ast * tree, RESULT_TYPE resultType)
2418 RESULT_TYPE resultTypeProp;
2423 /* if already has type then do nothing */
2424 if (tree->decorated)
2427 tree->decorated = 1;
2430 /* print the line */
2431 /* if not block & function */
2432 if (tree->type == EX_OP &&
2433 (tree->opval.op != FUNCTION &&
2434 tree->opval.op != BLOCK &&
2435 tree->opval.op != NULLOP))
2437 filename = tree->filename;
2438 lineno = tree->lineno;
2442 /* if any child is an error | this one is an error do nothing */
2443 if (tree->isError ||
2444 (tree->left && tree->left->isError) ||
2445 (tree->right && tree->right->isError))
2448 /*------------------------------------------------------------------*/
2449 /*----------------------------*/
2450 /* leaf has been reached */
2451 /*----------------------------*/
2452 lineno=tree->lineno;
2453 /* if this is of type value */
2454 /* just get the type */
2455 if (tree->type == EX_VALUE)
2458 if (IS_LITERAL (tree->opval.val->etype))
2461 /* if this is a character array then declare it */
2462 if (IS_ARRAY (tree->opval.val->type))
2463 tree->opval.val = stringToSymbol (tree->opval.val);
2465 /* otherwise just copy the type information */
2466 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2470 if (tree->opval.val->sym)
2472 /* if the undefined flag is set then give error message */
2473 if (tree->opval.val->sym->undefined)
2475 werror (E_ID_UNDEF, tree->opval.val->sym->name);
2477 TTYPE (tree) = TETYPE (tree) =
2478 tree->opval.val->type = tree->opval.val->sym->type =
2479 tree->opval.val->etype = tree->opval.val->sym->etype =
2480 copyLinkChain (INTTYPE);
2482 else if (tree->opval.val->sym->implicit)
2484 /* if implicit i.e. struct/union member then no type */
2485 TTYPE (tree) = TETYPE (tree) = NULL;
2489 /* copy the type from the value into the ast */
2490 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.val->type);
2492 /* and mark the symbol as referenced */
2493 tree->opval.val->sym->isref = 1;
2497 wassert(0); /* unreached: all values are literals or symbols */
2502 /* if type link for the case of cast */
2503 if (tree->type == EX_LINK)
2505 COPYTYPE (TTYPE (tree), TETYPE (tree), tree->opval.lnk);
2513 if (tree->opval.op == NULLOP || tree->opval.op == BLOCK)
2515 if (tree->left && tree->left->type == EX_OPERAND
2516 && (tree->left->opval.op == INC_OP
2517 || tree->left->opval.op == DEC_OP)
2518 && tree->left->left)
2520 tree->left->right = tree->left->left;
2521 tree->left->left = NULL;
2523 if (tree->right && tree->right->type == EX_OPERAND
2524 && (tree->right->opval.op == INC_OP
2525 || tree->right->opval.op == DEC_OP)
2526 && tree->right->left)
2528 tree->right->right = tree->right->left;
2529 tree->right->left = NULL;
2534 /* Before decorating the left branch we've to decide in dependence
2535 upon tree->opval.op, if resultType can be propagated */
2536 resultTypeProp = resultTypePropagate (tree, resultType);
2538 if (tree->opval.op == '?')
2539 dtl = decorateType (tree->left, RESULT_TYPE_IFX);
2541 dtl = decorateType (tree->left, resultTypeProp);
2543 /* if an array node, we may need to swap branches */
2544 if (tree->opval.op == '[')
2546 /* determine which is the array & which the index */
2547 if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
2548 IS_INTEGRAL (LTYPE (tree)))
2550 ast *tempTree = tree->left;
2551 tree->left = tree->right;
2552 tree->right = tempTree;
2556 /* After decorating the left branch there's type information available
2557 in tree->left->?type. If the op is e.g. '=' we extract the type
2558 information from there and propagate it to the right branch. */
2559 resultTypeProp = getLeftResultType (tree, resultTypeProp);
2561 switch (tree->opval.op)
2564 /* delay right side for '?' operator since conditional macro
2565 expansions might rely on this */
2569 /* decorate right side for CALL (parameter list) in processParms();
2570 there is resultType available */
2574 dtr = decorateType (tree->right, resultTypeProp);
2578 /* this is to take care of situations
2579 when the tree gets rewritten */
2580 if (dtl != tree->left)
2582 if (dtr != tree->right)
2584 if ((dtl && dtl->isError) || (dtr && dtr->isError))
2588 /* depending on type of operator do */
2590 switch (tree->opval.op)
2592 /*------------------------------------------------------------------*/
2593 /*----------------------------*/
2595 /*----------------------------*/
2598 /* first check if this is a array or a pointer */
2599 if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
2601 werror (E_NEED_ARRAY_PTR, "[]");
2602 goto errorTreeReturn;
2605 /* check if the type of the idx */
2606 if (!IS_INTEGRAL (RTYPE (tree)))
2608 werror (E_IDX_NOT_INT);
2609 goto errorTreeReturn;
2612 /* if the left is an rvalue then error */
2615 werror (E_LVALUE_REQUIRED, "array access");
2616 goto errorTreeReturn;
2619 if (IS_LITERAL (RTYPE (tree)))
2621 int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
2622 int arraySize = DCL_ELEM (LTYPE (tree));
2623 if (arraySize && arrayIndex >= arraySize)
2625 werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
2630 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
2631 SPEC_CONST (TETYPE (tree)) |= DCL_PTR_CONST (LTYPE (tree));
2634 /*------------------------------------------------------------------*/
2635 /*----------------------------*/
2637 /*----------------------------*/
2639 /* if this is not a structure */
2640 if (!IS_STRUCT (LTYPE (tree)))
2642 werror (E_STRUCT_UNION, ".");
2643 goto errorTreeReturn;
2645 TTYPE (tree) = structElemType (LTYPE (tree),
2646 (tree->right->type == EX_VALUE ?
2647 tree->right->opval.val : NULL));
2648 TETYPE (tree) = getSpec (TTYPE (tree));
2651 /*------------------------------------------------------------------*/
2652 /*----------------------------*/
2653 /* struct/union pointer */
2654 /*----------------------------*/
2656 /* if not pointer to a structure */
2657 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
2659 werror (E_PTR_REQD);
2660 goto errorTreeReturn;
2663 if (!IS_STRUCT (LTYPE (tree)->next))
2665 werror (E_STRUCT_UNION, "->");
2666 goto errorTreeReturn;
2669 TTYPE (tree) = structElemType (LTYPE (tree)->next,
2670 (tree->right->type == EX_VALUE ?
2671 tree->right->opval.val : NULL));
2672 TETYPE (tree) = getSpec (TTYPE (tree));
2674 /* adjust the storage class */
2675 switch (DCL_TYPE(tree->left->ftype)) {
2677 SPEC_SCLS(TETYPE(tree)) = S_DATA;
2680 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
2683 SPEC_SCLS(TETYPE(tree)) = S_CODE;
2686 SPEC_SCLS (TETYPE (tree)) = 0;
2689 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
2692 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
2695 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
2698 SPEC_SCLS (TETYPE (tree)) = 0;
2705 /* This breaks with extern declarations, bitfields, and perhaps other */
2706 /* cases (gcse). Let's leave this optimization disabled for now and */
2707 /* ponder if there's a safe way to do this. -- EEP */
2709 if (IS_ADDRESS_OF_OP (tree->left) && IS_AST_SYM_VALUE(tree->left->left)
2710 && SPEC_ABSA (AST_SYMBOL (tree->left->left)->etype))
2712 /* If defined struct type at addr var
2713 then rewrite (&struct var)->member
2715 and define membertype at (addr+offsetof(struct var,member)) temp
2718 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree)),
2719 AST_SYMBOL(tree->right));
2721 sym = newSymbol(genSymName (0), 0);
2722 sym->type = TTYPE (tree);
2723 sym->etype = getSpec(sym->type);
2724 sym->lineDef = tree->lineno;
2727 SPEC_STAT (sym->etype) = 1;
2728 SPEC_ADDR (sym->etype) = SPEC_ADDR (AST_SYMBOL (tree->left->left)->etype)
2730 SPEC_ABSA(sym->etype) = 1;
2731 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
2734 AST_VALUE (tree) = symbolVal(sym);
2737 tree->type = EX_VALUE;
2745 /*------------------------------------------------------------------*/
2746 /*----------------------------*/
2747 /* ++/-- operation */
2748 /*----------------------------*/
2752 sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
2753 COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
2754 if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
2755 werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
2764 /*------------------------------------------------------------------*/
2765 /*----------------------------*/
2767 /*----------------------------*/
2768 case '&': /* can be unary */
2769 /* if right is NULL then unary operation */
2770 if (tree->right) /* not an unary operation */
2773 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
2775 werror (E_BITWISE_OP);
2776 werror (W_CONTINUE, "left & right types are ");
2777 printTypeChain (LTYPE (tree), stderr);
2778 fprintf (stderr, ",");
2779 printTypeChain (RTYPE (tree), stderr);
2780 fprintf (stderr, "\n");
2781 goto errorTreeReturn;
2784 /* if they are both literal */
2785 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
2787 tree->type = EX_VALUE;
2788 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
2789 valFromType (RETYPE (tree)), '&');
2791 tree->right = tree->left = NULL;
2792 TETYPE (tree) = tree->opval.val->etype;
2793 TTYPE (tree) = tree->opval.val->type;
2797 /* see if this is a GETHBIT operation if yes
2800 ast *otree = optimizeGetHbit (tree, resultType);
2803 return decorateType (otree, RESULT_TYPE_NONE);
2806 /* see if this is a GETABIT operation if yes
2809 ast *otree = optimizeGetAbit (tree, resultType);
2812 return decorateType (otree, RESULT_TYPE_NONE);
2815 /* see if this is a GETBYTE operation if yes
2818 ast *otree = optimizeGetByte (tree, resultType);
2821 return decorateType (otree, RESULT_TYPE_NONE);
2824 /* see if this is a GETWORD operation if yes
2827 ast *otree = optimizeGetWord (tree, resultType);
2830 return decorateType (otree, RESULT_TYPE_NONE);
2833 /* if left is a literal exchange left & right */
2834 if (IS_LITERAL (LTYPE (tree)))
2836 ast *tTree = tree->left;
2837 tree->left = tree->right;
2838 tree->right = tTree;
2841 /* if right is a literal and */
2842 /* we can find a 2nd literal in an and-tree then */
2843 /* rearrange the tree */
2844 if (IS_LITERAL (RTYPE (tree)))
2847 ast *litTree = searchLitOp (tree, &parent, "&");
2851 ast *tTree = litTree->left;
2852 litTree->left = tree->right;
2853 tree->right = tTree;
2854 /* both operands in litTree are literal now */
2855 decorateType (parent, resultType);
2859 LRVAL (tree) = RRVAL (tree) = 1;
2861 TTYPE (tree) = computeType (LTYPE (tree),
2865 TETYPE (tree) = getSpec (TTYPE (tree));
2870 /*------------------------------------------------------------------*/
2871 /*----------------------------*/
2873 /*----------------------------*/
2874 p = newLink (DECLARATOR);
2875 /* if bit field then error */
2876 if (IS_BITVAR (tree->left->etype))
2878 werror (E_ILLEGAL_ADDR, "address of bit variable");
2879 goto errorTreeReturn;
2882 if (LETYPE(tree) && SPEC_SCLS (tree->left->etype) == S_REGISTER)
2884 werror (E_ILLEGAL_ADDR, "address of register variable");
2885 goto errorTreeReturn;
2888 if (IS_FUNC (LTYPE (tree)))
2890 // this ought to be ignored
2891 return (tree->left);
2894 if (IS_LITERAL(LTYPE(tree)))
2896 werror (E_ILLEGAL_ADDR, "address of literal");
2897 goto errorTreeReturn;
2902 werror (E_LVALUE_REQUIRED, "address of");
2903 goto errorTreeReturn;
2906 DCL_TYPE (p) = POINTER;
2907 else if (SPEC_SCLS (tree->left->etype) == S_CODE)
2908 DCL_TYPE (p) = CPOINTER;
2909 else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
2910 DCL_TYPE (p) = FPOINTER;
2911 else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
2912 DCL_TYPE (p) = PPOINTER;
2913 else if (SPEC_SCLS (tree->left->etype) == S_IDATA)
2914 DCL_TYPE (p) = IPOINTER;
2915 else if (SPEC_SCLS (tree->left->etype) == S_EEPROM)
2916 DCL_TYPE (p) = EEPPOINTER;
2917 else if (SPEC_OCLS(tree->left->etype))
2918 DCL_TYPE (p) = PTR_TYPE(SPEC_OCLS(tree->left->etype));
2920 DCL_TYPE (p) = POINTER;
2922 if (IS_AST_SYM_VALUE (tree->left))
2924 AST_SYMBOL (tree->left)->addrtaken = 1;
2925 AST_SYMBOL (tree->left)->allocreq = 1;
2928 p->next = LTYPE (tree);
2930 TETYPE (tree) = getSpec (TTYPE (tree));
2935 if (IS_AST_OP (tree->left) && tree->left->opval.op == PTR_OP
2936 && IS_AST_VALUE (tree->left->left) && !IS_AST_SYM_VALUE (tree->left->left))
2938 symbol *element = getStructElement (SPEC_STRUCT (LETYPE(tree->left)),
2939 AST_SYMBOL(tree->left->right));
2940 AST_VALUE(tree) = valPlus(AST_VALUE(tree->left->left),
2941 valueFromLit(element->offset));
2944 tree->type = EX_VALUE;
2945 tree->values.literalFromCast = 1;
2951 /*------------------------------------------------------------------*/
2952 /*----------------------------*/
2954 /*----------------------------*/
2956 /* if the rewrite succeeds then don't go any further */
2958 ast *wtree = optimizeRRCRLC (tree);
2960 return decorateType (wtree, RESULT_TYPE_NONE);
2962 wtree = optimizeSWAP (tree);
2964 return decorateType (wtree, RESULT_TYPE_NONE);
2967 /* if left is a literal exchange left & right */
2968 if (IS_LITERAL (LTYPE (tree)))
2970 ast *tTree = tree->left;
2971 tree->left = tree->right;
2972 tree->right = tTree;
2975 /* if right is a literal and */
2976 /* we can find a 2nd literal in an or-tree then */
2977 /* rearrange the tree */
2978 if (IS_LITERAL (RTYPE (tree)))
2981 ast *litTree = searchLitOp (tree, &parent, "|");
2985 ast *tTree = litTree->left;
2986 litTree->left = tree->right;
2987 tree->right = tTree;
2988 /* both operands in tTree are literal now */
2989 decorateType (parent, resultType);
2994 /*------------------------------------------------------------------*/
2995 /*----------------------------*/
2997 /*----------------------------*/
2999 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
3001 werror (E_BITWISE_OP);
3002 werror (W_CONTINUE, "left & right types are ");
3003 printTypeChain (LTYPE (tree), stderr);
3004 fprintf (stderr, ",");
3005 printTypeChain (RTYPE (tree), stderr);
3006 fprintf (stderr, "\n");
3007 goto errorTreeReturn;
3010 /* if they are both literal then rewrite the tree */
3011 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3013 tree->type = EX_VALUE;
3014 tree->opval.val = valBitwise (valFromType (LETYPE (tree)),
3015 valFromType (RETYPE (tree)),
3017 tree->right = tree->left = NULL;
3018 TETYPE (tree) = tree->opval.val->etype;
3019 TTYPE (tree) = tree->opval.val->type;
3023 /* if left is a literal exchange left & right */
3024 if (IS_LITERAL (LTYPE (tree)))
3026 ast *tTree = tree->left;
3027 tree->left = tree->right;
3028 tree->right = tTree;
3031 /* if right is a literal and */
3032 /* we can find a 2nd literal in a xor-tree then */
3033 /* rearrange the tree */
3034 if (IS_LITERAL (RTYPE (tree)) &&
3035 tree->opval.op == '^') /* the same source is used by 'bitwise or' */
3038 ast *litTree = searchLitOp (tree, &parent, "^");
3042 ast *tTree = litTree->left;
3043 litTree->left = tree->right;
3044 tree->right = tTree;
3045 /* both operands in litTree are literal now */
3046 decorateType (parent, resultType);
3050 LRVAL (tree) = RRVAL (tree) = 1;
3052 TTYPE (tree) = computeType (LTYPE (tree),
3056 TETYPE (tree) = getSpec (TTYPE (tree));
3060 /*------------------------------------------------------------------*/
3061 /*----------------------------*/
3063 /*----------------------------*/
3065 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
3067 werror (E_INVALID_OP, "divide");
3068 goto errorTreeReturn;
3070 /* if they are both literal then */
3071 /* rewrite the tree */
3072 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3074 tree->type = EX_VALUE;
3075 tree->opval.val = valDiv (valFromType (LETYPE (tree)),
3076 valFromType (RETYPE (tree)));
3077 tree->right = tree->left = NULL;
3078 TETYPE (tree) = getSpec (TTYPE (tree) =
3079 tree->opval.val->type);
3083 LRVAL (tree) = RRVAL (tree) = 1;
3085 TETYPE (tree) = getSpec (TTYPE (tree) =
3086 computeType (LTYPE (tree),
3091 /* if right is a literal and */
3092 /* left is also a division by a literal then */
3093 /* rearrange the tree */
3094 if (IS_LITERAL (RTYPE (tree))
3095 /* avoid infinite loop */
3096 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 1)
3099 ast *litTree = searchLitOp (tree, &parent, "/");
3102 if (IS_LITERAL (RTYPE (litTree)))
3106 litTree->right = newNode ('*',
3108 copyAst (tree->right));
3109 litTree->right->lineno = tree->lineno;
3111 tree->right->opval.val = constVal ("1");
3112 decorateType (parent, resultType);
3116 /* litTree->left is literal: no gcse possible.
3117 We can't call decorateType(parent, RESULT_TYPE_NONE), because
3118 this would cause an infinit loop. */
3119 parent->decorated = 1;
3120 decorateType (litTree, resultType);
3127 /*------------------------------------------------------------------*/
3128 /*----------------------------*/
3130 /*----------------------------*/
3132 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
3134 werror (E_BITWISE_OP);
3135 werror (W_CONTINUE, "left & right types are ");
3136 printTypeChain (LTYPE (tree), stderr);
3137 fprintf (stderr, ",");
3138 printTypeChain (RTYPE (tree), stderr);
3139 fprintf (stderr, "\n");
3140 goto errorTreeReturn;
3142 /* if they are both literal then */
3143 /* rewrite the tree */
3144 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3146 tree->type = EX_VALUE;
3147 tree->opval.val = valMod (valFromType (LETYPE (tree)),
3148 valFromType (RETYPE (tree)));
3149 tree->right = tree->left = NULL;
3150 TETYPE (tree) = getSpec (TTYPE (tree) =
3151 tree->opval.val->type);
3154 LRVAL (tree) = RRVAL (tree) = 1;
3155 TETYPE (tree) = getSpec (TTYPE (tree) =
3156 computeType (LTYPE (tree),
3162 /*------------------------------------------------------------------*/
3163 /*----------------------------*/
3164 /* address dereference */
3165 /*----------------------------*/
3166 case '*': /* can be unary : if right is null then unary operation */
3169 if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3171 werror (E_PTR_REQD);
3172 goto errorTreeReturn;
3177 werror (E_LVALUE_REQUIRED, "pointer deref");
3178 goto errorTreeReturn;
3180 if (IS_ADDRESS_OF_OP(tree->left))
3182 /* replace *&obj with obj */
3183 return tree->left->left;
3185 TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
3186 TETYPE (tree) = getSpec (TTYPE (tree));
3187 /* adjust the storage class */
3188 switch (DCL_TYPE(tree->left->ftype)) {
3190 SPEC_SCLS(TETYPE(tree)) = S_DATA;
3193 SPEC_SCLS(TETYPE(tree)) = S_XDATA;
3196 SPEC_SCLS(TETYPE(tree)) = S_CODE;
3199 SPEC_SCLS (TETYPE (tree)) = 0;
3202 SPEC_SCLS(TETYPE(tree)) = S_XSTACK;
3205 SPEC_SCLS(TETYPE(tree)) = S_IDATA;
3208 SPEC_SCLS(TETYPE(tree)) = S_EEPROM;
3211 SPEC_SCLS (TETYPE (tree)) = 0;
3220 /*------------------------------------------------------------------*/
3221 /*----------------------------*/
3222 /* multiplication */
3223 /*----------------------------*/
3224 if (!IS_ARITHMETIC (LTYPE (tree)) || !IS_ARITHMETIC (RTYPE (tree)))
3226 werror (E_INVALID_OP, "multiplication");
3227 goto errorTreeReturn;
3230 /* if they are both literal then */
3231 /* rewrite the tree */
3232 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3234 tree->type = EX_VALUE;
3235 tree->opval.val = valMult (valFromType (LETYPE (tree)),
3236 valFromType (RETYPE (tree)));
3237 tree->right = tree->left = NULL;
3238 TETYPE (tree) = getSpec (TTYPE (tree) =
3239 tree->opval.val->type);
3243 /* if left is a literal exchange left & right */
3244 if (IS_LITERAL (LTYPE (tree)))
3246 ast *tTree = tree->left;
3247 tree->left = tree->right;
3248 tree->right = tTree;
3251 /* if right is a literal and */
3252 /* we can find a 2nd literal in a mul-tree then */
3253 /* rearrange the tree */
3254 if (IS_LITERAL (RTYPE (tree)))
3257 ast *litTree = searchLitOp (tree, &parent, "*");
3261 ast *tTree = litTree->left;
3262 litTree->left = tree->right;
3263 tree->right = tTree;
3264 /* both operands in litTree are literal now */
3265 decorateType (parent, resultType);
3269 LRVAL (tree) = RRVAL (tree) = 1;
3270 tree->left = addCast (tree->left, resultType, FALSE);
3271 tree->right = addCast (tree->right, resultType, FALSE);
3272 TETYPE (tree) = getSpec (TTYPE (tree) =
3273 computeType (LTYPE (tree),
3280 /*------------------------------------------------------------------*/
3281 /*----------------------------*/
3282 /* unary '+' operator */
3283 /*----------------------------*/
3288 if (!IS_ARITHMETIC (LTYPE (tree)))
3290 werror (E_UNARY_OP, '+');
3291 goto errorTreeReturn;
3294 /* if left is a literal then do it */
3295 if (IS_LITERAL (LTYPE (tree)))
3297 tree->type = EX_VALUE;
3298 tree->opval.val = valFromType (LETYPE (tree));
3300 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3304 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3308 /*------------------------------------------------------------------*/
3309 /*----------------------------*/
3311 /*----------------------------*/
3313 /* this is not a unary operation */
3314 /* if both pointers then problem */
3315 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3316 (IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree))))
3318 werror (E_PTR_PLUS_PTR);
3319 goto errorTreeReturn;
3322 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3323 !IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE (tree)))
3325 werror (E_PLUS_INVALID, "+");
3326 goto errorTreeReturn;
3329 if (!IS_ARITHMETIC (RTYPE (tree)) &&
3330 !IS_PTR (RTYPE (tree)) && !IS_ARRAY (RTYPE (tree)))
3332 werror (E_PLUS_INVALID, "+");
3333 goto errorTreeReturn;
3335 /* if they are both literal then */
3336 /* rewrite the tree */
3337 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3339 tree->type = EX_VALUE;
3340 tree->left = addCast (tree->left, resultType, TRUE);
3341 tree->right = addCast (tree->right, resultType, TRUE);
3342 tree->opval.val = valPlus (valFromType (LETYPE (tree)),
3343 valFromType (RETYPE (tree)));
3344 tree->right = tree->left = NULL;
3345 TETYPE (tree) = getSpec (TTYPE (tree) =
3346 tree->opval.val->type);
3350 /* if the right is a pointer or left is a literal
3351 xchange left & right */
3352 if (IS_ARRAY (RTYPE (tree)) ||
3353 IS_PTR (RTYPE (tree)) ||
3354 IS_LITERAL (LTYPE (tree)))
3356 ast *tTree = tree->left;
3357 tree->left = tree->right;
3358 tree->right = tTree;
3361 /* if right is a literal and */
3362 /* left is also an addition/subtraction with a literal then */
3363 /* rearrange the tree */
3364 if (IS_LITERAL (RTYPE (tree)))
3366 ast *litTree, *parent;
3367 litTree = searchLitOp (tree, &parent, "+-");
3370 if (litTree->opval.op == '+')
3374 ast *tTree = litTree->left;
3375 litTree->left = tree->right;
3376 tree->right = tree->left;
3379 else if (litTree->opval.op == '-')
3381 if (IS_LITERAL (RTYPE (litTree)))
3385 ast *tTree = litTree->left;
3386 litTree->left = tree->right;
3387 tree->right = tTree;
3393 ast *tTree = litTree->right;
3394 litTree->right = tree->right;
3395 tree->right = tTree;
3396 litTree->opval.op = '+';
3397 tree->opval.op = '-';
3400 decorateType (parent, resultType);
3404 LRVAL (tree) = RRVAL (tree) = 1;
3405 /* if the left is a pointer */
3406 if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) )
3407 TETYPE (tree) = getSpec (TTYPE (tree) =
3411 tree->left = addCast (tree->left, resultType, TRUE);
3412 tree->right = addCast (tree->right, resultType, TRUE);
3413 TETYPE (tree) = getSpec (TTYPE (tree) =
3414 computeType (LTYPE (tree),
3422 /*------------------------------------------------------------------*/
3423 /*----------------------------*/
3425 /*----------------------------*/
3426 case '-': /* can be unary */
3427 /* if right is null then unary */
3431 if (!IS_ARITHMETIC (LTYPE (tree)))
3433 werror (E_UNARY_OP, tree->opval.op);
3434 goto errorTreeReturn;
3437 /* if left is a literal then do it */
3438 if (IS_LITERAL (LTYPE (tree)))
3440 tree->type = EX_VALUE;
3441 tree->opval.val = valUnaryPM (valFromType (LETYPE (tree)));
3443 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3446 tree->left = addCast (tree->left, resultType, TRUE);
3447 TETYPE (tree) = getSpec (TTYPE (tree) =
3448 computeType (LTYPE (tree),
3456 /*------------------------------------------------------------------*/
3457 /*----------------------------*/
3459 /*----------------------------*/
3461 if (!(IS_PTR (LTYPE (tree)) ||
3462 IS_ARRAY (LTYPE (tree)) ||
3463 IS_ARITHMETIC (LTYPE (tree))))
3465 werror (E_PLUS_INVALID, "-");
3466 goto errorTreeReturn;
3469 if (!(IS_PTR (RTYPE (tree)) ||
3470 IS_ARRAY (RTYPE (tree)) ||
3471 IS_ARITHMETIC (RTYPE (tree))))
3473 werror (E_PLUS_INVALID, "-");
3474 goto errorTreeReturn;
3477 if ((IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) &&
3478 !(IS_PTR (RTYPE (tree)) || IS_ARRAY (RTYPE (tree)) ||
3479 IS_INTEGRAL (RTYPE (tree))))
3481 werror (E_PLUS_INVALID, "-");
3482 goto errorTreeReturn;
3485 /* if they are both literal then */
3486 /* rewrite the tree */
3487 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3489 tree->type = EX_VALUE;
3490 tree->left = addCast (tree->left, resultType, TRUE);
3491 tree->right = addCast (tree->right, resultType, TRUE);
3492 tree->opval.val = valMinus (valFromType (LETYPE (tree)),
3493 valFromType (RETYPE (tree)));
3494 tree->right = tree->left = NULL;
3495 TETYPE (tree) = getSpec (TTYPE (tree) =
3496 tree->opval.val->type);
3500 /* if the left & right are equal then zero */
3501 if (isAstEqual (tree->left, tree->right))
3503 tree->type = EX_VALUE;
3504 tree->left = tree->right = NULL;
3505 tree->opval.val = constVal ("0");
3506 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3510 /* if both of them are pointers or arrays then */
3511 /* the result is going to be an integer */
3512 if ((IS_ARRAY (LTYPE (tree)) || IS_PTR (LTYPE (tree))) &&
3513 (IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))))
3514 TETYPE (tree) = TTYPE (tree) = newIntLink ();
3516 /* if only the left is a pointer */
3517 /* then result is a pointer */
3518 if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
3519 TETYPE (tree) = getSpec (TTYPE (tree) =
3523 tree->left = addCast (tree->left, resultType, TRUE);
3524 tree->right = addCast (tree->right, resultType, TRUE);
3526 TETYPE (tree) = getSpec (TTYPE (tree) =
3527 computeType (LTYPE (tree),
3533 LRVAL (tree) = RRVAL (tree) = 1;
3535 /* if right is a literal and */
3536 /* left is also an addition/subtraction with a literal then */
3537 /* rearrange the tree */
3538 if (IS_LITERAL (RTYPE (tree))
3539 /* avoid infinite loop */
3540 && (TYPE_UDWORD) floatFromVal (tree->right->opval.val) != 0)
3542 ast *litTree, *litParent;
3543 litTree = searchLitOp (tree, &litParent, "+-");
3546 if (litTree->opval.op == '+')
3550 ast *tTree = litTree->left;
3551 litTree->left = litTree->right;
3552 litTree->right = tree->right;
3553 tree->right = tTree;
3554 tree->opval.op = '+';
3555 litTree->opval.op = '-';
3557 else if (litTree->opval.op == '-')
3559 if (IS_LITERAL (RTYPE (litTree)))
3563 ast *tTree = litTree->left;
3564 litTree->left = tree->right;
3565 tree->right = litParent->left;
3566 litParent->left = tTree;
3567 litTree->opval.op = '+';
3569 tree->decorated = 0;
3570 decorateType (tree, resultType);
3576 ast *tTree = litTree->right;
3577 litTree->right = tree->right;
3578 tree->right = tTree;
3581 decorateType (litParent, resultType);
3586 /*------------------------------------------------------------------*/
3587 /*----------------------------*/
3589 /*----------------------------*/
3591 /* can be only integral type */
3592 if (!IS_INTEGRAL (LTYPE (tree)))
3594 werror (E_UNARY_OP, tree->opval.op);
3595 goto errorTreeReturn;
3598 /* if left is a literal then do it */
3599 if (IS_LITERAL (LTYPE (tree)))
3601 tree->type = EX_VALUE;
3602 tree->opval.val = valComplement (valFromType (LETYPE (tree)));
3604 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3605 return addCast (tree, resultType, TRUE);
3608 if (resultType == RESULT_TYPE_BIT &&
3609 IS_UNSIGNED (tree->left->etype) &&
3610 getSize (tree->left->etype) < INTSIZE)
3612 /* promotion rules are responsible for this strange result:
3613 bit -> int -> ~int -> bit
3614 uchar -> int -> ~int -> bit
3616 werror(W_COMPLEMENT);
3618 /* optimize bit-result, even if we optimize a buggy source */
3619 tree->type = EX_VALUE;
3620 tree->opval.val = constVal ("1");
3623 tree->left = addCast (tree->left, resultType, TRUE);
3625 COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
3628 /*------------------------------------------------------------------*/
3629 /*----------------------------*/
3631 /*----------------------------*/
3633 /* can be pointer */
3634 if (!IS_ARITHMETIC (LTYPE (tree)) &&
3635 !IS_PTR (LTYPE (tree)) &&
3636 !IS_ARRAY (LTYPE (tree)))
3638 werror (E_UNARY_OP, tree->opval.op);
3639 goto errorTreeReturn;
3642 /* if left is another '!' */
3643 if (tree->left->opval.op == '!')
3645 /* remove double '!!X' by 'X ? 1 : 0' */
3646 tree->opval.op = '?';
3647 tree->left = tree->left->left;
3648 tree->right = newNode (':',
3649 newAst_VALUE (constVal ("1")),
3650 newAst_VALUE (constVal ("0")));
3651 tree->right->lineno = tree->lineno;
3652 tree->decorated = 0;
3653 return decorateType (tree, resultType);
3656 /* if left is a literal then do it */
3657 if (IS_LITERAL (LTYPE (tree)))
3659 tree->type = EX_VALUE;
3660 tree->opval.val = valNot (valFromType (LETYPE (tree)));
3662 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3666 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3669 /*------------------------------------------------------------------*/
3670 /*----------------------------*/
3672 /*----------------------------*/
3676 TTYPE (tree) = LTYPE (tree);
3677 TETYPE (tree) = LETYPE (tree);
3682 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
3686 TTYPE (tree) = TETYPE (tree) = newCharLink();
3690 TTYPE (tree) = TETYPE (tree) = newIntLink();
3695 if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (tree->left->etype))
3697 werror (E_SHIFT_OP_INVALID);
3698 werror (W_CONTINUE, "left & right types are ");
3699 printTypeChain (LTYPE (tree), stderr);
3700 fprintf (stderr, ",");
3701 printTypeChain (RTYPE (tree), stderr);
3702 fprintf (stderr, "\n");
3703 goto errorTreeReturn;
3706 /* make smaller type only if it's a LEFT_OP */
3707 if (tree->opval.op == LEFT_OP)
3708 tree->left = addCast (tree->left, resultType, TRUE);
3710 /* if they are both literal then */
3711 /* rewrite the tree */
3712 if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
3714 tree->type = EX_VALUE;
3715 tree->opval.val = valShift (valFromType (LETYPE (tree)),
3716 valFromType (RETYPE (tree)),
3717 (tree->opval.op == LEFT_OP ? 1 : 0));
3718 tree->right = tree->left = NULL;
3719 TETYPE (tree) = getSpec (TTYPE (tree) =
3720 tree->opval.val->type);
3724 /* see if this is a GETBYTE operation if yes
3727 ast *otree = optimizeGetByte (tree, resultType);
3730 return decorateType (otree, RESULT_TYPE_NONE);
3733 /* see if this is a GETWORD operation if yes
3736 ast *otree = optimizeGetWord (tree, resultType);
3739 return decorateType (otree, RESULT_TYPE_NONE);
3742 LRVAL (tree) = RRVAL (tree) = 1;
3743 if (tree->opval.op == LEFT_OP)
3745 TETYPE (tree) = getSpec (TTYPE (tree) =
3746 computeType (LTYPE (tree),
3753 /* no promotion necessary */
3754 TTYPE (tree) = TETYPE (tree) = copyLinkChain (LTYPE (tree));
3755 if (IS_LITERAL (TTYPE (tree)))
3756 SPEC_SCLS (TTYPE (tree)) &= ~S_LITERAL;
3759 /* if only the right side is a literal & we are
3760 shifting more than size of the left operand then zero */
3761 if (IS_LITERAL (RTYPE (tree)) &&
3762 ((TYPE_UDWORD) floatFromVal (valFromType (RETYPE (tree)))) >=
3763 (getSize (TETYPE (tree)) * 8))
3765 if (tree->opval.op==LEFT_OP ||
3766 (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree))))
3768 lineno=tree->lineno;
3769 werror (W_SHIFT_CHANGED,
3770 (tree->opval.op == LEFT_OP ? "left" : "right"));
3771 tree->type = EX_VALUE;
3772 tree->left = tree->right = NULL;
3773 tree->opval.val = constVal ("0");
3774 TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
3781 /*------------------------------------------------------------------*/
3782 /*----------------------------*/
3784 /*----------------------------*/
3785 case CAST: /* change the type */
3786 /* cannot cast to an aggregate type */
3787 if (IS_AGGREGATE (LTYPE (tree)))
3789 werror (E_CAST_ILLEGAL);
3790 goto errorTreeReturn;
3793 /* make sure the type is complete and sane */
3794 changePointer(LTYPE(tree));
3795 checkTypeSanity(LETYPE(tree), "(cast)");
3797 /* if 'from' and 'to' are the same remove the superfluous cast, */
3798 /* this helps other optimizations */
3799 if (compareTypeExact (LTYPE(tree), RTYPE(tree), -1) == 1)
3804 /* If code memory is read only, then pointers to code memory */
3805 /* implicitly point to constants -- make this explicit */
3807 sym_link *t = LTYPE(tree);
3808 while (t && t->next)
3810 if (IS_CODEPTR(t) && port->mem.code_ro)
3812 if (IS_SPEC(t->next))
3813 SPEC_CONST (t->next) = 1;
3815 DCL_PTR_CONST (t->next) = 1;
3822 /* if the right is a literal replace the tree */
3823 if (IS_LITERAL (RETYPE (tree))) {
3824 if (!IS_PTR (LTYPE (tree))) {
3825 tree->type = EX_VALUE;
3827 valCastLiteral (LTYPE (tree),
3828 floatFromVal (valFromType (RETYPE (tree))));
3831 TTYPE (tree) = tree->opval.val->type;
3832 tree->values.literalFromCast = 1;
3833 } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
3834 ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */ {
3835 sym_link *rest = LTYPE(tree)->next;
3836 werror(W_LITERAL_GENERIC);
3837 TTYPE(tree) = newLink(DECLARATOR);
3838 DCL_TYPE(TTYPE(tree)) = FPOINTER;
3839 TTYPE(tree)->next = rest;
3840 tree->left->opval.lnk = TTYPE(tree);
3843 TTYPE (tree) = LTYPE (tree);
3847 TTYPE (tree) = LTYPE (tree);
3851 #if 0 // this is already checked, now this could be explicit
3852 /* if pointer to struct then check names */
3853 if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
3854 IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
3855 strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
3857 werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
3858 SPEC_STRUCT(LETYPE(tree))->tag);
3861 if (IS_ADDRESS_OF_OP(tree->right)
3862 && IS_AST_SYM_VALUE (tree->right->left)
3863 && SPEC_ABSA (AST_SYMBOL (tree->right->left)->etype)) {
3865 symbol * sym = AST_SYMBOL (tree->right->left);
3866 unsigned int gptype = 0;
3867 unsigned int addr = SPEC_ADDR (sym->etype);
3869 if (IS_GENPTR (LTYPE (tree)) && GPTRSIZE > FPTRSIZE)
3871 switch (SPEC_SCLS (sym->etype))
3874 gptype = GPTYPE_CODE;
3877 gptype = GPTYPE_FAR;
3881 gptype = GPTYPE_NEAR;
3884 gptype = GPTYPE_XSTACK;
3889 addr |= gptype << (8*(GPTRSIZE - 1));
3892 tree->type = EX_VALUE;
3894 valCastLiteral (LTYPE (tree), addr);
3895 TTYPE (tree) = tree->opval.val->type;
3896 TETYPE (tree) = getSpec (TTYPE (tree));
3899 tree->values.literalFromCast = 1;
3903 /* handle offsetof macro: */
3904 /* #define offsetof(TYPE, MEMBER) \ */
3905 /* ((unsigned) &((TYPE *)0)->MEMBER) */
3906 if (IS_ADDRESS_OF_OP(tree->right)
3907 && IS_AST_OP (tree->right->left)
3908 && tree->right->left->opval.op == PTR_OP
3909 && IS_AST_OP (tree->right->left->left)
3910 && tree->right->left->left->opval.op == CAST
3911 && IS_AST_LIT_VALUE(tree->right->left->left->right)) {
3913 symbol *element = getStructElement (
3914 SPEC_STRUCT (LETYPE(tree->right->left)),
3915 AST_SYMBOL(tree->right->left->right)
3919 tree->type = EX_VALUE;
3920 tree->opval.val = valCastLiteral (
3923 + floatFromVal (valFromType (RTYPE (tree->right->left->left)))
3926 TTYPE (tree) = tree->opval.val->type;
3927 TETYPE (tree) = getSpec (TTYPE (tree));
3934 /* if the right is a literal replace the tree */
3935 if (IS_LITERAL (RETYPE (tree))) {
3937 if (IS_PTR (LTYPE (tree)) && !IS_GENPTR (LTYPE (tree)) ) {
3938 /* rewrite (type *)litaddr
3940 and define type at litaddr temp
3941 (but only if type's storage class is not generic)
3943 ast *newTree = newNode ('&', NULL, NULL);
3946 TTYPE (newTree) = LTYPE (tree);
3947 TETYPE (newTree) = getSpec(LTYPE (tree));
3949 /* define a global symbol at the casted address*/
3950 sym = newSymbol(genSymName (0), 0);
3951 sym->type = LTYPE (tree)->next;
3953 sym->type = newLink (V_VOID);
3954 sym->etype = getSpec(sym->type);
3955 SPEC_SCLS (sym->etype) = sclsFromPtr (LTYPE (tree));
3956 sym->lineDef = tree->lineno;
3959 SPEC_STAT (sym->etype) = 1;
3960 SPEC_ADDR(sym->etype) = floatFromVal (valFromType (RTYPE (tree)));
3961 SPEC_ABSA(sym->etype) = 1;
3962 addSym (SymbolTab, sym, sym->name, 0, 0, 0);
3965 newTree->left = newAst_VALUE(symbolVal(sym));
3966 newTree->left->lineno = tree->lineno;
3967 LTYPE (newTree) = sym->type;
3968 LETYPE (newTree) = sym->etype;
3969 LLVAL (newTree) = 1;
3970 LRVAL (newTree) = 0;
3971 TLVAL (newTree) = 1;
3975 if (!IS_PTR (LTYPE (tree))) {
3976 tree->type = EX_VALUE;
3978 valCastLiteral (LTYPE (tree),
3979 floatFromVal (valFromType (RTYPE (tree))));
3980 TTYPE (tree) = tree->opval.val->type;
3983 tree->values.literalFromCast = 1;
3984 TETYPE (tree) = getSpec (TTYPE (tree));
3988 TTYPE (tree) = LTYPE (tree);
3992 TETYPE (tree) = getSpec (TTYPE (tree));
3996 /*------------------------------------------------------------------*/
3997 /*----------------------------*/
3998 /* logical &&, || */
3999 /*----------------------------*/
4002 /* each must be arithmetic type or be a pointer */
4003 if (!IS_PTR (LTYPE (tree)) &&
4004 !IS_ARRAY (LTYPE (tree)) &&
4005 !IS_INTEGRAL (LTYPE (tree)))
4007 werror (E_COMPARE_OP);
4008 goto errorTreeReturn;
4011 if (!IS_PTR (RTYPE (tree)) &&
4012 !IS_ARRAY (RTYPE (tree)) &&
4013 !IS_INTEGRAL (RTYPE (tree)))
4015 werror (E_COMPARE_OP);
4016 goto errorTreeReturn;
4018 /* if they are both literal then */
4019 /* rewrite the tree */
4020 if (IS_LITERAL (RTYPE (tree)) &&
4021 IS_LITERAL (LTYPE (tree)))
4023 tree->type = EX_VALUE;
4024 tree->opval.val = valLogicAndOr (valFromType (LTYPE (tree)),
4025 valFromType (RTYPE (tree)),
4027 tree->right = tree->left = NULL;
4028 TETYPE (tree) = getSpec (TTYPE (tree) =
4029 tree->opval.val->type);
4032 LRVAL (tree) = RRVAL (tree) = 1;
4033 TTYPE (tree) = TETYPE (tree) = (resultTypeProp == RESULT_TYPE_BIT) ? newBoolLink() :newCharLink();
4036 /*------------------------------------------------------------------*/
4037 /*----------------------------*/
4038 /* comparison operators */
4039 /*----------------------------*/
4047 ast *lt = optimizeCompare (tree);
4053 /* if they are pointers they must be castable */
4054 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4056 if (tree->opval.op==EQ_OP &&
4057 !IS_GENPTR(LTYPE(tree)) && IS_GENPTR(RTYPE(tree))) {
4058 // we cannot cast a gptr to a !gptr: switch the leaves
4059 struct ast *s=tree->left;
4060 tree->left=tree->right;
4063 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4065 werror (E_COMPARE_OP);
4066 fprintf (stderr, "comparing type ");
4067 printTypeChain (LTYPE (tree), stderr);
4068 fprintf (stderr, "to type ");
4069 printTypeChain (RTYPE (tree), stderr);
4070 fprintf (stderr, "\n");
4071 goto errorTreeReturn;
4074 /* else they should be promotable to one another */
4077 if (!((IS_PTR (LTYPE (tree)) && IS_LITERAL (RTYPE (tree))) ||
4078 (IS_PTR (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))))
4080 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4082 werror (E_COMPARE_OP);
4083 fprintf (stderr, "comparing type ");
4084 printTypeChain (LTYPE (tree), stderr);
4085 fprintf (stderr, "to type ");
4086 printTypeChain (RTYPE (tree), stderr);
4087 fprintf (stderr, "\n");
4088 goto errorTreeReturn;
4093 CCR_RESULT ccr_result = CCR_OK;
4095 /* if left is integral and right is literal
4096 then check constant range */
4097 if (IS_INTEGRAL(LTYPE(tree)) && IS_LITERAL(RTYPE(tree)))
4098 ccr_result = checkConstantRange (LTYPE (tree), RTYPE (tree),
4099 tree->opval.op, FALSE);
4100 if (ccr_result == CCR_OK &&
4101 IS_INTEGRAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree)))
4102 ccr_result = checkConstantRange (RTYPE (tree), LTYPE (tree),
4103 tree->opval.op, TRUE);
4106 case CCR_ALWAYS_TRUE:
4107 case CCR_ALWAYS_FALSE:
4108 if (!options.lessPedantic)
4109 werror (W_COMP_RANGE,
4110 ccr_result == CCR_ALWAYS_TRUE ? "true" : "false");
4111 return decorateType (newAst_VALUE (constVal (
4112 ccr_result == CCR_ALWAYS_TRUE ? "1" : "0")),
4120 /* if (unsigned value) > 0 then '(unsigned value) ? 1 : 0' */
4121 if (tree->opval.op == '>' &&
4122 SPEC_USIGN(LETYPE(tree)) &&
4123 IS_LITERAL(RTYPE(tree)) &&
4124 ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0)
4126 if (resultType == RESULT_TYPE_IFX)
4128 /* the parent is an ifx: */
4129 /* if (unsigned value) */
4133 /* (unsigned value) ? 1 : 0 */
4134 tree->opval.op = '?';
4135 tree->right = newNode (':',
4136 newAst_VALUE (constVal ("1")),
4137 tree->right); /* val 0 */
4138 tree->right->lineno = tree->lineno;
4139 tree->right->left->lineno = tree->lineno;
4140 tree->decorated = 0;
4141 return decorateType (tree, resultType);
4144 /* 'ifx (op == 0)' -> 'ifx (!(op))' */
4145 if (IS_LITERAL(RTYPE(tree)) &&
4146 floatFromVal (valFromType (RETYPE (tree))) == 0 &&
4147 tree->opval.op == EQ_OP &&
4148 resultType == RESULT_TYPE_IFX)
4150 tree->opval.op = '!';
4152 tree->decorated = 0;
4153 return decorateType (tree, resultType);
4156 /* if they are both literal then */
4157 /* rewrite the tree */
4158 if (IS_LITERAL (RTYPE (tree)) &&
4159 IS_LITERAL (LTYPE (tree)))
4161 tree->type = EX_VALUE;
4162 tree->opval.val = valCompare (valFromType (LETYPE (tree)),
4163 valFromType (RETYPE (tree)),
4165 tree->right = tree->left = NULL;
4166 TETYPE (tree) = getSpec (TTYPE (tree) =
4167 tree->opval.val->type);
4171 /* if one is 'signed char ' and the other one is 'unsigned char' */
4172 /* it's necessary to promote to int */
4173 if (IS_CHAR (RTYPE (tree)) && IS_CHAR (LTYPE (tree)) &&
4174 (IS_UNSIGNED (RTYPE (tree)) != IS_UNSIGNED (LTYPE (tree))))
4176 /* Literals are 'optimized' to 'unsigned char'. Try to figure out,
4177 if it's possible to use a 'signed char' */
4179 /* is left a 'unsigned char'? */
4180 if (IS_LITERAL (RTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)) &&
4181 /* the value range of a 'unsigned char' is 0...255;
4182 if the actual value is < 128 it can be changed to signed */
4183 (int) floatFromVal (valFromType (RETYPE (tree))) < 128)
4185 /* now we've got 2 'signed char'! */
4186 SPEC_USIGN (RETYPE (tree)) = 0;
4188 /* same test for the left operand: */
4189 else if (IS_LITERAL (LTYPE (tree)) && IS_UNSIGNED (LTYPE (tree)) &&
4190 (int) floatFromVal (valFromType (LETYPE (tree))) < 128)
4192 SPEC_USIGN (LETYPE (tree)) = 0;
4196 werror (W_CMP_SU_CHAR);
4197 tree->left = addCast (tree->left , RESULT_TYPE_INT, TRUE);
4198 tree->right = addCast (tree->right, RESULT_TYPE_INT, TRUE);
4202 LRVAL (tree) = RRVAL (tree) = 1;
4203 TTYPE (tree) = TETYPE (tree) = newBoolLink ();
4205 /* condition transformations */
4207 unsigned transformedOp = 0;
4209 switch (tree->opval.op)
4211 case '<': /* transform (a < b) to !(a >= b) */
4213 transformedOp = GE_OP;
4215 case '>': /* transform (a > b) to !(a <= b) */
4217 transformedOp = LE_OP;
4219 case LE_OP: /* transform (a <= b) to !(a > b) */
4221 transformedOp = '>';
4223 case GE_OP: /* transform (a >= b) to !(a < b) */
4225 transformedOp = '<';
4227 case NE_OP: /* transform (a != b) to !(a == b) */
4229 transformedOp = EQ_OP;
4231 case EQ_OP: /* transform (a == b) to !(a != b) */
4233 transformedOp = NE_OP;
4240 tree->opval.op = transformedOp;
4241 tree->decorated = 0;
4242 tree = newNode ('!', tree, NULL);
4243 tree->lineno = tree->left->lineno;
4244 return decorateType (tree, resultType);
4250 /*------------------------------------------------------------------*/
4251 /*----------------------------*/
4253 /*----------------------------*/
4254 case SIZEOF: /* evaluate wihout code generation */
4255 /* change the type to a integer */
4257 int size = getSize (tree->right->ftype);
4258 SNPRINTF(buffer, sizeof(buffer), "%d", size);
4259 if (!size && !IS_VOID(tree->right->ftype))
4260 werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
4262 tree->type = EX_VALUE;
4263 tree->opval.val = constVal (buffer);
4264 tree->right = tree->left = NULL;
4265 TETYPE (tree) = getSpec (TTYPE (tree) =
4266 tree->opval.val->type);
4269 /*------------------------------------------------------------------*/
4270 /*----------------------------*/
4272 /*----------------------------*/
4274 /* return typeof enum value */
4275 tree->type = EX_VALUE;
4278 if (IS_SPEC(tree->right->ftype)) {
4279 switch (SPEC_NOUN(tree->right->ftype)) {
4281 if (SPEC_LONG(tree->right->ftype)) typeofv = TYPEOF_LONG;
4282 else typeofv = TYPEOF_INT;
4285 typeofv = TYPEOF_FLOAT;
4288 typeofv = TYPEOF_FIXED16X16;
4291 typeofv = TYPEOF_CHAR;
4294 typeofv = TYPEOF_VOID;
4297 typeofv = TYPEOF_STRUCT;
4300 typeofv = TYPEOF_BITFIELD;
4303 typeofv = TYPEOF_BIT;
4306 typeofv = TYPEOF_SBIT;
4312 switch (DCL_TYPE(tree->right->ftype)) {
4314 typeofv = TYPEOF_POINTER;
4317 typeofv = TYPEOF_FPOINTER;
4320 typeofv = TYPEOF_CPOINTER;
4323 typeofv = TYPEOF_GPOINTER;
4326 typeofv = TYPEOF_PPOINTER;
4329 typeofv = TYPEOF_IPOINTER;
4332 typeofv = TYPEOF_ARRAY;
4335 typeofv = TYPEOF_FUNCTION;
4341 SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
4342 tree->opval.val = constVal (buffer);
4343 tree->right = tree->left = NULL;
4344 TETYPE (tree) = getSpec (TTYPE (tree) =
4345 tree->opval.val->type);
4348 /*------------------------------------------------------------------*/
4349 /*----------------------------*/
4350 /* conditional operator '?' */
4351 /*----------------------------*/
4353 /* the type is value of the colon operator (on the right) */
4354 assert (IS_COLON_OP (tree->right));
4355 /* if already known then replace the tree : optimizer will do it
4356 but faster to do it here */
4357 if (IS_LITERAL (LTYPE (tree)))
4359 if (((int) floatFromVal (valFromType (LETYPE (tree)))) != 0)
4360 return decorateType (tree->right->left, resultTypeProp);
4362 return decorateType (tree->right->right, resultTypeProp);
4366 tree->right = decorateType (tree->right, resultTypeProp);
4367 TTYPE (tree) = RTYPE (tree);
4368 TETYPE (tree) = getSpec (TTYPE (tree));
4373 /* if they don't match we have a problem */
4374 if ((compareType (LTYPE (tree), RTYPE (tree)) == 0) &&
4375 (compareType (RTYPE (tree), LTYPE (tree)) == 0))
4377 werror (E_TYPE_MISMATCH, "conditional operator", " ");
4378 goto errorTreeReturn;
4381 TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
4382 resultType, tree->opval.op);
4383 TETYPE (tree) = getSpec (TTYPE (tree));
4387 #if 0 // assignment operators are converted by the parser
4388 /*------------------------------------------------------------------*/
4389 /*----------------------------*/
4390 /* assignment operators */
4391 /*----------------------------*/
4394 /* for these it must be both must be integral */
4395 if (!IS_ARITHMETIC (LTYPE (tree)) ||
4396 !IS_ARITHMETIC (RTYPE (tree)))
4398 werror (E_OPS_INTEGRAL);
4399 goto errorTreeReturn;
4402 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4404 if (!tree->initMode && IS_CONSTANT (LTYPE (tree)))
4405 werror (E_CODE_WRITE, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4409 werror (E_LVALUE_REQUIRED, tree->opval.op==MUL_ASSIGN ? "*=" : "/=");
4410 goto errorTreeReturn;
4421 /* for these it must be both must be integral */
4422 if (!IS_INTEGRAL (LTYPE (tree)) ||
4423 !IS_INTEGRAL (RTYPE (tree)))
4425 werror (E_OPS_INTEGRAL);
4426 goto errorTreeReturn;
4429 TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree));
4431 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4432 werror (E_CODE_WRITE, "&= or |= or ^= or >>= or <<=");
4436 werror (E_LVALUE_REQUIRED, "&= or |= or ^= or >>= or <<=");
4437 goto errorTreeReturn;
4443 /*------------------------------------------------------------------*/
4444 /*----------------------------*/
4446 /*----------------------------*/
4448 if (!(IS_PTR (LTYPE (tree)) ||
4449 IS_ARITHMETIC (LTYPE (tree))))
4451 werror (E_PLUS_INVALID, "-=");
4452 goto errorTreeReturn;
4455 if (!(IS_PTR (RTYPE (tree)) ||
4456 IS_ARITHMETIC (RTYPE (tree))))
4458 werror (E_PLUS_INVALID, "-=");
4459 goto errorTreeReturn;
4462 TETYPE (tree) = getSpec (TTYPE (tree) =
4463 computeType (LTYPE (tree),
4468 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4469 werror (E_CODE_WRITE, "-=");
4473 werror (E_LVALUE_REQUIRED, "-=");
4474 goto errorTreeReturn;
4480 /*------------------------------------------------------------------*/
4481 /*----------------------------*/
4483 /*----------------------------*/
4485 /* this is not a unary operation */
4486 /* if both pointers then problem */
4487 if (IS_PTR (LTYPE (tree)) && IS_PTR (RTYPE (tree)))
4489 werror (E_PTR_PLUS_PTR);
4490 goto errorTreeReturn;
4493 if (!IS_ARITHMETIC (LTYPE (tree)) && !IS_PTR (LTYPE (tree)))
4495 werror (E_PLUS_INVALID, "+=");
4496 goto errorTreeReturn;
4499 if (!IS_ARITHMETIC (RTYPE (tree)) && !IS_PTR (RTYPE (tree)))
4501 werror (E_PLUS_INVALID, "+=");
4502 goto errorTreeReturn;
4505 TETYPE (tree) = getSpec (TTYPE (tree) =
4506 computeType (LTYPE (tree),
4511 if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
4512 werror (E_CODE_WRITE, "+=");
4516 werror (E_LVALUE_REQUIRED, "+=");
4517 goto errorTreeReturn;
4520 tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right), RESULT_TYPE_NONE);
4521 tree->opval.op = '=';
4526 /*------------------------------------------------------------------*/
4527 /*----------------------------*/
4528 /* straight assignemnt */
4529 /*----------------------------*/
4531 /* cannot be an aggregate */
4532 if (IS_AGGREGATE (LTYPE (tree)))
4534 werror (E_AGGR_ASSIGN);
4535 goto errorTreeReturn;
4538 /* they should either match or be castable */
4539 if (compareType (LTYPE (tree), RTYPE (tree)) == 0)
4541 werror (E_TYPE_MISMATCH, "assignment", " ");
4542 printFromToType(RTYPE(tree),LTYPE(tree));
4545 /* if the left side of the tree is of type void
4546 then report error */
4547 if (IS_VOID (LTYPE (tree)))
4549 werror (E_CAST_ZERO);
4550 printFromToType(RTYPE(tree), LTYPE(tree));
4553 TETYPE (tree) = getSpec (TTYPE (tree) =
4557 if (!tree->initMode ) {
4558 if (IS_CONSTANT(LTYPE(tree)))
4559 werror (E_CODE_WRITE, "=");
4563 werror (E_LVALUE_REQUIRED, "=");
4564 goto errorTreeReturn;
4569 /*------------------------------------------------------------------*/
4570 /*----------------------------*/
4571 /* comma operator */
4572 /*----------------------------*/
4574 TETYPE (tree) = getSpec (TTYPE (tree) = RTYPE (tree));
4577 /*------------------------------------------------------------------*/
4578 /*----------------------------*/
4580 /*----------------------------*/
4583 /* undo any explicit pointer derefernce; PCALL will handle it instead */
4584 if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
4586 if (tree->left->opval.op == '*' && !tree->left->right)
4587 tree->left = tree->left->left;
4590 /* require a function or pointer to function */
4591 if (!IS_FUNC (LTYPE (tree)) && !IS_FUNCPTR (LTYPE (tree)))
4593 werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
4594 goto errorTreeReturn;
4597 /* if there are parms, make sure that
4598 parms are decorate / process / reverse only once */
4600 !tree->right->decorated)
4605 if (IS_FUNCPTR (LTYPE (tree)))
4607 functype = LTYPE (tree)->next;
4608 processFuncPtrArgs (functype);
4611 functype = LTYPE (tree);
4613 if (processParms (tree->left, FUNC_ARGS(functype),
4614 &tree->right, &parmNumber, TRUE))
4616 goto errorTreeReturn;
4619 if ((options.stackAuto || IFFUNC_ISREENT (functype)) &&
4620 !IFFUNC_ISBUILTIN(functype))
4622 reverseParms (tree->right);
4625 TTYPE (tree) = functype->next;
4626 TETYPE (tree) = getSpec (TTYPE (tree));
4630 /*------------------------------------------------------------------*/
4631 /*----------------------------*/
4632 /* return statement */
4633 /*----------------------------*/
4638 if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
4640 werrorfl (tree->filename, tree->lineno, W_RETURN_MISMATCH);
4641 printFromToType (RTYPE(tree), currFunc->type->next);
4642 goto errorTreeReturn;
4645 if (IS_VOID (currFunc->type->next)
4647 !IS_VOID (RTYPE (tree)))
4649 werrorfl (tree->filename, tree->lineno, E_FUNC_VOID);
4650 goto errorTreeReturn;
4653 /* if there is going to be a casting required then add it */
4654 if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
4657 decorateType (newNode (CAST,
4658 newAst_LINK (copyLinkChain (currFunc->type->next)),
4668 if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
4670 werror (W_VOID_FUNC, currFunc->name);
4671 goto errorTreeReturn;
4674 TTYPE (tree) = TETYPE (tree) = NULL;
4677 /*------------------------------------------------------------------*/
4678 /*----------------------------*/
4679 /* switch statement */
4680 /*----------------------------*/
4682 /* the switch value must be an integer */
4683 if (!IS_INTEGRAL (LTYPE (tree)))
4685 werrorfl (tree->filename, tree->lineno, E_SWITCH_NON_INTEGER);
4686 goto errorTreeReturn;
4689 TTYPE (tree) = TETYPE (tree) = NULL;
4692 /*------------------------------------------------------------------*/
4693 /*----------------------------*/
4695 /*----------------------------*/
4697 tree->left = backPatchLabels (tree->left,
4700 TTYPE (tree) = TETYPE (tree) = NULL;
4703 /*------------------------------------------------------------------*/
4704 /*----------------------------*/
4706 /*----------------------------*/
4709 AST_FOR (tree, initExpr) = decorateType (
4710 resolveSymbols (AST_FOR (tree, initExpr)), RESULT_TYPE_NONE);
4711 AST_FOR (tree, condExpr) = decorateType (
4712 resolveSymbols (AST_FOR (tree, condExpr)), RESULT_TYPE_NONE);
4713 AST_FOR (tree, loopExpr) = decorateType (
4714 resolveSymbols (AST_FOR (tree, loopExpr)), RESULT_TYPE_NONE);
4716 /* if the for loop is reversible then
4717 reverse it otherwise do what we normally
4723 if (isLoopReversible (tree, &sym, &init, &end))
4724 return reverseLoop (tree, sym, init, end);
4726 return decorateType (createFor (AST_FOR (tree, trueLabel),
4727 AST_FOR (tree, continueLabel),
4728 AST_FOR (tree, falseLabel),
4729 AST_FOR (tree, condLabel),
4730 AST_FOR (tree, initExpr),
4731 AST_FOR (tree, condExpr),
4732 AST_FOR (tree, loopExpr),
4733 tree->left), RESULT_TYPE_NONE);
4736 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
4737 "node PARAM shouldn't be processed here");
4738 /* but in processParams() */
4741 TTYPE (tree) = TETYPE (tree) = NULL;
4745 /* some error found this tree will be killed */
4747 TTYPE (tree) = TETYPE (tree) = newCharLink ();
4748 tree->opval.op = NULLOP;
4754 /*-----------------------------------------------------------------*/
4755 /* sizeofOp - processes size of operation */
4756 /*-----------------------------------------------------------------*/
4758 sizeofOp (sym_link * type)
4763 /* make sure the type is complete and sane */
4764 checkTypeSanity(type, "(sizeof)");
4766 /* get the size and convert it to character */
4767 SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
4768 if (!size && !IS_VOID(type))
4769 werror (E_SIZEOF_INCOMPLETE_TYPE);
4771 /* now convert into value */
4772 return constVal (buff);
4776 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
4777 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
4778 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
4779 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
4780 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
4781 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
4782 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
4784 /*-----------------------------------------------------------------*/
4785 /* backPatchLabels - change and or not operators to flow control */
4786 /*-----------------------------------------------------------------*/
4788 backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
4794 /* while-loops insert a label between the IFX and the condition,
4795 therefore look behind the label too */
4796 if (tree->opval.op == LABEL &&
4797 IS_ANDORNOT (tree->right))
4799 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4803 if (!(IS_ANDORNOT (tree)))
4806 /* if this an and */
4809 static int localLbl = 0;
4812 SNPRINTF(buffer, sizeof(buffer), "_andif_%d", localLbl++);
4813 localLabel = newSymbol (buffer, NestLevel);
4815 tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
4817 /* if left is already a IFX then just change the if true label in that */
4818 if (!IS_IFX (tree->left))
4819 tree->left = newIfxNode (tree->left, localLabel, falseLabel);
4821 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4822 /* right is a IFX then just join */
4823 if (IS_IFX (tree->right))
4824 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4826 tree->right = createLabel (localLabel, tree->right);
4827 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4829 return newNode (NULLOP, tree->left, tree->right);
4832 /* if this is an or operation */
4835 static int localLbl = 0;
4838 SNPRINTF(buffer, sizeof(buffer), "_orif_%d", localLbl++);
4839 localLabel = newSymbol (buffer, NestLevel);
4841 tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
4843 /* if left is already a IFX then just change the if true label in that */
4844 if (!IS_IFX (tree->left))
4845 tree->left = newIfxNode (tree->left, trueLabel, localLabel);
4847 tree->right = backPatchLabels (tree->right, trueLabel, falseLabel);
4848 /* right is a IFX then just join */
4849 if (IS_IFX (tree->right))
4850 return newNode (NULLOP, tree->left, createLabel (localLabel, tree->right));
4852 tree->right = createLabel (localLabel, tree->right);
4853 tree->right = newIfxNode (tree->right, trueLabel, falseLabel);
4855 return newNode (NULLOP, tree->left, tree->right);
4861 /* call with exchanged labels */
4862 tree->left = backPatchLabels (tree->left, falseLabel, trueLabel);
4864 /* if left isn't already a IFX */
4865 if (!IS_IFX (tree->left))
4867 tree->left = newNode (IFX, tree->left, NULL);
4868 tree->left->trueLabel = falseLabel;
4869 tree->left->falseLabel = trueLabel;
4876 tree->trueLabel = trueLabel;
4877 tree->falseLabel = falseLabel;
4884 /*-----------------------------------------------------------------*/
4885 /* createBlock - create expression tree for block */
4886 /*-----------------------------------------------------------------*/
4888 createBlock (symbol * decl, ast * body)
4892 /* if the block has nothing */
4896 ex = newNode (BLOCK, NULL, body);
4897 ex->values.sym = decl;
4904 /*-----------------------------------------------------------------*/
4905 /* createLabel - creates the expression tree for labels */
4906 /*-----------------------------------------------------------------*/
4908 createLabel (symbol * label, ast * stmnt)
4911 char name[SDCC_NAME_MAX + 1];
4914 /* must create fresh symbol if the symbol name */
4915 /* exists in the symbol table, since there can */
4916 /* be a variable with the same name as the labl */
4917 if ((csym = findSym (SymbolTab, NULL, label->name)) &&
4918 (csym->level == label->level))
4919 label = newSymbol (label->name, label->level);
4921 /* change the name before putting it in add _ */
4922 SNPRINTF(name, sizeof(name), "%s", label->name);
4924 /* put the label in the LabelSymbol table */
4925 /* but first check if a label of the same */
4927 if ((csym = findSym (LabelTab, NULL, name)))
4928 werror (E_DUPLICATE_LABEL, label->name);
4930 addSym (LabelTab, label, name, label->level, 0, 0);
4934 label->key = labelKey++;
4935 rValue = newNode (LABEL, newAst_VALUE (symbolVal (label)), stmnt);
4941 /*-----------------------------------------------------------------*/
4942 /* createCase - generates the parsetree for a case statement */
4943 /*-----------------------------------------------------------------*/
4945 createCase (ast * swStat, ast * caseVal, ast * stmnt)
4947 char caseLbl[SDCC_NAME_MAX + 1];
4951 /* if the switch statement does not exist */
4952 /* then case is out of context */
4955 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONTEXT);
4959 caseVal = decorateType (resolveSymbols (caseVal), RESULT_TYPE_NONE);
4960 /* if not a constant then error */
4961 if (!IS_LITERAL (caseVal->ftype))
4963 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_CONSTANT);
4967 /* if not a integer than error */
4968 if (!IS_INTEGRAL (caseVal->ftype))
4970 werrorfl (caseVal->filename, caseVal->lineno, E_CASE_NON_INTEGER);
4974 /* find the end of the switch values chain */
4975 if (!(val = swStat->values.switchVals.swVals))
4976 swStat->values.switchVals.swVals = caseVal->opval.val;
4979 /* also order the cases according to value */
4981 int cVal = (int) floatFromVal (caseVal->opval.val);
4982 while (val && (int) floatFromVal (val) < cVal)
4988 /* if we reached the end then */
4991 pval->next = caseVal->opval.val;
4993 else if ((int) floatFromVal (val) == cVal)
4995 werrorfl (caseVal->filename, caseVal->lineno, E_DUPLICATE_LABEL,
5001 /* we found a value greater than */
5002 /* the current value we must add this */
5003 /* before the value */
5004 caseVal->opval.val->next = val;
5006 /* if this was the first in chain */
5007 if (swStat->values.switchVals.swVals == val)
5008 swStat->values.switchVals.swVals =
5011 pval->next = caseVal->opval.val;
5016 /* create the case label */
5017 SNPRINTF(caseLbl, sizeof(caseLbl),
5019 swStat->values.switchVals.swNum,
5020 (int) floatFromVal (caseVal->opval.val));
5022 rexpr = createLabel (newSymbol (caseLbl, 0), stmnt);
5027 /*-----------------------------------------------------------------*/
5028 /* createDefault - creates the parse tree for the default statement */
5029 /*-----------------------------------------------------------------*/
5031 createDefault (ast * swStat, ast * defaultVal, ast * stmnt)
5033 char defLbl[SDCC_NAME_MAX + 1];
5035 /* if the switch statement does not exist */
5036 /* then case is out of context */
5039 werrorfl (defaultVal->filename, defaultVal->lineno, E_CASE_CONTEXT);
5043 if (swStat->values.switchVals.swDefault)
5045 werrorfl (defaultVal->filename, defaultVal->lineno, E_DUPLICATE_LABEL,
5050 /* turn on the default flag */
5051 swStat->values.switchVals.swDefault = 1;
5053 /* create the label */
5054 SNPRINTF (defLbl, sizeof(defLbl),
5055 "_default_%d", swStat->values.switchVals.swNum);
5056 return createLabel (newSymbol (defLbl, 0), stmnt);
5059 /*-----------------------------------------------------------------*/
5060 /* createIf - creates the parsetree for the if statement */
5061 /*-----------------------------------------------------------------*/
5063 createIf (ast * condAst, ast * ifBody, ast * elseBody)
5065 static int Lblnum = 0;
5067 symbol *ifTrue, *ifFalse, *ifEnd;
5069 /* if neither exists */
5070 if (!elseBody && !ifBody) {
5071 // if there are no side effects (i++, j() etc)
5072 if (!hasSEFcalls(condAst)) {
5077 /* create the labels */
5078 SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
5079 ifFalse = newSymbol (buffer, NestLevel);
5080 /* if no else body then end == false */
5085 SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
5086 ifEnd = newSymbol (buffer, NestLevel);
5089 SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
5090 ifTrue = newSymbol (buffer, NestLevel);
5094 /* attach the ifTrue label to the top of it body */
5095 ifBody = createLabel (ifTrue, ifBody);
5096 /* attach a goto end to the ifBody if else is present */
5099 ifBody = newNode (NULLOP, ifBody,
5101 newAst_VALUE (symbolVal (ifEnd)),
5103 /* put the elseLabel on the else body */
5104 elseBody = createLabel (ifFalse, elseBody);
5105 /* out the end at the end of the body */
5106 elseBody = newNode (NULLOP,
5108 createLabel (ifEnd, NULL));
5112 ifBody = newNode (NULLOP, ifBody,
5113 createLabel (ifFalse, NULL));
5115 condAst = backPatchLabels (condAst, ifTrue, ifFalse);
5116 if (IS_IFX (condAst))
5119 ifTree = newIfxNode (condAst, ifTrue, ifFalse);
5121 return newNode (NULLOP, ifTree,
5122 newNode (NULLOP, ifBody, elseBody));
5126 /*-----------------------------------------------------------------*/
5127 /* createDo - creates parse tree for do */
5130 /* _docontinue_n: */
5131 /* condition_expression +-> trueLabel -> _dobody_n */
5133 /* +-> falseLabel-> _dobreak_n */
5135 /*-----------------------------------------------------------------*/
5137 createDo (symbol * trueLabel, symbol * continueLabel,
5138 symbol * falseLabel, ast * condAst, ast * doBody)
5143 /* if the body does not exist then it is simple */
5146 condAst = backPatchLabels (condAst, continueLabel, NULL);
5147 doTree = (IS_IFX (condAst) ? createLabel (continueLabel, condAst)
5148 : newNode (IFX, createLabel (continueLabel, condAst), NULL));
5149 doTree->trueLabel = continueLabel;
5150 doTree->falseLabel = NULL;
5154 /* otherwise we have a body */
5155 condAst = backPatchLabels (condAst, trueLabel, falseLabel);
5157 /* attach the body label to the top */
5158 doBody = createLabel (trueLabel, doBody);
5159 /* attach the continue label to end of body */
5160 doBody = newNode (NULLOP, doBody,
5161 createLabel (continueLabel, NULL));
5163 /* now put the break label at the end */
5164 if (IS_IFX (condAst))
5167 doTree = newIfxNode (condAst, trueLabel, falseLabel);
5169 doTree = newNode (NULLOP, doTree, createLabel (falseLabel, NULL));
5171 /* putting it together */
5172 return newNode (NULLOP, doBody, doTree);
5175 /*-----------------------------------------------------------------*/
5176 /* createFor - creates parse tree for 'for' statement */
5179 /* condExpr +-> trueLabel -> _forbody_n */
5181 /* +-> falseLabel-> _forbreak_n */
5184 /* _forcontinue_n: */
5186 /* goto _forcond_n ; */
5188 /*-----------------------------------------------------------------*/
5190 createFor (symbol * trueLabel, symbol * continueLabel,
5191 symbol * falseLabel, symbol * condLabel,
5192 ast * initExpr, ast * condExpr, ast * loopExpr,
5197 /* if loopexpression not present then we can generate it */
5198 /* the same way as a while */
5200 return newNode (NULLOP, initExpr,
5201 createWhile (trueLabel, continueLabel,
5202 falseLabel, condExpr, forBody));
5203 /* vanilla for statement */
5204 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
5206 if (condExpr && !IS_IFX (condExpr))
5207 condExpr = newIfxNode (condExpr, trueLabel, falseLabel);
5210 /* attach condition label to condition */
5211 condExpr = createLabel (condLabel, condExpr);
5213 /* attach body label to body */
5214 forBody = createLabel (trueLabel, forBody);
5216 /* attach continue to forLoop expression & attach */
5217 /* goto the forcond @ and of loopExpression */
5218 loopExpr = createLabel (continueLabel,
5222 newAst_VALUE (symbolVal (condLabel)),
5224 /* now start putting them together */
5225 forTree = newNode (NULLOP, initExpr, condExpr);
5226 forTree = newNode (NULLOP, forTree, forBody);
5227 forTree = newNode (NULLOP, forTree, loopExpr);
5228 /* finally add the break label */
5229 forTree = newNode (NULLOP, forTree,
5230 createLabel (falseLabel, NULL));
5234 /*-----------------------------------------------------------------*/
5235 /* createWhile - creates parse tree for while statement */
5236 /* the while statement will be created as follows */
5238 /* _while_continue_n: */
5239 /* condition_expression +-> trueLabel -> _while_boby_n */
5241 /* +-> falseLabel -> _while_break_n */
5242 /* _while_body_n: */
5244 /* goto _while_continue_n */
5245 /* _while_break_n: */
5246 /*-----------------------------------------------------------------*/
5248 createWhile (symbol * trueLabel, symbol * continueLabel,
5249 symbol * falseLabel, ast * condExpr, ast * whileBody)
5253 /* put the continue label */
5254 condExpr = backPatchLabels (condExpr, trueLabel, falseLabel);
5255 condExpr = createLabel (continueLabel, condExpr);
5256 condExpr->lineno = 0;
5258 /* put the body label in front of the body */
5259 whileBody = createLabel (trueLabel, whileBody);
5260 whileBody->lineno = 0;
5261 /* put a jump to continue at the end of the body */
5262 /* and put break label at the end of the body */
5263 whileBody = newNode (NULLOP,
5266 newAst_VALUE (symbolVal (continueLabel)),
5267 createLabel (falseLabel, NULL)));
5269 /* put it all together */
5270 if (IS_IFX (condExpr))
5271 whileTree = condExpr;
5274 whileTree = newNode (IFX, condExpr, NULL);
5275 /* put the true & false labels in place */
5276 whileTree->trueLabel = trueLabel;
5277 whileTree->falseLabel = falseLabel;
5280 return newNode (NULLOP, whileTree, whileBody);
5283 /*-----------------------------------------------------------------*/
5284 /* isShiftRightLitVal _BitAndLitVal - helper function */
5285 /*-----------------------------------------------------------------*/
5287 isShiftRightLitVal_BitAndLitVal (ast * tree)
5289 /* if this is not a bit and */
5290 if (!IS_BITAND (tree))
5293 /* will look for tree of the form
5294 ( expr >> litval2) & litval1 */
5295 if (!IS_AST_LIT_VALUE (tree->right))
5298 if (!IS_RIGHT_OP (tree->left))
5301 if (!IS_AST_LIT_VALUE (tree->left->right))
5304 return tree->left->left;
5307 /*-----------------------------------------------------------------*/
5308 /* isBitAndPowOf2 - helper function */
5309 /*-----------------------------------------------------------------*/
5311 isBitAndPow2 (ast * tree)
5313 /* if this is not a bit and */
5314 if (!IS_BITAND (tree))
5317 /* will look for tree of the form
5318 ( expr & (1 << litval) */
5319 if (!IS_AST_LIT_VALUE (tree->right))
5322 return powof2 ((TYPE_UDWORD)AST_LIT_VALUE (tree->right));
5325 /*-----------------------------------------------------------------*/
5326 /* optimizeGetHbit - get highest order bit of the expression */
5327 /*-----------------------------------------------------------------*/
5329 optimizeGetHbit (ast * tree, RESULT_TYPE resultType)
5334 expr = isShiftRightLitVal_BitAndLitVal(tree);
5337 if ((AST_LIT_VALUE (tree->right) != 1) ||
5338 ((i = (int) AST_LIT_VALUE (tree->left->right)) !=
5339 (j = (getSize (TTYPE (expr)) * 8 - 1))))
5342 if (!expr && (resultType == RESULT_TYPE_BIT))
5345 if (isBitAndPow2 (tree) != (signed)getSize (TTYPE (expr)) * 8 - 1)
5351 /* make sure the port supports GETHBIT */
5352 if (port->hasExtBitOp
5353 && !port->hasExtBitOp(GETHBIT, getSize (TTYPE (expr))))
5356 return decorateType (newNode (GETHBIT, expr, NULL), RESULT_TYPE_NONE);
5359 /*-----------------------------------------------------------------*/
5360 /* optimizeGetAbit - get a single bit of the expression */
5361 /*-----------------------------------------------------------------*/
5363 optimizeGetAbit (ast * tree, RESULT_TYPE resultType)
5368 expr = isShiftRightLitVal_BitAndLitVal(tree);
5371 if (AST_LIT_VALUE (tree->right) != 1)
5373 count = tree->left->right;
5375 if (!expr && (resultType == RESULT_TYPE_BIT))
5377 int p2 = isBitAndPow2 (tree);
5381 count = newAst_VALUE (valueFromLit (p2));
5387 /* make sure the port supports GETABIT */
5388 if (port->hasExtBitOp
5389 && !port->hasExtBitOp(GETABIT, getSize (TTYPE (expr))))
5392 return decorateType (newNode (GETABIT, expr, count), RESULT_TYPE_NONE);
5396 /*-----------------------------------------------------------------*/
5397 /* optimizeGetByte - get a byte of the expression */
5398 /*-----------------------------------------------------------------*/
5400 optimizeGetByte (ast * tree, RESULT_TYPE resultType)
5406 expr = isShiftRightLitVal_BitAndLitVal(tree);
5409 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5410 count = tree->left->right;
5411 if (AST_LIT_VALUE (tree->right) != 0xFF)
5414 if (!expr && resultType == RESULT_TYPE_CHAR)
5416 /* if this is a right shift over a multiple of 8 */
5417 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5419 i = (unsigned int) AST_LIT_VALUE (tree->right);
5420 count = tree->right;
5424 if (!expr || (i == 0) || (i % 8) || (i >= getSize (TTYPE (expr)) * 8))
5427 /* make sure the port supports GETBYTE */
5428 if (port->hasExtBitOp
5429 && !port->hasExtBitOp(GETBYTE, getSize (TTYPE (expr))))
5432 return decorateType (newNode (GETBYTE, expr, count), RESULT_TYPE_NONE);
5435 /*-----------------------------------------------------------------*/
5436 /* optimizeGetWord - get two bytes of the expression */
5437 /*-----------------------------------------------------------------*/
5439 optimizeGetWord (ast * tree, RESULT_TYPE resultType)
5445 expr = isShiftRightLitVal_BitAndLitVal(tree);
5448 i = (unsigned int) AST_LIT_VALUE (tree->left->right);
5449 count = tree->left->right;
5450 if (AST_LIT_VALUE (tree->right) != 0xFFFF)
5453 if (!expr && resultType == RESULT_TYPE_INT)
5455 /* if this is a right shift over a multiple of 8 */
5456 if (IS_RIGHT_OP (tree) && IS_AST_LIT_VALUE (tree->right))
5458 i = (unsigned int) AST_LIT_VALUE (tree->right);
5459 count = tree->right;
5463 if (!expr || (i == 0) || (i % 8) || (i >= (getSize (TTYPE (expr))-1) * 8))
5466 /* make sure the port supports GETWORD */
5467 if (port->hasExtBitOp
5468 && !port->hasExtBitOp(GETWORD, getSize (TTYPE (expr))))
5471 return decorateType (newNode (GETWORD, expr, count), RESULT_TYPE_NONE);
5474 /*-----------------------------------------------------------------*/
5475 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
5476 /*-----------------------------------------------------------------*/
5478 optimizeRRCRLC (ast * root)
5480 /* will look for trees of the form
5481 (?expr << 1) | (?expr >> 7) or
5482 (?expr >> 7) | (?expr << 1) will make that
5483 into a RLC : operation ..
5485 (?expr >> 1) | (?expr << 7) or
5486 (?expr << 7) | (?expr >> 1) will make that
5487 into a RRC operation
5488 note : by 7 I mean (number of bits required to hold the
5490 /* if the root operation is not a | operation then not */
5491 if (!IS_BITOR (root))
5494 /* I have to think of a better way to match patterns this sucks */
5495 /* that aside let's start looking for the first case : I use a
5496 negative check a lot to improve the efficiency */
5497 /* (?expr << 1) | (?expr >> 7) */
5498 if (IS_LEFT_OP (root->left) &&
5499 IS_RIGHT_OP (root->right))
5502 if (!SPEC_USIGN (TETYPE (root->left->left)))
5505 if (!IS_AST_LIT_VALUE (root->left->right) ||
5506 !IS_AST_LIT_VALUE (root->right->right))
5509 /* make sure it is the same expression */
5510 if (!isAstEqual (root->left->left,
5514 if (AST_LIT_VALUE (root->left->right) != 1)
5517 if (AST_LIT_VALUE (root->right->right) !=
5518 (getSize (TTYPE (root->left->left)) * 8 - 1))
5521 /* make sure the port supports RLC */
5522 if (port->hasExtBitOp
5523 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5526 /* whew got the first case : create the AST */
5527 return newNode (RLC, root->left->left, NULL);
5531 /* check for second case */
5532 /* (?expr >> 7) | (?expr << 1) */
5533 if (IS_LEFT_OP (root->right) &&
5534 IS_RIGHT_OP (root->left))
5537 if (!SPEC_USIGN (TETYPE (root->left->left)))
5540 if (!IS_AST_LIT_VALUE (root->left->right) ||
5541 !IS_AST_LIT_VALUE (root->right->right))
5544 /* make sure it is the same symbol */
5545 if (!isAstEqual (root->left->left,
5549 if (AST_LIT_VALUE (root->right->right) != 1)
5552 if (AST_LIT_VALUE (root->left->right) !=
5553 (getSize (TTYPE (root->left->left)) * 8 - 1))
5556 /* make sure the port supports RLC */
5557 if (port->hasExtBitOp
5558 && !port->hasExtBitOp(RLC, getSize (TTYPE (root->left->left))))
5561 /* whew got the first case : create the AST */
5562 return newNode (RLC, root->left->left, NULL);
5567 /* third case for RRC */
5568 /* (?symbol >> 1) | (?symbol << 7) */
5569 if (IS_LEFT_OP (root->right) &&
5570 IS_RIGHT_OP (root->left))
5573 if (!SPEC_USIGN (TETYPE (root->left->left)))
5576 if (!IS_AST_LIT_VALUE (root->left->right) ||
5577 !IS_AST_LIT_VALUE (root->right->right))
5580 /* make sure it is the same symbol */
5581 if (!isAstEqual (root->left->left,
5585 if (AST_LIT_VALUE (root->left->right) != 1)
5588 if (AST_LIT_VALUE (root->right->right) !=
5589 (getSize (TTYPE (root->left->left)) * 8 - 1))
5592 /* make sure the port supports RRC */
5593 if (port->hasExtBitOp
5594 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5597 /* whew got the first case : create the AST */
5598 return newNode (RRC, root->left->left, NULL);
5602 /* fourth and last case for now */
5603 /* (?symbol << 7) | (?symbol >> 1) */
5604 if (IS_RIGHT_OP (root->right) &&
5605 IS_LEFT_OP (root->left))
5608 if (!SPEC_USIGN (TETYPE (root->left->left)))
5611 if (!IS_AST_LIT_VALUE (root->left->right) ||
5612 !IS_AST_LIT_VALUE (root->right->right))
5615 /* make sure it is the same symbol */
5616 if (!isAstEqual (root->left->left,
5620 if (AST_LIT_VALUE (root->right->right) != 1)
5623 if (AST_LIT_VALUE (root->left->right) !=
5624 (getSize (TTYPE (root->left->left)) * 8 - 1))
5627 /* make sure the port supports RRC */
5628 if (port->hasExtBitOp
5629 && !port->hasExtBitOp(RRC, getSize (TTYPE (root->left->left))))
5632 /* whew got the first case : create the AST */
5633 return newNode (RRC, root->left->left, NULL);
5637 /* not found return root */
5641 /*-----------------------------------------------------------------*/
5642 /* optimizeSWAP :- optimize for nibble/byte/word swaps */
5643 /*-----------------------------------------------------------------*/
5645 optimizeSWAP (ast * root)
5647 /* will look for trees of the form
5648 (?expr << 4) | (?expr >> 4) or
5649 (?expr >> 4) | (?expr << 4) will make that
5650 into a SWAP : operation ..
5651 note : by 4 I mean (number of bits required to hold the
5653 /* if the root operation is not a | operation then not */
5654 if (!IS_BITOR (root))
5657 /* (?expr << 4) | (?expr >> 4) */
5658 if ((IS_LEFT_OP (root->left) && IS_RIGHT_OP (root->right))
5659 || (IS_RIGHT_OP (root->left) && IS_LEFT_OP (root->right)))
5662 if (!SPEC_USIGN (TETYPE (root->left->left)))
5665 if (!IS_AST_LIT_VALUE (root->left->right) ||
5666 !IS_AST_LIT_VALUE (root->right->right))
5669 /* make sure it is the same expression */
5670 if (!isAstEqual (root->left->left,
5674 if (AST_LIT_VALUE (root->left->right) !=
5675 (getSize (TTYPE (root->left->left)) * 4))
5678 if (AST_LIT_VALUE (root->right->right) !=
5679 (getSize (TTYPE (root->left->left)) * 4))
5682 /* make sure the port supports SWAP */
5683 if (port->hasExtBitOp
5684 && !port->hasExtBitOp(SWAP, getSize (TTYPE (root->left->left))))
5687 /* found it : create the AST */
5688 return newNode (SWAP, root->left->left, NULL);
5692 /* not found return root */
5696 /*-----------------------------------------------------------------*/
5697 /* optimizeCompare - optimizes compares for bit variables */
5698 /*-----------------------------------------------------------------*/
5700 optimizeCompare (ast * root)
5702 ast *optExpr = NULL;
5705 unsigned int litValue;
5707 /* if nothing then return nothing */
5711 /* if not a compare op then do leaves */
5712 if (!IS_COMPARE_OP (root))
5714 root->left = optimizeCompare (root->left);
5715 root->right = optimizeCompare (root->right);
5719 /* if left & right are the same then depending
5720 of the operation do */
5721 if (isAstEqual (root->left, root->right))
5723 switch (root->opval.op)
5728 optExpr = newAst_VALUE (constVal ("0"));
5733 optExpr = newAst_VALUE (constVal ("1"));
5737 return decorateType (optExpr, RESULT_TYPE_NONE);
5740 vleft = (root->left->type == EX_VALUE ?
5741 root->left->opval.val : NULL);
5743 vright = (root->right->type == EX_VALUE ?
5744 root->right->opval.val : NULL);
5746 /* if left is a BITVAR in BITSPACE */
5747 /* and right is a LITERAL then opt- */
5748 /* imize else do nothing */
5749 if (vleft && vright &&
5750 IS_BITVAR (vleft->etype) &&
5751 IN_BITSPACE (SPEC_OCLS (vleft->etype)) &&
5752 IS_LITERAL (vright->etype))
5755 /* if right side > 1 then comparison may never succeed */
5756 if ((litValue = (int) floatFromVal (vright)) > 1)
5758 werror (W_BAD_COMPARE);
5764 switch (root->opval.op)
5766 case '>': /* bit value greater than 1 cannot be */
5767 werror (W_BAD_COMPARE);
5771 case '<': /* bit value < 1 means 0 */
5773 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5776 case LE_OP: /* bit value <= 1 means no check */
5777 optExpr = newAst_VALUE (vright);
5780 case GE_OP: /* bit value >= 1 means only check for = */
5782 optExpr = newAst_VALUE (vleft);
5787 { /* literal is zero */
5788 switch (root->opval.op)
5790 case '<': /* bit value < 0 cannot be */
5791 werror (W_BAD_COMPARE);
5795 case '>': /* bit value > 0 means 1 */
5797 optExpr = newAst_VALUE (vleft);
5800 case LE_OP: /* bit value <= 0 means no check */
5801 case GE_OP: /* bit value >= 0 means no check */
5802 werror (W_BAD_COMPARE);
5806 case EQ_OP: /* bit == 0 means ! of bit */
5807 optExpr = newNode ('!', newAst_VALUE (vleft), NULL);
5811 return decorateType (resolveSymbols (optExpr), RESULT_TYPE_NONE);
5812 } /* end-of-if of BITVAR */
5817 /*-----------------------------------------------------------------*/
5818 /* addSymToBlock : adds the symbol to the first block we find */
5819 /*-----------------------------------------------------------------*/
5821 addSymToBlock (symbol * sym, ast * tree)
5823 /* reached end of tree or a leaf */
5824 if (!tree || IS_AST_LINK (tree) || IS_AST_VALUE (tree))
5828 if (IS_AST_OP (tree) &&
5829 tree->opval.op == BLOCK)
5832 symbol *lsym = copySymbol (sym);
5834 lsym->next = AST_VALUES (tree, sym);
5835 AST_VALUES (tree, sym) = lsym;
5839 addSymToBlock (sym, tree->left);
5840 addSymToBlock (sym, tree->right);
5843 /*-----------------------------------------------------------------*/
5844 /* processRegParms - do processing for register parameters */
5845 /*-----------------------------------------------------------------*/
5847 processRegParms (value * args, ast * body)
5851 if (IS_REGPARM (args->etype))
5852 addSymToBlock (args->sym, body);
5857 /*-----------------------------------------------------------------*/
5858 /* resetParmKey - resets the operandkeys for the symbols */
5859 /*-----------------------------------------------------------------*/
5860 DEFSETFUNC (resetParmKey)
5871 /*-----------------------------------------------------------------*/
5872 /* createFunction - This is the key node that calls the iCode for */
5873 /* generating the code for a function. Note code */
5874 /* is generated function by function, later when */
5875 /* add inter-procedural analysis this will change */
5876 /*-----------------------------------------------------------------*/
5878 createFunction (symbol * name, ast * body)
5884 iCode *piCode = NULL;
5886 if (getenv("SDCC_DEBUG_FUNCTION_POINTERS"))
5887 fprintf (stderr, "SDCCast.c:createFunction(%s)\n", name->name);
5889 /* if check function return 0 then some problem */
5890 if (checkFunction (name, NULL) == 0)
5893 /* create a dummy block if none exists */
5895 body = newNode (BLOCK, NULL, NULL);
5899 /* check if the function name already in the symbol table */
5900 if ((csym = findSym (SymbolTab, NULL, name->name)))
5903 /* special case for compiler defined functions
5904 we need to add the name to the publics list : this
5905 actually means we are now compiling the compiler
5909 addSet (&publics, name);
5914 addSymChain (&name);
5915 allocVariables (name);
5917 name->lastLine = mylineno;
5920 /* set the stack pointer */
5921 stackPtr = -port->stack.direction * port->stack.call_overhead;
5924 if (IFFUNC_ISISR (name->type))
5925 stackPtr -= port->stack.direction * port->stack.isr_overhead;
5927 if (IFFUNC_ISREENT (name->type) || options.stackAuto)
5929 if (options.useXstack)
5930 xstackPtr -= port->stack.direction * port->stack.reent_overhead;
5932 stackPtr -= port->stack.direction * port->stack.reent_overhead;
5935 fetype = getSpec (name->type); /* get the specifier for the function */
5936 /* if this is a reentrant function then */
5937 if (IFFUNC_ISREENT (name->type))
5940 allocParms (FUNC_ARGS(name->type)); /* allocate the parameters */
5942 /* do processing for parameters that are passed in registers */
5943 processRegParms (FUNC_ARGS(name->type), body);
5945 /* set the stack pointer */
5949 gatherImplicitVariables (body, NULL); /* move implicit variables into blocks */
5951 /* allocate & autoinit the block variables */
5952 processBlockVars (body, &stack, ALLOCATE);
5954 /* name needs to be mangled */
5955 SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
5957 body = resolveSymbols (body); /* resolve the symbols */
5958 body = decorateType (body, RESULT_TYPE_NONE); /* propagateType & do semantic checks */
5960 /* save the stack information */
5961 if (options.useXstack)
5962 name->xstack = SPEC_STAK (fetype) = stack;
5964 name->stack = SPEC_STAK (fetype) = stack;
5966 ex = newAst_VALUE (symbolVal (name)); /* create name */
5967 ex = newNode (FUNCTION, ex, body);
5968 ex->values.args = FUNC_ARGS(name->type);
5970 if (options.dump_tree) PA(ex);
5973 werror (E_FUNC_NO_CODE, name->name);
5977 /* create the node & generate intermediate code */
5979 codeOutFile = code->oFile;
5980 piCode = iCodeFromAst (ex);
5984 werror (E_FUNC_NO_CODE, name->name);
5988 eBBlockFromiCode (piCode);
5990 /* if there are any statics then do them */
5993 GcurMemmap = statsg;
5994 codeOutFile = statsg->oFile;
5995 eBBlockFromiCode (iCodeFromAst (decorateType (resolveSymbols (staticAutos), RESULT_TYPE_NONE)));
6001 /* dealloc the block variables */
6002 processBlockVars (body, &stack, DEALLOCATE);
6003 outputDebugStackSymbols();
6004 /* deallocate paramaters */
6005 deallocParms (FUNC_ARGS(name->type));
6007 if (IFFUNC_ISREENT (name->type))
6010 /* we are done freeup memory & cleanup */
6012 if (port->reset_labelKey) labelKey = 1;
6014 FUNC_HASBODY(name->type) = 1;
6015 addSet (&operKeyReset, name);
6016 applyToSet (operKeyReset, resetParmKey);
6021 cleanUpLevel (LabelTab, 0);
6022 cleanUpBlock (StructTab, 1);
6023 cleanUpBlock (TypedefTab, 1);
6025 xstack->syms = NULL;
6026 istack->syms = NULL;
6031 #define INDENT(x,f) { int i ; fprintf (f, "%d:", tree->lineno); for (i=0;i < x; i++) fprintf(f," "); }
6032 /*-----------------------------------------------------------------*/
6033 /* ast_print : prints the ast (for debugging purposes) */
6034 /*-----------------------------------------------------------------*/
6036 void ast_print (ast * tree, FILE *outfile, int indent)
6041 /* can print only decorated trees */
6042 if (!tree->decorated) return;
6044 /* if any child is an error | this one is an error do nothing */
6045 if (tree->isError ||
6046 (tree->left && tree->left->isError) ||
6047 (tree->right && tree->right->isError)) {
6048 fprintf(outfile,"ERROR_NODE(%p)\n",tree);
6052 /* print the line */
6053 /* if not block & function */
6054 if (tree->type == EX_OP &&
6055 (tree->opval.op != FUNCTION &&
6056 tree->opval.op != BLOCK &&
6057 tree->opval.op != NULLOP)) {
6060 if (tree->opval.op == FUNCTION) {
6062 value *args=FUNC_ARGS(tree->left->opval.val->type);
6063 fprintf(outfile,"FUNCTION (%s=%p) type (",
6064 tree->left->opval.val->name, tree);
6065 printTypeChain (tree->left->opval.val->type->next,outfile);
6066 fprintf(outfile,") args (");
6069 fprintf (outfile, ", ");
6071 printTypeChain (args ? args->type : NULL, outfile);
6073 args= args ? args->next : NULL;
6075 fprintf(outfile,")\n");
6076 ast_print(tree->left,outfile,indent);
6077 ast_print(tree->right,outfile,indent);
6080 if (tree->opval.op == BLOCK) {
6081 symbol *decls = tree->values.sym;
6082 INDENT(indent,outfile);
6083 fprintf(outfile,"{\n");
6085 INDENT(indent+2,outfile);
6086 fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
6087 decls->name, decls);
6088 printTypeChain(decls->type,outfile);
6089 fprintf(outfile,")\n");
6091 decls = decls->next;
6093 ast_print(tree->right,outfile,indent+2);
6094 INDENT(indent,outfile);
6095 fprintf(outfile,"}\n");
6098 if (tree->opval.op == NULLOP) {
6099 ast_print(tree->left,outfile,indent);
6100 ast_print(tree->right,outfile,indent);
6103 INDENT(indent,outfile);
6105 /*------------------------------------------------------------------*/
6106 /*----------------------------*/
6107 /* leaf has been reached */
6108 /*----------------------------*/
6109 /* if this is of type value */
6110 /* just get the type */
6111 if (tree->type == EX_VALUE) {
6113 if (IS_LITERAL (tree->opval.val->etype)) {
6114 fprintf(outfile,"CONSTANT (%p) value = ", tree);
6115 if (SPEC_USIGN (tree->opval.val->etype))
6116 fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
6118 fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
6119 fprintf(outfile,", 0x%x, %f", (TYPE_UDWORD) floatFromVal(tree->opval.val),
6120 floatFromVal(tree->opval.val));
6121 } else if (tree->opval.val->sym) {
6122 /* if the undefined flag is set then give error message */
6123 if (tree->opval.val->sym->undefined) {
6124 fprintf(outfile,"UNDEFINED SYMBOL ");
6126 fprintf(outfile,"SYMBOL ");
6128 fprintf(outfile,"(%s=%p @ %p)",
6129 tree->opval.val->sym->name, tree, tree->opval.val->sym);
6132 fprintf(outfile," type (");
6133 printTypeChain(tree->ftype,outfile);
6134 fprintf(outfile,")\n");
6136 fprintf(outfile,"\n");
6141 /* if type link for the case of cast */
6142 if (tree->type == EX_LINK) {
6143 fprintf(outfile,"TYPENODE (%p) type = (",tree);
6144 printTypeChain(tree->opval.lnk,outfile);
6145 fprintf(outfile,")\n");
6150 /* depending on type of operator do */
6152 switch (tree->opval.op) {
6153 /*------------------------------------------------------------------*/
6154 /*----------------------------*/
6156 /*----------------------------*/
6158 fprintf(outfile,"ARRAY_OP (%p) type (",tree);
6159 printTypeChain(tree->ftype,outfile);
6160 fprintf(outfile,")\n");
6161 ast_print(tree->left,outfile,indent+2);
6162 ast_print(tree->right,outfile,indent+2);
6165 /*------------------------------------------------------------------*/
6166 /*----------------------------*/
6168 /*----------------------------*/
6170 fprintf(outfile,"STRUCT_ACCESS (%p) type (",tree);
6171 printTypeChain(tree->ftype,outfile);
6172 fprintf(outfile,")\n");
6173 ast_print(tree->left,outfile,indent+2);
6174 ast_print(tree->right,outfile,indent+2);
6177 /*------------------------------------------------------------------*/
6178 /*----------------------------*/
6179 /* struct/union pointer */
6180 /*----------------------------*/
6182 fprintf(outfile,"PTR_ACCESS (%p) type (",tree);
6183 printTypeChain(tree->ftype,outfile);
6184 fprintf(outfile,")\n");
6185 ast_print(tree->left,outfile,indent+2);
6186 ast_print(tree->right,outfile,indent+2);
6189 /*------------------------------------------------------------------*/
6190 /*----------------------------*/
6191 /* ++/-- operation */
6192 /*----------------------------*/
6195 fprintf(outfile,"post-");
6197 fprintf(outfile,"pre-");
6198 fprintf(outfile,"INC_OP (%p) type (",tree);
6199 printTypeChain(tree->ftype,outfile);
6200 fprintf(outfile,")\n");
6201 ast_print(tree->left,outfile,indent+2); /* postincrement case */
6202 ast_print(tree->right,outfile,indent+2); /* preincrement case */
6207 fprintf(outfile,"post-");
6209 fprintf(outfile,"pre-");
6210 fprintf(outfile,"DEC_OP (%p) type (",tree);
6211 printTypeChain(tree->ftype,outfile);
6212 fprintf(outfile,")\n");
6213 ast_print(tree->left,outfile,indent+2); /* postdecrement case */
6214 ast_print(tree->right,outfile,indent+2); /* predecrement case */
6217 /*------------------------------------------------------------------*/
6218 /*----------------------------*/
6220 /*----------------------------*/
6223 fprintf(outfile,"& (%p) type (",tree);
6224 printTypeChain(tree->ftype,outfile);
6225 fprintf(outfile,")\n");
6226 ast_print(tree->left,outfile,indent+2);
6227 ast_print(tree->right,outfile,indent+2);
6229 fprintf(outfile,"ADDRESS_OF (%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 /*----------------------------*/
6238 /*----------------------------*/
6240 fprintf(outfile,"OR (%p) type (",tree);
6241 printTypeChain(tree->ftype,outfile);
6242 fprintf(outfile,")\n");
6243 ast_print(tree->left,outfile,indent+2);
6244 ast_print(tree->right,outfile,indent+2);
6246 /*------------------------------------------------------------------*/
6247 /*----------------------------*/
6249 /*----------------------------*/
6251 fprintf(outfile,"XOR (%p) type (",tree);
6252 printTypeChain(tree->ftype,outfile);
6253 fprintf(outfile,")\n");
6254 ast_print(tree->left,outfile,indent+2);
6255 ast_print(tree->right,outfile,indent+2);
6258 /*------------------------------------------------------------------*/
6259 /*----------------------------*/
6261 /*----------------------------*/
6263 fprintf(outfile,"DIV (%p) type (",tree);
6264 printTypeChain(tree->ftype,outfile);
6265 fprintf(outfile,")\n");
6266 ast_print(tree->left,outfile,indent+2);
6267 ast_print(tree->right,outfile,indent+2);
6269 /*------------------------------------------------------------------*/
6270 /*----------------------------*/
6272 /*----------------------------*/
6274 fprintf(outfile,"MOD (%p) type (",tree);
6275 printTypeChain(tree->ftype,outfile);
6276 fprintf(outfile,")\n");
6277 ast_print(tree->left,outfile,indent+2);
6278 ast_print(tree->right,outfile,indent+2);
6281 /*------------------------------------------------------------------*/
6282 /*----------------------------*/
6283 /* address dereference */
6284 /*----------------------------*/
6285 case '*': /* can be unary : if right is null then unary operation */
6287 fprintf(outfile,"DEREF (%p) type (",tree);
6288 printTypeChain(tree->ftype,outfile);
6289 fprintf(outfile,")\n");
6290 ast_print(tree->left,outfile,indent+2);
6293 /*------------------------------------------------------------------*/
6294 /*----------------------------*/
6295 /* multiplication */
6296 /*----------------------------*/
6297 fprintf(outfile,"MULT (%p) type (",tree);
6298 printTypeChain(tree->ftype,outfile);
6299 fprintf(outfile,")\n");
6300 ast_print(tree->left,outfile,indent+2);
6301 ast_print(tree->right,outfile,indent+2);
6305 /*------------------------------------------------------------------*/
6306 /*----------------------------*/
6307 /* unary '+' operator */
6308 /*----------------------------*/
6312 fprintf(outfile,"UPLUS (%p) type (",tree);
6313 printTypeChain(tree->ftype,outfile);
6314 fprintf(outfile,")\n");
6315 ast_print(tree->left,outfile,indent+2);
6317 /*------------------------------------------------------------------*/
6318 /*----------------------------*/
6320 /*----------------------------*/
6321 fprintf(outfile,"ADD (%p) type (",tree);
6322 printTypeChain(tree->ftype,outfile);
6323 fprintf(outfile,")\n");
6324 ast_print(tree->left,outfile,indent+2);
6325 ast_print(tree->right,outfile,indent+2);
6328 /*------------------------------------------------------------------*/
6329 /*----------------------------*/
6331 /*----------------------------*/
6332 case '-': /* can be unary */
6334 fprintf(outfile,"UMINUS (%p) type (",tree);
6335 printTypeChain(tree->ftype,outfile);
6336 fprintf(outfile,")\n");
6337 ast_print(tree->left,outfile,indent+2);
6339 /*------------------------------------------------------------------*/
6340 /*----------------------------*/
6342 /*----------------------------*/
6343 fprintf(outfile,"SUB (%p) type (",tree);
6344 printTypeChain(tree->ftype,outfile);
6345 fprintf(outfile,")\n");
6346 ast_print(tree->left,outfile,indent+2);
6347 ast_print(tree->right,outfile,indent+2);
6350 /*------------------------------------------------------------------*/
6351 /*----------------------------*/
6353 /*----------------------------*/
6355 fprintf(outfile,"COMPL (%p) type (",tree);
6356 printTypeChain(tree->ftype,outfile);
6357 fprintf(outfile,")\n");
6358 ast_print(tree->left,outfile,indent+2);
6360 /*------------------------------------------------------------------*/
6361 /*----------------------------*/
6363 /*----------------------------*/
6365 fprintf(outfile,"NOT (%p) type (",tree);
6366 printTypeChain(tree->ftype,outfile);
6367 fprintf(outfile,")\n");
6368 ast_print(tree->left,outfile,indent+2);
6370 /*------------------------------------------------------------------*/
6371 /*----------------------------*/
6373 /*----------------------------*/
6375 fprintf(outfile,"RRC (%p) type (",tree);
6376 printTypeChain(tree->ftype,outfile);
6377 fprintf(outfile,")\n");
6378 ast_print(tree->left,outfile,indent+2);
6382 fprintf(outfile,"RLC (%p) type (",tree);
6383 printTypeChain(tree->ftype,outfile);
6384 fprintf(outfile,")\n");
6385 ast_print(tree->left,outfile,indent+2);
6388 fprintf(outfile,"SWAP (%p) type (",tree);
6389 printTypeChain(tree->ftype,outfile);
6390 fprintf(outfile,")\n");
6391 ast_print(tree->left,outfile,indent+2);
6394 fprintf(outfile,"GETHBIT (%p) type (",tree);
6395 printTypeChain(tree->ftype,outfile);
6396 fprintf(outfile,")\n");
6397 ast_print(tree->left,outfile,indent+2);
6400 fprintf(outfile,"GETABIT (%p) type (",tree);
6401 printTypeChain(tree->ftype,outfile);
6402 fprintf(outfile,")\n");
6403 ast_print(tree->left,outfile,indent+2);
6404 ast_print(tree->right,outfile,indent+2);
6407 fprintf(outfile,"GETBYTE (%p) type (",tree);
6408 printTypeChain(tree->ftype,outfile);
6409 fprintf(outfile,")\n");
6410 ast_print(tree->left,outfile,indent+2);
6411 ast_print(tree->right,outfile,indent+2);
6414 fprintf(outfile,"GETWORD (%p) type (",tree);
6415 printTypeChain(tree->ftype,outfile);
6416 fprintf(outfile,")\n");
6417 ast_print(tree->left,outfile,indent+2);
6418 ast_print(tree->right,outfile,indent+2);
6421 fprintf(outfile,"LEFT_SHIFT (%p) type (",tree);
6422 printTypeChain(tree->ftype,outfile);
6423 fprintf(outfile,")\n");
6424 ast_print(tree->left,outfile,indent+2);
6425 ast_print(tree->right,outfile,indent+2);
6428 fprintf(outfile,"RIGHT_SHIFT (%p) type (",tree);
6429 printTypeChain(tree->ftype,outfile);
6430 fprintf(outfile,")\n");
6431 ast_print(tree->left,outfile,indent+2);
6432 ast_print(tree->right,outfile,indent+2);
6434 /*------------------------------------------------------------------*/
6435 /*----------------------------*/
6437 /*----------------------------*/
6438 case CAST: /* change the type */
6439 fprintf(outfile,"CAST (%p) from type (",tree);
6440 printTypeChain(tree->right->ftype,outfile);
6441 fprintf(outfile,") to type (");
6442 printTypeChain(tree->ftype,outfile);
6443 fprintf(outfile,")\n");
6444 ast_print(tree->right,outfile,indent+2);
6448 fprintf(outfile,"ANDAND (%p) type (",tree);
6449 printTypeChain(tree->ftype,outfile);
6450 fprintf(outfile,")\n");
6451 ast_print(tree->left,outfile,indent+2);
6452 ast_print(tree->right,outfile,indent+2);
6455 fprintf(outfile,"OROR (%p) type (",tree);
6456 printTypeChain(tree->ftype,outfile);
6457 fprintf(outfile,")\n");
6458 ast_print(tree->left,outfile,indent+2);
6459 ast_print(tree->right,outfile,indent+2);
6462 /*------------------------------------------------------------------*/
6463 /*----------------------------*/
6464 /* comparison operators */
6465 /*----------------------------*/
6467 fprintf(outfile,"GT(>) (%p) type (",tree);
6468 printTypeChain(tree->ftype,outfile);
6469 fprintf(outfile,")\n");
6470 ast_print(tree->left,outfile,indent+2);
6471 ast_print(tree->right,outfile,indent+2);
6474 fprintf(outfile,"LT(<) (%p) type (",tree);
6475 printTypeChain(tree->ftype,outfile);
6476 fprintf(outfile,")\n");
6477 ast_print(tree->left,outfile,indent+2);
6478 ast_print(tree->right,outfile,indent+2);
6481 fprintf(outfile,"LE(<=) (%p) type (",tree);
6482 printTypeChain(tree->ftype,outfile);
6483 fprintf(outfile,")\n");
6484 ast_print(tree->left,outfile,indent+2);
6485 ast_print(tree->right,outfile,indent+2);
6488 fprintf(outfile,"GE(>=) (%p) type (",tree);
6489 printTypeChain(tree->ftype,outfile);
6490 fprintf(outfile,")\n");
6491 ast_print(tree->left,outfile,indent+2);
6492 ast_print(tree->right,outfile,indent+2);
6495 fprintf(outfile,"EQ(==) (%p) type (",tree);
6496 printTypeChain(tree->ftype,outfile);
6497 fprintf(outfile,")\n");
6498 ast_print(tree->left,outfile,indent+2);
6499 ast_print(tree->right,outfile,indent+2);
6502 fprintf(outfile,"NE(!=) (%p) type (",tree);
6503 printTypeChain(tree->ftype,outfile);
6504 fprintf(outfile,")\n");
6505 ast_print(tree->left,outfile,indent+2);
6506 ast_print(tree->right,outfile,indent+2);
6507 /*------------------------------------------------------------------*/
6508 /*----------------------------*/
6510 /*----------------------------*/
6511 case SIZEOF: /* evaluate wihout code generation */
6512 fprintf(outfile,"SIZEOF %d\n",(getSize (tree->right->ftype)));
6515 /*------------------------------------------------------------------*/
6516 /*----------------------------*/
6517 /* conditional operator '?' */
6518 /*----------------------------*/
6520 fprintf(outfile,"QUEST(?) (%p) type (",tree);
6521 printTypeChain(tree->ftype,outfile);
6522 fprintf(outfile,")\n");
6523 ast_print(tree->left,outfile,indent+2);
6524 ast_print(tree->right,outfile,indent+2);
6528 fprintf(outfile,"COLON(:) (%p) type (",tree);
6529 printTypeChain(tree->ftype,outfile);
6530 fprintf(outfile,")\n");
6531 ast_print(tree->left,outfile,indent+2);
6532 ast_print(tree->right,outfile,indent+2);
6535 /*------------------------------------------------------------------*/
6536 /*----------------------------*/
6537 /* assignment operators */
6538 /*----------------------------*/
6540 fprintf(outfile,"MULASS(*=) (%p) type (",tree);
6541 printTypeChain(tree->ftype,outfile);
6542 fprintf(outfile,")\n");
6543 ast_print(tree->left,outfile,indent+2);
6544 ast_print(tree->right,outfile,indent+2);
6547 fprintf(outfile,"DIVASS(/=) (%p) type (",tree);
6548 printTypeChain(tree->ftype,outfile);
6549 fprintf(outfile,")\n");
6550 ast_print(tree->left,outfile,indent+2);
6551 ast_print(tree->right,outfile,indent+2);
6554 fprintf(outfile,"ANDASS(&=) (%p) type (",tree);
6555 printTypeChain(tree->ftype,outfile);
6556 fprintf(outfile,")\n");
6557 ast_print(tree->left,outfile,indent+2);
6558 ast_print(tree->right,outfile,indent+2);
6561 fprintf(outfile,"ORASS(|=) (%p) type (",tree);
6562 printTypeChain(tree->ftype,outfile);
6563 fprintf(outfile,")\n");
6564 ast_print(tree->left,outfile,indent+2);
6565 ast_print(tree->right,outfile,indent+2);
6568 fprintf(outfile,"XORASS(^=) (%p) type (",tree);
6569 printTypeChain(tree->ftype,outfile);
6570 fprintf(outfile,")\n");
6571 ast_print(tree->left,outfile,indent+2);
6572 ast_print(tree->right,outfile,indent+2);
6575 fprintf(outfile,"RSHFTASS(>>=) (%p) type (",tree);
6576 printTypeChain(tree->ftype,outfile);
6577 fprintf(outfile,")\n");
6578 ast_print(tree->left,outfile,indent+2);
6579 ast_print(tree->right,outfile,indent+2);
6582 fprintf(outfile,"LSHFTASS(<<=) (%p) type (",tree);
6583 printTypeChain(tree->ftype,outfile);
6584 fprintf(outfile,")\n");
6585 ast_print(tree->left,outfile,indent+2);
6586 ast_print(tree->right,outfile,indent+2);
6588 /*------------------------------------------------------------------*/
6589 /*----------------------------*/
6591 /*----------------------------*/
6593 fprintf(outfile,"SUBASS(-=) (%p) type (",tree);
6594 printTypeChain(tree->ftype,outfile);
6595 fprintf(outfile,")\n");
6596 ast_print(tree->left,outfile,indent+2);
6597 ast_print(tree->right,outfile,indent+2);
6599 /*------------------------------------------------------------------*/
6600 /*----------------------------*/
6602 /*----------------------------*/
6604 fprintf(outfile,"ADDASS(+=) (%p) type (",tree);
6605 printTypeChain(tree->ftype,outfile);
6606 fprintf(outfile,")\n");
6607 ast_print(tree->left,outfile,indent+2);
6608 ast_print(tree->right,outfile,indent+2);
6610 /*------------------------------------------------------------------*/
6611 /*----------------------------*/
6612 /* straight assignemnt */
6613 /*----------------------------*/
6615 fprintf(outfile,"ASSIGN(=) (%p) type (",tree);
6616 printTypeChain(tree->ftype,outfile);
6617 fprintf(outfile,")\n");
6618 ast_print(tree->left,outfile,indent+2);
6619 ast_print(tree->right,outfile,indent+2);
6621 /*------------------------------------------------------------------*/
6622 /*----------------------------*/
6623 /* comma operator */
6624 /*----------------------------*/
6626 fprintf(outfile,"COMMA(,) (%p) type (",tree);
6627 printTypeChain(tree->ftype,outfile);
6628 fprintf(outfile,")\n");
6629 ast_print(tree->left,outfile,indent+2);
6630 ast_print(tree->right,outfile,indent+2);
6632 /*------------------------------------------------------------------*/
6633 /*----------------------------*/
6635 /*----------------------------*/
6638 fprintf(outfile,"CALL (%p) type (",tree);
6639 printTypeChain(tree->ftype,outfile);
6640 fprintf(outfile,")\n");
6641 ast_print(tree->left,outfile,indent+2);
6642 ast_print(tree->right,outfile,indent+2);
6645 fprintf(outfile,"PARMS\n");
6646 ast_print(tree->left,outfile,indent+2);
6647 if (tree->right /*&& !IS_AST_PARAM(tree->right)*/) {
6648 ast_print(tree->right,outfile,indent+2);
6651 /*------------------------------------------------------------------*/
6652 /*----------------------------*/
6653 /* return statement */
6654 /*----------------------------*/
6656 fprintf(outfile,"RETURN (%p) type (",tree);
6658 printTypeChain(tree->right->ftype,outfile);
6660 fprintf(outfile,")\n");
6661 ast_print(tree->right,outfile,indent+2);
6663 /*------------------------------------------------------------------*/
6664 /*----------------------------*/
6665 /* label statement */
6666 /*----------------------------*/
6668 fprintf(outfile,"LABEL (%p)\n",tree);
6669 ast_print(tree->left,outfile,indent+2);
6670 ast_print(tree->right,outfile,indent);
6672 /*------------------------------------------------------------------*/
6673 /*----------------------------*/
6674 /* switch statement */
6675 /*----------------------------*/
6679 fprintf(outfile,"SWITCH (%p) ",tree);
6680 ast_print(tree->left,outfile,0);
6681 for (val = tree->values.switchVals.swVals; val ; val = val->next) {
6682 INDENT(indent+2,outfile);
6683 fprintf(outfile,"CASE 0x%x GOTO _case_%d_%d\n",
6684 (int) floatFromVal(val),
6685 tree->values.switchVals.swNum,
6686 (int) floatFromVal(val));
6688 ast_print(tree->right,outfile,indent);
6691 /*------------------------------------------------------------------*/
6692 /*----------------------------*/
6694 /*----------------------------*/
6696 fprintf(outfile,"IF (%p) \n",tree);
6697 ast_print(tree->left,outfile,indent+2);
6698 if (tree->trueLabel) {
6699 INDENT(indent+2,outfile);
6700 fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
6702 if (tree->falseLabel) {
6703 INDENT(indent+2,outfile);
6704 fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
6706 ast_print(tree->right,outfile,indent+2);
6708 /*----------------------------*/
6709 /* goto Statement */
6710 /*----------------------------*/
6712 fprintf(outfile,"GOTO (%p) \n",tree);
6713 ast_print(tree->left,outfile,indent+2);
6714 fprintf(outfile,"\n");
6716 /*------------------------------------------------------------------*/
6717 /*----------------------------*/
6719 /*----------------------------*/
6721 fprintf(outfile,"FOR (%p) \n",tree);
6722 if (AST_FOR( tree, initExpr)) {
6723 INDENT(indent+2,outfile);
6724 fprintf(outfile,"INIT EXPR ");
6725 ast_print(AST_FOR(tree, initExpr),outfile,indent+2);
6727 if (AST_FOR( tree, condExpr)) {
6728 INDENT(indent+2,outfile);
6729 fprintf(outfile,"COND EXPR ");
6730 ast_print(AST_FOR(tree, condExpr),outfile,indent+2);
6732 if (AST_FOR( tree, loopExpr)) {
6733 INDENT(indent+2,outfile);
6734 fprintf(outfile,"LOOP EXPR ");
6735 ast_print(AST_FOR(tree, loopExpr),outfile,indent+2);
6737 fprintf(outfile,"FOR LOOP BODY \n");
6738 ast_print(tree->left,outfile,indent+2);
6741 fprintf(outfile,"CRITICAL (%p) \n",tree);
6742 ast_print(tree->left,outfile,indent+2);
6750 ast_print(t,stdout,0);
6755 /*-----------------------------------------------------------------*/
6756 /* astErrors : returns non-zero if errors present in tree */
6757 /*-----------------------------------------------------------------*/
6758 int astErrors(ast *t)
6767 if (t->type == EX_VALUE
6768 && t->opval.val->sym
6769 && t->opval.val->sym->undefined)
6772 errors += astErrors(t->left);
6773 errors += astErrors(t->right);