42 Argument *method = ai++;
43 method->setName("method");
44 Argument *base_pc = ai++;
45 base_pc->setName("base_pc");
46 code_buffer()->set_base_pc(base_pc);
47 Argument *thread = ai++;
48 thread->setName("thread");
49 set_thread(thread);
50
51 // Create and push our stack frame
52 builder()->SetInsertPoint(CreateBlock());
53 _stack = SharkStack::CreateBuildAndPushFrame(this, method);
54 NOT_PRODUCT(method = NULL);
55
56 // Create the oopmap. We use the one oopmap for every call site in
57 // the wrapper, which results in the odd mild inefficiency but is a
58 // damn sight easier to code.
59 OopMap *oopmap = new OopMap(
60 SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),
61 SharkStack::oopmap_slot_munge(arg_size()));
62 oopmap->set_oop(SharkStack::slot2reg(stack()->method_slot_offset()));
63
64 // Set up the oop_tmp slot if required:
65 // - For static methods we use it to handlize the class argument
66 // for the call, and to protect the same during slow path locks
67 // (if synchronized).
68 // - For methods returning oops, we use it to protect the return
69 // value across safepoints or slow path unlocking.
70 if (is_static() || is_returning_oop()) {
71 _oop_tmp_slot = stack()->slot_addr(
72 stack()->oop_tmp_slot_offset(),
73 SharkType::oop_type(),
74 "oop_tmp_slot");
75
76 oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));
77 }
78
79 // Set up the monitor slot, for synchronized methods
80 if (is_synchronized()) {
81 Unimplemented();
82 _lock_slot_offset = 23;
83 }
84
85 // Start building the argument list
86 std::vector<const Type*> param_types;
87 std::vector<Value*> param_values;
88 const PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());
89
90 // First argument is the JNIEnv
91 param_types.push_back(SharkType::jniEnv_type());
92 param_values.push_back(
93 builder()->CreateAddressOfStructEntry(
94 thread,
95 JavaThread::jni_environment_offset(),
96 SharkType::jniEnv_type(),
97 "jni_environment"));
98
99 // For static methods, the second argument is the class
100 if (is_static()) {
101 builder()->CreateStore(
102 builder()->CreateInlineOop(
103 JNIHandles::make_local(
104 target()->method_holder()->java_mirror())),
105 oop_tmp_slot());
106
107 param_types.push_back(box_type);
108 param_values.push_back(oop_tmp_slot());
132 case T_ARRAY:
133 null = CreateBlock("null");
134 not_null = CreateBlock("not_null");
135 merge = CreateBlock("merge");
136
137 box = stack()->slot_addr(slot_offset, SharkType::oop_type());
138 builder()->CreateCondBr(
139 builder()->CreateICmp(
140 ICmpInst::ICMP_EQ,
141 builder()->CreateLoad(box),
142 LLVMValue::null()),
143 null, not_null);
144
145 builder()->SetInsertPoint(null);
146 builder()->CreateBr(merge);
147
148 builder()->SetInsertPoint(not_null);
149 builder()->CreateBr(merge);
150
151 builder()->SetInsertPoint(merge);
152 phi = builder()->CreatePHI(box_type, "boxed_object");
153 phi->addIncoming(ConstantPointerNull::get(box_type), null);
154 phi->addIncoming(box, not_null);
155 box = phi;
156
157 param_types.push_back(box_type);
158 param_values.push_back(box);
159
160 oopmap->set_oop(SharkStack::slot2reg(slot_offset));
161
162 if (i == 0 && !is_static())
163 _receiver_slot_offset = slot_offset;
164
165 break;
166
167 case T_LONG:
168 case T_DOUBLE:
169 adjusted_offset--;
170 // fall through
171
172 default:
173 const Type *param_type = SharkType::to_stackType(arg_type(i));
174
175 param_types.push_back(param_type);
176 param_values.push_back(
177 builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));
178 }
179 }
180
181 // The oopmap is now complete, and everything is written
182 // into the frame except the PC.
183 int pc_offset = code_buffer()->create_unique_offset();
184
185 _oop_maps = new OopMapSet();
186 oop_maps()->add_gc_map(pc_offset, oopmap);
187
188 builder()->CreateStore(
189 builder()->code_buffer_address(pc_offset),
190 stack()->slot_addr(stack()->pc_slot_offset()));
191
192 // Set up the Java frame anchor
193 stack()->CreateSetLastJavaFrame();
194
195 // Lock if necessary
196 if (is_synchronized())
197 Unimplemented();
198
199 // Change the thread state to _thread_in_native
200 CreateSetThreadState(_thread_in_native);
201
202 // Make the call
203 BasicType result_type = target()->result_type();
204 const Type* return_type;
205 if (result_type == T_VOID)
206 return_type = SharkType::void_type();
207 else if (is_returning_oop())
208 return_type = box_type;
209 else
210 return_type = SharkType::to_arrayType(result_type);
211 Value* native_function = builder()->CreateIntToPtr(
212 LLVMValue::intptr_constant((intptr_t) target()->native_function()),
213 PointerType::getUnqual(
214 FunctionType::get(return_type, param_types, false)));
215 Value *result = builder()->CreateCall(
216 native_function, param_values.begin(), param_values.end());
217
218 // Start the transition back to _thread_in_Java
219 CreateSetThreadState(_thread_in_native_trans);
220
221 // Make sure new state is visible in the GC thread
222 if (os::is_MP()) {
223 if (UseMembar)
224 builder()->CreateMemoryBarrier(SharkBuilder::BARRIER_STORELOAD);
225 else
226 CreateWriteMemorySerializePage();
227 }
228
229 // Handle safepoint operations, pending suspend requests,
230 // and pending asynchronous exceptions.
231 BasicBlock *check_thread = CreateBlock("check_thread");
232 BasicBlock *do_safepoint = CreateBlock("do_safepoint");
233 BasicBlock *safepointed = CreateBlock("safepointed");
234
235 Value *global_state = builder()->CreateLoad(
236 builder()->CreateIntToPtr(
237 LLVMValue::intptr_constant(
238 (intptr_t) SafepointSynchronize::address_of_state()),
239 PointerType::getUnqual(SharkType::jint_type())),
240 "global_state");
241
242 builder()->CreateCondBr(
243 builder()->CreateICmpNE(
244 global_state,
288 builder()->CreateRet(LLVMValue::jint_constant(0));
289
290 builder()->SetInsertPoint(no_exception);
291
292 // If the result was an oop then unbox it before
293 // releasing the handle it might be protected by
294 if (is_returning_oop()) {
295 BasicBlock *null = builder()->GetInsertBlock();
296 BasicBlock *not_null = CreateBlock("not_null");
297 BasicBlock *merge = CreateBlock("merge");
298
299 builder()->CreateCondBr(
300 builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),
301 not_null, merge);
302
303 builder()->SetInsertPoint(not_null);
304 Value *unboxed_result = builder()->CreateLoad(result);
305 builder()->CreateBr(merge);
306
307 builder()->SetInsertPoint(merge);
308 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), "result");
309 phi->addIncoming(LLVMValue::null(), null);
310 phi->addIncoming(unboxed_result, not_null);
311 result = phi;
312 }
313
314 // Reset handle block
315 CreateResetHandleBlock();
316
317 // Unlock if necessary.
318 if (is_synchronized())
319 Unimplemented();
320
321 // Unwind and return
322 Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);
323 if (result_type != T_VOID) {
324 bool needs_cast = false;
325 bool is_signed = false;
326 switch (result_type) {
327 case T_BOOLEAN:
328 result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));
|
42 Argument *method = ai++;
43 method->setName("method");
44 Argument *base_pc = ai++;
45 base_pc->setName("base_pc");
46 code_buffer()->set_base_pc(base_pc);
47 Argument *thread = ai++;
48 thread->setName("thread");
49 set_thread(thread);
50
51 // Create and push our stack frame
52 builder()->SetInsertPoint(CreateBlock());
53 _stack = SharkStack::CreateBuildAndPushFrame(this, method);
54 NOT_PRODUCT(method = NULL);
55
56 // Create the oopmap. We use the one oopmap for every call site in
57 // the wrapper, which results in the odd mild inefficiency but is a
58 // damn sight easier to code.
59 OopMap *oopmap = new OopMap(
60 SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),
61 SharkStack::oopmap_slot_munge(arg_size()));
62
63 // Set up the oop_tmp slot if required:
64 // - For static methods we use it to handlize the class argument
65 // for the call, and to protect the same during slow path locks
66 // (if synchronized).
67 // - For methods returning oops, we use it to protect the return
68 // value across safepoints or slow path unlocking.
69 if (is_static() || is_returning_oop()) {
70 _oop_tmp_slot = stack()->slot_addr(
71 stack()->oop_tmp_slot_offset(),
72 SharkType::oop_type(),
73 "oop_tmp_slot");
74
75 oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));
76 }
77
78 // Set up the monitor slot, for synchronized methods
79 if (is_synchronized()) {
80 Unimplemented();
81 _lock_slot_offset = 23;
82 }
83
84 // Start building the argument list
85 std::vector<Type*> param_types;
86 std::vector<Value*> param_values;
87 PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());
88
89 // First argument is the JNIEnv
90 param_types.push_back(SharkType::jniEnv_type());
91 param_values.push_back(
92 builder()->CreateAddressOfStructEntry(
93 thread,
94 JavaThread::jni_environment_offset(),
95 SharkType::jniEnv_type(),
96 "jni_environment"));
97
98 // For static methods, the second argument is the class
99 if (is_static()) {
100 builder()->CreateStore(
101 builder()->CreateInlineOop(
102 JNIHandles::make_local(
103 target()->method_holder()->java_mirror())),
104 oop_tmp_slot());
105
106 param_types.push_back(box_type);
107 param_values.push_back(oop_tmp_slot());
131 case T_ARRAY:
132 null = CreateBlock("null");
133 not_null = CreateBlock("not_null");
134 merge = CreateBlock("merge");
135
136 box = stack()->slot_addr(slot_offset, SharkType::oop_type());
137 builder()->CreateCondBr(
138 builder()->CreateICmp(
139 ICmpInst::ICMP_EQ,
140 builder()->CreateLoad(box),
141 LLVMValue::null()),
142 null, not_null);
143
144 builder()->SetInsertPoint(null);
145 builder()->CreateBr(merge);
146
147 builder()->SetInsertPoint(not_null);
148 builder()->CreateBr(merge);
149
150 builder()->SetInsertPoint(merge);
151 phi = builder()->CreatePHI(box_type, 0, "boxed_object");
152 phi->addIncoming(ConstantPointerNull::get(box_type), null);
153 phi->addIncoming(box, not_null);
154 box = phi;
155
156 param_types.push_back(box_type);
157 param_values.push_back(box);
158
159 oopmap->set_oop(SharkStack::slot2reg(slot_offset));
160
161 if (i == 0 && !is_static())
162 _receiver_slot_offset = slot_offset;
163
164 break;
165
166 case T_LONG:
167 case T_DOUBLE:
168 adjusted_offset--;
169 // fall through
170
171 default:
172 Type *param_type = SharkType::to_stackType(arg_type(i));
173
174 param_types.push_back(param_type);
175 param_values.push_back(
176 builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));
177 }
178 }
179
180 // The oopmap is now complete, and everything is written
181 // into the frame except the PC.
182 int pc_offset = code_buffer()->create_unique_offset();
183
184 _oop_maps = new OopMapSet();
185 oop_maps()->add_gc_map(pc_offset, oopmap);
186
187 builder()->CreateStore(
188 builder()->code_buffer_address(pc_offset),
189 stack()->slot_addr(stack()->pc_slot_offset()));
190
191 // Set up the Java frame anchor
192 stack()->CreateSetLastJavaFrame();
193
194 // Lock if necessary
195 if (is_synchronized())
196 Unimplemented();
197
198 // Change the thread state to _thread_in_native
199 CreateSetThreadState(_thread_in_native);
200
201 // Make the call
202 BasicType result_type = target()->result_type();
203 Type* return_type;
204 if (result_type == T_VOID)
205 return_type = SharkType::void_type();
206 else if (is_returning_oop())
207 return_type = box_type;
208 else
209 return_type = SharkType::to_arrayType(result_type);
210 Value* native_function = builder()->CreateIntToPtr(
211 LLVMValue::intptr_constant((intptr_t) target()->native_function()),
212 PointerType::getUnqual(
213 FunctionType::get(return_type, param_types, false)));
214 Value *result = builder()->CreateCall(
215 native_function, llvm::makeArrayRef(param_values));
216
217 // Start the transition back to _thread_in_Java
218 CreateSetThreadState(_thread_in_native_trans);
219
220 // Make sure new state is visible in the GC thread
221 if (os::is_MP()) {
222 if (UseMembar)
223 builder()->CreateFence(llvm::SequentiallyConsistent, llvm::CrossThread);
224 else
225 CreateWriteMemorySerializePage();
226 }
227
228 // Handle safepoint operations, pending suspend requests,
229 // and pending asynchronous exceptions.
230 BasicBlock *check_thread = CreateBlock("check_thread");
231 BasicBlock *do_safepoint = CreateBlock("do_safepoint");
232 BasicBlock *safepointed = CreateBlock("safepointed");
233
234 Value *global_state = builder()->CreateLoad(
235 builder()->CreateIntToPtr(
236 LLVMValue::intptr_constant(
237 (intptr_t) SafepointSynchronize::address_of_state()),
238 PointerType::getUnqual(SharkType::jint_type())),
239 "global_state");
240
241 builder()->CreateCondBr(
242 builder()->CreateICmpNE(
243 global_state,
287 builder()->CreateRet(LLVMValue::jint_constant(0));
288
289 builder()->SetInsertPoint(no_exception);
290
291 // If the result was an oop then unbox it before
292 // releasing the handle it might be protected by
293 if (is_returning_oop()) {
294 BasicBlock *null = builder()->GetInsertBlock();
295 BasicBlock *not_null = CreateBlock("not_null");
296 BasicBlock *merge = CreateBlock("merge");
297
298 builder()->CreateCondBr(
299 builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),
300 not_null, merge);
301
302 builder()->SetInsertPoint(not_null);
303 Value *unboxed_result = builder()->CreateLoad(result);
304 builder()->CreateBr(merge);
305
306 builder()->SetInsertPoint(merge);
307 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "result");
308 phi->addIncoming(LLVMValue::null(), null);
309 phi->addIncoming(unboxed_result, not_null);
310 result = phi;
311 }
312
313 // Reset handle block
314 CreateResetHandleBlock();
315
316 // Unlock if necessary.
317 if (is_synchronized())
318 Unimplemented();
319
320 // Unwind and return
321 Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);
322 if (result_type != T_VOID) {
323 bool needs_cast = false;
324 bool is_signed = false;
325 switch (result_type) {
326 case T_BOOLEAN:
327 result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));
|