blob: fbdf77e8dc88140a1c171d883999546e9c0d059a [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIMCCodeEmitter.cpp - SI Code Emitter -------------------------------===//
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 The SI code emitter produces machine code that can be executed
12/// directly on the GPU device.
13//
14//===----------------------------------------------------------------------===//
15
16#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17#include "MCTargetDesc/AMDGPUMCCodeEmitter.h"
18#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCContext.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000020#include "llvm/MC/MCFixup.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Support/raw_ostream.h"
26
Tom Stellard75aadc22012-12-11 21:25:42 +000027using namespace llvm;
28
29namespace {
30class SIMCCodeEmitter : public AMDGPUMCCodeEmitter {
31 SIMCCodeEmitter(const SIMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const SIMCCodeEmitter &); // DO NOT IMPLEMENT
33 const MCInstrInfo &MCII;
34 const MCRegisterInfo &MRI;
35 const MCSubtargetInfo &STI;
36 MCContext &Ctx;
37
38public:
39 SIMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri,
40 const MCSubtargetInfo &sti, MCContext &ctx)
41 : MCII(mcii), MRI(mri), STI(sti), Ctx(ctx) { }
42
43 ~SIMCCodeEmitter() { }
44
45 /// \breif Encode the instruction and write it to the OS.
46 virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
47 SmallVectorImpl<MCFixup> &Fixups) const;
48
49 /// \returns the encoding for an MCOperand.
50 virtual uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
51 SmallVectorImpl<MCFixup> &Fixups) const;
52
53public:
54
55 /// \brief Encode a sequence of registers with the correct alignment.
56 unsigned GPRAlign(const MCInst &MI, unsigned OpNo, unsigned shift) const;
57
58 /// \brief Encoding for when 2 consecutive registers are used
59 virtual unsigned GPR2AlignEncode(const MCInst &MI, unsigned OpNo,
60 SmallVectorImpl<MCFixup> &Fixup) const;
61
62 /// \brief Encoding for when 4 consectuive registers are used
63 virtual unsigned GPR4AlignEncode(const MCInst &MI, unsigned OpNo,
64 SmallVectorImpl<MCFixup> &Fixup) const;
Tom Stellard75aadc22012-12-11 21:25:42 +000065};
66
67} // End anonymous namespace
68
69MCCodeEmitter *llvm::createSIMCCodeEmitter(const MCInstrInfo &MCII,
70 const MCRegisterInfo &MRI,
71 const MCSubtargetInfo &STI,
72 MCContext &Ctx) {
73 return new SIMCCodeEmitter(MCII, MRI, STI, Ctx);
74}
75
76void SIMCCodeEmitter::EncodeInstruction(const MCInst &MI, raw_ostream &OS,
77 SmallVectorImpl<MCFixup> &Fixups) const {
78 uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups);
Tom Stellard1c822a82013-02-07 19:39:45 +000079 unsigned bytes = MCII.get(MI.getOpcode()).getSize();
Tom Stellard75aadc22012-12-11 21:25:42 +000080 for (unsigned i = 0; i < bytes; i++) {
81 OS.write((uint8_t) ((Encoding >> (8 * i)) & 0xff));
82 }
83}
84
85uint64_t SIMCCodeEmitter::getMachineOpValue(const MCInst &MI,
86 const MCOperand &MO,
87 SmallVectorImpl<MCFixup> &Fixups) const {
88 if (MO.isReg()) {
Tom Stellard1c822a82013-02-07 19:39:45 +000089 return MRI.getEncodingValue(MO.getReg());
Tom Stellard75aadc22012-12-11 21:25:42 +000090 } else if (MO.isImm()) {
91 return MO.getImm();
92 } else if (MO.isFPImm()) {
93 // XXX: Not all instructions can use inline literals
94 // XXX: We should make sure this is a 32-bit constant
95 union {
96 float F;
97 uint32_t I;
98 } Imm;
99 Imm.F = MO.getFPImm();
100 return Imm.I;
Tom Stellard9e90b582012-12-17 15:14:54 +0000101 } else if (MO.isExpr()) {
102 const MCExpr *Expr = MO.getExpr();
103 MCFixupKind Kind = MCFixupKind(FK_PCRel_4);
104 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
105 return 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000106 } else{
107 llvm_unreachable("Encoding of this operand type is not supported yet.");
108 }
109 return 0;
110}
111
112//===----------------------------------------------------------------------===//
113// Custom Operand Encodings
114//===----------------------------------------------------------------------===//
115
116unsigned SIMCCodeEmitter::GPRAlign(const MCInst &MI, unsigned OpNo,
117 unsigned shift) const {
Tom Stellard1c822a82013-02-07 19:39:45 +0000118 unsigned regCode = MRI.getEncodingValue(MI.getOperand(OpNo).getReg());
119 return (regCode & 0xff) >> shift;
Tom Stellard75aadc22012-12-11 21:25:42 +0000120}
121unsigned SIMCCodeEmitter::GPR2AlignEncode(const MCInst &MI,
122 unsigned OpNo ,
123 SmallVectorImpl<MCFixup> &Fixup) const {
124 return GPRAlign(MI, OpNo, 1);
125}
126
127unsigned SIMCCodeEmitter::GPR4AlignEncode(const MCInst &MI,
128 unsigned OpNo,
129 SmallVectorImpl<MCFixup> &Fixup) const {
130 return GPRAlign(MI, OpNo, 2);
131}