1 /*
   2  * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
   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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "opto/addnode.hpp"
  27 #include "opto/callnode.hpp"
  28 #include "opto/cfgnode.hpp"
  29 #include "opto/compile.hpp"
  30 #include "opto/connode.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 
  41 //--------------------gen_stub-------------------------------
  42 void GraphKit::gen_stub(address C_function,
  43                         const char *name,
  44                         int is_fancy_jump,
  45                         bool pass_tls,
  46                         bool return_pc) {
  47   ResourceMark rm;
  48 
  49   // Do we need to convert ints to longs for c calls?
  50   const bool convert_ints_to_longs =
  51     SharedRuntime::c_calling_convention_requires_ints_as_longs();
  52 
  53   const TypeTuple *jdomain = C->tf()->domain();
  54   const TypeTuple *jrange  = C->tf()->range();
  55 
  56   // The procedure start
  57   StartNode* start = new (C) StartNode(root(), jdomain);
  58   _gvn.set_type_bottom(start);
  59 
  60   // Make a map, with JVM state
  61   uint parm_cnt = jdomain->cnt();
  62   uint max_map = MAX2(2*parm_cnt+1, jrange->cnt());
  63   // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces
  64   assert(SynchronizationEntryBCI == InvocationEntryBci, "");
  65   JVMState* jvms = new (C) JVMState(0);
  66   jvms->set_bci(InvocationEntryBci);
  67   jvms->set_monoff(max_map);
  68   jvms->set_endoff(max_map);
  69   {
  70     SafePointNode *map = new (C) SafePointNode( max_map, jvms );
  71     jvms->set_map(map);
  72     set_jvms(jvms);
  73     assert(map == this->map(), "kit.map is set");
  74   }
  75 
  76   // Make up the parameters
  77   uint i;
  78   for( i = 0; i < parm_cnt; i++ )
  79     map()->init_req(i, _gvn.transform(new (C) ParmNode(start, i)));
  80   for( ; i<map()->req(); i++ )
  81     map()->init_req(i, top());      // For nicer debugging
  82 
  83   // GraphKit requires memory to be a MergeMemNode:
  84   set_all_memory(map()->memory());
  85 
  86   // Get base of thread-local storage area
  87   Node* thread = _gvn.transform( new (C) ThreadLocalNode() );
  88 
  89   const int NoAlias = Compile::AliasIdxBot;
  90 
  91   Node* adr_last_Java_pc = basic_plus_adr(top(),
  92                                             thread,
  93                                             in_bytes(JavaThread::frame_anchor_offset()) +
  94                                             in_bytes(JavaFrameAnchor::last_Java_pc_offset()));
  95 #if defined(SPARC)
  96   Node* adr_flags = basic_plus_adr(top(),
  97                                    thread,
  98                                    in_bytes(JavaThread::frame_anchor_offset()) +
  99                                    in_bytes(JavaFrameAnchor::flags_offset()));
 100 #endif /* defined(SPARC) */
 101 
 102 
 103   // Drop in the last_Java_sp.  last_Java_fp is not touched.
 104   // Always do this after the other "last_Java_frame" fields are set since
 105   // as soon as last_Java_sp != NULL the has_last_Java_frame is true and
 106   // users will look at the other fields.
 107   //
 108   Node *adr_sp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_sp_offset()));
 109   Node *last_sp = basic_plus_adr(top(), frameptr(), (intptr_t) STACK_BIAS);
 110   store_to_memory(NULL, adr_sp, last_sp, T_ADDRESS, NoAlias);
 111 
 112   // Set _thread_in_native
 113   // The order of stores into TLS is critical!  Setting _thread_in_native MUST
 114   // be last, because a GC is allowed at any time after setting it and the GC
 115   // will require last_Java_pc and last_Java_sp.
 116   Node* adr_state = basic_plus_adr(top(), thread, in_bytes(JavaThread::thread_state_offset()));
 117 
 118   //-----------------------------
 119   // Compute signature for C call.  Varies from the Java signature!
 120   const Type **fields = TypeTuple::fields(2*parm_cnt+2);
 121   uint cnt = TypeFunc::Parms;
 122   // The C routines gets the base of thread-local storage passed in as an
 123   // extra argument.  Not all calls need it, but its cheap to add here.
 124   for (uint pcnt = cnt; pcnt < parm_cnt; pcnt++, cnt++) {
 125     // Convert ints to longs if required.
 126     if (convert_ints_to_longs && jdomain->field_at(pcnt)->isa_int()) {
 127       fields[cnt++] = TypeLong::LONG;
 128       fields[cnt]   = Type::HALF; // must add an additional half for a long
 129     } else {
 130       fields[cnt] = jdomain->field_at(pcnt);
 131     }
 132   }
 133 
 134   fields[cnt++] = TypeRawPtr::BOTTOM; // Thread-local storage
 135   // Also pass in the caller's PC, if asked for.
 136   if( return_pc )
 137     fields[cnt++] = TypeRawPtr::BOTTOM; // Return PC
 138 
 139   const TypeTuple* domain = TypeTuple::make(cnt,fields);
 140   // The C routine we are about to call cannot return an oop; it can block on
 141   // exit and a GC will trash the oop while it sits in C-land.  Instead, we
 142   // return the oop through TLS for runtime calls.
 143   // Also, C routines returning integer subword values leave the high
 144   // order bits dirty; these must be cleaned up by explicit sign extension.
 145   const Type* retval = (jrange->cnt() == TypeFunc::Parms) ? Type::TOP : jrange->field_at(TypeFunc::Parms);
 146   // Make a private copy of jrange->fields();
 147   const Type **rfields = TypeTuple::fields(jrange->cnt() - TypeFunc::Parms);
 148   // Fixup oop returns
 149   int retval_ptr = retval->isa_oop_ptr();
 150   if( retval_ptr ) {
 151     assert( pass_tls, "Oop must be returned thru TLS" );
 152     // Fancy-jumps return address; others return void
 153     rfields[TypeFunc::Parms] = is_fancy_jump ? TypeRawPtr::BOTTOM : Type::TOP;
 154 
 155   } else if( retval->isa_int() ) { // Returning any integer subtype?
 156     // "Fatten" byte, char & short return types to 'int' to show that
 157     // the native C code can return values with junk high order bits.
 158     // We'll sign-extend it below later.
 159     rfields[TypeFunc::Parms] = TypeInt::INT; // It's "dirty" and needs sign-ext
 160 
 161   } else if( jrange->cnt() >= TypeFunc::Parms+1 ) { // Else copy other types
 162     rfields[TypeFunc::Parms] = jrange->field_at(TypeFunc::Parms);
 163     if( jrange->cnt() == TypeFunc::Parms+2 )
 164       rfields[TypeFunc::Parms+1] = jrange->field_at(TypeFunc::Parms+1);
 165   }
 166   const TypeTuple* range = TypeTuple::make(jrange->cnt(),rfields);
 167 
 168   // Final C signature
 169   const TypeFunc *c_sig = TypeFunc::make(domain,range);
 170 
 171   //-----------------------------
 172   // Make the call node
 173   CallRuntimeNode *call = new (C)
 174     CallRuntimeNode(c_sig, C_function, name, TypePtr::BOTTOM);
 175   //-----------------------------
 176 
 177   // Fix-up the debug info for the call
 178   call->set_jvms( new (C) JVMState(0) );
 179   call->jvms()->set_bci(0);
 180   call->jvms()->set_offsets(cnt);
 181 
 182   // Set fixed predefined input arguments
 183   cnt = 0;
 184   for (i = 0; i < TypeFunc::Parms; i++)
 185     call->init_req(cnt++, map()->in(i));
 186   // A little too aggressive on the parm copy; return address is not an input
 187   call->set_req(TypeFunc::ReturnAdr, top());
 188   for (; i < parm_cnt; i++) { // Regular input arguments
 189     // Convert ints to longs if required.
 190     if (convert_ints_to_longs && jdomain->field_at(i)->isa_int()) {
 191       Node* int_as_long = _gvn.transform(new (C) ConvI2LNode(map()->in(i)));
 192       call->init_req(cnt++, int_as_long); // long
 193       call->init_req(cnt++, top());       // half
 194     } else {
 195       call->init_req(cnt++, map()->in(i));
 196     }
 197   }
 198 
 199   call->init_req( cnt++, thread );
 200   if( return_pc )             // Return PC, if asked for
 201     call->init_req( cnt++, returnadr() );
 202   _gvn.transform_no_reclaim(call);
 203 
 204 
 205   //-----------------------------
 206   // Now set up the return results
 207   set_control( _gvn.transform( new (C) ProjNode(call,TypeFunc::Control)) );
 208   set_i_o(     _gvn.transform( new (C) ProjNode(call,TypeFunc::I_O    )) );
 209   set_all_memory_call(call);
 210   if (range->cnt() > TypeFunc::Parms) {
 211     Node* retnode = _gvn.transform( new (C) ProjNode(call,TypeFunc::Parms) );
 212     // C-land is allowed to return sub-word values.  Convert to integer type.
 213     assert( retval != Type::TOP, "" );
 214     if (retval == TypeInt::BOOL) {
 215       retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFF)) );
 216     } else if (retval == TypeInt::CHAR) {
 217       retnode = _gvn.transform( new (C) AndINode(retnode, intcon(0xFFFF)) );
 218     } else if (retval == TypeInt::BYTE) {
 219       retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(24)) );
 220       retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(24)) );
 221     } else if (retval == TypeInt::SHORT) {
 222       retnode = _gvn.transform( new (C) LShiftINode(retnode, intcon(16)) );
 223       retnode = _gvn.transform( new (C) RShiftINode(retnode, intcon(16)) );
 224     }
 225     map()->set_req( TypeFunc::Parms, retnode );
 226   }
 227 
 228   //-----------------------------
 229 
 230   // Clear last_Java_sp
 231   store_to_memory(NULL, adr_sp, null(), T_ADDRESS, NoAlias);
 232   // Clear last_Java_pc and (optionally)_flags
 233   store_to_memory(NULL, adr_last_Java_pc, null(), T_ADDRESS, NoAlias);
 234 #if defined(SPARC)
 235   store_to_memory(NULL, adr_flags, intcon(0), T_INT, NoAlias);
 236 #endif /* defined(SPARC) */
 237 #if (defined(IA64) && !defined(AIX))
 238   Node* adr_last_Java_fp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_fp_offset()));
 239   if( os::is_MP() ) insert_mem_bar(Op_MemBarRelease);
 240   store_to_memory(NULL, adr_last_Java_fp,    null(),    T_ADDRESS, NoAlias);
 241 #endif
 242 
 243   // For is-fancy-jump, the C-return value is also the branch target
 244   Node* target = map()->in(TypeFunc::Parms);
 245   // Runtime call returning oop in TLS?  Fetch it out
 246   if( pass_tls ) {
 247     Node* adr = basic_plus_adr(top(), thread, in_bytes(JavaThread::vm_result_offset()));
 248     Node* vm_result = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, false);
 249     map()->set_req(TypeFunc::Parms, vm_result); // vm_result passed as result
 250     // clear thread-local-storage(tls)
 251     store_to_memory(NULL, adr, null(), T_ADDRESS, NoAlias);
 252   }
 253 
 254   //-----------------------------
 255   // check exception
 256   Node* adr = basic_plus_adr(top(), thread, in_bytes(Thread::pending_exception_offset()));
 257   Node* pending = make_load(NULL, adr, TypeOopPtr::BOTTOM, T_OBJECT, NoAlias, false);
 258 
 259   Node* exit_memory = reset_memory();
 260 
 261   Node* cmp = _gvn.transform( new (C) CmpPNode(pending, null()) );
 262   Node* bo  = _gvn.transform( new (C) BoolNode(cmp, BoolTest::ne) );
 263   IfNode   *iff = create_and_map_if(control(), bo, PROB_MIN, COUNT_UNKNOWN);
 264 
 265   Node* if_null     = _gvn.transform( new (C) IfFalseNode(iff) );
 266   Node* if_not_null = _gvn.transform( new (C) IfTrueNode(iff)  );
 267 
 268   assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
 269   Node *exc_target = makecon(TypeRawPtr::make( StubRoutines::forward_exception_entry() ));
 270   Node *to_exc = new (C) TailCallNode(if_not_null,
 271                                       i_o(),
 272                                       exit_memory,
 273                                       frameptr(),
 274                                       returnadr(),
 275                                       exc_target, null());
 276   root()->add_req(_gvn.transform(to_exc));  // bind to root to keep live
 277   C->init_start(start);
 278 
 279   //-----------------------------
 280   // If this is a normal subroutine return, issue the return and be done.
 281   Node *ret;
 282   switch( is_fancy_jump ) {
 283   case 0:                       // Make a return instruction
 284     // Return to caller, free any space for return address
 285     ret = new (C) ReturnNode(TypeFunc::Parms, if_null,
 286                              i_o(),
 287                              exit_memory,
 288                              frameptr(),
 289                              returnadr());
 290     if (C->tf()->range()->cnt() > TypeFunc::Parms)
 291       ret->add_req( map()->in(TypeFunc::Parms) );
 292     break;
 293   case 1:    // This is a fancy tail-call jump.  Jump to computed address.
 294     // Jump to new callee; leave old return address alone.
 295     ret = new (C) TailCallNode(if_null,
 296                                i_o(),
 297                                exit_memory,
 298                                frameptr(),
 299                                returnadr(),
 300                                target, map()->in(TypeFunc::Parms));
 301     break;
 302   case 2:                       // Pop return address & jump
 303     // Throw away old return address; jump to new computed address
 304     //assert(C_function == CAST_FROM_FN_PTR(address, OptoRuntime::rethrow_C), "fancy_jump==2 only for rethrow");
 305     ret = new (C) TailJumpNode(if_null,
 306                                i_o(),
 307                                exit_memory,
 308                                frameptr(),
 309                                target, map()->in(TypeFunc::Parms));
 310     break;
 311   default:
 312     ShouldNotReachHere();
 313   }
 314   root()->add_req(_gvn.transform(ret));
 315 }