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