xref: /freebsd/contrib/llvm-project/llvm/lib/Target/VE/VEMCInstLower.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===-- VEMCInstLower.cpp - Convert VE MachineInstr to 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 VE MachineInstrs to their corresponding
10 // MCInst records.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MCTargetDesc/VEMCExpr.h"
15 #include "VE.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 
26 using namespace llvm;
27 
28 static MCOperand LowerSymbolOperand(const MachineInstr *MI,
29                                     const MachineOperand &MO,
30                                     const MCSymbol *Symbol, AsmPrinter &AP) {
31   VEMCExpr::VariantKind Kind = (VEMCExpr::VariantKind)MO.getTargetFlags();
32 
33   const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, AP.OutContext);
34   // Add offset iff MO is not jump table info or machine basic block.
35   if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
36     Expr = MCBinaryExpr::createAdd(
37         Expr, MCConstantExpr::create(MO.getOffset(), AP.OutContext),
38         AP.OutContext);
39   Expr = VEMCExpr::create(Kind, Expr, AP.OutContext);
40   return MCOperand::createExpr(Expr);
41 }
42 
43 static MCOperand LowerOperand(const MachineInstr *MI, const MachineOperand &MO,
44                               AsmPrinter &AP) {
45   switch (MO.getType()) {
46   default:
47     report_fatal_error("unsupported operand type");
48 
49   case MachineOperand::MO_Register:
50     if (MO.isImplicit())
51       break;
52     return MCOperand::createReg(MO.getReg());
53 
54   case MachineOperand::MO_ExternalSymbol:
55     return LowerSymbolOperand(
56         MI, MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
57   case MachineOperand::MO_GlobalAddress:
58     return LowerSymbolOperand(MI, MO, AP.getSymbol(MO.getGlobal()), AP);
59   case MachineOperand::MO_Immediate:
60     return MCOperand::createImm(MO.getImm());
61 
62   case MachineOperand::MO_MachineBasicBlock:
63     return LowerSymbolOperand(MI, MO, MO.getMBB()->getSymbol(), AP);
64 
65   case MachineOperand::MO_RegisterMask:
66     break;
67   }
68   return MCOperand();
69 }
70 
71 void llvm::LowerVEMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
72                                        AsmPrinter &AP) {
73   OutMI.setOpcode(MI->getOpcode());
74 
75   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
76     const MachineOperand &MO = MI->getOperand(i);
77     MCOperand MCOp = LowerOperand(MI, MO, AP);
78 
79     if (MCOp.isValid())
80       OutMI.addOperand(MCOp);
81   }
82 }
83