1 //=- LoongArchMCInstLower.cpp - Convert LoongArch 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 // This file contains code to lower LoongArch MachineInstrs to their
10 // corresponding MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LoongArch.h"
15 #include "LoongArchSubtarget.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 using namespace llvm;
24 
25 bool llvm::lowerLoongArchMachineOperandToMCOperand(const MachineOperand &MO,
26                                                    MCOperand &MCOp,
27                                                    const AsmPrinter &AP) {
28   switch (MO.getType()) {
29   default:
30     report_fatal_error(
31         "lowerLoongArchMachineOperandToMCOperand: unknown operand type");
32   case MachineOperand::MO_Register:
33     // Ignore all implicit register operands.
34     if (MO.isImplicit())
35       return false;
36     MCOp = MCOperand::createReg(MO.getReg());
37     break;
38   case MachineOperand::MO_RegisterMask:
39     // Regmasks are like implicit defs.
40     return false;
41   case MachineOperand::MO_Immediate:
42     MCOp = MCOperand::createImm(MO.getImm());
43     break;
44   // TODO: lower special operands
45   case MachineOperand::MO_MachineBasicBlock:
46   case MachineOperand::MO_GlobalAddress:
47   case MachineOperand::MO_BlockAddress:
48   case MachineOperand::MO_ExternalSymbol:
49   case MachineOperand::MO_ConstantPoolIndex:
50   case MachineOperand::MO_JumpTableIndex:
51     break;
52   }
53   return true;
54 }
55 
56 bool llvm::lowerLoongArchMachineInstrToMCInst(const MachineInstr *MI,
57                                               MCInst &OutMI, AsmPrinter &AP) {
58   OutMI.setOpcode(MI->getOpcode());
59 
60   for (const MachineOperand &MO : MI->operands()) {
61     MCOperand MCOp;
62     if (lowerLoongArchMachineOperandToMCOperand(MO, MCOp, AP))
63       OutMI.addOperand(MCOp);
64   }
65   return false;
66 }
67