duke@435: /* mikael@4153: * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "c1/c1_FrameMap.hpp" stefank@2314: #include "c1/c1_LIR.hpp" stefank@2314: #include "runtime/sharedRuntime.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "vmreg_x86.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "vmreg_sparc.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "vmreg_zero.inline.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "vmreg_arm.inline.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "vmreg_ppc.inline.hpp" bobv@2508: #endif duke@435: duke@435: duke@435: duke@435: //----------------------------------------------------- duke@435: duke@435: // Convert method signature into an array of BasicTypes for the arguments duke@435: BasicTypeArray* FrameMap::signature_type_array_for(const ciMethod* method) { duke@435: ciSignature* sig = method->signature(); duke@435: BasicTypeList* sta = new BasicTypeList(method->arg_size()); duke@435: // add receiver, if any duke@435: if (!method->is_static()) sta->append(T_OBJECT); duke@435: // add remaining arguments duke@435: for (int i = 0; i < sig->count(); i++) { duke@435: ciType* type = sig->type_at(i); duke@435: BasicType t = type->basic_type(); duke@435: if (t == T_ARRAY) { duke@435: t = T_OBJECT; duke@435: } duke@435: sta->append(t); duke@435: } duke@435: // done duke@435: return sta; duke@435: } duke@435: duke@435: duke@435: CallingConvention* FrameMap::java_calling_convention(const BasicTypeArray* signature, bool outgoing) { duke@435: // compute the size of the arguments first. The signature array duke@435: // that java_calling_convention takes includes a T_VOID after double duke@435: // work items but our signatures do not. duke@435: int i; duke@435: int sizeargs = 0; duke@435: for (i = 0; i < signature->length(); i++) { duke@435: sizeargs += type2size[signature->at(i)]; duke@435: } duke@435: duke@435: BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); duke@435: VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); duke@435: int sig_index = 0; duke@435: for (i = 0; i < sizeargs; i++, sig_index++) { duke@435: sig_bt[i] = signature->at(sig_index); duke@435: if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) { duke@435: sig_bt[i + 1] = T_VOID; duke@435: i++; duke@435: } duke@435: } duke@435: duke@435: intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, outgoing); duke@435: LIR_OprList* args = new LIR_OprList(signature->length()); duke@435: for (i = 0; i < sizeargs;) { duke@435: BasicType t = sig_bt[i]; duke@435: assert(t != T_VOID, "should be skipping these"); duke@435: LIR_Opr opr = map_to_opr(t, regs + i, outgoing); duke@435: args->append(opr); duke@435: if (opr->is_address()) { duke@435: LIR_Address* addr = opr->as_address_ptr(); duke@435: assert(addr->disp() == (int)addr->disp(), "out of range value"); iveresov@2416: out_preserve = MAX2(out_preserve, (intptr_t)(addr->disp() - STACK_BIAS) / 4); duke@435: } duke@435: i += type2size[t]; duke@435: } duke@435: assert(args->length() == signature->length(), "size mismatch"); duke@435: out_preserve += SharedRuntime::out_preserve_stack_slots(); duke@435: duke@435: if (outgoing) { duke@435: // update the space reserved for arguments. bobv@2036: update_reserved_argument_area_size(out_preserve * BytesPerWord); duke@435: } duke@435: return new CallingConvention(args, out_preserve); duke@435: } duke@435: duke@435: duke@435: CallingConvention* FrameMap::c_calling_convention(const BasicTypeArray* signature) { duke@435: // compute the size of the arguments first. The signature array duke@435: // that java_calling_convention takes includes a T_VOID after double duke@435: // work items but our signatures do not. duke@435: int i; duke@435: int sizeargs = 0; duke@435: for (i = 0; i < signature->length(); i++) { duke@435: sizeargs += type2size[signature->at(i)]; duke@435: } duke@435: duke@435: BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs); duke@435: VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs); duke@435: int sig_index = 0; duke@435: for (i = 0; i < sizeargs; i++, sig_index++) { duke@435: sig_bt[i] = signature->at(sig_index); duke@435: if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) { duke@435: sig_bt[i + 1] = T_VOID; duke@435: i++; duke@435: } duke@435: } duke@435: goetz@6466: intptr_t out_preserve = SharedRuntime::c_calling_convention(sig_bt, regs, NULL, sizeargs); duke@435: LIR_OprList* args = new LIR_OprList(signature->length()); duke@435: for (i = 0; i < sizeargs;) { duke@435: BasicType t = sig_bt[i]; duke@435: assert(t != T_VOID, "should be skipping these"); duke@435: duke@435: // C calls are always outgoing duke@435: bool outgoing = true; duke@435: LIR_Opr opr = map_to_opr(t, regs + i, outgoing); duke@435: // they might be of different types if for instance floating point duke@435: // values are passed in cpu registers, but the sizes must match. duke@435: assert(type2size[opr->type()] == type2size[t], "type mismatch"); duke@435: args->append(opr); duke@435: if (opr->is_address()) { duke@435: LIR_Address* addr = opr->as_address_ptr(); iveresov@2416: out_preserve = MAX2(out_preserve, (intptr_t)(addr->disp() - STACK_BIAS) / 4); duke@435: } duke@435: i += type2size[t]; duke@435: } duke@435: assert(args->length() == signature->length(), "size mismatch"); duke@435: out_preserve += SharedRuntime::out_preserve_stack_slots(); bobv@2036: update_reserved_argument_area_size(out_preserve * BytesPerWord); duke@435: return new CallingConvention(args, out_preserve); duke@435: } duke@435: duke@435: duke@435: //-------------------------------------------------------- duke@435: // FrameMap duke@435: //-------------------------------------------------------- duke@435: duke@435: bool FrameMap::_init_done = false; duke@435: Register FrameMap::_cpu_rnr2reg [FrameMap::nof_cpu_regs]; duke@435: int FrameMap::_cpu_reg2rnr [FrameMap::nof_cpu_regs]; duke@435: duke@435: duke@435: FrameMap::FrameMap(ciMethod* method, int monitors, int reserved_argument_area_size) { iveresov@1939: assert(_init_done, "should already be completed"); duke@435: duke@435: _framesize = -1; duke@435: _num_spills = -1; duke@435: duke@435: assert(monitors >= 0, "not set"); duke@435: _num_monitors = monitors; duke@435: assert(reserved_argument_area_size >= 0, "not set"); duke@435: _reserved_argument_area_size = MAX2(4, reserved_argument_area_size) * BytesPerWord; duke@435: duke@435: _argcount = method->arg_size(); duke@435: _argument_locations = new intArray(_argcount, -1); duke@435: _incoming_arguments = java_calling_convention(signature_type_array_for(method), false); duke@435: _oop_map_arg_count = _incoming_arguments->reserved_stack_slots(); duke@435: duke@435: int java_index = 0; duke@435: for (int i = 0; i < _incoming_arguments->length(); i++) { duke@435: LIR_Opr opr = _incoming_arguments->at(i); duke@435: if (opr->is_address()) { duke@435: LIR_Address* address = opr->as_address_ptr(); duke@435: _argument_locations->at_put(java_index, address->disp() - STACK_BIAS); duke@435: _incoming_arguments->args()->at_put(i, LIR_OprFact::stack(java_index, as_BasicType(as_ValueType(address->type())))); duke@435: } duke@435: java_index += type2size[opr->type()]; duke@435: } duke@435: duke@435: } duke@435: duke@435: duke@435: bool FrameMap::finalize_frame(int nof_slots) { duke@435: assert(nof_slots >= 0, "must be positive"); duke@435: assert(_num_spills == -1, "can only be set once"); duke@435: _num_spills = nof_slots; duke@435: assert(_framesize == -1, "should only be calculated once"); duke@435: _framesize = round_to(in_bytes(sp_offset_for_monitor_base(0)) + duke@435: _num_monitors * sizeof(BasicObjectLock) + duke@435: sizeof(intptr_t) + // offset of deopt orig pc duke@435: frame_pad_in_bytes, duke@435: StackAlignmentInBytes) / 4; duke@435: int java_index = 0; duke@435: for (int i = 0; i < _incoming_arguments->length(); i++) { duke@435: LIR_Opr opr = _incoming_arguments->at(i); duke@435: if (opr->is_stack()) { duke@435: _argument_locations->at_put(java_index, in_bytes(framesize_in_bytes()) + duke@435: _argument_locations->at(java_index)); duke@435: } duke@435: java_index += type2size[opr->type()]; duke@435: } duke@435: // make sure it's expressible on the platform duke@435: return validate_frame(); duke@435: } duke@435: duke@435: VMReg FrameMap::sp_offset2vmreg(ByteSize offset) const { duke@435: int offset_in_bytes = in_bytes(offset); duke@435: assert(offset_in_bytes % 4 == 0, "must be multiple of 4 bytes"); duke@435: assert(offset_in_bytes / 4 < framesize() + oop_map_arg_count(), "out of range"); duke@435: return VMRegImpl::stack2reg(offset_in_bytes / 4); duke@435: } duke@435: duke@435: duke@435: bool FrameMap::location_for_sp_offset(ByteSize byte_offset_from_sp, duke@435: Location::Type loc_type, duke@435: Location* loc) const { duke@435: int offset = in_bytes(byte_offset_from_sp); duke@435: assert(offset >= 0, "incorrect offset"); duke@435: if (!Location::legal_offset_in_bytes(offset)) { duke@435: return false; duke@435: } duke@435: Location tmp_loc = Location::new_stk_loc(loc_type, offset); duke@435: *loc = tmp_loc; duke@435: return true; duke@435: } duke@435: duke@435: duke@435: bool FrameMap::locations_for_slot (int index, Location::Type loc_type, duke@435: Location* loc, Location* second) const { duke@435: ByteSize offset_from_sp = sp_offset_for_slot(index); duke@435: if (!location_for_sp_offset(offset_from_sp, loc_type, loc)) { duke@435: return false; duke@435: } duke@435: if (second != NULL) { duke@435: // two word item duke@435: offset_from_sp = offset_from_sp + in_ByteSize(4); duke@435: return location_for_sp_offset(offset_from_sp, loc_type, second); duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: ////////////////////// duke@435: // Public accessors // duke@435: ////////////////////// duke@435: duke@435: duke@435: ByteSize FrameMap::sp_offset_for_slot(const int index) const { duke@435: if (index < argcount()) { duke@435: int offset = _argument_locations->at(index); duke@435: assert(offset != -1, "not a memory argument"); duke@435: assert(offset >= framesize() * 4, "argument inside of frame"); duke@435: return in_ByteSize(offset); duke@435: } duke@435: ByteSize offset = sp_offset_for_spill(index - argcount()); duke@435: assert(in_bytes(offset) < framesize() * 4, "spill outside of frame"); duke@435: return offset; duke@435: } duke@435: duke@435: duke@435: ByteSize FrameMap::sp_offset_for_double_slot(const int index) const { duke@435: ByteSize offset = sp_offset_for_slot(index); duke@435: if (index >= argcount()) { duke@435: assert(in_bytes(offset) + 4 < framesize() * 4, "spill outside of frame"); duke@435: } duke@435: return offset; duke@435: } duke@435: duke@435: duke@435: ByteSize FrameMap::sp_offset_for_spill(const int index) const { duke@435: assert(index >= 0 && index < _num_spills, "out of range"); duke@435: int offset = round_to(first_available_sp_in_frame + _reserved_argument_area_size, sizeof(double)) + duke@435: index * spill_slot_size_in_bytes; duke@435: return in_ByteSize(offset); duke@435: } duke@435: duke@435: ByteSize FrameMap::sp_offset_for_monitor_base(const int index) const { duke@435: int end_of_spills = round_to(first_available_sp_in_frame + _reserved_argument_area_size, sizeof(double)) + duke@435: _num_spills * spill_slot_size_in_bytes; never@739: int offset = (int) round_to(end_of_spills, HeapWordSize) + index * sizeof(BasicObjectLock); duke@435: return in_ByteSize(offset); duke@435: } duke@435: duke@435: ByteSize FrameMap::sp_offset_for_monitor_lock(int index) const { duke@435: check_monitor_index(index); duke@435: return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::lock_offset_in_bytes());; duke@435: } duke@435: duke@435: ByteSize FrameMap::sp_offset_for_monitor_object(int index) const { duke@435: check_monitor_index(index); duke@435: return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::obj_offset_in_bytes()); duke@435: } duke@435: duke@435: duke@435: // For OopMaps, map a local variable or spill index to an VMReg. duke@435: // This is the offset from sp() in the frame of the slot for the index, duke@435: // skewed by SharedInfo::stack0 to indicate a stack location (vs.a register.) duke@435: // duke@435: // C ABI size + duke@435: // framesize + framesize + duke@435: // stack0 stack0 stack0 0 <- VMReg->value() duke@435: // | | | | duke@435: // ..........|..............|..............|.............| duke@435: // 0 1 2 3 | | 4 5 6 ...... | <- local indices duke@435: // ^ ^ sp() duke@435: // | | duke@435: // arguments non-argument locals duke@435: duke@435: duke@435: VMReg FrameMap::regname(LIR_Opr opr) const { duke@435: if (opr->is_single_cpu()) { duke@435: assert(!opr->is_virtual(), "should not see virtual registers here"); duke@435: return opr->as_register()->as_VMReg(); duke@435: } else if (opr->is_single_stack()) { duke@435: return sp_offset2vmreg(sp_offset_for_slot(opr->single_stack_ix())); duke@435: } else if (opr->is_address()) { duke@435: LIR_Address* addr = opr->as_address_ptr(); duke@435: assert(addr->base() == stack_pointer(), "sp based addressing only"); duke@435: return sp_offset2vmreg(in_ByteSize(addr->index()->as_jint())); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return VMRegImpl::Bad(); duke@435: } duke@435: duke@435: duke@435: duke@435: duke@435: // ------------ extra spill slots ---------------