LLVM 24.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/MCContext.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCFixup.h"
21#include "llvm/MC/MCSection.h"
23#include "llvm/MC/MCSymbol.h"
27
28namespace llvm {
29class MCContext;
30class MCStreamer;
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
48 uint64_t Offset) {
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 auto *Symbol = static_cast<MCSymbolWasm *>(S);
133 getAssembler().registerSymbol(*Symbol);
134
135 Symbol->setExternal(true);
136
137 Symbol->setType(wasm::WASM_SYMBOL_TYPE_DATA);
138
139 if (Symbol->declareCommon(Size, ByteAlignment))
140 report_fatal_error(Twine("Symbol: ") + Symbol->getName() +
141 " redeclared as different type");
142
143 Symbol->setSize(MCConstantExpr::create(Size, getContext()));
144}
145
147 static_cast<MCSymbolWasm *>(Symbol)->setSize(Value);
148}
149
151 Align ByteAlignment) {
153 "local common symbols are not yet implemented "
154 "for Wasm: " +
155 S->getName());
156}
157
159 // TODO(sbc): Add the ident section once we support mergeable strings
160 // sections in the object format
161}
162
168
170 std::unique_ptr<MCAsmBackend> &&MAB,
171 std::unique_ptr<MCObjectWriter> &&OW,
172 std::unique_ptr<MCCodeEmitter> &&CE) {
173 MCWasmStreamer *S =
174 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
175 return S;
176}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:84
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
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.
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:577
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCContext & getContext() const
Definition MCStreamer.h:325
SMLoc getStartTokLoc() const
Definition MCStreamer.h:313
MCSection * getCurrentSectionOnly() const
Definition MCStreamer.h:437
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
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:22
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
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_DATA
Definition Wasm.h:232
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:231
@ WASM_SEG_FLAG_TLS
Definition Wasm.h:241
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
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