1 /*
  2  * Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CODE_VMREG_HPP
 26 #define SHARE_CODE_VMREG_HPP
 27 
 28 #include "asm/register.hpp"
 29 #include "code/vmregTypes.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/macros.hpp"
 33 #include "utilities/ostream.hpp"
 34 #ifdef COMPILER2
 35 #include "opto/adlcVMDeps.hpp"
 36 #endif
 37 
 38 //------------------------------VMReg------------------------------------------
 39 // The VM uses 'unwarped' stack slots; the compiler uses 'warped' stack slots.
 40 // Register numbers below VMRegImpl::stack0 are the same for both.  Register
 41 // numbers above stack0 are either warped (in the compiler) or unwarped
 42 // (in the VM).  Unwarped numbers represent stack indices, offsets from
 43 // the current stack pointer.  Warped numbers are required during compilation
 44 // when we do not yet know how big the frame will be.
 45 
 46 class VMRegImpl;
 47 typedef VMRegImpl* VMReg;
 48 
 49 class VMRegImpl {
 50 // friend class OopMap;
 51 friend class VMStructs;
 52 friend class OptoReg;
 53 // friend class Location;
 54 private:
 55   enum {
 56     BAD_REG = -1
 57   };
 58 
 59 
 60 
 61   static VMReg stack0;
 62   // Names for registers
 63   static const char *regName[];
 64   static const int register_count;
 65 
 66 
 67 public:
 68 
 69   static VMReg  as_VMReg(int val, bool bad_ok = false) { assert(val > BAD_REG || bad_ok, "invalid"); return (VMReg) (intptr_t) val; }
 70 
 71   const char*  name() {
 72     if (is_reg()) {
 73       return regName[value()];
 74     } else if (!is_valid()) {
 75       return "BAD";
 76     } else {
 77       // shouldn't really be called with stack
 78       return "STACKED REG";
 79     }
 80   }
 81   static VMReg Bad() { return (VMReg) (intptr_t) BAD_REG; }
 82   bool is_valid() const { return ((intptr_t) this) != BAD_REG; }
 83   bool is_stack() const { return (intptr_t) this >= (intptr_t) stack0; }
 84   bool is_reg()   const { return is_valid() && !is_stack(); }
 85 
 86   // A concrete register is a value that returns true for is_reg() and is
 87   // also a register you could use in the assembler. On machines with
 88   // 64bit registers only one half of the VMReg (and OptoReg) is considered
 89   // concrete.
 90   //  bool is_concrete();
 91 
 92   // VMRegs are 4 bytes wide on all platforms
 93   static const int stack_slot_size;
 94   static const int slots_per_word;
 95 
 96 
 97   // This really ought to check that the register is "real" in the sense that
 98   // we don't try and get the VMReg number of a physical register that doesn't
 99   // have an expressible part. That would be pd specific code
100   VMReg next() {
101     assert((is_reg() && value() < stack0->value() - 1) || is_stack(), "must be");
102     return (VMReg)(intptr_t)(value() + 1);
103   }
104   VMReg next(int i) {
105     assert((is_reg() && value() < stack0->value() - i) || is_stack(), "must be");
106     return (VMReg)(intptr_t)(value() + i);
107   }
108   VMReg prev() {
109     assert((is_stack() && value() > stack0->value()) || (is_reg() && value() != 0), "must be");
110     return (VMReg)(intptr_t)(value() - 1);
111   }
112 
113 
114   intptr_t value() const         {return (intptr_t) this; }
115 
116   void print_on(outputStream* st) const;
117   void print() const;
118 
119   // bias a stack slot.
120   // Typically used to adjust a virtual frame slots by amounts that are offset by
121   // amounts that are part of the native abi. The VMReg must be a stack slot
122   // and the result must be also.
123 
124   VMReg bias(int offset) {
125     assert(is_stack(), "must be");
126     // VMReg res = VMRegImpl::as_VMReg(value() + offset);
127     VMReg res = stack2reg(reg2stack() + offset);
128     assert(res->is_stack(), "must be");
129     return res;
130   }
131 
132   // Convert register numbers to stack slots and vice versa
133   static VMReg stack2reg( int idx ) {
134     return (VMReg) (intptr_t) (stack0->value() + idx);
135   }
136 
137   uintptr_t reg2stack() {
138     assert( is_stack(), "Not a stack-based register" );
139     return value() - stack0->value();
140   }
141 
142   static void set_regName();
143 
144 #include CPU_HEADER(vmreg)
145 
146 };
147 
148 //---------------------------VMRegPair-------------------------------------------
149 // Pairs of 32-bit registers for arguments.
150 // SharedRuntime::java_calling_convention will overwrite the structs with
151 // the calling convention's registers.  VMRegImpl::Bad is returned for any
152 // unused 32-bit register.  This happens for the unused high half of Int
153 // arguments, or for 32-bit pointers or for longs in the 32-bit sparc build
154 // (which are passed to natives in low 32-bits of e.g. O0/O1 and the high
155 // 32-bits of O0/O1 are set to VMRegImpl::Bad).  Longs in one register & doubles
156 // always return a high and a low register, as do 64-bit pointers.
157 //
158 class VMRegPair {
159 private:
160   VMReg _second;
161   VMReg _first;
162 public:
163   void set_bad (                   ) { _second=VMRegImpl::Bad(); _first=VMRegImpl::Bad(); }
164   void set1    (         VMReg v  ) { _second=VMRegImpl::Bad(); _first=v; }
165   void set2    (         VMReg v  ) { _second=v->next();  _first=v; }
166   void set_pair( VMReg second, VMReg first    ) { _second= second;    _first= first; }
167   void set_ptr ( VMReg ptr ) {
168 #ifdef _LP64
169     _second = ptr->next();
170 #else
171     _second = VMRegImpl::Bad();
172 #endif
173     _first = ptr;
174   }
175   // Return true if single register, even if the pair is really just adjacent stack slots
176   bool is_single_reg() const {
177     return (_first->is_valid()) && (_first->value() + 1 == _second->value());
178   }
179 
180   // Return true if single stack based "register" where the slot alignment matches input alignment
181   bool is_adjacent_on_stack(int alignment) const {
182     return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
183   }
184 
185   // Return true if single stack based "register" where the slot alignment matches input alignment
186   bool is_adjacent_aligned_on_stack(int alignment) const {
187     return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
188   }
189 
190   // Return true if single register but adjacent stack slots do not count
191   bool is_single_phys_reg() const {
192     return (_first->is_reg() && (_first->value() + 1 == _second->value()));
193   }
194 
195   VMReg second() const { return _second; }
196   VMReg first()  const { return _first; }
197   VMRegPair(VMReg s, VMReg f) {  _second = s; _first = f; }
198   VMRegPair(VMReg f) { _second = VMRegImpl::Bad(); _first = f; }
199   VMRegPair() { _second = VMRegImpl::Bad(); _first = VMRegImpl::Bad(); }
200 };
201 
202 #endif // SHARE_CODE_VMREG_HPP