sharkRuntime.cpp revision 6646:b596a1063e90
1261639Sbr/*
2261639Sbr * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
3261639Sbr * Copyright 2008, 2009, 2010 Red Hat, Inc.
4261639Sbr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5261639Sbr *
6261639Sbr * This code is free software; you can redistribute it and/or modify it
7261639Sbr * under the terms of the GNU General Public License version 2 only, as
8261639Sbr * published by the Free Software Foundation.
9261639Sbr *
10261639Sbr * This code is distributed in the hope that it will be useful, but WITHOUT
11261639Sbr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12261639Sbr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13261639Sbr * version 2 for more details (a copy is included in the LICENSE file that
14261639Sbr * accompanied this code).
15261639Sbr *
16261639Sbr * You should have received a copy of the GNU General Public License version
17261639Sbr * 2 along with this work; if not, write to the Free Software Foundation,
18261639Sbr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19261639Sbr *
20261639Sbr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21261639Sbr * or visit www.oracle.com if you need additional information or have any
22261639Sbr * questions.
23261639Sbr *
24261639Sbr */
25261639Sbr
26261639Sbr#include "precompiled.hpp"
27261639Sbr#include "runtime/atomic.inline.hpp"
28261639Sbr#include "runtime/biasedLocking.hpp"
29261639Sbr#include "runtime/deoptimization.hpp"
30261639Sbr#include "runtime/thread.hpp"
31261639Sbr#include "shark/llvmHeaders.hpp"
32261639Sbr#include "shark/sharkRuntime.hpp"
33261639Sbr#ifdef TARGET_ARCH_zero
34261639Sbr# include "stack_zero.inline.hpp"
35261639Sbr#endif
36261639Sbr
37261639Sbrusing namespace llvm;
38261639Sbr
39261639SbrJRT_ENTRY(int, SharkRuntime::find_exception_handler(JavaThread* thread,
40261639Sbr                                                    int*        indexes,
41261639Sbr                                                    int         num_indexes))
42261639Sbr  constantPoolHandle pool(thread, method(thread)->constants());
43261639Sbr  KlassHandle exc_klass(thread, ((oop) tos_at(thread, 0))->klass());
44261639Sbr
45261639Sbr  for (int i = 0; i < num_indexes; i++) {
46261639Sbr    Klass* tmp = pool->klass_at(indexes[i], CHECK_0);
47261639Sbr    KlassHandle chk_klass(thread, tmp);
48261639Sbr
49    if (exc_klass() == chk_klass())
50      return i;
51
52    if (exc_klass()->is_subtype_of(chk_klass()))
53      return i;
54  }
55
56  return -1;
57JRT_END
58
59JRT_ENTRY(void, SharkRuntime::monitorenter(JavaThread*      thread,
60                                           BasicObjectLock* lock))
61  if (PrintBiasedLockingStatistics)
62    Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
63
64  Handle object(thread, lock->obj());
65  assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
66  if (UseBiasedLocking) {
67    // Retry fast entry if bias is revoked to avoid unnecessary inflation
68    ObjectSynchronizer::fast_enter(object, lock->lock(), true, CHECK);
69  } else {
70    ObjectSynchronizer::slow_enter(object, lock->lock(), CHECK);
71  }
72  assert(Universe::heap()->is_in_reserved_or_null(lock->obj()), "should be");
73JRT_END
74
75JRT_ENTRY(void, SharkRuntime::monitorexit(JavaThread*      thread,
76                                          BasicObjectLock* lock))
77  Handle object(thread, lock->obj());
78  assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
79  if (lock == NULL || object()->is_unlocked()) {
80    THROW(vmSymbols::java_lang_IllegalMonitorStateException());
81  }
82  ObjectSynchronizer::slow_exit(object(), lock->lock(), thread);
83JRT_END
84
85JRT_ENTRY(void, SharkRuntime::new_instance(JavaThread* thread, int index))
86  Klass* k_oop = method(thread)->constants()->klass_at(index, CHECK);
87  instanceKlassHandle klass(THREAD, k_oop);
88
89  // Make sure we are not instantiating an abstract klass
90  klass->check_valid_for_instantiation(true, CHECK);
91
92  // Make sure klass is initialized
93  klass->initialize(CHECK);
94
95  // At this point the class may not be fully initialized
96  // because of recursive initialization. If it is fully
97  // initialized & has_finalized is not set, we rewrite
98  // it into its fast version (Note: no locking is needed
99  // here since this is an atomic byte write and can be
100  // done more than once).
101  //
102  // Note: In case of classes with has_finalized we don't
103  //       rewrite since that saves us an extra check in
104  //       the fast version which then would call the
105  //       slow version anyway (and do a call back into
106  //       Java).
107  //       If we have a breakpoint, then we don't rewrite
108  //       because the _breakpoint bytecode would be lost.
109  oop obj = klass->allocate_instance(CHECK);
110  thread->set_vm_result(obj);
111JRT_END
112
113JRT_ENTRY(void, SharkRuntime::newarray(JavaThread* thread,
114                                       BasicType   type,
115                                       int         size))
116  oop obj = oopFactory::new_typeArray(type, size, CHECK);
117  thread->set_vm_result(obj);
118JRT_END
119
120JRT_ENTRY(void, SharkRuntime::anewarray(JavaThread* thread,
121                                        int         index,
122                                        int         size))
123  Klass* klass = method(thread)->constants()->klass_at(index, CHECK);
124  objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
125  thread->set_vm_result(obj);
126JRT_END
127
128JRT_ENTRY(void, SharkRuntime::multianewarray(JavaThread* thread,
129                                             int         index,
130                                             int         ndims,
131                                             int*        dims))
132  Klass* klass = method(thread)->constants()->klass_at(index, CHECK);
133  oop obj = ArrayKlass::cast(klass)->multi_allocate(ndims, dims, CHECK);
134  thread->set_vm_result(obj);
135JRT_END
136
137JRT_ENTRY(void, SharkRuntime::register_finalizer(JavaThread* thread,
138                                                 oop         object))
139  assert(object->is_oop(), "should be");
140  assert(object->klass()->has_finalizer(), "should have");
141  InstanceKlass::register_finalizer(instanceOop(object), CHECK);
142JRT_END
143
144JRT_ENTRY(void, SharkRuntime::throw_ArithmeticException(JavaThread* thread,
145                                                        const char* file,
146                                                        int         line))
147  Exceptions::_throw_msg(
148    thread, file, line,
149    vmSymbols::java_lang_ArithmeticException(),
150    "");
151JRT_END
152
153JRT_ENTRY(void, SharkRuntime::throw_ArrayIndexOutOfBoundsException(
154                                                     JavaThread* thread,
155                                                     const char* file,
156                                                     int         line,
157                                                     int         index))
158  char msg[jintAsStringSize];
159  snprintf(msg, sizeof(msg), "%d", index);
160  Exceptions::_throw_msg(
161    thread, file, line,
162    vmSymbols::java_lang_ArrayIndexOutOfBoundsException(),
163    msg);
164JRT_END
165
166JRT_ENTRY(void, SharkRuntime::throw_ClassCastException(JavaThread* thread,
167                                                       const char* file,
168                                                       int         line))
169  Exceptions::_throw_msg(
170    thread, file, line,
171    vmSymbols::java_lang_ClassCastException(),
172    "");
173JRT_END
174
175JRT_ENTRY(void, SharkRuntime::throw_NullPointerException(JavaThread* thread,
176                                                         const char* file,
177                                                         int         line))
178  Exceptions::_throw_msg(
179    thread, file, line,
180    vmSymbols::java_lang_NullPointerException(),
181    "");
182JRT_END
183
184// Non-VM calls
185// Nothing in these must ever GC!
186
187void SharkRuntime::dump(const char *name, intptr_t value) {
188  oop valueOop = (oop) value;
189  tty->print("%s = ", name);
190  if (valueOop->is_oop(true))
191    valueOop->print_on(tty);
192  else if (value >= ' ' && value <= '~')
193    tty->print("'%c' (%d)", value, value);
194  else
195    tty->print("%p", value);
196  tty->print_cr("");
197}
198
199bool SharkRuntime::is_subtype_of(Klass* check_klass, Klass* object_klass) {
200  return object_klass->is_subtype_of(check_klass);
201}
202
203int SharkRuntime::uncommon_trap(JavaThread* thread, int trap_request) {
204  Thread *THREAD = thread;
205
206  // In C2, uncommon_trap_blob creates a frame, so all the various
207  // deoptimization functions expect to find the frame of the method
208  // being deopted one frame down on the stack.  We create a dummy
209  // frame to mirror this.
210  FakeStubFrame *stubframe = FakeStubFrame::build(CHECK_0);
211  thread->push_zero_frame(stubframe);
212
213  // Initiate the trap
214  thread->set_last_Java_frame();
215  Deoptimization::UnrollBlock *urb =
216    Deoptimization::uncommon_trap(thread, trap_request);
217  thread->reset_last_Java_frame();
218
219  // Pop our dummy frame and the frame being deoptimized
220  thread->pop_zero_frame();
221  thread->pop_zero_frame();
222
223  // Push skeleton frames
224  int number_of_frames = urb->number_of_frames();
225  for (int i = 0; i < number_of_frames; i++) {
226    intptr_t size = urb->frame_sizes()[i];
227    InterpreterFrame *frame = InterpreterFrame::build(size, CHECK_0);
228    thread->push_zero_frame(frame);
229  }
230
231  // Push another dummy frame
232  stubframe = FakeStubFrame::build(CHECK_0);
233  thread->push_zero_frame(stubframe);
234
235  // Fill in the skeleton frames
236  thread->set_last_Java_frame();
237  Deoptimization::unpack_frames(thread, Deoptimization::Unpack_uncommon_trap);
238  thread->reset_last_Java_frame();
239
240  // Pop our dummy frame
241  thread->pop_zero_frame();
242
243  // Fall back into the interpreter
244  return number_of_frames;
245}
246
247FakeStubFrame* FakeStubFrame::build(TRAPS) {
248  ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
249  stack->overflow_check(header_words, CHECK_NULL);
250
251  stack->push(0); // next_frame, filled in later
252  intptr_t *fp = stack->sp();
253  assert(fp - stack->sp() == next_frame_off, "should be");
254
255  stack->push(FAKE_STUB_FRAME);
256  assert(fp - stack->sp() == frame_type_off, "should be");
257
258  return (FakeStubFrame *) fp;
259}
260