Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/shark/sharkNativeWrapper.cpp
32285 views
/*1* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.2* Copyright 2009, 2010 Red Hat, Inc.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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "shark/llvmHeaders.hpp"27#include "shark/sharkNativeWrapper.hpp"28#include "shark/sharkType.hpp"2930using namespace llvm;3132void SharkNativeWrapper::initialize(const char *name) {33// Create the function34_function = Function::Create(35SharkType::entry_point_type(),36GlobalVariable::InternalLinkage,37name);3839// Get our arguments40Function::arg_iterator ai = function()->arg_begin();41Argument *method = ai++;42method->setName("method");43Argument *base_pc = ai++;44base_pc->setName("base_pc");45code_buffer()->set_base_pc(base_pc);46Argument *thread = ai++;47thread->setName("thread");48set_thread(thread);4950// Create and push our stack frame51builder()->SetInsertPoint(CreateBlock());52#error Needs to be updated for tagged jweak; see JNIHandles.53_stack = SharkStack::CreateBuildAndPushFrame(this, method);54NOT_PRODUCT(method = NULL);5556// Create the oopmap. We use the one oopmap for every call site in57// the wrapper, which results in the odd mild inefficiency but is a58// damn sight easier to code.59OopMap *oopmap = new OopMap(60SharkStack::oopmap_slot_munge(stack()->oopmap_frame_size()),61SharkStack::oopmap_slot_munge(arg_size()));6263// Set up the oop_tmp slot if required:64// - For static methods we use it to handlize the class argument65// for the call, and to protect the same during slow path locks66// (if synchronized).67// - For methods returning oops, we use it to protect the return68// value across safepoints or slow path unlocking.69if (is_static() || is_returning_oop()) {70_oop_tmp_slot = stack()->slot_addr(71stack()->oop_tmp_slot_offset(),72SharkType::oop_type(),73"oop_tmp_slot");7475oopmap->set_oop(SharkStack::slot2reg(stack()->oop_tmp_slot_offset()));76}7778// Set up the monitor slot, for synchronized methods79if (is_synchronized()) {80Unimplemented();81_lock_slot_offset = 23;82}8384// Start building the argument list85std::vector<Type*> param_types;86std::vector<Value*> param_values;87PointerType *box_type = PointerType::getUnqual(SharkType::oop_type());8889// First argument is the JNIEnv90param_types.push_back(SharkType::jniEnv_type());91param_values.push_back(92builder()->CreateAddressOfStructEntry(93thread,94JavaThread::jni_environment_offset(),95SharkType::jniEnv_type(),96"jni_environment"));9798// For static methods, the second argument is the class99if (is_static()) {100builder()->CreateStore(101builder()->CreateInlineOop(102JNIHandles::make_local(103target()->method_holder()->java_mirror())),104oop_tmp_slot());105106param_types.push_back(box_type);107param_values.push_back(oop_tmp_slot());108109_receiver_slot_offset = stack()->oop_tmp_slot_offset();110}111else if (is_returning_oop()) {112// The oop_tmp slot is registered in the oopmap,113// so we need to clear it. This is one of the114// mild inefficiencies I mentioned earlier.115builder()->CreateStore(LLVMValue::null(), oop_tmp_slot());116}117118// Parse the arguments119for (int i = 0; i < arg_size(); i++) {120int slot_offset = stack()->locals_slots_offset() + arg_size() - 1 - i;121int adjusted_offset = slot_offset;122BasicBlock *null, *not_null, *merge;123Value *box;124PHINode *phi;125126switch (arg_type(i)) {127case T_VOID:128break;129130case T_OBJECT:131case T_ARRAY:132null = CreateBlock("null");133not_null = CreateBlock("not_null");134merge = CreateBlock("merge");135136box = stack()->slot_addr(slot_offset, SharkType::oop_type());137builder()->CreateCondBr(138builder()->CreateICmp(139ICmpInst::ICMP_EQ,140builder()->CreateLoad(box),141LLVMValue::null()),142null, not_null);143144builder()->SetInsertPoint(null);145builder()->CreateBr(merge);146147builder()->SetInsertPoint(not_null);148builder()->CreateBr(merge);149150builder()->SetInsertPoint(merge);151phi = builder()->CreatePHI(box_type, 0, "boxed_object");152phi->addIncoming(ConstantPointerNull::get(box_type), null);153phi->addIncoming(box, not_null);154box = phi;155156param_types.push_back(box_type);157param_values.push_back(box);158159oopmap->set_oop(SharkStack::slot2reg(slot_offset));160161if (i == 0 && !is_static())162_receiver_slot_offset = slot_offset;163164break;165166case T_LONG:167case T_DOUBLE:168adjusted_offset--;169// fall through170171default:172Type *param_type = SharkType::to_stackType(arg_type(i));173174param_types.push_back(param_type);175param_values.push_back(176builder()->CreateLoad(stack()->slot_addr(adjusted_offset, param_type)));177}178}179180// The oopmap is now complete, and everything is written181// into the frame except the PC.182int pc_offset = code_buffer()->create_unique_offset();183184_oop_maps = new OopMapSet();185oop_maps()->add_gc_map(pc_offset, oopmap);186187builder()->CreateStore(188builder()->code_buffer_address(pc_offset),189stack()->slot_addr(stack()->pc_slot_offset()));190191// Set up the Java frame anchor192stack()->CreateSetLastJavaFrame();193194// Lock if necessary195if (is_synchronized())196Unimplemented();197198// Change the thread state to _thread_in_native199CreateSetThreadState(_thread_in_native);200201// Make the call202BasicType result_type = target()->result_type();203Type* return_type;204if (result_type == T_VOID)205return_type = SharkType::void_type();206else if (is_returning_oop())207return_type = box_type;208else209return_type = SharkType::to_arrayType(result_type);210Value* native_function = builder()->CreateIntToPtr(211LLVMValue::intptr_constant((intptr_t) target()->native_function()),212PointerType::getUnqual(213FunctionType::get(return_type, param_types, false)));214Value *result = builder()->CreateCall(215native_function, llvm::makeArrayRef(param_values));216217// Start the transition back to _thread_in_Java218CreateSetThreadState(_thread_in_native_trans);219220// Make sure new state is visible in the GC thread221if (os::is_MP()) {222if (UseMembar)223builder()->CreateFence(llvm::SequentiallyConsistent, llvm::CrossThread);224else225CreateWriteMemorySerializePage();226}227228// Handle safepoint operations, pending suspend requests,229// and pending asynchronous exceptions.230BasicBlock *check_thread = CreateBlock("check_thread");231BasicBlock *do_safepoint = CreateBlock("do_safepoint");232BasicBlock *safepointed = CreateBlock("safepointed");233234Value *global_state = builder()->CreateLoad(235builder()->CreateIntToPtr(236LLVMValue::intptr_constant(237(intptr_t) SafepointSynchronize::address_of_state()),238PointerType::getUnqual(SharkType::jint_type())),239"global_state");240241builder()->CreateCondBr(242builder()->CreateICmpNE(243global_state,244LLVMValue::jint_constant(SafepointSynchronize::_not_synchronized)),245do_safepoint, check_thread);246247builder()->SetInsertPoint(check_thread);248Value *thread_state = builder()->CreateValueOfStructEntry(249thread,250JavaThread::suspend_flags_offset(),251SharkType::jint_type(),252"thread_state");253254builder()->CreateCondBr(255builder()->CreateICmpNE(256thread_state,257LLVMValue::jint_constant(0)),258do_safepoint, safepointed);259260builder()->SetInsertPoint(do_safepoint);261builder()->CreateCall(262builder()->check_special_condition_for_native_trans(), thread);263builder()->CreateBr(safepointed);264265// Finally we can change the thread state to _thread_in_Java266builder()->SetInsertPoint(safepointed);267CreateSetThreadState(_thread_in_Java);268269// Clear the frame anchor270stack()->CreateResetLastJavaFrame();271272// If there is a pending exception then we can just unwind and273// return. It seems totally wrong that unlocking is skipped here274// but apparently the template interpreter does this so we do too.275BasicBlock *exception = CreateBlock("exception");276BasicBlock *no_exception = CreateBlock("no_exception");277278builder()->CreateCondBr(279builder()->CreateICmpEQ(280CreateLoadPendingException(),281LLVMValue::null()),282no_exception, exception);283284builder()->SetInsertPoint(exception);285CreateResetHandleBlock();286stack()->CreatePopFrame(0);287builder()->CreateRet(LLVMValue::jint_constant(0));288289builder()->SetInsertPoint(no_exception);290291// If the result was an oop then unbox it before292// releasing the handle it might be protected by293if (is_returning_oop()) {294BasicBlock *null = builder()->GetInsertBlock();295BasicBlock *not_null = CreateBlock("not_null");296BasicBlock *merge = CreateBlock("merge");297298builder()->CreateCondBr(299builder()->CreateICmpNE(result, ConstantPointerNull::get(box_type)),300not_null, merge);301302builder()->SetInsertPoint(not_null);303Value *unboxed_result = builder()->CreateLoad(result);304builder()->CreateBr(merge);305306builder()->SetInsertPoint(merge);307PHINode *phi = builder()->CreatePHI(SharkType::oop_type(), 0, "result");308phi->addIncoming(LLVMValue::null(), null);309phi->addIncoming(unboxed_result, not_null);310result = phi;311}312313// Reset handle block314CreateResetHandleBlock();315316// Unlock if necessary.317if (is_synchronized())318Unimplemented();319320// Unwind and return321Value *result_addr = stack()->CreatePopFrame(type2size[result_type]);322if (result_type != T_VOID) {323bool needs_cast = false;324bool is_signed = false;325switch (result_type) {326case T_BOOLEAN:327result = builder()->CreateICmpNE(result, LLVMValue::jbyte_constant(0));328needs_cast = true;329break;330331case T_CHAR:332needs_cast = true;333break;334335case T_BYTE:336case T_SHORT:337needs_cast = true;338is_signed = true;339break;340}341if (needs_cast) {342result = builder()->CreateIntCast(343result, SharkType::to_stackType(result_type), is_signed);344}345346builder()->CreateStore(347result,348builder()->CreateIntToPtr(349result_addr,350PointerType::getUnqual(SharkType::to_stackType(result_type))));351}352builder()->CreateRet(LLVMValue::jint_constant(0));353}354355356