Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 3 | #include "assembler.h" |
| 4 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | #include <vector> |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 7 | |
| 8 | #include "assembler_arm.h" |
| 9 | #include "assembler_x86.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "globals.h" |
| 11 | #include "memory_region.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 12 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 13 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 14 | |
| 15 | static byte* NewContents(size_t capacity) { |
| 16 | byte* result = new byte[capacity]; |
| 17 | #if defined(DEBUG) |
| 18 | // Initialize the buffer with kBreakPointInstruction to force a break |
| 19 | // point if we ever execute an uninitialized part of the code buffer. |
| 20 | Assembler::InitializeMemoryWithBreakpoints(result, capacity); |
| 21 | #endif |
| 22 | return result; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | #if defined(DEBUG) |
| 27 | AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer* buffer) { |
| 28 | if (buffer->cursor() >= buffer->limit()) buffer->ExtendCapacity(); |
| 29 | // In debug mode, we save the assembler buffer along with the gap |
| 30 | // size before we start emitting to the buffer. This allows us to |
| 31 | // check that any single generated instruction doesn't overflow the |
| 32 | // limit implied by the minimum gap size. |
| 33 | buffer_ = buffer; |
| 34 | gap_ = ComputeGap(); |
| 35 | // Make sure that extending the capacity leaves a big enough gap |
| 36 | // for any kind of instruction. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 37 | CHECK_GE(gap_, kMinimumGap); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 38 | // Mark the buffer as having ensured the capacity. |
| 39 | CHECK(!buffer->HasEnsuredCapacity()); // Cannot nest. |
| 40 | buffer->has_ensured_capacity_ = true; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
| 45 | // Unmark the buffer, so we cannot emit after this. |
| 46 | buffer_->has_ensured_capacity_ = false; |
| 47 | // Make sure the generated instruction doesn't take up more |
| 48 | // space than the minimum gap. |
| 49 | int delta = gap_ - ComputeGap(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 50 | CHECK_LE(delta, kMinimumGap); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | } |
| 52 | #endif |
| 53 | |
| 54 | |
| 55 | AssemblerBuffer::AssemblerBuffer() { |
| 56 | static const size_t kInitialBufferCapacity = 4 * KB; |
| 57 | contents_ = NewContents(kInitialBufferCapacity); |
| 58 | cursor_ = contents_; |
| 59 | limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
| 60 | fixup_ = NULL; |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 61 | slow_path_ = NULL; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 62 | #if defined(DEBUG) |
| 63 | has_ensured_capacity_ = false; |
| 64 | fixups_processed_ = false; |
| 65 | #endif |
| 66 | |
| 67 | // Verify internal state. |
| 68 | CHECK_EQ(Capacity(), kInitialBufferCapacity); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 69 | CHECK_EQ(Size(), 0U); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | AssemblerBuffer::~AssemblerBuffer() { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 74 | delete[] contents_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | void AssemblerBuffer::ProcessFixups(const MemoryRegion& region) { |
| 79 | AssemblerFixup* fixup = fixup_; |
| 80 | while (fixup != NULL) { |
| 81 | fixup->Process(region, fixup->position()); |
| 82 | fixup = fixup->previous(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) { |
| 88 | // Copy the instructions from the buffer. |
| 89 | MemoryRegion from(reinterpret_cast<void*>(contents()), Size()); |
| 90 | instructions.CopyFrom(0, from); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 91 | // Process fixups in the instructions. |
| 92 | ProcessFixups(instructions); |
| 93 | #if defined(DEBUG) |
| 94 | fixups_processed_ = true; |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void AssemblerBuffer::ExtendCapacity() { |
| 100 | size_t old_size = Size(); |
| 101 | size_t old_capacity = Capacity(); |
| 102 | size_t new_capacity = std::min(old_capacity * 2, old_capacity + 1 * MB); |
| 103 | |
| 104 | // Allocate the new data area and copy contents of the old one to it. |
| 105 | byte* new_contents = NewContents(new_capacity); |
| 106 | memmove(reinterpret_cast<void*>(new_contents), |
| 107 | reinterpret_cast<void*>(contents_), |
| 108 | old_size); |
| 109 | |
| 110 | // Compute the relocation delta and switch to the new contents area. |
| 111 | ptrdiff_t delta = new_contents - contents_; |
| 112 | contents_ = new_contents; |
| 113 | |
| 114 | // Update the cursor and recompute the limit. |
| 115 | cursor_ += delta; |
| 116 | limit_ = ComputeLimit(new_contents, new_capacity); |
| 117 | |
| 118 | // Verify internal state. |
| 119 | CHECK_EQ(Capacity(), new_capacity); |
| 120 | CHECK_EQ(Size(), old_size); |
| 121 | } |
| 122 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 123 | |
| 124 | Assembler* Assembler::Create(InstructionSet instruction_set) { |
| 125 | if (instruction_set == kX86) { |
| 126 | return new x86::X86Assembler(); |
| 127 | } else { |
| 128 | CHECK(instruction_set == kArm || instruction_set == kThumb2); |
| 129 | return new arm::ArmAssembler(); |
| 130 | } |
| 131 | } |
| 132 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 133 | } // namespace art |