1 //===-- SparcMCInstLower.cpp - Convert Sparc MachineInstr to MCInst -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains code to lower Sparc MachineInstrs to their corresponding
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/SparcMCExpr.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"
29 static MCOperand
LowerSymbolOperand(const MachineInstr
*MI
,
30 const MachineOperand
&MO
,
33 SparcMCExpr::VariantKind Kind
=
34 (SparcMCExpr::VariantKind
)MO
.getTargetFlags();
35 const MCSymbol
*Symbol
= nullptr;
37 switch(MO
.getType()) {
38 default: llvm_unreachable("Unknown type in LowerSymbolOperand");
39 case MachineOperand::MO_MachineBasicBlock
:
40 Symbol
= MO
.getMBB()->getSymbol();
43 case MachineOperand::MO_GlobalAddress
:
44 Symbol
= AP
.getSymbol(MO
.getGlobal());
47 case MachineOperand::MO_BlockAddress
:
48 Symbol
= AP
.GetBlockAddressSymbol(MO
.getBlockAddress());
51 case MachineOperand::MO_ExternalSymbol
:
52 Symbol
= AP
.GetExternalSymbolSymbol(MO
.getSymbolName());
55 case MachineOperand::MO_ConstantPoolIndex
:
56 Symbol
= AP
.GetCPISymbol(MO
.getIndex());
60 const MCSymbolRefExpr
*MCSym
= MCSymbolRefExpr::create(Symbol
,
62 const SparcMCExpr
*expr
= SparcMCExpr::create(Kind
, MCSym
,
64 return MCOperand::createExpr(expr
);
67 static MCOperand
LowerOperand(const MachineInstr
*MI
,
68 const MachineOperand
&MO
,
70 switch(MO
.getType()) {
71 default: llvm_unreachable("unknown operand type"); break;
72 case MachineOperand::MO_Register
:
75 return MCOperand::createReg(MO
.getReg());
77 case MachineOperand::MO_Immediate
:
78 return MCOperand::createImm(MO
.getImm());
80 case MachineOperand::MO_MachineBasicBlock
:
81 case MachineOperand::MO_GlobalAddress
:
82 case MachineOperand::MO_BlockAddress
:
83 case MachineOperand::MO_ExternalSymbol
:
84 case MachineOperand::MO_ConstantPoolIndex
:
85 return LowerSymbolOperand(MI
, MO
, AP
);
87 case MachineOperand::MO_RegisterMask
: break;
93 void llvm::LowerSparcMachineInstrToMCInst(const MachineInstr
*MI
,
98 OutMI
.setOpcode(MI
->getOpcode());
100 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
101 const MachineOperand
&MO
= MI
->getOperand(i
);
102 MCOperand MCOp
= LowerOperand(MI
, MO
, AP
);
105 OutMI
.addOperand(MCOp
);