@@ -46,79 +46,23 @@ MCAssembler *MCObjectStreamer::getAssemblerPtr() {
46
46
return nullptr ;
47
47
}
48
48
49
- constexpr size_t FragBlockSize = 16384 ;
50
- // Ensure the new fragment can at least store a few bytes.
51
- constexpr size_t NewFragHeadroom = 8 ;
52
-
53
- static_assert (NewFragHeadroom >= alignof (MCFragment));
54
- static_assert (FragBlockSize >= sizeof (MCFragment) + NewFragHeadroom);
55
-
56
- MCFragment *MCObjectStreamer::allocFragSpace (size_t Headroom) {
57
- auto Size = std::max (FragBlockSize, sizeof (MCFragment) + Headroom);
58
- FragSpace = Size - sizeof (MCFragment);
59
- auto Block = std::unique_ptr<uint8_t []>(new uint8_t [Size]);
60
- auto *F = reinterpret_cast <MCFragment *>(Block.get ());
61
- FragStorage.push_back (std::move (Block));
62
- return F;
63
- }
64
-
65
49
void MCObjectStreamer::newFragment () {
66
- MCFragment *F;
67
- if (LLVM_LIKELY (sizeof (MCFragment) + NewFragHeadroom <= FragSpace)) {
68
- auto End = reinterpret_cast <size_t >(getCurFragEnd ());
69
- F = reinterpret_cast <MCFragment *>(
70
- alignToPowerOf2 (End, alignof (MCFragment)));
71
- FragSpace -= size_t (F) - End + sizeof (MCFragment);
72
- } else {
73
- F = allocFragSpace (0 );
74
- }
75
- new (F) MCFragment ();
76
- addFragment (F);
77
- }
78
-
79
- void MCObjectStreamer::ensureHeadroom (size_t Headroom) {
80
- if (Headroom <= FragSpace)
81
- return ;
82
- auto *F = allocFragSpace (Headroom);
83
- new (F) MCFragment ();
84
- addFragment (F);
50
+ addFragment (allocFragment<MCFragment>());
85
51
}
86
52
87
53
void MCObjectStreamer::addSpecialFragment (MCFragment *Frag) {
88
54
assert (Frag->getKind () != MCFragment::FT_Data &&
89
- " F should have a variable-size tail" );
90
- // Frag is not connected to FragSpace. Before modifying CurFrag with
91
- // addFragment(Frag), allocate an empty fragment to maintain FragSpace
92
- // connectivity, potentially reusing CurFrag's associated space.
93
- MCFragment *F;
94
- if (LLVM_LIKELY (sizeof (MCFragment) + NewFragHeadroom <= FragSpace)) {
95
- auto End = reinterpret_cast <size_t >(getCurFragEnd ());
96
- F = reinterpret_cast <MCFragment *>(
97
- alignToPowerOf2 (End, alignof (MCFragment)));
98
- FragSpace -= size_t (F) - End + sizeof (MCFragment);
99
- } else {
100
- F = allocFragSpace (0 );
101
- }
102
- new (F) MCFragment ();
103
-
55
+ " Frag should have a variable-size tail" );
104
56
addFragment (Frag);
105
- addFragment (F );
57
+ newFragment ( );
106
58
}
107
59
108
60
void MCObjectStreamer::appendContents (ArrayRef<char > Contents) {
109
- ensureHeadroom (Contents.size ());
110
- assert (FragSpace >= Contents.size ());
111
- llvm::copy (Contents, getCurFragEnd ());
112
- CurFrag->FixedSize += Contents.size ();
113
- FragSpace -= Contents.size ();
61
+ CurFrag->appendContents (Contents);
114
62
}
115
63
116
- void MCObjectStreamer::appendContents (size_t Num, uint8_t Elt) {
117
- ensureHeadroom (Num);
118
- MutableArrayRef<uint8_t > Data (getCurFragEnd (), Num);
119
- llvm::fill (Data, Elt);
120
- CurFrag->FixedSize += Num;
121
- FragSpace -= Num;
64
+ void MCObjectStreamer::appendContents (size_t Num, char Elt) {
65
+ CurFrag->appendContents (Num, Elt);
122
66
}
123
67
124
68
void MCObjectStreamer::addFixup (const MCExpr *Value, MCFixupKind Kind) {
@@ -171,8 +115,6 @@ void MCObjectStreamer::reset() {
171
115
}
172
116
EmitEHFrame = true ;
173
117
EmitDebugFrame = false ;
174
- FragStorage.clear ();
175
- FragSpace = 0 ;
176
118
SpecialFragAllocator.Reset ();
177
119
MCStreamer::reset ();
178
120
}
@@ -202,6 +144,7 @@ void MCObjectStreamer::emitCFISections(bool EH, bool Debug, bool SFrame) {
202
144
void MCObjectStreamer::emitValueImpl (const MCExpr *Value, unsigned Size,
203
145
SMLoc Loc) {
204
146
MCStreamer::emitValueImpl (Value, Size, Loc);
147
+ MCFragment *DF = getCurrentFragment ();
205
148
206
149
MCDwarfLineEntry::make (this , getCurrentSectionOnly ());
207
150
@@ -216,9 +159,9 @@ void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
216
159
emitIntValue (AbsValue, Size);
217
160
return ;
218
161
}
219
- ensureHeadroom (Size);
220
- addFixup (Value, MCFixup::getDataKindForSize (Size));
221
- appendContents (Size, 0 );
162
+ DF-> addFixup ( MCFixup::create (DF-> getContents (). size (), Value,
163
+ MCFixup::getDataKindForSize (Size) ));
164
+ DF-> appendContents (Size, 0 );
222
165
}
223
166
224
167
MCSymbol *MCObjectStreamer::emitCFILabel () {
@@ -252,7 +195,7 @@ void MCObjectStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
252
195
// section.
253
196
MCFragment *F = CurFrag;
254
197
Symbol->setFragment (F);
255
- Symbol->setOffset (F->getFixedSize ());
198
+ Symbol->setOffset (F->getContents (). size ());
256
199
257
200
emitPendingAssignments (Symbol);
258
201
}
@@ -318,38 +261,20 @@ void MCObjectStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
318
261
F0 = CurFrag;
319
262
}
320
263
321
- // To maintain connectivity between CurFrag and FragSpace when CurFrag is
322
- // modified, allocate an empty fragment and append it to the fragment list.
323
- // (Subsections[I].second.Tail is not connected to FragSpace.)
324
- MCFragment *F;
325
- if (LLVM_LIKELY (sizeof (MCFragment) + NewFragHeadroom <= FragSpace)) {
326
- auto End = reinterpret_cast <size_t >(getCurFragEnd ());
327
- F = reinterpret_cast <MCFragment *>(
328
- alignToPowerOf2 (End, alignof (MCFragment)));
329
- FragSpace -= size_t (F) - End + sizeof (MCFragment);
330
- } else {
331
- F = allocFragSpace (0 );
332
- }
333
- new (F) MCFragment ();
334
- F->setParent (Section);
335
-
336
264
auto &Subsections = Section->Subsections ;
337
265
size_t I = 0 , E = Subsections.size ();
338
266
while (I != E && Subsections[I].first < Subsection)
339
267
++I;
340
268
// If the subsection number is not in the sorted Subsections list, create a
341
269
// new fragment list.
342
270
if (I == E || Subsections[I].first != Subsection) {
271
+ auto *F = allocFragment<MCFragment>();
272
+ F->setParent (Section);
343
273
Subsections.insert (Subsections.begin () + I,
344
274
{Subsection, MCSection::FragList{F, F}});
345
- Section->CurFragList = &Subsections[I].second ;
346
- CurFrag = F;
347
- } else {
348
- Section->CurFragList = &Subsections[I].second ;
349
- CurFrag = Subsections[I].second .Tail ;
350
- // Ensure CurFrag is associated with FragSpace.
351
- addFragment (F);
352
275
}
276
+ Section->CurFragList = &Subsections[I].second ;
277
+ CurFrag = Section->CurFragList ->Tail ;
353
278
354
279
// Define the section symbol at subsection 0's initial fragment if required.
355
280
if (!NewSec)
@@ -420,15 +345,11 @@ void MCObjectStreamer::emitInstToData(const MCInst &Inst,
420
345
MCFragment *F = getCurrentFragment ();
421
346
422
347
// Append the instruction to the data fragment.
423
- size_t CodeOffset = getCurFragSize ();
424
- SmallString<16 > Content;
348
+ size_t CodeOffset = F->getContents ().size ();
425
349
SmallVector<MCFixup, 1 > Fixups;
426
- getAssembler ().getEmitter ().encodeInstruction (Inst, Content, Fixups, STI);
427
- appendContents (Content);
428
- if (CurFrag != F) {
429
- F = CurFrag;
430
- CodeOffset = 0 ;
431
- }
350
+ getAssembler ().getEmitter ().encodeInstruction (
351
+ Inst, F->getContentsForAppending (), Fixups, STI);
352
+ F->doneAppending ();
432
353
F->setHasInstructions (STI);
433
354
434
355
if (Fixups.empty ())
0 commit comments