1 //===- R600MCInstLower.cpp - Lower R600 MachineInstr to an MCInst ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Code to lower R600 MachineInstrs to their corresponding MCInst.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 
15 #include "AMDGPUMCInstLower.h"
16 #include "MCTargetDesc/R600MCTargetDesc.h"
17 #include "R600AsmPrinter.h"
18 #include "R600Subtarget.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 
23 class R600MCInstLower : public AMDGPUMCInstLower {
24 public:
25   R600MCInstLower(MCContext &ctx, const R600Subtarget &ST,
26                   const AsmPrinter &AP);
27 
28   /// Lower a MachineInstr to an MCInst
29   void lower(const MachineInstr *MI, MCInst &OutMI) const;
30 };
31 
32 R600MCInstLower::R600MCInstLower(MCContext &Ctx, const R600Subtarget &ST,
33                                  const AsmPrinter &AP)
34     : AMDGPUMCInstLower(Ctx, ST, AP) {}
35 
36 void R600MCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
37   OutMI.setOpcode(MI->getOpcode());
38   for (const MachineOperand &MO : MI->explicit_operands()) {
39     MCOperand MCOp;
40     lowerOperand(MO, MCOp);
41     OutMI.addOperand(MCOp);
42   }
43 }
44 
45 void R600AsmPrinter::emitInstruction(const MachineInstr *MI) {
46   R600_MC::verifyInstructionPredicates(MI->getOpcode(),
47                                        getSubtargetInfo().getFeatureBits());
48 
49   const R600Subtarget &STI = MF->getSubtarget<R600Subtarget>();
50   R600MCInstLower MCInstLowering(OutContext, STI, *this);
51 
52   StringRef Err;
53   if (!STI.getInstrInfo()->verifyInstruction(*MI, Err)) {
54     LLVMContext &C = MI->getParent()->getParent()->getFunction().getContext();
55     C.emitError("Illegal instruction detected: " + Err);
56     MI->print(errs());
57   }
58 
59   if (MI->isBundle()) {
60     const MachineBasicBlock *MBB = MI->getParent();
61     MachineBasicBlock::const_instr_iterator I = ++MI->getIterator();
62     while (I != MBB->instr_end() && I->isInsideBundle()) {
63       emitInstruction(&*I);
64       ++I;
65     }
66   } else {
67     MCInst TmpInst;
68     MCInstLowering.lower(MI, TmpInst);
69     EmitToStreamer(*OutStreamer, TmpInst);
70   }
71 }
72 
73 const MCExpr *R600AsmPrinter::lowerConstant(const Constant *CV) {
74   if (const MCExpr *E = lowerAddrSpaceCast(TM, CV, OutContext))
75     return E;
76   return AsmPrinter::lowerConstant(CV);
77 }
78