1 //===-- SparcMCInstLower.cpp - Convert Sparc MachineInstr to MCInst -------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains code to lower Sparc MachineInstrs to their corresponding 11 // MCInst records. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "Sparc.h" 16 #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" 26 27 using namespace llvm; 28 29 30 static MCOperand LowerSymbolOperand(const MachineInstr *MI, 31 const MachineOperand &MO, 32 AsmPrinter &AP) { 33 34 SparcMCExpr::VariantKind Kind = 35 (SparcMCExpr::VariantKind)MO.getTargetFlags(); 36 const MCSymbol *Symbol = nullptr; 37 38 switch(MO.getType()) { 39 default: llvm_unreachable("Unknown type in LowerSymbolOperand"); 40 case MachineOperand::MO_MachineBasicBlock: 41 Symbol = MO.getMBB()->getSymbol(); 42 break; 43 44 case MachineOperand::MO_GlobalAddress: 45 Symbol = AP.getSymbol(MO.getGlobal()); 46 break; 47 48 case MachineOperand::MO_BlockAddress: 49 Symbol = AP.GetBlockAddressSymbol(MO.getBlockAddress()); 50 break; 51 52 case MachineOperand::MO_ExternalSymbol: 53 Symbol = AP.GetExternalSymbolSymbol(MO.getSymbolName()); 54 break; 55 56 case MachineOperand::MO_ConstantPoolIndex: 57 Symbol = AP.GetCPISymbol(MO.getIndex()); 58 break; 59 } 60 61 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, 62 AP.OutContext); 63 const SparcMCExpr *expr = SparcMCExpr::create(Kind, MCSym, 64 AP.OutContext); 65 return MCOperand::createExpr(expr); 66 } 67 68 static MCOperand LowerOperand(const MachineInstr *MI, 69 const MachineOperand &MO, 70 AsmPrinter &AP) { 71 switch(MO.getType()) { 72 default: llvm_unreachable("unknown operand type"); break; 73 case MachineOperand::MO_Register: 74 if (MO.isImplicit()) 75 break; 76 return MCOperand::createReg(MO.getReg()); 77 78 case MachineOperand::MO_Immediate: 79 return MCOperand::createImm(MO.getImm()); 80 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); 87 88 case MachineOperand::MO_RegisterMask: break; 89 90 } 91 return MCOperand(); 92 } 93 94 void llvm::LowerSparcMachineInstrToMCInst(const MachineInstr *MI, 95 MCInst &OutMI, 96 AsmPrinter &AP) 97 { 98 99 OutMI.setOpcode(MI->getOpcode()); 100 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); 104 105 if (MCOp.isValid()) 106 OutMI.addOperand(MCOp); 107 } 108 } 109