blob: da7d153c08daf51538122ce44bbff1e9d7ae9cd3 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -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 */
Ian Rogersb033c752011-07-20 12:22:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INSTRUCTION_SET_H_
18#define ART_RUNTIME_INSTRUCTION_SET_H_
Ian Rogersb033c752011-07-20 12:22:35 -070019
Ian Rogersc8b306f2012-02-17 21:34:44 -080020#include <iosfwd>
Dave Allison70202782013-10-22 17:52:19 -070021#include <string>
22
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070023#include "base/logging.h" // Logging is required for FATAL in the helper functions.
Dave Allison70202782013-10-22 17:52:19 -070024#include "base/macros.h"
Andreas Gampe7cd26f32014-06-18 17:01:15 -070025#include "globals.h" // For KB.
Ian Rogersc8b306f2012-02-17 21:34:44 -080026
buzbeec143c552011-08-20 17:38:58 -070027namespace art {
28
29enum InstructionSet {
30 kNone,
31 kArm,
Serban Constantinescued8dd492014-02-11 14:15:10 +000032 kArm64,
buzbeec143c552011-08-20 17:38:58 -070033 kThumb2,
Shih-wei Liao6edfde42012-03-01 15:49:12 -080034 kX86,
Ian Rogersef7d42f2014-01-06 12:55:46 -080035 kX86_64,
Douglas Leung2db3e262014-06-25 16:02:55 -070036 kMips,
37 kMips64
buzbeec143c552011-08-20 17:38:58 -070038};
Ian Rogers8afeb852014-04-02 14:55:49 -070039std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs);
buzbeec143c552011-08-20 17:38:58 -070040
Andreas Gampe7cd26f32014-06-18 17:01:15 -070041#if defined(__arm__)
42static constexpr InstructionSet kRuntimeISA = kArm;
43#elif defined(__aarch64__)
44static constexpr InstructionSet kRuntimeISA = kArm64;
45#elif defined(__mips__)
46static constexpr InstructionSet kRuntimeISA = kMips;
47#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
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070055// 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 kX86PointerSize = 4;
60static constexpr size_t kX86_64PointerSize = 8;
61
62// ARM instruction alignment. ARM processors require code to be 4-byte aligned,
63// but ARM ELF requires 8..
64static constexpr size_t kArmAlignment = 8;
65
66// ARM64 instruction alignment. This is the recommended alignment for maximum performance.
67static constexpr size_t kArm64Alignment = 16;
68
69// MIPS instruction alignment. MIPS processors require code to be 4-byte aligned.
70// TODO: Can this be 4?
71static constexpr size_t kMipsAlignment = 8;
72
73// X86 instruction alignment. This is the recommended alignment for maximum performance.
74static constexpr size_t kX86Alignment = 16;
75
76
Brian Carlstrom2afe4942014-05-19 10:25:33 -070077const char* GetInstructionSetString(InstructionSet isa);
Andreas Gampeaabbb202014-08-19 17:28:06 -070078
79// Note: Returns kNone when the string cannot be parsed to a known value.
Narayan Kamath11d9f062014-04-23 20:24:57 +010080InstructionSet GetInstructionSetFromString(const char* instruction_set);
81
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070082static inline size_t GetInstructionSetPointerSize(InstructionSet isa) {
83 switch (isa) {
84 case kArm:
85 // Fall-through.
86 case kThumb2:
87 return kArmPointerSize;
88 case kArm64:
89 return kArm64PointerSize;
90 case kX86:
91 return kX86PointerSize;
92 case kX86_64:
93 return kX86_64PointerSize;
94 case kMips:
95 return kMipsPointerSize;
96 case kNone:
97 LOG(FATAL) << "ISA kNone does not have pointer size.";
98 return 0;
99 default:
100 LOG(FATAL) << "Unknown ISA " << isa;
101 return 0;
102 }
103}
104
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700105size_t GetInstructionSetAlignment(InstructionSet isa);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700106
107static inline bool Is64BitInstructionSet(InstructionSet isa) {
108 switch (isa) {
109 case kArm:
110 case kThumb2:
111 case kX86:
112 case kMips:
113 return false;
114
115 case kArm64:
116 case kX86_64:
117 return true;
118
119 case kNone:
120 LOG(FATAL) << "ISA kNone does not have bit width.";
121 return 0;
122 default:
123 LOG(FATAL) << "Unknown ISA " << isa;
124 return 0;
125 }
126}
127
Mathieu Chartiere832e642014-11-10 11:08:06 -0800128static inline size_t InstructionSetPointerSize(InstructionSet isa) {
129 return Is64BitInstructionSet(isa) ? 8U : 4U;
130}
131
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700132static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
133 switch (isa) {
134 case kArm:
135 // Fall-through.
136 case kThumb2:
137 return 4;
138 case kArm64:
139 return 8;
140 case kX86:
141 return 4;
142 case kX86_64:
143 return 8;
144 case kMips:
145 return 4;
146 case kNone:
147 LOG(FATAL) << "ISA kNone does not have spills.";
148 return 0;
149 default:
150 LOG(FATAL) << "Unknown ISA " << isa;
151 return 0;
152 }
153}
154
155static inline size_t GetBytesPerFprSpillLocation(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 8;
165 case kX86_64:
166 return 8;
167 case kMips:
168 return 4;
169 case kNone:
170 LOG(FATAL) << "ISA kNone does not have spills.";
171 return 0;
172 default:
173 LOG(FATAL) << "Unknown ISA " << isa;
174 return 0;
175 }
176}
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700177
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700178size_t GetStackOverflowReservedBytes(InstructionSet isa);
Andreas Gampe91268c12014-04-03 17:50:24 -0700179
Dave Allison70202782013-10-22 17:52:19 -0700180enum InstructionFeatures {
Vladimir Marko674744e2014-04-24 15:18:26 +0100181 kHwDiv = 0x1, // Supports hardware divide.
182 kHwLpae = 0x2, // Supports Large Physical Address Extension.
Dave Allison70202782013-10-22 17:52:19 -0700183};
184
185// This is a bitmask of supported features per architecture.
186class PACKED(4) InstructionSetFeatures {
187 public:
188 InstructionSetFeatures() : mask_(0) {}
189 explicit InstructionSetFeatures(uint32_t mask) : mask_(mask) {}
190
Ian Rogers8afeb852014-04-02 14:55:49 -0700191 static InstructionSetFeatures GuessInstructionSetFeatures();
192
Dave Allison70202782013-10-22 17:52:19 -0700193 bool HasDivideInstruction() const {
194 return (mask_ & kHwDiv) != 0;
195 }
196
197 void SetHasDivideInstruction(bool v) {
198 mask_ = (mask_ & ~kHwDiv) | (v ? kHwDiv : 0);
199 }
200
Vladimir Marko674744e2014-04-24 15:18:26 +0100201 bool HasLpae() const {
202 return (mask_ & kHwLpae) != 0;
203 }
204
205 void SetHasLpae(bool v) {
206 mask_ = (mask_ & ~kHwLpae) | (v ? kHwLpae : 0);
207 }
208
Ian Rogers8afeb852014-04-02 14:55:49 -0700209 std::string GetFeatureString() const;
Dave Allison70202782013-10-22 17:52:19 -0700210
211 // Other features in here.
212
213 bool operator==(const InstructionSetFeatures &peer) const {
214 return mask_ == peer.mask_;
215 }
216
217 bool operator!=(const InstructionSetFeatures &peer) const {
218 return mask_ != peer.mask_;
219 }
220
Serban Constantinescu75b91132014-04-09 18:39:10 +0100221 bool operator<=(const InstructionSetFeatures &peer) const {
222 return (mask_ & peer.mask_) == mask_;
223 }
224
Dave Allison70202782013-10-22 17:52:19 -0700225 private:
226 uint32_t mask_;
227};
228
Andreas Gamped58342c2014-06-05 14:18:08 -0700229// The following definitions create return types for two word-sized entities that will be passed
230// in registers so that memory operations for the interface trampolines can be avoided. The entities
231// are the resolved method and the pointer to the code to be invoked.
232//
233// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
234// uint64_t or long long int.
235//
236// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two
237// size_t-sized values.
238//
239// We need two operations:
240//
241// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
242// GetTwoWordFailureValue() will return a value that has lower part == 0.
243//
244// 2) A value that combines two word-sized values.
245// GetTwoWordSuccessValue() constructs this.
246//
247// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
248// that the object does not move or the value is updated. Simple use of this is NOT SAFE
249// when the garbage collector can move objects concurrently. Ensure that required locks
250// are held when using!
251
252#if defined(__i386__) || defined(__arm__) || defined(__mips__)
253typedef uint64_t TwoWordReturn;
254
255// Encodes method_ptr==nullptr and code_ptr==nullptr
256static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
257 return 0;
258}
259
260// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
261static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
262 uint32_t lo32 = static_cast<uint32_t>(lo);
263 uint64_t hi64 = static_cast<uint64_t>(hi);
264 return ((hi64 << 32) | lo32);
265}
266
267#elif defined(__x86_64__) || defined(__aarch64__)
268struct TwoWordReturn {
269 uintptr_t lo;
270 uintptr_t hi;
271};
272
273// Encodes method_ptr==nullptr. Leaves random value in code pointer.
274static inline TwoWordReturn GetTwoWordFailureValue() {
275 TwoWordReturn ret;
276 ret.lo = 0;
277 return ret;
278}
279
280// Write values into their respective members.
281static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
282 TwoWordReturn ret;
283 ret.lo = lo;
284 ret.hi = hi;
285 return ret;
286}
287#else
288#error "Unsupported architecture"
289#endif
290
buzbeec143c552011-08-20 17:38:58 -0700291} // namespace art
292
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700293#endif // ART_RUNTIME_INSTRUCTION_SET_H_