LLVM  5.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/MCAsmInfo.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionWasm.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolWasm.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Debug.h"
38 
39 using namespace llvm;
40 
42 
43 void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
44  flushPendingLabels(DF, DF->getContents().size());
45 
46  for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
47  EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
48  DF->getContents().size());
49  DF->getFixups().push_back(EF->getFixups()[i]);
50  }
51  DF->setHasInstructions(true);
52  DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
53 }
54 
56  // Let the target do whatever target specific stuff it needs to do.
58 
59  // Do any generic stuff we need to do.
60  llvm_unreachable("invalid assembler flag!");
61 }
62 
64  const MCExpr *Subsection) {
66  auto *SectionWasm = static_cast<const MCSectionWasm *>(Section);
67  const MCSymbol *Grp = SectionWasm->getGroup();
68  if (Grp)
69  Asm.registerSymbol(*Grp);
70 
71  this->MCObjectStreamer::ChangeSection(Section, Subsection);
72 }
73 
75  const MCSymbol *Symbol) {
76  getAssembler().registerSymbol(*Symbol);
79  Alias->setVariableValue(Value);
80 }
81 
83  assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
84 
85  auto *Symbol = cast<MCSymbolWasm>(S);
86 
87  // Adding a symbol attribute always introduces the symbol, note that an
88  // important side effect of calling registerSymbol here is to register
89  // the symbol with the assembler.
91 
92  switch (Attribute) {
93  case MCSA_LazyReference:
94  case MCSA_Reference:
96  case MCSA_PrivateExtern:
99  case MCSA_Invalid:
100  case MCSA_IndirectSymbol:
101  case MCSA_Hidden:
102  return false;
103 
104  case MCSA_Weak:
105  case MCSA_WeakReference:
106  Symbol->setWeak(true);
107  Symbol->setExternal(true);
108  break;
109 
110  case MCSA_Global:
111  Symbol->setExternal(true);
112  break;
113 
115  Symbol->setIsFunction(true);
116  break;
117 
118  case MCSA_ELF_TypeObject:
119  Symbol->setIsFunction(false);
120  break;
121 
122  default:
123  // unrecognized directive
124  llvm_unreachable("unexpected MCSymbolAttr");
125  return false;
126  }
127 
128  return true;
129 }
130 
132  unsigned ByteAlignment) {
133  llvm_unreachable("Common symbols are not yet implemented for Wasm");
134 }
135 
137  cast<MCSymbolWasm>(Symbol)->setSize(Value);
138 }
139 
141  unsigned ByteAlignment) {
142  llvm_unreachable("Local common symbols are not yet implemented for Wasm");
143 }
144 
146  SMLoc Loc) {
147  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
148 }
149 
151  unsigned ValueSize,
152  unsigned MaxBytesToEmit) {
153  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
154  MaxBytesToEmit);
155 }
156 
159  ".comment", 0, 0);
160  PushSection();
161  SwitchSection(Comment);
162  if (!SeenIdent) {
163  EmitIntValue(0, 1);
164  SeenIdent = true;
165  }
166  EmitBytes(IdentString);
167  EmitIntValue(0, 1);
168  PopSection();
169 }
170 
171 void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
172  const MCSubtargetInfo &STI) {
173  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
174 }
175 
176 void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
177  const MCSubtargetInfo &STI) {
178  MCAssembler &Assembler = getAssembler();
180  SmallString<256> Code;
181  raw_svector_ostream VecOS(Code);
182  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
183 
184  // Append the encoded instruction to the current data fragment (or create a
185  // new such fragment if the current fragment is not a data fragment).
187 
188  // Add the fixups and data.
189  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
190  Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
191  DF->getFixups().push_back(Fixups[i]);
192  }
193  DF->setHasInstructions(true);
194  DF->getContents().append(Code.begin(), Code.end());
195 }
196 
198  EmitFrames(nullptr);
199 
201 }
202 
205  bool RelaxAll) {
206  MCWasmStreamer *S = new MCWasmStreamer(Context, MAB, OS, CE);
207  if (RelaxAll)
208  S->getAssembler().setRelaxAll(true);
209  return S;
210 }
211 
213  llvm_unreachable("Generic Wasm doesn't support this directive");
214 }
215 
216 void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
217  llvm_unreachable("Wasm doesn't support this directive");
218 }
219 
221  uint64_t Size, unsigned ByteAlignment) {
222  llvm_unreachable("Wasm doesn't support this directive");
223 }
224 
226  uint64_t Size, unsigned ByteAlignment) {
227  llvm_unreachable("Wasm doesn't support this directive");
228 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
MCSectionWasm * getWasmSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:444
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
.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...
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:261
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
void PushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:330
MCContext & getContext() const
Definition: MCAssembler.h:257
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:288
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
ExternalFunctions * EF
MCContext & getContext() const
Definition: MCStreamer.h:226
Context object for machine code objects.
Definition: MCContext.h:59
bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void setHasInstructions(bool V)
Definition: MCFragment.h:229
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.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
.lazy_reference (MachO)
Definition: MCDirectives.h:34
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:181
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:93
.reference (MachO)
Definition: MCDirectives.h:41
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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:121
void EmitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, unsigned ByteAlignment=0) override
Emit the zerofill section and an optional symbol.
.hidden (ELF)
Definition: MCDirectives.h:31
Streaming machine code generation interface.
Definition: MCStreamer.h:167
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:45
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:854
This represents a section on wasm.
Definition: MCSectionWasm.h:28
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
MCAssembler & getAssembler()
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:206
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
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.
void FinishImpl() override
Streamer specific finalization.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:48
MCSymbolAttr
Definition: MCDirectives.h:19
void flushPendingLabels(MCFragment *F, uint64_t FOffset=0)
If any labels have been emitted but not assigned fragments, ensure that they get assigned, either to F if possible or to a new data fragment.
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:395
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:128
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
.symbol_resolver (MachO)
Definition: MCDirectives.h:37
MCStreamer * createWasmStreamer(MCContext &Ctx, MCAsmBackend &TAB, raw_pwrite_stream &OS, MCCodeEmitter *CE, bool RelaxAll)
.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
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:259
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
MCSubtargetInfo - Generic base class for all target subtargets.
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
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
Fragment for data and encoded instructions.
Definition: MCFragment.h:224
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
.private_extern (MachO)
Definition: MCDirectives.h:39
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
bool PopSection()
Restore the current and previous section from the section stack.
Definition: MCStreamer.h:339
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Represents a location in source code.
Definition: SMLoc.h:24
MCDataFragment * getOrCreateDataFragment()
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment...