1 //===-- SparcMCInstLower.cpp - Convert Sparc MachineInstr to MCInst -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower Sparc MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "MCTargetDesc/SparcMCExpr.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineOperand.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
30 static MCOperand LowerSymbolOperand(const MachineInstr *MI,
31 const MachineOperand &MO,
34 SparcMCExpr::VariantKind Kind =
35 (SparcMCExpr::VariantKind)MO.getTargetFlags();
36 const MCSymbol *Symbol = nullptr;
38 switch(MO.getType()) {
39 default: llvm_unreachable("Unknown type in LowerSymbolOperand");
40 case MachineOperand::MO_MachineBasicBlock:
41 Symbol = MO.getMBB()->getSymbol();
44 case MachineOperand::MO_GlobalAddress:
45 Symbol = AP.getSymbol(MO.getGlobal());
48 case MachineOperand::MO_BlockAddress:
49 Symbol = AP.GetBlockAddressSymbol(MO.getBlockAddress());
52 case MachineOperand::MO_ExternalSymbol:
53 Symbol = AP.GetExternalSymbolSymbol(MO.getSymbolName());
56 case MachineOperand::MO_ConstantPoolIndex:
57 Symbol = AP.GetCPISymbol(MO.getIndex());
61 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol,
63 const SparcMCExpr *expr = SparcMCExpr::create(Kind, MCSym,
65 return MCOperand::createExpr(expr);
68 static MCOperand LowerOperand(const MachineInstr *MI,
69 const MachineOperand &MO,
71 switch(MO.getType()) {
72 default: llvm_unreachable("unknown operand type"); break;
73 case MachineOperand::MO_Register:
76 return MCOperand::createReg(MO.getReg());
78 case MachineOperand::MO_Immediate:
79 return MCOperand::createImm(MO.getImm());
81 case MachineOperand::MO_MachineBasicBlock:
82 case MachineOperand::MO_GlobalAddress:
83 case MachineOperand::MO_BlockAddress:
84 case MachineOperand::MO_ExternalSymbol:
85 case MachineOperand::MO_ConstantPoolIndex:
86 return LowerSymbolOperand(MI, MO, AP);
88 case MachineOperand::MO_RegisterMask: break;
94 void llvm::LowerSparcMachineInstrToMCInst(const MachineInstr *MI,
99 OutMI.setOpcode(MI->getOpcode());
101 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
102 const MachineOperand &MO = MI->getOperand(i);
103 MCOperand MCOp = LowerOperand(MI, MO, AP);
106 OutMI.addOperand(MCOp);