src/cpu/sparc/vm/sharedRuntime_sparc.cpp

changeset 3969
1d7922586cf6
parent 3627
8a48c2906f91
child 4037
da91efe96a93
     1.1 --- a/src/cpu/sparc/vm/sharedRuntime_sparc.cpp	Mon Jul 23 13:04:59 2012 -0700
     1.2 +++ b/src/cpu/sparc/vm/sharedRuntime_sparc.cpp	Tue Jul 24 10:51:00 2012 -0700
     1.3 @@ -400,13 +400,13 @@
     1.4      case T_LONG:                // LP64, longs compete with int args
     1.5        assert(sig_bt[i+1] == T_VOID, "");
     1.6  #ifdef _LP64
     1.7 -      if (int_reg_cnt < int_reg_max) int_reg_cnt++;
     1.8 +      if (int_reg_cnt < int_reg_max)  int_reg_cnt++;
     1.9  #endif
    1.10        break;
    1.11      case T_OBJECT:
    1.12      case T_ARRAY:
    1.13      case T_ADDRESS: // Used, e.g., in slow-path locking for the lock's stack address
    1.14 -      if (int_reg_cnt < int_reg_max) int_reg_cnt++;
    1.15 +      if (int_reg_cnt < int_reg_max)  int_reg_cnt++;
    1.16  #ifndef _LP64
    1.17        else                            stk_reg_pairs++;
    1.18  #endif
    1.19 @@ -416,11 +416,11 @@
    1.20      case T_CHAR:
    1.21      case T_BYTE:
    1.22      case T_BOOLEAN:
    1.23 -      if (int_reg_cnt < int_reg_max) int_reg_cnt++;
    1.24 +      if (int_reg_cnt < int_reg_max)  int_reg_cnt++;
    1.25        else                            stk_reg_pairs++;
    1.26        break;
    1.27      case T_FLOAT:
    1.28 -      if (flt_reg_cnt < flt_reg_max) flt_reg_cnt++;
    1.29 +      if (flt_reg_cnt < flt_reg_max)  flt_reg_cnt++;
    1.30        else                            stk_reg_pairs++;
    1.31        break;
    1.32      case T_DOUBLE:
    1.33 @@ -436,7 +436,6 @@
    1.34    // This is where the longs/doubles start on the stack.
    1.35    stk_reg_pairs = (stk_reg_pairs+1) & ~1; // Round
    1.36  
    1.37 -  int int_reg_pairs = (int_reg_cnt+1) & ~1; // 32-bit 2-reg longs only
    1.38    int flt_reg_pairs = (flt_reg_cnt+1) & ~1;
    1.39  
    1.40    // int stk_reg = frame::register_save_words*(wordSize>>2);
    1.41 @@ -517,24 +516,15 @@
    1.42            stk_reg_pairs += 2;
    1.43          }
    1.44  #else // COMPILER2
    1.45 -        if (int_reg_pairs + 1 < int_reg_max) {
    1.46 -          if (is_outgoing) {
    1.47 -            regs[i].set_pair(as_oRegister(int_reg_pairs + 1)->as_VMReg(), as_oRegister(int_reg_pairs)->as_VMReg());
    1.48 -          } else {
    1.49 -            regs[i].set_pair(as_iRegister(int_reg_pairs + 1)->as_VMReg(), as_iRegister(int_reg_pairs)->as_VMReg());
    1.50 -          }
    1.51 -          int_reg_pairs += 2;
    1.52 -        } else {
    1.53            regs[i].set2(VMRegImpl::stack2reg(stk_reg_pairs));
    1.54            stk_reg_pairs += 2;
    1.55 -        }
    1.56  #endif // COMPILER2
    1.57  #endif // _LP64
    1.58        break;
    1.59  
    1.60      case T_FLOAT:
    1.61        if (flt_reg < flt_reg_max) regs[i].set1(as_FloatRegister(flt_reg++)->as_VMReg());
    1.62 -      else                       regs[i].set1(    VMRegImpl::stack2reg(stk_reg++));
    1.63 +      else                       regs[i].set1(VMRegImpl::stack2reg(stk_reg++));
    1.64        break;
    1.65      case T_DOUBLE:
    1.66        assert(sig_bt[i+1] == T_VOID, "expecting half");
    1.67 @@ -886,6 +876,20 @@
    1.68    __ delayed()->add(SP, G1, Gargs);
    1.69  }
    1.70  
    1.71 +static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg, Register temp2_reg,
    1.72 +                        address code_start, address code_end,
    1.73 +                        Label& L_ok) {
    1.74 +  Label L_fail;
    1.75 +  __ set(ExternalAddress(code_start), temp_reg);
    1.76 +  __ set(pointer_delta(code_end, code_start, 1), temp2_reg);
    1.77 +  __ cmp(pc_reg, temp_reg);
    1.78 +  __ brx(Assembler::lessEqualUnsigned, false, Assembler::pn, L_fail);
    1.79 +  __ delayed()->add(temp_reg, temp2_reg, temp_reg);
    1.80 +  __ cmp(pc_reg, temp_reg);
    1.81 +  __ cmp_and_brx_short(pc_reg, temp_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
    1.82 +  __ bind(L_fail);
    1.83 +}
    1.84 +
    1.85  void AdapterGenerator::gen_i2c_adapter(
    1.86                              int total_args_passed,
    1.87                              // VMReg max_arg,
    1.88 @@ -907,6 +911,51 @@
    1.89    // This removes all sorts of headaches on the x86 side and also eliminates
    1.90    // the possibility of having c2i -> i2c -> c2i -> ... endless transitions.
    1.91  
    1.92 +  // More detail:
    1.93 +  // Adapters can be frameless because they do not require the caller
    1.94 +  // to perform additional cleanup work, such as correcting the stack pointer.
    1.95 +  // An i2c adapter is frameless because the *caller* frame, which is interpreted,
    1.96 +  // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
    1.97 +  // even if a callee has modified the stack pointer.
    1.98 +  // A c2i adapter is frameless because the *callee* frame, which is interpreted,
    1.99 +  // routinely repairs its caller's stack pointer (from sender_sp, which is set
   1.100 +  // up via the senderSP register).
   1.101 +  // In other words, if *either* the caller or callee is interpreted, we can
   1.102 +  // get the stack pointer repaired after a call.
   1.103 +  // This is why c2i and i2c adapters cannot be indefinitely composed.
   1.104 +  // In particular, if a c2i adapter were to somehow call an i2c adapter,
   1.105 +  // both caller and callee would be compiled methods, and neither would
   1.106 +  // clean up the stack pointer changes performed by the two adapters.
   1.107 +  // If this happens, control eventually transfers back to the compiled
   1.108 +  // caller, but with an uncorrected stack, causing delayed havoc.
   1.109 +
   1.110 +  if (VerifyAdapterCalls &&
   1.111 +      (Interpreter::code() != NULL || StubRoutines::code1() != NULL)) {
   1.112 +    // So, let's test for cascading c2i/i2c adapters right now.
   1.113 +    //  assert(Interpreter::contains($return_addr) ||
   1.114 +    //         StubRoutines::contains($return_addr),
   1.115 +    //         "i2c adapter must return to an interpreter frame");
   1.116 +    __ block_comment("verify_i2c { ");
   1.117 +    Label L_ok;
   1.118 +    if (Interpreter::code() != NULL)
   1.119 +      range_check(masm, O7, O0, O1,
   1.120 +                  Interpreter::code()->code_start(), Interpreter::code()->code_end(),
   1.121 +                  L_ok);
   1.122 +    if (StubRoutines::code1() != NULL)
   1.123 +      range_check(masm, O7, O0, O1,
   1.124 +                  StubRoutines::code1()->code_begin(), StubRoutines::code1()->code_end(),
   1.125 +                  L_ok);
   1.126 +    if (StubRoutines::code2() != NULL)
   1.127 +      range_check(masm, O7, O0, O1,
   1.128 +                  StubRoutines::code2()->code_begin(), StubRoutines::code2()->code_end(),
   1.129 +                  L_ok);
   1.130 +    const char* msg = "i2c adapter must return to an interpreter frame";
   1.131 +    __ block_comment(msg);
   1.132 +    __ stop(msg);
   1.133 +    __ bind(L_ok);
   1.134 +    __ block_comment("} verify_i2ce ");
   1.135 +  }
   1.136 +
   1.137    // As you can see from the list of inputs & outputs there are not a lot
   1.138    // of temp registers to work with: mostly G1, G3 & G4.
   1.139  
   1.140 @@ -1937,20 +1986,156 @@
   1.141    __ bind(done);
   1.142  }
   1.143  
   1.144 +static void verify_oop_args(MacroAssembler* masm,
   1.145 +                            int total_args_passed,
   1.146 +                            const BasicType* sig_bt,
   1.147 +                            const VMRegPair* regs) {
   1.148 +  Register temp_reg = G5_method;  // not part of any compiled calling seq
   1.149 +  if (VerifyOops) {
   1.150 +    for (int i = 0; i < total_args_passed; i++) {
   1.151 +      if (sig_bt[i] == T_OBJECT ||
   1.152 +          sig_bt[i] == T_ARRAY) {
   1.153 +        VMReg r = regs[i].first();
   1.154 +        assert(r->is_valid(), "bad oop arg");
   1.155 +        if (r->is_stack()) {
   1.156 +          RegisterOrConstant ld_off = reg2offset(r) + STACK_BIAS;
   1.157 +          ld_off = __ ensure_simm13_or_reg(ld_off, temp_reg);
   1.158 +          __ ld_ptr(SP, ld_off, temp_reg);
   1.159 +          __ verify_oop(temp_reg);
   1.160 +        } else {
   1.161 +          __ verify_oop(r->as_Register());
   1.162 +        }
   1.163 +      }
   1.164 +    }
   1.165 +  }
   1.166 +}
   1.167 +
   1.168 +static void gen_special_dispatch(MacroAssembler* masm,
   1.169 +                                 int total_args_passed,
   1.170 +                                 int comp_args_on_stack,
   1.171 +                                 vmIntrinsics::ID special_dispatch,
   1.172 +                                 const BasicType* sig_bt,
   1.173 +                                 const VMRegPair* regs) {
   1.174 +  verify_oop_args(masm, total_args_passed, sig_bt, regs);
   1.175 +
   1.176 +  // Now write the args into the outgoing interpreter space
   1.177 +  bool     has_receiver   = false;
   1.178 +  Register receiver_reg   = noreg;
   1.179 +  int      member_arg_pos = -1;
   1.180 +  Register member_reg     = noreg;
   1.181 +  int      ref_kind       = MethodHandles::signature_polymorphic_intrinsic_ref_kind(special_dispatch);
   1.182 +  if (ref_kind != 0) {
   1.183 +    member_arg_pos = total_args_passed - 1;  // trailing MemberName argument
   1.184 +    member_reg = G5_method;  // known to be free at this point
   1.185 +    has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind);
   1.186 +  } else if (special_dispatch == vmIntrinsics::_invokeBasic) {
   1.187 +    has_receiver = true;
   1.188 +  } else {
   1.189 +    fatal(err_msg("special_dispatch=%d", special_dispatch));
   1.190 +  }
   1.191 +
   1.192 +  if (member_reg != noreg) {
   1.193 +    // Load the member_arg into register, if necessary.
   1.194 +    assert(member_arg_pos >= 0 && member_arg_pos < total_args_passed, "oob");
   1.195 +    assert(sig_bt[member_arg_pos] == T_OBJECT, "dispatch argument must be an object");
   1.196 +    VMReg r = regs[member_arg_pos].first();
   1.197 +    assert(r->is_valid(), "bad member arg");
   1.198 +    if (r->is_stack()) {
   1.199 +      RegisterOrConstant ld_off = reg2offset(r) + STACK_BIAS;
   1.200 +      ld_off = __ ensure_simm13_or_reg(ld_off, member_reg);
   1.201 +      __ ld_ptr(SP, ld_off, member_reg);
   1.202 +    } else {
   1.203 +      // no data motion is needed
   1.204 +      member_reg = r->as_Register();
   1.205 +    }
   1.206 +  }
   1.207 +
   1.208 +  if (has_receiver) {
   1.209 +    // Make sure the receiver is loaded into a register.
   1.210 +    assert(total_args_passed > 0, "oob");
   1.211 +    assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object");
   1.212 +    VMReg r = regs[0].first();
   1.213 +    assert(r->is_valid(), "bad receiver arg");
   1.214 +    if (r->is_stack()) {
   1.215 +      // Porting note:  This assumes that compiled calling conventions always
   1.216 +      // pass the receiver oop in a register.  If this is not true on some
   1.217 +      // platform, pick a temp and load the receiver from stack.
   1.218 +      assert(false, "receiver always in a register");
   1.219 +      receiver_reg = G3_scratch;  // known to be free at this point
   1.220 +      RegisterOrConstant ld_off = reg2offset(r) + STACK_BIAS;
   1.221 +      ld_off = __ ensure_simm13_or_reg(ld_off, member_reg);
   1.222 +      __ ld_ptr(SP, ld_off, receiver_reg);
   1.223 +    } else {
   1.224 +      // no data motion is needed
   1.225 +      receiver_reg = r->as_Register();
   1.226 +    }
   1.227 +  }
   1.228 +
   1.229 +  // Figure out which address we are really jumping to:
   1.230 +  MethodHandles::generate_method_handle_dispatch(masm, special_dispatch,
   1.231 +                                                 receiver_reg, member_reg, /*for_compiler_entry:*/ true);
   1.232 +}
   1.233 +
   1.234  // ---------------------------------------------------------------------------
   1.235  // Generate a native wrapper for a given method.  The method takes arguments
   1.236  // in the Java compiled code convention, marshals them to the native
   1.237  // convention (handlizes oops, etc), transitions to native, makes the call,
   1.238  // returns to java state (possibly blocking), unhandlizes any result and
   1.239  // returns.
   1.240 +//
   1.241 +// Critical native functions are a shorthand for the use of
   1.242 +// GetPrimtiveArrayCritical and disallow the use of any other JNI
   1.243 +// functions.  The wrapper is expected to unpack the arguments before
   1.244 +// passing them to the callee and perform checks before and after the
   1.245 +// native call to ensure that they GC_locker
   1.246 +// lock_critical/unlock_critical semantics are followed.  Some other
   1.247 +// parts of JNI setup are skipped like the tear down of the JNI handle
   1.248 +// block and the check for pending exceptions it's impossible for them
   1.249 +// to be thrown.
   1.250 +//
   1.251 +// They are roughly structured like this:
   1.252 +//    if (GC_locker::needs_gc())
   1.253 +//      SharedRuntime::block_for_jni_critical();
   1.254 +//    tranistion to thread_in_native
   1.255 +//    unpack arrray arguments and call native entry point
   1.256 +//    check for safepoint in progress
   1.257 +//    check if any thread suspend flags are set
   1.258 +//      call into JVM and possible unlock the JNI critical
   1.259 +//      if a GC was suppressed while in the critical native.
   1.260 +//    transition back to thread_in_Java
   1.261 +//    return to caller
   1.262 +//
   1.263  nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler* masm,
   1.264                                                  methodHandle method,
   1.265                                                  int compile_id,
   1.266                                                  int total_in_args,
   1.267                                                  int comp_args_on_stack, // in VMRegStackSlots
   1.268 -                                                BasicType *in_sig_bt,
   1.269 -                                                VMRegPair *in_regs,
   1.270 +                                                BasicType* in_sig_bt,
   1.271 +                                                VMRegPair* in_regs,
   1.272                                                  BasicType ret_type) {
   1.273 +  if (method->is_method_handle_intrinsic()) {
   1.274 +    vmIntrinsics::ID iid = method->intrinsic_id();
   1.275 +    intptr_t start = (intptr_t)__ pc();
   1.276 +    int vep_offset = ((intptr_t)__ pc()) - start;
   1.277 +    gen_special_dispatch(masm,
   1.278 +                         total_in_args,
   1.279 +                         comp_args_on_stack,
   1.280 +                         method->intrinsic_id(),
   1.281 +                         in_sig_bt,
   1.282 +                         in_regs);
   1.283 +    int frame_complete = ((intptr_t)__ pc()) - start;  // not complete, period
   1.284 +    __ flush();
   1.285 +    int stack_slots = SharedRuntime::out_preserve_stack_slots();  // no out slots at all, actually
   1.286 +    return nmethod::new_native_nmethod(method,
   1.287 +                                       compile_id,
   1.288 +                                       masm->code(),
   1.289 +                                       vep_offset,
   1.290 +                                       frame_complete,
   1.291 +                                       stack_slots / VMRegImpl::slots_per_word,
   1.292 +                                       in_ByteSize(-1),
   1.293 +                                       in_ByteSize(-1),
   1.294 +                                       (OopMapSet*)NULL);
   1.295 +  }
   1.296    bool is_critical_native = true;
   1.297    address native_func = method->critical_native_function();
   1.298    if (native_func == NULL) {

mercurial