cleanup; moved extern's from .c to .h files for double check
[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
33 #define  EX_OP       0
34 #define  EX_VALUE    1
35 #define  EX_LINK     2
36 #define  EX_STMNT    3
37 #define  EX_OPERAND  4
38
39 /* expression tree   */
40 typedef  struct   ast {
41
42     unsigned  type      :3       ;  
43     unsigned  decorated   :1     ;
44     unsigned  hasVargs    :1     ;
45     unsigned  isError     :1     ;
46     unsigned  funcName    :1     ;
47     unsigned  rvalue      :1     ;
48     unsigned  lvalue      :1     ;
49     unsigned  initMode    :1     ;
50     int       level       ;  /* level for expr */
51     int       block       ;  /* block number   */
52     /* union of values expression can have */
53     union {
54         value *val        ;  /* value if type = EX_VALUE */
55         link  *lnk        ;  /* 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  */
59     } opval ;
60     
61     /* union for special processing */
62     union {
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 */
67         struct {
68             value    *swVals     ;  /* switch comparison values */
69             int      swDefault   ;  /* default if present       */
70             int      swNum       ;  /* switch number            */
71         } switchVals   ;
72         /* if for then for values */
73         struct {
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   */
81         } forVals ;
82     } values ;
83     
84     int   lineno         ;  /* source file line number     */   
85     char  *filename      ;  /* filename of the source file */
86     
87     link  *ftype         ;  /* start of type chain for this subtree */
88     link  *etype         ;  /* end of type chain for this subtree   */
89     
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 */
96 } ast ;
97
98
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 == '&'      &&              \
121                                 x->right == NULL )
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
130
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 == '='  )
138
139 #define  LEFT_FIRST(x) ( x  ==  AND_OP  ||      x       ==      OR_OP   )
140
141 #define SIDE_EFFECTS_CHECK(op,rVal)  if (!sideEffects)  {               \
142                                          werror(W_NO_SIDE_EFFECTS,op);  \
143                                          return rVal    ;               \
144                                      }
145 #define IS_MODIFYING_OP(x) ( x == INC_OP || x == DEC_OP || x == '=' ||  \
146                         x == AND_ASSIGN || x== OR_ASSIGN || x == XOR_ASSIGN )
147
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 == '.')
152
153 /* forward declarations for global variables */
154 extern  ast     *staticAutos    ;
155 extern FILE *codeOutFile;
156
157 /* forward definitions for functions   */
158 ast     *newAst       (int  ,  void  *        );
159 void      initAst      (                       );
160 ast     *newNode       (int  ,ast *  ,ast *  );
161 ast     *copyAst      (ast *                 );
162 value    *sizeofOp      (link *                 );
163 value    *evalStmnt     (ast *                 );
164 ast     *createFunction(symbol  *,ast   *     );
165 ast     *createBlock   (symbol  *,ast   *     );
166 ast     *createLabel   (symbol  *,ast   *     );
167 ast     *createCase    (ast    *,ast   *,ast   *);
168 ast     *createDefault (ast    *,ast   *     );
169 ast      *optimizeCompare (ast   *                              );
170 ast      *forLoopOptForm( ast    *                              );
171 ast      *argAst                ( ast    *                              );
172 ast     *resolveSymbols (ast *) ;
173 ast     *decorateType   (ast *) ;
174 ast     *createWhile    (symbol *, symbol *, symbol *, ast *, ast *);
175 ast     *createIf       (ast *, ast *, ast *);
176 ast     *createDo       (symbol *,symbol *,symbol *,ast *,ast *);
177 ast     *createFor      (symbol *,symbol *,symbol *,symbol *,ast *,ast *,ast *, ast *);
178 void     eval2icode     (ast *);
179 value   *constExprValue (ast *,int);
180 symbol  *funcOfType     (char *,link *,link *,int,int);
181 ast     *initAggregates ( symbol *,initList *, ast *);
182 bool     hasSEFcalls    ( ast *);
183 void     addSymToBlock (symbol *, ast *) ;
184
185 // exported variables 
186 extern set *operKeyReset;
187 extern int noAlloc;
188
189 #endif