| OLD | NEW |
| 1 //===- subzero/src/assembler.cpp - Assembler base class -------------------===// | 1 //===- subzero/src/assembler.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 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // The Subzero Code Generator | 10 // The Subzero Code Generator |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "IceGlobalContext.h" | 22 #include "IceGlobalContext.h" |
| 23 #include "IceOperand.h" | 23 #include "IceOperand.h" |
| 24 | 24 |
| 25 namespace Ice { | 25 namespace Ice { |
| 26 | 26 |
| 27 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { | 27 static uintptr_t NewContents(Assembler &assembler, intptr_t capacity) { |
| 28 uintptr_t result = assembler.AllocateBytes(capacity); | 28 uintptr_t result = assembler.AllocateBytes(capacity); |
| 29 return result; | 29 return result; |
| 30 } | 30 } |
| 31 | 31 |
| 32 AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, |
| 33 const Constant *Value) { |
| 34 AssemblerFixup *F = |
| 35 new (assembler_.Allocate<AssemblerFixup>()) AssemblerFixup(); |
| 36 F->set_position(0); |
| 37 F->set_kind(Kind); |
| 38 F->set_value(Value); |
| 39 fixups_.push_back(F); |
| 40 return F; |
| 41 } |
| 42 |
| 32 #ifndef NDEBUG | 43 #ifndef NDEBUG |
| 33 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { | 44 AssemblerBuffer::EnsureCapacity::EnsureCapacity(AssemblerBuffer *buffer) { |
| 34 if (buffer->cursor() >= buffer->limit()) | 45 if (buffer->cursor() >= buffer->limit()) |
| 35 buffer->ExtendCapacity(); | 46 buffer->ExtendCapacity(); |
| 36 // In debug mode, we save the assembler buffer along with the gap | 47 // In debug mode, we save the assembler buffer along with the gap |
| 37 // size before we start emitting to the buffer. This allows us to | 48 // size before we start emitting to the buffer. This allows us to |
| 38 // check that any single generated instruction doesn't overflow the | 49 // check that any single generated instruction doesn't overflow the |
| 39 // limit implied by the minimum gap size. | 50 // limit implied by the minimum gap size. |
| 40 buffer_ = buffer; | 51 buffer_ = buffer; |
| 41 gap_ = ComputeGap(); | 52 gap_ = ComputeGap(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 58 #endif // !NDEBUG | 69 #endif // !NDEBUG |
| 59 | 70 |
| 60 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { | 71 AssemblerBuffer::AssemblerBuffer(Assembler &assembler) : assembler_(assembler) { |
| 61 const intptr_t OneKB = 1024; | 72 const intptr_t OneKB = 1024; |
| 62 static const intptr_t kInitialBufferCapacity = 4 * OneKB; | 73 static const intptr_t kInitialBufferCapacity = 4 * OneKB; |
| 63 contents_ = NewContents(assembler_, kInitialBufferCapacity); | 74 contents_ = NewContents(assembler_, kInitialBufferCapacity); |
| 64 cursor_ = contents_; | 75 cursor_ = contents_; |
| 65 limit_ = ComputeLimit(contents_, kInitialBufferCapacity); | 76 limit_ = ComputeLimit(contents_, kInitialBufferCapacity); |
| 66 #ifndef NDEBUG | 77 #ifndef NDEBUG |
| 67 has_ensured_capacity_ = false; | 78 has_ensured_capacity_ = false; |
| 68 fixups_processed_ = false; | |
| 69 #endif // !NDEBUG | 79 #endif // !NDEBUG |
| 70 | 80 |
| 71 // Verify internal state. | 81 // Verify internal state. |
| 72 assert(Capacity() == kInitialBufferCapacity); | 82 assert(Capacity() == kInitialBufferCapacity); |
| 73 assert(Size() == 0); | 83 assert(Size() == 0); |
| 74 } | 84 } |
| 75 | 85 |
| 76 AssemblerBuffer::~AssemblerBuffer() {} | 86 AssemblerBuffer::~AssemblerBuffer() {} |
| 77 | 87 |
| 78 void AssemblerBuffer::ExtendCapacity() { | 88 void AssemblerBuffer::ExtendCapacity() { |
| 79 intptr_t old_size = Size(); | 89 intptr_t old_size = Size(); |
| 80 intptr_t old_capacity = Capacity(); | 90 intptr_t old_capacity = Capacity(); |
| 81 const intptr_t OneMB = 1 << 20; | 91 const intptr_t OneMB = 1 << 20; |
| 82 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); | 92 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
| 83 if (new_capacity < old_capacity) { | 93 if (new_capacity < old_capacity) { |
| 84 // FATAL | 94 llvm::report_fatal_error( |
| 85 llvm_unreachable("Unexpected overflow in AssemblerBuffer::ExtendCapacity"); | 95 "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
| 86 } | 96 } |
| 87 | 97 |
| 88 // Allocate the new data area and copy contents of the old one to it. | 98 // Allocate the new data area and copy contents of the old one to it. |
| 89 uintptr_t new_contents = NewContents(assembler_, new_capacity); | 99 uintptr_t new_contents = NewContents(assembler_, new_capacity); |
| 90 memmove(reinterpret_cast<void *>(new_contents), | 100 memmove(reinterpret_cast<void *>(new_contents), |
| 91 reinterpret_cast<void *>(contents_), old_size); | 101 reinterpret_cast<void *>(contents_), old_size); |
| 92 | 102 |
| 93 // Compute the relocation delta and switch to the new contents area. | 103 // Compute the relocation delta and switch to the new contents area. |
| 94 intptr_t delta = new_contents - contents_; | 104 intptr_t delta = new_contents - contents_; |
| 95 contents_ = new_contents; | 105 contents_ = new_contents; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 106 llvm::StringRef Assembler::getBufferView() const { | 116 llvm::StringRef Assembler::getBufferView() const { |
| 107 return llvm::StringRef(reinterpret_cast<const char *>(buffer_.contents()), | 117 return llvm::StringRef(reinterpret_cast<const char *>(buffer_.contents()), |
| 108 buffer_.Size()); | 118 buffer_.Size()); |
| 109 } | 119 } |
| 110 | 120 |
| 111 void Assembler::emitIASBytes(GlobalContext *Ctx) const { | 121 void Assembler::emitIASBytes(GlobalContext *Ctx) const { |
| 112 Ostream &Str = Ctx->getStrEmit(); | 122 Ostream &Str = Ctx->getStrEmit(); |
| 113 intptr_t EndPosition = buffer_.Size(); | 123 intptr_t EndPosition = buffer_.Size(); |
| 114 intptr_t CurPosition = 0; | 124 intptr_t CurPosition = 0; |
| 115 const intptr_t FixupSize = 4; | 125 const intptr_t FixupSize = 4; |
| 116 for (AssemblerBuffer::FixupList::const_iterator | 126 for (const AssemblerFixup *NextFixup : fixups()) { |
| 117 FixupI = buffer_.fixups_begin(), | |
| 118 FixupE = buffer_.fixups_end(); FixupI != FixupE; ++FixupI) { | |
| 119 AssemblerFixup *NextFixup = *FixupI; | |
| 120 intptr_t NextFixupLoc = NextFixup->position(); | 127 intptr_t NextFixupLoc = NextFixup->position(); |
| 121 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { | 128 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { |
| 122 Str << "\t.byte 0x"; | 129 Str << "\t.byte 0x"; |
| 123 Str.write_hex(buffer_.Load<uint8_t>(i)); | 130 Str.write_hex(buffer_.Load<uint8_t>(i)); |
| 124 Str << "\n"; | 131 Str << "\n"; |
| 125 } | 132 } |
| 126 Str << "\t.long "; | 133 Str << "\t.long "; |
| 127 NextFixup->emit(Ctx); | 134 NextFixup->emit(Ctx); |
| 128 bool IsPCRel = NextFixup->kind() == FK_PcRel_4; | 135 if (fixupIsPCRel(NextFixup->kind())) |
| 129 if (IsPCRel) | |
| 130 Str << " - (. + " << FixupSize << ")"; | 136 Str << " - (. + " << FixupSize << ")"; |
| 131 Str << "\n"; | 137 Str << "\n"; |
| 132 CurPosition = NextFixupLoc + FixupSize; | 138 CurPosition = NextFixupLoc + FixupSize; |
| 133 assert(CurPosition <= EndPosition); | 139 assert(CurPosition <= EndPosition); |
| 134 } | 140 } |
| 135 // Handle any bytes that are not prefixed by a fixup. | 141 // Handle any bytes that are not prefixed by a fixup. |
| 136 for (intptr_t i = CurPosition; i < EndPosition; ++i) { | 142 for (intptr_t i = CurPosition; i < EndPosition; ++i) { |
| 137 Str << "\t.byte 0x"; | 143 Str << "\t.byte 0x"; |
| 138 Str.write_hex(buffer_.Load<uint8_t>(i)); | 144 Str.write_hex(buffer_.Load<uint8_t>(i)); |
| 139 Str << "\n"; | 145 Str << "\n"; |
| 140 } | 146 } |
| 141 } | 147 } |
| 142 | 148 |
| 143 RelocOffsetT AssemblerFixup::offset() const { | |
| 144 if (const auto CR = llvm::dyn_cast<ConstantRelocatable>(value_)) | |
| 145 return CR->getOffset(); | |
| 146 return 0; | |
| 147 } | |
| 148 | |
| 149 IceString AssemblerFixup::symbol(GlobalContext *Ctx) const { | |
| 150 std::string Buffer; | |
| 151 llvm::raw_string_ostream Str(Buffer); | |
| 152 const Constant *C = value_; | |
| 153 if (const auto CR = llvm::dyn_cast<ConstantRelocatable>(C)) { | |
| 154 if (CR->getSuppressMangling()) | |
| 155 Str << CR->getName(); | |
| 156 else | |
| 157 Str << Ctx->mangleName(CR->getName()); | |
| 158 } else { | |
| 159 assert(llvm::isa<ConstantFloat>(C) || llvm::isa<ConstantDouble>(C)); | |
| 160 C->emitPoolLabel(Str); | |
| 161 } | |
| 162 return Str.str(); | |
| 163 } | |
| 164 | |
| 165 void AssemblerFixup::emit(GlobalContext *Ctx) const { | |
| 166 Ostream &Str = Ctx->getStrEmit(); | |
| 167 Str << symbol(Ctx); | |
| 168 RelocOffsetT Offset = offset(); | |
| 169 if (Offset) | |
| 170 Str << " + " << Offset; | |
| 171 } | |
| 172 | |
| 173 } // end of namespace Ice | 149 } // end of namespace Ice |
| OLD | NEW |