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