Taints the non-acquire RMW's store address with the load part
[oota-llvm.git] / lib / Target / WebAssembly / MCTargetDesc / WebAssemblyMCCodeEmitter.cpp
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/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
27
28 #define DEBUG_TYPE "mccodeemitter"
29
30 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
31 STATISTIC(MCNumFixups, "Number of MC fixups created.");
32
33 namespace {
34 class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
35   const MCInstrInfo &MCII;
36   const MCContext &Ctx;
37
38   // Implementation generated by tablegen.
39   uint64_t getBinaryCodeForInstr(const MCInst &MI,
40                                  SmallVectorImpl<MCFixup> &Fixups,
41                                  const MCSubtargetInfo &STI) const;
42
43   void encodeInstruction(const MCInst &MI, raw_ostream &OS,
44                          SmallVectorImpl<MCFixup> &Fixups,
45                          const MCSubtargetInfo &STI) const override;
46
47 public:
48   WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
49       : MCII(mcii), Ctx(ctx) {}
50 };
51 } // end anonymous namespace
52
53 MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
54                                                     MCContext &Ctx) {
55   return new WebAssemblyMCCodeEmitter(MCII, Ctx);
56 }
57
58 void WebAssemblyMCCodeEmitter::encodeInstruction(
59     const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
60     const MCSubtargetInfo &STI) const {
61   // FIXME: This is not the real binary encoding. This is an extremely
62   // over-simplified encoding where we just use uint64_t for everything. This
63   // is a temporary measure.
64   support::endian::Writer<support::little>(OS).write<uint64_t>(MI.getOpcode());
65   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
66   if (Desc.isVariadic())
67     support::endian::Writer<support::little>(OS).write<uint64_t>(
68         MI.getNumOperands() - Desc.NumOperands);
69   for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) {
70     const MCOperand &MO = MI.getOperand(i);
71     if (MO.isReg()) {
72       support::endian::Writer<support::little>(OS).write<uint64_t>(MO.getReg());
73     } else if (MO.isImm()) {
74       support::endian::Writer<support::little>(OS).write<uint64_t>(MO.getImm());
75     } else if (MO.isFPImm()) {
76       support::endian::Writer<support::little>(OS).write<double>(MO.getFPImm());
77     } else if (MO.isExpr()) {
78       support::endian::Writer<support::little>(OS).write<uint64_t>(0);
79       Fixups.push_back(MCFixup::create(
80           (1 + MCII.get(MI.getOpcode()).isVariadic() + i) * sizeof(uint64_t),
81           MO.getExpr(), STI.getTargetTriple().isArch64Bit() ? FK_Data_8 : FK_Data_4,
82           MI.getLoc()));
83       ++MCNumFixups;
84     } else {
85       llvm_unreachable("unexpected operand kind");
86     }
87   }
88
89   ++MCNumEmitted; // Keep track of the # of mi's emitted.
90 }
91
92 #include "WebAssemblyGenMCCodeEmitter.inc"