2c9909ff9d94ce096ab0914c9c597c4fdd9fce96
[oota-llvm.git] / lib / Target / R600 / AMDGPUMCInstLower.cpp
1 //===- AMDGPUMCInstLower.cpp - Lower AMDGPU MachineInstr to an MCInst -----===//
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 Code to lower AMDGPU MachineInstrs to their corresponding MCInst.
12 //
13 //===----------------------------------------------------------------------===//
14 //
15
16 #include "AMDGPUMCInstLower.h"
17 #include "AMDGPUAsmPrinter.h"
18 #include "InstPrinter/AMDGPUInstPrinter.h"
19 #include "R600InstrInfo.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCObjectStreamer.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/Format.h"
30 #include <algorithm>
31
32 using namespace llvm;
33
34 AMDGPUMCInstLower::AMDGPUMCInstLower(MCContext &ctx):
35   Ctx(ctx)
36 { }
37
38 void AMDGPUMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
39   OutMI.setOpcode(MI->getOpcode());
40
41   for (unsigned i = 0, e = MI->getNumExplicitOperands(); i != e; ++i) {
42     const MachineOperand &MO = MI->getOperand(i);
43
44     MCOperand MCOp;
45     switch (MO.getType()) {
46     default:
47       llvm_unreachable("unknown operand type");
48     case MachineOperand::MO_FPImmediate: {
49       const APFloat &FloatValue = MO.getFPImm()->getValueAPF();
50       assert(&FloatValue.getSemantics() == &APFloat::IEEEsingle &&
51              "Only floating point immediates are supported at the moment.");
52       MCOp = MCOperand::CreateFPImm(FloatValue.convertToFloat());
53       break;
54     }
55     case MachineOperand::MO_Immediate:
56       MCOp = MCOperand::CreateImm(MO.getImm());
57       break;
58     case MachineOperand::MO_Register:
59       MCOp = MCOperand::CreateReg(MO.getReg());
60       break;
61     case MachineOperand::MO_MachineBasicBlock:
62       MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
63                                    MO.getMBB()->getSymbol(), Ctx));
64     }
65     OutMI.addOperand(MCOp);
66   }
67 }
68
69 void AMDGPUAsmPrinter::EmitInstruction(const MachineInstr *MI) {
70   AMDGPUMCInstLower MCInstLowering(OutContext);
71
72 #ifdef _DEBUG
73   StringRef Err;
74   if (!TM.getInstrInfo()->verifyInstruction(MI, Err)) {
75     errs() << "Warning: Illegal instruction detected: " << Err << "\n";
76     MI->dump();
77   }
78 #endif
79   if (MI->isBundle()) {
80     const MachineBasicBlock *MBB = MI->getParent();
81     MachineBasicBlock::const_instr_iterator I = MI;
82     ++I;
83     while (I != MBB->end() && I->isInsideBundle()) {
84       EmitInstruction(I);
85       ++I;
86     }
87   } else {
88     MCInst TmpInst;
89     MCInstLowering.lower(MI, TmpInst);
90     EmitToStreamer(OutStreamer, TmpInst);
91
92     if (DisasmEnabled) {
93       // Disassemble instruction/operands to text.
94       DisasmLines.resize(DisasmLines.size() + 1);
95       std::string &DisasmLine = DisasmLines.back();
96       raw_string_ostream DisasmStream(DisasmLine);
97
98       AMDGPUInstPrinter InstPrinter(*TM.getMCAsmInfo(), *TM.getInstrInfo(),
99                                     *TM.getRegisterInfo());
100       InstPrinter.printInst(&TmpInst, DisasmStream, StringRef());
101
102       // Disassemble instruction/operands to hex representation.
103       SmallVector<MCFixup, 4> Fixups;
104       SmallVector<char, 16> CodeBytes;
105       raw_svector_ostream CodeStream(CodeBytes);
106
107       MCObjectStreamer &ObjStreamer = (MCObjectStreamer &)OutStreamer;
108       MCCodeEmitter &InstEmitter = ObjStreamer.getAssembler().getEmitter();
109       InstEmitter.EncodeInstruction(TmpInst, CodeStream, Fixups,
110                                     TM.getSubtarget<MCSubtargetInfo>());
111       CodeStream.flush();
112
113       HexLines.resize(HexLines.size() + 1);
114       std::string &HexLine = HexLines.back();
115       raw_string_ostream HexStream(HexLine);
116
117       for (size_t i = 0; i < CodeBytes.size(); i += 4) {
118         unsigned int CodeDWord = *(unsigned int *)&CodeBytes[i];
119         HexStream << format("%s%08X", (i > 0 ? " " : ""), CodeDWord);
120       }
121
122       DisasmStream.flush();
123       DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLine.size());
124     }
125   }
126 }