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