1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "calling_convention_x86_64.h"
18
19 #include "base/bit_utils.h"
20 #include "base/logging.h"
21 #include "handle_scope-inl.h"
22 #include "utils/x86_64/managed_register_x86_64.h"
23
24 namespace art {
25 namespace x86_64 {
26
27 // Calling convention
28
InterproceduralScratchRegister()29 ManagedRegister X86_64ManagedRuntimeCallingConvention::InterproceduralScratchRegister() {
30 return X86_64ManagedRegister::FromCpuRegister(RAX);
31 }
32
InterproceduralScratchRegister()33 ManagedRegister X86_64JniCallingConvention::InterproceduralScratchRegister() {
34 return X86_64ManagedRegister::FromCpuRegister(RAX);
35 }
36
ReturnScratchRegister() const37 ManagedRegister X86_64JniCallingConvention::ReturnScratchRegister() const {
38 return ManagedRegister::NoRegister(); // No free regs, so assembler uses push/pop
39 }
40
ReturnRegisterForShorty(const char * shorty,bool jni ATTRIBUTE_UNUSED)41 static ManagedRegister ReturnRegisterForShorty(const char* shorty, bool jni ATTRIBUTE_UNUSED) {
42 if (shorty[0] == 'F' || shorty[0] == 'D') {
43 return X86_64ManagedRegister::FromXmmRegister(XMM0);
44 } else if (shorty[0] == 'J') {
45 return X86_64ManagedRegister::FromCpuRegister(RAX);
46 } else if (shorty[0] == 'V') {
47 return ManagedRegister::NoRegister();
48 } else {
49 return X86_64ManagedRegister::FromCpuRegister(RAX);
50 }
51 }
52
ReturnRegister()53 ManagedRegister X86_64ManagedRuntimeCallingConvention::ReturnRegister() {
54 return ReturnRegisterForShorty(GetShorty(), false);
55 }
56
ReturnRegister()57 ManagedRegister X86_64JniCallingConvention::ReturnRegister() {
58 return ReturnRegisterForShorty(GetShorty(), true);
59 }
60
IntReturnRegister()61 ManagedRegister X86_64JniCallingConvention::IntReturnRegister() {
62 return X86_64ManagedRegister::FromCpuRegister(RAX);
63 }
64
65 // Managed runtime calling convention
66
MethodRegister()67 ManagedRegister X86_64ManagedRuntimeCallingConvention::MethodRegister() {
68 return X86_64ManagedRegister::FromCpuRegister(RDI);
69 }
70
IsCurrentParamInRegister()71 bool X86_64ManagedRuntimeCallingConvention::IsCurrentParamInRegister() {
72 return !IsCurrentParamOnStack();
73 }
74
IsCurrentParamOnStack()75 bool X86_64ManagedRuntimeCallingConvention::IsCurrentParamOnStack() {
76 // We assume all parameters are on stack, args coming via registers are spilled as entry_spills
77 return true;
78 }
79
CurrentParamRegister()80 ManagedRegister X86_64ManagedRuntimeCallingConvention::CurrentParamRegister() {
81 ManagedRegister res = ManagedRegister::NoRegister();
82 if (!IsCurrentParamAFloatOrDouble()) {
83 switch (itr_args_ - itr_float_and_doubles_) {
84 case 0: res = X86_64ManagedRegister::FromCpuRegister(RSI); break;
85 case 1: res = X86_64ManagedRegister::FromCpuRegister(RDX); break;
86 case 2: res = X86_64ManagedRegister::FromCpuRegister(RCX); break;
87 case 3: res = X86_64ManagedRegister::FromCpuRegister(R8); break;
88 case 4: res = X86_64ManagedRegister::FromCpuRegister(R9); break;
89 }
90 } else if (itr_float_and_doubles_ < 8) {
91 // First eight float parameters are passed via XMM0..XMM7
92 res = X86_64ManagedRegister::FromXmmRegister(
93 static_cast<FloatRegister>(XMM0 + itr_float_and_doubles_));
94 }
95 return res;
96 }
97
CurrentParamStackOffset()98 FrameOffset X86_64ManagedRuntimeCallingConvention::CurrentParamStackOffset() {
99 return FrameOffset(displacement_.Int32Value() + // displacement
100 kX86_64PointerSize + // Method ref
101 itr_slots_ * sizeof(uint32_t)); // offset into in args
102 }
103
EntrySpills()104 const ManagedRegisterEntrySpills& X86_64ManagedRuntimeCallingConvention::EntrySpills() {
105 // We spill the argument registers on X86 to free them up for scratch use, we then assume
106 // all arguments are on the stack.
107 if (entry_spills_.size() == 0) {
108 ResetIterator(FrameOffset(0));
109 while (HasNext()) {
110 ManagedRegister in_reg = CurrentParamRegister();
111 if (!in_reg.IsNoRegister()) {
112 int32_t size = IsParamALongOrDouble(itr_args_)? 8 : 4;
113 int32_t spill_offset = CurrentParamStackOffset().Uint32Value();
114 ManagedRegisterSpill spill(in_reg, size, spill_offset);
115 entry_spills_.push_back(spill);
116 }
117 Next();
118 }
119 }
120 return entry_spills_;
121 }
122
123 // JNI calling convention
124
X86_64JniCallingConvention(bool is_static,bool is_synchronized,const char * shorty)125 X86_64JniCallingConvention::X86_64JniCallingConvention(bool is_static, bool is_synchronized,
126 const char* shorty)
127 : JniCallingConvention(is_static, is_synchronized, shorty, kFramePointerSize) {
128 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(RBX));
129 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(RBP));
130 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(R12));
131 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(R13));
132 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(R14));
133 callee_save_regs_.push_back(X86_64ManagedRegister::FromCpuRegister(R15));
134 callee_save_regs_.push_back(X86_64ManagedRegister::FromXmmRegister(XMM12));
135 callee_save_regs_.push_back(X86_64ManagedRegister::FromXmmRegister(XMM13));
136 callee_save_regs_.push_back(X86_64ManagedRegister::FromXmmRegister(XMM14));
137 callee_save_regs_.push_back(X86_64ManagedRegister::FromXmmRegister(XMM15));
138 }
139
CoreSpillMask() const140 uint32_t X86_64JniCallingConvention::CoreSpillMask() const {
141 return 1 << RBX | 1 << RBP | 1 << R12 | 1 << R13 | 1 << R14 | 1 << R15 |
142 1 << kNumberOfCpuRegisters;
143 }
144
FpSpillMask() const145 uint32_t X86_64JniCallingConvention::FpSpillMask() const {
146 return 1 << XMM12 | 1 << XMM13 | 1 << XMM14 | 1 << XMM15;
147 }
148
FrameSize()149 size_t X86_64JniCallingConvention::FrameSize() {
150 // Method*, return address and callee save area size, local reference segment state
151 size_t frame_data_size = kX86_64PointerSize +
152 (2 + CalleeSaveRegisters().size()) * kFramePointerSize;
153 // References plus link_ (pointer) and number_of_references_ (uint32_t) for HandleScope header
154 size_t handle_scope_size = HandleScope::SizeOf(kFramePointerSize, ReferenceCount());
155 // Plus return value spill area size
156 return RoundUp(frame_data_size + handle_scope_size + SizeOfReturnValue(), kStackAlignment);
157 }
158
OutArgSize()159 size_t X86_64JniCallingConvention::OutArgSize() {
160 return RoundUp(NumberOfOutgoingStackArgs() * kFramePointerSize, kStackAlignment);
161 }
162
IsCurrentParamInRegister()163 bool X86_64JniCallingConvention::IsCurrentParamInRegister() {
164 return !IsCurrentParamOnStack();
165 }
166
IsCurrentParamOnStack()167 bool X86_64JniCallingConvention::IsCurrentParamOnStack() {
168 return CurrentParamRegister().IsNoRegister();
169 }
170
CurrentParamRegister()171 ManagedRegister X86_64JniCallingConvention::CurrentParamRegister() {
172 ManagedRegister res = ManagedRegister::NoRegister();
173 if (!IsCurrentParamAFloatOrDouble()) {
174 switch (itr_args_ - itr_float_and_doubles_) {
175 case 0: res = X86_64ManagedRegister::FromCpuRegister(RDI); break;
176 case 1: res = X86_64ManagedRegister::FromCpuRegister(RSI); break;
177 case 2: res = X86_64ManagedRegister::FromCpuRegister(RDX); break;
178 case 3: res = X86_64ManagedRegister::FromCpuRegister(RCX); break;
179 case 4: res = X86_64ManagedRegister::FromCpuRegister(R8); break;
180 case 5: res = X86_64ManagedRegister::FromCpuRegister(R9); break;
181 }
182 } else if (itr_float_and_doubles_ < 8) {
183 // First eight float parameters are passed via XMM0..XMM7
184 res = X86_64ManagedRegister::FromXmmRegister(
185 static_cast<FloatRegister>(XMM0 + itr_float_and_doubles_));
186 }
187 return res;
188 }
189
CurrentParamStackOffset()190 FrameOffset X86_64JniCallingConvention::CurrentParamStackOffset() {
191 size_t offset = itr_args_
192 - std::min(8U, itr_float_and_doubles_) // Float arguments passed through Xmm0..Xmm7
193 - std::min(6U, itr_args_ - itr_float_and_doubles_); // Integer arguments passed through GPR
194 return FrameOffset(displacement_.Int32Value() - OutArgSize() + (offset * kFramePointerSize));
195 }
196
NumberOfOutgoingStackArgs()197 size_t X86_64JniCallingConvention::NumberOfOutgoingStackArgs() {
198 size_t static_args = IsStatic() ? 1 : 0; // count jclass
199 // regular argument parameters and this
200 size_t param_args = NumArgs() + NumLongOrDoubleArgs();
201 // count JNIEnv* and return pc (pushed after Method*)
202 size_t total_args = static_args + param_args + 2;
203
204 // Float arguments passed through Xmm0..Xmm7
205 // Other (integer) arguments passed through GPR (RDI, RSI, RDX, RCX, R8, R9)
206 size_t total_stack_args = total_args
207 - std::min(8U, static_cast<unsigned int>(NumFloatOrDoubleArgs()))
208 - std::min(6U, static_cast<unsigned int>(NumArgs() - NumFloatOrDoubleArgs()));
209
210 return total_stack_args;
211 }
212
213 } // namespace x86_64
214 } // namespace art
215