blob: 917acc9eec1dff375443c77419c3c026fca0c37e [file] [log] [blame]
Ian Rogersd582fa42014-11-05 23:46:43 -08001/*
2 * Copyright (C) 2011 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#ifndef ART_RUNTIME_ARCH_INSTRUCTION_SET_H_
18#define ART_RUNTIME_ARCH_INSTRUCTION_SET_H_
19
20#include <iosfwd>
21#include <string>
22
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "base/logging.h" // Logging is required for FATAL in the helper functions.
25
26namespace art {
27
28enum InstructionSet {
29 kNone,
30 kArm,
31 kArm64,
32 kThumb2,
33 kX86,
34 kX86_64,
35 kMips,
36 kMips64
37};
38std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs);
39
40#if defined(__arm__)
41static constexpr InstructionSet kRuntimeISA = kArm;
42#elif defined(__aarch64__)
43static constexpr InstructionSet kRuntimeISA = kArm64;
Andreas Gampe57b34292015-01-14 15:45:59 -080044#elif defined(__mips__) && !defined(__LP64__)
Ian Rogersd582fa42014-11-05 23:46:43 -080045static constexpr InstructionSet kRuntimeISA = kMips;
Andreas Gampe57b34292015-01-14 15:45:59 -080046#elif defined(__mips__) && defined(__LP64__)
47static constexpr InstructionSet kRuntimeISA = kMips64;
Ian Rogersd582fa42014-11-05 23:46:43 -080048#elif defined(__i386__)
49static constexpr InstructionSet kRuntimeISA = kX86;
50#elif defined(__x86_64__)
51static constexpr InstructionSet kRuntimeISA = kX86_64;
52#else
53static constexpr InstructionSet kRuntimeISA = kNone;
54#endif
55
56// Architecture-specific pointer sizes
Andreas Gampe542451c2016-07-26 09:02:02 -070057static constexpr PointerSize kArmPointerSize = PointerSize::k32;
58static constexpr PointerSize kArm64PointerSize = PointerSize::k64;
59static constexpr PointerSize kMipsPointerSize = PointerSize::k32;
60static constexpr PointerSize kMips64PointerSize = PointerSize::k64;
61static constexpr PointerSize kX86PointerSize = PointerSize::k32;
62static constexpr PointerSize kX86_64PointerSize = PointerSize::k64;
Ian Rogersd582fa42014-11-05 23:46:43 -080063
64// ARM instruction alignment. ARM processors require code to be 4-byte aligned,
65// but ARM ELF requires 8..
66static constexpr size_t kArmAlignment = 8;
67
68// ARM64 instruction alignment. This is the recommended alignment for maximum performance.
69static constexpr size_t kArm64Alignment = 16;
70
71// MIPS instruction alignment. MIPS processors require code to be 4-byte aligned.
72// TODO: Can this be 4?
73static constexpr size_t kMipsAlignment = 8;
74
75// X86 instruction alignment. This is the recommended alignment for maximum performance.
76static constexpr size_t kX86Alignment = 16;
77
78
79const char* GetInstructionSetString(InstructionSet isa);
80
81// Note: Returns kNone when the string cannot be parsed to a known value.
82InstructionSet GetInstructionSetFromString(const char* instruction_set);
83
Andreas Gampe6f611412015-01-21 22:25:24 -080084InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags);
85
Andreas Gampe542451c2016-07-26 09:02:02 -070086static inline PointerSize GetInstructionSetPointerSize(InstructionSet isa) {
Ian Rogersd582fa42014-11-05 23:46:43 -080087 switch (isa) {
88 case kArm:
89 // Fall-through.
90 case kThumb2:
91 return kArmPointerSize;
92 case kArm64:
93 return kArm64PointerSize;
94 case kX86:
95 return kX86PointerSize;
96 case kX86_64:
97 return kX86_64PointerSize;
98 case kMips:
99 return kMipsPointerSize;
100 case kMips64:
101 return kMips64PointerSize;
102 case kNone:
103 LOG(FATAL) << "ISA kNone does not have pointer size.";
104 UNREACHABLE();
105 default:
106 LOG(FATAL) << "Unknown ISA " << isa;
107 UNREACHABLE();
108 }
109}
110
Vladimir Marko09d09432015-09-08 13:47:48 +0100111static inline bool IsValidInstructionSet(InstructionSet isa) {
112 switch (isa) {
113 case kArm:
114 case kThumb2:
115 case kArm64:
116 case kX86:
117 case kX86_64:
118 case kMips:
119 case kMips64:
120 return true;
121 case kNone:
122 default:
123 return false;
124 }
125}
126
Ian Rogersd582fa42014-11-05 23:46:43 -0800127size_t GetInstructionSetAlignment(InstructionSet isa);
128
129static inline bool Is64BitInstructionSet(InstructionSet isa) {
130 switch (isa) {
131 case kArm:
132 case kThumb2:
133 case kX86:
134 case kMips:
135 return false;
136
137 case kArm64:
138 case kX86_64:
139 case kMips64:
140 return true;
141
142 case kNone:
143 LOG(FATAL) << "ISA kNone does not have bit width.";
144 UNREACHABLE();
145 default:
146 LOG(FATAL) << "Unknown ISA " << isa;
147 UNREACHABLE();
148 }
149}
150
Andreas Gampe542451c2016-07-26 09:02:02 -0700151static inline PointerSize InstructionSetPointerSize(InstructionSet isa) {
152 return Is64BitInstructionSet(isa) ? PointerSize::k64 : PointerSize::k32;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800153}
154
Ian Rogersd582fa42014-11-05 23:46:43 -0800155static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
156 switch (isa) {
157 case kArm:
158 // Fall-through.
159 case kThumb2:
160 return 4;
161 case kArm64:
162 return 8;
163 case kX86:
164 return 4;
165 case kX86_64:
166 return 8;
167 case kMips:
168 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800169 case kMips64:
170 return 8;
Ian Rogersd582fa42014-11-05 23:46:43 -0800171 case kNone:
172 LOG(FATAL) << "ISA kNone does not have spills.";
173 UNREACHABLE();
174 default:
175 LOG(FATAL) << "Unknown ISA " << isa;
176 UNREACHABLE();
177 }
178}
179
180static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) {
181 switch (isa) {
182 case kArm:
183 // Fall-through.
184 case kThumb2:
185 return 4;
186 case kArm64:
187 return 8;
188 case kX86:
189 return 8;
190 case kX86_64:
191 return 8;
192 case kMips:
193 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800194 case kMips64:
195 return 8;
Ian Rogersd582fa42014-11-05 23:46:43 -0800196 case kNone:
197 LOG(FATAL) << "ISA kNone does not have spills.";
198 UNREACHABLE();
199 default:
200 LOG(FATAL) << "Unknown ISA " << isa;
201 UNREACHABLE();
202 }
203}
204
205size_t GetStackOverflowReservedBytes(InstructionSet isa);
206
207// The following definitions create return types for two word-sized entities that will be passed
208// in registers so that memory operations for the interface trampolines can be avoided. The entities
209// are the resolved method and the pointer to the code to be invoked.
210//
211// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
212// uint64_t or long long int.
213//
Andreas Gampe57b34292015-01-14 15:45:59 -0800214// On x86_64, ARM64 and MIPS64, structs are decomposed for allocation, so we can create a structs of
215// two size_t-sized values.
Ian Rogersd582fa42014-11-05 23:46:43 -0800216//
217// We need two operations:
218//
219// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
220// GetTwoWordFailureValue() will return a value that has lower part == 0.
221//
222// 2) A value that combines two word-sized values.
223// GetTwoWordSuccessValue() constructs this.
224//
225// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
226// that the object does not move or the value is updated. Simple use of this is NOT SAFE
227// when the garbage collector can move objects concurrently. Ensure that required locks
228// are held when using!
229
Andreas Gampe57b34292015-01-14 15:45:59 -0800230#if defined(__i386__) || defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800231typedef uint64_t TwoWordReturn;
232
233// Encodes method_ptr==nullptr and code_ptr==nullptr
234static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
235 return 0;
236}
237
238// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
239static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
240 static_assert(sizeof(uint32_t) == sizeof(uintptr_t), "Unexpected size difference");
241 uint32_t lo32 = lo;
242 uint64_t hi64 = static_cast<uint64_t>(hi);
243 return ((hi64 << 32) | lo32);
244}
245
Andreas Gampe57b34292015-01-14 15:45:59 -0800246#elif defined(__x86_64__) || defined(__aarch64__) || (defined(__mips__) && defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800247struct TwoWordReturn {
248 uintptr_t lo;
249 uintptr_t hi;
250};
251
252// Encodes method_ptr==nullptr. Leaves random value in code pointer.
253static inline TwoWordReturn GetTwoWordFailureValue() {
254 TwoWordReturn ret;
255 ret.lo = 0;
256 return ret;
257}
258
259// Write values into their respective members.
260static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
261 TwoWordReturn ret;
262 ret.lo = lo;
263 ret.hi = hi;
264 return ret;
265}
266#else
267#error "Unsupported architecture"
268#endif
269
270} // namespace art
271
272#endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_H_