src/share/vm/shark/sharkNativeWrapper.cpp

Wed, 11 Aug 2010 05:51:21 -0700

author
twisti
date
Wed, 11 Aug 2010 05:51:21 -0700
changeset 2047
d2ede61b7a12
child 2314
f95d63e2154a
permissions
-rw-r--r--

6976186: integrate Shark HotSpot changes
Summary: Shark is a JIT compiler for Zero that uses the LLVM compiler infrastructure.
Reviewed-by: kvn, twisti
Contributed-by: Gary Benson <gbenson@redhat.com>

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

mercurial