b70f56fdfff95949a81addec74430bb9e2a6edbf
[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 7797 2007-04-23 20:12:39Z michi $
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/builtin.h"
50 #include "vm/global.h"
51
52 #include "vm/jit/dseg.h"
53 #include "vm/jit/jit.h"
54 #include "vm/jit/reg.h"
55 #include "vm/jit/code.h"
56 #include "vm/jit/replace.h"
57
58 #include "vmcore/descriptor.h"
59 #include "vmcore/method.h"
60 #include "vmcore/references.h"
61
62
63 #define MCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
64 #define DSEGINITSIZE  (1<<12)       /*  4 Kbyte data area initialization size */
65
66 #define NCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
67
68
69 /* Register Pack/Unpack Macros ************************************************/
70
71 /* ATTENTION: Don't change the order where low and high bits are
72    stored! At least mips32 relies in one case on that order. */
73
74 #define PACK_REGS(low,high) \
75     ( (((high) & 0x0000ffff) << 16) | ((low) & 0x0000ffff) )
76
77 #define GET_LOW_REG(a)      ((a) & 0x0000ffff)
78 #define GET_HIGH_REG(a)    (((a) & 0xffff0000) >> 16)
79
80
81 /* branch conditions **********************************************************/
82
83 #define BRANCH_UNCONDITIONAL    -1
84
85 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
86 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
87 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
88 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
89 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
90 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
91
92 #define BRANCH_ULT              256
93 #define BRANCH_ULE              257
94 #define BRANCH_UGE              258
95 #define BRANCH_UGT              259
96
97 #define BRANCH_NAN              260
98
99
100 /* common branch options ******************************************************/
101
102 #define BRANCH_OPT_NONE         0
103
104
105
106 /************************* critical sections  *********************************/
107
108 struct codegen_critical_section_t {
109         codegen_critical_section_t *next;
110         s4                  mcodebegin;
111         s4                  mcodeend;
112         s4                  mcoderestart;
113 };
114
115
116 /* codegendata ****************************************************************/
117
118 struct codegendata {
119         u4              flags;          /* code generator flags                   */
120         u1             *mcodebase;      /* base pointer of code area              */
121         u1             *mcodeend;       /* pointer to end of code area            */
122         s4              mcodesize;      /* complete size of code area (bytes)     */
123         u1             *mcodeptr;       /* code generation pointer                */
124         u1             *lastmcodeptr;   /* last patcher position of basic block   */
125
126 #if defined(ENABLE_INTRP)
127         u1             *ncodebase;      /* base pointer of native code area       */
128         s4              ncodesize;      /* complete size of native code area      */
129         u1             *ncodeptr;       /* native code generation pointer         */
130
131         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
132
133         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
134         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
135         s4              dynsupern;      /* ... superinstruction starts            */
136         struct superstart *superstarts; /* list of supers without patchers        */
137 #endif
138
139         dsegentry      *dseg;           /* chain of data segment entries          */
140         s4              dseglen;        /* used size of data area (bytes)         */
141                                     /* data area grows from top to bottom     */
142
143         jumpref        *jumpreferences; /* list of jumptable target addresses     */
144
145 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
146         dataref        *datareferences; /* list of data segment references        */
147 #endif
148
149 /*      list_t         *patchrefs; */
150         patchref       *patchrefs;
151         list_t         *brancheslabel;
152
153         linenumberref  *linenumberreferences; /* list of line numbers and the     */
154                                         /* program counters of their first        */
155                                         /* instruction                            */
156         s4              linenumbertablesizepos;
157         s4              linenumbertablestartpos;
158         s4              linenumbertab;
159
160         methodinfo     *method;
161
162         codegen_critical_section_t *threadcrit; /* List of critical code regions          */
163         codegen_critical_section_t threadcritcurrent;
164         s4                 threadcritcount; /* Number of critical regions         */
165
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_t 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 #if defined(ENABLE_DISASSEMBLER)
313 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
314 #endif
315
316 /* stub functions */
317
318 u1       *codegen_generate_stub_compiler(methodinfo *m);
319 void      codegen_generate_stub_builtin(builtintable_entry *bte);
320 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f);
321
322 void      codegen_emit_stub_compiler(jitdata *jd);
323 void      codegen_emit_stub_builtin(jitdata *jd, builtintable_entry *bte);
324 void      codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f);
325
326 #if defined(ENABLE_INTRP)
327 u1 *intrp_createcompilerstub(methodinfo *m);
328 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *md);
329 #endif
330
331 void removecompilerstub(u1 *stub);
332 void removenativestub(u1 *stub);
333
334 void codegen_stub_builtin_enter(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
335 void codegen_stub_builtin_exit(u1 *datasp);
336
337 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
338 java_objectheader *codegen_finish_native_call(u1 *datasp);
339
340 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
341 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
342
343 #if defined(ENABLE_THREADS)
344 void codegen_threadcritrestart(codegendata *cd, int offset);
345 void codegen_threadcritstart(codegendata *cd, int offset);
346 void codegen_threadcritstop(codegendata *cd, int offset);
347 #endif
348
349 /* machine dependent functions */
350 u1 *md_codegen_get_pv_from_pc(u1 *ra);
351
352 #endif /* _CODEGEN_COMMON_H */
353
354
355 /*
356  * These are local overrides for various environment variables in Emacs.
357  * Please do not remove this and leave it at the end of the file, where
358  * Emacs will automagically detect them.
359  * ---------------------------------------------------------------------
360  * Local variables:
361  * mode: c
362  * indent-tabs-mode: t
363  * c-basic-offset: 4
364  * tab-width: 4
365  * End:
366  * vim:noexpandtab:sw=4:ts=4:
367  */