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