OLD | NEW |
1 //===- subzero/src/IceAssembler.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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "IceGlobalContext.h" | 28 #include "IceGlobalContext.h" |
29 #include "IceOperand.h" | 29 #include "IceOperand.h" |
30 | 30 |
31 namespace Ice { | 31 namespace Ice { |
32 | 32 |
33 static uintptr_t NewContents(Assembler &Assemblr, intptr_t Capacity) { | 33 static uintptr_t NewContents(Assembler &Assemblr, intptr_t Capacity) { |
34 uintptr_t Result = Assemblr.allocateBytes(Capacity); | 34 uintptr_t Result = Assemblr.allocateBytes(Capacity); |
35 return Result; | 35 return Result; |
36 } | 36 } |
37 | 37 |
| 38 void AssemblerBuffer::installFixup(AssemblerFixup *F) { |
| 39 F->set_position(0); |
| 40 if (!Assemblr.getPreliminary()) |
| 41 Fixups.push_back(F); |
| 42 } |
| 43 |
38 AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, | 44 AssemblerFixup *AssemblerBuffer::createFixup(FixupKind Kind, |
39 const Constant *Value) { | 45 const Constant *Value) { |
40 AssemblerFixup *F = | 46 AssemblerFixup *F = |
41 new (Assemblr.allocate<AssemblerFixup>()) AssemblerFixup(); | 47 new (Assemblr.allocate<AssemblerFixup>()) AssemblerFixup(); |
42 F->set_position(0); | |
43 F->set_kind(Kind); | 48 F->set_kind(Kind); |
44 F->set_value(Value); | 49 F->set_value(Value); |
45 if (!Assemblr.getPreliminary()) | 50 installFixup(F); |
46 Fixups.push_back(F); | |
47 return F; | 51 return F; |
48 } | 52 } |
49 | 53 |
| 54 AssemblerTextFixup *AssemblerBuffer::createTextFixup(const std::string &Text, |
| 55 size_t BytesUsed) { |
| 56 AssemblerTextFixup *F = new (Assemblr.allocate<AssemblerTextFixup>()) |
| 57 AssemblerTextFixup(Text, BytesUsed); |
| 58 installFixup(F); |
| 59 TextFixupNeeded = false; |
| 60 return F; |
| 61 } |
| 62 |
50 void AssemblerBuffer::EnsureCapacity::validate(AssemblerBuffer *buffer) { | 63 void AssemblerBuffer::EnsureCapacity::validate(AssemblerBuffer *buffer) { |
51 // In debug mode, we save the assembler buffer along with the gap size before | 64 // In debug mode, we save the assembler buffer along with the gap size before |
52 // we start emitting to the buffer. This allows us to check that any single | 65 // we start emitting to the buffer. This allows us to check that any single |
53 // generated instruction doesn't overflow the limit implied by the minimum | 66 // generated instruction doesn't overflow the limit implied by the minimum |
54 // gap size. | 67 // gap size. |
55 Gap = computeGap(); | 68 Gap = computeGap(); |
56 // Make sure that extending the capacity leaves a big enough gap for any kind | 69 // Make sure that extending the capacity leaves a big enough gap for any kind |
57 // of instruction. | 70 // of instruction. |
58 assert(Gap >= kMinimumGap); | 71 assert(Gap >= kMinimumGap); |
59 // Mark the buffer as having ensured the capacity. | 72 // Mark the buffer as having ensured the capacity. |
60 assert(!buffer->hasEnsuredCapacity()); // Cannot nest. | 73 assert(!buffer->hasEnsuredCapacity()); // Cannot nest. |
61 buffer->HasEnsuredCapacity = true; | 74 buffer->HasEnsuredCapacity = true; |
62 } | 75 } |
63 | 76 |
64 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { | 77 AssemblerBuffer::EnsureCapacity::~EnsureCapacity() { |
65 // Unmark the buffer, so we cannot emit after this. | 78 // Unmark the buffer, so we cannot emit after this. |
66 Buffer->HasEnsuredCapacity = false; | 79 Buffer->HasEnsuredCapacity = false; |
67 // Make sure the generated instruction doesn't take up more space than the | 80 // Make sure the generated instruction doesn't take up more space than the |
68 // minimum gap. | 81 // minimum gap. |
69 intptr_t delta = Gap - computeGap(); | 82 intptr_t delta = Gap - computeGap(); |
70 (void)delta; | 83 (void)delta; |
71 assert(delta <= kMinimumGap); | 84 assert(delta <= kMinimumGap); |
72 } | 85 } |
73 | 86 |
74 AssemblerBuffer::AssemblerBuffer(Assembler &Asm) : Assemblr(Asm) { | 87 AssemblerBuffer::AssemblerBuffer(Assembler &Asm) : Assemblr(Asm) { |
75 const intptr_t OneKB = 1024; | 88 constexpr intptr_t OneKB = 1024; |
76 static const intptr_t kInitialBufferCapacity = 4 * OneKB; | 89 static constexpr intptr_t kInitialBufferCapacity = 4 * OneKB; |
77 Contents = NewContents(Assemblr, kInitialBufferCapacity); | 90 Contents = NewContents(Assemblr, kInitialBufferCapacity); |
78 Cursor = Contents; | 91 Cursor = Contents; |
79 Limit = computeLimit(Contents, kInitialBufferCapacity); | 92 Limit = computeLimit(Contents, kInitialBufferCapacity); |
80 HasEnsuredCapacity = false; | 93 HasEnsuredCapacity = false; |
| 94 TextFixupNeeded = false; |
81 | 95 |
82 // Verify internal state. | 96 // Verify internal state. |
83 assert(capacity() == kInitialBufferCapacity); | 97 assert(capacity() == kInitialBufferCapacity); |
84 assert(size() == 0); | 98 assert(size() == 0); |
85 } | 99 } |
86 | 100 |
87 AssemblerBuffer::~AssemblerBuffer() = default; | 101 AssemblerBuffer::~AssemblerBuffer() = default; |
88 | 102 |
89 void AssemblerBuffer::extendCapacity() { | 103 void AssemblerBuffer::extendCapacity() { |
90 intptr_t old_size = size(); | 104 intptr_t old_size = size(); |
91 intptr_t old_capacity = capacity(); | 105 intptr_t old_capacity = capacity(); |
92 const intptr_t OneMB = 1 << 20; | 106 constexpr intptr_t OneMB = 1 << 20; |
93 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); | 107 intptr_t new_capacity = std::min(old_capacity * 2, old_capacity + OneMB); |
94 if (new_capacity < old_capacity) { | 108 if (new_capacity < old_capacity) { |
95 llvm::report_fatal_error( | 109 llvm::report_fatal_error( |
96 "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); | 110 "Unexpected overflow in AssemblerBuffer::ExtendCapacity"); |
97 } | 111 } |
98 | 112 |
99 // Allocate the new data area and copy contents of the old one to it. | 113 // Allocate the new data area and copy contents of the old one to it. |
100 uintptr_t new_contents = NewContents(Assemblr, new_capacity); | 114 uintptr_t new_contents = NewContents(Assemblr, new_capacity); |
101 memmove(reinterpret_cast<void *>(new_contents), | 115 memmove(reinterpret_cast<void *>(new_contents), |
102 reinterpret_cast<void *>(Contents), old_size); | 116 reinterpret_cast<void *>(Contents), old_size); |
(...skipping 13 matching lines...) Expand all Loading... |
116 | 130 |
117 llvm::StringRef Assembler::getBufferView() const { | 131 llvm::StringRef Assembler::getBufferView() const { |
118 return llvm::StringRef(reinterpret_cast<const char *>(Buffer.contents()), | 132 return llvm::StringRef(reinterpret_cast<const char *>(Buffer.contents()), |
119 Buffer.size()); | 133 Buffer.size()); |
120 } | 134 } |
121 | 135 |
122 void Assembler::emitIASBytes() const { | 136 void Assembler::emitIASBytes() const { |
123 Ostream &Str = Ctx->getStrEmit(); | 137 Ostream &Str = Ctx->getStrEmit(); |
124 intptr_t EndPosition = Buffer.size(); | 138 intptr_t EndPosition = Buffer.size(); |
125 intptr_t CurPosition = 0; | 139 intptr_t CurPosition = 0; |
126 const intptr_t FixupSize = 4; | |
127 for (const AssemblerFixup *NextFixup : fixups()) { | 140 for (const AssemblerFixup *NextFixup : fixups()) { |
128 intptr_t NextFixupLoc = NextFixup->position(); | 141 intptr_t NextFixupLoc = NextFixup->position(); |
129 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { | 142 for (intptr_t i = CurPosition; i < NextFixupLoc; ++i) { |
130 Str << "\t.byte 0x"; | 143 Str << "\t.byte 0x"; |
131 Str.write_hex(Buffer.load<uint8_t>(i)); | 144 Str.write_hex(Buffer.load<uint8_t>(i)); |
132 Str << "\n"; | 145 Str << "\n"; |
133 } | 146 } |
134 Str << "\t.long "; | |
135 // For PCRel fixups, we write the pc-offset from a symbol into the Buffer | 147 // For PCRel fixups, we write the pc-offset from a symbol into the Buffer |
136 // (e.g., -4), but we don't represent that in the fixup's offset. Otherwise | 148 // (e.g., -4), but we don't represent that in the fixup's offset. Otherwise |
137 // the fixup holds the true offset, and so does the Buffer. Just load the | 149 // the fixup holds the true offset, and so does the Buffer. Just load the |
138 // offset from the buffer. | 150 // offset from the buffer. |
139 NextFixup->emit(Ctx, Buffer.load<RelocOffsetT>(NextFixupLoc)); | 151 CurPosition = NextFixupLoc + |
140 if (fixupIsPCRel(NextFixup->kind())) | 152 NextFixup->emit(Ctx, Buffer.load<RelocOffsetT>(NextFixupLoc), |
141 Str << " - ."; | 153 fixupIsPCRel(NextFixup->kind())); |
142 Str << "\n"; | |
143 CurPosition = NextFixupLoc + FixupSize; | |
144 assert(CurPosition <= EndPosition); | 154 assert(CurPosition <= EndPosition); |
145 } | 155 } |
146 // Handle any bytes that are not prefixed by a fixup. | 156 // Handle any bytes that are not prefixed by a fixup. |
147 for (intptr_t i = CurPosition; i < EndPosition; ++i) { | 157 for (intptr_t i = CurPosition; i < EndPosition; ++i) { |
148 Str << "\t.byte 0x"; | 158 Str << "\t.byte 0x"; |
149 Str.write_hex(Buffer.load<uint8_t>(i)); | 159 Str.write_hex(Buffer.load<uint8_t>(i)); |
150 Str << "\n"; | 160 Str << "\n"; |
151 } | 161 } |
152 } | 162 } |
153 | 163 |
154 } // end of namespace Ice | 164 } // end of namespace Ice |
OLD | NEW |