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 -------------------------------------------------------------------------*/
28 #include "SDCCglobl.h"
51 int level; /* level for expr */
52 int block; /* block number */
53 /* union of values expression can have */
56 value *val; /* value if type = EX_VALUE */
57 sym_link *lnk; /* sym_link * if type= EX_LINK */
58 struct operand *oprnd; /* used only for side effecting function calls */
59 unsigned stmnt; /* statement if type=EX_STMNT */
60 unsigned op; /* operator if type= EX_OP */
64 /* union for special processing */
67 char *inlineasm; /* pointer to inline assembler code */
68 symbol *sym; /* if block then -> symbols */
69 value *args; /* if function then args */
70 /* if switch then switch values */
73 value *swVals; /* switch comparison values */
74 int swDefault; /* default if present */
75 int swNum; /* switch number */
78 /* if for then for values */
81 struct ast *initExpr; /* init portion */
82 struct ast *condExpr; /* conditional portion */
83 struct ast *loopExpr; /* iteration portion */
84 symbol *trueLabel; /* entry point into body */
85 symbol *falseLabel; /* exit point */
86 symbol *continueLabel; /* conditional check */
87 symbol *condLabel; /* conditional label */
90 unsigned literalFromCast; /* true if this is an EX_VALUE of LITERAL
91 * type resulting from a typecast.
96 int lineno; /* source file line number */
97 char *filename; /* filename of the source file */
99 sym_link *ftype; /* start of type chain for this subtree */
100 sym_link *etype; /* end of type chain for this subtree */
102 symbol *argSym; /* argument symbols */
103 value *args; /* args of a function */
104 struct ast *left; /* pointer to left tree */
105 struct ast *right; /* pointer to right tree */
106 symbol *trueLabel; /* if statement trueLabel */
107 symbol *falseLabel; /* if statement falseLabel */
112 /* easy access macros */
113 #define IS_AST_OP(x) (x && x->type == EX_OP)
114 #define IS_BITOR(x) (IS_AST_OP(x) && x->opval.op == '|')
115 #define IS_BITAND(x) (IS_AST_OP(x) && x->opval.op == '&' && \
116 x->left && x->right )
117 #define IS_FOR_STMT(x) (IS_AST_OP(x) && x->opval.op == FOR)
118 #define IS_LEFT_OP(x) (IS_AST_OP(x) && x->opval.op == LEFT_OP)
119 #define IS_RIGHT_OP(x) (IS_AST_OP(x) && x->opval.op == RIGHT_OP)
120 #define IS_AST_VALUE(x) (x && x->type == EX_VALUE && x->opval.val)
121 #define IS_AST_LINK(x) (x->type == EX_LINK)
122 #define IS_AST_NOT_OPER(x) (x && IS_AST_OP(x) && x->opval.op == '!')
123 #define IS_ARRAY_OP(x) (IS_AST_OP(x) && x->opval.op == '[')
124 #define IS_COMPARE_OP(x) (IS_AST_OP(x) && \
125 (x->opval.op == '>' || \
126 x->opval.op == '<' || \
127 x->opval.op == LE_OP || \
128 x->opval.op == GE_OP || \
129 x->opval.op == EQ_OP || \
130 x->opval.op == NE_OP ))
131 #define IS_CAST_OP(x) (IS_AST_OP(x) && x->opval.op == CAST)
132 #define IS_ADDRESS_OF_OP(x) (IS_AST_OP(x) && \
133 x->opval.op == '&' && \
135 #define IS_AST_LIT_VALUE(x) (IS_AST_VALUE(x) && \
136 IS_LITERAL(x->opval.val->etype))
137 #define IS_AST_SYM_VALUE(x) (IS_AST_VALUE(x) && x->opval.val->sym)
138 #define AST_LIT_VALUE(x) (floatFromVal(x->opval.val))
139 #define AST_SYMBOL(x) (x->opval.val->sym)
140 #define AST_VALUE(x) (x->opval.val)
141 #define AST_VALUES(x,y) (x->values.y)
142 #define AST_FOR(x,y) x->values.forVals.y
144 #define CAN_EVAL(x) ( x == '[' || x == '.' || x == PTR_OP || \
145 x == '&' || x == '|' || x == '^' || x == '*' || \
146 x == '-' || x == '+' || x == '~' || \
147 x == '!' || x == LEFT_OP || x == RIGHT_OP || \
148 x == '/' || x == '%' || x == '>' || x == '<' || \
149 x == LE_OP || x == GE_OP || x == EQ_OP || x == NE_OP || \
150 x == AND_OP || x == OR_OP || x == '=' )
152 #define LEFT_FIRST(x) ( x == AND_OP || x == OR_OP )
154 #define SIDE_EFFECTS_CHECK(op,rVal) if (!sideEffects) { \
155 werror(W_NO_SIDE_EFFECTS,op); \
158 #define IS_MODIFYING_OP(x) ( x == INC_OP || x == DEC_OP || x == '=' || \
159 x == AND_ASSIGN || x== OR_ASSIGN || x == XOR_ASSIGN )
161 #define IS_ASSIGN_OP(x) ( x == '=' || x == ADD_ASSIGN || x == SUB_ASSIGN ||\
162 x == MUL_ASSIGN || x == DIV_ASSIGN || x == XOR_ASSIGN ||\
163 x == AND_ASSIGN || x == OR_ASSIGN || x == INC_OP || x == DEC_OP)
164 #define IS_DEREF_OP(x) (( x->opval.op == '*' && x->right == NULL) || x->opval.op == '.')
166 /* forward declarations for global variables */
167 extern ast *staticAutos;
168 extern FILE *codeOutFile;
170 /* forward definitions for functions */
171 ast *newAst_VALUE (value * val);
172 ast *newAst_OP (unsigned op);
173 ast *newAst_LINK (sym_link * val);
174 ast *newAst_STMNT (unsigned val);
177 ast *newNode (long, ast *, ast *);
178 ast *copyAst (ast *);
179 value *sizeofOp (sym_link *);
180 value *evalStmnt (ast *);
181 ast *createFunction (symbol *, ast *);
182 ast *createBlock (symbol *, ast *);
183 ast *createLabel (symbol *, ast *);
184 ast *createCase (ast *, ast *, ast *);
185 ast *createDefault (ast *, ast *);
186 ast *optimizeCompare (ast *);
187 ast *forLoopOptForm (ast *);
189 ast *resolveSymbols (ast *);
190 ast *decorateType (ast *);
191 ast *createWhile (symbol *, symbol *, symbol *, ast *, ast *);
192 ast *createIf (ast *, ast *, ast *);
193 ast *createDo (symbol *, symbol *, symbol *, ast *, ast *);
194 ast *createFor (symbol *, symbol *, symbol *, symbol *, ast *, ast *, ast *, ast *);
195 void eval2icode (ast *);
196 value *constExprValue (ast *, int);
197 symbol *funcOfType (char *, sym_link *, sym_link *, int, int);
198 ast *initAggregates (symbol *, initList *, ast *);
199 bool hasSEFcalls (ast *);
200 void addSymToBlock (symbol *, ast *);
202 // exported variables
203 extern set *operKeyReset;