1 /* 2 * Copyright (c) 1999, 2025, 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 "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" 41 42 //--------------------gen_stub------------------------------- 43 void GraphKit::gen_stub(address C_function, 44 const char *name, 45 int is_fancy_jump, 46 bool pass_tls, 47 bool return_pc) { 48 ResourceMark rm; 49 50 const TypeTuple *jdomain = C->tf()->domain(); 51 const TypeTuple *jrange = C->tf()->range(); 52 53 // The procedure start 54 StartNode* start = new StartNode(root(), jdomain); 55 _gvn.set_type_bottom(start); 56 57 // Make a map, with JVM state 58 uint parm_cnt = jdomain->cnt(); 59 uint max_map = MAX2(2*parm_cnt+1, jrange->cnt()); 60 // %%% SynchronizationEntryBCI is redundant; use InvocationEntryBci in interfaces 61 assert(SynchronizationEntryBCI == InvocationEntryBci, ""); 62 JVMState* jvms = new (C) JVMState(0); 63 jvms->set_bci(InvocationEntryBci); 64 jvms->set_monoff(max_map); 65 jvms->set_scloff(max_map); 66 jvms->set_endoff(max_map); 67 { 68 SafePointNode *map = new SafePointNode( max_map, jvms ); 69 jvms->set_map(map); 70 set_jvms(jvms); 71 assert(map == this->map(), "kit.map is set"); 72 } 73 74 // Make up the parameters 75 uint i; 76 for (i = 0; i < parm_cnt; i++) { 77 map()->init_req(i, _gvn.transform(new ParmNode(start, i))); 78 } 79 for ( ; i<map()->req(); i++) { 80 map()->init_req(i, top()); // For nicer debugging 81 } 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 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 96 // 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 since 98 // as soon as last_Java_sp != nullptr the has_last_Java_frame is true and 99 // users will look at the other fields. 100 // 101 Node *adr_sp = basic_plus_adr(top(), thread, in_bytes(JavaThread::last_Java_sp_offset())); 102 Node *last_sp = frameptr(); 103 store_to_memory(control(), adr_sp, last_sp, T_ADDRESS, MemNode::unordered); 104 105 // Set _thread_in_native 106 // The order of stores into TLS is critical! Setting _thread_in_native MUST 107 // be last, because a GC is allowed at any time after setting it and the GC 108 // will require last_Java_pc and last_Java_sp. 109 110 //----------------------------- 111 // Compute signature for C call. Varies from the Java signature! 112 113 const Type **fields = TypeTuple::fields(2*parm_cnt+2); 114 uint cnt = TypeFunc::Parms; 115 // The C routines gets the base of thread-local storage passed in as an 116 // extra argument. Not all calls need it, but it is cheap to add here. 117 for (uint pcnt = cnt; pcnt < parm_cnt; pcnt++, cnt++) { 118 const Type *f = jdomain->field_at(pcnt); 119 if (CCallingConventionRequiresIntsAsLongs && f->isa_int()) { 120 fields[cnt++] = TypeLong::LONG; 121 fields[cnt] = Type::HALF; // Must add an additional half for a long. 122 } else { 123 fields[cnt] = f; 124 } 125 } 126 fields[cnt++] = TypeRawPtr::BOTTOM; // Thread-local storage 127 // Also pass in the caller's PC, if asked for. 128 if (return_pc) { 129 fields[cnt++] = TypeRawPtr::BOTTOM; // Return PC 130 } 131 const TypeTuple* domain = TypeTuple::make(cnt, fields); 132 133 // The C routine we are about to call cannot return an oop; it can block on 134 // exit and a GC will trash the oop while it sits in C-land. Instead, we 135 // return the oop through TLS for runtime calls. 136 // Also, C routines returning integer subword values leave the high 137 // order bits dirty; these must be cleaned up by explicit sign extension. 138 const Type* retval = (jrange->cnt() == TypeFunc::Parms) ? Type::TOP : jrange->field_at(TypeFunc::Parms); 139 // Make a private copy of jrange->fields(); 140 const Type **rfields = TypeTuple::fields(jrange->cnt() - TypeFunc::Parms); 141 // Fixup oop returns 142 int retval_ptr = retval->isa_oop_ptr(); 143 if (retval_ptr) { 144 assert( pass_tls, "Oop must be returned thru TLS" ); 145 // Fancy-jumps return address; others return void 146 rfields[TypeFunc::Parms] = is_fancy_jump ? TypeRawPtr::BOTTOM : Type::TOP; 147 148 } else if (retval->isa_int()) { // Returning any integer subtype? 149 // "Fatten" byte, char & short return types to 'int' to show that 150 // the native C code can return values with junk high order bits. 151 // We'll sign-extend it below later. 152 rfields[TypeFunc::Parms] = TypeInt::INT; // It's "dirty" and needs sign-ext 153 154 } else if (jrange->cnt() >= TypeFunc::Parms+1) { // Else copy other types 155 rfields[TypeFunc::Parms] = jrange->field_at(TypeFunc::Parms); 156 if (jrange->cnt() == TypeFunc::Parms+2) { 157 rfields[TypeFunc::Parms+1] = jrange->field_at(TypeFunc::Parms+1); 158 } 159 } 160 const TypeTuple* range = TypeTuple::make(jrange->cnt(), rfields); 161 162 // Final C signature 163 const TypeFunc *c_sig = TypeFunc::make(domain, range); 164 165 //----------------------------- 166 // Make the call node. 167 CallRuntimeNode* call = new CallRuntimeNode(c_sig, C_function, name, TypePtr::BOTTOM, new (C) JVMState(0)); 168 //----------------------------- 169 170 // Fix-up the debug info for the call. 171 call->jvms()->set_bci(0); 172 call->jvms()->set_offsets(cnt); 173 174 // Set fixed predefined input arguments. 175 cnt = 0; 176 for (i = 0; i < TypeFunc::Parms; i++) { 177 call->init_req(cnt++, map()->in(i)); 178 } 179 // A little too aggressive on the parm copy; return address is not an input. 180 call->set_req(TypeFunc::ReturnAdr, top()); 181 for (; i < parm_cnt; i++) { // Regular input arguments. 182 const Type *f = jdomain->field_at(i); 183 if (CCallingConventionRequiresIntsAsLongs && f->isa_int()) { 184 call->init_req(cnt++, _gvn.transform(new ConvI2LNode(map()->in(i)))); 185 call->init_req(cnt++, top()); 186 } else { 187 call->init_req(cnt++, map()->in(i)); 188 } 189 } 190 call->init_req(cnt++, thread); 191 if (return_pc) { // Return PC, if asked for. 192 call->init_req(cnt++, returnadr()); 193 } 194 195 _gvn.transform(call); 196 197 //----------------------------- 198 // Now set up the return results 199 set_control( _gvn.transform( new ProjNode(call,TypeFunc::Control)) ); 200 set_i_o( _gvn.transform( new ProjNode(call,TypeFunc::I_O )) ); 201 set_all_memory_call(call); 202 if (range->cnt() > TypeFunc::Parms) { 203 Node* retnode = _gvn.transform( new ProjNode(call,TypeFunc::Parms) ); 204 // C-land is allowed to return sub-word values. Convert to integer type. 205 assert( retval != Type::TOP, "" ); 206 if (retval == TypeInt::BOOL) { 207 retnode = _gvn.transform( new AndINode(retnode, intcon(0xFF)) ); 208 } else if (retval == TypeInt::CHAR) { 209 retnode = _gvn.transform( new AndINode(retnode, intcon(0xFFFF)) ); 210 } else if (retval == TypeInt::BYTE) { 211 retnode = _gvn.transform( new LShiftINode(retnode, intcon(24)) ); 212 retnode = _gvn.transform( new RShiftINode(retnode, intcon(24)) ); 213 } else if (retval == TypeInt::SHORT) { 214 retnode = _gvn.transform( new LShiftINode(retnode, intcon(16)) ); 215 retnode = _gvn.transform( new RShiftINode(retnode, intcon(16)) ); 216 } 217 map()->set_req( TypeFunc::Parms, retnode ); 218 } 219 220 //----------------------------- 221 222 // Clear last_Java_sp 223 store_to_memory(control(), adr_sp, null(), T_ADDRESS, MemNode::unordered); 224 // Clear last_Java_pc 225 store_to_memory(control(), adr_last_Java_pc, null(), T_ADDRESS, MemNode::unordered); 226 227 // For is-fancy-jump, the C-return value is also the branch target 228 Node* target = map()->in(TypeFunc::Parms); 229 // Runtime call returning oop in TLS? Fetch it out 230 if( pass_tls ) { 231 Node* adr = basic_plus_adr(top(), thread, in_bytes(JavaThread::vm_result_offset())); 232 Node* vm_result = make_load(nullptr, adr, TypeOopPtr::BOTTOM, T_OBJECT, MemNode::unordered); 233 map()->set_req(TypeFunc::Parms, vm_result); // vm_result passed as result 234 // clear thread-local-storage(tls) 235 store_to_memory(control(), adr, null(), T_ADDRESS, MemNode::unordered); 236 } 237 238 //----------------------------- 239 // check exception 240 Node* adr = basic_plus_adr(top(), thread, in_bytes(Thread::pending_exception_offset())); 241 Node* pending = make_load(nullptr, adr, TypeOopPtr::BOTTOM, T_OBJECT, MemNode::unordered); 242 243 Node* exit_memory = reset_memory(); 244 245 Node* cmp = _gvn.transform( new CmpPNode(pending, null()) ); 246 Node* bo = _gvn.transform( new BoolNode(cmp, BoolTest::ne) ); 247 IfNode *iff = create_and_map_if(control(), bo, PROB_MIN, COUNT_UNKNOWN); 248 249 Node* if_null = _gvn.transform( new IfFalseNode(iff) ); 250 Node* if_not_null = _gvn.transform( new IfTrueNode(iff) ); 251 252 assert (StubRoutines::forward_exception_entry() != nullptr, "must be generated before"); 253 Node *to_exc = new ForwardExceptionNode(if_not_null, 254 i_o(), 255 exit_memory, 256 frameptr(), 257 returnadr()); 258 root()->add_req(_gvn.transform(to_exc)); // bind to root to keep live 259 C->verify_start(start); 260 261 //----------------------------- 262 // If this is a normal subroutine return, issue the return and be done. 263 Node *ret = nullptr; 264 switch( is_fancy_jump ) { 265 case 0: // Make a return instruction 266 // Return to caller, free any space for return address 267 ret = new ReturnNode(TypeFunc::Parms, if_null, 268 i_o(), 269 exit_memory, 270 frameptr(), 271 returnadr()); 272 if (C->tf()->range()->cnt() > TypeFunc::Parms) 273 ret->add_req( map()->in(TypeFunc::Parms) ); 274 break; 275 case 1: // This is a fancy tail-call jump. Jump to computed address. 276 // Jump to new callee; leave old return address alone. 277 ret = new TailCallNode(if_null, 278 i_o(), 279 exit_memory, 280 frameptr(), 281 returnadr(), 282 target, map()->in(TypeFunc::Parms)); 283 break; 284 case 2: // Pop return address & jump 285 // Throw away old return address; jump to new computed address 286 //assert(C_function == CAST_FROM_FN_PTR(address, OptoRuntime::rethrow_C), "fancy_jump==2 only for rethrow"); 287 ret = new TailJumpNode(if_null, 288 i_o(), 289 exit_memory, 290 frameptr(), 291 target, map()->in(TypeFunc::Parms)); 292 break; 293 default: 294 ShouldNotReachHere(); 295 } 296 root()->add_req(_gvn.transform(ret)); 297 }