* src/SDCCast.h (struct ast),
[fw/sdcc] / src / SDCCast.h
1 /*-------------------------------------------------------------------------
2   SDCCast.h - header file for parser support & all ast related routines
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
5
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
9    later version.
10    
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.
15    
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.
19    
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 -------------------------------------------------------------------------*/
24
25 #ifndef SDCCEXPR_H
26 #define SDCCEXPR_H
27
28 #include "SDCCglobl.h"
29 #include "SDCCsymt.h"
30 #include "SDCCval.h"
31 #include "SDCCset.h"
32 #include "SDCCmem.h"
33
34 typedef enum {
35   EX_OP=0,
36   EX_VALUE,
37   EX_LINK,
38   EX_OPERAND
39 } ASTTYPE;
40
41 /* expression tree   */
42 typedef struct ast
43   {
44
45     ASTTYPE type;
46     unsigned decorated:1;
47     unsigned isError:1;
48     unsigned funcName:1;
49     unsigned rvalue:1;
50     unsigned lvalue:1;
51     unsigned initMode:1;
52     unsigned reversed:1;
53     int level;                  /* level for expr */
54     int block;                  /* block number   */
55     int seqPoint;               /* sequence point */
56     /* union of values expression can have */
57     union
58       {
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  */
63       }
64     opval;
65
66     /* union for special processing */
67     union
68       {
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 */
74         struct
75           {
76             value *swVals;      /* switch comparison values */
77             int swDefault;      /* default if present       */
78             int swNum;          /* switch number            */
79           }
80         switchVals;
81         /* if for then for values */
82         struct
83           {
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   */
91           }
92         forVals;
93         unsigned literalFromCast;       /* true if this is an EX_VALUE of LITERAL 
94                                          * type resulting from a typecast.
95                                          */
96         int argreg;                     /* argreg number when operand type == EX_OPERAND */
97       }
98     values;
99
100     int lineno;                 /* source file line number     */
101     char *filename;             /* filename of the source file */
102
103     sym_link *ftype;            /* start of type chain for this subtree */
104     sym_link *etype;            /* end of type chain for this subtree   */
105
106     struct ast *left;           /* pointer to left tree        */
107     struct ast *right;          /* pointer to right tree       */
108     symbol *trueLabel;          /* if statement trueLabel */
109     symbol *falseLabel;         /* if statement falseLabel */
110   }
111 ast;
112
113
114 /* easy access macros   */
115 #define  IS_AST_OP(x)                   ((x) && (x)->type == EX_OP)
116 #define IS_CALLOP(x)   (IS_AST_OP(x) && x->opval.op == CALL)
117 #define IS_BITOR(x) (IS_AST_OP(x) && x->opval.op == '|')
118 #define IS_BITAND(x) (IS_AST_OP(x) && x->opval.op == '&' && \
119                       x->left && x->right )
120 #define IS_FOR_STMT(x) (IS_AST_OP(x) && x->opval.op == FOR)
121 #define IS_LEFT_OP(x) (IS_AST_OP(x) && x->opval.op == LEFT_OP)
122 #define IS_RIGHT_OP(x) (IS_AST_OP(x) && x->opval.op == RIGHT_OP)
123 #define  IS_AST_VALUE(x)        ((x) && (x)->type == EX_VALUE && (x)->opval.val)
124 #define  IS_AST_LINK(x)         (x->type == EX_LINK)
125 #define  IS_AST_NOT_OPER(x)     (x && IS_AST_OP(x) && x->opval.op == '!')
126 #define  IS_ARRAY_OP(x) (IS_AST_OP(x) && x->opval.op == '[')
127 #define  IS_COMPARE_OP(x)       (IS_AST_OP(x)           &&              \
128                                   (x->opval.op == '>'   ||              \
129                                    x->opval.op == '<'   ||              \
130                                    x->opval.op == LE_OP ||              \
131                                    x->opval.op == GE_OP ||              \
132                                    x->opval.op == EQ_OP ||              \
133                                    x->opval.op == NE_OP ))
134 #define IS_CAST_OP(x) (IS_AST_OP(x) && (x)->opval.op == CAST)
135 #define IS_TERNARY_OP(x) (IS_AST_OP(x) && x->opval.op == '?')
136 #define IS_COLON_OP(x) (IS_AST_OP(x) && x->opval.op == ':')
137 #define IS_ADDRESS_OF_OP(x)    (IS_AST_OP(x)            &&              \
138                                 x->opval.op == '&'      &&              \
139                                 x->right == NULL )
140 #define IS_AST_LIT_VALUE(x) (IS_AST_VALUE(x) && \
141                              IS_LITERAL((x)->opval.val->etype))
142 #define IS_AST_SYM_VALUE(x)     (IS_AST_VALUE(x) && x->opval.val->sym)
143 #define AST_LIT_VALUE(x)        (floatFromVal(x->opval.val))
144 #define AST_SYMBOL(x)           (x->opval.val->sym)
145 #define AST_VALUE(x)            (x->opval.val)
146 #define AST_VALUES(x,y) (x->values.y)
147 #define AST_FOR(x,y) x->values.forVals.y
148 #define AST_ARGREG(x) x->values.argreg
149
150 #define  IS_AST_PARAM(x)        (IS_AST_OP(x) && x->opval.op == PARAM)
151
152 #define  CAN_EVAL(x)    (     x ==  '[' || x == '.' || x == PTR_OP ||   \
153               x ==  '&' || x == '|' || x == '^'    || x == '*' || \
154               x ==  '-' || x == '+' || x == '~'    ||   \
155               x ==  '!' || x == LEFT_OP || x == RIGHT_OP ||       \
156               x ==  '/' || x == '%' || x == '>' || x == '<'    || \
157               x == LE_OP || x == GE_OP || x == EQ_OP || x == NE_OP || \
158               x == AND_OP || x == OR_OP || x == '='  )
159
160 #define  LEFT_FIRST(x) ( x  ==  AND_OP  ||      x       ==      OR_OP   )
161
162 #define SIDE_EFFECTS_CHECK(op,rVal)  if (!sideEffects)  {               \
163                                          werror(W_NO_SIDE_EFFECTS,op);  \
164                                          return rVal    ;               \
165                                      }
166 #define IS_MODIFYING_OP(x) ( x == INC_OP || x == DEC_OP || x == '=' ||  \
167                         x == AND_ASSIGN || x== OR_ASSIGN || x == XOR_ASSIGN )
168
169 #define IS_ASSIGN_OP(x) ( x == '=' || x == ADD_ASSIGN || x == SUB_ASSIGN ||\
170                           x == MUL_ASSIGN || x == DIV_ASSIGN || x == XOR_ASSIGN ||\
171                           x == AND_ASSIGN || x == OR_ASSIGN || x == INC_OP || x == DEC_OP)
172 #define IS_DEREF_OP(x) (( x->opval.op == '*' && x->right == NULL) || x->opval.op == '.')
173
174 /* forward declarations for global variables */
175 extern ast *staticAutos;
176 extern FILE *codeOutFile;
177 extern struct memmap *GcurMemmap;
178
179 /* forward definitions for functions   */
180 ast *newAst_VALUE (value * val);
181 ast *newAst_OP (unsigned op);
182 ast *newAst_LINK (sym_link * val);
183
184 void initAst ();
185 ast *newNode (long, ast *, ast *);
186 ast *copyAst (ast *);
187 ast *removeIncDecOps (ast *);
188 ast *removePreIncDecOps (ast *);
189 ast *removePostIncDecOps (ast *);
190 value *sizeofOp (sym_link *);
191 value *evalStmnt (ast *);
192 ast *createFunction (symbol *, ast *);
193 ast *createBlock (symbol *, ast *);
194 ast *createLabel (symbol *, ast *);
195 ast *createCase (ast *, ast *, ast *);
196 ast *createDefault (ast *, ast *, ast *);
197 ast *forLoopOptForm (ast *);
198 ast *argAst (ast *);
199 ast *resolveSymbols (ast *);
200 ast *decorateType (ast *, RESULT_TYPE);
201 ast *createWhile (symbol *, symbol *, symbol *, ast *, ast *);
202 ast *createIf (ast *, ast *, ast *);
203 ast *createDo (symbol *, symbol *, symbol *, ast *, ast *);
204 ast *createFor (symbol *, symbol *, symbol *, symbol *, ast *, ast *, ast *, ast *);
205 void eval2icode (ast *);
206 value *constExprValue (ast *, int);
207 bool constExprTree (ast *);
208 int setAstLineno (ast *, int);
209 symbol *funcOfType (char *, sym_link *, sym_link *, int, int);
210 symbol * funcOfTypeVarg (char *, char * , int , char **);
211 ast *initAggregates (symbol *, initList *, ast *);
212 bool hasSEFcalls (ast *);
213 void addSymToBlock (symbol *, ast *);
214 void freeStringSymbol(symbol *);
215 DEFSETFUNC(resetParmKey);
216 int astErrors(ast *);
217 RESULT_TYPE getResultTypeFromType (sym_link *);
218
219 // exported variables 
220 extern set *operKeyReset;
221 extern int noAlloc;
222 extern int inInitMode;
223
224 #endif