Path: blob/master/src/hotspot/share/opto/generateOptoStub.cpp
40930 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/resourceArea.hpp"26#include "opto/addnode.hpp"27#include "opto/callnode.hpp"28#include "opto/cfgnode.hpp"29#include "opto/compile.hpp"30#include "opto/convertnode.hpp"31#include "opto/locknode.hpp"32#include "opto/memnode.hpp"33#include "opto/mulnode.hpp"34#include "opto/node.hpp"35#include "opto/parse.hpp"36#include "opto/phaseX.hpp"37#include "opto/rootnode.hpp"38#include "opto/runtime.hpp"39#include "opto/type.hpp"40#include "runtime/stubRoutines.hpp"4142//--------------------gen_stub-------------------------------43void GraphKit::gen_stub(address C_function,44const char *name,45int is_fancy_jump,46bool pass_tls,47bool return_pc) {48ResourceMark rm;4950const TypeTuple *jdomain = C->tf()->domain();51const TypeTuple *jrange = C->tf()->range();5253// The procedure start54StartNode* start = new StartNode(root(), jdomain);55_gvn.set_type_bottom(start);5657// Make a map, with JVM state58uint parm_cnt = jdomain->cnt();59uint max_map = MAX2(2*parm_cnt+1, jrange->cnt());60// %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces61assert(SynchronizationEntryBCI == InvocationEntryBci, "");62JVMState* jvms = new (C) JVMState(0);63jvms->set_bci(InvocationEntryBci);64jvms->set_monoff(max_map);65jvms->set_scloff(max_map);66jvms->set_endoff(max_map);67{68SafePointNode *map = new SafePointNode( max_map, jvms );69jvms->set_map(map);70set_jvms(jvms);71assert(map == this->map(), "kit.map is set");72}7374// Make up the parameters75uint i;76for (i = 0; i < parm_cnt; i++) {77map()->init_req(i, _gvn.transform(new ParmNode(start, i)));78}79for ( ; i<map()->req(); i++) {80map()->init_req(i, top()); // For nicer debugging81}8283// GraphKit requires memory to be a MergeMemNode:84set_all_memory(map()->memory());8586// Get base of thread-local storage area87Node* thread = _gvn.transform(new ThreadLocalNode());8889const int NoAlias = Compile::AliasIdxBot;9091Node* adr_last_Java_pc = basic_plus_adr(top(),92thread,93in_bytes(JavaThread::frame_anchor_offset()) +94in_bytes(JavaFrameAnchor::last_Java_pc_offset()));9596// Drop in the last_Java_sp. last_Java_fp is not touched.97// Always do this after the other "last_Java_frame" fields are set since98// as soon as last_Java_sp != NULL the has_last_Java_frame is true and99// users will look at the other fields.100//101Node *adr_sp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_sp_offset()));102Node *last_sp = frameptr();103store_to_memory(control(), adr_sp, last_sp, T_ADDRESS, NoAlias, MemNode::unordered);104105// Set _thread_in_native106// The order of stores into TLS is critical! Setting _thread_in_native MUST107// be last, because a GC is allowed at any time after setting it and the GC108// will require last_Java_pc and last_Java_sp.109110//-----------------------------111// Compute signature for C call. Varies from the Java signature!112113const Type **fields = TypeTuple::fields(2*parm_cnt+2);114uint cnt = TypeFunc::Parms;115// The C routines gets the base of thread-local storage passed in as an116// extra argument. Not all calls need it, but it is cheap to add here.117for (uint pcnt = cnt; pcnt < parm_cnt; pcnt++, cnt++) {118const Type *f = jdomain->field_at(pcnt);119if (CCallingConventionRequiresIntsAsLongs && f->isa_int()) {120fields[cnt++] = TypeLong::LONG;121fields[cnt] = Type::HALF; // Must add an additional half for a long.122} else {123fields[cnt] = f;124}125}126fields[cnt++] = TypeRawPtr::BOTTOM; // Thread-local storage127// Also pass in the caller's PC, if asked for.128if (return_pc) {129fields[cnt++] = TypeRawPtr::BOTTOM; // Return PC130}131const TypeTuple* domain = TypeTuple::make(cnt, fields);132133// The C routine we are about to call cannot return an oop; it can block on134// exit and a GC will trash the oop while it sits in C-land. Instead, we135// return the oop through TLS for runtime calls.136// Also, C routines returning integer subword values leave the high137// order bits dirty; these must be cleaned up by explicit sign extension.138const Type* retval = (jrange->cnt() == TypeFunc::Parms) ? Type::TOP : jrange->field_at(TypeFunc::Parms);139// Make a private copy of jrange->fields();140const Type **rfields = TypeTuple::fields(jrange->cnt() - TypeFunc::Parms);141// Fixup oop returns142int retval_ptr = retval->isa_oop_ptr();143if (retval_ptr) {144assert( pass_tls, "Oop must be returned thru TLS" );145// Fancy-jumps return address; others return void146rfields[TypeFunc::Parms] = is_fancy_jump ? TypeRawPtr::BOTTOM : Type::TOP;147148} else if (retval->isa_int()) { // Returning any integer subtype?149// "Fatten" byte, char & short return types to 'int' to show that150// the native C code can return values with junk high order bits.151// We'll sign-extend it below later.152rfields[TypeFunc::Parms] = TypeInt::INT; // It's "dirty" and needs sign-ext153154} else if (jrange->cnt() >= TypeFunc::Parms+1) { // Else copy other types155rfields[TypeFunc::Parms] = jrange->field_at(TypeFunc::Parms);156if (jrange->cnt() == TypeFunc::Parms+2) {157rfields[TypeFunc::Parms+1] = jrange->field_at(TypeFunc::Parms+1);158}159}160const TypeTuple* range = TypeTuple::make(jrange->cnt(), rfields);161162// Final C signature163const TypeFunc *c_sig = TypeFunc::make(domain, range);164165//-----------------------------166// Make the call node.167CallRuntimeNode* call = new CallRuntimeNode(c_sig, C_function, name, TypePtr::BOTTOM, new (C) JVMState(0));168//-----------------------------169170// Fix-up the debug info for the call.171call->jvms()->set_bci(0);172call->jvms()->set_offsets(cnt);173174// Set fixed predefined input arguments.175cnt = 0;176for (i = 0; i < TypeFunc::Parms; i++) {177call->init_req(cnt++, map()->in(i));178}179// A little too aggressive on the parm copy; return address is not an input.180call->set_req(TypeFunc::ReturnAdr, top());181for (; i < parm_cnt; i++) { // Regular input arguments.182const Type *f = jdomain->field_at(i);183if (CCallingConventionRequiresIntsAsLongs && f->isa_int()) {184call->init_req(cnt++, _gvn.transform(new ConvI2LNode(map()->in(i))));185call->init_req(cnt++, top());186} else {187call->init_req(cnt++, map()->in(i));188}189}190call->init_req(cnt++, thread);191if (return_pc) { // Return PC, if asked for.192call->init_req(cnt++, returnadr());193}194195_gvn.transform_no_reclaim(call);196197//-----------------------------198// Now set up the return results199set_control( _gvn.transform( new ProjNode(call,TypeFunc::Control)) );200set_i_o( _gvn.transform( new ProjNode(call,TypeFunc::I_O )) );201set_all_memory_call(call);202if (range->cnt() > TypeFunc::Parms) {203Node* retnode = _gvn.transform( new ProjNode(call,TypeFunc::Parms) );204// C-land is allowed to return sub-word values. Convert to integer type.205assert( retval != Type::TOP, "" );206if (retval == TypeInt::BOOL) {207retnode = _gvn.transform( new AndINode(retnode, intcon(0xFF)) );208} else if (retval == TypeInt::CHAR) {209retnode = _gvn.transform( new AndINode(retnode, intcon(0xFFFF)) );210} else if (retval == TypeInt::BYTE) {211retnode = _gvn.transform( new LShiftINode(retnode, intcon(24)) );212retnode = _gvn.transform( new RShiftINode(retnode, intcon(24)) );213} else if (retval == TypeInt::SHORT) {214retnode = _gvn.transform( new LShiftINode(retnode, intcon(16)) );215retnode = _gvn.transform( new RShiftINode(retnode, intcon(16)) );216}217map()->set_req( TypeFunc::Parms, retnode );218}219220//-----------------------------221222// Clear last_Java_sp223store_to_memory(control(), adr_sp, null(), T_ADDRESS, NoAlias, MemNode::unordered);224// Clear last_Java_pc225store_to_memory(control(), adr_last_Java_pc, null(), T_ADDRESS, NoAlias, MemNode::unordered);226#if (defined(IA64) && !defined(AIX))227Node* adr_last_Java_fp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_fp_offset()));228store_to_memory(control(), adr_last_Java_fp, null(), T_ADDRESS, NoAlias, MemNode::unordered);229#endif230231// For is-fancy-jump, the C-return value is also the branch target232Node* target = map()->in(TypeFunc::Parms);233// Runtime call returning oop in TLS? Fetch it out234if( pass_tls ) {235Node* adr = basic_plus_adr(top(), thread, in_bytes(JavaThread::vm_result_offset()));236Node* vm_result = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);237map()->set_req(TypeFunc::Parms, vm_result); // vm_result passed as result238// clear thread-local-storage(tls)239store_to_memory(control(), adr, null(), T_ADDRESS, NoAlias, MemNode::unordered);240}241242//-----------------------------243// check exception244Node* adr = basic_plus_adr(top(), thread, in_bytes(Thread::pending_exception_offset()));245Node* pending = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);246247Node* exit_memory = reset_memory();248249Node* cmp = _gvn.transform( new CmpPNode(pending, null()) );250Node* bo = _gvn.transform( new BoolNode(cmp, BoolTest::ne) );251IfNode *iff = create_and_map_if(control(), bo, PROB_MIN, COUNT_UNKNOWN);252253Node* if_null = _gvn.transform( new IfFalseNode(iff) );254Node* if_not_null = _gvn.transform( new IfTrueNode(iff) );255256assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");257Node *exc_target = makecon(TypeRawPtr::make( StubRoutines::forward_exception_entry() ));258Node *to_exc = new TailCallNode(if_not_null,259i_o(),260exit_memory,261frameptr(),262returnadr(),263exc_target, null());264root()->add_req(_gvn.transform(to_exc)); // bind to root to keep live265C->init_start(start);266267//-----------------------------268// If this is a normal subroutine return, issue the return and be done.269Node *ret = NULL;270switch( is_fancy_jump ) {271case 0: // Make a return instruction272// Return to caller, free any space for return address273ret = new ReturnNode(TypeFunc::Parms, if_null,274i_o(),275exit_memory,276frameptr(),277returnadr());278if (C->tf()->range()->cnt() > TypeFunc::Parms)279ret->add_req( map()->in(TypeFunc::Parms) );280break;281case 1: // This is a fancy tail-call jump. Jump to computed address.282// Jump to new callee; leave old return address alone.283ret = new TailCallNode(if_null,284i_o(),285exit_memory,286frameptr(),287returnadr(),288target, map()->in(TypeFunc::Parms));289break;290case 2: // Pop return address & jump291// Throw away old return address; jump to new computed address292//assert(C_function == CAST_FROM_FN_PTR(address, OptoRuntime::rethrow_C), "fancy_jump==2 only for rethrow");293ret = new TailJumpNode(if_null,294i_o(),295exit_memory,296frameptr(),297target, map()->in(TypeFunc::Parms));298break;299default:300ShouldNotReachHere();301}302root()->add_req(_gvn.transform(ret));303}304305306