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