1 /*
   2  * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
   3  * Copyright 2007, 2008, 2010 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21  * CA 95054 USA or visit www.sun.com if you need additional information or
  22  * have any questions.
  23  *
  24  */
  25 
  26 #include "incls/_precompiled.incl"
  27 #include "incls/_stubGenerator_zero.cpp.incl"
  28 
  29 // Declaration and definition of StubGenerator (no .hpp file).
  30 // For a more detailed description of the stub routine structure
  31 // see the comment in stubRoutines.hpp
  32 
  33 class StubGenerator: public StubCodeGenerator {
  34  private:
  35   // The call stub is used to call Java from C
  36   static void call_stub(
  37     JavaCallWrapper *call_wrapper,
  38     intptr_t*        result,
  39     BasicType        result_type,
  40     methodOop        method,
  41     address          entry_point,
  42     intptr_t*        parameters,
  43     int              parameter_words,
  44     TRAPS) {
  45     JavaThread *thread = (JavaThread *) THREAD;
  46     ZeroStack *stack = thread->zero_stack();
  47 
  48     // Make sure we have no pending exceptions
  49     assert(!HAS_PENDING_EXCEPTION, "call_stub called with pending exception");
  50 
  51     // Set up the stack if necessary
  52     bool stack_needs_teardown = false;
  53     if (stack->needs_setup()) {
  54       size_t zero_stack_size = stack->suggest_size(thread);
  55       stack->setup(alloca(zero_stack_size), zero_stack_size);
  56       stack_needs_teardown = true;
  57     }
  58 
  59     // Allocate and initialize our frame
  60     EntryFrame *frame =
  61       EntryFrame::build(parameters, parameter_words, call_wrapper, THREAD);
  62 
  63     if (!HAS_PENDING_EXCEPTION) {
  64       // Push the frame
  65       thread->push_zero_frame(frame);
  66 
  67       // Make the call
  68       Interpreter::invoke_method(method, entry_point, THREAD);
  69 
  70       // Store the result
  71       if (!HAS_PENDING_EXCEPTION) {
  72         switch (result_type) {
  73         case T_INT:
  74           *(jint *) result = *(jint *) stack->sp();
  75           break;
  76         case T_LONG:
  77           *(jlong *) result = *(jlong *) stack->sp();
  78           break;
  79         case T_FLOAT:
  80           *(jfloat *) result = *(jfloat *) stack->sp();
  81           break;
  82         case T_DOUBLE:
  83           *(jdouble *) result = *(jdouble *) stack->sp();
  84           break;
  85         case T_OBJECT:
  86           *(oop *) result = *(oop *) stack->sp();
  87           break;
  88         default:
  89           ShouldNotReachHere();
  90         }
  91       }
  92 
  93       // Unwind the frame
  94       thread->pop_zero_frame();
  95     }
  96 
  97     // Tear down the stack if necessary
  98     if (stack_needs_teardown)
  99       stack->teardown();
 100   }
 101 
 102   // These stubs get called from some dumb test routine.
 103   // I'll write them properly when they're called from
 104   // something that's actually doing something.
 105   static void fake_arraycopy_stub(address src, address dst, int count) {
 106     assert(count == 0, "huh?");
 107   }
 108 
 109   void generate_arraycopy_stubs() {
 110     // Call the conjoint generation methods immediately after
 111     // the disjoint ones so that short branches from the former
 112     // to the latter can be generated.
 113     StubRoutines::_jbyte_disjoint_arraycopy  = (address) fake_arraycopy_stub;
 114     StubRoutines::_jbyte_arraycopy           = (address) fake_arraycopy_stub;
 115 
 116     StubRoutines::_jshort_disjoint_arraycopy = (address) fake_arraycopy_stub;
 117     StubRoutines::_jshort_arraycopy          = (address) fake_arraycopy_stub;
 118 
 119     StubRoutines::_jint_disjoint_arraycopy   = (address) fake_arraycopy_stub;
 120     StubRoutines::_jint_arraycopy            = (address) fake_arraycopy_stub;
 121 
 122     StubRoutines::_jlong_disjoint_arraycopy  = (address) fake_arraycopy_stub;
 123     StubRoutines::_jlong_arraycopy           = (address) fake_arraycopy_stub;
 124 
 125     StubRoutines::_oop_disjoint_arraycopy    = ShouldNotCallThisStub();
 126     StubRoutines::_oop_arraycopy             = ShouldNotCallThisStub();
 127 
 128     StubRoutines::_checkcast_arraycopy       = ShouldNotCallThisStub();
 129     StubRoutines::_unsafe_arraycopy          = ShouldNotCallThisStub();
 130     StubRoutines::_generic_arraycopy         = ShouldNotCallThisStub();
 131 
 132     // We don't generate specialized code for HeapWord-aligned source
 133     // arrays, so just use the code we've already generated
 134     StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
 135       StubRoutines::_jbyte_disjoint_arraycopy;
 136     StubRoutines::_arrayof_jbyte_arraycopy =
 137       StubRoutines::_jbyte_arraycopy;
 138 
 139     StubRoutines::_arrayof_jshort_disjoint_arraycopy =
 140       StubRoutines::_jshort_disjoint_arraycopy;
 141     StubRoutines::_arrayof_jshort_arraycopy =
 142       StubRoutines::_jshort_arraycopy;
 143 
 144     StubRoutines::_arrayof_jint_disjoint_arraycopy =
 145       StubRoutines::_jint_disjoint_arraycopy;
 146     StubRoutines::_arrayof_jint_arraycopy =
 147       StubRoutines::_jint_arraycopy;
 148 
 149     StubRoutines::_arrayof_jlong_disjoint_arraycopy =
 150       StubRoutines::_jlong_disjoint_arraycopy;
 151     StubRoutines::_arrayof_jlong_arraycopy =
 152       StubRoutines::_jlong_arraycopy;
 153 
 154     StubRoutines::_arrayof_oop_disjoint_arraycopy =
 155       StubRoutines::_oop_disjoint_arraycopy;
 156     StubRoutines::_arrayof_oop_arraycopy =
 157       StubRoutines::_oop_arraycopy;
 158   }
 159 
 160   void generate_initial() {
 161     // Generates all stubs and initializes the entry points
 162 
 163     // entry points that exist in all platforms Note: This is code
 164     // that could be shared among different platforms - however the
 165     // benefit seems to be smaller than the disadvantage of having a
 166     // much more complicated generator structure. See also comment in
 167     // stubRoutines.hpp.
 168 
 169     StubRoutines::_forward_exception_entry   = ShouldNotCallThisStub();
 170     StubRoutines::_call_stub_entry           = (address) call_stub;
 171     StubRoutines::_catch_exception_entry     = ShouldNotCallThisStub();
 172 
 173     // atomic calls
 174     StubRoutines::_atomic_xchg_entry         = ShouldNotCallThisStub();
 175     StubRoutines::_atomic_xchg_ptr_entry     = ShouldNotCallThisStub();
 176     StubRoutines::_atomic_cmpxchg_entry      = ShouldNotCallThisStub();
 177     StubRoutines::_atomic_cmpxchg_ptr_entry  = ShouldNotCallThisStub();
 178     StubRoutines::_atomic_cmpxchg_long_entry = ShouldNotCallThisStub();
 179     StubRoutines::_atomic_add_entry          = ShouldNotCallThisStub();
 180     StubRoutines::_atomic_add_ptr_entry      = ShouldNotCallThisStub();
 181     StubRoutines::_fence_entry               = ShouldNotCallThisStub();
 182 
 183     // amd64 does this here, sparc does it in generate_all()
 184     StubRoutines::_handler_for_unsafe_access_entry =
 185       ShouldNotCallThisStub();
 186   }
 187 
 188   void generate_all() {
 189     // Generates all stubs and initializes the entry points
 190 
 191     // These entry points require SharedInfo::stack0 to be set up in
 192     // non-core builds and need to be relocatable, so they each
 193     // fabricate a RuntimeStub internally.
 194     StubRoutines::_throw_AbstractMethodError_entry =
 195       ShouldNotCallThisStub();
 196 
 197     StubRoutines::_throw_ArithmeticException_entry =
 198       ShouldNotCallThisStub();
 199 
 200     StubRoutines::_throw_NullPointerException_entry =
 201       ShouldNotCallThisStub();
 202 
 203     StubRoutines::_throw_NullPointerException_at_call_entry =
 204       ShouldNotCallThisStub();
 205 
 206     StubRoutines::_throw_StackOverflowError_entry =
 207       ShouldNotCallThisStub();
 208 
 209     // support for verify_oop (must happen after universe_init)
 210     StubRoutines::_verify_oop_subroutine_entry =
 211       ShouldNotCallThisStub();
 212 
 213     // arraycopy stubs used by compilers
 214     generate_arraycopy_stubs();
 215   }
 216 
 217  public:
 218   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
 219     if (all) {
 220       generate_all();
 221     } else {
 222       generate_initial();
 223     }
 224   }
 225 };
 226 
 227 void StubGenerator_generate(CodeBuffer* code, bool all) {
 228   StubGenerator g(code, all);
 229 }
 230 
 231 EntryFrame *EntryFrame::build(const intptr_t*  parameters,
 232                               int              parameter_words,
 233                               JavaCallWrapper* call_wrapper,
 234                               TRAPS) {
 235 
 236   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 237   stack->overflow_check(header_words + parameter_words, CHECK_NULL);
 238 
 239   stack->push(0); // next_frame, filled in later
 240   intptr_t *fp = stack->sp();
 241   assert(fp - stack->sp() == next_frame_off, "should be");
 242 
 243   stack->push(ENTRY_FRAME);
 244   assert(fp - stack->sp() == frame_type_off, "should be");
 245 
 246   stack->push((intptr_t) call_wrapper);
 247   assert(fp - stack->sp() == call_wrapper_off, "should be");
 248 
 249   for (int i = 0; i < parameter_words; i++)
 250     stack->push(parameters[i]);
 251 
 252   return (EntryFrame *) fp;
 253 }