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 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 22 | #include "arm/assembler_arm.h" |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 23 | #include "arm64/assembler_arm64.h" |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 24 | #include "mips/assembler_mips.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | #include "x86/assembler_x86.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "globals.h" |
| 27 | #include "memory_region.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 28 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 29 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 30 | |
| 31 | static byte* NewContents(size_t capacity) { |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 32 | return new byte[capacity]; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 36 | AssemblerBuffer::AssemblerBuffer() { |
| 37 | static const size_t kInitialBufferCapacity = 4 * KB; |
| 38 | contents_ = NewContents(kInitialBufferCapacity); |
| 39 | cursor_ = contents_; |
| 40 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
| 41 | fixup_ = NULL; |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 42 | slow_path_ = NULL; |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 43 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 44 | has_ensured_capacity_ = false; |
| 45 | fixups_processed_ = false; |
| 46 | #endif |
| 47 | |
| 48 | // Verify internal state. |
| 49 | CHECK_EQ(Capacity(), kInitialBufferCapacity); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 50 | CHECK_EQ(Size(), 0U); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | AssemblerBuffer::~AssemblerBuffer() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 55 | delete[] contents_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
| 59 | void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) { |
| 60 | AssemblerFixup* fixup = fixup_; |
| 61 | while (fixup != NULL) { |
| 62 | fixup->Process(region, fixup->position()); |
| 63 | fixup = fixup->previous(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) { |
| 69 | // Copy the instructions from the buffer. |
| 70 | MemoryRegion from(reinterpret_cast<void*>(contents()), Size()); |
| 71 | instructions.CopyFrom(0, from); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 72 | // Process fixups in the instructions. |
| 73 | ProcessFixups(instructions); |
Elliott Hughes | 31f1f4f | 2012-03-12 13:57:36 -0700 | [diff] [blame] | 74 | #ifndef NDEBUG |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 75 | fixups_processed_ = true; |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void AssemblerBuffer::ExtendCapacity() { |
| 81 | size_t old_size = Size(); |
| 82 | size_t old_capacity = Capacity(); |
| 83 | size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB); |
| 84 | |
| 85 | // Allocate the new data area and copy contents of the old one to it. |
| 86 | byte* new_contents = NewContents(new_capacity); |
| 87 | memmove(reinterpret_cast<void*>(new_contents), |
| 88 | reinterpret_cast<void*>(contents_), |
| 89 | old_size); |
| 90 | |
| 91 | // Compute the relocation delta and switch to the new contents area. |
| 92 | ptrdiff_t delta = new_contents - contents_; |
| 93 | contents_ = new_contents; |
| 94 | |
| 95 | // Update the cursor and recompute the limit. |
| 96 | cursor_ += delta; |
| 97 | limit_ = ComputeLimit(new_contents, new_capacity); |
| 98 | |
| 99 | // Verify internal state. |
| 100 | CHECK_EQ(Capacity(), new_capacity); |
| 101 | CHECK_EQ(Size(), old_size); |
| 102 | } |
| 103 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 104 | |
| 105 | Assembler* Assembler::Create(InstructionSet instruction_set) { |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 106 | switch (instruction_set) { |
| 107 | case kArm: |
| 108 | case kThumb2: |
| 109 | return new arm::ArmAssembler(); |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 110 | case kArm64: |
| 111 | return new arm64::Arm64Assembler(); |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 112 | case kMips: |
| 113 | return new mips::MipsAssembler(); |
Ian Rogers | befbd57 | 2014-03-06 01:13:39 -0800 | [diff] [blame] | 114 | case kX86: // Fall-through. |
| 115 | case kX86_64: |
jeffhao | 7fbee07 | 2012-08-24 17:56:54 -0700 | [diff] [blame] | 116 | return new x86::X86Assembler(); |
| 117 | default: |
| 118 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 119 | return NULL; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 123 | } // namespace art |