1 //===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCFragment.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
33 MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
34 // Compute the section layout order. Virtual sections must go last.
35 for (MCSection &Sec : Asm)
36 if (!Sec.isVirtualSection())
37 SectionOrder.push_back(&Sec);
38 for (MCSection &Sec : Asm)
39 if (Sec.isVirtualSection())
40 SectionOrder.push_back(&Sec);
43 bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
44 const MCSection *Sec = F->getParent();
45 const MCFragment *LastValid = LastValidFragment.lookup(Sec);
48 assert(LastValid->getParent() == Sec);
49 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
52 void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
53 // If this fragment wasn't already valid, we don't need to do anything.
54 if (!isFragmentValid(F))
57 // Otherwise, reset the last valid fragment to the previous fragment
58 // (if this is the first fragment, it will be NULL).
59 LastValidFragment[F->getParent()] = F->getPrevNode();
62 void MCAsmLayout::ensureValid(const MCFragment *F) const {
63 MCSection *Sec = F->getParent();
64 MCSection::iterator I;
65 if (MCFragment *Cur = LastValidFragment[Sec])
66 I = ++MCSection::iterator(Cur);
70 // Advance the layout position until the fragment is valid.
71 while (!isFragmentValid(F)) {
72 assert(I != Sec->end() && "Layout bookkeeping error");
73 const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
78 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
80 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
84 // Simple getSymbolOffset helper for the non-variable case.
85 static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
86 bool ReportError, uint64_t &Val) {
87 if (!S.getFragment()) {
89 report_fatal_error("unable to evaluate offset to undefined symbol '" +
93 Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
97 static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
98 bool ReportError, uint64_t &Val) {
100 return getLabelOffset(Layout, S, ReportError, Val);
102 // If SD is a variable, evaluate it.
104 if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
105 report_fatal_error("unable to evaluate offset for variable '" +
108 uint64_t Offset = Target.getConstant();
110 const MCSymbolRefExpr *A = Target.getSymA();
113 if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
118 const MCSymbolRefExpr *B = Target.getSymB();
121 if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
130 bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
131 return getSymbolOffsetImpl(*this, S, false, Val);
134 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
136 getSymbolOffsetImpl(*this, S, true, Val);
140 const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
141 if (!Symbol.isVariable())
144 const MCExpr *Expr = Symbol.getVariableValue();
146 if (!Expr->evaluateAsValue(Value, *this)) {
147 Assembler.getContext().reportError(
148 Expr->getLoc(), "expression could not be evaluated");
152 const MCSymbolRefExpr *RefB = Value.getSymB();
154 Assembler.getContext().reportError(
155 Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
156 "' could not be evaluated in a subtraction expression");
160 const MCSymbolRefExpr *A = Value.getSymA();
164 const MCSymbol &ASym = A->getSymbol();
165 const MCAssembler &Asm = getAssembler();
166 if (ASym.isCommon()) {
167 Asm.getContext().reportError(Expr->getLoc(),
168 "Common symbol '" + ASym.getName() +
169 "' cannot be used in assignment expr");
176 uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
177 // The size is the last fragment's end offset.
178 const MCFragment &F = Sec->getFragmentList().back();
179 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
182 uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
183 // Virtual sections have no file size.
184 if (Sec->isVirtualSection())
187 // Otherwise, the file size is the same as the address space size.
188 return getSectionAddressSize(Sec);
191 uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
192 const MCEncodedFragment *F,
193 uint64_t FOffset, uint64_t FSize) {
194 uint64_t BundleSize = Assembler.getBundleAlignSize();
195 assert(BundleSize > 0 &&
196 "computeBundlePadding should only be called if bundling is enabled");
197 uint64_t BundleMask = BundleSize - 1;
198 uint64_t OffsetInBundle = FOffset & BundleMask;
199 uint64_t EndOfFragment = OffsetInBundle + FSize;
201 // There are two kinds of bundling restrictions:
203 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
204 // *end* on a bundle boundary.
205 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
206 // would, add padding until the end of the bundle so that the fragment
207 // will start in a new one.
208 if (F->alignToBundleEnd()) {
209 // Three possibilities here:
211 // A) The fragment just happens to end at a bundle boundary, so we're good.
212 // B) The fragment ends before the current bundle boundary: pad it just
213 // enough to reach the boundary.
214 // C) The fragment ends after the current bundle boundary: pad it until it
215 // reaches the end of the next bundle boundary.
217 // Note: this code could be made shorter with some modulo trickery, but it's
218 // intentionally kept in its more explicit form for simplicity.
219 if (EndOfFragment == BundleSize)
221 else if (EndOfFragment < BundleSize)
222 return BundleSize - EndOfFragment;
223 else { // EndOfFragment > BundleSize
224 return 2 * BundleSize - EndOfFragment;
226 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
227 return BundleSize - OffsetInBundle;
234 void ilist_alloc_traits<MCFragment>::deleteNode(MCFragment *V) { V->destroy(); }
236 MCFragment::~MCFragment() = default;
238 MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
240 : Kind(Kind), HasInstructions(HasInstructions), Parent(Parent),
241 Atom(nullptr), Offset(~UINT64_C(0)) {
242 if (Parent && !isDummy())
243 Parent->getFragmentList().push_back(this);
246 void MCFragment::destroy() {
247 // First check if we are the sentinal.
248 if (Kind == FragmentType(~0)) {
255 delete cast<MCAlignFragment>(this);
258 delete cast<MCDataFragment>(this);
260 case FT_CompactEncodedInst:
261 delete cast<MCCompactEncodedInstFragment>(this);
264 delete cast<MCFillFragment>(this);
267 delete cast<MCRelaxableFragment>(this);
270 delete cast<MCOrgFragment>(this);
273 delete cast<MCDwarfLineAddrFragment>(this);
276 delete cast<MCDwarfCallFrameFragment>(this);
279 delete cast<MCLEBFragment>(this);
282 delete cast<MCPaddingFragment>(this);
285 delete cast<MCSymbolIdFragment>(this);
287 case FT_CVInlineLines:
288 delete cast<MCCVInlineLineTableFragment>(this);
291 delete cast<MCCVDefRangeFragment>(this);
294 delete cast<MCDummyFragment>(this);
303 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
304 OS << "<MCFixup" << " Offset:" << AF.getOffset()
305 << " Value:" << *AF.getValue()
306 << " Kind:" << AF.getKind() << ">";
310 } // end namespace llvm
312 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
313 LLVM_DUMP_METHOD void MCFragment::dump() const {
314 raw_ostream &OS = errs();
318 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
319 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
320 case MCFragment::FT_CompactEncodedInst:
321 OS << "MCCompactEncodedInstFragment"; break;
322 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
323 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
324 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
325 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
326 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
327 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
328 case MCFragment::FT_Padding: OS << "MCPaddingFragment"; break;
329 case MCFragment::FT_SymbolId: OS << "MCSymbolIdFragment"; break;
330 case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
331 case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
332 case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
335 OS << "<MCFragment " << (const void *)this << " LayoutOrder:" << LayoutOrder
336 << " Offset:" << Offset << " HasInstructions:" << hasInstructions();
337 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(this))
338 OS << " BundlePadding:" << static_cast<unsigned>(EF->getBundlePadding());
342 case MCFragment::FT_Align: {
343 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
344 if (AF->hasEmitNops())
345 OS << " (emit nops)";
347 OS << " Alignment:" << AF->getAlignment()
348 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
349 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
352 case MCFragment::FT_Data: {
353 const MCDataFragment *DF = cast<MCDataFragment>(this);
356 const SmallVectorImpl<char> &Contents = DF->getContents();
357 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
359 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
361 OS << "] (" << Contents.size() << " bytes)";
363 if (DF->fixup_begin() != DF->fixup_end()) {
366 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
367 ie = DF->fixup_end(); it != ie; ++it) {
368 if (it != DF->fixup_begin()) OS << ",\n ";
375 case MCFragment::FT_CompactEncodedInst: {
376 const MCCompactEncodedInstFragment *CEIF =
377 cast<MCCompactEncodedInstFragment>(this);
380 const SmallVectorImpl<char> &Contents = CEIF->getContents();
381 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
383 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
385 OS << "] (" << Contents.size() << " bytes)";
388 case MCFragment::FT_Fill: {
389 const MCFillFragment *FF = cast<MCFillFragment>(this);
390 OS << " Value:" << static_cast<unsigned>(FF->getValue())
391 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
392 << " NumValues:" << FF->getNumValues();
395 case MCFragment::FT_Relaxable: {
396 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
399 F->getInst().dump_pretty(OS);
402 case MCFragment::FT_Org: {
403 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
405 OS << " Offset:" << OF->getOffset()
406 << " Value:" << static_cast<unsigned>(OF->getValue());
409 case MCFragment::FT_Dwarf: {
410 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
412 OS << " AddrDelta:" << OF->getAddrDelta()
413 << " LineDelta:" << OF->getLineDelta();
416 case MCFragment::FT_DwarfFrame: {
417 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
419 OS << " AddrDelta:" << CF->getAddrDelta();
422 case MCFragment::FT_LEB: {
423 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
425 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
428 case MCFragment::FT_Padding: {
429 const MCPaddingFragment *F = cast<MCPaddingFragment>(this);
431 OS << " PaddingPoliciesMask:" << F->getPaddingPoliciesMask()
432 << " IsInsertionPoint:" << F->isInsertionPoint()
433 << " Size:" << F->getSize();
436 F->getInst().dump_pretty(OS);
437 OS << " InstSize:" << F->getInstSize();
441 case MCFragment::FT_SymbolId: {
442 const MCSymbolIdFragment *F = cast<MCSymbolIdFragment>(this);
444 OS << " Sym:" << F->getSymbol();
447 case MCFragment::FT_CVInlineLines: {
448 const auto *F = cast<MCCVInlineLineTableFragment>(this);
450 OS << " Sym:" << *F->getFnStartSym();
453 case MCFragment::FT_CVDefRange: {
454 const auto *F = cast<MCCVDefRangeFragment>(this);
456 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
458 OS << " RangeStart:" << RangeStartEnd.first;
459 OS << " RangeEnd:" << RangeStartEnd.second;
463 case MCFragment::FT_Dummy: