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