Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp
40949 views
1
/*
2
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include "precompiled.hpp"
25
#include "classfile/javaClasses.inline.hpp"
26
#include "code/compiledIC.hpp"
27
#include "compiler/compileBroker.hpp"
28
#include "compiler/compilerThread.hpp"
29
#include "compiler/oopMap.hpp"
30
#include "jvmci/jvmciCodeInstaller.hpp"
31
#include "jvmci/jvmciCompilerToVM.hpp"
32
#include "jvmci/jvmciRuntime.hpp"
33
#include "memory/universe.hpp"
34
#include "oops/compressedOops.inline.hpp"
35
#include "oops/klass.inline.hpp"
36
#include "prims/jvmtiExport.hpp"
37
#include "prims/methodHandles.hpp"
38
#include "runtime/interfaceSupport.inline.hpp"
39
#include "runtime/jniHandles.inline.hpp"
40
#include "runtime/sharedRuntime.hpp"
41
#include "utilities/align.hpp"
42
43
// frequently used constants
44
// Allocate them with new so they are never destroyed (otherwise, a
45
// forced exit could destroy these objects while they are still in
46
// use).
47
ConstantOopWriteValue* CodeInstaller::_oop_null_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantOopWriteValue(NULL);
48
ConstantIntValue* CodeInstaller::_int_m1_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(-1);
49
ConstantIntValue* CodeInstaller::_int_0_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue((jint)0);
50
ConstantIntValue* CodeInstaller::_int_1_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(1);
51
ConstantIntValue* CodeInstaller::_int_2_scope_value = new (ResourceObj::C_HEAP, mtJVMCI) ConstantIntValue(2);
52
LocationValue* CodeInstaller::_illegal_value = new (ResourceObj::C_HEAP, mtJVMCI) LocationValue(Location());
53
MarkerValue* CodeInstaller::_virtual_byte_array_marker = new (ResourceObj::C_HEAP, mtJVMCI) MarkerValue();
54
55
VMReg CodeInstaller::getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS) {
56
if (location.is_null()) {
57
JVMCI_THROW_NULL(NullPointerException);
58
}
59
60
JVMCIObject reg = jvmci_env()->get_code_Location_reg(location);
61
jint offset = jvmci_env()->get_code_Location_offset(location);
62
63
if (reg.is_non_null()) {
64
// register
65
jint number = jvmci_env()->get_code_Register_number(reg);
66
VMReg vmReg = CodeInstaller::get_hotspot_reg(number, JVMCI_CHECK_NULL);
67
if (offset % 4 == 0) {
68
return vmReg->next(offset / 4);
69
} else {
70
JVMCI_ERROR_NULL("unaligned subregister offset %d in oop map", offset);
71
}
72
} else {
73
// stack slot
74
if (offset % 4 == 0) {
75
VMReg vmReg = VMRegImpl::stack2reg(offset / 4);
76
if (!OopMapValue::legal_vm_reg_name(vmReg)) {
77
// This restriction only applies to VMRegs that are used in OopMap but
78
// since that's the only use of VMRegs it's simplest to put this test
79
// here. This test should also be equivalent legal_vm_reg_name but JVMCI
80
// clients can use max_oop_map_stack_stack_offset to detect this problem
81
// directly. The asserts just ensure that the tests are in agreement.
82
assert(offset > CompilerToVM::Data::max_oop_map_stack_offset(), "illegal VMReg");
83
JVMCI_ERROR_NULL("stack offset %d is too large to be encoded in OopMap (max %d)",
84
offset, CompilerToVM::Data::max_oop_map_stack_offset());
85
}
86
assert(OopMapValue::legal_vm_reg_name(vmReg), "illegal VMReg");
87
return vmReg;
88
} else {
89
JVMCI_ERROR_NULL("unaligned stack offset %d in oop map", offset);
90
}
91
}
92
}
93
94
// creates a HotSpot oop map out of the byte arrays provided by DebugInfo
95
OopMap* CodeInstaller::create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS) {
96
JVMCIObject reference_map = jvmci_env()->get_DebugInfo_referenceMap(debug_info);
97
if (reference_map.is_null()) {
98
JVMCI_THROW_NULL(NullPointerException);
99
}
100
if (!jvmci_env()->isa_HotSpotReferenceMap(reference_map)) {
101
JVMCI_ERROR_NULL("unknown reference map: %s", jvmci_env()->klass_name(reference_map));
102
}
103
if (!_has_wide_vector && SharedRuntime::is_wide_vector(jvmci_env()->get_HotSpotReferenceMap_maxRegisterSize(reference_map))) {
104
if (SharedRuntime::polling_page_vectors_safepoint_handler_blob() == NULL) {
105
JVMCI_ERROR_NULL("JVMCI is producing code using vectors larger than the runtime supports");
106
}
107
_has_wide_vector = true;
108
}
109
OopMap* map = new OopMap(_total_frame_size, _parameter_count);
110
JVMCIObjectArray objects = jvmci_env()->get_HotSpotReferenceMap_objects(reference_map);
111
JVMCIObjectArray derivedBase = jvmci_env()->get_HotSpotReferenceMap_derivedBase(reference_map);
112
JVMCIPrimitiveArray sizeInBytes = jvmci_env()->get_HotSpotReferenceMap_sizeInBytes(reference_map);
113
if (objects.is_null() || derivedBase.is_null() || sizeInBytes.is_null()) {
114
JVMCI_THROW_NULL(NullPointerException);
115
}
116
if (JVMCIENV->get_length(objects) != JVMCIENV->get_length(derivedBase) || JVMCIENV->get_length(objects) != JVMCIENV->get_length(sizeInBytes)) {
117
JVMCI_ERROR_NULL("arrays in reference map have different sizes: %d %d %d", JVMCIENV->get_length(objects), JVMCIENV->get_length(derivedBase), JVMCIENV->get_length(sizeInBytes));
118
}
119
for (int i = 0; i < JVMCIENV->get_length(objects); i++) {
120
JVMCIObject location = JVMCIENV->get_object_at(objects, i);
121
JVMCIObject baseLocation = JVMCIENV->get_object_at(derivedBase, i);
122
jint bytes = JVMCIENV->get_int_at(sizeInBytes, i);
123
124
VMReg vmReg = getVMRegFromLocation(location, _total_frame_size, JVMCI_CHECK_NULL);
125
if (baseLocation.is_non_null()) {
126
// derived oop
127
#ifdef _LP64
128
if (bytes == 8) {
129
#else
130
if (bytes == 4) {
131
#endif
132
VMReg baseReg = getVMRegFromLocation(baseLocation, _total_frame_size, JVMCI_CHECK_NULL);
133
map->set_derived_oop(vmReg, baseReg);
134
} else {
135
JVMCI_ERROR_NULL("invalid derived oop size in ReferenceMap: %d", bytes);
136
}
137
#ifdef _LP64
138
} else if (bytes == 8) {
139
// wide oop
140
map->set_oop(vmReg);
141
} else if (bytes == 4) {
142
// narrow oop
143
map->set_narrowoop(vmReg);
144
#else
145
} else if (bytes == 4) {
146
map->set_oop(vmReg);
147
#endif
148
} else {
149
JVMCI_ERROR_NULL("invalid oop size in ReferenceMap: %d", bytes);
150
}
151
}
152
153
JVMCIObject callee_save_info = jvmci_env()->get_DebugInfo_calleeSaveInfo(debug_info);
154
if (callee_save_info.is_non_null()) {
155
JVMCIObjectArray registers = jvmci_env()->get_RegisterSaveLayout_registers(callee_save_info);
156
JVMCIPrimitiveArray slots = jvmci_env()->get_RegisterSaveLayout_slots(callee_save_info);
157
for (jint i = 0; i < JVMCIENV->get_length(slots); i++) {
158
JVMCIObject jvmci_reg = JVMCIENV->get_object_at(registers, i);
159
jint jvmci_reg_number = jvmci_env()->get_code_Register_number(jvmci_reg);
160
VMReg hotspot_reg = CodeInstaller::get_hotspot_reg(jvmci_reg_number, JVMCI_CHECK_NULL);
161
// HotSpot stack slots are 4 bytes
162
jint jvmci_slot = JVMCIENV->get_int_at(slots, i);
163
jint hotspot_slot = jvmci_slot * VMRegImpl::slots_per_word;
164
VMReg hotspot_slot_as_reg = VMRegImpl::stack2reg(hotspot_slot);
165
map->set_callee_saved(hotspot_slot_as_reg, hotspot_reg);
166
#ifdef _LP64
167
// (copied from generate_oop_map() in c1_Runtime1_x86.cpp)
168
VMReg hotspot_slot_hi_as_reg = VMRegImpl::stack2reg(hotspot_slot + 1);
169
map->set_callee_saved(hotspot_slot_hi_as_reg, hotspot_reg->next());
170
#endif
171
}
172
}
173
return map;
174
}
175
176
void* CodeInstaller::record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {
177
/*
178
* This method needs to return a raw (untyped) pointer, since the value of a pointer to the base
179
* class is in general not equal to the pointer of the subclass. When patching metaspace pointers,
180
* the compiler expects a direct pointer to the subclass (Klass* or Method*), not a pointer to the
181
* base class (Metadata* or MetaspaceObj*).
182
*/
183
JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);
184
if (jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {
185
Klass* klass = JVMCIENV->asKlass(obj);
186
assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed klass pointer %s @ " INTPTR_FORMAT, klass->name()->as_C_string(), p2i(klass));
187
int index = _oop_recorder->find_index(klass);
188
section->relocate(dest, metadata_Relocation::spec(index));
189
JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
190
return klass;
191
} else if (jvmci_env()->isa_HotSpotResolvedJavaMethodImpl(obj)) {
192
Method* method = jvmci_env()->asMethod(obj);
193
assert(!jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected compressed method pointer %s @ " INTPTR_FORMAT, method->name()->as_C_string(), p2i(method));
194
int index = _oop_recorder->find_index(method);
195
section->relocate(dest, metadata_Relocation::spec(index));
196
JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), method->name()->as_C_string());
197
return method;
198
} else {
199
JVMCI_ERROR_NULL("unexpected metadata reference for constant of type %s", jvmci_env()->klass_name(obj));
200
}
201
}
202
203
#ifdef _LP64
204
narrowKlass CodeInstaller::record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS) {
205
JVMCIObject obj = jvmci_env()->get_HotSpotMetaspaceConstantImpl_metaspaceObject(constant);
206
assert(jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant), "unexpected uncompressed pointer");
207
208
if (!jvmci_env()->isa_HotSpotResolvedObjectTypeImpl(obj)) {
209
JVMCI_ERROR_0("unexpected compressed pointer of type %s", jvmci_env()->klass_name(obj));
210
}
211
212
Klass* klass = JVMCIENV->asKlass(obj);
213
int index = _oop_recorder->find_index(klass);
214
section->relocate(dest, metadata_Relocation::spec(index));
215
JVMCI_event_3("narrowKlass[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
216
return CompressedKlassPointers::encode(klass);
217
}
218
#endif
219
220
Location::Type CodeInstaller::get_oop_type(JVMCIObject value) {
221
JVMCIObject valueKind = jvmci_env()->get_Value_valueKind(value);
222
JVMCIObject platformKind = jvmci_env()->get_ValueKind_platformKind(valueKind);
223
224
if (jvmci_env()->equals(platformKind, word_kind())) {
225
return Location::oop;
226
} else {
227
return Location::narrowoop;
228
}
229
}
230
231
ScopeValue* CodeInstaller::get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS) {
232
second = NULL;
233
if (value.is_null()) {
234
JVMCI_THROW_NULL(NullPointerException);
235
} else if (JVMCIENV->equals(value, jvmci_env()->get_Value_ILLEGAL())) {
236
if (type != T_ILLEGAL) {
237
JVMCI_ERROR_NULL("unexpected illegal value, expected %s", basictype_to_str(type));
238
}
239
return _illegal_value;
240
} else if (jvmci_env()->isa_RegisterValue(value)) {
241
JVMCIObject reg = jvmci_env()->get_RegisterValue_reg(value);
242
jint number = jvmci_env()->get_code_Register_number(reg);
243
VMReg hotspotRegister = get_hotspot_reg(number, JVMCI_CHECK_NULL);
244
if (is_general_purpose_reg(hotspotRegister)) {
245
Location::Type locationType;
246
if (type == T_OBJECT) {
247
locationType = get_oop_type(value);
248
} else if (type == T_LONG) {
249
locationType = Location::lng;
250
} else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
251
locationType = Location::int_in_long;
252
} else {
253
JVMCI_ERROR_NULL("unexpected type %s in cpu register", basictype_to_str(type));
254
}
255
ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
256
if (type == T_LONG) {
257
second = value;
258
}
259
return value;
260
} else {
261
Location::Type locationType;
262
if (type == T_FLOAT) {
263
// this seems weird, but the same value is used in c1_LinearScan
264
locationType = Location::normal;
265
} else if (type == T_DOUBLE) {
266
locationType = Location::dbl;
267
} else {
268
JVMCI_ERROR_NULL("unexpected type %s in floating point register", basictype_to_str(type));
269
}
270
ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
271
if (type == T_DOUBLE) {
272
second = value;
273
}
274
return value;
275
}
276
} else if (jvmci_env()->isa_StackSlot(value)) {
277
jint offset = jvmci_env()->get_StackSlot_offset(value);
278
if (jvmci_env()->get_StackSlot_addFrameSize(value)) {
279
offset += _total_frame_size;
280
}
281
282
Location::Type locationType;
283
if (type == T_OBJECT) {
284
locationType = get_oop_type(value);
285
} else if (type == T_LONG) {
286
locationType = Location::lng;
287
} else if (type == T_DOUBLE) {
288
locationType = Location::dbl;
289
} else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
290
locationType = Location::normal;
291
} else {
292
JVMCI_ERROR_NULL("unexpected type %s in stack slot", basictype_to_str(type));
293
}
294
ScopeValue* value = new LocationValue(Location::new_stk_loc(locationType, offset));
295
if (type == T_DOUBLE || type == T_LONG) {
296
second = value;
297
}
298
return value;
299
} else if (jvmci_env()->isa_JavaConstant(value)) {
300
if (jvmci_env()->isa_PrimitiveConstant(value)) {
301
if (jvmci_env()->isa_RawConstant(value)) {
302
jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);
303
return new ConstantLongValue(prim);
304
} else {
305
BasicType constantType = jvmci_env()->kindToBasicType(jvmci_env()->get_PrimitiveConstant_kind(value), JVMCI_CHECK_NULL);
306
if (type != constantType) {
307
JVMCI_ERROR_NULL("primitive constant type doesn't match, expected %s but got %s", basictype_to_str(type), basictype_to_str(constantType));
308
}
309
if (type == T_INT || type == T_FLOAT) {
310
jint prim = (jint)jvmci_env()->get_PrimitiveConstant_primitive(value);
311
switch (prim) {
312
case -1: return _int_m1_scope_value;
313
case 0: return _int_0_scope_value;
314
case 1: return _int_1_scope_value;
315
case 2: return _int_2_scope_value;
316
default: return new ConstantIntValue(prim);
317
}
318
} else if (type == T_LONG || type == T_DOUBLE) {
319
jlong prim = jvmci_env()->get_PrimitiveConstant_primitive(value);
320
second = _int_1_scope_value;
321
return new ConstantLongValue(prim);
322
} else {
323
JVMCI_ERROR_NULL("unexpected primitive constant type %s", basictype_to_str(type));
324
}
325
}
326
} else if (jvmci_env()->isa_NullConstant(value) || jvmci_env()->isa_HotSpotCompressedNullConstant(value)) {
327
if (type == T_OBJECT) {
328
return _oop_null_scope_value;
329
} else {
330
JVMCI_ERROR_NULL("unexpected null constant, expected %s", basictype_to_str(type));
331
}
332
} else if (jvmci_env()->isa_HotSpotObjectConstantImpl(value)) {
333
if (type == T_OBJECT) {
334
Handle obj = jvmci_env()->asConstant(value, JVMCI_CHECK_NULL);
335
if (obj == NULL) {
336
JVMCI_ERROR_NULL("null value must be in NullConstant");
337
}
338
return new ConstantOopWriteValue(JNIHandles::make_local(obj()));
339
} else {
340
JVMCI_ERROR_NULL("unexpected object constant, expected %s", basictype_to_str(type));
341
}
342
}
343
} else if (jvmci_env()->isa_VirtualObject(value)) {
344
if (type == T_OBJECT) {
345
int id = jvmci_env()->get_VirtualObject_id(value);
346
if (0 <= id && id < objects->length()) {
347
ScopeValue* object = objects->at(id);
348
if (object != NULL) {
349
return object;
350
}
351
}
352
JVMCI_ERROR_NULL("unknown virtual object id %d", id);
353
} else {
354
JVMCI_ERROR_NULL("unexpected virtual object, expected %s", basictype_to_str(type));
355
}
356
}
357
358
JVMCI_ERROR_NULL("unexpected value in scope: %s", jvmci_env()->klass_name(value))
359
}
360
361
void CodeInstaller::record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {
362
JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);
363
int id = jvmci_env()->get_VirtualObject_id(value);
364
Klass* klass = JVMCIENV->asKlass(type);
365
bool isLongArray = klass == Universe::longArrayKlassObj();
366
bool isByteArray = klass == Universe::byteArrayKlassObj();
367
368
JVMCIObjectArray values = jvmci_env()->get_VirtualObject_values(value);
369
JVMCIObjectArray slotKinds = jvmci_env()->get_VirtualObject_slotKinds(value);
370
for (jint i = 0; i < JVMCIENV->get_length(values); i++) {
371
ScopeValue* cur_second = NULL;
372
JVMCIObject object = JVMCIENV->get_object_at(values, i);
373
BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
374
ScopeValue* value;
375
if (JVMCIENV->equals(object, jvmci_env()->get_Value_ILLEGAL())) {
376
if (isByteArray && type == T_ILLEGAL) {
377
/*
378
* The difference between a virtualized large access and a deferred write is the kind stored in the slotKinds
379
* of the virtual object: in the virtualization case, the kind is illegal, in the deferred write case, the kind
380
* is access stack kind (an int).
381
*/
382
value = _virtual_byte_array_marker;
383
} else {
384
value = _illegal_value;
385
if (type == T_DOUBLE || type == T_LONG) {
386
cur_second = _illegal_value;
387
}
388
}
389
} else {
390
value = get_scope_value(object, type, objects, cur_second, JVMCI_CHECK);
391
}
392
393
if (isLongArray && cur_second == NULL) {
394
// we're trying to put ints into a long array... this isn't really valid, but it's used for some optimizations.
395
// add an int 0 constant
396
cur_second = _int_0_scope_value;
397
}
398
399
if (isByteArray && cur_second != NULL && (type == T_DOUBLE || type == T_LONG)) {
400
// we are trying to write a long in a byte Array. We will need to count the illegals to restore the type of
401
// the thing we put inside.
402
cur_second = NULL;
403
}
404
405
if (cur_second != NULL) {
406
sv->field_values()->append(cur_second);
407
}
408
assert(value != NULL, "missing value");
409
sv->field_values()->append(value);
410
}
411
}
412
413
MonitorValue* CodeInstaller::get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS) {
414
if (value.is_null()) {
415
JVMCI_THROW_NULL(NullPointerException);
416
}
417
if (!jvmci_env()->isa_StackLockValue(value)) {
418
JVMCI_ERROR_NULL("Monitors must be of type StackLockValue, got %s", jvmci_env()->klass_name(value));
419
}
420
421
ScopeValue* second = NULL;
422
ScopeValue* owner_value = get_scope_value(jvmci_env()->get_StackLockValue_owner(value), T_OBJECT, objects, second, JVMCI_CHECK_NULL);
423
assert(second == NULL, "monitor cannot occupy two stack slots");
424
425
ScopeValue* lock_data_value = get_scope_value(jvmci_env()->get_StackLockValue_slot(value), T_LONG, objects, second, JVMCI_CHECK_NULL);
426
assert(second == lock_data_value, "monitor is LONG value that occupies two stack slots");
427
assert(lock_data_value->is_location(), "invalid monitor location");
428
Location lock_data_loc = ((LocationValue*)lock_data_value)->location();
429
430
bool eliminated = false;
431
if (jvmci_env()->get_StackLockValue_eliminated(value)) {
432
eliminated = true;
433
}
434
435
return new MonitorValue(owner_value, lock_data_loc, eliminated);
436
}
437
438
void CodeInstaller::initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS) {
439
JavaThread* thread = JavaThread::current();
440
CompilerThread* compilerThread = thread->is_Compiler_thread() ? CompilerThread::cast(thread) : NULL;
441
_oop_recorder = oop_recorder;
442
_dependencies = new Dependencies(&_arena, _oop_recorder, compilerThread != NULL ? compilerThread->log() : NULL);
443
JVMCIObjectArray assumptions = jvmci_env()->get_HotSpotCompiledCode_assumptions(compiled_code);
444
if (assumptions.is_non_null()) {
445
int length = JVMCIENV->get_length(assumptions);
446
for (int i = 0; i < length; ++i) {
447
JVMCIObject assumption = JVMCIENV->get_object_at(assumptions, i);
448
if (assumption.is_non_null()) {
449
if (jvmci_env()->isa_Assumptions_NoFinalizableSubclass(assumption)) {
450
assumption_NoFinalizableSubclass(assumption);
451
} else if (jvmci_env()->isa_Assumptions_ConcreteSubtype(assumption)) {
452
assumption_ConcreteSubtype(assumption);
453
} else if (jvmci_env()->isa_Assumptions_LeafType(assumption)) {
454
assumption_LeafType(assumption);
455
} else if (jvmci_env()->isa_Assumptions_ConcreteMethod(assumption)) {
456
assumption_ConcreteMethod(assumption);
457
} else if (jvmci_env()->isa_Assumptions_CallSiteTargetValue(assumption)) {
458
assumption_CallSiteTargetValue(assumption, JVMCI_CHECK);
459
} else {
460
JVMCI_ERROR("unexpected Assumption subclass %s", jvmci_env()->klass_name(assumption));
461
}
462
}
463
}
464
}
465
if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
466
JVMCIObjectArray methods = jvmci_env()->get_HotSpotCompiledCode_methods(compiled_code);
467
if (methods.is_non_null()) {
468
int length = JVMCIENV->get_length(methods);
469
for (int i = 0; i < length; ++i) {
470
JVMCIObject method_handle = JVMCIENV->get_object_at(methods, i);
471
Method* method = jvmci_env()->asMethod(method_handle);
472
_dependencies->assert_evol_method(method);
473
}
474
}
475
}
476
}
477
478
// constructor used to create a method
479
JVMCI::CodeInstallResult CodeInstaller::install(JVMCICompiler* compiler,
480
JVMCIObject target,
481
JVMCIObject compiled_code,
482
CodeBlob*& cb,
483
JVMCIObject installed_code,
484
FailedSpeculation** failed_speculations,
485
char* speculations,
486
int speculations_len,
487
JVMCI_TRAPS) {
488
489
CodeBuffer buffer("JVMCI Compiler CodeBuffer");
490
OopRecorder* recorder = new OopRecorder(&_arena, true);
491
initialize_dependencies(compiled_code, recorder, JVMCI_CHECK_OK);
492
493
// Get instructions and constants CodeSections early because we need it.
494
_instructions = buffer.insts();
495
_constants = buffer.consts();
496
497
initialize_fields(target, compiled_code, JVMCI_CHECK_OK);
498
JVMCI::CodeInstallResult result = initialize_buffer(buffer, true, JVMCI_CHECK_OK);
499
if (result != JVMCI::ok) {
500
return result;
501
}
502
503
int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
504
505
if (!jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {
506
JVMCIObject stubName = jvmci_env()->get_HotSpotCompiledCode_name(compiled_code);
507
if (stubName.is_null()) {
508
JVMCI_ERROR_OK("stub should have a name");
509
}
510
char* name = strdup(jvmci_env()->as_utf8_string(stubName));
511
cb = RuntimeStub::new_runtime_stub(name,
512
&buffer,
513
_offsets.value(CodeOffsets::Frame_Complete),
514
stack_slots,
515
_debug_recorder->_oopmaps,
516
false);
517
result = JVMCI::ok;
518
} else {
519
JVMCICompileState* compile_state = (JVMCICompileState*) (address) jvmci_env()->get_HotSpotCompiledNmethod_compileState(compiled_code);
520
if (compile_state != NULL) {
521
jvmci_env()->set_compile_state(compile_state);
522
}
523
524
Thread* thread = Thread::current();
525
526
methodHandle method(thread, jvmci_env()->asMethod(jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code)));
527
jint entry_bci = jvmci_env()->get_HotSpotCompiledNmethod_entryBCI(compiled_code);
528
bool has_unsafe_access = jvmci_env()->get_HotSpotCompiledNmethod_hasUnsafeAccess(compiled_code) == JNI_TRUE;
529
jint id = jvmci_env()->get_HotSpotCompiledNmethod_id(compiled_code);
530
if (id == -1) {
531
// Make sure a valid compile_id is associated with every compile
532
id = CompileBroker::assign_compile_id_unlocked(thread, method, entry_bci);
533
jvmci_env()->set_HotSpotCompiledNmethod_id(compiled_code, id);
534
}
535
if (!jvmci_env()->isa_HotSpotNmethod(installed_code)) {
536
JVMCI_THROW_MSG_(IllegalArgumentException, "InstalledCode object must be a HotSpotNmethod when installing a HotSpotCompiledNmethod", JVMCI::ok);
537
}
538
539
JVMCIObject mirror = installed_code;
540
nmethod* nm = NULL;
541
result = runtime()->register_method(jvmci_env(), method, nm, entry_bci, &_offsets, _orig_pc_offset, &buffer,
542
stack_slots, _debug_recorder->_oopmaps, &_exception_handler_table, &_implicit_exception_table,
543
compiler, _debug_recorder, _dependencies, id,
544
has_unsafe_access, _has_wide_vector, compiled_code, mirror,
545
failed_speculations, speculations, speculations_len);
546
cb = nm->as_codeblob_or_null();
547
if (nm != NULL && compile_state == NULL) {
548
// This compile didn't come through the CompileBroker so perform the printing here
549
DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler);
550
nm->maybe_print_nmethod(directive);
551
DirectivesStack::release(directive);
552
}
553
}
554
555
if (cb != NULL) {
556
// Make sure the pre-calculated constants section size was correct.
557
guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);
558
}
559
return result;
560
}
561
562
void CodeInstaller::initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS) {
563
if (jvmci_env()->isa_HotSpotCompiledNmethod(compiled_code)) {
564
JVMCIObject hotspotJavaMethod = jvmci_env()->get_HotSpotCompiledNmethod_method(compiled_code);
565
Thread* thread = Thread::current();
566
methodHandle method(thread, jvmci_env()->asMethod(hotspotJavaMethod));
567
_parameter_count = method->size_of_parameters();
568
JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());
569
} else {
570
// Must be a HotSpotCompiledRuntimeStub.
571
// Only used in OopMap constructor for non-product builds
572
_parameter_count = 0;
573
}
574
_sites_handle = jvmci_env()->get_HotSpotCompiledCode_sites(compiled_code);
575
576
_code_handle = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);
577
_code_size = jvmci_env()->get_HotSpotCompiledCode_targetCodeSize(compiled_code);
578
_total_frame_size = jvmci_env()->get_HotSpotCompiledCode_totalFrameSize(compiled_code);
579
580
JVMCIObject deoptRescueSlot = jvmci_env()->get_HotSpotCompiledCode_deoptRescueSlot(compiled_code);
581
if (deoptRescueSlot.is_null()) {
582
_orig_pc_offset = -1;
583
} else {
584
_orig_pc_offset = jvmci_env()->get_StackSlot_offset(deoptRescueSlot);
585
if (jvmci_env()->get_StackSlot_addFrameSize(deoptRescueSlot)) {
586
_orig_pc_offset += _total_frame_size;
587
}
588
if (_orig_pc_offset < 0) {
589
JVMCI_ERROR("invalid deopt rescue slot: %d", _orig_pc_offset);
590
}
591
}
592
593
// Pre-calculate the constants section size. This is required for PC-relative addressing.
594
_data_section_handle = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);
595
if ((_constants->alignment() % jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code)) != 0) {
596
JVMCI_ERROR("invalid data section alignment: %d", jvmci_env()->get_HotSpotCompiledCode_dataSectionAlignment(compiled_code));
597
}
598
_constants_size = JVMCIENV->get_length(data_section());
599
600
_data_section_patches_handle = jvmci_env()->get_HotSpotCompiledCode_dataSectionPatches(compiled_code);
601
602
#ifndef PRODUCT
603
_comments_handle = jvmci_env()->get_HotSpotCompiledCode_comments(compiled_code);
604
#endif
605
606
_next_call_type = INVOKE_INVALID;
607
608
_has_wide_vector = false;
609
610
JVMCIObject arch = jvmci_env()->get_TargetDescription_arch(target);
611
_word_kind_handle = jvmci_env()->get_Architecture_wordKind(arch);
612
}
613
614
int CodeInstaller::estimate_stubs_size(JVMCI_TRAPS) {
615
// Estimate the number of static call stubs that might be emitted.
616
int static_call_stubs = 0;
617
int trampoline_stubs = 0;
618
JVMCIObjectArray sites = this->sites();
619
for (int i = 0; i < JVMCIENV->get_length(sites); i++) {
620
JVMCIObject site = JVMCIENV->get_object_at(sites, i);
621
if (!site.is_null()) {
622
if (jvmci_env()->isa_site_Mark(site)) {
623
JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);
624
if (id_obj.is_non_null()) {
625
if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {
626
JVMCI_ERROR_0("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));
627
}
628
jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;
629
switch (id) {
630
case INVOKEINTERFACE:
631
case INVOKEVIRTUAL:
632
trampoline_stubs++;
633
break;
634
case INVOKESTATIC:
635
case INVOKESPECIAL:
636
static_call_stubs++;
637
trampoline_stubs++;
638
break;
639
default:
640
break;
641
}
642
}
643
}
644
}
645
}
646
int size = static_call_stubs * CompiledStaticCall::to_interp_stub_size();
647
size += trampoline_stubs * CompiledStaticCall::to_trampoline_stub_size();
648
return size;
649
}
650
651
// perform data and call relocation on the CodeBuffer
652
JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS) {
653
HandleMark hm(Thread::current());
654
JVMCIObjectArray sites = this->sites();
655
int locs_buffer_size = JVMCIENV->get_length(sites) * (relocInfo::length_limit + sizeof(relocInfo));
656
657
// Allocate enough space in the stub section for the static call
658
// stubs. Stubs have extra relocs but they are managed by the stub
659
// section itself so they don't need to be accounted for in the
660
// locs_buffer above.
661
int stubs_size = estimate_stubs_size(JVMCI_CHECK_OK);
662
int total_size = align_up(_code_size, buffer.insts()->alignment()) + align_up(_constants_size, buffer.consts()->alignment()) + align_up(stubs_size, buffer.stubs()->alignment());
663
664
if (check_size && total_size > JVMCINMethodSizeLimit) {
665
return JVMCI::code_too_large;
666
}
667
668
buffer.initialize(total_size, locs_buffer_size);
669
if (buffer.blob() == NULL) {
670
return JVMCI::cache_full;
671
}
672
buffer.initialize_stubs_size(stubs_size);
673
buffer.initialize_consts_size(_constants_size);
674
675
_debug_recorder = new DebugInformationRecorder(_oop_recorder);
676
_debug_recorder->set_oopmaps(new OopMapSet());
677
678
buffer.initialize_oop_recorder(_oop_recorder);
679
680
// copy the constant data into the newly created CodeBuffer
681
address end_data = _constants->start() + _constants_size;
682
JVMCIENV->copy_bytes_to(data_section(), (jbyte*) _constants->start(), 0, _constants_size);
683
_constants->set_end(end_data);
684
685
// copy the code into the newly created CodeBuffer
686
address end_pc = _instructions->start() + _code_size;
687
guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");
688
JVMCIENV->copy_bytes_to(code(), (jbyte*) _instructions->start(), 0, _code_size);
689
_instructions->set_end(end_pc);
690
691
for (int i = 0; i < JVMCIENV->get_length(data_section_patches()); i++) {
692
// HandleMark hm(THREAD);
693
JVMCIObject patch = JVMCIENV->get_object_at(data_section_patches(), i);
694
if (patch.is_null()) {
695
JVMCI_THROW_(NullPointerException, JVMCI::ok);
696
}
697
JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(patch);
698
if (reference.is_null()) {
699
JVMCI_THROW_(NullPointerException, JVMCI::ok);
700
}
701
if (!jvmci_env()->isa_site_ConstantReference(reference)) {
702
JVMCI_ERROR_OK("invalid patch in data section: %s", jvmci_env()->klass_name(reference));
703
}
704
JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);
705
if (constant.is_null()) {
706
JVMCI_THROW_(NullPointerException, JVMCI::ok);
707
}
708
address dest = _constants->start() + jvmci_env()->get_site_Site_pcOffset(patch);
709
if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {
710
if (jvmci_env()->get_HotSpotMetaspaceConstantImpl_compressed(constant)) {
711
#ifdef _LP64
712
*((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);
713
#else
714
JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");
715
#endif
716
} else {
717
*((void**) dest) = record_metadata_reference(_constants, dest, constant, JVMCI_CHECK_OK);
718
}
719
} else if (jvmci_env()->isa_HotSpotObjectConstantImpl(constant)) {
720
Handle obj = jvmci_env()->asConstant(constant, JVMCI_CHECK_OK);
721
jobject value = JNIHandles::make_local(obj());
722
int oop_index = _oop_recorder->find_index(value);
723
724
if (jvmci_env()->get_HotSpotObjectConstantImpl_compressed(constant)) {
725
#ifdef _LP64
726
_constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);
727
#else
728
JVMCI_ERROR_OK("unexpected compressed oop in 32-bit mode");
729
#endif
730
} else {
731
_constants->relocate(dest, oop_Relocation::spec(oop_index));
732
}
733
} else {
734
JVMCI_ERROR_OK("invalid constant in data section: %s", jvmci_env()->klass_name(constant));
735
}
736
}
737
jint last_pc_offset = -1;
738
for (int i = 0; i < JVMCIENV->get_length(sites); i++) {
739
// HandleMark hm(THREAD);
740
JVMCIObject site = JVMCIENV->get_object_at(sites, i);
741
if (site.is_null()) {
742
JVMCI_THROW_(NullPointerException, JVMCI::ok);
743
}
744
745
jint pc_offset = jvmci_env()->get_site_Site_pcOffset(site);
746
747
if (jvmci_env()->isa_site_Call(site)) {
748
JVMCI_event_4("call at %i", pc_offset);
749
site_Call(buffer, pc_offset, site, JVMCI_CHECK_OK);
750
} else if (jvmci_env()->isa_site_Infopoint(site)) {
751
// three reasons for infopoints denote actual safepoints
752
JVMCIObject reason = jvmci_env()->get_site_Infopoint_reason(site);
753
if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_SAFEPOINT()) ||
754
JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_CALL()) ||
755
JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {
756
JVMCI_event_4("safepoint at %i", pc_offset);
757
site_Safepoint(buffer, pc_offset, site, JVMCI_CHECK_OK);
758
if (_orig_pc_offset < 0) {
759
JVMCI_ERROR_OK("method contains safepoint, but has no deopt rescue slot");
760
}
761
if (JVMCIENV->equals(reason, jvmci_env()->get_site_InfopointReason_IMPLICIT_EXCEPTION())) {
762
if (jvmci_env()->isa_site_ImplicitExceptionDispatch(site)) {
763
jint dispatch_offset = jvmci_env()->get_site_ImplicitExceptionDispatch_dispatchOffset(site);
764
JVMCI_event_4("implicit exception at %i, dispatch to %i", pc_offset, dispatch_offset);
765
_implicit_exception_table.append(pc_offset, dispatch_offset);
766
} else {
767
JVMCI_event_4("implicit exception at %i", pc_offset);
768
_implicit_exception_table.add_deoptimize(pc_offset);
769
}
770
}
771
} else {
772
JVMCI_event_4("infopoint at %i", pc_offset);
773
site_Infopoint(buffer, pc_offset, site, JVMCI_CHECK_OK);
774
}
775
} else if (jvmci_env()->isa_site_DataPatch(site)) {
776
JVMCI_event_4("datapatch at %i", pc_offset);
777
site_DataPatch(buffer, pc_offset, site, JVMCI_CHECK_OK);
778
} else if (jvmci_env()->isa_site_Mark(site)) {
779
JVMCI_event_4("mark at %i", pc_offset);
780
site_Mark(buffer, pc_offset, site, JVMCI_CHECK_OK);
781
} else if (jvmci_env()->isa_site_ExceptionHandler(site)) {
782
JVMCI_event_4("exceptionhandler at %i", pc_offset);
783
site_ExceptionHandler(pc_offset, site);
784
} else {
785
JVMCI_ERROR_OK("unexpected site subclass: %s", jvmci_env()->klass_name(site));
786
}
787
last_pc_offset = pc_offset;
788
789
JavaThread* thread = JavaThread::current();
790
if (SafepointMechanism::should_process(thread)) {
791
// this is a hacky way to force a safepoint check but nothing else was jumping out at me.
792
ThreadToNativeFromVM ttnfv(thread);
793
}
794
}
795
796
#ifndef PRODUCT
797
if (comments().is_non_null()) {
798
for (int i = 0; i < JVMCIENV->get_length(comments()); i++) {
799
JVMCIObject comment = JVMCIENV->get_object_at(comments(), i);
800
assert(jvmci_env()->isa_HotSpotCompiledCode_Comment(comment), "cce");
801
jint offset = jvmci_env()->get_HotSpotCompiledCode_Comment_pcOffset(comment);
802
const char* text = jvmci_env()->as_utf8_string(jvmci_env()->get_HotSpotCompiledCode_Comment_text(comment));
803
buffer.block_comment(offset, text);
804
}
805
}
806
#endif
807
if (_has_auto_box) {
808
JavaThread* THREAD = JavaThread::current(); // For exception macros.
809
JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));
810
}
811
return JVMCI::ok;
812
}
813
814
void CodeInstaller::assumption_NoFinalizableSubclass(JVMCIObject assumption) {
815
JVMCIObject receiverType_handle = jvmci_env()->get_Assumptions_NoFinalizableSubclass_receiverType(assumption);
816
Klass* receiverType = jvmci_env()->asKlass(receiverType_handle);
817
_dependencies->assert_has_no_finalizable_subclasses(receiverType);
818
}
819
820
void CodeInstaller::assumption_ConcreteSubtype(JVMCIObject assumption) {
821
JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_context(assumption);
822
JVMCIObject subtype_handle = jvmci_env()->get_Assumptions_ConcreteSubtype_subtype(assumption);
823
Klass* context = jvmci_env()->asKlass(context_handle);
824
Klass* subtype = jvmci_env()->asKlass(subtype_handle);
825
826
assert(context->is_abstract(), "");
827
_dependencies->assert_abstract_with_unique_concrete_subtype(context, subtype);
828
}
829
830
void CodeInstaller::assumption_LeafType(JVMCIObject assumption) {
831
JVMCIObject context_handle = jvmci_env()->get_Assumptions_LeafType_context(assumption);
832
Klass* context = jvmci_env()->asKlass(context_handle);
833
834
_dependencies->assert_leaf_type(context);
835
}
836
837
void CodeInstaller::assumption_ConcreteMethod(JVMCIObject assumption) {
838
JVMCIObject impl_handle = jvmci_env()->get_Assumptions_ConcreteMethod_impl(assumption);
839
JVMCIObject context_handle = jvmci_env()->get_Assumptions_ConcreteMethod_context(assumption);
840
841
Method* impl = jvmci_env()->asMethod(impl_handle);
842
Klass* context = jvmci_env()->asKlass(context_handle);
843
844
_dependencies->assert_unique_concrete_method(context, impl);
845
}
846
847
void CodeInstaller::assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS) {
848
JVMCIObject callSiteConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_callSite(assumption);
849
Handle callSite = jvmci_env()->asConstant(callSiteConstant, JVMCI_CHECK);
850
JVMCIObject methodConstant = jvmci_env()->get_Assumptions_CallSiteTargetValue_methodHandle(assumption);
851
Handle methodHandle = jvmci_env()->asConstant(methodConstant, JVMCI_CHECK);
852
_dependencies->assert_call_site_target_value(callSite(), methodHandle());
853
}
854
855
void CodeInstaller::site_ExceptionHandler(jint pc_offset, JVMCIObject exc) {
856
jint handler_offset = jvmci_env()->get_site_ExceptionHandler_handlerPos(exc);
857
858
// Subtable header
859
_exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
860
861
// Subtable entry
862
_exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
863
}
864
865
// If deoptimization happens, the interpreter should reexecute these bytecodes.
866
// This function mainly helps the compilers to set up the reexecute bit.
867
static bool bytecode_should_reexecute(Bytecodes::Code code) {
868
switch (code) {
869
case Bytecodes::_invokedynamic:
870
case Bytecodes::_invokevirtual:
871
case Bytecodes::_invokeinterface:
872
case Bytecodes::_invokespecial:
873
case Bytecodes::_invokestatic:
874
return false;
875
default:
876
return true;
877
}
878
return true;
879
}
880
881
GrowableArray<ScopeValue*>* CodeInstaller::record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS) {
882
JVMCIObjectArray virtualObjects = jvmci_env()->get_DebugInfo_virtualObjectMapping(debug_info);
883
if (virtualObjects.is_null()) {
884
return NULL;
885
}
886
GrowableArray<ScopeValue*>* objects = new GrowableArray<ScopeValue*>(JVMCIENV->get_length(virtualObjects), JVMCIENV->get_length(virtualObjects), NULL);
887
// Create the unique ObjectValues
888
for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {
889
// HandleMark hm(THREAD);
890
JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);
891
int id = jvmci_env()->get_VirtualObject_id(value);
892
JVMCIObject type = jvmci_env()->get_VirtualObject_type(value);
893
bool is_auto_box = jvmci_env()->get_VirtualObject_isAutoBox(value);
894
if (is_auto_box) {
895
_has_auto_box = true;
896
}
897
Klass* klass = jvmci_env()->asKlass(type);
898
oop javaMirror = klass->java_mirror();
899
ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(Thread::current(), javaMirror));
900
ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
901
if (id < 0 || id >= objects->length()) {
902
JVMCI_ERROR_NULL("virtual object id %d out of bounds", id);
903
}
904
if (objects->at(id) != NULL) {
905
JVMCI_ERROR_NULL("duplicate virtual object id %d", id);
906
}
907
objects->at_put(id, sv);
908
}
909
// All the values which could be referenced by the VirtualObjects
910
// exist, so now describe all the VirtualObjects themselves.
911
for (int i = 0; i < JVMCIENV->get_length(virtualObjects); i++) {
912
// HandleMark hm(THREAD);
913
JVMCIObject value = JVMCIENV->get_object_at(virtualObjects, i);
914
int id = jvmci_env()->get_VirtualObject_id(value);
915
record_object_value(objects->at(id)->as_ObjectValue(), value, objects, JVMCI_CHECK_NULL);
916
}
917
_debug_recorder->dump_object_pool(objects);
918
919
return objects;
920
}
921
922
void CodeInstaller::record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
923
JVMCIObject position = jvmci_env()->get_DebugInfo_bytecodePosition(debug_info);
924
if (position.is_null()) {
925
// Stubs do not record scope info, just oop maps
926
return;
927
}
928
929
GrowableArray<ScopeValue*>* objectMapping;
930
if (scope_mode == CodeInstaller::FullFrame) {
931
objectMapping = record_virtual_objects(debug_info, JVMCI_CHECK);
932
} else {
933
objectMapping = NULL;
934
}
935
record_scope(pc_offset, position, scope_mode, objectMapping, is_mh_invoke, return_oop, JVMCI_CHECK);
936
}
937
938
int CodeInstaller::map_jvmci_bci(int bci) {
939
if (bci < 0) {
940
if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {
941
return BeforeBci;
942
} else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_BCI()) {
943
return AfterBci;
944
} else if (bci == jvmci_env()->get_BytecodeFrame_UNWIND_BCI()) {
945
return UnwindBci;
946
} else if (bci == jvmci_env()->get_BytecodeFrame_AFTER_EXCEPTION_BCI()) {
947
return AfterExceptionBci;
948
} else if (bci == jvmci_env()->get_BytecodeFrame_UNKNOWN_BCI()) {
949
return UnknownBci;
950
} else if (bci == jvmci_env()->get_BytecodeFrame_INVALID_FRAMESTATE_BCI()) {
951
return InvalidFrameStateBci;
952
}
953
ShouldNotReachHere();
954
}
955
return bci;
956
}
957
958
void CodeInstaller::record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
959
JVMCIObject frame;
960
if (scope_mode == CodeInstaller::FullFrame) {
961
if (!jvmci_env()->isa_BytecodeFrame(position)) {
962
JVMCI_ERROR("Full frame expected for debug info at %i", pc_offset);
963
}
964
frame = position;
965
}
966
JVMCIObject caller_frame = jvmci_env()->get_BytecodePosition_caller(position);
967
if (caller_frame.is_non_null()) {
968
record_scope(pc_offset, caller_frame, scope_mode, objects, is_mh_invoke, return_oop, JVMCI_CHECK);
969
}
970
971
JVMCIObject hotspot_method = jvmci_env()->get_BytecodePosition_method(position);
972
Thread* thread = Thread::current();
973
methodHandle method(thread, jvmci_env()->asMethod(hotspot_method));
974
jint bci = map_jvmci_bci(jvmci_env()->get_BytecodePosition_bci(position));
975
if (bci == jvmci_env()->get_BytecodeFrame_BEFORE_BCI()) {
976
bci = SynchronizationEntryBCI;
977
}
978
979
JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());
980
981
bool reexecute = false;
982
if (frame.is_non_null()) {
983
if (bci < 0){
984
reexecute = false;
985
} else {
986
Bytecodes::Code code = Bytecodes::java_code_at(method(), method->bcp_from(bci));
987
reexecute = bytecode_should_reexecute(code);
988
if (frame.is_non_null()) {
989
reexecute = (jvmci_env()->get_BytecodeFrame_duringCall(frame) == JNI_FALSE);
990
}
991
}
992
}
993
994
DebugToken* locals_token = NULL;
995
DebugToken* expressions_token = NULL;
996
DebugToken* monitors_token = NULL;
997
bool throw_exception = false;
998
999
if (frame.is_non_null()) {
1000
jint local_count = jvmci_env()->get_BytecodeFrame_numLocals(frame);
1001
jint expression_count = jvmci_env()->get_BytecodeFrame_numStack(frame);
1002
jint monitor_count = jvmci_env()->get_BytecodeFrame_numLocks(frame);
1003
JVMCIObjectArray values = jvmci_env()->get_BytecodeFrame_values(frame);
1004
JVMCIObjectArray slotKinds = jvmci_env()->get_BytecodeFrame_slotKinds(frame);
1005
1006
if (values.is_null() || slotKinds.is_null()) {
1007
JVMCI_THROW(NullPointerException);
1008
}
1009
if (local_count + expression_count + monitor_count != JVMCIENV->get_length(values)) {
1010
JVMCI_ERROR("unexpected values length %d in scope (%d locals, %d expressions, %d monitors)", JVMCIENV->get_length(values), local_count, expression_count, monitor_count);
1011
}
1012
if (local_count + expression_count != JVMCIENV->get_length(slotKinds)) {
1013
JVMCI_ERROR("unexpected slotKinds length %d in scope (%d locals, %d expressions)", JVMCIENV->get_length(slotKinds), local_count, expression_count);
1014
}
1015
1016
GrowableArray<ScopeValue*>* locals = local_count > 0 ? new GrowableArray<ScopeValue*> (local_count) : NULL;
1017
GrowableArray<ScopeValue*>* expressions = expression_count > 0 ? new GrowableArray<ScopeValue*> (expression_count) : NULL;
1018
GrowableArray<MonitorValue*>* monitors = monitor_count > 0 ? new GrowableArray<MonitorValue*> (monitor_count) : NULL;
1019
1020
JVMCI_event_2("Scope at bci %d with %d values", bci, JVMCIENV->get_length(values));
1021
JVMCI_event_2("%d locals %d expressions, %d monitors", local_count, expression_count, monitor_count);
1022
1023
for (jint i = 0; i < JVMCIENV->get_length(values); i++) {
1024
// HandleMark hm(THREAD);
1025
ScopeValue* second = NULL;
1026
JVMCIObject value = JVMCIENV->get_object_at(values, i);
1027
if (i < local_count) {
1028
BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
1029
ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);
1030
if (second != NULL) {
1031
locals->append(second);
1032
}
1033
locals->append(first);
1034
} else if (i < local_count + expression_count) {
1035
BasicType type = jvmci_env()->kindToBasicType(JVMCIENV->get_object_at(slotKinds, i), JVMCI_CHECK);
1036
ScopeValue* first = get_scope_value(value, type, objects, second, JVMCI_CHECK);
1037
if (second != NULL) {
1038
expressions->append(second);
1039
}
1040
expressions->append(first);
1041
} else {
1042
MonitorValue *monitor = get_monitor_value(value, objects, JVMCI_CHECK);
1043
monitors->append(monitor);
1044
}
1045
if (second != NULL) {
1046
i++;
1047
if (i >= JVMCIENV->get_length(values) || !JVMCIENV->equals(JVMCIENV->get_object_at(values, i), jvmci_env()->get_Value_ILLEGAL())) {
1048
JVMCI_ERROR("double-slot value not followed by Value.ILLEGAL");
1049
}
1050
}
1051
}
1052
1053
locals_token = _debug_recorder->create_scope_values(locals);
1054
expressions_token = _debug_recorder->create_scope_values(expressions);
1055
monitors_token = _debug_recorder->create_monitor_values(monitors);
1056
1057
throw_exception = jvmci_env()->get_BytecodeFrame_rethrowException(frame) == JNI_TRUE;
1058
}
1059
1060
// has_ea_local_in_scope and arg_escape should be added to JVMCI
1061
const bool is_opt_native = false;
1062
const bool has_ea_local_in_scope = false;
1063
const bool arg_escape = false;
1064
_debug_recorder->describe_scope(pc_offset, method, NULL, bci, reexecute, throw_exception, is_mh_invoke, is_opt_native, return_oop,
1065
has_ea_local_in_scope, arg_escape,
1066
locals_token, expressions_token, monitors_token);
1067
}
1068
1069
void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1070
JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1071
if (debug_info.is_null()) {
1072
JVMCI_ERROR("debug info expected at safepoint at %i", pc_offset);
1073
}
1074
1075
// address instruction = _instructions->start() + pc_offset;
1076
// jint next_pc_offset = Assembler::locate_next_instruction(instruction) - _instructions->start();
1077
OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);
1078
_debug_recorder->add_safepoint(pc_offset, map);
1079
record_scope(pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);
1080
_debug_recorder->end_safepoint(pc_offset);
1081
}
1082
1083
void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1084
JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1085
if (debug_info.is_null()) {
1086
JVMCI_ERROR("debug info expected at infopoint at %i", pc_offset);
1087
}
1088
1089
// We'd like to check that pc_offset is greater than the
1090
// last pc recorded with _debug_recorder (raising an exception if not)
1091
// but DebugInformationRecorder doesn't have sufficient public API.
1092
1093
_debug_recorder->add_non_safepoint(pc_offset);
1094
record_scope(pc_offset, debug_info, CodeInstaller::BytecodePosition, JVMCI_CHECK);
1095
_debug_recorder->end_non_safepoint(pc_offset);
1096
}
1097
1098
void CodeInstaller::site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1099
JVMCIObject target = jvmci_env()->get_site_Call_target(site);
1100
JVMCIObject hotspot_method; // JavaMethod
1101
JVMCIObject foreign_call;
1102
1103
if (jvmci_env()->isa_HotSpotForeignCallTarget(target)) {
1104
foreign_call = target;
1105
} else {
1106
hotspot_method = target;
1107
}
1108
1109
JVMCIObject debug_info = jvmci_env()->get_site_Infopoint_debugInfo(site);
1110
1111
assert(hotspot_method.is_non_null() ^ foreign_call.is_non_null(), "Call site needs exactly one type");
1112
1113
NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
1114
jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, hotspot_method, JVMCI_CHECK);
1115
1116
if (debug_info.is_non_null()) {
1117
OopMap *map = create_oop_map(debug_info, JVMCI_CHECK);
1118
_debug_recorder->add_safepoint(next_pc_offset, map);
1119
1120
if (hotspot_method.is_non_null()) {
1121
Method *method = jvmci_env()->asMethod(hotspot_method);
1122
vmIntrinsics::ID iid = method->intrinsic_id();
1123
bool is_mh_invoke = false;
1124
if (jvmci_env()->get_site_Call_direct(site)) {
1125
is_mh_invoke = !method->is_static() && (iid == vmIntrinsics::_compiledLambdaForm ||
1126
(MethodHandles::is_signature_polymorphic(iid) && MethodHandles::is_signature_polymorphic_intrinsic(iid)));
1127
}
1128
bool return_oop = method->is_returning_oop();
1129
record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, is_mh_invoke, return_oop, JVMCI_CHECK);
1130
} else {
1131
record_scope(next_pc_offset, debug_info, CodeInstaller::FullFrame, JVMCI_CHECK);
1132
}
1133
}
1134
1135
if (foreign_call.is_non_null()) {
1136
jlong foreign_call_destination = jvmci_env()->get_HotSpotForeignCallTarget_address(foreign_call);
1137
CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);
1138
} else { // method != NULL
1139
if (debug_info.is_null()) {
1140
JVMCI_ERROR("debug info expected at call at %i", pc_offset);
1141
}
1142
1143
JVMCI_event_3("method call");
1144
CodeInstaller::pd_relocate_JavaMethod(buffer, hotspot_method, pc_offset, JVMCI_CHECK);
1145
if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {
1146
// Need a static call stub for transitions from compiled to interpreted.
1147
CompiledStaticCall::emit_to_interp_stub(buffer, _instructions->start() + pc_offset);
1148
}
1149
}
1150
1151
_next_call_type = INVOKE_INVALID;
1152
1153
if (debug_info.is_non_null()) {
1154
_debug_recorder->end_safepoint(next_pc_offset);
1155
}
1156
}
1157
1158
void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1159
JVMCIObject reference = jvmci_env()->get_site_DataPatch_reference(site);
1160
if (reference.is_null()) {
1161
JVMCI_THROW(NullPointerException);
1162
} else if (jvmci_env()->isa_site_ConstantReference(reference)) {
1163
JVMCIObject constant = jvmci_env()->get_site_ConstantReference_constant(reference);
1164
if (constant.is_null()) {
1165
JVMCI_THROW(NullPointerException);
1166
} else if (jvmci_env()->isa_DirectHotSpotObjectConstantImpl(constant)) {
1167
if (!JVMCIENV->is_hotspot()) {
1168
JVMCIObject string = JVMCIENV->call_HotSpotJVMCIRuntime_callToString(constant, JVMCI_CHECK);
1169
const char* to_string = JVMCIENV->as_utf8_string(string);
1170
JVMCI_THROW_MSG(IllegalArgumentException, err_msg("Direct object constant reached the backend: %s", to_string));
1171
}
1172
pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);
1173
} else if (jvmci_env()->isa_IndirectHotSpotObjectConstantImpl(constant)) {
1174
pd_patch_OopConstant(pc_offset, constant, JVMCI_CHECK);
1175
} else if (jvmci_env()->isa_HotSpotMetaspaceConstantImpl(constant)) {
1176
pd_patch_MetaspaceConstant(pc_offset, constant, JVMCI_CHECK);
1177
} else {
1178
JVMCI_ERROR("unknown constant type in data patch: %s", jvmci_env()->klass_name(constant));
1179
}
1180
} else if (jvmci_env()->isa_site_DataSectionReference(reference)) {
1181
int data_offset = jvmci_env()->get_site_DataSectionReference_offset(reference);
1182
if (0 <= data_offset && data_offset < _constants_size) {
1183
pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);
1184
} else {
1185
JVMCI_ERROR("data offset 0x%X points outside data section (size 0x%X)", data_offset, _constants_size);
1186
}
1187
} else {
1188
JVMCI_ERROR("unknown data patch type: %s", jvmci_env()->klass_name(reference));
1189
}
1190
}
1191
1192
void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS) {
1193
JVMCIObject id_obj = jvmci_env()->get_site_Mark_id(site);
1194
1195
if (id_obj.is_non_null()) {
1196
if (!jvmci_env()->is_boxing_object(T_INT, id_obj)) {
1197
JVMCI_ERROR("expected Integer id, got %s", jvmci_env()->klass_name(id_obj));
1198
}
1199
jint id = jvmci_env()->get_boxed_value(T_INT, id_obj).i;
1200
1201
address pc = _instructions->start() + pc_offset;
1202
1203
switch (id) {
1204
case UNVERIFIED_ENTRY:
1205
_offsets.set_value(CodeOffsets::Entry, pc_offset);
1206
break;
1207
case VERIFIED_ENTRY:
1208
_offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);
1209
break;
1210
case OSR_ENTRY:
1211
_offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);
1212
break;
1213
case EXCEPTION_HANDLER_ENTRY:
1214
_offsets.set_value(CodeOffsets::Exceptions, pc_offset);
1215
break;
1216
case DEOPT_HANDLER_ENTRY:
1217
_offsets.set_value(CodeOffsets::Deopt, pc_offset);
1218
break;
1219
case DEOPT_MH_HANDLER_ENTRY:
1220
_offsets.set_value(CodeOffsets::DeoptMH, pc_offset);
1221
break;
1222
case FRAME_COMPLETE:
1223
_offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
1224
break;
1225
case INVOKEVIRTUAL:
1226
case INVOKEINTERFACE:
1227
case INLINE_INVOKE:
1228
case INVOKESTATIC:
1229
case INVOKESPECIAL:
1230
_next_call_type = (MarkId) id;
1231
_invoke_mark_pc = pc;
1232
break;
1233
case POLL_NEAR:
1234
case POLL_FAR:
1235
case POLL_RETURN_NEAR:
1236
case POLL_RETURN_FAR:
1237
pd_relocate_poll(pc, id, JVMCI_CHECK);
1238
break;
1239
case CARD_TABLE_SHIFT:
1240
case CARD_TABLE_ADDRESS:
1241
case HEAP_TOP_ADDRESS:
1242
case HEAP_END_ADDRESS:
1243
case NARROW_KLASS_BASE_ADDRESS:
1244
case NARROW_OOP_BASE_ADDRESS:
1245
case CRC_TABLE_ADDRESS:
1246
case LOG_OF_HEAP_REGION_GRAIN_BYTES:
1247
case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:
1248
case VERIFY_OOPS:
1249
case VERIFY_OOP_BITS:
1250
case VERIFY_OOP_MASK:
1251
case VERIFY_OOP_COUNT_ADDRESS:
1252
break;
1253
default:
1254
JVMCI_ERROR("invalid mark id: %d", id);
1255
break;
1256
}
1257
}
1258
}
1259
1260