blob: 9135e58c1a9aca760414f623661a42cc78c08729 [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
23#include "base/logging.h" // Logging is required for FATAL in the helper functions.
24
25namespace art {
26
27enum InstructionSet {
28 kNone,
29 kArm,
30 kArm64,
31 kThumb2,
32 kX86,
33 kX86_64,
34 kMips,
35 kMips64
36};
37std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs);
38
39#if defined(__arm__)
40static constexpr InstructionSet kRuntimeISA = kArm;
41#elif defined(__aarch64__)
42static constexpr InstructionSet kRuntimeISA = kArm64;
Andreas Gampe57b34292015-01-14 15:45:59 -080043#elif defined(__mips__) && !defined(__LP64__)
Ian Rogersd582fa42014-11-05 23:46:43 -080044static constexpr InstructionSet kRuntimeISA = kMips;
Andreas Gampe57b34292015-01-14 15:45:59 -080045#elif defined(__mips__) && defined(__LP64__)
46static constexpr InstructionSet kRuntimeISA = kMips64;
Ian Rogersd582fa42014-11-05 23:46:43 -080047#elif defined(__i386__)
48static constexpr InstructionSet kRuntimeISA = kX86;
49#elif defined(__x86_64__)
50static constexpr InstructionSet kRuntimeISA = kX86_64;
51#else
52static constexpr InstructionSet kRuntimeISA = kNone;
53#endif
54
55// Architecture-specific pointer sizes
56static constexpr size_t kArmPointerSize = 4;
57static constexpr size_t kArm64PointerSize = 8;
58static constexpr size_t kMipsPointerSize = 4;
59static constexpr size_t kMips64PointerSize = 8;
60static constexpr size_t kX86PointerSize = 4;
61static constexpr size_t kX86_64PointerSize = 8;
62
63// ARM instruction alignment. ARM processors require code to be 4-byte aligned,
64// but ARM ELF requires 8..
65static constexpr size_t kArmAlignment = 8;
66
67// ARM64 instruction alignment. This is the recommended alignment for maximum performance.
68static constexpr size_t kArm64Alignment = 16;
69
70// MIPS instruction alignment. MIPS processors require code to be 4-byte aligned.
71// TODO: Can this be 4?
72static constexpr size_t kMipsAlignment = 8;
73
74// X86 instruction alignment. This is the recommended alignment for maximum performance.
75static constexpr size_t kX86Alignment = 16;
76
77
78const 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
83static inline size_t GetInstructionSetPointerSize(InstructionSet isa) {
84 switch (isa) {
85 case kArm:
86 // Fall-through.
87 case kThumb2:
88 return kArmPointerSize;
89 case kArm64:
90 return kArm64PointerSize;
91 case kX86:
92 return kX86PointerSize;
93 case kX86_64:
94 return kX86_64PointerSize;
95 case kMips:
96 return kMipsPointerSize;
97 case kMips64:
98 return kMips64PointerSize;
99 case kNone:
100 LOG(FATAL) << "ISA kNone does not have pointer size.";
101 UNREACHABLE();
102 default:
103 LOG(FATAL) << "Unknown ISA " << isa;
104 UNREACHABLE();
105 }
106}
107
108size_t GetInstructionSetAlignment(InstructionSet isa);
109
110static inline bool Is64BitInstructionSet(InstructionSet isa) {
111 switch (isa) {
112 case kArm:
113 case kThumb2:
114 case kX86:
115 case kMips:
116 return false;
117
118 case kArm64:
119 case kX86_64:
120 case kMips64:
121 return true;
122
123 case kNone:
124 LOG(FATAL) << "ISA kNone does not have bit width.";
125 UNREACHABLE();
126 default:
127 LOG(FATAL) << "Unknown ISA " << isa;
128 UNREACHABLE();
129 }
130}
131
Mathieu Chartier2d721012014-11-10 11:08:06 -0800132static inline size_t InstructionSetPointerSize(InstructionSet isa) {
133 return Is64BitInstructionSet(isa) ? 8U : 4U;
134}
135
Ian Rogersd582fa42014-11-05 23:46:43 -0800136static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
137 switch (isa) {
138 case kArm:
139 // Fall-through.
140 case kThumb2:
141 return 4;
142 case kArm64:
143 return 8;
144 case kX86:
145 return 4;
146 case kX86_64:
147 return 8;
148 case kMips:
149 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800150 case kMips64:
151 return 8;
Ian Rogersd582fa42014-11-05 23:46:43 -0800152 case kNone:
153 LOG(FATAL) << "ISA kNone does not have spills.";
154 UNREACHABLE();
155 default:
156 LOG(FATAL) << "Unknown ISA " << isa;
157 UNREACHABLE();
158 }
159}
160
161static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) {
162 switch (isa) {
163 case kArm:
164 // Fall-through.
165 case kThumb2:
166 return 4;
167 case kArm64:
168 return 8;
169 case kX86:
170 return 8;
171 case kX86_64:
172 return 8;
173 case kMips:
174 return 4;
Andreas Gampe57b34292015-01-14 15:45:59 -0800175 case kMips64:
176 return 8;
Ian Rogersd582fa42014-11-05 23:46:43 -0800177 case kNone:
178 LOG(FATAL) << "ISA kNone does not have spills.";
179 UNREACHABLE();
180 default:
181 LOG(FATAL) << "Unknown ISA " << isa;
182 UNREACHABLE();
183 }
184}
185
186size_t GetStackOverflowReservedBytes(InstructionSet isa);
187
188// The following definitions create return types for two word-sized entities that will be passed
189// in registers so that memory operations for the interface trampolines can be avoided. The entities
190// are the resolved method and the pointer to the code to be invoked.
191//
192// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
193// uint64_t or long long int.
194//
Andreas Gampe57b34292015-01-14 15:45:59 -0800195// On x86_64, ARM64 and MIPS64, structs are decomposed for allocation, so we can create a structs of
196// two size_t-sized values.
Ian Rogersd582fa42014-11-05 23:46:43 -0800197//
198// We need two operations:
199//
200// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
201// GetTwoWordFailureValue() will return a value that has lower part == 0.
202//
203// 2) A value that combines two word-sized values.
204// GetTwoWordSuccessValue() constructs this.
205//
206// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
207// that the object does not move or the value is updated. Simple use of this is NOT SAFE
208// when the garbage collector can move objects concurrently. Ensure that required locks
209// are held when using!
210
Andreas Gampe57b34292015-01-14 15:45:59 -0800211#if defined(__i386__) || defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800212typedef uint64_t TwoWordReturn;
213
214// Encodes method_ptr==nullptr and code_ptr==nullptr
215static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
216 return 0;
217}
218
219// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
220static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
221 static_assert(sizeof(uint32_t) == sizeof(uintptr_t), "Unexpected size difference");
222 uint32_t lo32 = lo;
223 uint64_t hi64 = static_cast<uint64_t>(hi);
224 return ((hi64 << 32) | lo32);
225}
226
Andreas Gampe57b34292015-01-14 15:45:59 -0800227#elif defined(__x86_64__) || defined(__aarch64__) || (defined(__mips__) && defined(__LP64__))
Ian Rogersd582fa42014-11-05 23:46:43 -0800228struct TwoWordReturn {
229 uintptr_t lo;
230 uintptr_t hi;
231};
232
233// Encodes method_ptr==nullptr. Leaves random value in code pointer.
234static inline TwoWordReturn GetTwoWordFailureValue() {
235 TwoWordReturn ret;
236 ret.lo = 0;
237 return ret;
238}
239
240// Write values into their respective members.
241static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
242 TwoWordReturn ret;
243 ret.lo = lo;
244 ret.hi = hi;
245 return ret;
246}
247#else
248#error "Unsupported architecture"
249#endif
250
251} // namespace art
252
253#endif // ART_RUNTIME_ARCH_INSTRUCTION_SET_H_