MCWasmStreamer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "llvm/MC/MCWasmStreamer.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/MC/MCAsmBackend.h"
  16. #include "llvm/MC/MCAsmLayout.h"
  17. #include "llvm/MC/MCAssembler.h"
  18. #include "llvm/MC/MCCodeEmitter.h"
  19. #include "llvm/MC/MCContext.h"
  20. #include "llvm/MC/MCExpr.h"
  21. #include "llvm/MC/MCInst.h"
  22. #include "llvm/MC/MCObjectStreamer.h"
  23. #include "llvm/MC/MCSection.h"
  24. #include "llvm/MC/MCSectionWasm.h"
  25. #include "llvm/MC/MCSymbol.h"
  26. #include "llvm/MC/MCSymbolWasm.h"
  27. #include "llvm/MC/MCValue.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/TargetRegistry.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. using namespace llvm;
  34. MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
  35. void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
  36. flushPendingLabels(DF, DF->getContents().size());
  37. for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
  38. EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
  39. DF->getContents().size());
  40. DF->getFixups().push_back(EF->getFixups()[I]);
  41. }
  42. if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
  43. DF->setHasInstructions(*EF->getSubtargetInfo());
  44. DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
  45. }
  46. void MCWasmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
  47. // Let the target do whatever target specific stuff it needs to do.
  48. getAssembler().getBackend().handleAssemblerFlag(Flag);
  49. // Do any generic stuff we need to do.
  50. llvm_unreachable("invalid assembler flag!");
  51. }
  52. void MCWasmStreamer::changeSection(MCSection *Section,
  53. const MCExpr *Subsection) {
  54. MCAssembler &Asm = getAssembler();
  55. auto *SectionWasm = cast<MCSectionWasm>(Section);
  56. const MCSymbol *Grp = SectionWasm->getGroup();
  57. if (Grp)
  58. Asm.registerSymbol(*Grp);
  59. this->MCObjectStreamer::changeSection(Section, Subsection);
  60. Asm.registerSymbol(*Section->getBeginSymbol());
  61. }
  62. void MCWasmStreamer::emitWeakReference(MCSymbol *Alias,
  63. const MCSymbol *Symbol) {
  64. getAssembler().registerSymbol(*Symbol);
  65. const MCExpr *Value = MCSymbolRefExpr::create(
  66. Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
  67. Alias->setVariableValue(Value);
  68. }
  69. bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
  70. assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
  71. auto *Symbol = cast<MCSymbolWasm>(S);
  72. // Adding a symbol attribute always introduces the symbol; note that an
  73. // important side effect of calling registerSymbol here is to register the
  74. // symbol with the assembler.
  75. getAssembler().registerSymbol(*Symbol);
  76. switch (Attribute) {
  77. case MCSA_LazyReference:
  78. case MCSA_Reference:
  79. case MCSA_SymbolResolver:
  80. case MCSA_PrivateExtern:
  81. case MCSA_WeakDefinition:
  82. case MCSA_WeakDefAutoPrivate:
  83. case MCSA_Invalid:
  84. case MCSA_IndirectSymbol:
  85. case MCSA_Protected:
  86. return false;
  87. case MCSA_Hidden:
  88. Symbol->setHidden(true);
  89. break;
  90. case MCSA_Weak:
  91. case MCSA_WeakReference:
  92. Symbol->setWeak(true);
  93. Symbol->setExternal(true);
  94. break;
  95. case MCSA_Global:
  96. Symbol->setExternal(true);
  97. break;
  98. case MCSA_ELF_TypeFunction:
  99. Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
  100. break;
  101. case MCSA_ELF_TypeObject:
  102. case MCSA_Cold:
  103. break;
  104. case MCSA_NoDeadStrip:
  105. Symbol->setNoStrip();
  106. break;
  107. default:
  108. // unrecognized directive
  109. llvm_unreachable("unexpected MCSymbolAttr");
  110. return false;
  111. }
  112. return true;
  113. }
  114. void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
  115. unsigned ByteAlignment) {
  116. llvm_unreachable("Common symbols are not yet implemented for Wasm");
  117. }
  118. void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
  119. cast<MCSymbolWasm>(Symbol)->setSize(Value);
  120. }
  121. void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
  122. unsigned ByteAlignment) {
  123. llvm_unreachable("Local common symbols are not yet implemented for Wasm");
  124. }
  125. void MCWasmStreamer::emitIdent(StringRef IdentString) {
  126. // TODO(sbc): Add the ident section once we support mergable strings
  127. // sections in the object format
  128. }
  129. void MCWasmStreamer::emitInstToFragment(const MCInst &Inst,
  130. const MCSubtargetInfo &STI) {
  131. this->MCObjectStreamer::emitInstToFragment(Inst, STI);
  132. }
  133. void MCWasmStreamer::emitInstToData(const MCInst &Inst,
  134. const MCSubtargetInfo &STI) {
  135. MCAssembler &Assembler = getAssembler();
  136. SmallVector<MCFixup, 4> Fixups;
  137. SmallString<256> Code;
  138. raw_svector_ostream VecOS(Code);
  139. Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  140. // Append the encoded instruction to the current data fragment (or create a
  141. // new such fragment if the current fragment is not a data fragment).
  142. MCDataFragment *DF = getOrCreateDataFragment();
  143. // Add the fixups and data.
  144. for (unsigned I = 0, E = Fixups.size(); I != E; ++I) {
  145. Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
  146. DF->getFixups().push_back(Fixups[I]);
  147. }
  148. DF->setHasInstructions(STI);
  149. DF->getContents().append(Code.begin(), Code.end());
  150. }
  151. void MCWasmStreamer::finishImpl() {
  152. emitFrames(nullptr);
  153. this->MCObjectStreamer::finishImpl();
  154. }
  155. MCStreamer *llvm::createWasmStreamer(MCContext &Context,
  156. std::unique_ptr<MCAsmBackend> &&MAB,
  157. std::unique_ptr<MCObjectWriter> &&OW,
  158. std::unique_ptr<MCCodeEmitter> &&CE,
  159. bool RelaxAll) {
  160. MCWasmStreamer *S =
  161. new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
  162. if (RelaxAll)
  163. S->getAssembler().setRelaxAll(true);
  164. return S;
  165. }
  166. void MCWasmStreamer::emitThumbFunc(MCSymbol *Func) {
  167. llvm_unreachable("Generic Wasm doesn't support this directive");
  168. }
  169. void MCWasmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  170. llvm_unreachable("Wasm doesn't support this directive");
  171. }
  172. void MCWasmStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
  173. uint64_t Size, unsigned ByteAlignment,
  174. SMLoc Loc) {
  175. llvm_unreachable("Wasm doesn't support this directive");
  176. }
  177. void MCWasmStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  178. uint64_t Size, unsigned ByteAlignment) {
  179. llvm_unreachable("Wasm doesn't support this directive");
  180. }