* src/vm/jit/codegen-common.cpp, src/vm/jit/x86_64/codegen.c: Generate
[cacao.git] / src / vm / jit / codegen-common.hpp
1 /* src/vm/jit/codegen-common.hpp - architecture independent code generator stuff
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CODEGEN_COMMON_HPP
27 #define _CODEGEN_COMMON_HPP
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct codegendata            codegendata;
32 typedef struct branchref              branchref;
33 typedef struct branch_label_ref_t     branch_label_ref_t;
34 typedef struct jumpref                jumpref;
35 typedef struct dataref                dataref;
36
37 struct patchref_t;
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include "toolbox/list.hpp"
43
44 #include "vm/jit/builtin.hpp"
45 #include "vm/descriptor.hpp"
46 #include "vm/global.h"
47 #include "vm/method.hpp"
48 #include "vm/references.h"
49
50 #include "vm/jit/dseg.h"
51 #include "vm/jit/jit.hpp"
52 #include "vm/jit/reg.h"
53 #include "vm/jit/code.hpp"
54 #include "vm/jit/linenumbertable.hpp"
55 #include "vm/jit/replace.hpp"
56
57
58 #define MCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
59 #define DSEGINITSIZE  (1<<12)       /*  4 Kbyte data area initialization size */
60
61 #define NCODEINITSIZE (1<<15)       /* 32 Kbyte code area initialization size */
62
63
64 /* Register Pack/Unpack Macros ************************************************/
65
66 /* ATTENTION: Don't change the order where low and high bits are
67    stored! At least mips32 relies in one case on that order. */
68
69 #define PACK_REGS(low,high) \
70     ( (((high) & 0x0000ffff) << 16) | ((low) & 0x0000ffff) )
71
72 #define GET_LOW_REG(a)      ((a) & 0x0000ffff)
73 #define GET_HIGH_REG(a)    (((a) & 0xffff0000) >> 16)
74
75 /* All 32-bit machines we support use packed registers to store
76    return values and temporary values. */
77
78 #if SIZEOF_VOID_P == 8
79 # define REG_LRESULT         REG_RESULT
80 # define REG_LTMP12          REG_ITMP1
81 # define REG_LTMP23          REG_ITMP2
82 #else
83 # define REG_LRESULT         REG_RESULT_PACKED
84 # define REG_LTMP12          REG_ITMP12_PACKED
85 # define REG_LTMP23          REG_ITMP23_PACKED
86 #endif
87
88
89 /* branch conditions **********************************************************/
90
91 #define BRANCH_UNCONDITIONAL    -1
92
93 #define BRANCH_EQ               (ICMD_IFEQ - ICMD_IFEQ)
94 #define BRANCH_NE               (ICMD_IFNE - ICMD_IFEQ)
95 #define BRANCH_LT               (ICMD_IFLT - ICMD_IFEQ)
96 #define BRANCH_GE               (ICMD_IFGE - ICMD_IFEQ)
97 #define BRANCH_GT               (ICMD_IFGT - ICMD_IFEQ)
98 #define BRANCH_LE               (ICMD_IFLE - ICMD_IFEQ)
99
100 #define BRANCH_ULT              256
101 #define BRANCH_ULE              257
102 #define BRANCH_UGE              258
103 #define BRANCH_UGT              259
104
105 #define BRANCH_NAN              260
106
107
108 /* common branch options ******************************************************/
109
110 #define BRANCH_OPT_NONE         0
111
112
113 /* codegendata ****************************************************************/
114
115 struct codegendata {
116         u4              flags;          /* code generator flags                   */
117         u1             *mcodebase;      /* base pointer of code area              */
118         u1             *mcodeend;       /* pointer to end of code area            */
119         s4              mcodesize;      /* complete size of code area (bytes)     */
120         u1             *mcodeptr;       /* code generation pointer                */
121         u1             *lastmcodeptr;   /* last patcher position of basic block   */
122
123 #if defined(ENABLE_INTRP)
124         u1             *ncodebase;      /* base pointer of native code area       */
125         s4              ncodesize;      /* complete size of native code area      */
126         u1             *ncodeptr;       /* native code generation pointer         */
127
128         u4              lastinstwithoutdispatch; /* ~0 if there was a dispatch    */
129
130         s4              lastpatcheroffset; /* -1 if current super has no patcher  */
131         s4              dynsuperm;      /* offsets of start of current dynamic ...*/
132         s4              dynsupern;      /* ... superinstruction starts            */
133         struct superstart *superstarts; /* list of supers without patchers        */
134 #endif
135
136         dsegentry      *dseg;           /* chain of data segment entries          */
137         s4              dseglen;        /* used size of data area (bytes)         */
138                                     /* data area grows from top to bottom     */
139
140         jumpref        *jumpreferences; /* list of jumptable target addresses     */
141
142 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP) || defined(__S390__)
143         dataref        *datareferences; /* list of data segment references        */
144 #endif
145
146 #ifdef __cplusplus
147         DumpList<branch_label_ref_t*>* brancheslabel;
148         DumpList<Linenumber>* linenumbers; ///< List of line numbers.
149 #else
150         // REMOVEME
151         DumpList* brancheslabel;
152         DumpList* linenumbers;
153 #endif
154
155         methodinfo     *method;
156
157         s4              stackframesize;    /* stackframe size of this method      */
158
159 #if defined(ENABLE_REPLACEMENT)
160         rplpoint       *replacementpoint;  /* current replacement point           */
161 #endif
162 };
163
164
165 #define CODEGENDATA_FLAG_ERROR           0x00000001
166 #define CODEGENDATA_FLAG_LONGBRANCHES    0x00000002
167
168
169 #define CODEGENDATA_HAS_FLAG_ERROR(cd) \
170     ((cd)->flags & CODEGENDATA_FLAG_ERROR)
171
172 #define CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd) \
173     ((cd)->flags & CODEGENDATA_FLAG_LONGBRANCHES)
174
175
176 /* branchref *****************************************************************/
177
178 struct branchref {
179         s4         branchmpc;       /* patching position in code segment          */
180         s4         condition;       /* conditional branch condition               */
181         s4         reg;             /* register number to check                   */
182         u4         options;         /* branch options                             */
183         branchref *next;            /* next element in branchref list             */
184 };
185
186
187 /* branch_label_ref_t *********************************************************/
188
189 struct branch_label_ref_t {
190         s4         mpc;             /* position in code segment                   */
191         s4         label;           /* label number                               */
192         s4         condition;       /* conditional branch condition               */
193         s4         reg;             /* register number to check                   */
194         u4         options;         /* branch options                             */
195 /*      listnode_t linkage; */
196 };
197
198
199 /* jumpref ********************************************************************/
200
201 struct jumpref {
202         s4          tablepos;       /* patching position in data segment          */
203         basicblock *target;         /* target basic block                         */
204         jumpref    *next;           /* next element in jumpref list               */
205 };
206
207
208 /* dataref ********************************************************************/
209
210 struct dataref {
211         s4       datapos;           /* patching position in generated code        */
212         dataref *next;              /* next element in dataref list               */
213 };
214
215
216 /* function prototypes ********************************************************/
217
218 #ifdef __cplusplus
219 extern "C" {
220 #endif
221
222 void codegen_init(void);
223 void codegen_setup(jitdata *jd);
224
225 bool codegen_generate(jitdata *jd);
226 bool codegen_emit(jitdata *jd);
227
228 void codegen_emit_prolog(jitdata* jd);
229 void codegen_emit_epilog(jitdata* jd);
230 void codegen_emit_instruction(jitdata* jd, instruction* iptr);
231
232 #if defined(USES_PATCHABLE_MEMORY_BARRIER)
233 void codegen_emit_patchable_barrier(instruction *iptr, codegendata *cd, struct patchref_t *pr, fieldinfo *fi);
234 #endif
235
236 #if defined(ENABLE_INTRP)
237 bool intrp_codegen(jitdata *jd);
238 #endif
239
240 void codegen_close(void);
241
242 void codegen_increase(codegendata *cd);
243
244 #if defined(ENABLE_INTRP)
245 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr);
246 #endif
247
248 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options);
249 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr);
250
251 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options);
252
253 #if defined(ENABLE_REPLACEMENT)
254 #if !defined(NDEBUG)
255 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type);
256 void codegen_set_replacement_point(codegendata *cd, s4 type);
257 #else
258 void codegen_set_replacement_point_notrap(codegendata *cd);
259 void codegen_set_replacement_point(codegendata *cd);
260 #endif
261 #endif /* defined(ENABLE_REPLACEMENT) */
262
263 void codegen_finish(jitdata *jd);
264
265 java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv);
266 java_object_t *codegen_finish_native_call(u1 *currentsp, u1 *pv);
267
268 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum);
269 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum);
270
271 #if defined(ENABLE_SSA)
272 void codegen_emit_phi_moves(jitdata *jd, basicblock *bptr);
273 #endif
274
275 // REMOVEME
276 void codegen_emit_stub_compiler(jitdata *jd);
277 void codegen_emit_stub_native(jitdata *jd, methoddesc *nmd, functionptr f, int skipparams);
278
279 #ifdef __cplusplus
280 }
281 #endif
282
283 #endif // _CODEGEN_COMMON_HPP
284
285
286 /*
287  * These are local overrides for various environment variables in Emacs.
288  * Please do not remove this and leave it at the end of the file, where
289  * Emacs will automagically detect them.
290  * ---------------------------------------------------------------------
291  * Local variables:
292  * mode: c++
293  * indent-tabs-mode: t
294  * c-basic-offset: 4
295  * tab-width: 4
296  * End:
297  * vim:noexpandtab:sw=4:ts=4:
298  */