1 //=- WebAssemblyMCCodeEmitter.cpp - Convert WebAssembly code to machine code -//
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 /// \file
11 /// \brief This file implements the WebAssemblyMCCodeEmitter class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCFixup.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCSubtargetInfo.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "mccodeemitter"
28 
29 namespace {
30 class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
31   const MCRegisterInfo &MRI;
32 
33 public:
WebAssemblyMCCodeEmitter(const MCInstrInfo &,const MCRegisterInfo & mri,MCContext &)34   WebAssemblyMCCodeEmitter(const MCInstrInfo &, const MCRegisterInfo &mri,
35                            MCContext &)
36       : MRI(mri) {}
37 
~WebAssemblyMCCodeEmitter()38   ~WebAssemblyMCCodeEmitter() override {}
39 
40   /// TableGen'erated function for getting the binary encoding for an
41   /// instruction.
42   uint64_t getBinaryCodeForInstr(const MCInst &MI,
43                                  SmallVectorImpl<MCFixup> &Fixups,
44                                  const MCSubtargetInfo &STI) const;
45 
46   /// Return binary encoding of operand. If the machine operand requires
47   /// relocation, record the relocation and return zero.
48   unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
49                              SmallVectorImpl<MCFixup> &Fixups,
50                              const MCSubtargetInfo &STI) const;
51 
52   uint64_t getMemoryOpValue(const MCInst &MI, unsigned Op,
53                             SmallVectorImpl<MCFixup> &Fixups,
54                             const MCSubtargetInfo &STI) const;
55 
56   void encodeInstruction(const MCInst &MI, raw_ostream &OS,
57                          SmallVectorImpl<MCFixup> &Fixups,
58                          const MCSubtargetInfo &STI) const override;
59 };
60 } // end anonymous namespace
61 
createWebAssemblyMCCodeEmitter(const MCInstrInfo & MCII,const MCRegisterInfo & MRI,MCContext & Ctx)62 MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
63                                                     const MCRegisterInfo &MRI,
64                                                     MCContext &Ctx) {
65   return new WebAssemblyMCCodeEmitter(MCII, MRI, Ctx);
66 }
67 
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const68 unsigned WebAssemblyMCCodeEmitter::getMachineOpValue(
69     const MCInst &MI, const MCOperand &MO, SmallVectorImpl<MCFixup> &Fixups,
70     const MCSubtargetInfo &STI) const {
71   if (MO.isReg())
72     return MRI.getEncodingValue(MO.getReg());
73   if (MO.isImm())
74     return static_cast<unsigned>(MO.getImm());
75 
76   assert(MO.isExpr());
77 
78   assert(MO.getExpr()->getKind() == MCExpr::SymbolRef);
79 
80   assert(false && "FIXME: not implemented yet");
81 
82   return 0;
83 }
84 
encodeInstruction(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const85 void WebAssemblyMCCodeEmitter::encodeInstruction(
86     const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
87     const MCSubtargetInfo &STI) const {
88   assert(false && "FIXME: not implemented yet");
89 }
90 
91 // Encode WebAssembly Memory Operand
92 uint64_t
getMemoryOpValue(const MCInst & MI,unsigned Op,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const93 WebAssemblyMCCodeEmitter::getMemoryOpValue(const MCInst &MI, unsigned Op,
94                                            SmallVectorImpl<MCFixup> &Fixups,
95                                            const MCSubtargetInfo &STI) const {
96   assert(false && "FIXME: not implemented yet");
97   return 0;
98 }
99 
100 #include "WebAssemblyGenMCCodeEmitter.inc"
101