blob: 4b4a146d8636213df3f41b4157281628cf49b3d9 [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Ian Rogersb033c752011-07-20 12:22:35 -07002
Ian Rogers2c8f6532011-09-02 17:16:34 -07003#include "calling_convention_arm.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#include "logging.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -07005#include "managed_register_arm.h"
Ian Rogersb033c752011-07-20 12:22:35 -07006
7namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -07008namespace arm {
Ian Rogersb033c752011-07-20 12:22:35 -07009
Ian Rogers2c8f6532011-09-02 17:16:34 -070010// Calling convention
11
12ManagedRegister ArmManagedRuntimeCallingConvention::InterproceduralScratchRegister() {
13 return ArmManagedRegister::FromCoreRegister(IP); // R12
Ian Rogersb033c752011-07-20 12:22:35 -070014}
15
Ian Rogers2c8f6532011-09-02 17:16:34 -070016ManagedRegister ArmJniCallingConvention::InterproceduralScratchRegister() {
17 return ArmManagedRegister::FromCoreRegister(IP); // R12
Shih-wei Liao668512a2011-09-01 14:18:34 -070018}
19
Ian Rogers2c8f6532011-09-02 17:16:34 -070020static ManagedRegister ReturnRegisterForMethod(Method* method) {
21 if (method->IsReturnAFloat()) {
22 return ArmManagedRegister::FromCoreRegister(R0);
23 } else if (method->IsReturnADouble()) {
24 return ArmManagedRegister::FromRegisterPair(R0_R1);
Ian Rogers45a76cb2011-07-21 22:00:15 -070025 } else if (method->IsReturnALong()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070026 return ArmManagedRegister::FromRegisterPair(R0_R1);
Ian Rogers45a76cb2011-07-21 22:00:15 -070027 } else if (method->IsReturnVoid()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070028 return ArmManagedRegister::NoRegister();
Ian Rogersb033c752011-07-20 12:22:35 -070029 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -070030 return ArmManagedRegister::FromCoreRegister(R0);
Ian Rogersb033c752011-07-20 12:22:35 -070031 }
32}
33
Ian Rogers2c8f6532011-09-02 17:16:34 -070034ManagedRegister ArmManagedRuntimeCallingConvention::ReturnRegister() {
35 return ReturnRegisterForMethod(GetMethod());
36}
37
38ManagedRegister ArmJniCallingConvention::ReturnRegister() {
39 return ReturnRegisterForMethod(GetMethod());
40}
41
Ian Rogersb033c752011-07-20 12:22:35 -070042// Managed runtime calling convention
43
Ian Rogers2c8f6532011-09-02 17:16:34 -070044ManagedRegister ArmManagedRuntimeCallingConvention::MethodRegister() {
45 return ArmManagedRegister::FromCoreRegister(R0);
46}
47
48bool ArmManagedRuntimeCallingConvention::IsCurrentParamInRegister() {
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070049 return itr_slots_ < 3;
Ian Rogersb033c752011-07-20 12:22:35 -070050}
51
Ian Rogers2c8f6532011-09-02 17:16:34 -070052bool ArmManagedRuntimeCallingConvention::IsCurrentParamOnStack() {
Ian Rogers7a99c112011-09-07 12:48:27 -070053 if (itr_slots_ < 2) {
54 return false;
55 } else if (itr_slots_ > 2) {
56 return true;
57 } else {
58 // handle funny case of a long/double straddling registers and the stack
59 return GetMethod()->IsParamALongOrDouble(itr_args_);
60 }
Ian Rogersb033c752011-07-20 12:22:35 -070061}
62
63static const Register kManagedArgumentRegisters[] = {
64 R1, R2, R3
65};
Ian Rogers2c8f6532011-09-02 17:16:34 -070066ManagedRegister ArmManagedRuntimeCallingConvention::CurrentParamRegister() {
Ian Rogers7a99c112011-09-07 12:48:27 -070067 CHECK(IsCurrentParamInRegister());
Carl Shapiroe2d373e2011-07-25 15:20:06 -070068 const Method* method = GetMethod();
Shih-wei Liao5381cf92011-07-27 00:28:04 -070069 if (method->IsParamALongOrDouble(itr_args_)) {
70 if (itr_slots_ == 0) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070071 return ArmManagedRegister::FromRegisterPair(R1_R2);
Shih-wei Liao5381cf92011-07-27 00:28:04 -070072 } else if (itr_slots_ == 1) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070073 return ArmManagedRegister::FromRegisterPair(R2_R3);
Shih-wei Liao5381cf92011-07-27 00:28:04 -070074 } else {
75 // This is a long/double split between registers and the stack
Ian Rogers2c8f6532011-09-02 17:16:34 -070076 return ArmManagedRegister::FromCoreRegister(
Shih-wei Liao5381cf92011-07-27 00:28:04 -070077 kManagedArgumentRegisters[itr_slots_]);
Carl Shapiroe2d373e2011-07-25 15:20:06 -070078 }
Carl Shapiroe2d373e2011-07-25 15:20:06 -070079 } else {
80 return
Ian Rogers2c8f6532011-09-02 17:16:34 -070081 ArmManagedRegister::FromCoreRegister(kManagedArgumentRegisters[itr_slots_]);
Carl Shapiroe2d373e2011-07-25 15:20:06 -070082 }
Ian Rogersb033c752011-07-20 12:22:35 -070083}
84
Ian Rogers2c8f6532011-09-02 17:16:34 -070085FrameOffset ArmManagedRuntimeCallingConvention::CurrentParamStackOffset() {
Ian Rogers7a99c112011-09-07 12:48:27 -070086 CHECK(IsCurrentParamOnStack());
87 FrameOffset result =
88 FrameOffset(displacement_.Int32Value() + // displacement
89 kPointerSize + // Method*
90 (itr_slots_ * kPointerSize)); // offset into in args
91 if (itr_slots_ == 2) {
92 // the odd spanning case, bump the offset to skip the first half of the
93 // input which is in a register
94 CHECK(IsCurrentParamInRegister());
95 result = FrameOffset(result.Int32Value() + 4);
96 }
97 return result;
Ian Rogersb033c752011-07-20 12:22:35 -070098}
99
100// JNI calling convention
101
Ian Rogers2c8f6532011-09-02 17:16:34 -0700102ArmJniCallingConvention::ArmJniCallingConvention(Method* method) :
103 JniCallingConvention(method) {
104 // A synchronized method will call monitor enter clobbering R1, R2 and R3
105 // unless they are spilled.
106 if (method->IsSynchronized()) {
107 spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R1));
108 spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R2));
109 spill_regs_.push_back(ArmManagedRegister::FromCoreRegister(R3));
110 }
111}
112
113size_t ArmJniCallingConvention::FrameSize() {
Ian Rogers0d666d82011-08-14 16:03:46 -0700114 // Method* and spill area size
115 size_t frame_data_size = kPointerSize + SpillAreaSize();
Ian Rogers408f79a2011-08-23 18:22:33 -0700116 // References plus 2 words for SIRT header
117 size_t sirt_size = (ReferenceCount() + 2) * kPointerSize;
Ian Rogers0d666d82011-08-14 16:03:46 -0700118 // Plus return value spill area size
Ian Rogers408f79a2011-08-23 18:22:33 -0700119 return RoundUp(frame_data_size + sirt_size + SizeOfReturnValue(),
120 kStackAlignment);
Ian Rogers0d666d82011-08-14 16:03:46 -0700121}
122
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123size_t ArmJniCallingConvention::OutArgSize() {
Ian Rogers7a99c112011-09-07 12:48:27 -0700124 const Method* method = GetMethod();
125 size_t padding; // padding to ensure longs and doubles are not split in AAPCS
126 if (method->IsStatic()) {
127 padding = (method->NumArgs() > 1) && !method->IsParamALongOrDouble(0) &&
128 method->IsParamALongOrDouble(1) ? 4 : 0;
129 } else {
130 padding = (method->NumArgs() > 2) && !method->IsParamALongOrDouble(1) &&
131 method->IsParamALongOrDouble(2) ? 4 : 0;
132 }
133 return RoundUp(NumberOfOutgoingStackArgs() * kPointerSize + padding,
134 kStackAlignment);
135}
136
Ian Rogers2c8f6532011-09-02 17:16:34 -0700137size_t ArmJniCallingConvention::ReturnPcOffset() {
Ian Rogers762400c2011-08-23 12:14:16 -0700138 // Link register is always the last value spilled, skip forward one word for
139 // the Method* then skip back one word to get the link register (ie +0)
140 return SpillAreaSize();
141}
142
Ian Rogers2c8f6532011-09-02 17:16:34 -0700143size_t ArmJniCallingConvention::SpillAreaSize() {
Ian Rogers0d666d82011-08-14 16:03:46 -0700144 // Space for link register. For synchronized methods we need enough space to
145 // save R1, R2 and R3 (R0 is the method register and always preserved)
146 return GetMethod()->IsSynchronized() ? (4 * kPointerSize) : kPointerSize;
147}
148
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700149// Will reg be crushed by an outgoing argument?
Ian Rogers2c8f6532011-09-02 17:16:34 -0700150bool ArmJniCallingConvention::IsOutArgRegister(ManagedRegister mreg) {
151 Register reg = mreg.AsArm().AsCoreRegister();
152 return reg >= R0 && reg <= R3;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700153}
154
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700155// JniCallingConvention ABI follows AAPCS
156//
157// In processing each parameter, we know that IsCurrentParamInRegister()
158// or IsCurrentParamOnStack() will be called first.
159// Both functions will ensure that we conform to AAPCS.
160//
Ian Rogers2c8f6532011-09-02 17:16:34 -0700161bool ArmJniCallingConvention::IsCurrentParamInRegister() {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700162 // AAPCS processing
Ian Rogers2c8f6532011-09-02 17:16:34 -0700163 Method* method = GetMethod();
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700164 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(method);
165 if ((itr_args_ >= 2) && method->IsParamALongOrDouble(arg_pos)) {
166 // itr_slots_ needs to be an even number, according to AAPCS.
167 if (itr_slots_ & 0x1u) {
168 itr_slots_++;
169 }
170 }
171
172 return itr_slots_ < 4;
Ian Rogersb033c752011-07-20 12:22:35 -0700173}
174
Ian Rogers2c8f6532011-09-02 17:16:34 -0700175bool ArmJniCallingConvention::IsCurrentParamOnStack() {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700176 return !IsCurrentParamInRegister();
Ian Rogersb033c752011-07-20 12:22:35 -0700177}
178
179static const Register kJniArgumentRegisters[] = {
180 R0, R1, R2, R3
181};
Ian Rogers2c8f6532011-09-02 17:16:34 -0700182ManagedRegister ArmJniCallingConvention::CurrentParamRegister() {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700183 CHECK_LT(itr_slots_, 4u);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700184 Method* method = GetMethod();
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700185 int arg_pos = itr_args_ - NumberOfExtraArgumentsForJni(method);
186 if ((itr_args_ >= 2) && method->IsParamALongOrDouble(arg_pos)) {
187 CHECK_EQ(itr_slots_, 2u);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700188 return ArmManagedRegister::FromRegisterPair(R2_R3);
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700189 } else {
190 return
Ian Rogers2c8f6532011-09-02 17:16:34 -0700191 ArmManagedRegister::FromCoreRegister(kJniArgumentRegisters[itr_slots_]);
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700192 }
Ian Rogersb033c752011-07-20 12:22:35 -0700193}
194
Ian Rogers2c8f6532011-09-02 17:16:34 -0700195FrameOffset ArmJniCallingConvention::CurrentParamStackOffset() {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700196 CHECK_GE(itr_slots_, 4u);
Ian Rogersb033c752011-07-20 12:22:35 -0700197 return FrameOffset(displacement_.Int32Value() - OutArgSize()
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700198 + ((itr_slots_ - 4) * kPointerSize));
Ian Rogersb033c752011-07-20 12:22:35 -0700199}
200
Ian Rogers2c8f6532011-09-02 17:16:34 -0700201size_t ArmJniCallingConvention::NumberOfOutgoingStackArgs() {
202 Method* method = GetMethod();
Ian Rogers7a99c112011-09-07 12:48:27 -0700203 size_t static_args = method->IsStatic() ? 1 : 0; // count jclass
204 // regular argument parameters and this
205 size_t param_args = method->NumArgs() +
206 method->NumLongOrDoubleArgs();
207 // count JNIEnv* less arguments in registers
208 return static_args + param_args + 1 - 4;
Ian Rogersb033c752011-07-20 12:22:35 -0700209}
210
Ian Rogers2c8f6532011-09-02 17:16:34 -0700211} // namespace arm
Ian Rogersb033c752011-07-20 12:22:35 -0700212} // namespace art