| OLD | NEW |
| (Empty) |
| 1 //===- subzero/src/assembler.cpp - Assembler base class -------------------===// | |
| 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 3 // for details. All rights reserved. Use of this source code is governed by a | |
| 4 // BSD-style license that can be found in the LICENSE file. | |
| 5 // | |
| 6 // Modified by the Subzero authors. | |
| 7 // | |
| 8 // This is forked from Dart revision 39313. | |
| 9 // Please update the revision if we merge back changes from Dart. | |
| 10 // https://code.google.com/p/dart/wiki/GettingTheSource | |
| 11 // | |
| 12 //===----------------------------------------------------------------------===// | |
| 13 // | |
| 14 // The Subzero Code Generator | |
| 15 // | |
| 16 // This file is distributed under the University of Illinois Open Source | |
| 17 // License. See LICENSE.TXT for details. | |
| 18 // | |
| 19 //===----------------------------------------------------------------------===// | |
| 20 // | |
| 21 // This file implements the Assembler class. | |
| 22 // | |
| 23 //===----------------------------------------------------------------------===// | |
| 24 | |
| 25 #include "assembler.h" | |
| 26 #include "IceGlobalContext.h" | |
| 27 #include "IceOperand.h" | |
| 28 | |
| 29 namespace Ice { | |
| 30 | |
| 31 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { | |
| 32 uintptr_t result = assembler.AllocateBytes(capacity); | |
| 33 return result; | |
| 34 } | |
| 35 | |
| 36 AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, | |
| 37 const Constant *Value) { | |
| 38 AssemblerFixup *F = | |
| 39 new (assembler_.Allocate<AssemblerFixup>()) AssemblerFixup(); | |
| 40 F->set_position(0); | |
| 41 F->set_kind(Kind); | |
| 42 F->set_value(Value); | |
| 43 if (!assembler_.getPreliminary()) | |
| 44 fixups_.push_back(F); | |
| 45 return F; | |
| 46 } | |
| 47 | |
| 48 #ifndef NDEBUG | |
| 49 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { | |
| 50 if (buffer->cursor() >= buffer->limit()) | |
| 51 buffer->ExtendCapacity(); | |
| 52 // In debug mode, we save the assembler buffer along with the gap | |
| 53 // size before we start emitting to the buffer. This allows us to | |
| 54 // check that any single generated instruction doesn't overflow the | |
| 55 // limit implied by the minimum gap size. | |
| 56 buffer_ = buffer; | |
| 57 gap_ = ComputeGap(); | |
| 58 // Make sure that extending the capacity leaves a big enough gap | |
| 59 // for any kind of instruction. | |
| 60 assert(gap_ >= kMinimumGap); | |
| 61 // Mark the buffer as having ensured the capacity. | |
| 62 assert(!buffer->HasEnsuredCapacity()); // Cannot nest. | |
| 63 buffer->has_ensured_capacity_ = true; | |
| 64 } | |
| 65 | |
| 66 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { | |
| 67 // Unmark the buffer, so we cannot emit after this. | |
| 68 buffer_->has_ensured_capacity_ = false; | |
| 69 // Make sure the generated instruction doesn't take up more | |
| 70 // space than the minimum gap. | |
| 71 intptr_t delta = gap_ - ComputeGap(); | |
| 72 assert(delta <= kMinimumGap); | |
| 73 } | |
| 74 #endif // !NDEBUG | |
| 75 | |
| 76 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { | |
| 77 const intptr_t OneKB = 1024; | |
| 78 static const intptr_t kInitialBufferCapacity = 4 * OneKB; | |
| 79 contents_ = NewContents(assembler_, kInitialBufferCapacity); | |
| 80 cursor_ = contents_; | |
| 81 limit_ = ComputeLimit(contents_, kInitialBufferCapacity); | |
| 82 #ifndef NDEBUG | |
| 83 has_ensured_capacity_ = false; | |
| 84 #endif // !NDEBUG | |
| 85 | |
| 86 // Verify internal state. | |
| 87 assert(Capacity() == kInitialBufferCapacity); | |
| 88 assert(Size() == 0); | |
| 89 } | |
| 90 | |
| 91 AssemblerBuffer::~AssemblerBuffer() {} | |
| 92 | |
| 93 void AssemblerBuffer::ExtendCapacity() { | |
| 94 intptr_t old_size = Size(); | |
| 95 intptr_t old_capacity = Capacity(); | |
| 96 const intptr_t OneMB = 1 << 20; | |
| 97 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); | |
| 98 if (new_capacity < old_capacity) { | |
| 99 llvm::report_fatal_error( | |
| 100 "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); | |
| 101 } | |
| 102 | |
| 103 // Allocate the new data area and copy contents of the old one to it. | |
| 104 uintptr_t new_contents = NewContents(assembler_, new_capacity); | |
| 105 memmove(reinterpret_cast<void *>(new_contents), | |
| 106 reinterpret_cast<void *>(contents_), old_size); | |
| 107 | |
| 108 // Compute the relocation delta and switch to the new contents area. | |
| 109 intptr_t delta = new_contents - contents_; | |
| 110 contents_ = new_contents; | |
| 111 | |
| 112 // Update the cursor and recompute the limit. | |
| 113 cursor_ += delta; | |
| 114 limit_ = ComputeLimit(new_contents, new_capacity); | |
| 115 | |
| 116 // Verify internal state. | |
| 117 assert(Capacity() == new_capacity); | |
| 118 assert(Size() == old_size); | |
| 119 } | |
| 120 | |
| 121 llvm::StringRef Assembler::getBufferView() const { | |
| 122 return llvm::StringRef(reinterpret_cast<const char *>(buffer_.contents()), | |
| 123 buffer_.Size()); | |
| 124 } | |
| 125 | |
| 126 void Assembler::emitIASBytes(GlobalContext *Ctx) const { | |
| 127 Ostream &Str = Ctx->getStrEmit(); | |
| 128 intptr_t EndPosition = buffer_.Size(); | |
| 129 intptr_t CurPosition = 0; | |
| 130 const intptr_t FixupSize = 4; | |
| 131 for (const AssemblerFixup *NextFixup : fixups()) { | |
| 132 intptr_t NextFixupLoc = NextFixup->position(); | |
| 133 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { | |
| 134 Str << "\t.byte 0x"; | |
| 135 Str.write_hex(buffer_.Load<uint8_t>(i)); | |
| 136 Str << "\n"; | |
| 137 } | |
| 138 Str << "\t.long "; | |
| 139 NextFixup->emit(Ctx, buffer_.Load<RelocOffsetT>(NextFixupLoc)); | |
| 140 if (fixupIsPCRel(NextFixup->kind())) | |
| 141 Str << " - ."; | |
| 142 Str << "\n"; | |
| 143 CurPosition = NextFixupLoc + FixupSize; | |
| 144 assert(CurPosition <= EndPosition); | |
| 145 } | |
| 146 // Handle any bytes that are not prefixed by a fixup. | |
| 147 for (intptr_t i = CurPosition; i < EndPosition; ++i) { | |
| 148 Str << "\t.byte 0x"; | |
| 149 Str.write_hex(buffer_.Load<uint8_t>(i)); | |
| 150 Str << "\n"; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 } // end of namespace Ice | |
| OLD | NEW |