1 /*
   2  * Copyright (c) 2013, Red Hat Inc.
   3  * Copyright (c) 2005, 2010, Oracle and/or its affiliates.
   4  * All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "c1/c1_FpuStackSim.hpp"
  29 #include "c1/c1_FrameMap.hpp"
  30 #include "utilities/array.hpp"
  31 #include "utilities/ostream.hpp"
  32 
  33 //--------------------------------------------------------
  34 //               FpuStackSim
  35 //--------------------------------------------------------
  36 
  37 // This class maps the FPU registers to their stack locations; it computes
  38 // the offsets between individual registers and simulates the FPU stack.
  39 
  40 const int EMPTY = -1;
  41 
  42 int FpuStackSim::regs_at(int i) const {
  43   assert(i >= 0 && i < FrameMap::nof_fpu_regs, "out of bounds");
  44   return _regs[i];
  45 }
  46 
  47 void FpuStackSim::set_regs_at(int i, int val) {
  48   assert(i >= 0 && i < FrameMap::nof_fpu_regs, "out of bounds");
  49   _regs[i] = val;
  50 }
  51 
  52 void FpuStackSim::dec_stack_size() {
  53   _stack_size--;
  54   assert(_stack_size >= 0, "FPU stack underflow");
  55 }
  56 
  57 void FpuStackSim::inc_stack_size() {
  58   _stack_size++;
  59   assert(_stack_size <= FrameMap::nof_fpu_regs, "FPU stack overflow");
  60 }
  61 
  62 FpuStackSim::FpuStackSim(Compilation* compilation)
  63  : _compilation(compilation)
  64 {
  65   _stack_size = 0;
  66   for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
  67     set_regs_at(i, EMPTY);
  68   }
  69 }
  70 
  71 
  72 void FpuStackSim::pop() {
  73   if (TraceFPUStack) { tty->print("FPU-pop "); print(); tty->cr(); }
  74   set_regs_at(tos_index(), EMPTY);
  75   dec_stack_size();
  76 }
  77 
  78 void FpuStackSim::pop(int rnr) {
  79   if (TraceFPUStack) { tty->print("FPU-pop %d", rnr); print(); tty->cr(); }
  80   assert(regs_at(tos_index()) == rnr, "rnr is not on TOS");
  81   set_regs_at(tos_index(), EMPTY);
  82   dec_stack_size();
  83 }
  84 
  85 
  86 void FpuStackSim::push(int rnr) {
  87   if (TraceFPUStack) { tty->print("FPU-push %d", rnr); print(); tty->cr(); }
  88   assert(regs_at(stack_size()) == EMPTY, "should be empty");
  89   set_regs_at(stack_size(), rnr);
  90   inc_stack_size();
  91 }
  92 
  93 
  94 void FpuStackSim::swap(int offset) {
  95   if (TraceFPUStack) { tty->print("FPU-swap %d", offset); print(); tty->cr(); }
  96   int t = regs_at(tos_index() - offset);
  97   set_regs_at(tos_index() - offset, regs_at(tos_index()));
  98   set_regs_at(tos_index(), t);
  99 }
 100 
 101 
 102 int FpuStackSim::offset_from_tos(int rnr) const {
 103   for (int i = tos_index(); i >= 0; i--) {
 104     if (regs_at(i) == rnr) {
 105       return tos_index() - i;
 106     }
 107   }
 108   assert(false, "FpuStackSim: register not found");
 109   BAILOUT_("FpuStackSim: register not found", 0);
 110 }
 111 
 112 
 113 int FpuStackSim::get_slot(int tos_offset) const {
 114   return regs_at(tos_index() - tos_offset);
 115 }
 116 
 117 void FpuStackSim::set_slot(int tos_offset, int rnr) {
 118   set_regs_at(tos_index() - tos_offset, rnr);
 119 }
 120 
 121 void FpuStackSim::rename(int old_rnr, int new_rnr) {
 122   if (TraceFPUStack) { tty->print("FPU-rename %d %d", old_rnr, new_rnr); print(); tty->cr(); }
 123   if (old_rnr == new_rnr)
 124     return;
 125   bool found = false;
 126   for (int i = 0; i < stack_size(); i++) {
 127     assert(regs_at(i) != new_rnr, "should not see old occurrences of new_rnr on the stack");
 128     if (regs_at(i) == old_rnr) {
 129       set_regs_at(i, new_rnr);
 130       found = true;
 131     }
 132   }
 133   assert(found, "should have found at least one instance of old_rnr");
 134 }
 135 
 136 
 137 bool FpuStackSim::contains(int rnr) {
 138   for (int i = 0; i < stack_size(); i++) {
 139     if (regs_at(i) == rnr) {
 140       return true;
 141     }
 142   }
 143   return false;
 144 }
 145 
 146 bool FpuStackSim::is_empty() {
 147 #ifdef ASSERT
 148   if (stack_size() == 0) {
 149     for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 150       assert(regs_at(i) == EMPTY, "must be empty");
 151     }
 152   }
 153 #endif
 154   return stack_size() == 0;
 155 }
 156 
 157 
 158 bool FpuStackSim::slot_is_empty(int tos_offset) {
 159   return (regs_at(tos_index() - tos_offset) == EMPTY);
 160 }
 161 
 162 
 163 void FpuStackSim::clear() {
 164   if (TraceFPUStack) { tty->print("FPU-clear"); print(); tty->cr(); }
 165   for (int i = tos_index(); i >= 0; i--) {
 166     set_regs_at(i, EMPTY);
 167   }
 168   _stack_size = 0;
 169 }
 170 
 171 
 172 intArray* FpuStackSim::write_state() {
 173   intArray* res = new intArray(1 + FrameMap::nof_fpu_regs);
 174   (*res)[0] = stack_size();
 175   for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 176     (*res)[1 + i] = regs_at(i);
 177   }
 178   return res;
 179 }
 180 
 181 
 182 void FpuStackSim::read_state(intArray* fpu_stack_state) {
 183   _stack_size = (*fpu_stack_state)[0];
 184   for (int i = 0; i < FrameMap::nof_fpu_regs; i++) {
 185     set_regs_at(i, (*fpu_stack_state)[1 + i]);
 186   }
 187 }
 188 
 189 
 190 #ifndef PRODUCT
 191 void FpuStackSim::print() {
 192   tty->print(" N=%d[", stack_size());\
 193   for (int i = 0; i < stack_size(); i++) {
 194     int reg = regs_at(i);
 195     if (reg != EMPTY) {
 196       tty->print("%d", reg);
 197     } else {
 198       tty->print("_");
 199     }
 200   };
 201   tty->print(" ]");
 202 }
 203 #endif