Path: blob/master/src/hotspot/share/c1/c1_FrameMap.cpp
40931 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "c1/c1_FrameMap.hpp"26#include "c1/c1_LIR.hpp"27#include "code/vmreg.inline.hpp"28#include "runtime/sharedRuntime.hpp"29#include "utilities/align.hpp"3031//-----------------------------------------------------3233// Convert method signature into an array of BasicTypes for the arguments34BasicTypeArray* FrameMap::signature_type_array_for(const ciMethod* method) {35ciSignature* sig = method->signature();36BasicTypeList* sta = new BasicTypeList(method->arg_size());37// add receiver, if any38if (!method->is_static()) sta->append(T_OBJECT);39// add remaining arguments40for (int i = 0; i < sig->count(); i++) {41ciType* type = sig->type_at(i);42BasicType t = type->basic_type();43if (t == T_ARRAY) {44t = T_OBJECT;45}46sta->append(t);47}48// done49return sta;50}515253CallingConvention* FrameMap::java_calling_convention(const BasicTypeArray* signature, bool outgoing) {54// compute the size of the arguments first. The signature array55// that java_calling_convention takes includes a T_VOID after double56// work items but our signatures do not.57int i;58int sizeargs = 0;59for (i = 0; i < signature->length(); i++) {60sizeargs += type2size[signature->at(i)];61}6263BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);64VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);65int sig_index = 0;66for (i = 0; i < sizeargs; i++, sig_index++) {67sig_bt[i] = signature->at(sig_index);68if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {69sig_bt[i + 1] = T_VOID;70i++;71}72}7374intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);75LIR_OprList* args = new LIR_OprList(signature->length());76for (i = 0; i < sizeargs;) {77BasicType t = sig_bt[i];78assert(t != T_VOID, "should be skipping these");79LIR_Opr opr = map_to_opr(t, regs + i, outgoing);80args->append(opr);81if (opr->is_address()) {82LIR_Address* addr = opr->as_address_ptr();83assert(addr->disp() == (int)addr->disp(), "out of range value");84out_preserve = MAX2(out_preserve, (intptr_t)addr->disp() / 4);85}86i += type2size[t];87}88assert(args->length() == signature->length(), "size mismatch");89out_preserve += SharedRuntime::out_preserve_stack_slots();9091if (outgoing) {92// update the space reserved for arguments.93update_reserved_argument_area_size(out_preserve * BytesPerWord);94}95return new CallingConvention(args, out_preserve);96}979899CallingConvention* FrameMap::c_calling_convention(const BasicTypeArray* signature) {100// compute the size of the arguments first. The signature array101// that java_calling_convention takes includes a T_VOID after double102// work items but our signatures do not.103int i;104int sizeargs = 0;105for (i = 0; i < signature->length(); i++) {106sizeargs += type2size[signature->at(i)];107}108109BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);110VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);111int sig_index = 0;112for (i = 0; i < sizeargs; i++, sig_index++) {113sig_bt[i] = signature->at(sig_index);114if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {115sig_bt[i + 1] = T_VOID;116i++;117}118}119120intptr_t out_preserve = SharedRuntime::c_calling_convention(sig_bt, regs, NULL, sizeargs);121LIR_OprList* args = new LIR_OprList(signature->length());122for (i = 0; i < sizeargs;) {123BasicType t = sig_bt[i];124assert(t != T_VOID, "should be skipping these");125126// C calls are always outgoing127bool outgoing = true;128LIR_Opr opr = map_to_opr(t, regs + i, outgoing);129// they might be of different types if for instance floating point130// values are passed in cpu registers, but the sizes must match.131assert(type2size[opr->type()] == type2size[t], "type mismatch");132args->append(opr);133if (opr->is_address()) {134LIR_Address* addr = opr->as_address_ptr();135out_preserve = MAX2(out_preserve, (intptr_t)addr->disp() / 4);136}137i += type2size[t];138}139assert(args->length() == signature->length(), "size mismatch");140out_preserve += SharedRuntime::out_preserve_stack_slots();141update_reserved_argument_area_size(out_preserve * BytesPerWord);142return new CallingConvention(args, out_preserve);143}144145146//--------------------------------------------------------147// FrameMap148//--------------------------------------------------------149150bool FrameMap::_init_done = false;151Register FrameMap::_cpu_rnr2reg [FrameMap::nof_cpu_regs];152int FrameMap::_cpu_reg2rnr [FrameMap::nof_cpu_regs];153154155FrameMap::FrameMap(ciMethod* method, int monitors, int reserved_argument_area_size) {156assert(_init_done, "should already be completed");157158_framesize = -1;159_num_spills = -1;160161assert(monitors >= 0, "not set");162_num_monitors = monitors;163assert(reserved_argument_area_size >= 0, "not set");164_reserved_argument_area_size = MAX2(4, reserved_argument_area_size) * BytesPerWord;165166_argcount = method->arg_size();167_argument_locations = new intArray(_argcount, _argcount, -1);168_incoming_arguments = java_calling_convention(signature_type_array_for(method), false);169_oop_map_arg_count = _incoming_arguments->reserved_stack_slots();170171int java_index = 0;172for (int i = 0; i < _incoming_arguments->length(); i++) {173LIR_Opr opr = _incoming_arguments->at(i);174if (opr->is_address()) {175LIR_Address* address = opr->as_address_ptr();176_argument_locations->at_put(java_index, address->disp());177_incoming_arguments->args()->at_put(i, LIR_OprFact::stack(java_index, as_BasicType(as_ValueType(address->type()))));178}179java_index += type2size[opr->type()];180}181182}183184185bool FrameMap::finalize_frame(int nof_slots) {186assert(nof_slots >= 0, "must be positive");187assert(_num_spills == -1, "can only be set once");188_num_spills = nof_slots;189assert(_framesize == -1, "should only be calculated once");190_framesize = align_up(in_bytes(sp_offset_for_monitor_base(0)) +191_num_monitors * (int)sizeof(BasicObjectLock) +192(int)sizeof(intptr_t) + // offset of deopt orig pc193frame_pad_in_bytes,194StackAlignmentInBytes) / 4;195int java_index = 0;196for (int i = 0; i < _incoming_arguments->length(); i++) {197LIR_Opr opr = _incoming_arguments->at(i);198if (opr->is_stack()) {199_argument_locations->at_put(java_index, in_bytes(framesize_in_bytes()) +200_argument_locations->at(java_index));201}202java_index += type2size[opr->type()];203}204// make sure it's expressible on the platform205return validate_frame();206}207208VMReg FrameMap::sp_offset2vmreg(ByteSize offset) const {209int offset_in_bytes = in_bytes(offset);210assert(offset_in_bytes % 4 == 0, "must be multiple of 4 bytes");211assert(offset_in_bytes / 4 < framesize() + oop_map_arg_count(), "out of range");212return VMRegImpl::stack2reg(offset_in_bytes / 4);213}214215216bool FrameMap::location_for_sp_offset(ByteSize byte_offset_from_sp,217Location::Type loc_type,218Location* loc) const {219int offset = in_bytes(byte_offset_from_sp);220assert(offset >= 0, "incorrect offset");221if (!Location::legal_offset_in_bytes(offset)) {222return false;223}224Location tmp_loc = Location::new_stk_loc(loc_type, offset);225*loc = tmp_loc;226return true;227}228229230bool FrameMap::locations_for_slot (int index, Location::Type loc_type,231Location* loc, Location* second) const {232ByteSize offset_from_sp = sp_offset_for_slot(index);233if (!location_for_sp_offset(offset_from_sp, loc_type, loc)) {234return false;235}236if (second != NULL) {237// two word item238offset_from_sp = offset_from_sp + in_ByteSize(4);239return location_for_sp_offset(offset_from_sp, loc_type, second);240}241return true;242}243244//////////////////////245// Public accessors //246//////////////////////247248249ByteSize FrameMap::sp_offset_for_slot(const int index) const {250if (index < argcount()) {251int offset = _argument_locations->at(index);252assert(offset != -1, "not a memory argument");253assert(offset >= framesize() * 4, "argument inside of frame");254return in_ByteSize(offset);255}256ByteSize offset = sp_offset_for_spill(index - argcount());257assert(in_bytes(offset) < framesize() * 4, "spill outside of frame");258return offset;259}260261262ByteSize FrameMap::sp_offset_for_double_slot(const int index) const {263ByteSize offset = sp_offset_for_slot(index);264if (index >= argcount()) {265assert(in_bytes(offset) + 4 < framesize() * 4, "spill outside of frame");266}267return offset;268}269270271ByteSize FrameMap::sp_offset_for_spill(const int index) const {272assert(index >= 0 && index < _num_spills, "out of range");273int offset = align_up(first_available_sp_in_frame + _reserved_argument_area_size, (int)sizeof(double)) +274index * spill_slot_size_in_bytes;275return in_ByteSize(offset);276}277278ByteSize FrameMap::sp_offset_for_monitor_base(const int index) const {279int end_of_spills = align_up(first_available_sp_in_frame + _reserved_argument_area_size, (int)sizeof(double)) +280_num_spills * spill_slot_size_in_bytes;281int offset = align_up(end_of_spills, HeapWordSize) + index * (int)sizeof(BasicObjectLock);282return in_ByteSize(offset);283}284285ByteSize FrameMap::sp_offset_for_monitor_lock(int index) const {286check_monitor_index(index);287return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::lock_offset_in_bytes());;288}289290ByteSize FrameMap::sp_offset_for_monitor_object(int index) const {291check_monitor_index(index);292return sp_offset_for_monitor_base(index) + in_ByteSize(BasicObjectLock::obj_offset_in_bytes());293}294295296// For OopMaps, map a local variable or spill index to an VMReg.297// This is the offset from sp() in the frame of the slot for the index,298// skewed by SharedInfo::stack0 to indicate a stack location (vs.a register.)299//300// C ABI size +301// framesize + framesize +302// stack0 stack0 stack0 0 <- VMReg->value()303// | | | <registers> |304// ..........|..............|..............|.............|305// 0 1 2 3 | <C ABI area> | 4 5 6 ...... | <- local indices306// ^ ^ sp()307// | |308// arguments non-argument locals309310311VMReg FrameMap::regname(LIR_Opr opr) const {312if (opr->is_single_cpu()) {313assert(!opr->is_virtual(), "should not see virtual registers here");314return opr->as_register()->as_VMReg();315} else if (opr->is_single_stack()) {316return sp_offset2vmreg(sp_offset_for_slot(opr->single_stack_ix()));317} else if (opr->is_address()) {318LIR_Address* addr = opr->as_address_ptr();319assert(addr->base() == stack_pointer(), "sp based addressing only");320return sp_offset2vmreg(in_ByteSize(addr->index()->as_jint()));321}322ShouldNotReachHere();323return VMRegImpl::Bad();324}325326327328329// ------------ extra spill slots ---------------330331332