1 /*-------------------------------------------------------------------------
2 SDCCast.h - header 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 #include "SDCCglobl.h"
42 unsigned decorated :1 ;
43 unsigned hasVargs :1 ;
45 unsigned funcName :1 ;
48 unsigned initMode :1 ;
49 int level ; /* level for expr */
50 int block ; /* block number */
51 /* union of values expression can have */
53 value *val ; /* value if type = EX_VALUE */
54 link *lnk ; /* link * if type= EX_LINK */
55 struct operand *oprnd; /* used only for side effecting function calls */
56 unsigned stmnt ; /* statement if type=EX_STMNT */
57 unsigned op ; /* operator if type= EX_OP */
60 /* union for special processing */
62 char *inlineasm ; /* pointer to inline assembler code */
63 symbol *sym ; /* if block then -> symbols */
64 value *args ; /* if function then args */
65 /* if switch then switch values */
67 value *swVals ; /* switch comparison values */
68 int swDefault ; /* default if present */
69 int swNum ; /* switch number */
71 /* if for then for values */
73 struct ast *initExpr ; /* init portion */
74 struct ast *condExpr ; /* conditional portion */
75 struct ast *loopExpr ; /* iteration portion */
76 symbol *trueLabel; /* entry point into body */
77 symbol *falseLabel; /* exit point */
78 symbol *continueLabel; /* conditional check */
79 symbol *condLabel; /* conditional label */
83 int lineno ; /* source file line number */
84 char *filename ; /* filename of the source file */
86 link *ftype ; /* start of type chain for this subtree */
87 link *etype ; /* end of type chain for this subtree */
89 symbol *argSym ; /* argument symbols */
90 value *args ; /* args of a function */
91 struct ast *left ; /* pointer to left tree */
92 struct ast *right; /* pointer to right tree */
93 symbol *trueLabel ; /* if statement trueLabel */
94 symbol *falseLabel ; /* if statement falseLabel */
98 /* easy access macros */
99 #define IS_AST_OP(x) (x && x->type == EX_OP)
100 #define IS_BITOR(x) (IS_AST_OP(x) && x->opval.op == '|')
101 #define IS_BITAND(x) (IS_AST_OP(x) && x->opval.op == '&' && \
102 x->left && x->right )
103 #define IS_FOR_STMT(x) (IS_AST_OP(x) && x->opval.op == FOR)
104 #define IS_LEFT_OP(x) (IS_AST_OP(x) && x->opval.op == LEFT_OP)
105 #define IS_RIGHT_OP(x) (IS_AST_OP(x) && x->opval.op == RIGHT_OP)
106 #define IS_AST_VALUE(x) (x && x->type == EX_VALUE && x->opval.val)
107 #define IS_AST_LINK(x) (x->type == EX_LINK)
108 #define IS_AST_NOT_OPER(x) (x && IS_AST_OP(x) && x->opval.op == '!')
109 #define IS_COMPARE_OP(x) (IS_AST_OP(x) && \
110 (x->opval.op == '>' || \
111 x->opval.op == '<' || \
112 x->opval.op == LE_OP || \
113 x->opval.op == GE_OP || \
114 x->opval.op == EQ_OP || \
115 x->opval.op == NE_OP ))
116 #define IS_CAST_OP(x) (IS_AST_OP(x) && x->opval.op == CAST)
117 #define IS_ADDRESS_OF_OP(x) (IS_AST_OP(x) && \
118 x->opval.op == '&' && \
120 #define IS_AST_LIT_VALUE(x) (IS_AST_VALUE(x) && \
121 IS_LITERAL(x->opval.val->etype))
122 #define IS_AST_SYM_VALUE(x) (IS_AST_VALUE(x) && x->opval.val->sym)
123 #define AST_LIT_VALUE(x) (floatFromVal(x->opval.val))
124 #define AST_SYMBOL(x) (x->opval.val->sym)
125 #define AST_VALUE(x) (x->opval.val)
126 #define AST_VALUES(x,y) (x->values.y)
127 #define AST_FOR(x,y) x->values.forVals.y
129 #define CAN_EVAL(x) ( x == '[' || x == '.' || x == PTR_OP || \
130 x == '&' || x == '|' || x == '^' || x == '*' || \
131 x == '-' || x == '+' || x == '~' || \
132 x == '!' || x == LEFT_OP || x == RIGHT_OP || \
133 x == '/' || x == '%' || x == '>' || x == '<' || \
134 x == LE_OP || x == GE_OP || x == EQ_OP || x == NE_OP || \
135 x == AND_OP || x == OR_OP || x == '=' )
137 #define LEFT_FIRST(x) ( x == AND_OP || x == OR_OP )
139 #define SIDE_EFFECTS_CHECK(op,rVal) if (!sideEffects) { \
140 werror(W_NO_SIDE_EFFECTS,op); \
143 #define IS_MODIFYING_OP(x) ( x == INC_OP || x == DEC_OP || x == '=' || \
144 x == AND_ASSIGN || x== OR_ASSIGN || x == XOR_ASSIGN )
146 #define IS_ASSIGN_OP(x) ( x == '=' || x == ADD_ASSIGN || x == SUB_ASSIGN ||\
147 x == MUL_ASSIGN || x == DIV_ASSIGN || x == XOR_ASSIGN ||\
148 x == AND_ASSIGN || x == OR_ASSIGN)
149 #define IS_DEREF_OP(x) (( x->opval.op == '*' && x->right == NULL) || x->opval.op == '.')
151 /* forward declrations for global variables */
152 extern ast *staticAutos ;
154 /* forward definitions for functions */
155 ast *newAst (int , void * );
157 ast *newNode (int ,ast * ,ast * );
158 ast *copyAst (ast * );
159 value *sizeofOp (link * );
160 value *evalStmnt (ast * );
161 ast *createFunction(symbol *,ast * );
162 ast *createBlock (symbol *,ast * );
163 ast *createLabel (symbol *,ast * );
164 ast *createCase (ast *,ast *,ast *);
165 ast *createDefault (ast *,ast * );
166 ast *optimizeCompare (ast * );
167 ast *forLoopOptForm( ast * );
168 ast *argAst ( ast * );
169 ast *resolveSymbols (ast *) ;
170 ast *decorateType (ast *) ;
171 ast *createWhile (symbol *, symbol *, symbol *, ast *, ast *);
172 ast *createIf (ast *, ast *, ast *);
173 ast *createDo (symbol *,symbol *,symbol *,ast *,ast *);
174 ast *createFor (symbol *,symbol *,symbol *,symbol *,ast *,ast *,ast *, ast *);
175 void eval2icode (ast *);
176 value *constExprValue (ast *,int);
177 symbol *funcOfType (char *,link *,link *,int,int);
178 ast *initAggregates ( symbol *,initList *, ast *);
179 bool hasSEFcalls ( ast *);
180 void addSymToBlock (symbol *, ast *) ;