Removing LLVM_DELETED_FUNCTION, as MSVC 2012 was the last reason for requiring the...
[oota-llvm.git] / lib / Target / BPF / MCTargetDesc / BPFMCCodeEmitter.cpp
1 //===-- BPFMCCodeEmitter.cpp - Convert BPF 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 // This file implements the BPFMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MCTargetDesc/BPFMCTargetDesc.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstrInfo.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25
26 #define DEBUG_TYPE "mccodeemitter"
27
28 namespace {
29 class BPFMCCodeEmitter : public MCCodeEmitter {
30   BPFMCCodeEmitter(const BPFMCCodeEmitter &) = delete;
31   void operator=(const BPFMCCodeEmitter &) = delete;
32   const MCRegisterInfo &MRI;
33
34 public:
35   BPFMCCodeEmitter(const MCRegisterInfo &mri) : MRI(mri) {}
36
37   ~BPFMCCodeEmitter() {}
38
39   // getBinaryCodeForInstr - TableGen'erated function for getting the
40   // binary encoding for an instruction.
41   uint64_t getBinaryCodeForInstr(const MCInst &MI,
42                                  SmallVectorImpl<MCFixup> &Fixups,
43                                  const MCSubtargetInfo &STI) const;
44
45   // getMachineOpValue - Return binary encoding of operand. If the machin
46   // operand requires relocation, record the relocation and return zero.
47   unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
48                              SmallVectorImpl<MCFixup> &Fixups,
49                              const MCSubtargetInfo &STI) const;
50
51   uint64_t getMemoryOpValue(const MCInst &MI, unsigned Op,
52                             SmallVectorImpl<MCFixup> &Fixups,
53                             const MCSubtargetInfo &STI) const;
54
55   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
56                          SmallVectorImpl<MCFixup> &Fixups,
57                          const MCSubtargetInfo &STI) const override;
58 };
59 }
60
61 MCCodeEmitter *llvm::createBPFMCCodeEmitter(const MCInstrInfo &MCII,
62                                             const MCRegisterInfo &MRI,
63                                             const MCSubtargetInfo &STI,
64                                             MCContext &Ctx) {
65   return new BPFMCCodeEmitter(MRI);
66 }
67
68 unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI,
69                                              const MCOperand &MO,
70                                              SmallVectorImpl<MCFixup> &Fixups,
71                                              const MCSubtargetInfo &STI) const {
72   if (MO.isReg())
73     return MRI.getEncodingValue(MO.getReg());
74   if (MO.isImm())
75     return static_cast<unsigned>(MO.getImm());
76
77   assert(MO.isExpr());
78
79   const MCExpr *Expr = MO.getExpr();
80   MCExpr::ExprKind Kind = Expr->getKind();
81
82   assert(Kind == MCExpr::SymbolRef);
83
84   if (MI.getOpcode() == BPF::JAL)
85     // func call name
86     Fixups.push_back(MCFixup::Create(0, Expr, FK_SecRel_4));
87   else if (MI.getOpcode() == BPF::LD_imm64)
88     Fixups.push_back(MCFixup::Create(0, Expr, FK_SecRel_8));
89   else
90     // bb label
91     Fixups.push_back(MCFixup::Create(0, Expr, FK_PCRel_2));
92
93   return 0;
94 }
95
96 // Emit one byte through output stream
97 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) {
98   OS << (char)C;
99   ++CurByte;
100 }
101
102 // Emit a series of bytes (little endian)
103 void EmitLEConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
104                     raw_ostream &OS) {
105   assert(Size <= 8 && "size too big in emit constant");
106
107   for (unsigned i = 0; i != Size; ++i) {
108     EmitByte(Val & 255, CurByte, OS);
109     Val >>= 8;
110   }
111 }
112
113 // Emit a series of bytes (big endian)
114 void EmitBEConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
115                     raw_ostream &OS) {
116   assert(Size <= 8 && "size too big in emit constant");
117
118   for (int i = (Size - 1) * 8; i >= 0; i -= 8)
119     EmitByte((Val >> i) & 255, CurByte, OS);
120 }
121
122 void BPFMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
123                                          SmallVectorImpl<MCFixup> &Fixups,
124                                          const MCSubtargetInfo &STI) const {
125   unsigned Opcode = MI.getOpcode();
126   // Keep track of the current byte being emitted
127   unsigned CurByte = 0;
128
129   if (Opcode == BPF::LD_imm64) {
130     uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI);
131     EmitByte(Value >> 56, CurByte, OS);
132     EmitByte(((Value >> 48) & 0xff), CurByte, OS);
133     EmitLEConstant(0, 2, CurByte, OS);
134     EmitLEConstant(Value & 0xffffFFFF, 4, CurByte, OS);
135
136     const MCOperand &MO = MI.getOperand(1);
137     uint64_t Imm = MO.isImm() ? MO.getImm() : 0;
138     EmitByte(0, CurByte, OS);
139     EmitByte(0, CurByte, OS);
140     EmitLEConstant(0, 2, CurByte, OS);
141     EmitLEConstant(Imm >> 32, 4, CurByte, OS);
142   } else {
143     // Get instruction encoding and emit it
144     uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI);
145     EmitByte(Value >> 56, CurByte, OS);
146     EmitByte((Value >> 48) & 0xff, CurByte, OS);
147     EmitLEConstant((Value >> 32) & 0xffff, 2, CurByte, OS);
148     EmitLEConstant(Value & 0xffffFFFF, 4, CurByte, OS);
149   }
150 }
151
152 // Encode BPF Memory Operand
153 uint64_t BPFMCCodeEmitter::getMemoryOpValue(const MCInst &MI, unsigned Op,
154                                             SmallVectorImpl<MCFixup> &Fixups,
155                                             const MCSubtargetInfo &STI) const {
156   uint64_t Encoding;
157   const MCOperand Op1 = MI.getOperand(1);
158   assert(Op1.isReg() && "First operand is not register.");
159   Encoding = MRI.getEncodingValue(Op1.getReg());
160   Encoding <<= 16;
161   MCOperand Op2 = MI.getOperand(2);
162   assert(Op2.isImm() && "Second operand is not immediate.");
163   Encoding |= Op2.getImm() & 0xffff;
164   return Encoding;
165 }
166
167 #include "BPFGenMCCodeEmitter.inc"