Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / vm / jit / codegen-common.h
1 /* src/vm/jit/codegen-common.h - architecture independent code generator stuff
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: codegen-common.h 7596 2007-03-28 21:05:53Z twisti $
26
27 */
28
29
30 #ifndef _CODEGEN_COMMON_H
31 #define _CODEGEN_COMMON_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct codegen_critical_section_t codegen_critical_section_t;
36 typedef struct codegendata                codegendata;
37 typedef struct branchref                  branchref;
38 typedef struct branch_label_ref_t         branch_label_ref_t;
39 typedef struct jumpref                    jumpref;
40 typedef struct dataref                    dataref;
41 typedef struct exceptionref               exceptionref;
42 typedef struct patchref                   patchref;
43 typedef struct linenumberref              linenumberref;
44
45
46 #include "config.h"
47 #include "vm/types.h"
48
49 #include "vm/global.h"
50
51 #include "vm/jit/dseg.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/reg.h"
54 #include "vm/jit/code.h"
55 #include "vm/jit/replace.h"
56
57 #include "vmcore/descriptor.h"
58 #include "vmcore/method.h"
59 #include "vmcore/references.h"
60
61
62 #define MCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
63 #define DSEGINITSIZE  (1<<12)       /*  4 Kbyte data area initialization size */
64
65 #define NCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
66
67
68 /* Register Pack/Unpack Macros ************************************************/
69
70 /* ATTENTION: Don't change the order where low and high bits are
71    stored! At least mips32 relies in one case on that order. */
72
73 #define PACK_REGS(low,high) \
74     ( (((high) & 0x0000ffff) << 16) | ((low) & 0x0000ffff) )
75
76 #define GET_LOW_REG(a)      ((a) & 0x0000ffff)
77 #define GET_HIGH_REG(a)    (((a) & 0xffff0000) >> 16)
78
79
80 /* branch conditions **********************************************************/
81
82 #define BRANCH_UNCONDITIONAL    -1
83
84 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
85 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
86 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
87 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
88 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
89 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
90
91 #define BRANCH_ULT              256
92 #define BRANCH_ULE              257
93 #define BRANCH_UGE              258
94 #define BRANCH_UGT              259
95
96 #define BRANCH_NAN              260
97
98
99 /* common branch options ******************************************************/
100
101 #define BRANCH_OPT_NONE         0
102
103
104
105 /************************* critical sections  *********************************/
106
107 struct codegen_critical_section_t {
108         codegen_critical_section_t *next;
109         s4                  mcodebegin;
110         s4                  mcodeend;
111         s4                  mcoderestart;
112 };
113
114
115 /* codegendata ****************************************************************/
116
117 struct codegendata {
118         u4              flags;          /* code generator flags                   */
119         u1             *mcodebase;      /* base pointer of code area              */
120         u1             *mcodeend;       /* pointer to end of code area            */
121         s4              mcodesize;      /* complete size of code area (bytes)     */
122         u1             *mcodeptr;       /* code generation pointer                */
123         u1             *lastmcodeptr;   /* last patcher position of basic block   */
124
125 #if defined(ENABLE_INTRP)
126         u1             *ncodebase;      /* base pointer of native code area       */
127         s4              ncodesize;      /* complete size of native code area      */
128         u1             *ncodeptr;       /* native code generation pointer         */
129
130         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
131
132         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
133         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
134         s4              dynsupern;      /* ... superinstruction starts            */
135         struct superstart *superstarts; /* list of supers without patchers        */
136 #endif
137
138         dsegentry      *dseg;           /* chain of data segment entries          */
139         s4              dseglen;        /* used size of data area (bytes)         */
140                                     /* data area grows from top to bottom     */
141
142         jumpref        *jumpreferences; /* list of jumptable target addresses     */
143
144 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
145         dataref        *datareferences; /* list of data segment references        */
146 #endif
147
148 /*      list           *patchrefs; */
149         patchref       *patchrefs;
150         list           *brancheslabel;
151
152         linenumberref  *linenumberreferences; /* list of line numbers and the     */
153                                         /* program counters of their first        */
154                                         /* instruction                            */
155         s4              linenumbertablesizepos;
156         s4              linenumbertablestartpos;
157         s4              linenumbertab;
158
159         methodinfo     *method;
160
161         codegen_critical_section_t *threadcrit; /* List of critical code regions          */
162         codegen_critical_section_t threadcritcurrent;
163         s4                 threadcritcount; /* Number of critical regions         */
164
165         s4              maxstack;
166         s4              stackframesize;    /* stackframe size of this method      */
167
168 #if defined(ENABLE_REPLACEMENT)
169         rplpoint       *replacementpoint;  /* current replacement point           */
170 #endif
171 };
172
173
174 #define CODEGENDATA_FLAG_ERROR           0x00000001
175 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
176
177
178 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
179     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
180
181 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
182     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
183
184
185 /* branchref *****************************************************************/
186
187 struct branchref {
188         s4         branchmpc;       /* patching position in code segment          */
189         s4         condition;       /* conditional branch condition               */
190         s4         reg;             /* register number to check                   */
191         u4         options;         /* branch options                             */
192         branchref *next;            /* next element in branchref list             */
193 };
194
195
196 /* branch_label_ref_t *********************************************************/
197
198 struct branch_label_ref_t {
199         s4         mpc;             /* position in code segment                   */
200         s4         label;           /* label number                               */
201         s4         condition;       /* conditional branch condition               */
202         s4         reg;             /* register number to check                   */
203         u4         options;         /* branch options                             */
204         listnode   linkage;
205 };
206
207
208 /* jumpref ********************************************************************/
209
210 struct jumpref {
211         s4          tablepos;       /* patching position in data segment          */
212         basicblock *target;         /* target basic block                         */
213         jumpref    *next;           /* next element in jumpref list               */
214 };
215
216
217 /* dataref ********************************************************************/
218
219 struct dataref {
220         s4       datapos;           /* patching position in generated code        */
221         dataref *next;              /* next element in dataref list               */
222 };
223
224
225 /* patchref *******************************************************************/
226
227 struct patchref {
228         s4           branchpos;     /* relative offset to method entrypoint       */
229         s4           disp;          /* displacement of ref in the data segment    */
230         functionptr  patcher;       /* patcher function to call                   */
231         voidptr      ref;           /* reference passed                           */
232 /*      listnode     linkage; */
233         patchref    *next;
234 };
235
236
237 /* linenumberref **************************************************************/
238
239 struct linenumberref {
240         s4             tablepos;    /* patching position in data segment          */
241         s4             linenumber;  /* line number, used for inserting into the   */
242                                     /* table and for validity checking            */
243                                     /* -1......start of inlined body              */
244                                     /* -2......end of inlined body                */
245                                     /* <= -3...special entry with methodinfo *    */
246                                                                 /* (see doc/inlining_stacktrace.txt)          */
247         ptrint         targetmpc;   /* machine code program counter of first      */
248                                     /* instruction for given line                 */
249                                                                 /* NOTE: for linenumber <= -3 this is a the   */
250                                     /* (methodinfo *) of the inlined method       */
251         linenumberref *next;        /* next element in linenumberref list         */
252 };
253
254
255 /* methodtree_element *********************************************************/
256
257 typedef struct methodtree_element methodtree_element;
258
259 struct methodtree_element {
260         u1 *startpc;
261         u1 *endpc;
262 };
263
264
265 /* function prototypes ********************************************************/
266
267 void codegen_init(void);
268 void codegen_setup(jitdata *jd);
269
270 bool codegen_generate(jitdata *jd);
271 bool codegen_emit(jitdata *jd);
272
273 #if defined(ENABLE_INTRP)
274 bool intrp_codegen(jitdata *jd);
275 #endif
276
277 void codegen_close(void);
278
279 void codegen_increase(codegendata *cd);
280
281 #if defined(ENABLE_INTRP)
282 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
283 #endif
284
285 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
286 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
287
288 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
289
290
291 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
292                                                    s4 disp);
293 /* XXX REMOVE ME: don't-break-trunk macro */
294 #define codegen_addpatchref codegen_add_patch_ref
295
296 void codegen_insertmethod(u1 *startpc, u1 *endpc);
297 u1 *codegen_get_pv_from_pc(u1 *pc);
298 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc);
299
300 #if defined(ENABLE_REPLACEMENT)
301 #if !defined(NDEBUG)
302 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
303 void codegen_set_replacement_point(codegendata *cd, s4 type);
304 #else
305 void codegen_set_replacement_point_notrap(codegendata *cd);
306 void codegen_set_replacement_point(codegendata *cd);
307 #endif
308 #endif /* defined(ENABLE_REPLACEMENT) */
309
310 void codegen_finish(jitdata *jd);
311
312 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m);
313 #if defined(ENABLE_DISASSEMBLER)
314 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
315 #endif
316
317 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
318 java_objectheader *codegen_finish_native_call(u1 *datasp);
319
320 u1 *createcompilerstub(methodinfo *m);
321 u1 *createnativestub(functionptr f, jitdata *jd, methoddesc *nmd);
322
323 #if defined(ENABLE_INTRP)
324 u1 *intrp_createcompilerstub(methodinfo *m);
325 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *md);
326 #endif
327
328 void removecompilerstub(u1 *stub);
329 void removenativestub(u1 *stub);
330
331 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
332 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
333
334 #if defined(ENABLE_THREADS)
335 void codegen_threadcritrestart(codegendata *cd, int offset);
336 void codegen_threadcritstart(codegendata *cd, int offset);
337 void codegen_threadcritstop(codegendata *cd, int offset);
338 #endif
339
340 /* machine dependent functions */
341 u1 *md_codegen_get_pv_from_pc(u1 *ra);
342
343 #endif /* _CODEGEN_COMMON_H */
344
345
346 /*
347  * These are local overrides for various environment variables in Emacs.
348  * Please do not remove this and leave it at the end of the file, where
349  * Emacs will automagically detect them.
350  * ---------------------------------------------------------------------
351  * Local variables:
352  * mode: c
353  * indent-tabs-mode: t
354  * c-basic-offset: 4
355  * tab-width: 4
356  * End:
357  * vim:noexpandtab:sw=4:ts=4:
358  */