LLVM  7.0.0
MCWasmStreamer.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits Wasm .o object files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/MC/MCWasmStreamer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionWasm.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCSymbolWasm.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
35 
36 using namespace llvm;
37 
39 
40 void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
41  flushPendingLabels(DF, DF->getContents().size());
42 
43  for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
44  EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
45  DF->getContents().size());
46  DF->getFixups().push_back(EF->getFixups()[i]);
47  }
48  if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
50  DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
51 }
52 
54  // Let the target do whatever target specific stuff it needs to do.
56 
57  // Do any generic stuff we need to do.
58  llvm_unreachable("invalid assembler flag!");
59 }
60 
62  const MCExpr *Subsection) {
64  auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
65  const MCSymbol *Grp = SectionWasm->getGroup();
66  if (Grp)
67  Asm.registerSymbol(*Grp);
68 
69  this->MCObjectStreamer::ChangeSection(Section, Subsection);
70  Asm.registerSymbol(*Section->getBeginSymbol());
71 }
72 
74  const MCSymbol *Symbol) {
75  getAssembler().registerSymbol(*Symbol);
78  Alias->setVariableValue(Value);
79 }
80 
82  assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
83 
84  auto *Symbol = cast<MCSymbolWasm>(S);
85 
86  // Adding a symbol attribute always introduces the symbol; note that an
87  // important side effect of calling registerSymbol here is to register the
88  // symbol with the assembler.
90 
91  switch (Attribute) {
92  case MCSA_LazyReference:
93  case MCSA_Reference:
95  case MCSA_PrivateExtern:
98  case MCSA_Invalid:
100  case MCSA_Protected:
101  return false;
102 
103  case MCSA_Hidden:
104  Symbol->setHidden(true);
105  break;
106 
107  case MCSA_Weak:
108  case MCSA_WeakReference:
109  Symbol->setWeak(true);
110  Symbol->setExternal(true);
111  break;
112 
113  case MCSA_Global:
114  Symbol->setExternal(true);
115  break;
116 
119  break;
120 
121  case MCSA_ELF_TypeObject:
123  break;
124 
125  default:
126  // unrecognized directive
127  llvm_unreachable("unexpected MCSymbolAttr");
128  return false;
129  }
130 
131  return true;
132 }
133 
135  unsigned ByteAlignment) {
136  llvm_unreachable("Common symbols are not yet implemented for Wasm");
137 }
138 
140  cast<MCSymbolWasm>(Symbol)->setSize(Value);
141 }
142 
144  unsigned ByteAlignment) {
145  llvm_unreachable("Local common symbols are not yet implemented for Wasm");
146 }
147 
149  SMLoc Loc) {
150  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
151 }
152 
154  unsigned ValueSize,
155  unsigned MaxBytesToEmit) {
156  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
157  MaxBytesToEmit);
158 }
159 
161  // TODO(sbc): Add the ident section once we support mergable strings
162  // sections in the object format
163 }
164 
165 void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
166  const MCSubtargetInfo &STI) {
167  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
168 }
169 
170 void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
171  const MCSubtargetInfo &STI) {
172  MCAssembler &Assembler = getAssembler();
174  SmallString<256> Code;
175  raw_svector_ostream VecOS(Code);
176  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
177 
178  // Append the encoded instruction to the current data fragment (or create a
179  // new such fragment if the current fragment is not a data fragment).
181 
182  // Add the fixups and data.
183  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
184  Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
185  DF->getFixups().push_back(Fixups[i]);
186  }
187  DF->setHasInstructions(STI);
188  DF->getContents().append(Code.begin(), Code.end());
189 }
190 
192  EmitFrames(nullptr);
193 
195 }
196 
198  std::unique_ptr<MCAsmBackend> &&MAB,
199  std::unique_ptr<MCObjectWriter> &&OW,
200  std::unique_ptr<MCCodeEmitter> &&CE,
201  bool RelaxAll) {
202  MCWasmStreamer *S =
203  new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
204  if (RelaxAll)
205  S->getAssembler().setRelaxAll(true);
206  return S;
207 }
208 
210  llvm_unreachable("Generic Wasm doesn't support this directive");
211 }
212 
213 void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
214  llvm_unreachable("Wasm doesn't support this directive");
215 }
216 
218  uint64_t Size, unsigned ByteAlignment,
219  SMLoc Loc) {
220  llvm_unreachable("Wasm doesn't support this directive");
221 }
222 
224  uint64_t Size, unsigned ByteAlignment) {
225  llvm_unreachable("Wasm doesn't support this directive");
226 }
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:321
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
.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:42
Not a valid directive.
Definition: MCDirectives.h:20
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:504
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCStreamer.h:250
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:288
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:315
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
void flushPendingLabels()
Create a dummy fragment to assign any pending labels.
Context object for machine code objects.
Definition: MCContext.h:63
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:40
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
.lazy_reference (MachO)
Definition: MCDirectives.h:34
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:198
.reference (MachO)
Definition: MCDirectives.h:41
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override
Set the DescValue for the Symbol.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:118
.hidden (ELF)
Definition: MCDirectives.h:31
Streaming machine code generation interface.
Definition: MCStreamer.h:183
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:45
This represents a section on wasm.
Definition: MCSectionWasm.h:28
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
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:44
size_t size() const
Definition: SmallVector.h:53
#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:286
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:847
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
MCSymbol * getBeginSymbol()
Definition: MCSection.h:106
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:49
MCSymbolAttr
Definition: MCDirectives.h:19
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:394
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:160
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
.symbol_resolver (MachO)
Definition: MCDirectives.h:37
.type _foo,
Definition: MCDirectives.h:30
void FinishImpl() override
Streamer specific finalization.
MCAssemblerFlag
Definition: MCDirectives.h:48
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
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.
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:43
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
.private_extern (MachO)
Definition: MCDirectives.h:39
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
LLVM Value Representation.
Definition: Value.h:73
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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:49
Represents a location in source code.
Definition: SMLoc.h:24