1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/cpu/x86/vm/c1_FpuStackSim_x86.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,201 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "c1/c1_FpuStackSim.hpp" 1.30 +#include "c1/c1_FrameMap.hpp" 1.31 +#include "utilities/array.hpp" 1.32 +#include "utilities/ostream.hpp" 1.33 + 1.34 +//-------------------------------------------------------- 1.35 +// FpuStackSim 1.36 +//-------------------------------------------------------- 1.37 + 1.38 +// This class maps the FPU registers to their stack locations; it computes 1.39 +// the offsets between individual registers and simulates the FPU stack. 1.40 + 1.41 +const int EMPTY = -1; 1.42 + 1.43 +int FpuStackSim::regs_at(int i) const { 1.44 + assert(i >= 0 && i < FrameMap::nof_fpu_regs, "out of bounds"); 1.45 + return _regs[i]; 1.46 +} 1.47 + 1.48 +void FpuStackSim::set_regs_at(int i, int val) { 1.49 + assert(i >= 0 && i < FrameMap::nof_fpu_regs, "out of bounds"); 1.50 + _regs[i] = val; 1.51 +} 1.52 + 1.53 +void FpuStackSim::dec_stack_size() { 1.54 + _stack_size--; 1.55 + assert(_stack_size >= 0, "FPU stack underflow"); 1.56 +} 1.57 + 1.58 +void FpuStackSim::inc_stack_size() { 1.59 + _stack_size++; 1.60 + assert(_stack_size <= FrameMap::nof_fpu_regs, "FPU stack overflow"); 1.61 +} 1.62 + 1.63 +FpuStackSim::FpuStackSim(Compilation* compilation) 1.64 + : _compilation(compilation) 1.65 +{ 1.66 + _stack_size = 0; 1.67 + for (int i = 0; i < FrameMap::nof_fpu_regs; i++) { 1.68 + set_regs_at(i, EMPTY); 1.69 + } 1.70 +} 1.71 + 1.72 + 1.73 +void FpuStackSim::pop() { 1.74 + if (TraceFPUStack) { tty->print("FPU-pop "); print(); tty->cr(); } 1.75 + set_regs_at(tos_index(), EMPTY); 1.76 + dec_stack_size(); 1.77 +} 1.78 + 1.79 +void FpuStackSim::pop(int rnr) { 1.80 + if (TraceFPUStack) { tty->print("FPU-pop %d", rnr); print(); tty->cr(); } 1.81 + assert(regs_at(tos_index()) == rnr, "rnr is not on TOS"); 1.82 + set_regs_at(tos_index(), EMPTY); 1.83 + dec_stack_size(); 1.84 +} 1.85 + 1.86 + 1.87 +void FpuStackSim::push(int rnr) { 1.88 + if (TraceFPUStack) { tty->print("FPU-push %d", rnr); print(); tty->cr(); } 1.89 + assert(regs_at(stack_size()) == EMPTY, "should be empty"); 1.90 + set_regs_at(stack_size(), rnr); 1.91 + inc_stack_size(); 1.92 +} 1.93 + 1.94 + 1.95 +void FpuStackSim::swap(int offset) { 1.96 + if (TraceFPUStack) { tty->print("FPU-swap %d", offset); print(); tty->cr(); } 1.97 + int t = regs_at(tos_index() - offset); 1.98 + set_regs_at(tos_index() - offset, regs_at(tos_index())); 1.99 + set_regs_at(tos_index(), t); 1.100 +} 1.101 + 1.102 + 1.103 +int FpuStackSim::offset_from_tos(int rnr) const { 1.104 + for (int i = tos_index(); i >= 0; i--) { 1.105 + if (regs_at(i) == rnr) { 1.106 + return tos_index() - i; 1.107 + } 1.108 + } 1.109 + assert(false, "FpuStackSim: register not found"); 1.110 + BAILOUT_("FpuStackSim: register not found", 0); 1.111 +} 1.112 + 1.113 + 1.114 +int FpuStackSim::get_slot(int tos_offset) const { 1.115 + return regs_at(tos_index() - tos_offset); 1.116 +} 1.117 + 1.118 +void FpuStackSim::set_slot(int tos_offset, int rnr) { 1.119 + set_regs_at(tos_index() - tos_offset, rnr); 1.120 +} 1.121 + 1.122 +void FpuStackSim::rename(int old_rnr, int new_rnr) { 1.123 + if (TraceFPUStack) { tty->print("FPU-rename %d %d", old_rnr, new_rnr); print(); tty->cr(); } 1.124 + if (old_rnr == new_rnr) 1.125 + return; 1.126 + bool found = false; 1.127 + for (int i = 0; i < stack_size(); i++) { 1.128 + assert(regs_at(i) != new_rnr, "should not see old occurrences of new_rnr on the stack"); 1.129 + if (regs_at(i) == old_rnr) { 1.130 + set_regs_at(i, new_rnr); 1.131 + found = true; 1.132 + } 1.133 + } 1.134 + assert(found, "should have found at least one instance of old_rnr"); 1.135 +} 1.136 + 1.137 + 1.138 +bool FpuStackSim::contains(int rnr) { 1.139 + for (int i = 0; i < stack_size(); i++) { 1.140 + if (regs_at(i) == rnr) { 1.141 + return true; 1.142 + } 1.143 + } 1.144 + return false; 1.145 +} 1.146 + 1.147 +bool FpuStackSim::is_empty() { 1.148 +#ifdef ASSERT 1.149 + if (stack_size() == 0) { 1.150 + for (int i = 0; i < FrameMap::nof_fpu_regs; i++) { 1.151 + assert(regs_at(i) == EMPTY, "must be empty"); 1.152 + } 1.153 + } 1.154 +#endif 1.155 + return stack_size() == 0; 1.156 +} 1.157 + 1.158 + 1.159 +bool FpuStackSim::slot_is_empty(int tos_offset) { 1.160 + return (regs_at(tos_index() - tos_offset) == EMPTY); 1.161 +} 1.162 + 1.163 + 1.164 +void FpuStackSim::clear() { 1.165 + if (TraceFPUStack) { tty->print("FPU-clear"); print(); tty->cr(); } 1.166 + for (int i = tos_index(); i >= 0; i--) { 1.167 + set_regs_at(i, EMPTY); 1.168 + } 1.169 + _stack_size = 0; 1.170 +} 1.171 + 1.172 + 1.173 +intArray* FpuStackSim::write_state() { 1.174 + intArray* res = new intArray(1 + FrameMap::nof_fpu_regs); 1.175 + (*res)[0] = stack_size(); 1.176 + for (int i = 0; i < FrameMap::nof_fpu_regs; i++) { 1.177 + (*res)[1 + i] = regs_at(i); 1.178 + } 1.179 + return res; 1.180 +} 1.181 + 1.182 + 1.183 +void FpuStackSim::read_state(intArray* fpu_stack_state) { 1.184 + _stack_size = (*fpu_stack_state)[0]; 1.185 + for (int i = 0; i < FrameMap::nof_fpu_regs; i++) { 1.186 + set_regs_at(i, (*fpu_stack_state)[1 + i]); 1.187 + } 1.188 +} 1.189 + 1.190 + 1.191 +#ifndef PRODUCT 1.192 +void FpuStackSim::print() { 1.193 + tty->print(" N=%d[", stack_size());\ 1.194 + for (int i = 0; i < stack_size(); i++) { 1.195 + int reg = regs_at(i); 1.196 + if (reg != EMPTY) { 1.197 + tty->print("%d", reg); 1.198 + } else { 1.199 + tty->print("_"); 1.200 + } 1.201 + }; 1.202 + tty->print(" ]"); 1.203 +} 1.204 +#endif