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