Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/generateOptoStub.cpp
32285 views
/*1* Copyright (c) 1999, 2013, 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 "opto/addnode.hpp"26#include "opto/callnode.hpp"27#include "opto/cfgnode.hpp"28#include "opto/compile.hpp"29#include "opto/connode.hpp"30#include "opto/locknode.hpp"31#include "opto/memnode.hpp"32#include "opto/mulnode.hpp"33#include "opto/node.hpp"34#include "opto/parse.hpp"35#include "opto/phaseX.hpp"36#include "opto/rootnode.hpp"37#include "opto/runtime.hpp"38#include "opto/type.hpp"3940//--------------------gen_stub-------------------------------41void GraphKit::gen_stub(address C_function,42const char *name,43int is_fancy_jump,44bool pass_tls,45bool return_pc) {46ResourceMark rm;4748const TypeTuple *jdomain = C->tf()->domain();49const TypeTuple *jrange = C->tf()->range();5051// The procedure start52StartNode* start = new (C) StartNode(root(), jdomain);53_gvn.set_type_bottom(start);5455// Make a map, with JVM state56uint parm_cnt = jdomain->cnt();57uint max_map = MAX2(2*parm_cnt+1, jrange->cnt());58// %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces59assert(SynchronizationEntryBCI == InvocationEntryBci, "");60JVMState* jvms = new (C) JVMState(0);61jvms->set_bci(InvocationEntryBci);62jvms->set_monoff(max_map);63jvms->set_scloff(max_map);64jvms->set_endoff(max_map);65{66SafePointNode *map = new (C) SafePointNode( max_map, jvms );67jvms->set_map(map);68set_jvms(jvms);69assert(map == this->map(), "kit.map is set");70}7172// Make up the parameters73uint i;74for( i = 0; i < parm_cnt; i++ )75map()->init_req(i, _gvn.transform(new (C) ParmNode(start, i)));76for( ; i<map()->req(); i++ )77map()->init_req(i, top()); // For nicer debugging7879// GraphKit requires memory to be a MergeMemNode:80set_all_memory(map()->memory());8182// Get base of thread-local storage area83Node* thread = _gvn.transform( new (C) ThreadLocalNode() );8485const int NoAlias = Compile::AliasIdxBot;8687Node* adr_last_Java_pc = basic_plus_adr(top(),88thread,89in_bytes(JavaThread::frame_anchor_offset()) +90in_bytes(JavaFrameAnchor::last_Java_pc_offset()));91#if defined(SPARC)92Node* adr_flags = basic_plus_adr(top(),93thread,94in_bytes(JavaThread::frame_anchor_offset()) +95in_bytes(JavaFrameAnchor::flags_offset()));96#endif /* defined(SPARC) */979899// Drop in the last_Java_sp. last_Java_fp is not touched.100// Always do this after the other "last_Java_frame" fields are set since101// as soon as last_Java_sp != NULL the has_last_Java_frame is true and102// users will look at the other fields.103//104Node *adr_sp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_sp_offset()));105Node *last_sp = basic_plus_adr(top(), frameptr(), (intptr_t) STACK_BIAS);106store_to_memory(NULL, adr_sp, last_sp, T_ADDRESS, NoAlias, MemNode::unordered);107108// Set _thread_in_native109// The order of stores into TLS is critical! Setting _thread_in_native MUST110// be last, because a GC is allowed at any time after setting it and the GC111// will require last_Java_pc and last_Java_sp.112113//-----------------------------114// Compute signature for C call. Varies from the Java signature!115const Type **fields = TypeTuple::fields(2*parm_cnt+2);116uint cnt = TypeFunc::Parms;117// The C routines gets the base of thread-local storage passed in as an118// extra argument. Not all calls need it, but its cheap to add here.119for (uint pcnt = cnt; pcnt < parm_cnt; pcnt++, cnt++) {120// Convert ints to longs if required.121if (CCallingConventionRequiresIntsAsLongs && jdomain->field_at(pcnt)->isa_int()) {122fields[cnt++] = TypeLong::LONG;123fields[cnt] = Type::HALF; // must add an additional half for a long124} else {125fields[cnt] = jdomain->field_at(pcnt);126}127}128129fields[cnt++] = TypeRawPtr::BOTTOM; // Thread-local storage130// Also pass in the caller's PC, if asked for.131if( return_pc )132fields[cnt++] = TypeRawPtr::BOTTOM; // Return PC133134const TypeTuple* domain = TypeTuple::make(cnt,fields);135// The C routine we are about to call cannot return an oop; it can block on136// exit and a GC will trash the oop while it sits in C-land. Instead, we137// return the oop through TLS for runtime calls.138// Also, C routines returning integer subword values leave the high139// order bits dirty; these must be cleaned up by explicit sign extension.140const Type* retval = (jrange->cnt() == TypeFunc::Parms) ? Type::TOP : jrange->field_at(TypeFunc::Parms);141// Make a private copy of jrange->fields();142const Type **rfields = TypeTuple::fields(jrange->cnt() - TypeFunc::Parms);143// Fixup oop returns144int retval_ptr = retval->isa_oop_ptr();145if( retval_ptr ) {146assert( pass_tls, "Oop must be returned thru TLS" );147// Fancy-jumps return address; others return void148rfields[TypeFunc::Parms] = is_fancy_jump ? TypeRawPtr::BOTTOM : Type::TOP;149150} else if( retval->isa_int() ) { // Returning any integer subtype?151// "Fatten" byte, char & short return types to 'int' to show that152// the native C code can return values with junk high order bits.153// We'll sign-extend it below later.154rfields[TypeFunc::Parms] = TypeInt::INT; // It's "dirty" and needs sign-ext155156} else if( jrange->cnt() >= TypeFunc::Parms+1 ) { // Else copy other types157rfields[TypeFunc::Parms] = jrange->field_at(TypeFunc::Parms);158if( jrange->cnt() == TypeFunc::Parms+2 )159rfields[TypeFunc::Parms+1] = jrange->field_at(TypeFunc::Parms+1);160}161const TypeTuple* range = TypeTuple::make(jrange->cnt(),rfields);162163// Final C signature164const TypeFunc *c_sig = TypeFunc::make(domain,range);165166//-----------------------------167// Make the call node168CallRuntimeNode *call = new (C)169CallRuntimeNode(c_sig, C_function, name, TypePtr::BOTTOM);170//-----------------------------171172// Fix-up the debug info for the call173call->set_jvms( new (C) JVMState(0) );174call->jvms()->set_bci(0);175call->jvms()->set_offsets(cnt);176177// Set fixed predefined input arguments178cnt = 0;179for (i = 0; i < TypeFunc::Parms; i++)180call->init_req(cnt++, map()->in(i));181// A little too aggressive on the parm copy; return address is not an input182call->set_req(TypeFunc::ReturnAdr, top());183for (; i < parm_cnt; i++) { // Regular input arguments184// Convert ints to longs if required.185if (CCallingConventionRequiresIntsAsLongs && jdomain->field_at(i)->isa_int()) {186Node* int_as_long = _gvn.transform(new (C) ConvI2LNode(map()->in(i)));187call->init_req(cnt++, int_as_long); // long188call->init_req(cnt++, top()); // half189} else {190call->init_req(cnt++, map()->in(i));191}192}193194call->init_req( cnt++, thread );195if( return_pc ) // Return PC, if asked for196call->init_req( cnt++, returnadr() );197_gvn.transform_no_reclaim(call);198199200//-----------------------------201// Now set up the return results202set_control( _gvn.transform( new (C) ProjNode(call,TypeFunc::Control)) );203set_i_o( _gvn.transform( new (C) ProjNode(call,TypeFunc::I_O )) );204set_all_memory_call(call);205if (range->cnt() > TypeFunc::Parms) {206Node* retnode = _gvn.transform( new (C) ProjNode(call,TypeFunc::Parms) );207// C-land is allowed to return sub-word values. Convert to integer type.208assert( retval != Type::TOP, "" );209if (retval == TypeInt::BOOL) {210retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFF)) );211} else if (retval == TypeInt::CHAR) {212retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFFFF)) );213} else if (retval == TypeInt::BYTE) {214retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(24)) );215retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(24)) );216} else if (retval == TypeInt::SHORT) {217retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(16)) );218retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(16)) );219}220map()->set_req( TypeFunc::Parms, retnode );221}222223//-----------------------------224225// Clear last_Java_sp226store_to_memory(NULL, adr_sp, null(), T_ADDRESS, NoAlias, MemNode::unordered);227// Clear last_Java_pc and (optionally)_flags228store_to_memory(NULL, adr_last_Java_pc, null(), T_ADDRESS, NoAlias, MemNode::unordered);229#if defined(SPARC)230store_to_memory(NULL, adr_flags, intcon(0), T_INT, NoAlias, MemNode::unordered);231#endif /* defined(SPARC) */232#if (defined(IA64) && !defined(AIX))233Node* adr_last_Java_fp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_fp_offset()));234store_to_memory(NULL, adr_last_Java_fp, null(), T_ADDRESS, NoAlias, MemNode::unordered);235#endif236237// For is-fancy-jump, the C-return value is also the branch target238Node* target = map()->in(TypeFunc::Parms);239// Runtime call returning oop in TLS? Fetch it out240if( pass_tls ) {241Node* adr = basic_plus_adr(top(), thread, in_bytes(JavaThread::vm_result_offset()));242Node* vm_result = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);243map()->set_req(TypeFunc::Parms, vm_result); // vm_result passed as result244// clear thread-local-storage(tls)245store_to_memory(NULL, adr, null(), T_ADDRESS, NoAlias, MemNode::unordered);246}247248//-----------------------------249// check exception250Node* adr = basic_plus_adr(top(), thread, in_bytes(Thread::pending_exception_offset()));251Node* pending = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);252253Node* exit_memory = reset_memory();254255Node* cmp = _gvn.transform( new (C) CmpPNode(pending, null()) );256Node* bo = _gvn.transform( new (C) BoolNode(cmp, BoolTest::ne) );257IfNode *iff = create_and_map_if(control(), bo, PROB_MIN, COUNT_UNKNOWN);258259Node* if_null = _gvn.transform( new (C) IfFalseNode(iff) );260Node* if_not_null = _gvn.transform( new (C) IfTrueNode(iff) );261262assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");263Node *exc_target = makecon(TypeRawPtr::make( StubRoutines::forward_exception_entry() ));264Node *to_exc = new (C) TailCallNode(if_not_null,265i_o(),266exit_memory,267frameptr(),268returnadr(),269exc_target, null());270root()->add_req(_gvn.transform(to_exc)); // bind to root to keep live271C->init_start(start);272273//-----------------------------274// If this is a normal subroutine return, issue the return and be done.275Node *ret = NULL;276switch( is_fancy_jump ) {277case 0: // Make a return instruction278// Return to caller, free any space for return address279ret = new (C) ReturnNode(TypeFunc::Parms, if_null,280i_o(),281exit_memory,282frameptr(),283returnadr());284if (C->tf()->range()->cnt() > TypeFunc::Parms)285ret->add_req( map()->in(TypeFunc::Parms) );286break;287case 1: // This is a fancy tail-call jump. Jump to computed address.288// Jump to new callee; leave old return address alone.289ret = new (C) TailCallNode(if_null,290i_o(),291exit_memory,292frameptr(),293returnadr(),294target, map()->in(TypeFunc::Parms));295break;296case 2: // Pop return address & jump297// Throw away old return address; jump to new computed address298//assert(C_function == CAST_FROM_FN_PTR(address, OptoRuntime::rethrow_C), "fancy_jump==2 only for rethrow");299ret = new (C) TailJumpNode(if_null,300i_o(),301exit_memory,302frameptr(),303target, map()->in(TypeFunc::Parms));304break;305default:306ShouldNotReachHere();307}308root()->add_req(_gvn.transform(ret));309}310311312