xa51 updates
[fw/sdcc] / src / SDCCicode.h
1 /*-------------------------------------------------------------------------
2
3   SDCCicode.h - intermediate code generation etc.                 
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 #include "SDCCbitv.h"
25 #include "SDCCset.h"
26
27 #ifndef SDCCICODE_H
28 #define SDCCICODE_H 1
29
30 extern symbol *returnLabel;
31 extern symbol *entryLabel;
32 extern int iCodeKey;
33 extern int operandKey;
34
35 enum
36   {
37     CONDITIONAL = 0,
38     EXPRESSION,
39     STATEMENT,
40     LEAF
41   };
42
43 typedef enum
44   {
45     SYMBOL = 1,
46     VALUE,
47     TYPE
48   }
49 OPTYPE;
50
51 #define IS_SYMOP(op) (op && op->type == SYMBOL)
52 #define IS_VALOP(op) (op && op->type == VALUE)
53 #define IS_TYPOP(op) (op && op->type == TYPE)
54
55 #define ADDTOCHAIN(x) addSetHead(&iCodeChain,x)
56
57 #define LRFTYPE       sym_link *ltype = operandType(left), \
58                            *rtype = operandType(right) ;
59 #define LRETYPE       sym_link *letype= getSpec(ltype)   , \
60                            *retype= getSpec(rtype);
61 #define LRTYPE        LRFTYPE LRETYPE
62 #define IS_ITEMP(op)       (IS_SYMOP(op) && op->operand.symOperand->isitmp == 1)
63 #define IS_PARM(op)        (IS_SYMOP(op) && op->operand.symOperand->_isparm)
64 #define IS_ITEMPLBL(op)    (IS_ITEMP(op) && op->operand.symOperand->isilbl == 1);
65 #define IS_OP_VOLATILE(op) (IS_SYMOP(op) && op->isvolatile)
66 #define IS_OP_LITERAL(op)  (op && op->isLiteral)
67 #define IS_OP_GLOBAL(op)   (IS_SYMOP(op) && op->isGlobal)
68 #define IS_OP_POINTER(op)  (IS_SYMOP(op) && op->isPtr)
69 #define IS_OP_PARM(op)     (IS_SYMOP(op) && op->isParm)
70 #define OP_SYMBOL(op)      op->operand.symOperand
71 #define OP_SYM_TYPE(op)    op->operand.symOperand->type
72 #define OP_SYM_ETYPE(op)   op->operand.symOperand->etype
73 #define OP_VALUE(op)       op->operand.valOperand
74 #define SPIL_LOC(op)       op->operand.symOperand->usl.spillLoc
75 #define OP_LIVEFROM(op)    op->operand.symOperand->liveFrom
76 #define OP_LIVETO(op)      op->operand.symOperand->liveTo
77 #define OP_REQV(op)        op->operand.symOperand->reqv
78 #define OP_ISLIVE_FCALL(op) (IS_ITEMP(op) && OP_SYMBOL(op)->isLiveFcall)
79 #define SYM_SPIL_LOC(sym)  sym->usl.spillLoc
80
81 /* typedef for operand */
82 typedef struct operand
83   {
84     OPTYPE type;                /* type of operand */
85     unsigned int isaddr:1;      /* is an address   */
86     unsigned int isvolatile:1;  /* is a volatile operand */
87     unsigned int isGlobal:1;    /* is a global operand */
88     unsigned int isPtr:1;       /* is assigned a pointer */
89     unsigned int isGptr:1;      /* is a generic pointer  */
90     unsigned int isParm:1;      /* is a parameter        */
91     unsigned int isLiteral:1;   /* operand is literal    */
92
93     unsigned key;
94     union
95       {
96         struct symbol *symOperand;      /* operand is of type symbol */
97         struct value *valOperand;       /* operand is of type value  */
98         struct sym_link *typeOperand;   /* operand is of type typechain */
99       }
100     operand;
101
102     bitVect *usesDefs;          /* which definitions are used by this */
103     struct asmop *aop;          /* asm op for this operand */
104   }
105 operand;
106
107 /* definition for intermediate code */
108 #define IC_RESULT(x) (x)->ulrrcnd.lrr.result
109 #define IC_LEFT(x)   (x)->ulrrcnd.lrr.left
110 #define IC_RIGHT(x)  (x)->ulrrcnd.lrr.right
111 #define IC_COND(x)   (x)->ulrrcnd.cnd.condition
112 #define IC_TRUE(x)   (x)->ulrrcnd.cnd.trueLabel
113 #define IC_FALSE(x)  (x)->ulrrcnd.cnd.falseLabel
114 #define IC_LABEL(x)  (x)->argLabel.label
115 // jwk #define IC_ARGS(x)   (x)->argLabel.args
116 #define IC_JTCOND(x) (x)->ulrrcnd.jmpTab.condition
117 #define IC_JTLABELS(x) (x)->ulrrcnd.jmpTab.labels
118 #define IC_INLINE(x) (x)->inlineAsm
119 #define IC_ARRAYILIST(x) (x)->arrayInitList
120
121 typedef struct iCode
122   {
123     unsigned int op;            /* operation defined */
124     int key;                    /* running key for this iCode */
125     int seq;                    /* sequence number within routine */
126     short depth;                /* loop depth of this iCode */
127     short level;                /* scope level */
128     short block;                /* sequential block number */
129     unsigned nosupdate:1;       /* don't update spillocation with this */
130     unsigned generated:1;       /* code generated for this one */
131     unsigned parmPush:1;        /* parameter push Vs spill push */
132     unsigned supportRtn:1;      /* will cause a call to a support routine */
133     unsigned regsSaved:1;       /* registers have been saved */
134     unsigned bankSaved:1;       /* register bank has been saved */
135     unsigned builtinSEND:1;     /* SEND for parameter of builtin function */
136
137     struct iCode *next;         /* next in chain */
138     struct iCode *prev;         /* previous in chain */
139     set *movedFrom;             /* if this iCode gets moved to another block */
140     bitVect *rlive;             /* ranges that are live at this point */
141     int defKey;                 /* key for the operand being defined  */
142     bitVect *uses;              /* vector of key of used symbols      */
143     bitVect *rUsed;             /* registers used by this instruction */
144     bitVect *rMask;             /* registers in use during this instruction */
145     union
146       {
147         struct
148           {
149             operand *left;      /* left if any   */
150             operand *right;     /* right if any  */
151             operand *result;    /* result of this op */
152           }
153         lrr;
154
155         struct
156           {
157             operand *condition; /* if this is a conditional */
158             symbol *trueLabel;  /* true for conditional     */
159             symbol *falseLabel; /* false for conditional    */
160           }
161         cnd;
162
163         struct
164           {
165             operand *condition; /* condition for the jump */
166             set *labels;        /* ordered set of labels  */
167           }
168         jmpTab;
169
170       }
171     ulrrcnd;
172
173     union
174       {
175         symbol *label;          /* for a goto statement     */
176         // jwk value *args;            /* for a function */
177       }
178     argLabel;
179
180     char *inlineAsm;            /* pointer to inline assembler code */
181     literalList *arrayInitList; /* point to array initializer list. */
182
183     int lineno;                 /* file & lineno for debug information */
184     char *filename;
185     
186     int parmBytes;              /* if call/pcall, count of parameter bytes 
187                                    on stack */
188     int eBBlockNum;             /* belongs to which eBBlock */
189   }
190 iCode;
191
192 /* various functions associated to iCode */
193 typedef struct icodeFuncTable
194   {
195     int icode;
196     char *printName;
197     void (*iCodePrint) (FILE *, iCode *, char *);
198     iCode *(*iCodeCopy) (iCode *);
199   }
200 iCodeTable;
201
202 /* useful macros */
203 #define SKIP_IC2(x)  (x->op == GOTO     ||     \
204                       x->op == LABEL    ||     \
205                       x->op == FUNCTION ||     \
206                       x->op == INLINEASM ||    \
207                       x->op == ENDFUNCTION   )
208
209 #define SKIP_IC1(x)  (x->op == CALL     ||     \
210                       SKIP_IC2(x) )
211
212 #define SKIP_IC(x)   (x->op == PCALL    ||     \
213                       x->op == IPUSH    ||     \
214                       x->op == IPOP     ||     \
215                       x->op == JUMPTABLE ||    \
216                       x->op == RECEIVE  ||     \
217                       x->op == ARRAYINIT ||    \
218                       SKIP_IC1(x)||  \
219                       x->op == SEND         )
220
221 #define SKIP_IC3(x) (SKIP_IC2(x) ||     \
222                      x->op == JUMPTABLE )
223
224 #define IS_CONDITIONAL(x) (x->op == EQ_OP || \
225                            x->op == '<'   || \
226                            x->op == '>'   || \
227                            x->op == LE_OP || \
228                            x->op == GE_OP || \
229                            x->op == NE_OP )
230
231 #define IS_TRUE_SYMOP(op) (op && IS_SYMOP(op) && !IS_ITEMP(op))
232
233 #define POINTER_SET(ic) ( ic && ic->op == '='           \
234                              && IS_ITEMP(IC_RESULT(ic)) \
235                              && IC_RESULT(ic)->isaddr )
236
237 #define POINTER_GET(ic) ( ic && ic->op == GET_VALUE_AT_ADDRESS  \
238                              &&  (IS_ITEMP(IC_LEFT(ic)) || IS_OP_LITERAL(IC_LEFT(ic)))\
239                              &&  IC_LEFT(ic)->isaddr )
240
241 #define IS_ARITHMETIC_OP(x) (x && (x->op == '+' || \
242                                    x->op == '-' || \
243                                    x->op == '/' || \
244                                    x->op == '*' || \
245                                    x->op == '%'))
246 #define IS_BITWISE_OP(x) (x && (x->op == BITWISEAND || \
247                                 x->op == '|'        || \
248                                 x->op == '^'))
249
250 #define ASSIGNMENT(ic) ( ic && ic->op == '=')
251
252 #define ASSIGN_SYM_TO_ITEMP(ic) (ic && ic->op == '=' && \
253                              IS_TRUE_SYMOP(IC_RIGHT(ic)) && \
254                              IS_ITEMP(IC_RESULT(ic)))
255
256 #define ASSIGN_ITEMP_TO_SYM(ic) (ic && ic->op == '=' && \
257                              IS_TRUE_SYMOP(IC_RESULT(ic)) && \
258                              IS_ITEMP(IC_RIGHT(ic)))
259
260 #define ASSIGN_ITEMP_TO_ITEMP(ic) (ic && ic->op == '=' &&\
261                                    !POINTER_SET(ic)    &&\
262                                    IS_ITEMP(IC_RIGHT(ic)) &&\
263                                    IS_ITEMP(IC_RESULT(ic)))
264
265 #define ADD_SUBTRACT_ITEMP(ic) (ic && (ic->op == '+' || ic->op == '-') && \
266                                 IS_ITEMP(IC_RESULT(ic)) && \
267                                 ( ( IS_ITEMP(IC_LEFT(ic)) ) ||  ( IS_SYMOP(IC_LEFT(ic)) ) ) && \
268                                   IS_OP_LITERAL(IC_RIGHT(ic)))
269
270 #define ASSIGNMENT_TO_SELF(ic) (!POINTER_SET(ic) && !POINTER_GET(ic) && \
271                                 ic->op == '=' && IC_RESULT(ic)->key == IC_RIGHT(ic)->key )
272
273 #define IS_CAST_ICODE(ic) (ic && ic->op == CAST)
274 #define SET_ISADDR(op,v) {op = operandFromOperand(op); op->isaddr = v;}
275 #define SET_RESULT_RIGHT(ic) {SET_ISADDR(IC_RIGHT(ic),0); SET_ISADDR(IC_RESULT(ic),0);}
276 #define IS_ASSIGN_ICODE(ic) (ASSIGNMENT(ic) && !POINTER_SET(ic))
277
278 #define OP_DEFS(op) op->operand.symOperand->defs
279 #define OP_USES(op) op->operand.symOperand->uses
280 /*-----------------------------------------------------------------*/
281 /* forward references for functions                                */
282 /*-----------------------------------------------------------------*/
283 iCode *reverseiCChain ();
284 bool isOperandOnStack (operand *);
285 int isOperandVolatile (operand *, bool);
286 int isOperandGlobal (operand *);
287 void printiCChain (iCode *, FILE *);
288 operand *ast2iCode (ast *,int);
289 operand *geniCodeCast (sym_link *, operand *, bool);
290 operand *geniCodePtrPtrSubtract (operand *, operand *);
291 void initiCode ();
292 iCode *iCodeFromAst (ast *);
293 int isiCodeEqual (iCode *, iCode *);
294 int isOperandEqual (operand *, operand *);
295 iCodeTable *getTableEntry (int);
296 int isOperandLiteral (operand *);
297 operand *operandOperation (operand *, operand *, int, sym_link *);
298 double operandLitValue (operand *);
299 operand *operandFromLit (double);
300 operand *operandFromOperand (operand *);
301 int isParameterToCall (value *, operand *);
302 iCode *newiCodeLabelGoto (int, symbol *);
303 symbol *newiTemp (char *);
304 symbol *newiTempLabel (char *);
305 symbol *newiTempPreheaderLabel ();
306 iCode *newiCode (int, operand *, operand *);
307 sym_link *operandType (operand *);
308 operand *operandFromValue (value *);
309 operand *operandFromSymbol (symbol *);
310 sym_link *aggrToPtr (sym_link *, bool);
311 int piCode (void *, FILE *);
312 int printOperand (operand *, FILE *);
313 void setOperandType (operand *, sym_link *);
314 bool isOperandInFarSpace (operand *);
315 bool isOperandInDirSpace (operand *);
316 bool isOperandInCodeSpace (operand *);
317 operand *opFromOpWithDU (operand *, bitVect *, bitVect *);
318 iCode *copyiCode (iCode *);
319 operand *newiTempFromOp (operand *);
320 iCode *getBuiltinParms (iCode *,int *, operand **);
321 /*-----------------------------------------------------------------*/
322 /* declaration of exported variables                               */
323 /*-----------------------------------------------------------------*/
324 extern char *filename;
325 extern int lineno;
326 #endif