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