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