src/share/vm/shark/sharkNativeWrapper.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * Copyright 2009, 2010 Red Hat, Inc.
aoqi@0 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 5 *
aoqi@0 6 * This code is free software; you can redistribute it and/or modify it
aoqi@0 7 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 8 * published by the Free Software Foundation.
aoqi@0 9 *
aoqi@0 10 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 13 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 14 * accompanied this code).
aoqi@0 15 *
aoqi@0 16 * You should have received a copy of the GNU General Public License version
aoqi@0 17 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 19 *
aoqi@0 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 21 * or visit www.oracle.com if you need additional information or have any
aoqi@0 22 * questions.
aoqi@0 23 *
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 #include "precompiled.hpp"
aoqi@0 27 #include "shark/llvmHeaders.hpp"
aoqi@0 28 #include "shark/sharkNativeWrapper.hpp"
aoqi@0 29 #include "shark/sharkType.hpp"
aoqi@0 30
aoqi@0 31 using namespace llvm;
aoqi@0 32
aoqi@0 33 void SharkNativeWrapper::initialize(const char *name) {
aoqi@0 34 // Create the function
aoqi@0 35 _function = Function::Create(
aoqi@0 36 SharkType::entry_point_type(),
aoqi@0 37 GlobalVariable::InternalLinkage,
aoqi@0 38 name);
aoqi@0 39
aoqi@0 40 // Get our arguments
aoqi@0 41 Function::arg_iterator ai = function()->arg_begin();
aoqi@0 42 Argument *method = ai++;
aoqi@0 43 method->setName("method");
aoqi@0 44 Argument *base_pc = ai++;
aoqi@0 45 base_pc->setName("base_pc");
aoqi@0 46 code_buffer()->set_base_pc(base_pc);
aoqi@0 47 Argument *thread = ai++;
aoqi@0 48 thread->setName("thread");
aoqi@0 49 set_thread(thread);
aoqi@0 50
aoqi@0 51 // Create and push our stack frame
aoqi@0 52 builder()->SetInsertPoint(CreateBlock());
aoqi@0 53 _stack = SharkStack::CreateBuildAndPushFrame(this, method);
aoqi@0 54 NOT_PRODUCT(method = NULL);
aoqi@0 55
aoqi@0 56 // Create the oopmap. We use the one oopmap for every call site in
aoqi@0 57 // the wrapper, which results in the odd mild inefficiency but is a
aoqi@0 58 // damn sight easier to code.
aoqi@0 59 OopMap *oopmap = new OopMap(
aoqi@0 60 SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),
aoqi@0 61 SharkStack::oopmap_slot_munge(arg_size()));
aoqi@0 62
aoqi@0 63 // Set up the oop_tmp slot if required:
aoqi@0 64 // - For static methods we use it to handlize the class argument
aoqi@0 65 // for the call, and to protect the same during slow path locks
aoqi@0 66 // (if synchronized).
aoqi@0 67 // - For methods returning oops, we use it to protect the return
aoqi@0 68 // value across safepoints or slow path unlocking.
aoqi@0 69 if (is_static() || is_returning_oop()) {
aoqi@0 70 _oop_tmp_slot = stack()->slot_addr(
aoqi@0 71 stack()->oop_tmp_slot_offset(),
aoqi@0 72 SharkType::oop_type(),
aoqi@0 73 "oop_tmp_slot");
aoqi@0 74
aoqi@0 75 oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 // Set up the monitor slot, for synchronized methods
aoqi@0 79 if (is_synchronized()) {
aoqi@0 80 Unimplemented();
aoqi@0 81 _lock_slot_offset = 23;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 // Start building the argument list
aoqi@0 85 std::vector<Type*> param_types;
aoqi@0 86 std::vector<Value*> param_values;
aoqi@0 87 PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());
aoqi@0 88
aoqi@0 89 // First argument is the JNIEnv
aoqi@0 90 param_types.push_back(SharkType::jniEnv_type());
aoqi@0 91 param_values.push_back(
aoqi@0 92 builder()->CreateAddressOfStructEntry(
aoqi@0 93 thread,
aoqi@0 94 JavaThread::jni_environment_offset(),
aoqi@0 95 SharkType::jniEnv_type(),
aoqi@0 96 "jni_environment"));
aoqi@0 97
aoqi@0 98 // For static methods, the second argument is the class
aoqi@0 99 if (is_static()) {
aoqi@0 100 builder()->CreateStore(
aoqi@0 101 builder()->CreateInlineOop(
aoqi@0 102 JNIHandles::make_local(
aoqi@0 103 target()->method_holder()->java_mirror())),
aoqi@0 104 oop_tmp_slot());
aoqi@0 105
aoqi@0 106 param_types.push_back(box_type);
aoqi@0 107 param_values.push_back(oop_tmp_slot());
aoqi@0 108
aoqi@0 109 _receiver_slot_offset = stack()->oop_tmp_slot_offset();
aoqi@0 110 }
aoqi@0 111 else if (is_returning_oop()) {
aoqi@0 112 // The oop_tmp slot is registered in the oopmap,
aoqi@0 113 // so we need to clear it. This is one of the
aoqi@0 114 // mild inefficiencies I mentioned earlier.
aoqi@0 115 builder()->CreateStore(LLVMValue::null(), oop_tmp_slot());
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 // Parse the arguments
aoqi@0 119 for (int i = 0; i < arg_size(); i++) {
aoqi@0 120 int slot_offset = stack()->locals_slots_offset() + arg_size() - 1 - i;
aoqi@0 121 int adjusted_offset = slot_offset;
aoqi@0 122 BasicBlock *null, *not_null, *merge;
aoqi@0 123 Value *box;
aoqi@0 124 PHINode *phi;
aoqi@0 125
aoqi@0 126 switch (arg_type(i)) {
aoqi@0 127 case T_VOID:
aoqi@0 128 break;
aoqi@0 129
aoqi@0 130 case T_OBJECT:
aoqi@0 131 case T_ARRAY:
aoqi@0 132 null = CreateBlock("null");
aoqi@0 133 not_null = CreateBlock("not_null");
aoqi@0 134 merge = CreateBlock("merge");
aoqi@0 135
aoqi@0 136 box = stack()->slot_addr(slot_offset, SharkType::oop_type());
aoqi@0 137 builder()->CreateCondBr(
aoqi@0 138 builder()->CreateICmp(
aoqi@0 139 ICmpInst::ICMP_EQ,
aoqi@0 140 builder()->CreateLoad(box),
aoqi@0 141 LLVMValue::null()),
aoqi@0 142 null, not_null);
aoqi@0 143
aoqi@0 144 builder()->SetInsertPoint(null);
aoqi@0 145 builder()->CreateBr(merge);
aoqi@0 146
aoqi@0 147 builder()->SetInsertPoint(not_null);
aoqi@0 148 builder()->CreateBr(merge);
aoqi@0 149
aoqi@0 150 builder()->SetInsertPoint(merge);
aoqi@0 151 phi = builder()->CreatePHI(box_type, 0, "boxed_object");
aoqi@0 152 phi->addIncoming(ConstantPointerNull::get(box_type), null);
aoqi@0 153 phi->addIncoming(box, not_null);
aoqi@0 154 box = phi;
aoqi@0 155
aoqi@0 156 param_types.push_back(box_type);
aoqi@0 157 param_values.push_back(box);
aoqi@0 158
aoqi@0 159 oopmap->set_oop(SharkStack::slot2reg(slot_offset));
aoqi@0 160
aoqi@0 161 if (i == 0 && !is_static())
aoqi@0 162 _receiver_slot_offset = slot_offset;
aoqi@0 163
aoqi@0 164 break;
aoqi@0 165
aoqi@0 166 case T_LONG:
aoqi@0 167 case T_DOUBLE:
aoqi@0 168 adjusted_offset--;
aoqi@0 169 // fall through
aoqi@0 170
aoqi@0 171 default:
aoqi@0 172 Type *param_type = SharkType::to_stackType(arg_type(i));
aoqi@0 173
aoqi@0 174 param_types.push_back(param_type);
aoqi@0 175 param_values.push_back(
aoqi@0 176 builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));
aoqi@0 177 }
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 // The oopmap is now complete, and everything is written
aoqi@0 181 // into the frame except the PC.
aoqi@0 182 int pc_offset = code_buffer()->create_unique_offset();
aoqi@0 183
aoqi@0 184 _oop_maps = new OopMapSet();
aoqi@0 185 oop_maps()->add_gc_map(pc_offset, oopmap);
aoqi@0 186
aoqi@0 187 builder()->CreateStore(
aoqi@0 188 builder()->code_buffer_address(pc_offset),
aoqi@0 189 stack()->slot_addr(stack()->pc_slot_offset()));
aoqi@0 190
aoqi@0 191 // Set up the Java frame anchor
aoqi@0 192 stack()->CreateSetLastJavaFrame();
aoqi@0 193
aoqi@0 194 // Lock if necessary
aoqi@0 195 if (is_synchronized())
aoqi@0 196 Unimplemented();
aoqi@0 197
aoqi@0 198 // Change the thread state to _thread_in_native
aoqi@0 199 CreateSetThreadState(_thread_in_native);
aoqi@0 200
aoqi@0 201 // Make the call
aoqi@0 202 BasicType result_type = target()->result_type();
aoqi@0 203 Type* return_type;
aoqi@0 204 if (result_type == T_VOID)
aoqi@0 205 return_type = SharkType::void_type();
aoqi@0 206 else if (is_returning_oop())
aoqi@0 207 return_type = box_type;
aoqi@0 208 else
aoqi@0 209 return_type = SharkType::to_arrayType(result_type);
aoqi@0 210 Value* native_function = builder()->CreateIntToPtr(
aoqi@0 211 LLVMValue::intptr_constant((intptr_t) target()->native_function()),
aoqi@0 212 PointerType::getUnqual(
aoqi@0 213 FunctionType::get(return_type, param_types, false)));
aoqi@0 214 Value *result = builder()->CreateCall(
aoqi@0 215 native_function, llvm::makeArrayRef(param_values));
aoqi@0 216
aoqi@0 217 // Start the transition back to _thread_in_Java
aoqi@0 218 CreateSetThreadState(_thread_in_native_trans);
aoqi@0 219
aoqi@0 220 // Make sure new state is visible in the GC thread
aoqi@0 221 if (os::is_MP()) {
aoqi@0 222 if (UseMembar)
aoqi@0 223 builder()->CreateFence(llvm::SequentiallyConsistent, llvm::CrossThread);
aoqi@0 224 else
aoqi@0 225 CreateWriteMemorySerializePage();
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 // Handle safepoint operations, pending suspend requests,
aoqi@0 229 // and pending asynchronous exceptions.
aoqi@0 230 BasicBlock *check_thread = CreateBlock("check_thread");
aoqi@0 231 BasicBlock *do_safepoint = CreateBlock("do_safepoint");
aoqi@0 232 BasicBlock *safepointed = CreateBlock("safepointed");
aoqi@0 233
aoqi@0 234 Value *global_state = builder()->CreateLoad(
aoqi@0 235 builder()->CreateIntToPtr(
aoqi@0 236 LLVMValue::intptr_constant(
aoqi@0 237 (intptr_t) SafepointSynchronize::address_of_state()),
aoqi@0 238 PointerType::getUnqual(SharkType::jint_type())),
aoqi@0 239 "global_state");
aoqi@0 240
aoqi@0 241 builder()->CreateCondBr(
aoqi@0 242 builder()->CreateICmpNE(
aoqi@0 243 global_state,
aoqi@0 244 LLVMValue::jint_constant(SafepointSynchronize::_not_synchronized)),
aoqi@0 245 do_safepoint, check_thread);
aoqi@0 246
aoqi@0 247 builder()->SetInsertPoint(check_thread);
aoqi@0 248 Value *thread_state = builder()->CreateValueOfStructEntry(
aoqi@0 249 thread,
aoqi@0 250 JavaThread::suspend_flags_offset(),
aoqi@0 251 SharkType::jint_type(),
aoqi@0 252 "thread_state");
aoqi@0 253
aoqi@0 254 builder()->CreateCondBr(
aoqi@0 255 builder()->CreateICmpNE(
aoqi@0 256 thread_state,
aoqi@0 257 LLVMValue::jint_constant(0)),
aoqi@0 258 do_safepoint, safepointed);
aoqi@0 259
aoqi@0 260 builder()->SetInsertPoint(do_safepoint);
aoqi@0 261 builder()->CreateCall(
aoqi@0 262 builder()->check_special_condition_for_native_trans(), thread);
aoqi@0 263 builder()->CreateBr(safepointed);
aoqi@0 264
aoqi@0 265 // Finally we can change the thread state to _thread_in_Java
aoqi@0 266 builder()->SetInsertPoint(safepointed);
aoqi@0 267 CreateSetThreadState(_thread_in_Java);
aoqi@0 268
aoqi@0 269 // Clear the frame anchor
aoqi@0 270 stack()->CreateResetLastJavaFrame();
aoqi@0 271
aoqi@0 272 // If there is a pending exception then we can just unwind and
aoqi@0 273 // return. It seems totally wrong that unlocking is skipped here
aoqi@0 274 // but apparently the template interpreter does this so we do too.
aoqi@0 275 BasicBlock *exception = CreateBlock("exception");
aoqi@0 276 BasicBlock *no_exception = CreateBlock("no_exception");
aoqi@0 277
aoqi@0 278 builder()->CreateCondBr(
aoqi@0 279 builder()->CreateICmpEQ(
aoqi@0 280 CreateLoadPendingException(),
aoqi@0 281 LLVMValue::null()),
aoqi@0 282 no_exception, exception);
aoqi@0 283
aoqi@0 284 builder()->SetInsertPoint(exception);
aoqi@0 285 CreateResetHandleBlock();
aoqi@0 286 stack()->CreatePopFrame(0);
aoqi@0 287 builder()->CreateRet(LLVMValue::jint_constant(0));
aoqi@0 288
aoqi@0 289 builder()->SetInsertPoint(no_exception);
aoqi@0 290
aoqi@0 291 // If the result was an oop then unbox it before
aoqi@0 292 // releasing the handle it might be protected by
aoqi@0 293 if (is_returning_oop()) {
aoqi@0 294 BasicBlock *null = builder()->GetInsertBlock();
aoqi@0 295 BasicBlock *not_null = CreateBlock("not_null");
aoqi@0 296 BasicBlock *merge = CreateBlock("merge");
aoqi@0 297
aoqi@0 298 builder()->CreateCondBr(
aoqi@0 299 builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),
aoqi@0 300 not_null, merge);
aoqi@0 301
aoqi@0 302 builder()->SetInsertPoint(not_null);
aoqi@0 303 Value *unboxed_result = builder()->CreateLoad(result);
aoqi@0 304 builder()->CreateBr(merge);
aoqi@0 305
aoqi@0 306 builder()->SetInsertPoint(merge);
aoqi@0 307 PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "result");
aoqi@0 308 phi->addIncoming(LLVMValue::null(), null);
aoqi@0 309 phi->addIncoming(unboxed_result, not_null);
aoqi@0 310 result = phi;
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 // Reset handle block
aoqi@0 314 CreateResetHandleBlock();
aoqi@0 315
aoqi@0 316 // Unlock if necessary.
aoqi@0 317 if (is_synchronized())
aoqi@0 318 Unimplemented();
aoqi@0 319
aoqi@0 320 // Unwind and return
aoqi@0 321 Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);
aoqi@0 322 if (result_type != T_VOID) {
aoqi@0 323 bool needs_cast = false;
aoqi@0 324 bool is_signed = false;
aoqi@0 325 switch (result_type) {
aoqi@0 326 case T_BOOLEAN:
aoqi@0 327 result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));
aoqi@0 328 needs_cast = true;
aoqi@0 329 break;
aoqi@0 330
aoqi@0 331 case T_CHAR:
aoqi@0 332 needs_cast = true;
aoqi@0 333 break;
aoqi@0 334
aoqi@0 335 case T_BYTE:
aoqi@0 336 case T_SHORT:
aoqi@0 337 needs_cast = true;
aoqi@0 338 is_signed = true;
aoqi@0 339 break;
aoqi@0 340 }
aoqi@0 341 if (needs_cast) {
aoqi@0 342 result = builder()->CreateIntCast(
aoqi@0 343 result, SharkType::to_stackType(result_type), is_signed);
aoqi@0 344 }
aoqi@0 345
aoqi@0 346 builder()->CreateStore(
aoqi@0 347 result,
aoqi@0 348 builder()->CreateIntToPtr(
aoqi@0 349 result_addr,
aoqi@0 350 PointerType::getUnqual(SharkType::to_stackType(result_type))));
aoqi@0 351 }
aoqi@0 352 builder()->CreateRet(LLVMValue::jint_constant(0));
aoqi@0 353 }

mercurial