blob: 99aea6246889553caa099b045324e65e56f0354f [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"
Andreas Gampebda1d602016-08-29 17:43:45 -070024#include "base/macros.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080025
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
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080078// Different than code alignment since code alignment is only first instruction of method.
79static constexpr size_t kThumb2InstructionAlignment = 2;
80static constexpr size_t kArm64InstructionAlignment = 4;
81static constexpr size_t kX86InstructionAlignment = 1;
82static constexpr size_t kX86_64InstructionAlignment = 1;
83static constexpr size_t kMipsInstructionAlignment = 2;
84static constexpr size_t kMips64InstructionAlignment = 2;
85
Ian Rogersd582fa42014-11-05 23:46:43 -080086const char* GetInstructionSetString(InstructionSet isa);
87
88// Note: Returns kNone when the string cannot be parsed to a known value.
89InstructionSet GetInstructionSetFromString(const char* instruction_set);
90
Andreas Gampe6f611412015-01-21 22:25:24 -080091InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags);
92
Andreas Gampebda1d602016-08-29 17:43:45 -070093// Fatal logging out of line to keep the header clean of logging.h.
94NO_RETURN void InstructionSetAbort(InstructionSet isa);
95
Andreas Gampe542451c2016-07-26 09:02:02 -070096static inline PointerSize GetInstructionSetPointerSize(InstructionSet isa) {
Ian Rogersd582fa42014-11-05 23:46:43 -080097 switch (isa) {
98 case kArm:
99 // Fall-through.
100 case kThumb2:
101 return kArmPointerSize;
102 case kArm64:
103 return kArm64PointerSize;
104 case kX86:
105 return kX86PointerSize;
106 case kX86_64:
107 return kX86_64PointerSize;
108 case kMips:
109 return kMipsPointerSize;
110 case kMips64:
111 return kMips64PointerSize;
Ian Rogersd582fa42014-11-05 23:46:43 -0800112 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700113 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800114 }
115}
116
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800117ALWAYS_INLINE static inline constexpr size_t GetInstructionSetInstructionAlignment(
118 InstructionSet isa) {
119 return (isa == kThumb2 || isa == kArm) ? kThumb2InstructionAlignment :
120 (isa == kArm64) ? kArm64InstructionAlignment :
121 (isa == kX86) ? kX86InstructionAlignment :
122 (isa == kX86_64) ? kX86_64InstructionAlignment :
123 (isa == kMips) ? kMipsInstructionAlignment :
124 (isa == kMips64) ? kMips64InstructionAlignment :
125 0; // Invalid case, but constexpr doesn't support asserts.
126}
127
Vladimir Marko09d09432015-09-08 13:47:48 +0100128static inline bool IsValidInstructionSet(InstructionSet isa) {
129 switch (isa) {
130 case kArm:
131 case kThumb2:
132 case kArm64:
133 case kX86:
134 case kX86_64:
135 case kMips:
136 case kMips64:
137 return true;
138 case kNone:
139 default:
140 return false;
141 }
142}
143
Ian Rogersd582fa42014-11-05 23:46:43 -0800144size_t GetInstructionSetAlignment(InstructionSet isa);
145
146static inline bool Is64BitInstructionSet(InstructionSet isa) {
147 switch (isa) {
148 case kArm:
149 case kThumb2:
150 case kX86:
151 case kMips:
152 return false;
153
154 case kArm64:
155 case kX86_64:
156 case kMips64:
157 return true;
158
Ian Rogersd582fa42014-11-05 23:46:43 -0800159 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700160 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800161 }
162}
163
Andreas Gampe542451c2016-07-26 09:02:02 -0700164static inline PointerSize InstructionSetPointerSize(InstructionSet isa) {
165 return Is64BitInstructionSet(isa) ? PointerSize::k64 : PointerSize::k32;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800166}
167
Ian Rogersd582fa42014-11-05 23:46:43 -0800168static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
169 switch (isa) {
170 case kArm:
171 // Fall-through.
172 case kThumb2:
173 return 4;
174 case kArm64:
175 return 8;
176 case kX86:
177 return 4;
178 case kX86_64:
179 return 8;
180 case kMips:
181 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800182 case kMips64:
183 return 8;
Andreas Gampebda1d602016-08-29 17:43:45 -0700184
Ian Rogersd582fa42014-11-05 23:46:43 -0800185 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700186 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800187 }
188}
189
190static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) {
191 switch (isa) {
192 case kArm:
193 // Fall-through.
194 case kThumb2:
195 return 4;
196 case kArm64:
197 return 8;
198 case kX86:
199 return 8;
200 case kX86_64:
201 return 8;
202 case kMips:
203 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800204 case kMips64:
205 return 8;
Andreas Gampebda1d602016-08-29 17:43:45 -0700206
Ian Rogersd582fa42014-11-05 23:46:43 -0800207 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700208 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800209 }
210}
211
212size_t GetStackOverflowReservedBytes(InstructionSet isa);
213
214// The following definitions create return types for two word-sized entities that will be passed
215// in registers so that memory operations for the interface trampolines can be avoided. The entities
216// are the resolved method and the pointer to the code to be invoked.
217//
218// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
219// uint64_t or long long int.
220//
Andreas Gampe57b34292015-01-14 15:45:59 -0800221// On x86_64, ARM64 and MIPS64, structs are decomposed for allocation, so we can create a structs of
222// two size_t-sized values.
Ian Rogersd582fa42014-11-05 23:46:43 -0800223//
224// We need two operations:
225//
226// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
227// GetTwoWordFailureValue() will return a value that has lower part == 0.
228//
229// 2) A value that combines two word-sized values.
230// GetTwoWordSuccessValue() constructs this.
231//
232// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
233// that the object does not move or the value is updated. Simple use of this is NOT SAFE
234// when the garbage collector can move objects concurrently. Ensure that required locks
235// are held when using!
236
Andreas Gampe57b34292015-01-14 15:45:59 -0800237#if defined(__i386__) || defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800238typedef uint64_t TwoWordReturn;
239
240// Encodes method_ptr==nullptr and code_ptr==nullptr
241static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
242 return 0;
243}
244
245// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
246static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
247 static_assert(sizeof(uint32_t) == sizeof(uintptr_t), "Unexpected size difference");
248 uint32_t lo32 = lo;
249 uint64_t hi64 = static_cast<uint64_t>(hi);
250 return ((hi64 << 32) | lo32);
251}
252
Andreas Gampe57b34292015-01-14 15:45:59 -0800253#elif defined(__x86_64__) || defined(__aarch64__) || (defined(__mips__) && defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800254struct TwoWordReturn {
255 uintptr_t lo;
256 uintptr_t hi;
257};
258
259// Encodes method_ptr==nullptr. Leaves random value in code pointer.
260static inline TwoWordReturn GetTwoWordFailureValue() {
261 TwoWordReturn ret;
262 ret.lo = 0;
263 return ret;
264}
265
266// Write values into their respective members.
267static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
268 TwoWordReturn ret;
269 ret.lo = lo;
270 ret.hi = hi;
271 return ret;
272}
273#else
274#error "Unsupported architecture"
275#endif
276
277} // namespace art
278
279#endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_H_