CACAO
replace.hpp
Go to the documentation of this file.
1 /* src/vm/jit/replace.hpp - on-stack replacement of methods
2 
3  Copyright (C) 1996-2013
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 REPLACE_HPP_
27 #define REPLACE_HPP_ 1
28 
29 #include <stddef.h> // for NULL
30 #include "config.h" // for ENABLE_JIT, etc
31 #include "md-abi.hpp" // for FLT_REG_CNT, INT_SAV_CNT
32 #include "vm/jit/jit.hpp" // for basicblock::Type
33 #include "vm/types.hpp" // for s4, u1, ptrint, u4, s8
34 
35 struct codeinfo;
36 struct executionstate_t;
37 struct java_object_t;
38 struct jitdata;
39 struct methodinfo;
40 struct rplalloc;
41 struct rplpoint;
42 struct sourceframe_t;
43 struct sourcestate_t;
44 struct stackframeinfo_t;
45 union replace_val_t;
46 
47 #if !defined(ENABLE_REPLACEMENT)
48 
49 /*** macros for the codegens (disabled version) ************************/
50 
51 #define REPLACEMENT_POINTS_INIT(cd, jd)
52 #define REPLACEMENT_POINTS_RESET(cd, jd)
53 #define REPLACEMENT_POINT_BLOCK_START(cd, bptr)
54 #define REPLACEMENT_POINT_INLINE_START(cd, iptr)
55 #define REPLACEMENT_POINT_INLINE_BODY(cd, iptr)
56 #define REPLACEMENT_POINT_RETURN(cd, iptr)
57 #define REPLACEMENT_POINT_INVOKE(cd, iptr)
58 #define REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr)
59 #define REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr)
60 #define REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr)
61 
62 #else /* defined(ENABLE_REPLACEMENT) */
63 
64 /*** structs *********************************************************/
65 
66 #define RPLALLOC_STACK -1
67 #define RPLALLOC_PARAM -2
68 #define RPLALLOC_SYNC -3
69 
70 /* `rplalloc` is a compact struct for register allocation info */
71 
72 /* XXX optimize this for space efficiency */
73 struct rplalloc {
74  s4 index; /* local index, -1 for stack slot */
75  s4 regoff; /* register index / stack slot offset */
76  unsigned int flags:4; /* OR of (INMEMORY,...) */
77  unsigned int type:4; /* TYPE_... constant */
78 };
79 
80 #if INMEMORY > 0x08
81 #error value of INMEMORY is too big to fit in rplalloc.flags
82 #endif
83 
84 #if !defined(NDEBUG)
85 #define RPLPOINT_CHECK(type) , rplpoint::TYPE_##type
86 #define RPLPOINT_CHECK_BB(bptr) , (bptr)->type
87 #else
88 #define RPLPOINT_CHECK(type)
89 #define RPLPOINT_CHECK_BB(bptr)
90 #endif
91 
92 /* An `rplpoint` represents a replacement point in a compiled method */
93 
94 struct rplpoint {
95  /**
96  * CAUTION: Do not change the numerical values. These are used as
97  * indices into replace_normalize_type_map.
98  * XXX what to do about overlapping rplpoints?
99  */
100  enum Type {
101  TYPE_STD = basicblock::TYPE_STD,
102  TYPE_EXH = basicblock::TYPE_EXH,
103  TYPE_SBR = basicblock::TYPE_SBR,
104  TYPE_CALL = 3,
105  TYPE_INLINE = 4,
106  TYPE_RETURN = 5,
107  TYPE_BODY = 6
108  };
109 
110  enum Flag {
111  FLAG_NOTRAP = 0x01, // rplpoint cannot be trapped
112  FLAG_COUNTDOWN = 0x02, // count down hits
113  FLAG_ACTIVE = 0x08 // trap is active
114  };
115 
116  u1 *pc; /* machine code PC of this point */
117  methodinfo *method; /* source method this point is in */
118  rplpoint *parent; /* rplpoint of the inlined body */ /* XXX unify with code */
119  rplalloc *regalloc; /* pointer to register index table */
120  s4 id; /* id of the rplpoint within method */
121  s4 callsize; /* size of call code in bytes */
122  unsigned int regalloccount:20; /* number of local allocations */
123  Type type:4; /* type of replacement point */
124  unsigned int flags:8; /* OR of Flag constants */
125 };
126 
127 
128 union replace_val_t {
129  s4 i;
130  s8 l;
131  ptrint p;
132  struct {
133  u4 lo;
134  u4 hi;
135  } words;
136  float f;
137  double d;
138  java_object_t *a;
139 };
140 
141 
142 struct sourceframe_t {
143  sourceframe_t *down; /* source frame down the call chain */
144 
145  methodinfo *method; /* method this frame is in */
146  s4 id;
147  s4 type;
148 
149  /* values */
150  replace_val_t instance;
151 
152  replace_val_t *javastack; /* values of stack vars */
153  u1 *javastacktype; /* types of stack vars */
154  s4 javastackdepth; /* number of stack vars */
155 
156  replace_val_t *javalocals; /* values of javalocals */
157  u1 *javalocaltype; /* types of javalocals */
158  s4 javalocalcount; /* number of javalocals */
159 
160  replace_val_t *syncslots;
161  s4 syncslotcount; /* XXX do we need more than one? */
162 
163  /* mapping info */
164  rplpoint *fromrp; /* rplpoint used to read this frame */
165  codeinfo *fromcode; /* code this frame was using */
166  rplpoint *torp; /* rplpoint this frame was mapped to */
167  codeinfo *tocode; /* code this frame was mapped to */
168 
169  /* info for native frames */
170  stackframeinfo_t *sfi; /* sfi for native frames, otherwise NULL */
171  s4 nativeframesize; /* size (bytes) of native frame */
172  u1 *nativepc;
173  ptrint nativesavint[INT_SAV_CNT]; /* XXX temporary */
174  double nativesavflt[FLT_REG_CNT]; /* XXX temporary */
175 };
176 
177 #define REPLACE_IS_NATIVE_FRAME(frame) ((frame)->sfi != NULL)
178 #define REPLACE_IS_JAVA_FRAME(frame) ((frame)->sfi == NULL)
179 
180 
181 struct sourcestate_t {
182  sourceframe_t *frames; /* list of source frames, from bottom up */
183 };
184 
185 
186 /*** macros for the codegens *******************************************/
187 
188 #define REPLACEMENT_POINTS_INIT(cd, jd) \
189  if (!replace_create_replacement_points(jd)) \
190  return false; \
191  (cd)->replacementpoint = (jd)->code->rplpoints;
192 
193 #define REPLACEMENT_POINTS_RESET(cd, jd) \
194  (cd)->replacementpoint = (jd)->code->rplpoints;
195 
196 #define REPLACEMENT_POINT_BLOCK_START(cd, bptr) \
197  if ((bptr)->bitflags & BBFLAG_REPLACEMENT) \
198  codegen_set_replacement_point((cd) RPLPOINT_CHECK_BB(bptr));
199 
200 #define REPLACEMENT_POINT_INLINE_START(cd, iptr) \
201  codegen_set_replacement_point(cd RPLPOINT_CHECK(INLINE));
202 
203 #define REPLACEMENT_POINT_INLINE_BODY(cd, iptr) \
204  codegen_set_replacement_point_notrap(cd RPLPOINT_CHECK(BODY));
205 
206 #define REPLACEMENT_POINT_RETURN(cd, iptr) \
207  codegen_set_replacement_point(cd RPLPOINT_CHECK(RETURN));
208 
209 #define REPLACEMENT_POINT_INVOKE(cd, iptr) \
210  codegen_set_replacement_point(cd RPLPOINT_CHECK(CALL));
211 
212 #define REPLACEMENT_POINT_INVOKE_RETURN(cd, iptr) \
213  if (iptr->opc != ICMD_BUILTIN) \
214  cd->replacementpoint[-1].callsize = (cd->mcodeptr - cd->mcodebase)\
215  - (ptrint) cd->replacementpoint[-1].pc;
216 
217 
218 /*** macros for the codegens (for GC) **********************************/
219 
220 #if defined(ENABLE_GC_CACAO)
221 
222 #define REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr) \
223  codegen_set_replacement_point(cd RPLPOINT_CHECK(CALL));
224 
225 #define REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr) \
226  if (iptr->opc == ICMD_BUILTIN) \
227  cd->replacementpoint[-1].callsize = (cd->mcodeptr - cd->mcodebase)\
228  - (ptrint) cd->replacementpoint[-1].pc;
229 
230 #else // ENABLE_GC_CACAO
231 
232 #define REPLACEMENT_POINT_FORGC_BUILTIN(cd, iptr)
233 #define REPLACEMENT_POINT_FORGC_BUILTIN_RETURN(cd, iptr)
234 
235 #endif // ENABLE_GC_CACAO
236 
237 
238 /*** prototypes ********************************************************/
239 
242 
243 void replace_activate_replacement_points(codeinfo *code, bool mappable);
245 
247 
248 #if !defined(NDEBUG)
250 void replace_replacement_point_println(rplpoint *rp, int depth);
251 void replace_sourcestate_println(sourcestate_t *ss);
252 void replace_sourcestate_println_short(sourcestate_t *ss);
253 void replace_source_frame_println(sourceframe_t *frame);
254 #endif
255 
256 /* machine dependent functions (code in ARCH_DIR/md.c) */
257 
258 #if defined(ENABLE_JIT)
259 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert);
260 #endif
261 
262 #endif // ENABLE_REPLACEMENT
263 
264 #endif // REPLACE_HPP_
265 
266 
267 /*
268  * These are local overrides for various environment variables in Emacs.
269  * Please do not remove this and leave it at the end of the file, where
270  * Emacs will automagically detect them.
271  * ---------------------------------------------------------------------
272  * Local variables:
273  * mode: c++
274  * indent-tabs-mode: t
275  * c-basic-offset: 4
276  * tab-width: 4
277  * End:
278  * vim:noexpandtab:sw=4:ts=4:
279  */
std::size_t index
void replace_replacement_point_println(rplpoint *rp, int depth)
Definition: replace.cpp:2980
void replace_show_replacement_points(codeinfo *code)
Definition: replace.cpp:3047
void replace_free_replacement_points(codeinfo *code)
Definition: replace.cpp:722
Definition: jit.hpp:126
void replace_activate_replacement_points(codeinfo *code, bool mappable)
Definition: replace.cpp:759
void replace_deactivate_replacement_points(codeinfo *code)
Definition: replace.cpp:840
bool replace_create_replacement_points(jitdata *jd)
Definition: replace.cpp:350
void replace_sourcestate_println_short(sourcestate_t *ss)
Definition: replace.cpp:3267
PassInfo::IDTy id
uint8_t u1
Definition: types.hpp:40
int64_t s8
Definition: types.hpp:48
void replace_sourcestate_println(sourcestate_t *ss)
Definition: replace.cpp:3237
#define INT_SAV_CNT
Definition: md-abi.hpp:73
jlong jlong jlong jlong jint depth
Definition: jvmti.h:497
JNIEnv jthread jmethodID method
Definition: jvmti.h:207
void replace_source_frame_println(sourceframe_t *frame)
Definition: replace.cpp:3125
Type
Types used internally by JITTED code.
Definition: global.hpp:117
MIIterator i
int32_t s4
Definition: types.hpp:45
bool regalloc(jitdata *jd)
Definition: simplereg.cpp:262
uint32_t u4
Definition: types.hpp:46
#define pc
Definition: md-asm.hpp:56
bool replace_handler(u1 *pc, executionstate_t *es)
Definition: replace.cpp:2758
uintptr_t ptrint
Definition: types.hpp:54
LoopTreeGraph * parent
#define FLT_REG_CNT
Definition: md-abi.hpp:79