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 methodinfo;
38 struct rplalloc;
39 struct rplpoint;
40 struct sourceframe_t;
41 struct sourcestate_t;
42 union replace_val_t;
43 
44 #if defined(ENABLE_REPLACEMENT)
45 
46 /*** structs *********************************************************/
47 
48 #define RPLALLOC_STACK -1 // allocation info for a stack variable
49 #define RPLALLOC_PARAM -2 // allocation info for a call parameter
50 #define RPLALLOC_SYNC -3 // allocation info for a synchronization slot
51 
52 /* `rplalloc` is a compact struct for register allocation info */
53 
54 /* XXX optimize this for space efficiency */
55 struct rplalloc {
56  s4 index; /* local index, -1 for stack slot */
57  s4 regoff; /* register index / stack slot offset */
58  bool inmemory; /* indicates whether value is stored in memory */
59  unsigned int type:4; /* TYPE_... constant */
60 };
61 
62 #if INMEMORY > 0x08
63 #error value of INMEMORY is too big to fit in rplalloc.flags
64 #endif
65 
66 #if !defined(NDEBUG)
67 #define RPLPOINT_CHECK(type) , rplpoint::TYPE_##type
68 #define RPLPOINT_CHECK_BB(bptr) , (bptr)->type
69 #else
70 #define RPLPOINT_CHECK(type)
71 #define RPLPOINT_CHECK_BB(bptr)
72 #endif
73 
74 /**
75  * A point in a compiled method where it is possible to recover the source
76  * state to perform on-stack replacement.
77  */
78 struct rplpoint {
79  /**
80  * CAUTION: Do not change the numerical values. These are used as
81  * indices into replace_normalize_type_map.
82  * XXX what to do about overlapping rplpoints?
83  */
84  enum Type {
85  TYPE_STD = basicblock::TYPE_STD,
86  TYPE_EXH = basicblock::TYPE_EXH,
87  TYPE_SBR = basicblock::TYPE_SBR,
88  TYPE_CALL = 3,
89  TYPE_INLINE = 4,
90  TYPE_RETURN = 5,
91  TYPE_BODY = 6
92  };
93 
94  enum Flag {
95  FLAG_TRAPPABLE = 0x01, // rplpoint can be trapped
96  FLAG_COUNTDOWN = 0x02, // count down hits
97  FLAG_DEOPTIMIZE = 0x04, // indicates a deoptimization point
98  FLAG_ACTIVE = 0x08 // trap is active
99  };
100 
101  u1 *pc; /* machine code PC of this point */
102  methodinfo *method; /* source method this point is in */
103  rplpoint *parent; /* rplpoint of the inlined body */ /* XXX unify with code */
104  rplalloc *regalloc; /* pointer to register index table */
105  s4 id; /* id of the rplpoint within method */
106  s4 callsize; /* size of call code in bytes */
107  unsigned int regalloccount:20; /* number of local allocations */
108  Type type:4; /* type of replacement point */
109  unsigned int flags:8; /* OR of Flag constants */
110  u1 *patch_target_addr; /* target address to patch for invoke Static/Special */
111 };
112 
113 /**
114  * Represents a value of a javalocal or stack variable that has been
115  * recovered from the execution state.
116  */
117 union replace_val_t {
118  s4 i;
119  s8 l;
120  ptrint p;
121  struct {
122  u4 lo;
123  u4 hi;
124  } words;
125  float f;
126  double d;
127  java_object_t *a;
128 };
129 
130 /**
131  * A machine-independent representation of a method's execution state.
132  */
133 struct sourceframe_t {
134  sourceframe_t *down; /* source frame down the call chain */
135 
136  methodinfo *method; /* method this frame is in */
137  s4 id;
138  s4 type;
139 
140  /* values */
141  replace_val_t instance;
142 
143  replace_val_t *javastack; /* values of stack vars */
144  u1 *javastacktype; /* types of stack vars */
145  s4 javastackdepth; /* number of stack vars */
146 
147  replace_val_t *javalocals; /* values of javalocals */
148  u1 *javalocaltype; /* types of javalocals */
149  s4 javalocalcount; /* number of javalocals */
150 
151  replace_val_t *syncslots;
152  s4 syncslotcount; /* XXX do we need more than one? */
153 
154  /* mapping info */
155  rplpoint *fromrp; /* rplpoint used to read this frame */
156  codeinfo *fromcode; /* code this frame was using */
157  rplpoint *torp; /* rplpoint this frame was mapped to */
158  codeinfo *tocode; /* code this frame was mapped to */
159 
160  /* info for native frames */
161  stackframeinfo_t *sfi; /* sfi for native frames, otherwise NULL */
162  s4 nativeframesize; /* size (bytes) of native frame */
163  u1 *nativepc;
164  ptrint nativesavint[INT_SAV_CNT]; /* XXX temporary */
165  double nativesavflt[FLT_REG_CNT]; /* XXX temporary */
166 };
167 
168 struct sourcestate_t {
169  sourceframe_t *frames; /* list of source frames, from bottom up */
170 };
171 
172 
173 /*** prototypes ********************************************************/
174 
176 
177 void replace_activate_replacement_points(codeinfo *code, bool mappable);
179 
183 
184 /* machine dependent functions (code in ARCH_DIR/md.c) */
185 
186 #if defined(ENABLE_JIT)
187 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert);
188 #endif
189 
190 namespace cacao {
191 
192 class OStream;
193 
194 OStream& operator<<(OStream &OS, const rplpoint *rp);
195 OStream& operator<<(OStream &OS, const rplpoint &rp);
196 
197 OStream& operator<<(OStream &OS, const rplalloc *ra);
198 OStream& operator<<(OStream &OS, const rplalloc &ra);
199 
200 OStream& operator<<(OStream &OS, const sourceframe_t *frame);
201 OStream& operator<<(OStream &OS, const sourceframe_t &frame);
202 
203 OStream& operator<<(OStream &OS, const sourcestate_t *ss);
204 OStream& operator<<(OStream &OS, const sourcestate_t &ss);
205 
206 } // end namespace cacao
207 
208 #endif // ENABLE_REPLACEMENT
209 
210 #endif // REPLACE_HPP_
211 
212 
213 /*
214  * These are local overrides for various environment variables in Emacs.
215  * Please do not remove this and leave it at the end of the file, where
216  * Emacs will automagically detect them.
217  * ---------------------------------------------------------------------
218  * Local variables:
219  * mode: c++
220  * indent-tabs-mode: t
221  * c-basic-offset: 4
222  * tab-width: 4
223  * End:
224  * vim:noexpandtab:sw=4:ts=4:
225  */
std::size_t index
void replace_free_replacement_points(codeinfo *code)
Definition: replace.cpp:100
#define ra
Definition: md-asm.hpp:62
void replace_activate_replacement_points(codeinfo *code, bool mappable)
Definition: replace.cpp:132
void replace_handle_deoptimization_trap(u1 *pc, executionstate_t *es)
Definition: replace.cpp:1974
void replace_deactivate_replacement_points(codeinfo *code)
Definition: replace.cpp:202
PassInfo::IDTy id
uint8_t u1
Definition: types.hpp:40
int64_t s8
Definition: types.hpp:48
#define INT_SAV_CNT
Definition: md-abi.hpp:75
JNIEnv jthread jmethodID method
Definition: jvmti.h:207
Type
Types used internally by JITTED code.
Definition: global.hpp:117
MIIterator i
int32_t s4
Definition: types.hpp:45
OStream & OS
OStream & operator<<(OStream &OS, const std::string &t)
Definition: OStream.hpp:459
bool regalloc(jitdata *jd)
Definition: simplereg.cpp:262
uint32_t u4
Definition: types.hpp:46
bool replace_handle_replacement_trap(u1 *pc, executionstate_t *es)
Definition: replace.cpp:1931
#define pc
Definition: md-asm.hpp:56
void replace_handle_countdown_trap(u1 *pc, executionstate_t *es)
Definition: replace.cpp:1887
uintptr_t ptrint
Definition: types.hpp:54
LoopTreeGraph * parent
#define FLT_REG_CNT
Definition: md-abi.hpp:81