1 /*
2 * Copyright (c) 1998, 2026, 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/checkedCast.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 #include "utilities/macros.hpp"
34 #include "utilities/ostream.hpp"
35 #ifdef COMPILER2
36 #include "opto/adlcVMDeps.hpp"
37 #endif
38
39 //------------------------------VMReg------------------------------------------
40 // The VM uses 'unwarped' stack slots; the compiler uses 'warped' stack slots.
41 // Register numbers below VMRegImpl::stack0 are the same for both. Register
42 // numbers above stack0 are either warped (in the compiler) or unwarped
43 // (in the VM). Unwarped numbers represent stack indices, offsets from
44 // the current stack pointer. Warped numbers are required during compilation
45 // when we do not yet know how big the frame will be.
46
47 class VMRegImpl;
48 typedef VMRegImpl* VMReg;
49
50 class VMRegImpl {
51 // friend class OopMap;
52 friend class VMStructs;
53 friend class OptoReg;
54 // friend class Location;
55 private:
56 enum {
57 BAD_REG = -1,
58 FIRST_STACK = (ConcreteRegisterImpl::number_of_registers + 7) & ~7
59 };
60
61 // Despite being private, this field is exported to the
62 // serviceability agent and our friends. It's not really a pointer,
63 // but that's fine and dandy as long as no-one tries to dereference
64 // it.
65 static VMReg stack0;
66
67 static constexpr VMReg first();
68 // Names for registers
69 static const char *regName[];
70 static const int register_count;
71
72 public:
73
74 static constexpr VMReg stack_0() {
75 return first() + FIRST_STACK;
76 }
77
78 static VMReg as_VMReg(int val, bool bad_ok = false) {
79 assert(val > BAD_REG || bad_ok, "invalid");
80 return val + first();
81 }
82
83 const char* name() {
84 if (is_reg()) {
85 return regName[value()];
86 } else if (!is_valid()) {
87 return "BAD";
88 } else {
89 // shouldn't really be called with stack
90 return "STACKED REG";
91 }
92 }
93 int value() const { return checked_cast<int>(this - first()); }
94 static VMReg Bad() { return BAD_REG+first(); }
95 bool is_valid() const { return value() != BAD_REG; }
96 bool is_stack() const { return this >= stack_0(); }
97 bool is_reg() const { return is_valid() && !is_stack(); }
98
99 // A concrete register is a value that returns true for is_reg() and is
100 // also a register you could use in the assembler. On machines with
101 // 64bit registers only one half of the VMReg (and OptoReg) is considered
102 // concrete.
103 // bool is_concrete();
104
105 // VMRegs are 4 bytes wide on all platforms
106 static const int stack_slot_size;
107 static const int slots_per_word;
108
109
110 // This really ought to check that the register is "real" in the sense that
111 // we don't try and get the VMReg number of a physical register that doesn't
112 // have an expressible part. That would be pd specific code
113 VMReg next() {
114 assert((is_reg() && this < stack_0() - 1) || is_stack(), "must be");
115 return this + 1;
116 }
117 VMReg next(int i) {
118 assert((is_reg() && this < stack_0() - i) || is_stack(), "must be");
119 return this + i;
120 }
121 VMReg prev() {
122 assert((is_stack() && this > stack_0()) || (is_reg() && value() != 0), "must be");
123 return this - 1;
124 }
125
126
127 void print_on(outputStream* st) const;
128 void print() const;
129
130 // bias a stack slot.
131 // Typically used to adjust a virtual frame slots by amounts that are offset by
132 // amounts that are part of the native abi. The VMReg must be a stack slot
133 // and the result must be also.
134
135 VMReg bias(int offset) const {
136 assert(is_stack(), "must be");
137 // VMReg res = VMRegImpl::as_VMReg(value() + offset);
138 VMReg res = stack2reg(reg2stack() + offset);
139 assert(res->is_stack(), "must be");
140 return res;
141 }
142
143 // Convert register numbers to stack slots and vice versa
144 static VMReg stack2reg(int idx) {
145 return stack_0() + idx;
146 }
147
148 int reg2stack() const {
149 assert(is_stack(), "Not a stack-based register");
150 return checked_cast<int>(this - stack_0());
151 }
152
153 static void set_regName();
154
155 #include CPU_HEADER(vmreg)
156
157 };
158
159 extern VMRegImpl all_VMRegs[ConcreteRegisterImpl::number_of_registers + 1] INTERNAL_VISIBILITY;
160 inline constexpr VMReg VMRegImpl::first() { return all_VMRegs + 1; }
161
162 //---------------------------VMRegPair-------------------------------------------
163 // Pairs of 32-bit registers for arguments.
164 // SharedRuntime::java_calling_convention will overwrite the structs with
165 // the calling convention's registers. VMRegImpl::Bad is returned for any
166 // unused 32-bit register. This happens for the unused high half of Int
167 // arguments, or for 32-bit pointers or for longs in the 32-bit sparc build
168 // (which are passed to natives in low 32-bits of e.g. O0/O1 and the high
169 // 32-bits of O0/O1 are set to VMRegImpl::Bad). Longs in one register & doubles
170 // always return a high and a low register, as do 64-bit pointers.
171 //
172 class VMRegPair {
173 private:
174 VMReg _second;
175 VMReg _first;
176 public:
177 void set_bad ( ) { _second=VMRegImpl::Bad(); _first=VMRegImpl::Bad(); }
178 void set1 ( VMReg v ) { _second=VMRegImpl::Bad(); _first=v; }
179 void set2 ( VMReg v ) { _second=v->next(); _first=v; }
180 void set_pair( VMReg second, VMReg first ) { _second= second; _first= first; }
181 void set_ptr ( VMReg ptr ) {
182 #ifdef _LP64
183 _second = ptr->next();
184 #else
185 _second = VMRegImpl::Bad();
186 #endif
187 _first = ptr;
188 }
189 // Return true if single register, even if the pair is really just adjacent stack slots
190 bool is_single_reg() const {
191 return (_first->is_valid()) && (_first->value() + 1 == _second->value());
192 }
193
194 // Return true if single stack based "register" where the slot alignment matches input alignment
195 bool is_adjacent_on_stack(int alignment) const {
196 return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
197 }
198
199 // Return true if single stack based "register" where the slot alignment matches input alignment
200 bool is_adjacent_aligned_on_stack(int alignment) const {
201 return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
202 }
203
204 // Return true if single register but adjacent stack slots do not count
205 bool is_single_phys_reg() const {
206 return (_first->is_reg() && (_first->value() + 1 == _second->value()));
207 }
208
209 VMReg second() const { return _second; }
210 VMReg first() const { return _first; }
211 VMRegPair(VMReg s, VMReg f) { _second = s; _first = f; }
212 VMRegPair(VMReg f) { _second = VMRegImpl::Bad(); _first = f; }
213 VMRegPair() { _second = VMRegImpl::Bad(); _first = VMRegImpl::Bad(); }
214
215 void print_on(outputStream* st) const;
216 };
217
218 #endif // SHARE_CODE_VMREG_HPP