1 /*-------------------------------------------------------------------------
2 SDCCast.c - source file for parser support & all ast related routines
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
29 set *operKeyReset = NULL;
30 extern char *currFname ;
31 ast *staticAutos = NULL;
34 #define LRVAL(x) x->left->rvalue
35 #define RRVAL(x) x->right->rvalue
36 #define TRVAL(x) x->rvalue
37 #define LLVAL(x) x->left->lvalue
38 #define RLVAL(x) x->right->lvalue
39 #define TLVAL(x) x->lvalue
40 #define RTYPE(x) x->right->ftype
41 #define RETYPE(x) x->right->etype
42 #define LTYPE(x) x->left->ftype
43 #define LETYPE(x) x->left->etype
44 #define TTYPE(x) x->ftype
45 #define TETYPE(x) x->etype
53 extern int fatalError ;
55 extern char *filename ;
57 extern ast *createIval (ast *, link *, initList *, ast *);
58 extern ast *createIvalCharPtr (ast *, link *, ast *);
59 ast *optimizeRRCRLC ( ast * );
60 ast *optimizeGetHbit(ast *);
61 ast *backPatchLabels (ast *,symbol *,symbol *);
65 printTypeChain(tree->ftype,stdout);
70 /*-----------------------------------------------------------------*/
71 /* newAst - creates a fresh node for an expression tree */
72 /*-----------------------------------------------------------------*/
73 ast *newAst (int type, void *op )
76 static int oldLineno = 0 ;
78 ALLOC(ex,sizeof(ast));
81 ex->lineno = (noLineno ? oldLineno : yylineno);
82 ex->filename = currFname ;
83 ex->level = NestLevel ;
84 ex->block = currBlockno ;
85 ex->initMode = inInitMode;
87 /* depending on the type */
90 ex->opval.val = (value *) op;
93 ex->opval.op = (int) op ;
96 ex->opval.lnk = (link *) op;
99 ex->opval.stmnt= (unsigned) op;
105 /*-----------------------------------------------------------------*/
106 /* newNode - creates a new node */
107 /*-----------------------------------------------------------------*/
108 ast *newNode ( int op, ast *left, ast *right )
112 ex = newAst (EX_OP,(void *) op) ;
119 /*-----------------------------------------------------------------*/
120 /* newIfxNode - creates a new Ifx Node */
121 /*-----------------------------------------------------------------*/
122 ast *newIfxNode (ast *condAst, symbol *trueLabel, symbol *falseLabel)
126 /* if this is a literal then we already know the result */
127 if (condAst->etype && IS_LITERAL(condAst->etype)) {
129 /* then depending on the expression value */
130 if ( floatFromVal(condAst->opval.val) )
131 ifxNode = newNode(GOTO, newAst(EX_VALUE,
132 symbolVal (trueLabel )),NULL);
134 ifxNode = newNode(GOTO, newAst(EX_VALUE,
135 symbolVal (falseLabel )),NULL);
138 ifxNode = newNode(IFX,condAst,NULL);
139 ifxNode->trueLabel = trueLabel;
140 ifxNode->falseLabel= falseLabel;
146 /*-----------------------------------------------------------------*/
147 /* copyAstValues - copies value portion of ast if needed */
148 /*-----------------------------------------------------------------*/
149 void copyAstValues (ast *dest,ast *src)
151 switch (src->opval.op) {
153 dest->values.sym = copySymbolChain(src->values.sym);
157 dest->values.switchVals.swVals =
158 copyValue(src->values.switchVals.swVals);
159 dest->values.switchVals.swDefault =
160 src->values.switchVals.swDefault ;
161 dest->values.switchVals.swNum =
162 src->values.switchVals.swNum ;
166 ALLOC_ATOMIC(dest->values.inlineasm,strlen(src->values.inlineasm));
167 strcpy(dest->values.inlineasm,src->values.inlineasm);
170 AST_FOR(dest,trueLabel) = copySymbol(AST_FOR(src,trueLabel));
171 AST_FOR(dest,continueLabel) = copySymbol(AST_FOR(src,continueLabel));
172 AST_FOR(dest,falseLabel) = copySymbol(AST_FOR(src,falseLabel));
173 AST_FOR(dest,condLabel) = copySymbol(AST_FOR(src,condLabel));
174 AST_FOR(dest,initExpr) = copyAst (AST_FOR(src,initExpr)) ;
175 AST_FOR(dest,condExpr) = copyAst (AST_FOR(src,condExpr)) ;
176 AST_FOR(dest,loopExpr) = copyAst (AST_FOR(src,loopExpr)) ;
181 /*-----------------------------------------------------------------*/
182 /* copyAst - makes a copy of a given astession */
183 /*-----------------------------------------------------------------*/
184 ast *copyAst (ast *src)
188 if (!src) return NULL ;
190 ALLOC(dest,sizeof(ast));
192 dest->type = src->type ;
193 dest->lineno = src->lineno ;
194 dest->level = src->level ;
195 dest->funcName = src->funcName;
196 dest->argSym = src->argSym;
198 /* if this is a leaf */
200 if (src->type == EX_VALUE) {
201 dest->opval.val = copyValue(src->opval.val);
206 if (src->type == EX_LINK) {
207 dest->opval.lnk = copyLinkChain(src->opval.lnk);
211 dest->opval.op = src->opval.op ;
213 /* if this is a node that has special values */
214 copyAstValues (dest,src);
217 dest->etype = getSpec(dest->ftype = copyLinkChain(src->ftype)) ;
219 dest->trueLabel = copySymbol (src->trueLabel);
220 dest->falseLabel= copySymbol (src->falseLabel);
221 dest->left = copyAst(src->left);
222 dest->right= copyAst(src->right);
228 /*-----------------------------------------------------------------*/
229 /* hasSEFcalls - returns TRUE if tree has a function call */
230 /*-----------------------------------------------------------------*/
231 bool hasSEFcalls ( ast *tree)
236 if (tree->type == EX_OP &&
237 ( tree->opval.op == CALL ||
238 tree->opval.op == PCALL ||
239 tree->opval.op == '=' ||
240 tree->opval.op == INC_OP ||
241 tree->opval.op == DEC_OP ))
244 return ( hasSEFcalls(tree->left) |
245 hasSEFcalls(tree->right));
248 /*-----------------------------------------------------------------*/
249 /* isAstEqual - compares two asts & returns 1 if they are equal */
250 /*-----------------------------------------------------------------*/
251 int isAstEqual (ast *t1, ast *t2)
260 if (t1->type != t2->type)
265 if (t1->opval.op != t2->opval.op)
267 return ( isAstEqual(t1->left,t2->left) &&
268 isAstEqual(t1->right,t2->right));
272 if (t1->opval.val->sym) {
273 if (!t2->opval.val->sym)
276 return isSymbolEqual(t1->opval.val->sym,
280 if (t2->opval.val->sym)
283 return (floatFromVal(t1->opval.val) ==
284 floatFromVal(t2->opval.val));
288 /* only compare these two types */
296 /*-----------------------------------------------------------------*/
297 /* resolveSymbols - resolve symbols from the symbol table */
298 /*-----------------------------------------------------------------*/
299 ast *resolveSymbols (ast *tree)
301 /* walk the entire tree and check for values */
302 /* with symbols if we find one then replace */
303 /* symbol with that from the symbol table */
309 /* if not block & function */
310 if ( tree->type == EX_OP &&
311 ( tree->opval.op != FUNCTION &&
312 tree->opval.op != BLOCK &&
313 tree->opval.op != NULLOP )) {
314 filename = tree->filename ;
315 lineno = tree->lineno ;
318 /* make sure we resolve the true & false labels for ifx */
319 if (tree->type == EX_OP && tree->opval.op == IFX ) {
322 if (tree->trueLabel) {
323 if (( csym = findSym(LabelTab,tree->trueLabel,
324 tree->trueLabel->name)))
325 tree->trueLabel = csym ;
327 werror(E_LABEL_UNDEF,tree->trueLabel->name);
330 if (tree->falseLabel) {
331 if (( csym = findSym(LabelTab,
333 tree->falseLabel->name)))
334 tree->falseLabel = csym ;
336 werror(E_LABEL_UNDEF,tree->falseLabel->name);
341 /* if this is a label resolve it from the labelTab*/
342 if (IS_AST_VALUE(tree) &&
343 tree->opval.val->sym &&
344 tree->opval.val->sym->islbl) {
346 symbol *csym = findSym (LabelTab, tree->opval.val->sym ,
347 tree->opval.val->sym->name);
350 werror (E_LABEL_UNDEF,tree->opval.val->sym->name);
352 tree->opval.val->sym = csym ;
354 goto resolveChildren ;
357 /* do only for leafs */
358 if (IS_AST_VALUE(tree) &&
359 tree->opval.val->sym &&
360 ! tree->opval.val->sym->implicit ) {
362 symbol *csym = findSymWithLevel (SymbolTab,tree->opval.val->sym);
364 /* if found in the symbol table & they r not the same */
365 if (csym && tree->opval.val->sym != csym ) {
366 tree->opval.val->sym = csym ;
367 tree->opval.val->type = csym->type;
368 tree->opval.val->etype = csym->etype;
371 /* if not found in the symbol table */
372 /* mark it as undefined assume it is*/
373 /* an integer in data space */
374 if (!csym && !tree->opval.val->sym->implicit) {
376 /* if this is a function name then */
377 /* mark it as returning an int */
378 if (tree->funcName) {
379 tree->opval.val->sym->type = newLink();
380 DCL_TYPE(tree->opval.val->sym->type) = FUNCTION;
381 tree->opval.val->sym->type->next =
382 tree->opval.val->sym->etype = newIntLink();
383 tree->opval.val->etype = tree->opval.val->etype;
384 tree->opval.val->type = tree->opval.val->sym->type;
385 werror(W_IMPLICIT_FUNC,tree->opval.val->sym->name);
387 tree->opval.val->sym->undefined =1 ;
388 tree->opval.val->type =
389 tree->opval.val->etype = newIntLink();
390 tree->opval.val->sym->type =
391 tree->opval.val->sym->etype = newIntLink();
397 resolveSymbols (tree->left);
398 resolveSymbols (tree->right);
403 /*-----------------------------------------------------------------*/
404 /* setAstLineno - walks a ast tree & sets the line number */
405 /*-----------------------------------------------------------------*/
406 int setAstLineno ( ast *tree, int lineno)
411 tree->lineno = lineno ;
412 setAstLineno ( tree->left, lineno);
413 setAstLineno ( tree->right, lineno);
417 /*-----------------------------------------------------------------*/
418 /* resolveFromTable - will return the symbal table value */
419 /*-----------------------------------------------------------------*/
420 value *resolveFromTable (value *val)
427 csym = findSymWithLevel (SymbolTab,val->sym);
429 /* if found in the symbol table & they r not the same */
430 if (csym && val->sym != csym &&
431 csym->level == val->sym->level &&
436 val->type = csym->type;
437 val->etype = csym->etype;
443 /*-----------------------------------------------------------------*/
444 /* funcOfType :- function of type with name */
445 /*-----------------------------------------------------------------*/
446 symbol *funcOfType (char *name, link *type, link *argType,
447 int nArgs , int rent)
451 /* create the symbol */
452 sym = newSymbol (name,0);
454 /* if arguments required */
458 args = sym->args = newValue();
461 argStack += getSize(type);
462 args->type = copyLinkChain(argType);
463 args->etype = getSpec(args->type);
466 args = args->next = newValue();
470 /* setup return value */
471 sym->type = newLink();
472 DCL_TYPE(sym->type) = FUNCTION;
473 sym->type->next = copyLinkChain(type);
474 sym->etype = getSpec(sym->type);
475 SPEC_RENT(sym->etype) = rent;
480 sym->argStack = (rent ? argStack : 0);
481 allocVariables (sym);
486 /*-----------------------------------------------------------------*/
487 /* reverseParms - will reverse a parameter tree */
488 /*-----------------------------------------------------------------*/
489 void reverseParms (ast *ptree)
495 /* top down if we find a nonParm tree then quit */
496 if (ptree->type == EX_OP && ptree->opval.op == PARAM ) {
498 ptree->left = ptree->right;
499 ptree->right = ttree;
500 reverseParms(ptree->left);
501 reverseParms(ptree->right);
507 /*-----------------------------------------------------------------*/
508 /* processParms - makes sure the parameters are okay and do some */
509 /* processing with them */
510 /*-----------------------------------------------------------------*/
511 int processParms (ast *func, value *defParm,
515 link *fetype = func->etype;
517 /* if none of them exist */
518 if ( !defParm && !actParm)
521 /* if the function is being called via a pointer & */
522 /* it has not been defined a reentrant then we cannot*/
523 /* have parameters */
524 if (func->type != EX_VALUE && !IS_RENT(fetype) && !options.stackAuto) {
525 werror (E_NONRENT_ARGS);
529 /* if defined parameters ended but actual parameters */
530 /* exist and this is not defined as a variable arg */
531 /* also check if statckAuto option is specified */
532 if ((! defParm) && actParm && (!func->hasVargs ) &&
533 !options.stackAuto && !IS_RENT(fetype)) {
534 werror(E_TOO_MANY_PARMS);
538 /* if defined parameters present and actual paramters ended */
539 if ( defParm && ! actParm) {
540 werror(E_TO_FEW_PARMS);
544 /* if defined parameters ended but actual has not & */
545 /* has a variable argument list or statckAuto */
546 if (! defParm && actParm &&
547 (func->hasVargs || options.stackAuto || IS_RENT(fetype)))
550 resolveSymbols(actParm);
551 /* if this is a PARAM node then match left & right */
552 if ( actParm->type == EX_OP && actParm->opval.op == PARAM) {
554 return (processParms (func,defParm,actParm->left,parmNumber) ||
555 processParms (func,defParm->next, actParm->right,parmNumber) );
558 /* the parameter type must be atleast castable */
559 if (checkType(defParm->type,actParm->ftype) == 0) {
560 werror(E_TYPE_MISMATCH_PARM,*parmNumber);
561 werror(E_CONTINUE,"defined type ");
562 printTypeChain(defParm->type,stderr);fprintf(stderr,"\n");
563 werror(E_CONTINUE,"actual type ");
564 printTypeChain(actParm->ftype,stderr);fprintf(stderr,"\n");
567 /* if the parameter is castable then add the cast */
568 if ( checkType (defParm->type,actParm->ftype) < 0) {
569 ast *pTree = resolveSymbols(copyAst(actParm));
571 /* now change the current one to a cast */
572 actParm->type = EX_OP ;
573 actParm->opval.op = CAST ;
574 actParm->left = newAst(EX_LINK,defParm->type);
575 actParm->right= pTree ;
576 actParm->etype= defParm->etype;
577 actParm->ftype= defParm->type;
580 actParm->argSym = resolveFromTable(defParm)->sym ;
581 /* make a copy and change the regparm type to the defined parm */
582 actParm->etype = getSpec(actParm->ftype = copyLinkChain(actParm->ftype));
583 SPEC_REGPARM(actParm->etype) = SPEC_REGPARM(defParm->etype);
586 /*-----------------------------------------------------------------*/
587 /* createIvalType - generates ival for basic types */
588 /*-----------------------------------------------------------------*/
589 ast *createIvalType ( ast *sym,link *type, initList *ilist)
593 /* if initList is deep */
594 if ( ilist->type == INIT_DEEP )
595 ilist = ilist->init.deep ;
597 iExpr = decorateType(resolveSymbols(list2expr(ilist)));
598 return newNode('=',sym,iExpr);
601 /*-----------------------------------------------------------------*/
602 /* createIvalStruct - generates initial value for structures */
603 /*-----------------------------------------------------------------*/
604 ast *createIvalStruct (ast *sym,link *type,initList *ilist)
610 sflds = SPEC_STRUCT(type)->fields ;
611 if (ilist->type != INIT_DEEP) {
612 werror(E_INIT_STRUCT,"");
616 iloop = ilist->init.deep;
618 for ( ; sflds ; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL )) {
621 /* if we have come to end */
625 lAst = decorateType(resolveSymbols(newNode('.',sym,
626 newAst(EX_VALUE,symbolVal(sflds)))));
627 rast = createIval (lAst, sflds->type, iloop,rast);
633 /*-----------------------------------------------------------------*/
634 /* createIvalArray - generates code for array initialization */
635 /*-----------------------------------------------------------------*/
636 ast *createIvalArray (ast *sym, link *type, initList *ilist)
640 int lcnt = 0, size =0 ;
642 /* take care of the special case */
643 /* array of characters can be init */
645 if ( IS_CHAR(type->next) )
646 if ( (rast = createIvalCharPtr(sym,
648 decorateType(resolveSymbols(list2expr(ilist))))))
652 /* not the special case */
653 if (ilist->type != INIT_DEEP) {
654 werror(E_INIT_STRUCT,"");
658 iloop = ilist->init.deep ;
659 lcnt = DCL_ELEM(type);
665 aSym = newNode('[',sym,newAst(EX_VALUE,valueFromLit(size-1)));
666 aSym = decorateType(resolveSymbols(aSym));
667 rast = createIval (aSym,type->next,iloop,rast) ;
668 iloop = (iloop ? iloop->next : NULL) ;
671 /* if not array limits given & we */
672 /* are out of initialisers then */
673 if (!DCL_ELEM(type) && !iloop)
676 /* no of elements given and we */
677 /* have generated for all of them */
682 /* if we have not been given a size */
684 DCL_ELEM(type) = size;
690 /*-----------------------------------------------------------------*/
691 /* createIvalCharPtr - generates initial values for char pointers */
692 /*-----------------------------------------------------------------*/
693 ast *createIvalCharPtr (ast *sym, link *type, ast *iexpr)
697 /* if this is a pointer & right is a literal array then */
698 /* just assignment will do */
699 if ( IS_PTR(type) && (( IS_LITERAL(iexpr->etype) ||
700 SPEC_SCLS(iexpr->etype) == S_CODE )
701 && IS_ARRAY(iexpr->ftype)))
702 return newNode('=',sym,iexpr);
704 /* left side is an array so we have to assign each */
706 if (( IS_LITERAL(iexpr->etype) ||
707 SPEC_SCLS(iexpr->etype) == S_CODE )
708 && IS_ARRAY(iexpr->ftype)) {
710 /* for each character generate an assignment */
711 /* to the array element */
712 char *s = SPEC_CVAL(iexpr->etype).v_char ;
716 rast = newNode(NULLOP,
720 newAst(EX_VALUE,valueFromLit(i))),
721 newAst(EX_VALUE,valueFromLit(*s))));
725 rast = newNode(NULLOP,
729 newAst(EX_VALUE,valueFromLit(i))),
730 newAst(EX_VALUE,valueFromLit(*s))));
737 /*-----------------------------------------------------------------*/
738 /* createIvalPtr - generates initial value for pointers */
739 /*-----------------------------------------------------------------*/
740 ast *createIvalPtr (ast *sym,link *type,initList *ilist)
746 if ( ilist->type == INIT_DEEP )
747 ilist = ilist->init.deep ;
749 iexpr = decorateType(resolveSymbols(list2expr(ilist)));
751 /* if character pointer */
752 if (IS_CHAR(type->next))
753 if ((rast = createIvalCharPtr (sym,type,iexpr)))
756 return newNode('=',sym,iexpr);
759 /*-----------------------------------------------------------------*/
760 /* createIval - generates code for initial value */
761 /*-----------------------------------------------------------------*/
762 ast *createIval (ast *sym, link *type, initList *ilist, ast *wid)
769 /* if structure then */
771 rast = createIvalStruct(sym, type,ilist);
773 /* if this is a pointer */
775 rast = createIvalPtr(sym, type,ilist);
777 /* if this is an array */
779 rast = createIvalArray(sym, type,ilist);
781 /* if type is SPECIFIER */
783 rast = createIvalType (sym,type,ilist);
785 return newNode(NULLOP,wid,rast);
790 /*-----------------------------------------------------------------*/
791 /* initAggregates - initialises aggregate variables with initv */
792 /*-----------------------------------------------------------------*/
793 ast *initAggregates ( symbol *sym, initList *ival, ast *wid)
795 return createIval (newAst(EX_VALUE,symbolVal(sym)),sym->type,ival,wid);
798 /*-----------------------------------------------------------------*/
799 /* gatherAutoInit - creates assignment expressions for initial */
801 /*-----------------------------------------------------------------*/
802 ast *gatherAutoInit ( symbol *autoChain )
809 for ( sym = autoChain ; sym ; sym = sym->next ) {
811 /* resolve the symbols in the ival */
813 resolveIvalSym(sym->ival);
815 /* if this is a static variable & has an */
816 /* initial value the code needs to be lifted */
817 /* here to the main portion since they can be */
818 /* initialised only once at the start */
819 if ( IS_STATIC(sym->etype) && sym->ival &&
820 SPEC_SCLS(sym->etype) != S_CODE) {
823 /* insert the symbol into the symbol table */
824 /* with level = 0 & name = rname */
825 newSym = copySymbol (sym);
826 addSym (SymbolTab,newSym,newSym->name,0,0);
828 /* now lift the code to main */
829 if (IS_AGGREGATE(sym->type))
830 work = initAggregates (sym, sym->ival,NULL);
832 work = newNode('=' ,newAst(EX_VALUE,symbolVal(newSym)),
833 list2expr(sym->ival));
835 setAstLineno(work,sym->lineDef);
839 staticAutos = newNode(NULLOP,staticAutos,work);
846 /* if there is an initial value */
847 if ( sym->ival && SPEC_SCLS(sym->etype)!=S_CODE) {
848 if (IS_AGGREGATE(sym->type))
849 work = initAggregates (sym,sym->ival,NULL);
851 work = newNode('=' ,newAst(EX_VALUE,symbolVal(sym)),
852 list2expr(sym->ival));
854 setAstLineno (work,sym->lineDef);
857 init = newNode(NULLOP,init,work);
866 /*-----------------------------------------------------------------*/
867 /* stringToSymbol - creates a symbol from a literal string */
868 /*-----------------------------------------------------------------*/
869 static value *stringToSymbol (value *val)
871 char name[SDCC_NAME_MAX+1];
872 static int charLbl = 0;
875 sprintf(name,"_str_%d",charLbl++);
876 sym = newSymbol(name,0); /* make it @ level 0 */
877 strcpy(sym->rname,name);
879 /* copy the type from the value passed */
880 sym->type = copyLinkChain(val->type);
881 sym->etype = getSpec(sym->type);
882 /* change to storage class & output class */
883 SPEC_SCLS(sym->etype) = S_CODE ;
884 SPEC_CVAL(sym->etype).v_char = SPEC_CVAL(val->etype).v_char ;
885 SPEC_STAT(sym->etype) = 1;
886 /* make the level & block = 0 */
887 sym->block = sym->level = 0;
890 sym->ival = newiList(INIT_NODE,newAst(EX_VALUE,val));
897 return symbolVal(sym);
901 /*-----------------------------------------------------------------*/
902 /* processBlockVars - will go thru the ast looking for block if */
903 /* a block is found then will allocate the syms */
904 /* will also gather the auto inits present */
905 /*-----------------------------------------------------------------*/
906 ast *processBlockVars ( ast *tree , int *stack, int action)
911 /* if this is a block */
912 if (tree->type == EX_OP && tree->opval.op == BLOCK ) {
915 if (action == ALLOCATE) {
916 autoInit = gatherAutoInit (tree->values.sym);
917 *stack += allocVariables (tree->values.sym);
919 /* if there are auto inits then do them */
921 tree->left = newNode(NULLOP,autoInit,tree->left);
922 } else /* action is deallocate */
923 deallocLocal (tree->values.sym) ;
926 processBlockVars (tree->left, stack, action);
927 processBlockVars (tree->right, stack, action);
931 /*-----------------------------------------------------------------*/
932 /* constExprValue - returns the value of a constant expression */
933 /*-----------------------------------------------------------------*/
934 value *constExprValue (ast *cexpr, int check)
936 cexpr = decorateType(resolveSymbols(cexpr));
938 /* if this is not a constant then */
939 if (!IS_LITERAL(cexpr->ftype)) {
940 /* then check if this is a literal array
942 if (SPEC_SCLS(cexpr->etype) == S_CODE &&
943 SPEC_CVAL(cexpr->etype).v_char &&
944 IS_ARRAY(cexpr->ftype)) {
945 value *val = valFromType(cexpr->ftype);
946 SPEC_SCLS(val->etype) = S_LITERAL;
947 val->sym =cexpr->opval.val->sym ;
948 val->sym->type = copyLinkChain(cexpr->ftype);
949 val->sym->etype = getSpec(val->sym->type);
950 strcpy(val->name,cexpr->opval.val->sym->rname);
954 /* if we are casting a literal value then */
955 if (IS_AST_OP(cexpr) &&
956 cexpr->opval.op == CAST &&
957 IS_LITERAL(cexpr->left->ftype))
958 return valCastLiteral(cexpr->ftype,
959 floatFromVal(cexpr->left->opval.val));
961 if (IS_AST_VALUE(cexpr))
962 return cexpr->opval.val;
965 werror(E_CONST_EXPECTED,"found expression");
970 /* return the value */
971 return cexpr->opval.val ;
975 /*-----------------------------------------------------------------*/
976 /* isLabelInAst - will return true if a given label is found */
977 /*-----------------------------------------------------------------*/
978 bool isLabelInAst (symbol *label, ast *tree)
980 if (!tree || IS_AST_VALUE(tree) || IS_AST_LINK(tree))
983 if (IS_AST_OP(tree) &&
984 tree->opval.op == LABEL &&
985 isSymbolEqual(AST_SYMBOL(tree->left),label))
988 return isLabelInAst(label,tree->right) &&
989 isLabelInAst(label,tree->left);
993 /*-----------------------------------------------------------------*/
994 /* isLoopCountable - return true if the loop count can be determi- */
995 /* -ned at compile time . */
996 /*-----------------------------------------------------------------*/
997 bool isLoopCountable (ast *initExpr, ast *condExpr, ast *loopExpr,
998 symbol **sym,ast **init, ast **end)
1001 /* the loop is considered countable if the following
1002 conditions are true :-
1004 a) initExpr :- <sym> = <const>
1005 b) condExpr :- <sym> < <const1>
1006 c) loopExpr :- <sym> ++
1009 /* first check the initExpr */
1010 if ( IS_AST_OP(initExpr) &&
1011 initExpr->opval.op == '=' && /* is assignment */
1012 IS_AST_SYM_VALUE(initExpr->left)) { /* left is a symbol */
1014 *sym = AST_SYMBOL(initExpr->left);
1015 *init= initExpr->right;
1020 /* for now the symbol has to be of
1022 if (!IS_INTEGRAL((*sym)->type))
1025 /* now check condExpr */
1026 if (IS_AST_OP(condExpr)) {
1028 switch (condExpr->opval.op) {
1030 if (IS_AST_SYM_VALUE(condExpr->left) &&
1031 isSymbolEqual (*sym,AST_SYMBOL(condExpr->left)) &&
1032 IS_AST_LIT_VALUE(condExpr->right)) {
1033 *end = condExpr->right;
1039 if (IS_AST_OP(condExpr->left) &&
1040 condExpr->left->opval.op == '>' &&
1041 IS_AST_LIT_VALUE(condExpr->left->right) &&
1042 IS_AST_SYM_VALUE(condExpr->left->left)&&
1043 isSymbolEqual (*sym,AST_SYMBOL(condExpr->left->left))) {
1045 *end = newNode('+', condExpr->left->right,
1046 newAst(EX_VALUE,constVal("1")));
1057 /* check loop expression is of the form <sym>++ */
1058 if (!IS_AST_OP(loopExpr))
1061 /* check if <sym> ++ */
1062 if (loopExpr->opval.op == INC_OP) {
1064 if (loopExpr->left) {
1066 if (IS_AST_SYM_VALUE(loopExpr->left) &&
1067 isSymbolEqual(*sym,AST_SYMBOL(loopExpr->left)))
1072 if (IS_AST_SYM_VALUE(loopExpr->right) &&
1073 isSymbolEqual(*sym,AST_SYMBOL(loopExpr->right)))
1080 if ( loopExpr->opval.op == ADD_ASSIGN ) {
1082 if (IS_AST_SYM_VALUE(loopExpr->left) &&
1083 isSymbolEqual(*sym,AST_SYMBOL(loopExpr->left)) &&
1084 IS_AST_LIT_VALUE(loopExpr->right) &&
1085 (int)AST_LIT_VALUE(loopExpr->right) != 1)
1093 /*-----------------------------------------------------------------*/
1094 /* astHasVolatile - returns true if ast contains any volatile */
1095 /*-----------------------------------------------------------------*/
1096 bool astHasVolatile (ast *tree)
1101 if (TETYPE(tree) && IS_VOLATILE(TETYPE(tree)))
1104 if (IS_AST_OP(tree))
1105 return astHasVolatile(tree->left) ||
1106 astHasVolatile(tree->right);
1111 /*-----------------------------------------------------------------*/
1112 /* astHasPointer - return true if the ast contains any ptr variable*/
1113 /*-----------------------------------------------------------------*/
1114 bool astHasPointer (ast *tree)
1119 if (IS_AST_LINK(tree))
1122 /* if we hit an array expression then check
1123 only the left side */
1124 if (IS_AST_OP(tree) && tree->opval.op == '[')
1125 return astHasPointer(tree->left);
1127 if (IS_AST_VALUE(tree))
1128 return IS_PTR(tree->ftype) || IS_ARRAY(tree->ftype);
1130 return astHasPointer(tree->left) ||
1131 astHasPointer(tree->right);
1135 /*-----------------------------------------------------------------*/
1136 /* astHasSymbol - return true if the ast has the given symbol */
1137 /*-----------------------------------------------------------------*/
1138 bool astHasSymbol (ast *tree, symbol *sym)
1140 if (!tree || IS_AST_LINK(tree))
1143 if (IS_AST_VALUE(tree)) {
1144 if (IS_AST_SYM_VALUE(tree))
1145 return isSymbolEqual(AST_SYMBOL(tree),sym);
1150 return astHasSymbol(tree->left,sym) ||
1151 astHasSymbol(tree->right,sym);
1154 /*-----------------------------------------------------------------*/
1155 /* isConformingBody - the loop body has to conform to a set of rules */
1156 /* for the loop to be considered reversible read on for rules */
1157 /*-----------------------------------------------------------------*/
1158 bool isConformingBody (ast *pbody, symbol *sym, ast *body)
1161 /* we are going to do a pre-order traversal of the
1162 tree && check for the following conditions. (essentially
1163 a set of very shallow tests )
1164 a) the sym passed does not participate in
1165 any arithmetic operation
1166 b) There are no function calls
1167 c) all jumps are within the body
1168 d) address of loop control variable not taken
1169 e) if an assignment has a pointer on the
1170 left hand side make sure right does not have
1171 loop control variable */
1173 /* if we reach the end or a leaf then true */
1174 if (!pbody || IS_AST_LINK(pbody) || IS_AST_VALUE(pbody))
1178 /* if anything else is "volatile" */
1179 if (IS_VOLATILE(TETYPE(pbody)))
1182 /* we will walk the body in a pre-order traversal for
1184 switch (pbody->opval.op) {
1185 /*------------------------------------------------------------------*/
1187 return isConformingBody (pbody->right,sym,body);
1189 /*------------------------------------------------------------------*/
1194 /*------------------------------------------------------------------*/
1195 case INC_OP: /* incerement operator unary so left only */
1198 /* sure we are not sym is not modified */
1200 IS_AST_SYM_VALUE(pbody->left) &&
1201 isSymbolEqual(AST_SYMBOL(pbody->left),sym))
1205 IS_AST_SYM_VALUE(pbody->right) &&
1206 isSymbolEqual(AST_SYMBOL(pbody->right),sym))
1211 /*------------------------------------------------------------------*/
1213 case '*' : /* can be unary : if right is null then unary operation */
1218 /* if right is NULL then unary operation */
1219 /*------------------------------------------------------------------*/
1220 /*----------------------------*/
1222 /*----------------------------*/
1223 if ( ! pbody->right ) {
1224 if (IS_AST_SYM_VALUE(pbody->left) &&
1225 isSymbolEqual(AST_SYMBOL(pbody->left),sym))
1228 return isConformingBody(pbody->left,sym,body) ;
1231 /*------------------------------------------------------------------*/
1239 if (IS_AST_SYM_VALUE(pbody->left) &&
1240 isSymbolEqual(AST_SYMBOL(pbody->left),sym))
1243 if (IS_AST_SYM_VALUE(pbody->right) &&
1244 isSymbolEqual(AST_SYMBOL(pbody->right),sym))
1247 return isConformingBody(pbody->left,sym,body) &&
1248 isConformingBody(pbody->right,sym,body);
1255 if (IS_AST_SYM_VALUE(pbody->left) &&
1256 isSymbolEqual(AST_SYMBOL(pbody->left),sym))
1258 return isConformingBody (pbody->left,sym,body);
1260 /*------------------------------------------------------------------*/
1272 case SIZEOF: /* evaluate wihout code generation */
1274 return isConformingBody(pbody->left,sym,body) &&
1275 isConformingBody(pbody->right,sym,body);
1277 /*------------------------------------------------------------------*/
1280 /* if left has a pointer & right has loop
1281 control variable then we cannot */
1282 if (astHasPointer(pbody->left) &&
1283 astHasSymbol (pbody->right,sym))
1285 if (astHasVolatile(pbody->left))
1288 if (IS_AST_SYM_VALUE(pbody->left) &&
1289 isSymbolEqual(AST_SYMBOL(pbody->left),sym))
1292 if (astHasVolatile(pbody->left))
1295 return isConformingBody(pbody->left,sym,body) &&
1296 isConformingBody(pbody->right,sym,body);
1307 assert("Parser should not have generated this\n");
1309 /*------------------------------------------------------------------*/
1310 /*----------------------------*/
1311 /* comma operator */
1312 /*----------------------------*/
1314 return isConformingBody(pbody->left,sym,body) &&
1315 isConformingBody(pbody->right,sym,body);
1317 /*------------------------------------------------------------------*/
1318 /*----------------------------*/
1320 /*----------------------------*/
1324 /*------------------------------------------------------------------*/
1325 /*----------------------------*/
1326 /* return statement */
1327 /*----------------------------*/
1332 if (isLabelInAst (AST_SYMBOL(pbody->left),body))
1337 if (astHasSymbol(pbody->left,sym))
1344 return isConformingBody(pbody->left,sym,body) &&
1345 isConformingBody(pbody->right,sym,body);
1351 /*-----------------------------------------------------------------*/
1352 /* isLoopReversible - takes a for loop as input && returns true */
1353 /* if the for loop is reversible. If yes will set the value of */
1354 /* the loop control var & init value & termination value */
1355 /*-----------------------------------------------------------------*/
1356 bool isLoopReversible (ast *loop, symbol **loopCntrl,
1357 ast **init, ast **end )
1359 /* if option says don't do it then don't */
1360 if (optimize.noLoopReverse)
1362 /* there are several tests to determine this */
1364 /* for loop has to be of the form
1365 for ( <sym> = <const1> ;
1366 [<sym> < <const2>] ;
1367 [<sym>++] | [<sym> += 1] | [<sym> = <sym> + 1] )
1369 if (! isLoopCountable (AST_FOR(loop,initExpr),
1370 AST_FOR(loop,condExpr),
1371 AST_FOR(loop,loopExpr),
1372 loopCntrl,init,end))
1375 /* now do some serious checking on the body of the loop
1378 return isConformingBody(loop->left,*loopCntrl,loop->left);
1382 /*-----------------------------------------------------------------*/
1383 /* replLoopSym - replace the loop sym by loop sym -1 */
1384 /*-----------------------------------------------------------------*/
1385 static void replLoopSym ( ast *body, symbol *sym)
1388 if (!body || IS_AST_LINK(body))
1391 if (IS_AST_SYM_VALUE(body)) {
1393 if (isSymbolEqual(AST_SYMBOL(body),sym)) {
1396 body->opval.op = '-';
1397 body->left = newAst(EX_VALUE,symbolVal(sym));
1398 body->right= newAst(EX_VALUE,constVal("1"));
1406 replLoopSym(body->left,sym);
1407 replLoopSym(body->right,sym);
1411 /*-----------------------------------------------------------------*/
1412 /* reverseLoop - do the actual loop reversal */
1413 /*-----------------------------------------------------------------*/
1414 ast *reverseLoop (ast *loop, symbol *sym, ast *init, ast *end)
1418 /* create the following tree
1423 if (sym) goto for_continue ;
1426 /* put it together piece by piece */
1427 rloop = newNode (NULLOP,
1428 createIf(newAst(EX_VALUE,symbolVal(sym)),
1431 symbolVal(AST_FOR(loop,continueLabel))),
1434 newAst(EX_VALUE,symbolVal(sym)),
1439 replLoopSym(loop->left, sym);
1441 rloop = newNode(NULLOP,
1443 newAst(EX_VALUE,symbolVal(sym)),
1444 newNode('-',end,init)),
1445 createLabel(AST_FOR(loop,continueLabel),
1450 newAst(EX_VALUE,symbolVal(sym)),
1451 newAst(EX_VALUE,constVal("1"))),
1454 return decorateType(rloop);
1458 /*-----------------------------------------------------------------*/
1459 /* decorateType - compute type for this tree also does type cheking*/
1460 /* this is done bottom up, since type have to flow upwards*/
1461 /* it also does constant folding, and paramater checking */
1462 /*-----------------------------------------------------------------*/
1463 ast *decorateType (ast *tree)
1471 /* if already has type then do nothing */
1472 if ( tree->decorated )
1475 tree->decorated = 1;
1477 /* print the line */
1478 /* if not block & function */
1479 if ( tree->type == EX_OP &&
1480 ( tree->opval.op != FUNCTION &&
1481 tree->opval.op != BLOCK &&
1482 tree->opval.op != NULLOP )) {
1483 filename = tree->filename ;
1484 lineno = tree->lineno ;
1487 /* if any child is an error | this one is an error do nothing */
1488 if ( tree->isError ||
1489 ( tree->left && tree->left->isError) ||
1490 ( tree->right && tree->right->isError ))
1493 /*------------------------------------------------------------------*/
1494 /*----------------------------*/
1495 /* leaf has been reached */
1496 /*----------------------------*/
1497 /* if this is of type value */
1498 /* just get the type */
1499 if ( tree->type == EX_VALUE ) {
1501 if ( IS_LITERAL(tree->opval.val->etype) ) {
1503 /* if this is a character array then declare it */
1504 if (IS_ARRAY(tree->opval.val->type))
1505 tree->opval.val = stringToSymbol(tree->opval.val);
1507 /* otherwise just copy the type information */
1508 COPYTYPE(TTYPE(tree),TETYPE(tree),tree->opval.val->type);
1512 if ( tree->opval.val->sym ) {
1513 /* if the undefined flag is set then give error message */
1514 if (tree->opval.val->sym->undefined ) {
1515 werror(E_ID_UNDEF,tree->opval.val->sym->name) ;
1517 TTYPE(tree) = TETYPE(tree) =
1518 tree->opval.val->type = tree->opval.val->sym->type =
1519 tree->opval.val->etype = tree->opval.val->sym->etype =
1520 copyLinkChain(intType);
1524 /* if impilicit i.e. struct/union member then no type */
1525 if (tree->opval.val->sym->implicit )
1526 TTYPE(tree) = TETYPE(tree) = NULL ;
1530 /* else copy the type */
1531 COPYTYPE(TTYPE(tree),TETYPE(tree),tree->opval.val->type);
1533 /* and mark it as referenced */
1534 tree->opval.val->sym->isref = 1;
1535 /* if this is of type function or function pointer */
1536 if (funcInChain(tree->opval.val->type)) {
1537 tree->hasVargs = tree->opval.val->sym->hasVargs;
1538 tree->args = copyValueChain(tree->opval.val->sym->args) ;
1548 /* if type link for the case of cast */
1549 if ( tree->type == EX_LINK ) {
1550 COPYTYPE(TTYPE(tree),TETYPE(tree),tree->opval.lnk);
1557 dtl = decorateType (tree->left);
1558 dtr = decorateType (tree->right);
1560 /* this is to take care of situations
1561 when the tree gets rewritten */
1562 if (dtl != tree->left)
1564 if (dtr != tree->right)
1568 /* depending on type of operator do */
1570 switch (tree->opval.op) {
1571 /*------------------------------------------------------------------*/
1572 /*----------------------------*/
1574 /*----------------------------*/
1577 /* determine which is the array & which the index */
1578 if ((IS_ARRAY(RTYPE(tree)) || IS_PTR(RTYPE(tree))) && IS_INTEGRAL(LTYPE(tree))) {
1580 ast *tempTree = tree->left ;
1581 tree->left = tree->right ;
1582 tree->right= tempTree ;
1585 /* first check if this is a array or a pointer */
1586 if ( (!IS_ARRAY(LTYPE(tree))) && (!IS_PTR(LTYPE(tree)))) {
1587 werror(E_NEED_ARRAY_PTR,"[]");
1588 goto errorTreeReturn ;
1591 /* check if the type of the idx */
1592 if (!IS_INTEGRAL(RTYPE(tree))) {
1593 werror(E_IDX_NOT_INT);
1594 goto errorTreeReturn ;
1597 /* if the left is an rvalue then error */
1599 werror(E_LVALUE_REQUIRED,"array access");
1600 goto errorTreeReturn ;
1603 COPYTYPE(TTYPE(tree),TETYPE(tree),LTYPE(tree)->next);
1606 /*------------------------------------------------------------------*/
1607 /*----------------------------*/
1609 /*----------------------------*/
1611 /* if this is not a structure */
1612 if (!IS_STRUCT(LTYPE(tree))) {
1613 werror(E_STRUCT_UNION,".");
1614 goto errorTreeReturn ;
1616 TTYPE(tree) = structElemType (LTYPE(tree),
1617 (tree->right->type == EX_VALUE ?
1618 tree->right->opval.val : NULL ),&tree->args);
1619 TETYPE(tree) = getSpec(TTYPE(tree));
1622 /*------------------------------------------------------------------*/
1623 /*----------------------------*/
1624 /* struct/union pointer */
1625 /*----------------------------*/
1627 /* if not pointer to a structure */
1628 if (!IS_PTR(LTYPE(tree))) {
1630 goto errorTreeReturn ;
1633 if (!IS_STRUCT(LTYPE(tree)->next)) {
1634 werror(E_STRUCT_UNION,"->");
1635 goto errorTreeReturn ;
1638 TTYPE(tree) = structElemType (LTYPE(tree)->next,
1639 (tree->right->type == EX_VALUE ?
1640 tree->right->opval.val : NULL ),&tree->args);
1641 TETYPE(tree) = getSpec(TTYPE(tree));
1644 /*------------------------------------------------------------------*/
1645 /*----------------------------*/
1646 /* ++/-- operation */
1647 /*----------------------------*/
1648 case INC_OP: /* incerement operator unary so left only */
1651 link *ltc = (tree->right ? RTYPE(tree) : LTYPE(tree) );
1652 COPYTYPE(TTYPE(tree),TETYPE(tree),ltc);
1653 if (!tree->initMode && IS_CONSTANT(TETYPE(tree)))
1654 werror(E_CODE_WRITE,"++/--");
1663 /*------------------------------------------------------------------*/
1664 /*----------------------------*/
1666 /*----------------------------*/
1667 case '&': /* can be unary */
1668 /* if right is NULL then unary operation */
1669 if ( tree->right ) /* not an unary operation */ {
1671 if (!IS_INTEGRAL(LTYPE(tree)) || !IS_INTEGRAL(RTYPE(tree))) {
1672 werror(E_BITWISE_OP);
1673 werror(E_CONTINUE,"left & right types are ");
1674 printTypeChain(LTYPE(tree),stderr);
1675 fprintf(stderr,",");
1676 printTypeChain(RTYPE(tree),stderr);
1677 fprintf(stderr,"\n");
1678 goto errorTreeReturn ;
1681 /* if they are both literal */
1682 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1683 tree->type = EX_VALUE ;
1684 tree->opval.val = valBitwise (valFromType(LETYPE(tree)),
1685 valFromType(RETYPE(tree)),'&');
1687 tree->right = tree->left = NULL;
1688 TETYPE(tree) = tree->opval.val->etype ;
1689 TTYPE(tree) = tree->opval.val->type;
1693 /* see if this is a GETHBIT operation if yes
1696 ast *otree = optimizeGetHbit(tree);
1699 return decorateType(otree);
1702 /* if right or left is literal then result of that type*/
1703 if (IS_LITERAL(RTYPE(tree))) {
1705 TTYPE(tree) = copyLinkChain(RTYPE(tree));
1706 TETYPE(tree) = getSpec(TTYPE(tree));
1707 SPEC_SCLS(TETYPE(tree)) = S_AUTO;
1710 if (IS_LITERAL(LTYPE(tree))) {
1711 TTYPE(tree) = copyLinkChain(LTYPE(tree));
1712 TETYPE(tree) = getSpec(TTYPE(tree));
1713 SPEC_SCLS(TETYPE(tree)) = S_AUTO;
1718 computeType (LTYPE(tree), RTYPE(tree));
1719 TETYPE(tree) = getSpec(TTYPE(tree));
1722 LRVAL(tree) = RRVAL(tree) = 1;
1726 /*------------------------------------------------------------------*/
1727 /*----------------------------*/
1729 /*----------------------------*/
1731 p->class = DECLARATOR;
1732 /* if bit field then error */
1733 if (IS_BITVAR(tree->left->etype)) {
1734 werror (E_ILLEGAL_ADDR,"addrress of bit variable");
1735 goto errorTreeReturn ;
1738 if (SPEC_SCLS(tree->left->etype)== S_REGISTER ) {
1739 werror (E_ILLEGAL_ADDR,"address of register variable");
1740 goto errorTreeReturn;
1743 if (IS_FUNC(LTYPE(tree))) {
1744 werror(E_ILLEGAL_ADDR,"address of function");
1745 goto errorTreeReturn ;
1749 werror(E_LVALUE_REQUIRED,"address of");
1750 goto errorTreeReturn ;
1752 if (SPEC_SCLS(tree->left->etype) == S_CODE) {
1753 DCL_TYPE(p) = CPOINTER ;
1754 DCL_PTR_CONST(p) = 1;
1757 if (SPEC_SCLS(tree->left->etype) == S_XDATA)
1758 DCL_TYPE(p) = FPOINTER;
1760 if (SPEC_SCLS(tree->left->etype) == S_XSTACK )
1761 DCL_TYPE(p) = PPOINTER ;
1763 if (SPEC_SCLS(tree->left->etype) == S_IDATA)
1764 DCL_TYPE(p) = IPOINTER ;
1766 DCL_TYPE(p) = POINTER ;
1768 if (IS_AST_SYM_VALUE(tree->left)) {
1769 AST_SYMBOL(tree->left)->addrtaken = 1;
1770 AST_SYMBOL(tree->left)->allocreq = 1;
1773 p->next = LTYPE(tree);
1775 TETYPE(tree) = getSpec(TTYPE(tree));
1776 DCL_PTR_CONST(p) = SPEC_CONST(TETYPE(tree));
1777 DCL_PTR_VOLATILE(p) = SPEC_VOLATILE(TETYPE(tree));
1782 /*------------------------------------------------------------------*/
1783 /*----------------------------*/
1785 /*----------------------------*/
1787 /* if the rewrite succeeds then don't go any furthur */
1789 ast *wtree = optimizeRRCRLC ( tree );
1791 return decorateType(wtree) ;
1793 /*------------------------------------------------------------------*/
1794 /*----------------------------*/
1796 /*----------------------------*/
1798 if (!IS_INTEGRAL(LTYPE(tree)) || !IS_INTEGRAL(RTYPE(tree))) {
1799 werror(E_BITWISE_OP);
1800 werror(E_CONTINUE,"left & right types are ");
1801 printTypeChain(LTYPE(tree),stderr);
1802 fprintf(stderr,",");
1803 printTypeChain(RTYPE(tree),stderr);
1804 fprintf(stderr,"\n");
1805 goto errorTreeReturn ;
1808 /* if they are both literal then */
1809 /* rewrite the tree */
1810 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1811 tree->type = EX_VALUE ;
1812 tree->opval.val = valBitwise (valFromType(LETYPE(tree)),
1813 valFromType(RETYPE(tree)),
1815 tree->right = tree->left = NULL;
1816 TETYPE(tree) = tree->opval.val->etype;
1817 TTYPE(tree) = tree->opval.val->type;
1820 LRVAL(tree) = RRVAL(tree) = 1;
1821 TETYPE(tree) = getSpec (TTYPE(tree) =
1822 computeType(LTYPE(tree),
1825 /*------------------------------------------------------------------*/
1826 /*----------------------------*/
1828 /*----------------------------*/
1830 if (!IS_ARITHMETIC(LTYPE(tree)) || !IS_ARITHMETIC(RTYPE(tree))) {
1831 werror(E_INVALID_OP,"divide");
1832 goto errorTreeReturn ;
1834 /* if they are both literal then */
1835 /* rewrite the tree */
1836 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1837 tree->type = EX_VALUE ;
1838 tree->opval.val = valDiv (valFromType(LETYPE(tree)),
1839 valFromType(RETYPE(tree)));
1840 tree->right = tree->left = NULL;
1841 TETYPE(tree) = getSpec(TTYPE(tree) =
1842 tree->opval.val->type);
1845 LRVAL(tree) = RRVAL(tree) = 1;
1846 TETYPE(tree) = getSpec (TTYPE(tree) =
1847 computeType(LTYPE(tree),
1851 /*------------------------------------------------------------------*/
1852 /*----------------------------*/
1854 /*----------------------------*/
1856 if (!IS_INTEGRAL(LTYPE(tree)) || !IS_INTEGRAL(RTYPE(tree))) {
1857 werror(E_BITWISE_OP);
1858 werror(E_CONTINUE,"left & right types are ");
1859 printTypeChain(LTYPE(tree),stderr);
1860 fprintf(stderr,",");
1861 printTypeChain(RTYPE(tree),stderr);
1862 fprintf(stderr,"\n");
1863 goto errorTreeReturn ;
1865 /* if they are both literal then */
1866 /* rewrite the tree */
1867 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1868 tree->type = EX_VALUE ;
1869 tree->opval.val = valMod (valFromType(LETYPE(tree)),
1870 valFromType(RETYPE(tree)));
1871 tree->right = tree->left = NULL;
1872 TETYPE(tree) = getSpec(TTYPE(tree) =
1873 tree->opval.val->type);
1876 LRVAL(tree) = RRVAL(tree) = 1;
1877 TETYPE(tree) = getSpec (TTYPE(tree) =
1878 computeType(LTYPE(tree),
1882 /*------------------------------------------------------------------*/
1883 /*----------------------------*/
1884 /* address dereference */
1885 /*----------------------------*/
1886 case '*': /* can be unary : if right is null then unary operation */
1887 if ( ! tree->right ) {
1888 if (!IS_PTR(LTYPE(tree)) && !IS_ARRAY(LTYPE(tree))) {
1890 goto errorTreeReturn ;
1894 werror(E_LVALUE_REQUIRED,"pointer deref");
1895 goto errorTreeReturn ;
1897 TTYPE(tree) = copyLinkChain ((IS_PTR(LTYPE(tree)) || IS_ARRAY(LTYPE(tree))) ?
1898 LTYPE(tree)->next : NULL );
1899 TETYPE(tree) = getSpec(TTYPE(tree));
1900 tree->args = tree->left->args ;
1901 tree->hasVargs = tree->left->hasVargs ;
1902 SPEC_CONST(TETYPE(tree)) = DCL_PTR_CONST(LTYPE(tree));
1906 /*------------------------------------------------------------------*/
1907 /*----------------------------*/
1908 /* multiplication */
1909 /*----------------------------*/
1910 if (!IS_ARITHMETIC(LTYPE(tree)) || !IS_ARITHMETIC(RTYPE(tree))) {
1911 werror(E_INVALID_OP,"multiplication");
1912 goto errorTreeReturn ;
1915 /* if they are both literal then */
1916 /* rewrite the tree */
1917 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1918 tree->type = EX_VALUE ;
1919 tree->opval.val = valMult (valFromType(LETYPE(tree)),
1920 valFromType(RETYPE(tree)));
1921 tree->right = tree->left = NULL;
1922 TETYPE(tree) = getSpec(TTYPE(tree) =
1923 tree->opval.val->type);
1927 /* if left is a literal exchange left & right */
1928 if (IS_LITERAL(LTYPE(tree))) {
1929 ast *tTree = tree->left ;
1930 tree->left = tree->right ;
1931 tree->right= tTree ;
1934 LRVAL(tree) = RRVAL(tree) = 1;
1935 TETYPE(tree) = getSpec (TTYPE(tree) =
1936 computeType(LTYPE(tree),
1940 /*------------------------------------------------------------------*/
1941 /*----------------------------*/
1942 /* unary '+' operator */
1943 /*----------------------------*/
1946 if ( ! tree->right ) {
1947 if (!IS_INTEGRAL(LTYPE(tree))) {
1948 werror(E_UNARY_OP,'+');
1949 goto errorTreeReturn ;
1952 /* if left is a literal then do it */
1953 if (IS_LITERAL(LTYPE(tree))) {
1954 tree->type = EX_VALUE ;
1955 tree->opval.val = valFromType(LETYPE(tree));
1957 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
1961 COPYTYPE(TTYPE(tree),TETYPE(tree),LTYPE(tree));
1965 /*------------------------------------------------------------------*/
1966 /*----------------------------*/
1968 /*----------------------------*/
1970 /* this is not a unary operation */
1971 /* if both pointers then problem */
1972 if ((IS_PTR(LTYPE(tree)) || IS_ARRAY(LTYPE(tree))) &&
1973 (IS_PTR(RTYPE(tree)) || IS_ARRAY(RTYPE(tree)))) {
1974 werror(E_PTR_PLUS_PTR);
1975 goto errorTreeReturn ;
1978 if (!IS_ARITHMETIC(LTYPE(tree)) &&
1979 !IS_PTR(LTYPE(tree)) && !IS_ARRAY(LTYPE(tree))) {
1980 werror(E_PLUS_INVALID,"+");
1981 goto errorTreeReturn ;
1984 if (!IS_ARITHMETIC(RTYPE(tree)) &&
1985 !IS_PTR(RTYPE(tree)) && !IS_ARRAY(RTYPE(tree))) {
1986 werror(E_PLUS_INVALID,"+");
1987 goto errorTreeReturn;
1989 /* if they are both literal then */
1990 /* rewrite the tree */
1991 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
1992 tree->type = EX_VALUE ;
1993 tree->opval.val = valPlus (valFromType(LETYPE(tree)),
1994 valFromType(RETYPE(tree)));
1995 tree->right = tree->left = NULL;
1996 TETYPE(tree) = getSpec(TTYPE(tree) =
1997 tree->opval.val->type);
2001 /* if the right is a pointer or left is a literal
2002 xchange left & right */
2003 if (IS_ARRAY(RTYPE(tree)) ||
2004 IS_PTR(RTYPE(tree)) ||
2005 IS_LITERAL(LTYPE(tree))) {
2006 ast *tTree = tree->left ;
2007 tree->left = tree->right ;
2008 tree->right= tTree ;
2011 LRVAL(tree) = RRVAL(tree) = 1;
2012 /* if the left is a pointer */
2013 if (IS_PTR(LTYPE(tree)))
2014 TETYPE(tree) = getSpec(TTYPE(tree) =
2017 TETYPE(tree) = getSpec(TTYPE(tree) =
2018 computeType(LTYPE(tree),
2022 /*------------------------------------------------------------------*/
2023 /*----------------------------*/
2025 /*----------------------------*/
2026 case '-' : /* can be unary */
2027 /* if right is null then unary */
2028 if ( ! tree->right ) {
2030 if (!IS_ARITHMETIC(LTYPE(tree))) {
2031 werror(E_UNARY_OP,tree->opval.op);
2032 goto errorTreeReturn ;
2035 /* if left is a literal then do it */
2036 if (IS_LITERAL(LTYPE(tree))) {
2037 tree->type = EX_VALUE ;
2038 tree->opval.val = valUnaryPM(valFromType(LETYPE(tree)));
2040 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
2044 TTYPE(tree) = LTYPE(tree);
2048 /*------------------------------------------------------------------*/
2049 /*----------------------------*/
2051 /*----------------------------*/
2053 if (!(IS_PTR(LTYPE(tree)) ||
2054 IS_ARRAY(LTYPE(tree)) ||
2055 IS_ARITHMETIC(LTYPE(tree)))) {
2056 werror(E_PLUS_INVALID,"-");
2057 goto errorTreeReturn ;
2060 if (!(IS_PTR(RTYPE(tree)) ||
2061 IS_ARRAY(RTYPE(tree)) ||
2062 IS_ARITHMETIC(RTYPE(tree)))) {
2063 werror(E_PLUS_INVALID,"-");
2064 goto errorTreeReturn ;
2067 if ( (IS_PTR(LTYPE(tree)) || IS_ARRAY(LTYPE(tree))) &&
2068 ! (IS_PTR(RTYPE(tree)) || IS_ARRAY(RTYPE(tree)) ||
2069 IS_INTEGRAL(RTYPE(tree))) ) {
2070 werror(E_PLUS_INVALID,"-");
2071 goto errorTreeReturn ;
2074 /* if they are both literal then */
2075 /* rewrite the tree */
2076 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
2077 tree->type = EX_VALUE ;
2078 tree->opval.val = valMinus (valFromType(LETYPE(tree)),
2079 valFromType(RETYPE(tree)));
2080 tree->right = tree->left = NULL;
2081 TETYPE(tree) = getSpec(TTYPE(tree) =
2082 tree->opval.val->type);
2086 /* if the left & right are equal then zero */
2087 if (isAstEqual(tree->left,tree->right)) {
2088 tree->type = EX_VALUE;
2089 tree->left = tree->right = NULL;
2090 tree->opval.val = constVal("0");
2091 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
2095 /* if both of them are pointers or arrays then */
2096 /* the result is going to be an integer */
2097 if (( IS_ARRAY(LTYPE(tree)) || IS_PTR(LTYPE(tree))) &&
2098 ( IS_ARRAY(RTYPE(tree)) || IS_PTR(RTYPE(tree))))
2099 TETYPE(tree) = TTYPE(tree) = newIntLink();
2101 /* if only the left is a pointer */
2102 /* then result is a pointer */
2103 if (IS_PTR(LTYPE(tree)) || IS_ARRAY(LTYPE(tree)))
2104 TETYPE(tree) = getSpec(TTYPE(tree) =
2107 TETYPE(tree) = getSpec (TTYPE(tree) =
2108 computeType(LTYPE(tree),
2110 LRVAL(tree) = RRVAL(tree) = 1;
2113 /*------------------------------------------------------------------*/
2114 /*----------------------------*/
2116 /*----------------------------*/
2118 /* can be only integral type */
2119 if (!IS_INTEGRAL(LTYPE(tree))) {
2120 werror(E_UNARY_OP,tree->opval.op);
2121 goto errorTreeReturn ;
2124 /* if left is a literal then do it */
2125 if (IS_LITERAL(LTYPE(tree))) {
2126 tree->type = EX_VALUE ;
2127 tree->opval.val = valComplement(valFromType(LETYPE(tree)));
2129 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
2133 COPYTYPE(TTYPE(tree),TETYPE(tree),LTYPE(tree));
2136 /*------------------------------------------------------------------*/
2137 /*----------------------------*/
2139 /*----------------------------*/
2141 /* can be pointer */
2142 if (!IS_ARITHMETIC(LTYPE(tree)) &&
2143 !IS_PTR(LTYPE(tree)) &&
2144 !IS_ARRAY(LTYPE(tree))) {
2145 werror(E_UNARY_OP,tree->opval.op);
2146 goto errorTreeReturn ;
2149 /* if left is a literal then do it */
2150 if (IS_LITERAL(LTYPE(tree))) {
2151 tree->type = EX_VALUE ;
2152 tree->opval.val = valNot(valFromType(LETYPE(tree)));
2154 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
2158 TTYPE(tree) = TETYPE(tree) = newCharLink();
2161 /*------------------------------------------------------------------*/
2162 /*----------------------------*/
2164 /*----------------------------*/
2167 TTYPE(tree) = LTYPE(tree);
2168 TETYPE(tree) = LETYPE(tree);
2172 TTYPE(tree) = TETYPE(tree) = newCharLink();
2177 if (!IS_INTEGRAL(LTYPE(tree)) || !IS_INTEGRAL(tree->left->etype)) {
2178 werror(E_SHIFT_OP_INVALID);
2179 werror(E_CONTINUE,"left & right types are ");
2180 printTypeChain(LTYPE(tree),stderr);
2181 fprintf(stderr,",");
2182 printTypeChain(RTYPE(tree),stderr);
2183 fprintf(stderr,"\n");
2184 goto errorTreeReturn ;
2187 /* if they are both literal then */
2188 /* rewrite the tree */
2189 if (IS_LITERAL(RTYPE(tree)) && IS_LITERAL(LTYPE(tree))) {
2190 tree->type = EX_VALUE ;
2191 tree->opval.val = valShift (valFromType(LETYPE(tree)),
2192 valFromType(RETYPE(tree)),
2193 (tree->opval.op == LEFT_OP ? 1 : 0));
2194 tree->right = tree->left = NULL;
2195 TETYPE(tree) = getSpec(TTYPE(tree) =
2196 tree->opval.val->type);
2199 /* if only the right side is a literal & we are
2200 shifting more than size of the left operand then zero */
2201 if (IS_LITERAL(RTYPE(tree)) &&
2202 ((int)floatFromVal( valFromType(RETYPE(tree)))) >=
2203 (getSize(LTYPE(tree))*8)) {
2204 werror(W_SHIFT_CHANGED,
2205 (tree->opval.op == LEFT_OP ? "left" : "right"));
2206 tree->type = EX_VALUE;
2207 tree->left = tree->right = NULL;
2208 tree->opval.val = constVal("0");
2209 TETYPE(tree) = TTYPE(tree) = tree->opval.val->type;
2212 LRVAL(tree) = RRVAL(tree) = 1;
2213 COPYTYPE(TTYPE(tree),TETYPE(tree),LTYPE(tree));
2216 /*------------------------------------------------------------------*/
2217 /*----------------------------*/
2219 /*----------------------------*/
2220 case CAST : /* change the type */
2221 /* cannot cast to an aggregate type */
2222 if (IS_AGGREGATE(LTYPE(tree))) {
2223 werror(E_CAST_ILLEGAL);
2224 goto errorTreeReturn ;
2227 /* if the right is a literal replace the tree */
2228 if (IS_LITERAL(RETYPE(tree)) && !IS_PTR(LTYPE(tree))) {
2229 tree->type = EX_VALUE ;
2231 valCastLiteral(LTYPE(tree),
2232 floatFromVal(valFromType(RETYPE(tree))));
2235 TTYPE(tree) = tree->opval.val->type;
2238 TTYPE(tree) = LTYPE(tree);
2242 TETYPE(tree) = getSpec(TTYPE(tree));
2246 /*------------------------------------------------------------------*/
2247 /*----------------------------*/
2248 /* logical &&, || */
2249 /*----------------------------*/
2252 /* each must me arithmetic type or be a pointer */
2253 if (!IS_PTR(LTYPE(tree)) &&
2254 !IS_ARRAY(LTYPE(tree)) &&
2255 !IS_INTEGRAL(LTYPE(tree))) {
2256 werror(E_COMPARE_OP);
2257 goto errorTreeReturn ;
2260 if (!IS_PTR(RTYPE(tree)) &&
2261 !IS_ARRAY(RTYPE(tree)) &&
2262 !IS_INTEGRAL(RTYPE(tree))) {
2263 werror(E_COMPARE_OP);
2264 goto errorTreeReturn ;
2266 /* if they are both literal then */
2267 /* rewrite the tree */
2268 if (IS_LITERAL(RTYPE(tree)) &&
2269 IS_LITERAL(LTYPE(tree))) {
2270 tree->type = EX_VALUE ;
2271 tree->opval.val = valLogicAndOr (valFromType(LETYPE(tree)),
2272 valFromType(RETYPE(tree)),
2274 tree->right = tree->left = NULL;
2275 TETYPE(tree) = getSpec(TTYPE(tree) =
2276 tree->opval.val->type);
2279 LRVAL(tree) = RRVAL(tree) = 1;
2280 TTYPE(tree) = TETYPE(tree) = newCharLink();
2283 /*------------------------------------------------------------------*/
2284 /*----------------------------*/
2285 /* comparison operators */
2286 /*----------------------------*/
2294 ast *lt = optimizeCompare(tree);
2300 /* if they are pointers they must be castable */
2301 if ( IS_PTR(LTYPE(tree)) && IS_PTR(RTYPE(tree))) {
2302 if (checkType(LTYPE(tree),RTYPE(tree)) == 0) {
2303 werror(E_COMPARE_OP);
2304 fprintf(stderr,"comparing type ");
2305 printTypeChain(LTYPE(tree),stderr);
2306 fprintf(stderr,"to type ");
2307 printTypeChain(RTYPE(tree),stderr);
2308 fprintf(stderr,"\n");
2309 goto errorTreeReturn ;
2312 /* else they should be promotable to one another */
2314 if (!( ( IS_PTR(LTYPE(tree)) && IS_LITERAL(RTYPE(tree))) ||
2315 ( IS_PTR(RTYPE(tree)) && IS_LITERAL(LTYPE(tree)))))
2317 if (checkType (LTYPE(tree),RTYPE(tree)) == 0 ) {
2318 werror(E_COMPARE_OP);
2319 fprintf(stderr,"comparing type ");
2320 printTypeChain(LTYPE(tree),stderr);
2321 fprintf(stderr,"to type ");
2322 printTypeChain(RTYPE(tree),stderr);
2323 fprintf(stderr,"\n");
2324 goto errorTreeReturn ;
2328 /* if they are both literal then */
2329 /* rewrite the tree */
2330 if (IS_LITERAL(RTYPE(tree)) &&
2331 IS_LITERAL(LTYPE(tree))) {
2332 tree->type = EX_VALUE ;
2333 tree->opval.val = valCompare (valFromType(LETYPE(tree)),
2334 valFromType(RETYPE(tree)),
2336 tree->right = tree->left = NULL;
2337 TETYPE(tree) = getSpec(TTYPE(tree) =
2338 tree->opval.val->type);
2341 LRVAL(tree) = RRVAL(tree) = 1;
2342 TTYPE(tree) = TETYPE(tree) = newCharLink();
2345 /*------------------------------------------------------------------*/
2346 /*----------------------------*/
2348 /*----------------------------*/
2349 case SIZEOF : /* evaluate wihout code generation */
2350 /* change the type to a integer */
2351 tree->type = EX_VALUE;
2352 sprintf(buffer,"%d",(getSize(tree->right->ftype)));
2353 tree->opval.val = constVal(buffer);
2354 tree->right = tree->left = NULL;
2355 TETYPE(tree) = getSpec(TTYPE(tree) =
2356 tree->opval.val->type);
2359 /*------------------------------------------------------------------*/
2360 /*----------------------------*/
2361 /* conditional operator '?' */
2362 /*----------------------------*/
2364 /* the type is one on the left */
2365 TTYPE(tree) = LTYPE(tree);
2366 TETYPE(tree)= getSpec (TTYPE(tree));
2370 /* if they don't match we have a problem */
2371 if (checkType( LTYPE(tree), RTYPE(tree)) == 0) {
2372 werror(E_TYPE_MISMATCH,"conditional operator"," ");
2373 goto errorTreeReturn ;
2376 TTYPE(tree) = computeType(LTYPE(tree),RTYPE(tree));
2377 TETYPE(tree)= getSpec(TTYPE(tree));
2381 /*------------------------------------------------------------------*/
2382 /*----------------------------*/
2383 /* assignment operators */
2384 /*----------------------------*/
2387 /* for these it must be both must be integral */
2388 if (!IS_ARITHMETIC(LTYPE(tree)) ||
2389 !IS_ARITHMETIC(RTYPE(tree))) {
2390 werror (E_OPS_INTEGRAL);
2391 goto errorTreeReturn ;
2394 TETYPE(tree) = getSpec(TTYPE(tree) = LTYPE(tree));
2396 if (!tree->initMode && IS_CONSTANT(LETYPE(tree)))
2397 werror(E_CODE_WRITE," ");
2400 werror(E_LVALUE_REQUIRED,"*= or /=");
2401 goto errorTreeReturn ;
2411 /* for these it must be both must be integral */
2412 if (!IS_INTEGRAL(LTYPE(tree)) ||
2413 !IS_INTEGRAL(RTYPE(tree))) {
2414 werror (E_OPS_INTEGRAL);
2415 goto errorTreeReturn ;
2418 TETYPE(tree) = getSpec(TTYPE(tree) = LTYPE(tree));
2420 if (!tree->initMode && IS_CONSTANT(LETYPE(tree)))
2421 werror(E_CODE_WRITE," ");
2424 werror(E_LVALUE_REQUIRED,"&= or |= or ^= or >>= or <<=");
2425 goto errorTreeReturn ;
2430 /*------------------------------------------------------------------*/
2431 /*----------------------------*/
2433 /*----------------------------*/
2435 if (!(IS_PTR(LTYPE(tree)) ||
2436 IS_ARITHMETIC(LTYPE(tree)))) {
2437 werror(E_PLUS_INVALID,"-=");
2438 goto errorTreeReturn ;
2441 if (!(IS_PTR(RTYPE(tree)) ||
2442 IS_ARITHMETIC(RTYPE(tree)))) {
2443 werror(E_PLUS_INVALID,"-=");
2444 goto errorTreeReturn ;
2447 TETYPE(tree) = getSpec (TTYPE(tree) =
2448 computeType(LTYPE(tree),
2451 if (!tree->initMode && IS_CONSTANT(LETYPE(tree)))
2452 werror(E_CODE_WRITE," ");
2455 werror(E_LVALUE_REQUIRED,"-=");
2456 goto errorTreeReturn ;
2461 /*------------------------------------------------------------------*/
2462 /*----------------------------*/
2464 /*----------------------------*/
2466 /* this is not a unary operation */
2467 /* if both pointers then problem */
2468 if (IS_PTR(LTYPE(tree)) && IS_PTR(RTYPE(tree)) ) {
2469 werror(E_PTR_PLUS_PTR);
2470 goto errorTreeReturn ;
2473 if (!IS_ARITHMETIC(LTYPE(tree)) && !IS_PTR(LTYPE(tree))) {
2474 werror(E_PLUS_INVALID,"+=");
2475 goto errorTreeReturn ;
2478 if (!IS_ARITHMETIC(RTYPE(tree)) && !IS_PTR(RTYPE(tree))) {
2479 werror(E_PLUS_INVALID,"+=");
2480 goto errorTreeReturn;
2483 TETYPE(tree) = getSpec (TTYPE(tree) =
2484 computeType(LTYPE(tree),
2487 if (!tree->initMode && IS_CONSTANT(LETYPE(tree)))
2488 werror(E_CODE_WRITE," ");
2491 werror(E_LVALUE_REQUIRED,"+=");
2492 goto errorTreeReturn ;
2495 tree->right = decorateType(newNode('+',copyAst(tree->left),tree->right));
2496 tree->opval.op = '=';
2499 /*------------------------------------------------------------------*/
2500 /*----------------------------*/
2501 /* straight assignemnt */
2502 /*----------------------------*/
2504 /* cannot be an aggregate */
2505 if (IS_AGGREGATE(LTYPE(tree))) {
2506 werror(E_AGGR_ASSIGN);
2507 goto errorTreeReturn;
2510 /* they should either match or be castable */
2511 if (checkType (LTYPE(tree),RTYPE(tree)) == 0) {
2512 werror(E_TYPE_MISMATCH,"assignment"," ");
2513 fprintf(stderr,"type --> '");
2514 printTypeChain (RTYPE(tree),stderr); fprintf(stderr,"' ");
2515 fprintf(stderr,"assigned to type --> '");
2516 printTypeChain (LTYPE(tree),stderr); fprintf(stderr,"'\n");
2517 goto errorTreeReturn ;
2520 /* if the left side of the tree is of type void
2521 then report error */
2522 if (IS_VOID(LTYPE(tree))) {
2523 werror(E_CAST_ZERO);
2524 fprintf(stderr,"type --> '");
2525 printTypeChain (RTYPE(tree),stderr); fprintf(stderr,"' ");
2526 fprintf(stderr,"assigned to type --> '");
2527 printTypeChain (LTYPE(tree),stderr); fprintf(stderr,"'\n");
2530 /* extra checks for pointer types */
2531 if (IS_PTR(LTYPE(tree)) && IS_PTR(RTYPE(tree)) &&
2532 !IS_GENPTR(LTYPE(tree))) {
2533 if (DCL_TYPE(LTYPE(tree)) != DCL_TYPE(RTYPE(tree)))
2534 werror(W_PTR_ASSIGN);
2537 TETYPE(tree) = getSpec(TTYPE(tree) =
2541 if (!tree->initMode && IS_CONSTANT(LETYPE(tree)))
2542 werror(E_CODE_WRITE," ");
2545 werror(E_LVALUE_REQUIRED,"=");
2546 goto errorTreeReturn ;
2551 /*------------------------------------------------------------------*/
2552 /*----------------------------*/
2553 /* comma operator */
2554 /*----------------------------*/
2556 TETYPE(tree) = getSpec(TTYPE(tree) = RTYPE(tree));
2559 /*------------------------------------------------------------------*/
2560 /*----------------------------*/
2562 /*----------------------------*/
2567 if (processParms (tree->left,
2569 tree->right,&parmNumber))
2570 goto errorTreeReturn ;
2572 if (options.stackAuto || IS_RENT(LETYPE(tree))) {
2573 tree->left->args = reverseVal(tree->left->args);
2574 reverseParms(tree->right);
2577 tree->args = tree->left->args ;
2578 TETYPE(tree) = getSpec (TTYPE(tree) = LTYPE(tree)->next);
2581 /*------------------------------------------------------------------*/
2582 /*----------------------------*/
2583 /* return statement */
2584 /*----------------------------*/
2589 if (checkType(currFunc->type->next,RTYPE(tree)) == 0) {
2590 werror(E_RETURN_MISMATCH);
2591 goto errorTreeReturn ;
2594 if (IS_VOID(currFunc->type->next)
2596 !IS_VOID(RTYPE(tree))) {
2597 werror(E_FUNC_VOID);
2598 goto errorTreeReturn ;
2601 /* if there is going to be a casing required then add it */
2602 if (checkType(currFunc->type->next,RTYPE(tree)) < 0 ) {
2604 decorateType(newNode(CAST,
2606 copyLinkChain(currFunc->type->next)),
2615 if (!IS_VOID(currFunc->type->next) && tree->right == NULL ) {
2616 werror(E_VOID_FUNC,currFunc->name);
2617 goto errorTreeReturn ;
2620 TTYPE(tree) = TETYPE(tree) = NULL ;
2623 /*------------------------------------------------------------------*/
2624 /*----------------------------*/
2625 /* switch statement */
2626 /*----------------------------*/
2628 /* the switch value must be an integer */
2629 if (!IS_INTEGRAL(LTYPE(tree))) {
2630 werror (E_SWITCH_NON_INTEGER);
2631 goto errorTreeReturn ;
2634 TTYPE(tree) = TETYPE(tree) = NULL ;
2637 /*------------------------------------------------------------------*/
2638 /*----------------------------*/
2640 /*----------------------------*/
2642 tree->left = backPatchLabels(tree->left,
2645 TTYPE(tree) = TETYPE(tree) = NULL;
2648 /*------------------------------------------------------------------*/
2649 /*----------------------------*/
2651 /*----------------------------*/
2654 decorateType(resolveSymbols(AST_FOR(tree,initExpr)));
2655 decorateType(resolveSymbols(AST_FOR(tree,condExpr)));
2656 decorateType(resolveSymbols(AST_FOR(tree,loopExpr)));
2658 /* if the for loop is reversible then
2659 reverse it otherwise do what we normally
2665 if (isLoopReversible (tree,&sym,&init,&end))
2666 return reverseLoop (tree,sym,init,end);
2668 return decorateType(createFor ( AST_FOR(tree,trueLabel),
2669 AST_FOR(tree,continueLabel) ,
2670 AST_FOR(tree,falseLabel) ,
2671 AST_FOR(tree,condLabel) ,
2672 AST_FOR(tree,initExpr) ,
2673 AST_FOR(tree,condExpr) ,
2674 AST_FOR(tree,loopExpr),
2678 TTYPE(tree) = TETYPE(tree) = NULL ;
2682 /* some error found this tree will be killed */
2684 TTYPE(tree) = TETYPE(tree) = newCharLink();
2685 tree->opval.op = NULLOP ;
2691 /*-----------------------------------------------------------------*/
2692 /* sizeofOp - processes size of operation */
2693 /*-----------------------------------------------------------------*/
2694 value *sizeofOp( link *type)
2698 /* get the size and convert it to character */
2699 sprintf (buff,"%d", getSize(type));
2701 /* now convert into value */
2702 return constVal (buff);
2706 #define IS_AND(ex) (ex->type == EX_OP && ex->opval.op == AND_OP )
2707 #define IS_OR(ex) (ex->type == EX_OP && ex->opval.op == OR_OP )
2708 #define IS_NOT(ex) (ex->type == EX_OP && ex->opval.op == '!' )
2709 #define IS_ANDORNOT(ex) (IS_AND(ex) || IS_OR(ex) || IS_NOT(ex))
2710 #define IS_IFX(ex) (ex->type == EX_OP && ex->opval.op == IFX )
2711 #define IS_LT(ex) (ex->type == EX_OP && ex->opval.op == '<' )
2712 #define IS_GT(ex) (ex->type == EX_OP && ex->opval.op == '>')
2714 /*-----------------------------------------------------------------*/
2715 /* backPatchLabels - change and or not operators to flow control */
2716 /*-----------------------------------------------------------------*/
2717 ast *backPatchLabels (ast *tree, symbol *trueLabel, symbol *falseLabel )
2723 if ( ! (IS_ANDORNOT(tree)))
2726 /* if this an and */
2728 static int localLbl = 0 ;
2729 symbol *localLabel ;
2731 sprintf (buffer,"_and_%d",localLbl++);
2732 localLabel = newSymbol(buffer,NestLevel);
2734 tree->left = backPatchLabels (tree->left, localLabel,falseLabel);
2736 /* if left is already a IFX then just change the if true label in that */
2737 if (!IS_IFX(tree->left))
2738 tree->left = newIfxNode(tree->left,localLabel,falseLabel);
2740 tree->right = backPatchLabels(tree->right,trueLabel,falseLabel);
2741 /* right is a IFX then just join */
2742 if (IS_IFX(tree->right))
2743 return newNode(NULLOP,tree->left,createLabel(localLabel,tree->right));
2745 tree->right = createLabel(localLabel,tree->right);
2746 tree->right = newIfxNode(tree->right,trueLabel,falseLabel);
2748 return newNode(NULLOP,tree->left,tree->right);
2751 /* if this is an or operation */
2753 static int localLbl = 0 ;
2754 symbol *localLabel ;
2756 sprintf (buffer,"_or_%d",localLbl++);
2757 localLabel = newSymbol(buffer,NestLevel);
2759 tree->left = backPatchLabels (tree->left, trueLabel,localLabel);
2761 /* if left is already a IFX then just change the if true label in that */
2762 if (!IS_IFX(tree->left))
2763 tree->left = newIfxNode(tree->left,trueLabel,localLabel);
2765 tree->right = backPatchLabels(tree->right,trueLabel,falseLabel);
2766 /* right is a IFX then just join */
2767 if (IS_IFX(tree->right))
2768 return newNode(NULLOP,tree->left,createLabel(localLabel,tree->right));
2770 tree->right = createLabel(localLabel,tree->right);
2771 tree->right = newIfxNode(tree->right,trueLabel,falseLabel);
2773 return newNode(NULLOP,tree->left,tree->right);
2778 tree->left = backPatchLabels (tree->left,falseLabel,trueLabel);
2780 /* if the left is already a IFX */
2781 if ( ! IS_IFX(tree->left) )
2782 tree->left = newNode (IFX,tree->left,NULL);
2784 tree->left->trueLabel = falseLabel ;
2785 tree->left->falseLabel= trueLabel ;
2790 tree->trueLabel = trueLabel ;
2791 tree->falseLabel= falseLabel;
2798 /*-----------------------------------------------------------------*/
2799 /* createBlock - create expression tree for block */
2800 /*-----------------------------------------------------------------*/
2801 ast *createBlock ( symbol *decl, ast *body )
2805 /* if the block has nothing */
2809 ex = newNode(BLOCK,NULL,body);
2810 ex->values.sym = decl ;
2812 ex->right = ex->right ;
2818 /*-----------------------------------------------------------------*/
2819 /* createLabel - creates the expression tree for labels */
2820 /*-----------------------------------------------------------------*/
2821 ast *createLabel ( symbol *label, ast *stmnt )
2824 char name[SDCC_NAME_MAX+1];
2827 /* must create fresh symbol if the symbol name */
2828 /* exists in the symbol table, since there can */
2829 /* be a variable with the same name as the labl */
2830 if ((csym = findSym (SymbolTab,NULL,label->name)) &&
2831 (csym->level == label->level))
2832 label = newSymbol(label->name,label->level);
2834 /* change the name before putting it in add _*/
2835 sprintf (name,"%s",label->name);
2837 /* put the label in the LabelSymbol table */
2838 /* but first check if a label of the same */
2840 if ( (csym = findSym(LabelTab,NULL,name)))
2841 werror(E_DUPLICATE_LABEL,label->name);
2843 addSym (LabelTab, label, name,label->level,0);
2846 label->key = labelKey++ ;
2847 rValue = newNode (LABEL,newAst(EX_VALUE,symbolVal(label)),stmnt);
2853 /*-----------------------------------------------------------------*/
2854 /* createCase - generates the parsetree for a case statement */
2855 /*-----------------------------------------------------------------*/
2856 ast *createCase (ast *swStat, ast *caseVal, ast *stmnt )
2858 char caseLbl[SDCC_NAME_MAX+1];
2862 /* if the switch statement does not exist */
2863 /* then case is out of context */
2865 werror(E_CASE_CONTEXT);
2869 caseVal = decorateType(resolveSymbols(caseVal));
2870 /* if not a constant then error */
2871 if (!IS_LITERAL(caseVal->ftype)) {
2872 werror(E_CASE_CONSTANT);
2876 /* if not a integer than error */
2877 if (!IS_INTEGRAL(caseVal->ftype)) {
2878 werror(E_CASE_NON_INTEGER);
2882 /* find the end of the switch values chain */
2883 if (!(val = swStat->values.switchVals.swVals))
2884 swStat->values.switchVals.swVals = caseVal->opval.val ;
2886 /* also order the cases according to value */
2888 int cVal = (int) floatFromVal(caseVal->opval.val);
2889 while (val && (int) floatFromVal(val) < cVal) {
2894 /* if we reached the end then */
2896 pval->next = caseVal->opval.val;
2898 /* we found a value greater than */
2899 /* the current value we must add this */
2900 /* before the value */
2901 caseVal->opval.val->next = val;
2903 /* if this was the first in chain */
2904 if (swStat->values.switchVals.swVals == val)
2905 swStat->values.switchVals.swVals =
2908 pval->next = caseVal->opval.val;
2913 /* create the case label */
2914 sprintf(caseLbl,"_case_%d_%d",
2915 swStat->values.switchVals.swNum,
2916 (int) floatFromVal(caseVal->opval.val));
2918 rexpr = createLabel(newSymbol(caseLbl,0),stmnt);
2923 /*-----------------------------------------------------------------*/
2924 /* createDefault - creates the parse tree for the default statement*/
2925 /*-----------------------------------------------------------------*/
2926 ast *createDefault (ast *swStat, ast *stmnt)
2928 char defLbl[SDCC_NAME_MAX+1];
2930 /* if the switch statement does not exist */
2931 /* then case is out of context */
2933 werror(E_CASE_CONTEXT);
2937 /* turn on the default flag */
2938 swStat->values.switchVals.swDefault = 1 ;
2940 /* create the label */
2941 sprintf (defLbl,"_default_%d",swStat->values.switchVals.swNum);
2942 return createLabel(newSymbol(defLbl,0),stmnt);
2945 /*-----------------------------------------------------------------*/
2946 /* createIf - creates the parsetree for the if statement */
2947 /*-----------------------------------------------------------------*/
2948 ast *createIf ( ast *condAst, ast *ifBody, ast *elseBody )
2950 static int Lblnum = 0 ;
2952 symbol *ifTrue , *ifFalse, *ifEnd ;
2954 /* if neither exists */
2955 if (! elseBody && !ifBody)
2958 /* create the labels */
2959 sprintf (buffer,"_iffalse_%d",Lblnum);
2960 ifFalse = newSymbol (buffer,NestLevel);
2961 /* if no else body then end == false */
2965 sprintf (buffer,"_ifend_%d",Lblnum);
2966 ifEnd = newSymbol (buffer,NestLevel);
2969 sprintf (buffer,"_iftrue_%d",Lblnum);
2970 ifTrue = newSymbol (buffer,NestLevel);
2974 /* attach the ifTrue label to the top of it body */
2975 ifBody = createLabel(ifTrue,ifBody);
2976 /* attach a goto end to the ifBody if else is present */
2978 ifBody = newNode(NULLOP,ifBody,
2980 newAst(EX_VALUE,symbolVal(ifEnd)),
2982 /* put the elseLabel on the else body */
2983 elseBody = createLabel (ifFalse,elseBody);
2984 /* out the end at the end of the body */
2985 elseBody = newNode(NULLOP,
2987 createLabel(ifEnd,NULL));
2990 ifBody = newNode(NULLOP,ifBody,
2991 createLabel(ifFalse,NULL));
2993 condAst = backPatchLabels (condAst,ifTrue,ifFalse);
2994 if (IS_IFX(condAst))
2997 ifTree = newIfxNode(condAst,ifTrue,ifFalse);
2999 return newNode(NULLOP,ifTree,
3000 newNode(NULLOP,ifBody,elseBody));
3004 /*-----------------------------------------------------------------*/
3005 /* createDo - creates parse tree for do */
3008 /* _docontinue_n: */
3009 /* condition_expression +-> trueLabel -> _dobody_n */
3011 /* +-> falseLabel-> _dobreak_n */
3013 /*-----------------------------------------------------------------*/
3014 ast *createDo ( symbol *trueLabel, symbol *continueLabel,
3015 symbol *falseLabel, ast *condAst, ast *doBody )
3020 /* if the body does not exist then it is simple */
3022 condAst = backPatchLabels(condAst,continueLabel,NULL);
3023 doTree = (IS_IFX(condAst) ? createLabel(continueLabel,condAst)
3024 : newNode(IFX,createLabel(continueLabel,condAst),NULL));
3025 doTree->trueLabel = continueLabel ;
3026 doTree->falseLabel= NULL ;
3030 /* otherwise we have a body */
3031 condAst = backPatchLabels(condAst,trueLabel,falseLabel);
3033 /* attach the body label to the top */
3034 doBody = createLabel(trueLabel,doBody);
3035 /* attach the continue label to end of body */
3036 doBody = newNode(NULLOP, doBody,
3037 createLabel(continueLabel,NULL));
3039 /* now put the break label at the end */
3040 if (IS_IFX(condAst))
3043 doTree = newIfxNode(condAst,trueLabel,falseLabel);
3045 doTree = newNode(NULLOP,doTree,createLabel(falseLabel,NULL));
3047 /* putting it together */
3048 return newNode(NULLOP,doBody,doTree);
3051 /*-----------------------------------------------------------------*/
3052 /* createFor - creates parse tree for 'for' statement */
3055 /* condExpr +-> trueLabel -> _forbody_n */
3057 /* +-> falseLabel-> _forbreak_n */
3060 /* _forcontinue_n: */
3062 /* goto _forcond_n ; */
3064 /*-----------------------------------------------------------------*/
3065 ast *createFor ( symbol *trueLabel, symbol *continueLabel ,
3066 symbol *falseLabel,symbol *condLabel ,
3067 ast *initExpr, ast *condExpr, ast *loopExpr,
3072 /* if loopexpression not present then we can generate it */
3073 /* the same way as a while */
3075 return newNode(NULLOP,initExpr,
3076 createWhile (trueLabel, continueLabel,
3077 falseLabel,condExpr, forBody ));
3078 /* vanilla for statement */
3079 condExpr = backPatchLabels(condExpr,trueLabel,falseLabel);
3081 if (condExpr && !IS_IFX(condExpr))
3082 condExpr = newIfxNode(condExpr,trueLabel,falseLabel);
3085 /* attach condition label to condition */
3086 condExpr = createLabel(condLabel,condExpr);
3088 /* attach body label to body */
3089 forBody = createLabel(trueLabel,forBody);
3091 /* attach continue to forLoop expression & attach */
3092 /* goto the forcond @ and of loopExpression */
3093 loopExpr = createLabel(continueLabel,
3097 newAst(EX_VALUE,symbolVal(condLabel)),
3099 /* now start putting them together */
3100 forTree = newNode(NULLOP,initExpr,condExpr);
3101 forTree = newNode(NULLOP,forTree,forBody);
3102 forTree = newNode(NULLOP,forTree,loopExpr);
3103 /* finally add the break label */
3104 forTree = newNode(NULLOP,forTree,
3105 createLabel(falseLabel,NULL));
3109 /*-----------------------------------------------------------------*/
3110 /* createWhile - creates parse tree for while statement */
3111 /* the while statement will be created as follows */
3113 /* _while_continue_n: */
3114 /* condition_expression +-> trueLabel -> _while_boby_n */
3116 /* +-> falseLabel -> _while_break_n*/
3117 /* _while_body_n: */
3119 /* goto _while_continue_n */
3120 /* _while_break_n: */
3121 /*-----------------------------------------------------------------*/
3122 ast *createWhile (symbol *trueLabel, symbol *continueLabel,
3123 symbol *falseLabel,ast *condExpr, ast *whileBody )
3127 /* put the continue label */
3128 condExpr = backPatchLabels (condExpr,trueLabel,falseLabel);
3129 condExpr = createLabel(continueLabel,condExpr);
3130 condExpr->lineno = 0;
3132 /* put the body label in front of the body */
3133 whileBody = createLabel(trueLabel,whileBody);
3134 whileBody->lineno = 0;
3135 /* put a jump to continue at the end of the body */
3136 /* and put break label at the end of the body */
3137 whileBody = newNode(NULLOP,
3141 symbolVal(continueLabel)),
3142 createLabel(falseLabel,NULL)));
3144 /* put it all together */
3145 if ( IS_IFX(condExpr) )
3146 whileTree = condExpr ;
3148 whileTree = newNode (IFX, condExpr,NULL );
3149 /* put the true & false labels in place */
3150 whileTree->trueLabel = trueLabel ;
3151 whileTree->falseLabel= falseLabel;
3154 return newNode(NULLOP,whileTree,whileBody );
3157 /*-----------------------------------------------------------------*/
3158 /* optimizeGetHbit - get highest order bit of the expression */
3159 /*-----------------------------------------------------------------*/
3160 ast *optimizeGetHbit (ast *tree)
3163 /* if this is not a bit and */
3164 if (!IS_BITAND(tree))
3167 /* will look for tree of the form
3168 ( expr >> ((sizeof expr) -1) ) & 1 */
3169 if (!IS_AST_LIT_VALUE(tree->right))
3172 if (AST_LIT_VALUE(tree->right) != 1)
3175 if (!IS_RIGHT_OP(tree->left))
3178 if (!IS_AST_LIT_VALUE(tree->left->right))
3181 if ((i = AST_LIT_VALUE(tree->left->right)) !=
3182 ( j = (getSize(TTYPE(tree->left->left))*8 - 1)))
3185 return decorateType(newNode(GETHBIT,tree->left->left,NULL));
3189 /*-----------------------------------------------------------------*/
3190 /* optimizeRRCRLC :- optimize for Rotate Left/Right with carry */
3191 /*-----------------------------------------------------------------*/
3192 ast *optimizeRRCRLC ( ast *root )
3194 /* will look for trees of the form
3195 (?expr << 1) | (?expr >> 7) or
3196 (?expr >> 7) | (?expr << 1) will make that
3197 into a RLC : operation ..
3199 (?expr >> 1) | (?expr << 7) or
3200 (?expr << 7) | (?expr >> 1) will make that
3201 into a RRC operation
3202 note : by 7 I mean (number of bits required to hold the
3204 /* if the root operations is not a | operation the not */
3205 if (!IS_BITOR(root))
3208 /* I have to think of a better way to match patterns this sucks */
3209 /* that aside let start looking for the first case : I use a the
3210 negative check a lot to improve the efficiency */
3211 /* (?expr << 1) | (?expr >> 7) */
3212 if (IS_LEFT_OP(root->left) &&
3213 IS_RIGHT_OP(root->right) ) {
3215 if (!SPEC_USIGN(TETYPE(root->left->left)))
3218 if (!IS_AST_LIT_VALUE(root->left->right) ||
3219 !IS_AST_LIT_VALUE(root->right->right))
3222 /* make sure it is the same expression */
3223 if (!isAstEqual(root->left->left,
3227 if (AST_LIT_VALUE(root->left->right) != 1 )
3230 if (AST_LIT_VALUE(root->right->right) !=
3231 (getSize(TTYPE(root->left->left))*8 - 1))
3234 /* whew got the first case : create the AST */
3235 return newNode(RLC,root->left->left,NULL);
3239 /* check for second case */
3240 /* (?expr >> 7) | (?expr << 1) */
3241 if (IS_LEFT_OP(root->right) &&
3242 IS_RIGHT_OP(root->left) ) {
3244 if (!SPEC_USIGN(TETYPE(root->left->left)))
3247 if (!IS_AST_LIT_VALUE(root->left->right) ||
3248 !IS_AST_LIT_VALUE(root->right->right))
3251 /* make sure it is the same symbol */
3252 if (!isAstEqual(root->left->left,
3256 if (AST_LIT_VALUE(root->right->right) != 1 )
3259 if (AST_LIT_VALUE(root->left->right) !=
3260 (getSize(TTYPE(root->left->left))*8 - 1))
3263 /* whew got the first case : create the AST */
3264 return newNode(RLC,root->left->left,NULL);
3269 /* third case for RRC */
3270 /* (?symbol >> 1) | (?symbol << 7) */
3271 if (IS_LEFT_OP(root->right) &&
3272 IS_RIGHT_OP(root->left) ) {
3274 if (!SPEC_USIGN(TETYPE(root->left->left)))
3277 if (!IS_AST_LIT_VALUE(root->left->right) ||
3278 !IS_AST_LIT_VALUE(root->right->right))
3281 /* make sure it is the same symbol */
3282 if (!isAstEqual(root->left->left,
3286 if (AST_LIT_VALUE(root->left->right) != 1 )
3289 if (AST_LIT_VALUE(root->right->right) !=
3290 (getSize(TTYPE(root->left->left))*8 - 1))
3293 /* whew got the first case : create the AST */
3294 return newNode(RRC,root->left->left,NULL);
3298 /* fourth and last case for now */
3299 /* (?symbol << 7) | (?symbol >> 1) */
3300 if (IS_RIGHT_OP(root->right) &&
3301 IS_LEFT_OP(root->left) ) {
3303 if (!SPEC_USIGN(TETYPE(root->left->left)))
3306 if (!IS_AST_LIT_VALUE(root->left->right) ||
3307 !IS_AST_LIT_VALUE(root->right->right))
3310 /* make sure it is the same symbol */
3311 if (!isAstEqual(root->left->left,
3315 if (AST_LIT_VALUE(root->right->right) != 1 )
3318 if (AST_LIT_VALUE(root->left->right) !=
3319 (getSize(TTYPE(root->left->left))*8 - 1))
3322 /* whew got the first case : create the AST */
3323 return newNode(RRC,root->left->left,NULL);
3327 /* not found return root */
3331 /*-----------------------------------------------------------------*/
3332 /* optimizeCompare - otimizes compares for bit variables */
3333 /*-----------------------------------------------------------------*/
3334 ast *optimizeCompare ( ast *root )
3336 ast *optExpr = NULL;
3339 unsigned int litValue ;
3341 /* if nothing then return nothing */
3345 /* if not a compare op then do leaves */
3346 if (!IS_COMPARE_OP(root)) {
3347 root->left = optimizeCompare (root->left);
3348 root->right= optimizeCompare (root->right);
3352 /* if left & right are the same then depending
3353 of the operation do */
3354 if (isAstEqual(root->left,root->right)) {
3355 switch (root->opval.op) {
3359 optExpr = newAst(EX_VALUE,constVal("0"));
3364 optExpr = newAst(EX_VALUE,constVal("1"));
3368 return decorateType(optExpr);
3371 vleft = (root->left->type == EX_VALUE ?
3372 root->left->opval.val : NULL );
3374 vright = (root->right->type == EX_VALUE ?
3375 root->right->opval.val : NULL);
3377 /* if left is a BITVAR in BITSPACE */
3378 /* and right is a LITERAL then opt-*/
3379 /* imize else do nothing */
3380 if (vleft && vright &&
3381 IS_BITVAR(vleft->etype) &&
3382 IN_BITSPACE(SPEC_OCLS(vleft->etype)) &&
3383 IS_LITERAL(vright->etype)) {
3385 /* if right side > 1 then comparison may never succeed */
3386 if ( (litValue = (int) floatFromVal(vright)) > 1 ) {
3387 werror(W_BAD_COMPARE);
3392 switch (root->opval.op) {
3393 case '>' : /* bit value greater than 1 cannot be */
3394 werror(W_BAD_COMPARE);
3398 case '<' : /* bit value < 1 means 0 */
3400 optExpr = newNode('!',newAst(EX_VALUE,vleft),NULL);
3403 case LE_OP : /* bit value <= 1 means no check */
3404 optExpr = newAst(EX_VALUE,vright);
3407 case GE_OP : /* bit value >= 1 means only check for = */
3409 optExpr = newAst(EX_VALUE,vleft);
3412 } else { /* literal is zero */
3413 switch (root->opval.op) {
3414 case '<' : /* bit value < 0 cannot be */
3415 werror(W_BAD_COMPARE);
3419 case '>' : /* bit value > 0 means 1 */
3421 optExpr = newAst(EX_VALUE,vleft);
3424 case LE_OP : /* bit value <= 0 means no check */
3425 case GE_OP : /* bit value >= 0 means no check */
3426 werror(W_BAD_COMPARE);
3430 case EQ_OP : /* bit == 0 means ! of bit */
3431 optExpr = newNode('!',newAst(EX_VALUE,vleft),NULL);
3435 return decorateType(resolveSymbols(optExpr));
3436 } /* end-of-if of BITVAR */
3441 /*-----------------------------------------------------------------*/
3442 /* addSymToBlock : adds the symbol to the first block we find */
3443 /*-----------------------------------------------------------------*/
3444 void addSymToBlock (symbol *sym, ast *tree)
3446 /* reached end of tree or a leaf */
3447 if (!tree || IS_AST_LINK(tree) || IS_AST_VALUE(tree))
3451 if (IS_AST_OP(tree) &&
3452 tree->opval.op == BLOCK ) {
3454 symbol *lsym = copySymbol(sym);
3456 lsym->next = AST_VALUES(tree,sym);
3457 AST_VALUES(tree,sym) = lsym ;
3461 addSymToBlock(sym,tree->left);
3462 addSymToBlock(sym,tree->right);
3465 /*-----------------------------------------------------------------*/
3466 /* processRegParms - do processing for register parameters */
3467 /*-----------------------------------------------------------------*/
3468 static void processRegParms (value *args, ast *body)
3471 if (IS_REGPARM(args->etype))
3472 addSymToBlock(args->sym,body);
3477 /*-----------------------------------------------------------------*/
3478 /* resetParmKey - resets the operandkeys for the symbols */
3479 /*-----------------------------------------------------------------*/
3480 DEFSETFUNC(resetParmKey)
3491 /*-----------------------------------------------------------------*/
3492 /* createFunction - This is the key node that calls the iCode for */
3493 /* generating the code for a function. Note code */
3494 /* is generated function by function, later when */
3495 /* add inter-procedural analysis this will change */
3496 /*-----------------------------------------------------------------*/
3497 ast *createFunction (symbol *name, ast *body )
3503 iCode *piCode = NULL;
3505 /* if check function return 0 then some problem */
3506 if (checkFunction (name) == 0)
3509 /* create a dummy block if none exists */
3511 body = newNode(BLOCK,NULL,NULL);
3515 /* check if the function name already in the symbol table */
3516 if ((csym = findSym (SymbolTab,NULL,name->name))) {
3518 /* special case for compiler defined functions
3519 we need to add the name to the publics list : this
3520 actually means we are now compiling the compiler
3523 addSet(&publics,name);
3527 allocVariables(name);
3529 name->lastLine = yylineno;
3531 processFuncArgs(currFunc,0);
3533 /* set the stack pointer */
3534 /* PENDING: check this for the mcs51 */
3535 stackPtr = -port->stack.direction * port->stack.call_overhead;
3536 if (IS_ISR(name->etype))
3537 stackPtr -= port->stack.direction * port->stack.isr_overhead;
3538 if (IS_RENT(name->etype) || options.stackAuto)
3539 stackPtr -= port->stack.direction * port->stack.reent_overhead;
3541 xstackPtr = -port->stack.direction * port->stack.call_overhead;
3543 fetype = getSpec(name->type); /* get the specifier for the function */
3544 /* if this is a reentrant function then */
3545 if (IS_RENT(fetype))
3548 allocParms (name->args); /* allocate the parameters */
3550 /* do processing for parameters that are passed in registers */
3551 processRegParms (name->args,body);
3553 /* set the stack pointer */
3557 /* allocate & autoinit the block variables */
3558 processBlockVars (body, &stack,ALLOCATE);
3560 /* save the stack information */
3561 if (options.useXstack)
3562 name->xstack = SPEC_STAK(fetype) = stack;
3564 name->stack = SPEC_STAK(fetype) = stack;
3566 /* name needs to be mangled */
3567 sprintf (name->rname,"_%s",name->name);
3569 body = resolveSymbols(body); /* resolve the symbols */
3570 body = decorateType (body); /* propagateType & do semantic checks */
3572 ex = newAst (EX_VALUE, symbolVal(name)); /* create name */
3573 ex = newNode (FUNCTION,ex,body);
3574 ex->values.args = name->args ;
3577 werror(E_FUNC_NO_CODE,name->name);
3581 /* create the node & generate intermediate code */
3582 codeOutFile = code->oFile;
3583 piCode = iCodeFromAst(ex);
3586 werror(E_FUNC_NO_CODE,name->name);
3590 eBBlockFromiCode(piCode);
3592 /* if there are any statics then do them */
3594 codeOutFile = statsg->oFile;
3595 eBBlockFromiCode (iCodeFromAst (decorateType(resolveSymbols(staticAutos))));
3601 /* dealloc the block variables */
3602 processBlockVars(body, &stack,DEALLOCATE);
3603 /* deallocate paramaters */
3604 deallocParms(name->args);
3606 if (IS_RENT(fetype))
3609 /* we are done freeup memory & cleanup */
3614 addSet(&operKeyReset,name);
3615 applyToSet(operKeyReset,resetParmKey);
3618 cdbStructBlock(1,cdbFile);
3620 cleanUpLevel(LabelTab,0);
3621 cleanUpBlock(StructTab,1);
3622 cleanUpBlock(TypedefTab,1);
3624 xstack->syms = NULL;
3625 istack->syms = NULL;