Merged revisions 7797-7917 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 7918 2007-05-20 20:42:18Z michi $
26
27 */
28
29
30 #ifndef _CODEGEN_COMMON_H
31 #define _CODEGEN_COMMON_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct codegendata            codegendata;
36 typedef struct branchref              branchref;
37 typedef struct branch_label_ref_t     branch_label_ref_t;
38 typedef struct critical_section_ref_t critical_section_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 /* codegendata ****************************************************************/
106
107 struct codegendata {
108         u4              flags;          /* code generator flags                   */
109         u1             *mcodebase;      /* base pointer of code area              */
110         u1             *mcodeend;       /* pointer to end of code area            */
111         s4              mcodesize;      /* complete size of code area (bytes)     */
112         u1             *mcodeptr;       /* code generation pointer                */
113         u1             *lastmcodeptr;   /* last patcher position of basic block   */
114
115 #if defined(ENABLE_INTRP)
116         u1             *ncodebase;      /* base pointer of native code area       */
117         s4              ncodesize;      /* complete size of native code area      */
118         u1             *ncodeptr;       /* native code generation pointer         */
119
120         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
121
122         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
123         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
124         s4              dynsupern;      /* ... superinstruction starts            */
125         struct superstart *superstarts; /* list of supers without patchers        */
126 #endif
127
128         dsegentry      *dseg;           /* chain of data segment entries          */
129         s4              dseglen;        /* used size of data area (bytes)         */
130                                     /* data area grows from top to bottom     */
131
132         jumpref        *jumpreferences; /* list of jumptable target addresses     */
133
134 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
135         dataref        *datareferences; /* list of data segment references        */
136 #endif
137
138 /*      list_t         *patchrefs; */
139         patchref       *patchrefs;
140         list_t         *brancheslabel;
141         list_t         *listcritical;   /* list of critical sections              */
142
143         linenumberref  *linenumberreferences; /* list of line numbers and the     */
144                                         /* program counters of their first        */
145                                         /* instruction                            */
146         s4              linenumbertablesizepos;
147         s4              linenumbertablestartpos;
148         s4              linenumbertab;
149
150         methodinfo     *method;
151
152         s4              stackframesize;    /* stackframe size of this method      */
153
154 #if defined(ENABLE_REPLACEMENT)
155         rplpoint       *replacementpoint;  /* current replacement point           */
156 #endif
157 };
158
159
160 #define CODEGENDATA_FLAG_ERROR           0x00000001
161 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
162
163
164 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
165     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
166
167 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
168     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
169
170
171 /* branchref *****************************************************************/
172
173 struct branchref {
174         s4         branchmpc;       /* patching position in code segment          */
175         s4         condition;       /* conditional branch condition               */
176         s4         reg;             /* register number to check                   */
177         u4         options;         /* branch options                             */
178         branchref *next;            /* next element in branchref list             */
179 };
180
181
182 /* branch_label_ref_t *********************************************************/
183
184 struct branch_label_ref_t {
185         s4         mpc;             /* position in code segment                   */
186         s4         label;           /* label number                               */
187         s4         condition;       /* conditional branch condition               */
188         s4         reg;             /* register number to check                   */
189         u4         options;         /* branch options                             */
190         listnode_t linkage;
191 };
192
193
194 /* critical_section_ref_t *****************************************************/
195
196 struct critical_section_ref_t {
197         s4         start;           /* relative offset to method entry-point      */
198         s4         end;
199         s4         restart;
200         listnode_t linkage;
201 };
202
203
204 /* jumpref ********************************************************************/
205
206 struct jumpref {
207         s4          tablepos;       /* patching position in data segment          */
208         basicblock *target;         /* target basic block                         */
209         jumpref    *next;           /* next element in jumpref list               */
210 };
211
212
213 /* dataref ********************************************************************/
214
215 struct dataref {
216         s4       datapos;           /* patching position in generated code        */
217         dataref *next;              /* next element in dataref list               */
218 };
219
220
221 /* patchref *******************************************************************/
222
223 struct patchref {
224         s4           branchpos;     /* relative offset to method entrypoint       */
225         s4           disp;          /* displacement of ref in the data segment    */
226         functionptr  patcher;       /* patcher function to call                   */
227         voidptr      ref;           /* reference passed                           */
228 /*      listnode     linkage; */
229         patchref    *next;
230 };
231
232
233 /* linenumberref **************************************************************/
234
235 struct linenumberref {
236         s4             tablepos;    /* patching position in data segment          */
237         s4             linenumber;  /* line number, used for inserting into the   */
238                                     /* table and for validity checking            */
239                                     /* -1......start of inlined body              */
240                                     /* -2......end of inlined body                */
241                                     /* <= -3...special entry with methodinfo *    */
242                                                                 /* (see doc/inlining_stacktrace.txt)          */
243         ptrint         targetmpc;   /* machine code program counter of first      */
244                                     /* instruction for given line                 */
245                                                                 /* NOTE: for linenumber <= -3 this is a the   */
246                                     /* (methodinfo *) of the inlined method       */
247         linenumberref *next;        /* next element in linenumberref list         */
248 };
249
250
251 /* methodtree_element *********************************************************/
252
253 typedef struct methodtree_element methodtree_element;
254
255 struct methodtree_element {
256         u1 *startpc;
257         u1 *endpc;
258 };
259
260
261 /* function prototypes ********************************************************/
262
263 void codegen_init(void);
264 void codegen_setup(jitdata *jd);
265
266 bool codegen_generate(jitdata *jd);
267 bool codegen_emit(jitdata *jd);
268
269 #if defined(ENABLE_INTRP)
270 bool intrp_codegen(jitdata *jd);
271 #endif
272
273 void codegen_close(void);
274
275 void codegen_increase(codegendata *cd);
276
277 #if defined(ENABLE_INTRP)
278 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
279 #endif
280
281 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
282 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
283
284 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
285
286
287 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
288                                                    s4 disp);
289 /* XXX REMOVE ME: don't-break-trunk macro */
290 #define codegen_addpatchref codegen_add_patch_ref
291
292 void codegen_insertmethod(u1 *startpc, u1 *endpc);
293 u1 *codegen_get_pv_from_pc(u1 *pc);
294 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc);
295
296 #if defined(ENABLE_REPLACEMENT)
297 #if !defined(NDEBUG)
298 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
299 void codegen_set_replacement_point(codegendata *cd, s4 type);
300 #else
301 void codegen_set_replacement_point_notrap(codegendata *cd);
302 void codegen_set_replacement_point(codegendata *cd);
303 #endif
304 #endif /* defined(ENABLE_REPLACEMENT) */
305
306 void codegen_finish(jitdata *jd);
307
308 #if defined(ENABLE_DISASSEMBLER)
309 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end);
310 #endif
311
312 /* stub functions */
313
314 u1       *codegen_generate_stub_compiler(methodinfo *m);
315 void      codegen_generate_stub_builtin(builtintable_entry *bte);
316 codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f);
317
318 void      codegen_emit_stub_compiler(jitdata *jd);
319 void      codegen_emit_stub_builtin(jitdata *jd, builtintable_entry *bte);
320 void      codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f);
321
322 #if defined(ENABLE_INTRP)
323 u1 *intrp_createcompilerstub(methodinfo *m);
324 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *md);
325 #endif
326
327 void removecompilerstub(u1 *stub);
328 void removenativestub(u1 *stub);
329
330 void codegen_stub_builtin_enter(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
331 void codegen_stub_builtin_exit(u1 *datasp);
332
333 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra);
334 java_objectheader *codegen_finish_native_call(u1 *datasp);
335
336 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
337 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
338
339 #if defined(ENABLE_THREADS)
340 void codegen_critical_section_new(codegendata *cd);
341 void codegen_critical_section_start(codegendata *cd);
342 void codegen_critical_section_end(codegendata *cd);
343
344 # define CODEGEN_CRITICAL_SECTION_NEW      codegen_critical_section_new(cd)
345 # define CODEGEN_CRITICAL_SECTION_START    codegen_critical_section_start(cd)
346 # define CODEGEN_CRITICAL_SECTION_END      codegen_critical_section_end(cd)
347 #else
348 # define CODEGEN_CRITICAL_SECTION_NEW      /* no-op */
349 # define CODEGEN_CRITICAL_SECTION_START    /* no-op */
350 # define CODEGEN_CRITICAL_SECTION_END      /* no-op */
351 #endif
352
353 /* machine dependent functions */
354 u1 *md_codegen_get_pv_from_pc(u1 *ra);
355
356
357 #if defined(ENABLE_SSA)
358 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
359 #endif
360
361 #endif /* _CODEGEN_COMMON_H */
362
363
364 /*
365  * These are local overrides for various environment variables in Emacs.
366  * Please do not remove this and leave it at the end of the file, where
367  * Emacs will automagically detect them.
368  * ---------------------------------------------------------------------
369  * Local variables:
370  * mode: c
371  * indent-tabs-mode: t
372  * c-basic-offset: 4
373  * tab-width: 4
374  * End:
375  * vim:noexpandtab:sw=4:ts=4:
376  */