LLVM  10.0.0svn
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 //===----------------------------------------------------------------------===//
13 #include "llvm/MC/MCWasmStreamer.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmLayout.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCSectionWasm.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Debug.h"
35 using namespace llvm;
37 MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
39 void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
40  flushPendingLabels(DF, DF->getContents().size());
42  for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
43  EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
44  DF->getContents().size());
45  DF->getFixups().push_back(EF->getFixups()[I]);
46  }
47  if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
49  DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
50 }
53  // Let the target do whatever target specific stuff it needs to do.
56  // Do any generic stuff we need to do.
57  llvm_unreachable("invalid assembler flag!");
58 }
61  const MCExpr *Subsection) {
63  auto *SectionWasm = cast<MCSectionWasm>(Section);
64  const MCSymbol *Grp = SectionWasm->getGroup();
65  if (Grp)
66  Asm.registerSymbol(*Grp);
68  this->MCObjectStreamer::ChangeSection(Section, Subsection);
69  Asm.registerSymbol(*Section->getBeginSymbol());
70 }
73  const MCSymbol *Symbol) {
74  getAssembler().registerSymbol(*Symbol);
77  Alias->setVariableValue(Value);
78 }
81  assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
83  auto *Symbol = cast<MCSymbolWasm>(S);
85  // Adding a symbol attribute always introduces the symbol; note that an
86  // important side effect of calling registerSymbol here is to register the
87  // symbol with the assembler.
90  switch (Attribute) {
91  case MCSA_LazyReference:
92  case MCSA_Reference:
94  case MCSA_PrivateExtern:
97  case MCSA_Invalid:
99  case MCSA_Protected:
100  return false;
102  case MCSA_Hidden:
103  Symbol->setHidden(true);
104  break;
106  case MCSA_Weak:
107  case MCSA_WeakReference:
108  Symbol->setWeak(true);
109  Symbol->setExternal(true);
110  break;
112  case MCSA_Global:
113  Symbol->setExternal(true);
114  break;
118  break;
120  case MCSA_ELF_TypeObject:
121  case MCSA_Cold:
122  break;
124  case MCSA_NoDeadStrip:
125  Symbol->setNoStrip();
126  break;
128  default:
129  // unrecognized directive
130  llvm_unreachable("unexpected MCSymbolAttr");
131  return false;
132  }
134  return true;
135 }
138  unsigned ByteAlignment) {
139  llvm_unreachable("Common symbols are not yet implemented for Wasm");
140 }
143  cast<MCSymbolWasm>(Symbol)->setSize(Value);
144 }
147  unsigned ByteAlignment) {
148  llvm_unreachable("Local common symbols are not yet implemented for Wasm");
149 }
152  SMLoc Loc) {
153  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
154 }
157  unsigned ValueSize,
158  unsigned MaxBytesToEmit) {
159  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
160  MaxBytesToEmit);
161 }
164  // TODO(sbc): Add the ident section once we support mergable strings
165  // sections in the object format
166 }
168 void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
169  const MCSubtargetInfo &STI) {
170  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
171 }
173 void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
174  const MCSubtargetInfo &STI) {
175  MCAssembler &Assembler = getAssembler();
177  SmallString<256> Code;
178  raw_svector_ostream VecOS(Code);
179  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
181  // Append the encoded instruction to the current data fragment (or create a
182  // new such fragment if the current fragment is not a data fragment).
185  // Add the fixups and data.
186  for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
187  Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
188  DF->getFixups().push_back(Fixups[I]);
189  }
190  DF->setHasInstructions(STI);
191  DF->getContents().append(Code.begin(), Code.end());
192 }
195  EmitFrames(nullptr);
198 }
201  std::unique_ptr<MCAsmBackend> &&MAB,
202  std::unique_ptr<MCObjectWriter> &&OW,
203  std::unique_ptr<MCCodeEmitter> &&CE,
204  bool RelaxAll) {
205  MCWasmStreamer *S =
206  new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
207  if (RelaxAll)
208  S->getAssembler().setRelaxAll(true);
209  return S;
210 }
213  llvm_unreachable("Generic Wasm doesn't support this directive");
214 }
216 void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
217  llvm_unreachable("Wasm doesn't support this directive");
218 }
221  uint64_t Size, unsigned ByteAlignment,
222  SMLoc Loc) {
223  llvm_unreachable("Wasm doesn't support this directive");
224 }
227  uint64_t Size, unsigned ByteAlignment) {
228  llvm_unreachable("Wasm doesn't support this directive");
229 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:329
This class represents lattice values for constants.
Definition: AllocatorList.h:23
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Not a valid directive.
Definition: MCDirectives.h:19
virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
.cold (MachO)
Definition: MCDirectives.h:22
MCContext & getContext() const
Definition: MCStreamer.h:258
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:294
void EmitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
void setRelaxAll(bool Value)
Definition: MCAssembler.h:321
~MCWasmStreamer() override
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
.no_dead_strip (MachO)
Definition: MCDirectives.h:37
void flushPendingLabels()
Create a dummy fragment to assign any pending labels.
Context object for machine code objects.
Definition: MCContext.h:65
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment=0) override
Emit a thread local bss (.tbss) symbol.
.protected (ELF)
Definition: MCDirectives.h:41
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
.lazy_reference (MachO)
Definition: MCDirectives.h:35
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:198
.reference (MachO)
Definition: MCDirectives.h:42
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override
Set the DescValue for the Symbol.
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:131
.hidden (ELF)
Definition: MCDirectives.h:32
Streaming machine code generation interface.
Definition: MCStreamer.h:196
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:46
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCAssembler & getAssembler()
void setHasInstructions(const MCSubtargetInfo &STI)
Record that the fragment contains instructions with the MCSubtargetInfo in effect when the instructio...
Definition: MCFragment.h:178
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:224
void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
void EmitFrames(MCAsmBackend *MAB)
void EmitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
.weak_reference (MachO)
Definition: MCDirectives.h:45
size_t size() const
Definition: SmallVector.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a local common (.lcomm) symbol.
void EmitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:292
void FinishImpl() override
Streamer specific finalization.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
.indirect_symbol (MachO)
Definition: MCDirectives.h:33
MCSymbol * getBeginSymbol()
Definition: MCSection.h:110
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:48
Definition: MCDirectives.h:18
void EmitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, unsigned ByteAlignment=0, SMLoc Loc=SMLoc()) override
Emit the zerofill section and an optional symbol.
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:174
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a common symbol.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment...
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:175
.symbol_resolver (MachO)
Definition: MCDirectives.h:38
.type _foo,
Definition: MCDirectives.h:30
void FinishImpl() override
Streamer specific finalization.
Definition: MCDirectives.h:49
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
MCWasmStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
uint32_t Size
Definition: Profile.cpp:46
void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void EmitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
.weak_definition (MachO)
Definition: MCDirectives.h:44
Fragment for data and encoded instructions.
Definition: MCFragment.h:243
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
.private_extern (MachO)
Definition: MCDirectives.h:40
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
LLVM Value Representation.
Definition: Value.h:74
MCStreamer * createWasmStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE, bool RelaxAll)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Represents a location in source code.
Definition: SMLoc.h:23