src/share/vm/shark/sharkStack.cpp

Wed, 11 Aug 2010 05:51:21 -0700

author
twisti
date
Wed, 11 Aug 2010 05:51:21 -0700
changeset 2047
d2ede61b7a12
child 2314
f95d63e2154a
permissions
-rw-r--r--

6976186: integrate Shark HotSpot changes
Summary: Shark is a JIT compiler for Zero that uses the LLVM compiler infrastructure.
Reviewed-by: kvn, twisti
Contributed-by: Gary Benson <gbenson@redhat.com>

twisti@2047 1 /*
twisti@2047 2 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
twisti@2047 3 * Copyright 2008, 2009, 2010 Red Hat, Inc.
twisti@2047 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
twisti@2047 5 *
twisti@2047 6 * This code is free software; you can redistribute it and/or modify it
twisti@2047 7 * under the terms of the GNU General Public License version 2 only, as
twisti@2047 8 * published by the Free Software Foundation.
twisti@2047 9 *
twisti@2047 10 * This code is distributed in the hope that it will be useful, but WITHOUT
twisti@2047 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
twisti@2047 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
twisti@2047 13 * version 2 for more details (a copy is included in the LICENSE file that
twisti@2047 14 * accompanied this code).
twisti@2047 15 *
twisti@2047 16 * You should have received a copy of the GNU General Public License version
twisti@2047 17 * 2 along with this work; if not, write to the Free Software Foundation,
twisti@2047 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
twisti@2047 19 *
twisti@2047 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
twisti@2047 21 * or visit www.oracle.com if you need additional information or have any
twisti@2047 22 * questions.
twisti@2047 23 *
twisti@2047 24 */
twisti@2047 25
twisti@2047 26 #include "incls/_precompiled.incl"
twisti@2047 27 #include "incls/_sharkStack.cpp.incl"
twisti@2047 28
twisti@2047 29 using namespace llvm;
twisti@2047 30
twisti@2047 31 void SharkStack::initialize(Value* method) {
twisti@2047 32 bool setup_sp_and_method = (method != NULL);
twisti@2047 33
twisti@2047 34 int locals_words = max_locals();
twisti@2047 35 int extra_locals = locals_words - arg_size();
twisti@2047 36 int header_words = SharkFrame::header_words;
twisti@2047 37 int monitor_words = max_monitors()*frame::interpreter_frame_monitor_size();
twisti@2047 38 int stack_words = max_stack();
twisti@2047 39 int frame_words = header_words + monitor_words + stack_words;
twisti@2047 40
twisti@2047 41 _extended_frame_size = frame_words + locals_words;
twisti@2047 42
twisti@2047 43 // Update the stack pointer
twisti@2047 44 Value *stack_pointer = builder()->CreateSub(
twisti@2047 45 CreateLoadStackPointer(),
twisti@2047 46 LLVMValue::intptr_constant((frame_words + extra_locals) * wordSize));
twisti@2047 47 CreateStackOverflowCheck(stack_pointer);
twisti@2047 48 if (setup_sp_and_method)
twisti@2047 49 CreateStoreStackPointer(stack_pointer);
twisti@2047 50
twisti@2047 51 // Create the frame
twisti@2047 52 _frame = builder()->CreateIntToPtr(
twisti@2047 53 stack_pointer,
twisti@2047 54 PointerType::getUnqual(
twisti@2047 55 ArrayType::get(SharkType::intptr_type(), extended_frame_size())),
twisti@2047 56 "frame");
twisti@2047 57 int offset = 0;
twisti@2047 58
twisti@2047 59 // Expression stack
twisti@2047 60 _stack_slots_offset = offset;
twisti@2047 61 offset += stack_words;
twisti@2047 62
twisti@2047 63 // Monitors
twisti@2047 64 _monitors_slots_offset = offset;
twisti@2047 65 offset += monitor_words;
twisti@2047 66
twisti@2047 67 // Temporary oop slot
twisti@2047 68 _oop_tmp_slot_offset = offset++;
twisti@2047 69
twisti@2047 70 // Method pointer
twisti@2047 71 _method_slot_offset = offset++;
twisti@2047 72 if (setup_sp_and_method) {
twisti@2047 73 builder()->CreateStore(
twisti@2047 74 method, slot_addr(method_slot_offset(), SharkType::methodOop_type()));
twisti@2047 75 }
twisti@2047 76
twisti@2047 77 // Unextended SP
twisti@2047 78 builder()->CreateStore(stack_pointer, slot_addr(offset++));
twisti@2047 79
twisti@2047 80 // PC
twisti@2047 81 _pc_slot_offset = offset++;
twisti@2047 82
twisti@2047 83 // Frame header
twisti@2047 84 builder()->CreateStore(
twisti@2047 85 LLVMValue::intptr_constant(ZeroFrame::SHARK_FRAME), slot_addr(offset++));
twisti@2047 86 Value *fp = slot_addr(offset++);
twisti@2047 87
twisti@2047 88 // Local variables
twisti@2047 89 _locals_slots_offset = offset;
twisti@2047 90 offset += locals_words;
twisti@2047 91
twisti@2047 92 // Push the frame
twisti@2047 93 assert(offset == extended_frame_size(), "should do");
twisti@2047 94 builder()->CreateStore(CreateLoadFramePointer(), fp);
twisti@2047 95 CreateStoreFramePointer(
twisti@2047 96 builder()->CreatePtrToInt(fp, SharkType::intptr_type()));
twisti@2047 97 }
twisti@2047 98
twisti@2047 99 // This function should match ZeroStack::overflow_check
twisti@2047 100 void SharkStack::CreateStackOverflowCheck(Value* sp) {
twisti@2047 101 BasicBlock *zero_ok = CreateBlock("zero_stack_ok");
twisti@2047 102 BasicBlock *overflow = CreateBlock("stack_overflow");
twisti@2047 103 BasicBlock *abi_ok = CreateBlock("abi_stack_ok");
twisti@2047 104
twisti@2047 105 // Check the Zero stack
twisti@2047 106 builder()->CreateCondBr(
twisti@2047 107 builder()->CreateICmpULT(sp, stack_base()),
twisti@2047 108 overflow, zero_ok);
twisti@2047 109
twisti@2047 110 // Check the ABI stack
twisti@2047 111 builder()->SetInsertPoint(zero_ok);
twisti@2047 112 Value *stack_top = builder()->CreateSub(
twisti@2047 113 builder()->CreateValueOfStructEntry(
twisti@2047 114 thread(),
twisti@2047 115 Thread::stack_base_offset(),
twisti@2047 116 SharkType::intptr_type(),
twisti@2047 117 "abi_base"),
twisti@2047 118 builder()->CreateValueOfStructEntry(
twisti@2047 119 thread(),
twisti@2047 120 Thread::stack_size_offset(),
twisti@2047 121 SharkType::intptr_type(),
twisti@2047 122 "abi_size"));
twisti@2047 123 Value *free_stack = builder()->CreateSub(
twisti@2047 124 builder()->CreatePtrToInt(
twisti@2047 125 builder()->CreateGetFrameAddress(),
twisti@2047 126 SharkType::intptr_type(),
twisti@2047 127 "abi_sp"),
twisti@2047 128 stack_top);
twisti@2047 129 builder()->CreateCondBr(
twisti@2047 130 builder()->CreateICmpULT(
twisti@2047 131 free_stack,
twisti@2047 132 LLVMValue::intptr_constant(StackShadowPages * os::vm_page_size())),
twisti@2047 133 overflow, abi_ok);
twisti@2047 134
twisti@2047 135 // Handle overflows
twisti@2047 136 builder()->SetInsertPoint(overflow);
twisti@2047 137 builder()->CreateCall(builder()->throw_StackOverflowError(), thread());
twisti@2047 138 builder()->CreateRet(LLVMValue::jint_constant(0));
twisti@2047 139
twisti@2047 140 builder()->SetInsertPoint(abi_ok);
twisti@2047 141 }
twisti@2047 142
twisti@2047 143 Value* SharkStack::CreatePopFrame(int result_slots) {
twisti@2047 144 assert(result_slots >= 0 && result_slots <= 2, "should be");
twisti@2047 145 int locals_to_pop = max_locals() - result_slots;
twisti@2047 146
twisti@2047 147 Value *fp = CreateLoadFramePointer();
twisti@2047 148 Value *sp = builder()->CreateAdd(
twisti@2047 149 fp,
twisti@2047 150 LLVMValue::intptr_constant((1 + locals_to_pop) * wordSize));
twisti@2047 151
twisti@2047 152 CreateStoreStackPointer(sp);
twisti@2047 153 CreateStoreFramePointer(
twisti@2047 154 builder()->CreateLoad(
twisti@2047 155 builder()->CreateIntToPtr(
twisti@2047 156 fp, PointerType::getUnqual(SharkType::intptr_type()))));
twisti@2047 157
twisti@2047 158 return sp;
twisti@2047 159 }
twisti@2047 160
twisti@2047 161 Value* SharkStack::slot_addr(int offset,
twisti@2047 162 const Type* type,
twisti@2047 163 const char* name) const {
twisti@2047 164 bool needs_cast = type && type != SharkType::intptr_type();
twisti@2047 165
twisti@2047 166 Value* result = builder()->CreateStructGEP(
twisti@2047 167 _frame, offset, needs_cast ? "" : name);
twisti@2047 168
twisti@2047 169 if (needs_cast) {
twisti@2047 170 result = builder()->CreateBitCast(
twisti@2047 171 result, PointerType::getUnqual(type), name);
twisti@2047 172 }
twisti@2047 173 return result;
twisti@2047 174 }
twisti@2047 175
twisti@2047 176 // The bits that differentiate stacks with normal and native frames on top
twisti@2047 177
twisti@2047 178 SharkStack* SharkStack::CreateBuildAndPushFrame(SharkFunction* function,
twisti@2047 179 Value* method) {
twisti@2047 180 return new SharkStackWithNormalFrame(function, method);
twisti@2047 181 }
twisti@2047 182 SharkStack* SharkStack::CreateBuildAndPushFrame(SharkNativeWrapper* wrapper,
twisti@2047 183 Value* method) {
twisti@2047 184 return new SharkStackWithNativeFrame(wrapper, method);
twisti@2047 185 }
twisti@2047 186
twisti@2047 187 SharkStackWithNormalFrame::SharkStackWithNormalFrame(SharkFunction* function,
twisti@2047 188 Value* method)
twisti@2047 189 : SharkStack(function), _function(function) {
twisti@2047 190 // For normal frames, the stack pointer and the method slot will
twisti@2047 191 // be set during each decache, so it is not necessary to do them
twisti@2047 192 // at the time the frame is created. However, we set them for
twisti@2047 193 // non-PRODUCT builds to make crash dumps easier to understand.
twisti@2047 194 initialize(PRODUCT_ONLY(NULL) NOT_PRODUCT(method));
twisti@2047 195 }
twisti@2047 196 SharkStackWithNativeFrame::SharkStackWithNativeFrame(SharkNativeWrapper* wrp,
twisti@2047 197 Value* method)
twisti@2047 198 : SharkStack(wrp), _wrapper(wrp) {
twisti@2047 199 initialize(method);
twisti@2047 200 }
twisti@2047 201
twisti@2047 202 int SharkStackWithNormalFrame::arg_size() const {
twisti@2047 203 return function()->arg_size();
twisti@2047 204 }
twisti@2047 205 int SharkStackWithNativeFrame::arg_size() const {
twisti@2047 206 return wrapper()->arg_size();
twisti@2047 207 }
twisti@2047 208
twisti@2047 209 int SharkStackWithNormalFrame::max_locals() const {
twisti@2047 210 return function()->max_locals();
twisti@2047 211 }
twisti@2047 212 int SharkStackWithNativeFrame::max_locals() const {
twisti@2047 213 return wrapper()->arg_size();
twisti@2047 214 }
twisti@2047 215
twisti@2047 216 int SharkStackWithNormalFrame::max_stack() const {
twisti@2047 217 return function()->max_stack();
twisti@2047 218 }
twisti@2047 219 int SharkStackWithNativeFrame::max_stack() const {
twisti@2047 220 return 0;
twisti@2047 221 }
twisti@2047 222
twisti@2047 223 int SharkStackWithNormalFrame::max_monitors() const {
twisti@2047 224 return function()->max_monitors();
twisti@2047 225 }
twisti@2047 226 int SharkStackWithNativeFrame::max_monitors() const {
twisti@2047 227 return wrapper()->is_synchronized() ? 1 : 0;
twisti@2047 228 }
twisti@2047 229
twisti@2047 230 BasicBlock* SharkStackWithNormalFrame::CreateBlock(const char* name) const {
twisti@2047 231 return function()->CreateBlock(name);
twisti@2047 232 }
twisti@2047 233 BasicBlock* SharkStackWithNativeFrame::CreateBlock(const char* name) const {
twisti@2047 234 return wrapper()->CreateBlock(name);
twisti@2047 235 }
twisti@2047 236
twisti@2047 237 address SharkStackWithNormalFrame::interpreter_entry_point() const {
twisti@2047 238 return (address) CppInterpreter::normal_entry;
twisti@2047 239 }
twisti@2047 240 address SharkStackWithNativeFrame::interpreter_entry_point() const {
twisti@2047 241 return (address) CppInterpreter::native_entry;
twisti@2047 242 }
twisti@2047 243
twisti@2047 244 #ifndef PRODUCT
twisti@2047 245 void SharkStack::CreateAssertLastJavaSPIsNull() const {
twisti@2047 246 #ifdef ASSERT
twisti@2047 247 BasicBlock *fail = CreateBlock("assert_failed");
twisti@2047 248 BasicBlock *pass = CreateBlock("assert_ok");
twisti@2047 249
twisti@2047 250 builder()->CreateCondBr(
twisti@2047 251 builder()->CreateICmpEQ(
twisti@2047 252 builder()->CreateLoad(last_Java_sp_addr()),
twisti@2047 253 LLVMValue::intptr_constant(0)),
twisti@2047 254 pass, fail);
twisti@2047 255
twisti@2047 256 builder()->SetInsertPoint(fail);
twisti@2047 257 builder()->CreateShouldNotReachHere(__FILE__, __LINE__);
twisti@2047 258 builder()->CreateUnreachable();
twisti@2047 259
twisti@2047 260 builder()->SetInsertPoint(pass);
twisti@2047 261 #endif // ASSERT
twisti@2047 262 }
twisti@2047 263 #endif // !PRODUCT

mercurial