Fixed pointer post increment problem
[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 #define  EX_OP       0
35 #define  EX_VALUE    1
36 #define  EX_LINK     2
37 #define  EX_STMNT    3
38 #define  EX_OPERAND  4
39
40 /* expression tree   */
41 typedef struct ast
42   {
43
44     unsigned type:3;
45     unsigned decorated:1;
46     unsigned hasVargs:1;
47     unsigned isError:1;
48     unsigned funcName:1;
49     unsigned rvalue:1;
50     unsigned lvalue:1;
51     unsigned initMode:1;
52     int level;                  /* level for expr */
53     int block;                  /* block number   */
54     /* union of values expression can have */
55     union
56       {
57         value *val;             /* value if type = EX_VALUE */
58         sym_link *lnk;          /* sym_link * if type= EX_LINK  */
59         struct operand *oprnd;  /* used only for side effecting function calls */
60         unsigned stmnt;         /* statement if type=EX_STMNT */
61         unsigned op;            /* operator if type= EX_OP  */
62       }
63     opval;
64
65     /* union for special processing */
66     union
67       {
68         char *inlineasm;        /* pointer to inline assembler code */
69         symbol *sym;            /* if block then -> symbols */
70         value *args;            /* if function then args    */
71         /* if switch then switch values */
72         struct
73           {
74             value *swVals;      /* switch comparison values */
75             int swDefault;      /* default if present       */
76             int swNum;          /* switch number            */
77           }
78         switchVals;
79         /* if for then for values */
80         struct
81           {
82             struct ast *initExpr;       /* init portion */
83             struct ast *condExpr;       /* conditional portion */
84             struct ast *loopExpr;       /* iteration portion   */
85             symbol *trueLabel;  /* entry point into body */
86             symbol *falseLabel; /* exit point */
87             symbol *continueLabel;      /* conditional check   */
88             symbol *condLabel;  /* conditional label   */
89           }
90         forVals;
91         unsigned literalFromCast;       /* true if this is an EX_VALUE of LITERAL 
92                                          * type resulting from a typecast.
93                                          */
94       }
95     values;
96
97     int lineno;                 /* source file line number     */
98     char *filename;             /* filename of the source file */
99
100     sym_link *ftype;            /* start of type chain for this subtree */
101     sym_link *etype;            /* end of type chain for this subtree   */
102
103     symbol *argSym;             /* argument symbols            */
104     value *args;                /* args of a function          */
105     struct ast *left;           /* pointer to left tree        */
106     struct ast *right;          /* pointer to right tree       */
107     symbol *trueLabel;          /* if statement trueLabel */
108     symbol *falseLabel;         /* if statement falseLabel */
109   }
110 ast;
111
112
113 /* easy access macros   */
114 #define  IS_AST_OP(x)                   (x && x->type == EX_OP)
115 #define IS_CALLOP(x)   (IS_AST_OP(x) && x->opval.op == CALL)
116 #define IS_BITOR(x) (IS_AST_OP(x) && x->opval.op == '|')
117 #define IS_BITAND(x) (IS_AST_OP(x) && x->opval.op == '&' && \
118                       x->left && x->right )
119 #define IS_FOR_STMT(x) (IS_AST_OP(x) && x->opval.op == FOR)
120 #define IS_LEFT_OP(x) (IS_AST_OP(x) && x->opval.op == LEFT_OP)
121 #define IS_RIGHT_OP(x) (IS_AST_OP(x) && x->opval.op == RIGHT_OP)
122 #define  IS_AST_VALUE(x)        (x && x->type == EX_VALUE && x->opval.val)
123 #define  IS_AST_LINK(x)         (x->type == EX_LINK)
124 #define  IS_AST_NOT_OPER(x)     (x && IS_AST_OP(x) && x->opval.op == '!')
125 #define  IS_ARRAY_OP(x) (IS_AST_OP(x) && x->opval.op == '[')
126 #define  IS_COMPARE_OP(x)       (IS_AST_OP(x)           &&              \
127                                   (x->opval.op == '>'   ||              \
128                                    x->opval.op == '<'   ||              \
129                                    x->opval.op == LE_OP ||              \
130                                    x->opval.op == GE_OP ||              \
131                                    x->opval.op == EQ_OP ||              \
132                                    x->opval.op == NE_OP ))
133 #define IS_CAST_OP(x) (IS_AST_OP(x) && x->opval.op == CAST)
134 #define IS_TERNARY_OP(x) (IS_AST_OP(x) && x->opval.op == '?')
135 #define IS_COLON_OP(x) (IS_AST_OP(x) && x->opval.op == ':')
136 #define IS_ADDRESS_OF_OP(x)    (IS_AST_OP(x)            &&              \
137                                 x->opval.op == '&'      &&              \
138                                 x->right == NULL )
139 #define IS_AST_LIT_VALUE(x) (IS_AST_VALUE(x) && \
140                              IS_LITERAL(x->opval.val->etype))
141 #define IS_AST_SYM_VALUE(x)     (IS_AST_VALUE(x) && x->opval.val->sym)
142 #define AST_LIT_VALUE(x)        (floatFromVal(x->opval.val))
143 #define AST_SYMBOL(x)           (x->opval.val->sym)
144 #define AST_VALUE(x)            (x->opval.val)
145 #define AST_VALUES(x,y) (x->values.y)
146 #define AST_FOR(x,y) x->values.forVals.y
147 #define  IS_AST_PARAM(x)        (IS_AST_OP(x) && x->opval.op == PARAM)
148
149 #define  CAN_EVAL(x)    (     x ==  '[' || x == '.' || x == PTR_OP ||   \
150               x ==  '&' || x == '|' || x == '^'    || x == '*' || \
151               x ==  '-' || x == '+' || x == '~'    ||   \
152               x ==  '!' || x == LEFT_OP || x == RIGHT_OP ||       \
153               x ==  '/' || x == '%' || x == '>' || x == '<'    || \
154               x == LE_OP || x == GE_OP || x == EQ_OP || x == NE_OP || \
155               x == AND_OP || x == OR_OP || x == '='  )
156
157 #define  LEFT_FIRST(x) ( x  ==  AND_OP  ||      x       ==      OR_OP   )
158
159 #define SIDE_EFFECTS_CHECK(op,rVal)  if (!sideEffects)  {               \
160                                          werror(W_NO_SIDE_EFFECTS,op);  \
161                                          return rVal    ;               \
162                                      }
163 #define IS_MODIFYING_OP(x) ( x == INC_OP || x == DEC_OP || x == '=' ||  \
164                         x == AND_ASSIGN || x== OR_ASSIGN || x == XOR_ASSIGN )
165
166 #define IS_ASSIGN_OP(x) ( x == '=' || x == ADD_ASSIGN || x == SUB_ASSIGN ||\
167                           x == MUL_ASSIGN || x == DIV_ASSIGN || x == XOR_ASSIGN ||\
168                           x == AND_ASSIGN || x == OR_ASSIGN || x == INC_OP || x == DEC_OP)
169 #define IS_DEREF_OP(x) (( x->opval.op == '*' && x->right == NULL) || x->opval.op == '.')
170
171 /* forward declarations for global variables */
172 extern ast *staticAutos;
173 extern FILE *codeOutFile;
174 extern struct memmap *GcurMemmap;
175
176 /* forward definitions for functions   */
177 ast *newAst_VALUE (value * val);
178 ast *newAst_OP (unsigned op);
179 ast *newAst_LINK (sym_link * val);
180 ast *newAst_STMNT (unsigned val);
181
182 void initAst ();
183 ast *newNode (long, ast *, ast *);
184 ast *copyAst (ast *);
185 value *sizeofOp (sym_link *);
186 value *evalStmnt (ast *);
187 ast *createFunction (symbol *, ast *);
188 ast *createBlock (symbol *, ast *);
189 ast *createLabel (symbol *, ast *);
190 ast *createCase (ast *, ast *, ast *);
191 ast *createDefault (ast *, ast *);
192 ast *optimizeCompare (ast *);
193 ast *forLoopOptForm (ast *);
194 ast *argAst (ast *);
195 ast *resolveSymbols (ast *);
196 ast *decorateType (ast *);
197 ast *createWhile (symbol *, symbol *, symbol *, ast *, ast *);
198 ast *createIf (ast *, ast *, ast *);
199 ast *createDo (symbol *, symbol *, symbol *, ast *, ast *);
200 ast *createFor (symbol *, symbol *, symbol *, symbol *, ast *, ast *, ast *, ast *);
201 void eval2icode (ast *);
202 value *constExprValue (ast *, int);
203 symbol *funcOfType (char *, sym_link *, sym_link *, int, int);
204 ast *initAggregates (symbol *, initList *, ast *);
205 bool hasSEFcalls (ast *);
206 void addSymToBlock (symbol *, ast *);
207
208 // exported variables 
209 extern set *operKeyReset;
210 extern int noAlloc;
211 extern int inInitMode;
212
213 #endif