blob: 4a8bea45e50f83f4c3eda1eebcc7fc82dc130a5b [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
Ian Rogersd582fa42014-11-05 23:46:43 -080078const char* GetInstructionSetString(InstructionSet isa);
79
80// Note: Returns kNone when the string cannot be parsed to a known value.
81InstructionSet GetInstructionSetFromString(const char* instruction_set);
82
Andreas Gampe6f611412015-01-21 22:25:24 -080083InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags);
84
Andreas Gampebda1d602016-08-29 17:43:45 -070085// Fatal logging out of line to keep the header clean of logging.h.
86NO_RETURN void InstructionSetAbort(InstructionSet isa);
87
Andreas Gampe542451c2016-07-26 09:02:02 -070088static inline PointerSize GetInstructionSetPointerSize(InstructionSet isa) {
Ian Rogersd582fa42014-11-05 23:46:43 -080089 switch (isa) {
90 case kArm:
91 // Fall-through.
92 case kThumb2:
93 return kArmPointerSize;
94 case kArm64:
95 return kArm64PointerSize;
96 case kX86:
97 return kX86PointerSize;
98 case kX86_64:
99 return kX86_64PointerSize;
100 case kMips:
101 return kMipsPointerSize;
102 case kMips64:
103 return kMips64PointerSize;
Ian Rogersd582fa42014-11-05 23:46:43 -0800104 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700105 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800106 }
107}
108
Vladimir Marko09d09432015-09-08 13:47:48 +0100109static inline bool IsValidInstructionSet(InstructionSet isa) {
110 switch (isa) {
111 case kArm:
112 case kThumb2:
113 case kArm64:
114 case kX86:
115 case kX86_64:
116 case kMips:
117 case kMips64:
118 return true;
119 case kNone:
120 default:
121 return false;
122 }
123}
124
Ian Rogersd582fa42014-11-05 23:46:43 -0800125size_t GetInstructionSetAlignment(InstructionSet isa);
126
127static inline bool Is64BitInstructionSet(InstructionSet isa) {
128 switch (isa) {
129 case kArm:
130 case kThumb2:
131 case kX86:
132 case kMips:
133 return false;
134
135 case kArm64:
136 case kX86_64:
137 case kMips64:
138 return true;
139
Ian Rogersd582fa42014-11-05 23:46:43 -0800140 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700141 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800142 }
143}
144
Andreas Gampe542451c2016-07-26 09:02:02 -0700145static inline PointerSize InstructionSetPointerSize(InstructionSet isa) {
146 return Is64BitInstructionSet(isa) ? PointerSize::k64 : PointerSize::k32;
Mathieu Chartier2d721012014-11-10 11:08:06 -0800147}
148
Ian Rogersd582fa42014-11-05 23:46:43 -0800149static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
150 switch (isa) {
151 case kArm:
152 // Fall-through.
153 case kThumb2:
154 return 4;
155 case kArm64:
156 return 8;
157 case kX86:
158 return 4;
159 case kX86_64:
160 return 8;
161 case kMips:
162 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800163 case kMips64:
164 return 8;
Andreas Gampebda1d602016-08-29 17:43:45 -0700165
Ian Rogersd582fa42014-11-05 23:46:43 -0800166 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700167 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800168 }
169}
170
171static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) {
172 switch (isa) {
173 case kArm:
174 // Fall-through.
175 case kThumb2:
176 return 4;
177 case kArm64:
178 return 8;
179 case kX86:
180 return 8;
181 case kX86_64:
182 return 8;
183 case kMips:
184 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800185 case kMips64:
186 return 8;
Andreas Gampebda1d602016-08-29 17:43:45 -0700187
Ian Rogersd582fa42014-11-05 23:46:43 -0800188 default:
Andreas Gampebda1d602016-08-29 17:43:45 -0700189 InstructionSetAbort(isa);
Ian Rogersd582fa42014-11-05 23:46:43 -0800190 }
191}
192
193size_t GetStackOverflowReservedBytes(InstructionSet isa);
194
195// The following definitions create return types for two word-sized entities that will be passed
196// in registers so that memory operations for the interface trampolines can be avoided. The entities
197// are the resolved method and the pointer to the code to be invoked.
198//
199// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
200// uint64_t or long long int.
201//
Andreas Gampe57b34292015-01-14 15:45:59 -0800202// On x86_64, ARM64 and MIPS64, structs are decomposed for allocation, so we can create a structs of
203// two size_t-sized values.
Ian Rogersd582fa42014-11-05 23:46:43 -0800204//
205// We need two operations:
206//
207// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
208// GetTwoWordFailureValue() will return a value that has lower part == 0.
209//
210// 2) A value that combines two word-sized values.
211// GetTwoWordSuccessValue() constructs this.
212//
213// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
214// that the object does not move or the value is updated. Simple use of this is NOT SAFE
215// when the garbage collector can move objects concurrently. Ensure that required locks
216// are held when using!
217
Andreas Gampe57b34292015-01-14 15:45:59 -0800218#if defined(__i386__) || defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800219typedef uint64_t TwoWordReturn;
220
221// Encodes method_ptr==nullptr and code_ptr==nullptr
222static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
223 return 0;
224}
225
226// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
227static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
228 static_assert(sizeof(uint32_t) == sizeof(uintptr_t), "Unexpected size difference");
229 uint32_t lo32 = lo;
230 uint64_t hi64 = static_cast<uint64_t>(hi);
231 return ((hi64 << 32) | lo32);
232}
233
Andreas Gampe57b34292015-01-14 15:45:59 -0800234#elif defined(__x86_64__) || defined(__aarch64__) || (defined(__mips__) && defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800235struct TwoWordReturn {
236 uintptr_t lo;
237 uintptr_t hi;
238};
239
240// Encodes method_ptr==nullptr. Leaves random value in code pointer.
241static inline TwoWordReturn GetTwoWordFailureValue() {
242 TwoWordReturn ret;
243 ret.lo = 0;
244 return ret;
245}
246
247// Write values into their respective members.
248static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
249 TwoWordReturn ret;
250 ret.lo = lo;
251 ret.hi = hi;
252 return ret;
253}
254#else
255#error "Unsupported architecture"
256#endif
257
258} // namespace art
259
260#endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_H_