LLVM  9.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;
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  break;
123  default:
124  // unrecognized directive
125  llvm_unreachable("unexpected MCSymbolAttr");
126  return false;
127  }
129  return true;
130 }
133  unsigned ByteAlignment) {
134  llvm_unreachable("Common symbols are not yet implemented for Wasm");
135 }
138  cast<MCSymbolWasm>(Symbol)->setSize(Value);
139 }
142  unsigned ByteAlignment) {
143  llvm_unreachable("Local common symbols are not yet implemented for Wasm");
144 }
147  SMLoc Loc) {
148  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
149 }
152  unsigned ValueSize,
153  unsigned MaxBytesToEmit) {
154  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
155  MaxBytesToEmit);
156 }
159  // TODO(sbc): Add the ident section once we support mergable strings
160  // sections in the object format
161 }
163 void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
164  const MCSubtargetInfo &STI) {
165  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
166 }
168 void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
169  const MCSubtargetInfo &STI) {
170  MCAssembler &Assembler = getAssembler();
172  SmallString<256> Code;
173  raw_svector_ostream VecOS(Code);
174  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
176  // Append the encoded instruction to the current data fragment (or create a
177  // new such fragment if the current fragment is not a data fragment).
180  // Add the fixups and data.
181  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
182  Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
183  DF->getFixups().push_back(Fixups[i]);
184  }
185  DF->setHasInstructions(STI);
186  DF->getContents().append(Code.begin(), Code.end());
187 }
190  EmitFrames(nullptr);
193 }
196  std::unique_ptr<MCAsmBackend> &&MAB,
197  std::unique_ptr<MCObjectWriter> &&OW,
198  std::unique_ptr<MCCodeEmitter> &&CE,
199  bool RelaxAll) {
200  MCWasmStreamer *S =
201  new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
202  if (RelaxAll)
203  S->getAssembler().setRelaxAll(true);
204  return S;
205 }
208  llvm_unreachable("Generic Wasm doesn't support this directive");
209 }
211 void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
212  llvm_unreachable("Wasm doesn't support this directive");
213 }
216  uint64_t Size, unsigned ByteAlignment,
217  SMLoc Loc) {
218  llvm_unreachable("Wasm doesn't support this directive");
219 }
222  uint64_t Size, unsigned ByteAlignment) {
223  llvm_unreachable("Wasm doesn't support this directive");
224 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:322
This class represents lattice values for constants.
Definition: AllocatorList.h:23
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:24
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:509
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCStreamer.h:250
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
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
void flushPendingLabels()
Create a dummy fragment to assign any pending labels.
Context object for machine code objects.
Definition: MCContext.h:62
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:39
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
.lazy_reference (MachO)
Definition: MCDirectives.h:33
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:197
.reference (MachO)
Definition: MCDirectives.h:40
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:117
.hidden (ELF)
Definition: MCDirectives.h:30
Streaming machine code generation interface.
Definition: MCStreamer.h:188
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:44
MCAssembler & getAssembler()
void setHasInstructions(const MCSubtargetInfo &STI)
Record that the fragment contains instructions with the MCSubtargetInfo in effect when the instructio...
Definition: MCFragment.h:177
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:223
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:43
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:839
.indirect_symbol (MachO)
Definition: MCDirectives.h:31
MCSymbol * getBeginSymbol()
Definition: MCSection.h:109
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:173
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:386
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:159
.symbol_resolver (MachO)
Definition: MCDirectives.h:36
.type _foo,
Definition: MCDirectives.h:29
void FinishImpl() override
Streamer specific finalization.
Definition: MCDirectives.h:47
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:22
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:42
Fragment for data and encoded instructions.
Definition: MCFragment.h:241
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
.private_extern (MachO)
Definition: MCDirectives.h:38
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
LLVM Value Representation.
Definition: Value.h:72
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