LLVM 23.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;
31class MCSubtargetInfo;
32} // namespace llvm
33
34using namespace llvm;
35
36MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
37
39 auto *Symbol = static_cast<MCSymbolWasm *>(S);
41
42 const MCSectionWasm &Section =
43 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
44 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
45 Symbol->setTLS();
46}
47
50 auto *Symbol = static_cast<MCSymbolWasm *>(S);
52
53 const MCSectionWasm &Section =
54 static_cast<const MCSectionWasm &>(*getCurrentSectionOnly());
55 if (Section.getSegmentFlags() & wasm::WASM_SEG_FLAG_TLS)
56 Symbol->setTLS();
57}
58
61 auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
62 const MCSymbol *Grp = SectionWasm->getGroup();
63 if (Grp)
64 Asm.registerSymbol(*Grp);
65
66 this->MCObjectStreamer::changeSection(Section, Subsection);
67 Asm.registerSymbol(*Section->getBeginSymbol());
68}
69
71 assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
72 auto *Symbol = static_cast<MCSymbolWasm *>(S);
73
74 // Adding a symbol attribute always introduces the symbol; note that an
75 // important side effect of calling registerSymbol here is to register the
76 // symbol with the assembler.
78
79 switch (Attribute) {
81 case MCSA_Reference:
86 case MCSA_Invalid:
88 case MCSA_Protected:
89 case MCSA_Exported:
90 return false;
91
92 case MCSA_Hidden:
93 Symbol->setHidden(true);
94 break;
95
96 case MCSA_Weak:
98 Symbol->setWeak(true);
99 Symbol->setExternal(true);
100 break;
101
102 case MCSA_Global:
103 Symbol->setExternal(true);
104 break;
105
107 Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
108 break;
109
110 case MCSA_ELF_TypeTLS:
111 Symbol->setTLS();
112 break;
113
115 case MCSA_Cold:
116 break;
117
118 case MCSA_NoDeadStrip:
119 Symbol->setNoStrip();
120 break;
121
122 default:
123 // unrecognized directive
124 llvm_unreachable("unexpected MCSymbolAttr");
125 return false;
126 }
127
128 return true;
129}
130
132 Align ByteAlignment) {
134 "common symbols are not yet implemented for Wasm: " +
135 S->getName());
136}
137
139 static_cast<MCSymbolWasm *>(Symbol)->setSize(Value);
140}
141
143 Align ByteAlignment) {
145 "local common symbols are not yet implemented "
146 "for Wasm: " +
147 S->getName());
148}
149
151 // TODO(sbc): Add the ident section once we support mergable strings
152 // sections in the object format
153}
154
160
162 std::unique_ptr<MCAsmBackend> &&MAB,
163 std::unique_ptr<MCObjectWriter> &&OW,
164 std::unique_ptr<MCCodeEmitter> &&CE) {
165 MCWasmStreamer *S =
166 new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
167 return S;
168}
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:105
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
Context object for machine code objects.
Definition MCContext.h:83
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: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.
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:516
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCContext & getContext() const
Definition MCStreamer.h:322
SMLoc getStartTokLoc() const
Definition MCStreamer.h:310
MCSection * getCurrentSectionOnly() const
Definition MCStreamer.h:427
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
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
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.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
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