Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_ARCH_INSTRUCTION_SET_H_ |
| 18 | #define ART_LIBARTBASE_ARCH_INSTRUCTION_SET_H_ |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <iosfwd> |
| 21 | #include <string> |
| 22 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 23 | #include "base/enums.h" |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 28 | enum class InstructionSet { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 29 | kNone, |
| 30 | kArm, |
| 31 | kArm64, |
| 32 | kThumb2, |
| 33 | kX86, |
| 34 | kX86_64, |
| 35 | kMips, |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 36 | kMips64, |
| 37 | kLast = kMips64 |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 38 | }; |
| 39 | std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs); |
| 40 | |
| 41 | #if defined(__arm__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 42 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kArm; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 43 | #elif defined(__aarch64__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 44 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kArm64; |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 45 | #elif defined(__mips__) && !defined(__LP64__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 46 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kMips; |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 47 | #elif defined(__mips__) && defined(__LP64__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 48 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kMips64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 49 | #elif defined(__i386__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 50 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kX86; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 51 | #elif defined(__x86_64__) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 52 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kX86_64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 53 | #else |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 54 | static constexpr InstructionSet kRuntimeISA = InstructionSet::kNone; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | // Architecture-specific pointer sizes |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 58 | static constexpr PointerSize kArmPointerSize = PointerSize::k32; |
| 59 | static constexpr PointerSize kArm64PointerSize = PointerSize::k64; |
| 60 | static constexpr PointerSize kMipsPointerSize = PointerSize::k32; |
| 61 | static constexpr PointerSize kMips64PointerSize = PointerSize::k64; |
| 62 | static constexpr PointerSize kX86PointerSize = PointerSize::k32; |
| 63 | static constexpr PointerSize kX86_64PointerSize = PointerSize::k64; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 64 | |
| 65 | // ARM instruction alignment. ARM processors require code to be 4-byte aligned, |
| 66 | // but ARM ELF requires 8.. |
| 67 | static constexpr size_t kArmAlignment = 8; |
| 68 | |
| 69 | // ARM64 instruction alignment. This is the recommended alignment for maximum performance. |
| 70 | static constexpr size_t kArm64Alignment = 16; |
| 71 | |
Alexey Frunze | 85885fe | 2017-02-01 16:25:37 -0800 | [diff] [blame] | 72 | // MIPS instruction alignment. MIPS processors require code to be 4-byte aligned, |
| 73 | // but 64-bit literals must be 8-byte aligned. |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 74 | static constexpr size_t kMipsAlignment = 8; |
| 75 | |
| 76 | // X86 instruction alignment. This is the recommended alignment for maximum performance. |
| 77 | static constexpr size_t kX86Alignment = 16; |
| 78 | |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 79 | // Different than code alignment since code alignment is only first instruction of method. |
| 80 | static constexpr size_t kThumb2InstructionAlignment = 2; |
| 81 | static constexpr size_t kArm64InstructionAlignment = 4; |
| 82 | static constexpr size_t kX86InstructionAlignment = 1; |
| 83 | static constexpr size_t kX86_64InstructionAlignment = 1; |
Alexey Frunze | 85885fe | 2017-02-01 16:25:37 -0800 | [diff] [blame] | 84 | static constexpr size_t kMipsInstructionAlignment = 4; |
| 85 | static constexpr size_t kMips64InstructionAlignment = 4; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 86 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 87 | const char* GetInstructionSetString(InstructionSet isa); |
| 88 | |
| 89 | // Note: Returns kNone when the string cannot be parsed to a known value. |
| 90 | InstructionSet GetInstructionSetFromString(const char* instruction_set); |
| 91 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 92 | // Fatal logging out of line to keep the header clean of logging.h. |
| 93 | NO_RETURN void InstructionSetAbort(InstructionSet isa); |
| 94 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 95 | constexpr PointerSize GetInstructionSetPointerSize(InstructionSet isa) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 96 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 97 | case InstructionSet::kArm: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 98 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 99 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 100 | return kArmPointerSize; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 101 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 102 | return kArm64PointerSize; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 103 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 104 | return kX86PointerSize; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 105 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 106 | return kX86_64PointerSize; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 107 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 108 | return kMipsPointerSize; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 109 | case InstructionSet::kMips64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 110 | return kMips64PointerSize; |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 111 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 112 | case InstructionSet::kNone: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 113 | break; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 114 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 115 | InstructionSetAbort(isa); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 118 | constexpr size_t GetInstructionSetInstructionAlignment(InstructionSet isa) { |
| 119 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 120 | case InstructionSet::kArm: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 121 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 122 | case InstructionSet::kThumb2: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 123 | return kThumb2InstructionAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 124 | case InstructionSet::kArm64: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 125 | return kArm64InstructionAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 126 | case InstructionSet::kX86: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 127 | return kX86InstructionAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 128 | case InstructionSet::kX86_64: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 129 | return kX86_64InstructionAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 130 | case InstructionSet::kMips: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 131 | return kMipsInstructionAlignment; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 132 | case InstructionSet::kMips64: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 133 | return kMips64InstructionAlignment; |
| 134 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 135 | case InstructionSet::kNone: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | InstructionSetAbort(isa); |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 141 | constexpr bool IsValidInstructionSet(InstructionSet isa) { |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 142 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 143 | case InstructionSet::kArm: |
| 144 | case InstructionSet::kThumb2: |
| 145 | case InstructionSet::kArm64: |
| 146 | case InstructionSet::kX86: |
| 147 | case InstructionSet::kX86_64: |
| 148 | case InstructionSet::kMips: |
| 149 | case InstructionSet::kMips64: |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 150 | return true; |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 151 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 152 | case InstructionSet::kNone: |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 153 | return false; |
| 154 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 155 | return false; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 158 | size_t GetInstructionSetAlignment(InstructionSet isa); |
| 159 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 160 | constexpr bool Is64BitInstructionSet(InstructionSet isa) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 161 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 162 | case InstructionSet::kArm: |
| 163 | case InstructionSet::kThumb2: |
| 164 | case InstructionSet::kX86: |
| 165 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 166 | return false; |
| 167 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 168 | case InstructionSet::kArm64: |
| 169 | case InstructionSet::kX86_64: |
| 170 | case InstructionSet::kMips64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 171 | return true; |
| 172 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 173 | case InstructionSet::kNone: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 174 | break; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 175 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 176 | InstructionSetAbort(isa); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 179 | constexpr PointerSize InstructionSetPointerSize(InstructionSet isa) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 180 | return Is64BitInstructionSet(isa) ? PointerSize::k64 : PointerSize::k32; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 183 | constexpr size_t GetBytesPerGprSpillLocation(InstructionSet isa) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 184 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 185 | case InstructionSet::kArm: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 186 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 187 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 188 | return 4; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 189 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 190 | return 8; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 191 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 192 | return 4; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 193 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 194 | return 8; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 195 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 196 | return 4; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 197 | case InstructionSet::kMips64: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 198 | return 8; |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 199 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 200 | case InstructionSet::kNone: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 201 | break; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 202 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 203 | InstructionSetAbort(isa); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 206 | constexpr size_t GetBytesPerFprSpillLocation(InstructionSet isa) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 207 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 208 | case InstructionSet::kArm: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 209 | // Fall-through. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 210 | case InstructionSet::kThumb2: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 211 | return 4; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 212 | case InstructionSet::kArm64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 213 | return 8; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 214 | case InstructionSet::kX86: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 215 | return 8; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 216 | case InstructionSet::kX86_64: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 217 | return 8; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 218 | case InstructionSet::kMips: |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 219 | return 4; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 220 | case InstructionSet::kMips64: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 221 | return 8; |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 222 | |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 223 | case InstructionSet::kNone: |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 224 | break; |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 225 | } |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 226 | InstructionSetAbort(isa); |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Andreas Gampe | a9b2f7b | 2018-11-01 11:10:03 -0700 | [diff] [blame] | 229 | namespace instruction_set_details { |
| 230 | |
| 231 | #if !defined(ART_STACK_OVERFLOW_GAP_arm) || !defined(ART_STACK_OVERFLOW_GAP_arm64) || \ |
| 232 | !defined(ART_STACK_OVERFLOW_GAP_mips) || !defined(ART_STACK_OVERFLOW_GAP_mips64) || \ |
| 233 | !defined(ART_STACK_OVERFLOW_GAP_x86) || !defined(ART_STACK_OVERFLOW_GAP_x86_64) |
| 234 | #error "Missing defines for stack overflow gap" |
| 235 | #endif |
| 236 | |
| 237 | static constexpr size_t kArmStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm; |
| 238 | static constexpr size_t kArm64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_arm64; |
| 239 | static constexpr size_t kMipsStackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips; |
| 240 | static constexpr size_t kMips64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_mips64; |
| 241 | static constexpr size_t kX86StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86; |
| 242 | static constexpr size_t kX86_64StackOverflowReservedBytes = ART_STACK_OVERFLOW_GAP_x86_64; |
| 243 | |
| 244 | NO_RETURN void GetStackOverflowReservedBytesFailure(const char* error_msg); |
| 245 | |
| 246 | } // namespace instruction_set_details |
| 247 | |
| 248 | ALWAYS_INLINE |
| 249 | constexpr size_t GetStackOverflowReservedBytes(InstructionSet isa) { |
| 250 | switch (isa) { |
| 251 | case InstructionSet::kArm: // Intentional fall-through. |
| 252 | case InstructionSet::kThumb2: |
| 253 | return instruction_set_details::kArmStackOverflowReservedBytes; |
| 254 | |
| 255 | case InstructionSet::kArm64: |
| 256 | return instruction_set_details::kArm64StackOverflowReservedBytes; |
| 257 | |
| 258 | case InstructionSet::kMips: |
| 259 | return instruction_set_details::kMipsStackOverflowReservedBytes; |
| 260 | |
| 261 | case InstructionSet::kMips64: |
| 262 | return instruction_set_details::kMips64StackOverflowReservedBytes; |
| 263 | |
| 264 | case InstructionSet::kX86: |
| 265 | return instruction_set_details::kX86StackOverflowReservedBytes; |
| 266 | |
| 267 | case InstructionSet::kX86_64: |
| 268 | return instruction_set_details::kX86_64StackOverflowReservedBytes; |
| 269 | |
| 270 | case InstructionSet::kNone: |
| 271 | instruction_set_details::GetStackOverflowReservedBytesFailure( |
| 272 | "kNone has no stack overflow size"); |
| 273 | } |
| 274 | instruction_set_details::GetStackOverflowReservedBytesFailure("Unknown instruction set"); |
| 275 | } |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 276 | |
| 277 | // The following definitions create return types for two word-sized entities that will be passed |
| 278 | // in registers so that memory operations for the interface trampolines can be avoided. The entities |
| 279 | // are the resolved method and the pointer to the code to be invoked. |
| 280 | // |
| 281 | // On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be |
| 282 | // uint64_t or long long int. |
| 283 | // |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 284 | // On x86_64, ARM64 and MIPS64, structs are decomposed for allocation, so we can create a structs of |
| 285 | // two size_t-sized values. |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 286 | // |
| 287 | // We need two operations: |
| 288 | // |
| 289 | // 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0". |
| 290 | // GetTwoWordFailureValue() will return a value that has lower part == 0. |
| 291 | // |
| 292 | // 2) A value that combines two word-sized values. |
| 293 | // GetTwoWordSuccessValue() constructs this. |
| 294 | // |
| 295 | // IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure |
| 296 | // that the object does not move or the value is updated. Simple use of this is NOT SAFE |
| 297 | // when the garbage collector can move objects concurrently. Ensure that required locks |
| 298 | // are held when using! |
| 299 | |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 300 | #if defined(__i386__) || defined(__arm__) || (defined(__mips__) && !defined(__LP64__)) |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 301 | typedef uint64_t TwoWordReturn; |
| 302 | |
| 303 | // Encodes method_ptr==nullptr and code_ptr==nullptr |
| 304 | static inline constexpr TwoWordReturn GetTwoWordFailureValue() { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | // Use the lower 32b for the method pointer and the upper 32b for the code pointer. |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 309 | static inline constexpr TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 310 | static_assert(sizeof(uint32_t) == sizeof(uintptr_t), "Unexpected size difference"); |
| 311 | uint32_t lo32 = lo; |
| 312 | uint64_t hi64 = static_cast<uint64_t>(hi); |
| 313 | return ((hi64 << 32) | lo32); |
| 314 | } |
| 315 | |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 316 | #elif defined(__x86_64__) || defined(__aarch64__) || (defined(__mips__) && defined(__LP64__)) |
Andreas Gampe | 705543e | 2017-05-24 13:20:49 -0700 | [diff] [blame] | 317 | |
| 318 | // Note: TwoWordReturn can't be constexpr for 64-bit targets. We'd need a constexpr constructor, |
| 319 | // which would violate C-linkage in the entrypoint functions. |
| 320 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 321 | struct TwoWordReturn { |
| 322 | uintptr_t lo; |
| 323 | uintptr_t hi; |
| 324 | }; |
| 325 | |
| 326 | // Encodes method_ptr==nullptr. Leaves random value in code pointer. |
| 327 | static inline TwoWordReturn GetTwoWordFailureValue() { |
| 328 | TwoWordReturn ret; |
| 329 | ret.lo = 0; |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | // Write values into their respective members. |
| 334 | static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
| 335 | TwoWordReturn ret; |
| 336 | ret.lo = lo; |
| 337 | ret.hi = hi; |
| 338 | return ret; |
| 339 | } |
| 340 | #else |
| 341 | #error "Unsupported architecture" |
| 342 | #endif |
| 343 | |
| 344 | } // namespace art |
| 345 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 346 | #endif // ART_LIBARTBASE_ARCH_INSTRUCTION_SET_H_ |