LLVM 22.0.0git
MCWasmStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file assembles .s files and emits Wasm .o object files.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixup.h"
20#include "llvm/MC/MCSection.h"
22#include "llvm/MC/MCSymbol.h"
26
27namespace llvm {
28class MCContext;
29class MCStreamer;
30class MCSubtargetInfo;
31} // namespace llvm
32
33using namespace llvm;
34
35MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
36
38 auto *Symbol = static_cast<MCSymbolWasm *>(S);
40
41 const MCSectionWasm &Section =
42 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
43 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
44 Symbol->setTLS();
45}
46
49 auto *Symbol = static_cast<MCSymbolWasm *>(S);
51
52 const MCSectionWasm &Section =
53 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
54 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
55 Symbol->setTLS();
56}
57
60 auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
61 const MCSymbol *Grp = SectionWasm->getGroup();
62 if (Grp)
63 Asm.registerSymbol(*Grp);
64
65 this->MCObjectStreamer::changeSection(Section, Subsection);
66 Asm.registerSymbol(*Section->getBeginSymbol());
67}
68
70 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
71 auto *Symbol = static_cast<MCSymbolWasm *>(S);
72
73 // Adding a symbol attribute always introduces the symbol; note that an
74 // important side effect of calling registerSymbol here is to register the
75 // symbol with the assembler.
77
78 switch (Attribute) {
80 case MCSA_Reference:
85 case MCSA_Invalid:
87 case MCSA_Protected:
88 case MCSA_Exported:
89 return false;
90
91 case MCSA_Hidden:
92 Symbol->setHidden(true);
93 break;
94
95 case MCSA_Weak:
97 Symbol->setWeak(true);
98 Symbol->setExternal(true);
99 break;
100
101 case MCSA_Global:
102 Symbol->setExternal(true);
103 break;
104
106 Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
107 break;
108
109 case MCSA_ELF_TypeTLS:
110 Symbol->setTLS();
111 break;
112
114 case MCSA_Cold:
115 break;
116
117 case MCSA_NoDeadStrip:
118 Symbol->setNoStrip();
119 break;
120
121 default:
122 // unrecognized directive
123 llvm_unreachable("unexpected MCSymbolAttr");
124 return false;
125 }
126
127 return true;
128}
129
131 Align ByteAlignment) {
132 llvm_unreachable("Common symbols are not yet implemented for Wasm");
133}
134
136 static_cast<MCSymbolWasm *>(Symbol)->setSize(Value);
137}
138
140 Align ByteAlignment) {
141 llvm_unreachable("Local common symbols are not yet implemented for Wasm");
142}
143
145 // TODO(sbc): Add the ident section once we support mergable strings
146 // sections in the object format
147}
148
150 emitFrames(nullptr);
151
153}
154
156 std::unique_ptr<MCAsmBackend> &&MAB,
157 std::unique_ptr<MCObjectWriter> &&OW,
158 std::unique_ptr<MCCodeEmitter> &&CE) {
159 MCWasmStreamer *S =
160 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
161 return S;
162}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:55
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
MCAssembler & getAssembler()
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void finishImpl() override
Streamer specific finalization.
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitFrames(MCAsmBackend *MAB)
This represents a section on wasm.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:496
Streaming machine code generation interface.
Definition MCStreamer.h:220
MCSection * getCurrentSectionOnly() const
Definition MCStreamer.h:421
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a local common (.lcomm) symbol.
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset) override
void emitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
~MCWasmStreamer() override
void changeSection(MCSection *Section, uint32_t Subsection) override
This is called by popSection and switchSection, if the current section changes.
void finishImpl() override
Streamer specific finalization.
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Represents a location in source code.
Definition SMLoc.h:23
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:220
@ WASM_SEG_FLAG_TLS
Definition Wasm.h:230
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
LLVM_ABI MCStreamer * createWasmStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
@ MCSA_WeakDefAutoPrivate
.weak_def_can_be_hidden (MachO)
@ MCSA_Protected
.protected (ELF)
@ MCSA_Exported
.globl _foo, exported (XCOFF)
@ MCSA_PrivateExtern
.private_extern (MachO)
@ MCSA_WeakReference
.weak_reference (MachO)
@ MCSA_LazyReference
.lazy_reference (MachO)
@ MCSA_Reference
.reference (MachO)
@ MCSA_SymbolResolver
.symbol_resolver (MachO)
@ MCSA_Weak
.weak
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
@ MCSA_WeakDefinition
.weak_definition (MachO)
@ MCSA_Global
.type _foo, @gnu_unique_object
@ MCSA_Cold
.cold (MachO)
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
@ MCSA_Hidden
.hidden (ELF)
@ MCSA_Invalid
Not a valid directive.
@ MCSA_NoDeadStrip
.no_dead_strip (MachO)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39