Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -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 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 17 | #include "assembler.h" |
| 18 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | #include <vector> |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 21 | |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 22 | #ifdef ART_ENABLE_CODEGEN_arm |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 23 | #include "arm/assembler_arm32.h" |
| 24 | #include "arm/assembler_thumb2.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 25 | #endif |
| 26 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 27 | #include "arm64/assembler_arm64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 28 | #endif |
| 29 | #ifdef ART_ENABLE_CODEGEN_mips |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 30 | #include "mips/assembler_mips.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 33 | #include "mips64/assembler_mips64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 34 | #endif |
| 35 | #ifdef ART_ENABLE_CODEGEN_x86 |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 36 | #include "x86/assembler_x86.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 37 | #endif |
| 38 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 39 | #include "x86_64/assembler_x86_64.h" |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 40 | #endif |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 41 | #include "base/casts.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 42 | #include "globals.h" |
| 43 | #include "memory_region.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 44 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 45 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 46 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 47 | static uint8_t* NewContents(size_t capacity) { |
| 48 | return new uint8_t[capacity]; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 52 | AssemblerBuffer::AssemblerBuffer() { |
| 53 | static const size_t kInitialBufferCapacity = 4 * KB; |
| 54 | contents_ = NewContents(kInitialBufferCapacity); |
| 55 | cursor_ = contents_; |
| 56 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | fixup_ = nullptr; |
| 58 | slow_path_ = nullptr; |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 59 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 60 | has_ensured_capacity_ = false; |
| 61 | fixups_processed_ = false; |
| 62 | #endif |
| 63 | |
| 64 | // Verify internal state. |
| 65 | CHECK_EQ(Capacity(), kInitialBufferCapacity); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 66 | CHECK_EQ(Size(), 0U); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | AssemblerBuffer::~AssemblerBuffer() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 71 | delete[] contents_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
| 75 | void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) { |
| 76 | AssemblerFixup* fixup = fixup_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 77 | while (fixup != nullptr) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 78 | fixup->Process(region, fixup->position()); |
| 79 | fixup = fixup->previous(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) { |
| 85 | // Copy the instructions from the buffer. |
| 86 | MemoryRegion from(reinterpret_cast<void*>(contents()), Size()); |
| 87 | instructions.CopyFrom(0, from); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 88 | // Process fixups in the instructions. |
| 89 | ProcessFixups(instructions); |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 90 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 91 | fixups_processed_ = true; |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 96 | void AssemblerBuffer::ExtendCapacity(size_t min_capacity) { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 97 | size_t old_size = Size(); |
| 98 | size_t old_capacity = Capacity(); |
| 99 | size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 100 | new_capacity = std::max(new_capacity, min_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 101 | |
| 102 | // Allocate the new data area and copy contents of the old one to it. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 103 | uint8_t* new_contents = NewContents(new_capacity); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 104 | memmove(reinterpret_cast<void*>(new_contents), |
| 105 | reinterpret_cast<void*>(contents_), |
| 106 | old_size); |
| 107 | |
| 108 | // Compute the relocation delta and switch to the new contents area. |
| 109 | ptrdiff_t delta = new_contents - contents_; |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 110 | delete[] contents_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 111 | contents_ = new_contents; |
| 112 | |
| 113 | // Update the cursor and recompute the limit. |
| 114 | cursor_ += delta; |
| 115 | limit_ = ComputeLimit(new_contents, new_capacity); |
| 116 | |
| 117 | // Verify internal state. |
| 118 | CHECK_EQ(Capacity(), new_capacity); |
| 119 | CHECK_EQ(Size(), old_size); |
| 120 | } |
| 121 | |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 122 | void DebugFrameOpCodeWriterForAssembler::ImplicitlyAdvancePC() { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 123 | uint32_t pc = dchecked_integral_cast<uint32_t>(assembler_->CodeSize()); |
| 124 | if (delay_emitting_advance_pc_) { |
| 125 | uint32_t stream_pos = dchecked_integral_cast<uint32_t>(opcodes_.size()); |
| 126 | delayed_advance_pcs_.push_back(DelayedAdvancePC {stream_pos, pc}); |
| 127 | } else { |
| 128 | AdvancePC(pc); |
| 129 | } |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 130 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 131 | |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 132 | Assembler* Assembler::Create(InstructionSet instruction_set, |
| 133 | const InstructionSetFeatures* instruction_set_features) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 134 | switch (instruction_set) { |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 135 | #ifdef ART_ENABLE_CODEGEN_arm |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 136 | case kArm: |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 137 | return new arm::Arm32Assembler(); |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 138 | case kThumb2: |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 139 | return new arm::Thumb2Assembler(); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 140 | #endif |
| 141 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 142 | case kArm64: |
| 143 | return new arm64::Arm64Assembler(); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 144 | #endif |
| 145 | #ifdef ART_ENABLE_CODEGEN_mips |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 146 | case kMips: |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 147 | return new mips::MipsAssembler(instruction_set_features != nullptr |
| 148 | ? instruction_set_features->AsMipsInstructionSetFeatures() |
| 149 | : nullptr); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 150 | #endif |
| 151 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 152 | case kMips64: |
| 153 | return new mips64::Mips64Assembler(); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 154 | #endif |
| 155 | #ifdef ART_ENABLE_CODEGEN_x86 |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 156 | case kX86: |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 157 | return new x86::X86Assembler(); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 158 | #endif |
| 159 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 160 | case kX86_64: |
| 161 | return new x86_64::X86_64Assembler(); |
Alex Light | 50fa993 | 2015-08-10 15:30:07 -0700 | [diff] [blame] | 162 | #endif |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 163 | default: |
| 164 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 165 | return nullptr; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 169 | void Assembler::StoreImmediateToThread32(ThreadOffset<4> dest ATTRIBUTE_UNUSED, |
| 170 | uint32_t imm ATTRIBUTE_UNUSED, |
| 171 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 172 | UNIMPLEMENTED(FATAL); |
| 173 | } |
| 174 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 175 | void Assembler::StoreImmediateToThread64(ThreadOffset<8> dest ATTRIBUTE_UNUSED, |
| 176 | uint32_t imm ATTRIBUTE_UNUSED, |
| 177 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 178 | UNIMPLEMENTED(FATAL); |
| 179 | } |
| 180 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 181 | void Assembler::StoreStackOffsetToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED, |
| 182 | FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 183 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 184 | UNIMPLEMENTED(FATAL); |
| 185 | } |
| 186 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 187 | void Assembler::StoreStackOffsetToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED, |
| 188 | FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 189 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 190 | UNIMPLEMENTED(FATAL); |
| 191 | } |
| 192 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 193 | void Assembler::StoreStackPointerToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 194 | UNIMPLEMENTED(FATAL); |
| 195 | } |
| 196 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 197 | void Assembler::StoreStackPointerToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 198 | UNIMPLEMENTED(FATAL); |
| 199 | } |
| 200 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 201 | void Assembler::LoadFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED, |
| 202 | ThreadOffset<4> src ATTRIBUTE_UNUSED, |
| 203 | size_t size ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 204 | UNIMPLEMENTED(FATAL); |
| 205 | } |
| 206 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 207 | void Assembler::LoadFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED, |
| 208 | ThreadOffset<8> src ATTRIBUTE_UNUSED, |
| 209 | size_t size ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 210 | UNIMPLEMENTED(FATAL); |
| 211 | } |
| 212 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 213 | void Assembler::LoadRawPtrFromThread32(ManagedRegister dest ATTRIBUTE_UNUSED, |
| 214 | ThreadOffset<4> offs ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 215 | UNIMPLEMENTED(FATAL); |
| 216 | } |
| 217 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 218 | void Assembler::LoadRawPtrFromThread64(ManagedRegister dest ATTRIBUTE_UNUSED, |
| 219 | ThreadOffset<8> offs ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 220 | UNIMPLEMENTED(FATAL); |
| 221 | } |
| 222 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 223 | void Assembler::CopyRawPtrFromThread32(FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 224 | ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED, |
| 225 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 226 | UNIMPLEMENTED(FATAL); |
| 227 | } |
| 228 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 229 | void Assembler::CopyRawPtrFromThread64(FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 230 | ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED, |
| 231 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 232 | UNIMPLEMENTED(FATAL); |
| 233 | } |
| 234 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 235 | void Assembler::CopyRawPtrToThread32(ThreadOffset<4> thr_offs ATTRIBUTE_UNUSED, |
| 236 | FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 237 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 238 | UNIMPLEMENTED(FATAL); |
| 239 | } |
| 240 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 241 | void Assembler::CopyRawPtrToThread64(ThreadOffset<8> thr_offs ATTRIBUTE_UNUSED, |
| 242 | FrameOffset fr_offs ATTRIBUTE_UNUSED, |
| 243 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 244 | UNIMPLEMENTED(FATAL); |
| 245 | } |
| 246 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 247 | void Assembler::CallFromThread32(ThreadOffset<4> offset ATTRIBUTE_UNUSED, |
| 248 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 249 | UNIMPLEMENTED(FATAL); |
| 250 | } |
| 251 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 252 | void Assembler::CallFromThread64(ThreadOffset<8> offset ATTRIBUTE_UNUSED, |
| 253 | ManagedRegister scratch ATTRIBUTE_UNUSED) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 254 | UNIMPLEMENTED(FATAL); |
| 255 | } |
| 256 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 257 | } // namespace art |