generateOptoStub.cpp revision 6010:abec000618bf
125603Skjc/*
255205Speter * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
325603Skjc * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
425603Skjc *
525603Skjc * This code is free software; you can redistribute it and/or modify it
625603Skjc * under the terms of the GNU General Public License version 2 only, as
725603Skjc * published by the Free Software Foundation.
825603Skjc *
925603Skjc * This code is distributed in the hope that it will be useful, but WITHOUT
1025603Skjc * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1125603Skjc * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1225603Skjc * version 2 for more details (a copy is included in the LICENSE file that
1325603Skjc * accompanied this code).
1425603Skjc *
1525603Skjc * You should have received a copy of the GNU General Public License version
1625603Skjc * 2 along with this work; if not, write to the Free Software Foundation,
1725603Skjc * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1825603Skjc *
1925603Skjc * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2025603Skjc * or visit www.oracle.com if you need additional information or have any
2125603Skjc * questions.
2225603Skjc *
2325603Skjc */
2425603Skjc
2525603Skjc#include "precompiled.hpp"
2625603Skjc#include "opto/addnode.hpp"
2725603Skjc#include "opto/callnode.hpp"
2825603Skjc#include "opto/cfgnode.hpp"
2925603Skjc#include "opto/compile.hpp"
3025603Skjc#include "opto/connode.hpp"
3125603Skjc#include "opto/locknode.hpp"
3225603Skjc#include "opto/memnode.hpp"
3325603Skjc#include "opto/mulnode.hpp"
3425603Skjc#include "opto/node.hpp"
3525603Skjc#include "opto/parse.hpp"
3625603Skjc#include "opto/phaseX.hpp"
3725603Skjc#include "opto/rootnode.hpp"
3825603Skjc#include "opto/runtime.hpp"
3925603Skjc#include "opto/type.hpp"
40114739Sharti
41114739Sharti//--------------------gen_stub-------------------------------
42114739Shartivoid GraphKit::gen_stub(address C_function,
43114739Sharti                        const char *name,
44114739Sharti                        int is_fancy_jump,
45114739Sharti                        bool pass_tls,
46114739Sharti                        bool return_pc) {
47114739Sharti  ResourceMark rm;
48114739Sharti
49116441Sharti  const TypeTuple *jdomain = C->tf()->domain();
50116441Sharti  const TypeTuple *jrange  = C->tf()->range();
51116441Sharti
52116441Sharti  // The procedure start
53114739Sharti  StartNode* start = new (C) StartNode(root(), jdomain);
54114739Sharti  _gvn.set_type_bottom(start);
55116441Sharti
56116441Sharti  // Make a map, with JVM state
57116441Sharti  uint parm_cnt = jdomain->cnt();
58116441Sharti  uint max_map = MAX2(2*parm_cnt+1, jrange->cnt());
59116441Sharti  // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
60116441Sharti  assert(SynchronizationEntryBCI == InvocationEntryBci, "");
61116441Sharti  JVMState* jvms = new (C) JVMState(0);
62116441Sharti  jvms->set_bci(InvocationEntryBci);
63116441Sharti  jvms->set_monoff(max_map);
64116441Sharti  jvms->set_scloff(max_map);
65116441Sharti  jvms->set_endoff(max_map);
66114739Sharti  {
67114739Sharti    SafePointNode *map = new (C) SafePointNode( max_map, jvms );
68114739Sharti    jvms->set_map(map);
69114739Sharti    set_jvms(jvms);
70114739Sharti    assert(map == this->map(), "kit.map is set");
71114739Sharti  }
72114739Sharti
73114739Sharti  // Make up the parameters
74114739Sharti  uint i;
75114739Sharti  for( i = 0; i < parm_cnt; i++ )
76114739Sharti    map()->init_req(i, _gvn.transform(new (C) ParmNode(start, i)));
77114739Sharti  for( ; i<map()->req(); i++ )
78114739Sharti    map()->init_req(i, top());      // For nicer debugging
79114739Sharti
80114739Sharti  // GraphKit requires memory to be a MergeMemNode:
81114739Sharti  set_all_memory(map()->memory());
82114739Sharti
83114739Sharti  // Get base of thread-local storage area
84114739Sharti  Node* thread = _gvn.transform( new (C) ThreadLocalNode() );
85114739Sharti
86114739Sharti  const int NoAlias = Compile::AliasIdxBot;
87114739Sharti
88116441Sharti  Node* adr_last_Java_pc = basic_plus_adr(top(),
89116441Sharti                                            thread,
90116441Sharti                                            in_bytes(JavaThread::frame_anchor_offset()) +
91116441Sharti                                            in_bytes(JavaFrameAnchor::last_Java_pc_offset()));
92116441Sharti#if defined(SPARC)
93116441Sharti  Node* adr_flags = basic_plus_adr(top(),
94116441Sharti                                   thread,
95116441Sharti                                   in_bytes(JavaThread::frame_anchor_offset()) +
96116441Sharti                                   in_bytes(JavaFrameAnchor::flags_offset()));
97116441Sharti#endif /* defined(SPARC) */
98116441Sharti
99116441Sharti
100116441Sharti  // Drop in the last_Java_sp.  last_Java_fp is not touched.
101116441Sharti  // Always do this after the other "last_Java_frame" fields are set since
102116441Sharti  // as soon as last_Java_sp != NULL the has_last_Java_frame is true and
103116441Sharti  // users will look at the other fields.
104116441Sharti  //
105116441Sharti  Node *adr_sp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_sp_offset()));
106116441Sharti  Node *last_sp = basic_plus_adr(top(), frameptr(), (intptr_t) STACK_BIAS);
107116441Sharti  store_to_memory(NULL, adr_sp, last_sp, T_ADDRESS, NoAlias, MemNode::unordered);
108116441Sharti
109116441Sharti  // Set _thread_in_native
110116441Sharti  // The order of stores into TLS is critical!  Setting _thread_in_native MUST
111116441Sharti  // be last, because a GC is allowed at any time after setting it and the GC
112116441Sharti  // will require last_Java_pc and last_Java_sp.
113116441Sharti
114116441Sharti  //-----------------------------
115116441Sharti  // Compute signature for C call.  Varies from the Java signature!
116116441Sharti  const Type **fields = TypeTuple::fields(2*parm_cnt+2);
117116441Sharti  uint cnt = TypeFunc::Parms;
118116441Sharti  // The C routines gets the base of thread-local storage passed in as an
119116441Sharti  // extra argument.  Not all calls need it, but its cheap to add here.
120116441Sharti  for (uint pcnt = cnt; pcnt < parm_cnt; pcnt++, cnt++) {
121116441Sharti    // Convert ints to longs if required.
122116441Sharti    if (CCallingConventionRequiresIntsAsLongs && jdomain->field_at(pcnt)->isa_int()) {
123116441Sharti      fields[cnt++] = TypeLong::LONG;
124116441Sharti      fields[cnt]   = Type::HALF; // must add an additional half for a long
125116441Sharti    } else {
126116441Sharti      fields[cnt] = jdomain->field_at(pcnt);
127116441Sharti    }
128116441Sharti  }
129116441Sharti
130116441Sharti  fields[cnt++] = TypeRawPtr::BOTTOM; // Thread-local storage
131116441Sharti  // Also pass in the caller's PC, if asked for.
132116441Sharti  if( return_pc )
133116441Sharti    fields[cnt++] = TypeRawPtr::BOTTOM; // Return PC
134116441Sharti
135116441Sharti  const TypeTuple* domain = TypeTuple::make(cnt,fields);
136116441Sharti  // The C routine we are about to call cannot return an oop; it can block on
137116441Sharti  // exit and a GC will trash the oop while it sits in C-land.  Instead, we
138116441Sharti  // return the oop through TLS for runtime calls.
139116441Sharti  // Also, C routines returning integer subword values leave the high
140116441Sharti  // order bits dirty; these must be cleaned up by explicit sign extension.
141116441Sharti  const Type* retval = (jrange->cnt() == TypeFunc::Parms) ? Type::TOP : jrange->field_at(TypeFunc::Parms);
142116441Sharti  // Make a private copy of jrange->fields();
143116441Sharti  const Type **rfields = TypeTuple::fields(jrange->cnt() - TypeFunc::Parms);
144116441Sharti  // Fixup oop returns
145116441Sharti  int retval_ptr = retval->isa_oop_ptr();
146116441Sharti  if( retval_ptr ) {
147116441Sharti    assert( pass_tls, "Oop must be returned thru TLS" );
148116441Sharti    // Fancy-jumps return address; others return void
149116441Sharti    rfields[TypeFunc::Parms] = is_fancy_jump ? TypeRawPtr::BOTTOM : Type::TOP;
150116441Sharti
151116441Sharti  } else if( retval->isa_int() ) { // Returning any integer subtype?
152116441Sharti    // "Fatten" byte, char & short return types to 'int' to show that
153116441Sharti    // the native C code can return values with junk high order bits.
154116441Sharti    // We'll sign-extend it below later.
155116441Sharti    rfields[TypeFunc::Parms] = TypeInt::INT; // It's "dirty" and needs sign-ext
156116441Sharti
157116441Sharti  } else if( jrange->cnt() >= TypeFunc::Parms+1 ) { // Else copy other types
158116441Sharti    rfields[TypeFunc::Parms] = jrange->field_at(TypeFunc::Parms);
159116441Sharti    if( jrange->cnt() == TypeFunc::Parms+2 )
160116441Sharti      rfields[TypeFunc::Parms+1] = jrange->field_at(TypeFunc::Parms+1);
161116441Sharti  }
162116441Sharti  const TypeTuple* range = TypeTuple::make(jrange->cnt(),rfields);
163114739Sharti
164114739Sharti  // Final C signature
165114739Sharti  const TypeFunc *c_sig = TypeFunc::make(domain,range);
166114739Sharti
167114739Sharti  //-----------------------------
168114739Sharti  // Make the call node
169114739Sharti  CallRuntimeNode *call = new (C)
170114739Sharti    CallRuntimeNode(c_sig, C_function, name, TypePtr::BOTTOM);
171114739Sharti  //-----------------------------
172114739Sharti
173116480Sharti  // Fix-up the debug info for the call
174116480Sharti  call->set_jvms( new (C) JVMState(0) );
175116480Sharti  call->jvms()->set_bci(0);
176116480Sharti  call->jvms()->set_offsets(cnt);
177116480Sharti
178116480Sharti  // Set fixed predefined input arguments
179116480Sharti  cnt = 0;
180116480Sharti  for (i = 0; i < TypeFunc::Parms; i++)
181116480Sharti    call->init_req(cnt++, map()->in(i));
182116480Sharti  // A little too aggressive on the parm copy; return address is not an input
183116480Sharti  call->set_req(TypeFunc::ReturnAdr, top());
184116480Sharti  for (; i < parm_cnt; i++) { // Regular input arguments
185116480Sharti    // Convert ints to longs if required.
186116480Sharti    if (CCallingConventionRequiresIntsAsLongs && jdomain->field_at(i)->isa_int()) {
187116480Sharti      Node* int_as_long = _gvn.transform(new (C) ConvI2LNode(map()->in(i)));
188116480Sharti      call->init_req(cnt++, int_as_long); // long
18925603Skjc      call->init_req(cnt++, top());       // half
19025603Skjc    } else {
19125603Skjc      call->init_req(cnt++, map()->in(i));
19225603Skjc    }
19325603Skjc  }
19425603Skjc
19525603Skjc  call->init_req( cnt++, thread );
19625603Skjc  if( return_pc )             // Return PC, if asked for
19725603Skjc    call->init_req( cnt++, returnadr() );
19825603Skjc  _gvn.transform_no_reclaim(call);
19925603Skjc
20025603Skjc
20125603Skjc  //-----------------------------
20225603Skjc  // Now set up the return results
20325603Skjc  set_control( _gvn.transform( new (C) ProjNode(call,TypeFunc::Control)) );
20425603Skjc  set_i_o(     _gvn.transform( new (C) ProjNode(call,TypeFunc::I_O    )) );
20525603Skjc  set_all_memory_call(call);
20625603Skjc  if (range->cnt() > TypeFunc::Parms) {
20725603Skjc    Node* retnode = _gvn.transform( new (C) ProjNode(call,TypeFunc::Parms) );
20825603Skjc    // C-land is allowed to return sub-word values.  Convert to integer type.
20925603Skjc    assert( retval != Type::TOP, "" );
21025603Skjc    if (retval == TypeInt::BOOL) {
21125603Skjc      retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFF)) );
21225603Skjc    } else if (retval == TypeInt::CHAR) {
21325603Skjc      retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFFFF)) );
21425603Skjc    } else if (retval == TypeInt::BYTE) {
21525603Skjc      retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(24)) );
21625603Skjc      retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(24)) );
21725603Skjc    } else if (retval == TypeInt::SHORT) {
21825603Skjc      retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(16)) );
21925603Skjc      retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(16)) );
22025603Skjc    }
22125603Skjc    map()->set_req( TypeFunc::Parms, retnode );
22225603Skjc  }
22325603Skjc
22425603Skjc  //-----------------------------
22525603Skjc
22625603Skjc  // Clear last_Java_sp
22725603Skjc  store_to_memory(NULL, adr_sp, null(), T_ADDRESS, NoAlias, MemNode::unordered);
22825603Skjc  // Clear last_Java_pc and (optionally)_flags
22925603Skjc  store_to_memory(NULL, adr_last_Java_pc, null(), T_ADDRESS, NoAlias, MemNode::unordered);
230116441Sharti#if defined(SPARC)
231116480Sharti  store_to_memory(NULL, adr_flags, intcon(0), T_INT, NoAlias, MemNode::unordered);
232116480Sharti#endif /* defined(SPARC) */
23325603Skjc#if (defined(IA64) && !defined(AIX))
234116441Sharti  Node* adr_last_Java_fp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_fp_offset()));
23537939Skjc  store_to_memory(NULL, adr_last_Java_fp, null(), T_ADDRESS, NoAlias, MemNode::unordered);
23625603Skjc#endif
23725603Skjc
23825603Skjc  // For is-fancy-jump, the C-return value is also the branch target
23925603Skjc  Node* target = map()->in(TypeFunc::Parms);
24025603Skjc  // Runtime call returning oop in TLS?  Fetch it out
24125603Skjc  if( pass_tls ) {
24225603Skjc    Node* adr = basic_plus_adr(top(), thread, in_bytes(JavaThread::vm_result_offset()));
24325603Skjc    Node* vm_result = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);
24425603Skjc    map()->set_req(TypeFunc::Parms, vm_result); // vm_result passed as result
24525603Skjc    // clear thread-local-storage(tls)
24625603Skjc    store_to_memory(NULL, adr, null(), T_ADDRESS, NoAlias, MemNode::unordered);
24725603Skjc  }
24825603Skjc
249112193Sharti  //-----------------------------
250112193Sharti  // check exception
25125603Skjc  Node* adr = basic_plus_adr(top(), thread, in_bytes(Thread::pending_exception_offset()));
25225603Skjc  Node* pending = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, MemNode::unordered);
25325603Skjc
25492725Salfred  Node* exit_memory = reset_memory();
255114201Sharti
25692725Salfred  Node* cmp = _gvn.transform( new (C) CmpPNode(pending, null()) );
25792725Salfred  Node* bo  = _gvn.transform( new (C) BoolNode(cmp, BoolTest::ne) );
25892725Salfred  IfNode   *iff = create_and_map_if(control(), bo, PROB_MIN, COUNT_UNKNOWN);
25992725Salfred
26025603Skjc  Node* if_null     = _gvn.transform( new (C) IfFalseNode(iff) );
26146695Skjc  Node* if_not_null = _gvn.transform( new (C) IfTrueNode(iff)  );
262
263  assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
264  Node *exc_target = makecon(TypeRawPtr::make( StubRoutines::forward_exception_entry() ));
265  Node *to_exc = new (C) TailCallNode(if_not_null,
266                                      i_o(),
267                                      exit_memory,
268                                      frameptr(),
269                                      returnadr(),
270                                      exc_target, null());
271  root()->add_req(_gvn.transform(to_exc));  // bind to root to keep live
272  C->init_start(start);
273
274  //-----------------------------
275  // If this is a normal subroutine return, issue the return and be done.
276  Node *ret;
277  switch( is_fancy_jump ) {
278  case 0:                       // Make a return instruction
279    // Return to caller, free any space for return address
280    ret = new (C) ReturnNode(TypeFunc::Parms, if_null,
281                             i_o(),
282                             exit_memory,
283                             frameptr(),
284                             returnadr());
285    if (C->tf()->range()->cnt() > TypeFunc::Parms)
286      ret->add_req( map()->in(TypeFunc::Parms) );
287    break;
288  case 1:    // This is a fancy tail-call jump.  Jump to computed address.
289    // Jump to new callee; leave old return address alone.
290    ret = new (C) TailCallNode(if_null,
291                               i_o(),
292                               exit_memory,
293                               frameptr(),
294                               returnadr(),
295                               target, map()->in(TypeFunc::Parms));
296    break;
297  case 2:                       // Pop return address & jump
298    // Throw away old return address; jump to new computed address
299    //assert(C_function == CAST_FROM_FN_PTR(address, OptoRuntime::rethrow_C), "fancy_jump==2 only for rethrow");
300    ret = new (C) TailJumpNode(if_null,
301                               i_o(),
302                               exit_memory,
303                               frameptr(),
304                               target, map()->in(TypeFunc::Parms));
305    break;
306  default:
307    ShouldNotReachHere();
308  }
309  root()->add_req(_gvn.transform(ret));
310}
311