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"
53 int level; /* level for expr */
54 int block; /* block number */
55 int seqPoint; /* sequence point */
56 /* union of values expression can have */
59 value *val; /* value if type = EX_VALUE */
60 sym_link *lnk; /* sym_link * if type= EX_LINK */
61 struct operand *oprnd; /* used only for side effecting function calls */
62 unsigned op; /* operator if type= EX_OP */
66 /* union for special processing */
69 char *inlineasm; /* pointer to inline assembler code */
70 literalList *constlist; /* init list for array initializer. */
71 symbol *sym; /* if block then -> symbols */
72 value *args; /* if function then args */
73 /* if switch then switch values */
76 value *swVals; /* switch comparison values */
77 int swDefault; /* default if present */
78 int swNum; /* switch number */
81 /* if for then for values */
84 struct ast *initExpr; /* init portion */
85 struct ast *condExpr; /* conditional portion */
86 struct ast *loopExpr; /* iteration portion */
87 symbol *trueLabel; /* entry point into body */
88 symbol *falseLabel; /* exit point */
89 symbol *continueLabel; /* conditional check */
90 symbol *condLabel; /* conditional label */
95 unsigned literalFromCast:1; /* true if this is an EX_VALUE of LITERAL
96 * type resulting from a typecast.
98 unsigned removedCast:1; /* true if the explicit cast has been removed */
100 int argreg; /* argreg number when operand type == EX_OPERAND */
104 int lineno; /* source file line number */
105 char *filename; /* filename of the source file */
107 sym_link *ftype; /* start of type chain for this subtree */
108 sym_link *etype; /* end of type chain for this subtree */
110 struct ast *left; /* pointer to left tree */
111 struct ast *right; /* pointer to right tree */
112 symbol *trueLabel; /* if statement trueLabel */
113 symbol *falseLabel; /* if statement falseLabel */
118 /* easy access macros */
119 #define IS_AST_OP(x) ((x) && (x)->type == EX_OP)
120 #define IS_CALLOP(x) (IS_AST_OP(x) && (x)->opval.op == CALL)
121 #define IS_BITOR(x) (IS_AST_OP(x) && (x)->opval.op == '|')
122 #define IS_BITAND(x) (IS_AST_OP(x) && (x)->opval.op == '&' && \
123 (x)->left && (x)->right )
124 #define IS_FOR_STMT(x) (IS_AST_OP(x) && (x)->opval.op == FOR)
125 #define IS_LEFT_OP(x) (IS_AST_OP(x) && (x)->opval.op == LEFT_OP)
126 #define IS_RIGHT_OP(x) (IS_AST_OP(x) && (x)->opval.op == RIGHT_OP)
127 #define IS_AST_VALUE(x) ((x) && (x)->type == EX_VALUE && (x)->opval.val)
128 #define IS_AST_LINK(x) ((x)->type == EX_LINK)
129 #define IS_AST_NOT_OPER(x) (x && IS_AST_OP(x) && (x)->opval.op == '!')
130 #define IS_ARRAY_OP(x) (IS_AST_OP(x) && (x)->opval.op == '[')
131 #define IS_COMPARE_OP(x) (IS_AST_OP(x) && \
132 ((x)->opval.op == '>' || \
133 (x)->opval.op == '<' || \
134 (x)->opval.op == LE_OP || \
135 (x)->opval.op == GE_OP || \
136 (x)->opval.op == EQ_OP || \
137 (x)->opval.op == NE_OP ))
138 #define IS_CAST_OP(x) (IS_AST_OP(x) && (x)->opval.op == CAST)
139 #define IS_TERNARY_OP(x) (IS_AST_OP(x) && (x)->opval.op == '?')
140 #define IS_COLON_OP(x) (IS_AST_OP(x) && (x)->opval.op == ':')
141 #define IS_ADDRESS_OF_OP(x) (IS_AST_OP(x) && \
142 (x)->opval.op == '&' && \
144 #define IS_AST_LIT_VALUE(x) (IS_AST_VALUE(x) && \
145 IS_LITERAL((x)->opval.val->etype))
146 #define IS_AST_SYM_VALUE(x) (IS_AST_VALUE(x) && (x)->opval.val->sym)
147 #define AST_FLOAT_VALUE(x) (floatFromVal((x)->opval.val))
148 #define AST_ULONG_VALUE(x) (ulFromVal((x)->opval.val))
149 #define AST_SYMBOL(x) ((x)->opval.val->sym)
150 #define AST_VALUE(x) ((x)->opval.val)
151 #define AST_VALUES(x,y) ((x)->values.y)
152 #define AST_FOR(x,y) ((x)->values.forVals.y)
153 #define AST_ARGREG(x) ((x)->values.argreg)
155 #define IS_AST_PARAM(x) (IS_AST_OP(x) && (x)->opval.op == PARAM)
157 #define CAN_EVAL(x) ( (x) == '[' || (x) == '.' || (x) == PTR_OP || \
158 (x) == '&' || (x) == '|' || (x) == '^' || (x) == '*' || \
159 (x) == '-' || (x) == '+' || (x) == '~' || \
160 (x) == '!' || (x) == LEFT_OP || (x) == RIGHT_OP || \
161 (x) == '/' || (x) == '%' || (x) == '>' || (x) == '<' || \
162 (x) == LE_OP || (x) == GE_OP || (x) == EQ_OP || (x) == NE_OP || \
163 (x) == AND_OP || (x) == OR_OP || (x) == '=' )
165 #define LEFT_FIRST(x) ( x == AND_OP || x == OR_OP )
167 #define SIDE_EFFECTS_CHECK(op,rVal) if (!sideEffects) { \
168 werror(W_NO_SIDE_EFFECTS,op); \
171 #define IS_MODIFYING_OP(x) ( (x) == INC_OP || (x) == DEC_OP || (x) == '=' || \
172 (x) == AND_ASSIGN || x== OR_ASSIGN || (x) == XOR_ASSIGN )
174 #define IS_ASSIGN_OP(x) ( (x) == '=' || (x) == ADD_ASSIGN || (x) == SUB_ASSIGN ||\
175 (x) == MUL_ASSIGN || (x) == DIV_ASSIGN || (x) == XOR_ASSIGN ||\
176 (x) == AND_ASSIGN || (x) == OR_ASSIGN || (x) == INC_OP || (x) == DEC_OP)
177 #define IS_DEREF_OP(x) (( (x)->opval.op == '*' && (x)->right == NULL) || (x)->opval.op == '.')
179 /* forward declarations for global variables */
180 extern ast *staticAutos;
181 extern struct dbuf_s *codeOutBuf;
182 extern struct memmap *GcurMemmap;
184 /* forward definitions for functions */
185 ast *newAst_VALUE (value * val);
186 ast *newAst_OP (unsigned op);
187 ast *newAst_LINK (sym_link * val);
190 ast *newNode (long, ast *, ast *);
191 ast *copyAst (ast *);
192 ast *removeIncDecOps (ast *);
193 ast *removePreIncDecOps (ast *);
194 ast *removePostIncDecOps (ast *);
195 value *sizeofOp (sym_link *);
196 value *evalStmnt (ast *);
197 ast *createRMW (ast *, unsigned, ast *);
198 ast *createFunction (symbol *, ast *);
199 ast *createBlock (symbol *, ast *);
200 ast *createLabel (symbol *, ast *);
201 ast *createCase (ast *, ast *, ast *);
202 ast *createDefault (ast *, ast *, ast *);
203 ast *forLoopOptForm (ast *);
205 ast *resolveSymbols (ast *);
206 ast *decorateType (ast *, RESULT_TYPE);
207 ast *createWhile (symbol *, symbol *, symbol *, ast *, ast *);
208 ast *createIf (ast *, ast *, ast *);
209 ast *createDo (symbol *, symbol *, symbol *, ast *, ast *);
210 ast *createFor (symbol *, symbol *, symbol *, symbol *, ast *, ast *, ast *, ast *);
211 void eval2icode (ast *);
212 value *constExprValue (ast *, int);
213 bool constExprTree (ast *);
214 int setAstFileLine (ast *, char *, int);
215 symbol *funcOfType (char *, sym_link *, sym_link *, int, int);
216 symbol * funcOfTypeVarg (char *, char * , int , char **);
217 ast *initAggregates (symbol *, initList *, ast *);
218 bool hasSEFcalls (ast *);
219 void addSymToBlock (symbol *, ast *);
220 void freeStringSymbol(symbol *);
221 DEFSETFUNC(resetParmKey);
222 int astErrors(ast *);
223 RESULT_TYPE getResultTypeFromType (sym_link *);
225 // exported variables
226 extern set *operKeyReset;
228 extern int inInitMode;