1 //===-- MBLazeMCInstLower.cpp - Convert MBlaze MachineInstr to an 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 MBlaze MachineInstrs to their corresponding 11 // MCInst records. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "MBlazeMCInstLower.h" 16 #include "MBlazeInstrInfo.h" 17 #include "llvm/Constants.h" 18 #include "llvm/CodeGen/AsmPrinter.h" 19 #include "llvm/CodeGen/MachineBasicBlock.h" 20 #include "llvm/CodeGen/MachineInstr.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 #include "llvm/Target/Mangler.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include "llvm/ADT/SmallString.h" 30 using namespace llvm; 31 32 MCSymbol *MBlazeMCInstLower:: 33 GetGlobalAddressSymbol(const MachineOperand &MO) const { 34 switch (MO.getTargetFlags()) { 35 default: llvm_unreachable("Unknown target flag on GV operand"); 36 case 0: break; 37 } 38 39 return Printer.Mang->getSymbol(MO.getGlobal()); 40 } 41 42 MCSymbol *MBlazeMCInstLower:: 43 GetExternalSymbolSymbol(const MachineOperand &MO) const { 44 switch (MO.getTargetFlags()) { 45 default: llvm_unreachable("Unknown target flag on GV operand"); 46 case 0: break; 47 } 48 49 return Printer.GetExternalSymbolSymbol(MO.getSymbolName()); 50 } 51 52 MCSymbol *MBlazeMCInstLower:: 53 GetJumpTableSymbol(const MachineOperand &MO) const { 54 SmallString<256> Name; 55 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI" 56 << Printer.getFunctionNumber() << '_' 57 << MO.getIndex(); 58 switch (MO.getTargetFlags()) { 59 default: llvm_unreachable("Unknown target flag on GV operand"); 60 case 0: break; 61 } 62 63 // Create a symbol for the name. 64 return Ctx.GetOrCreateSymbol(Name.str()); 65 } 66 67 MCSymbol *MBlazeMCInstLower:: 68 GetConstantPoolIndexSymbol(const MachineOperand &MO) const { 69 SmallString<256> Name; 70 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI" 71 << Printer.getFunctionNumber() << '_' 72 << MO.getIndex(); 73 74 switch (MO.getTargetFlags()) { 75 default: 76 llvm_unreachable("Unknown target flag on GV operand"); 77 78 case 0: break; 79 } 80 81 // Create a symbol for the name. 82 return Ctx.GetOrCreateSymbol(Name.str()); 83 } 84 85 MCSymbol *MBlazeMCInstLower:: 86 GetBlockAddressSymbol(const MachineOperand &MO) const { 87 switch (MO.getTargetFlags()) { 88 default: 89 assert(0 && "Unknown target flag on GV operand"); 90 91 case 0: break; 92 } 93 94 return Printer.GetBlockAddressSymbol(MO.getBlockAddress()); 95 } 96 97 MCOperand MBlazeMCInstLower:: 98 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const { 99 // FIXME: We would like an efficient form for this, so we don't have to do a 100 // lot of extra uniquing. 101 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx); 102 103 switch (MO.getTargetFlags()) { 104 default: 105 llvm_unreachable("Unknown target flag on GV operand"); 106 107 case 0: break; 108 } 109 110 if (!MO.isJTI() && MO.getOffset()) 111 Expr = MCBinaryExpr::CreateAdd(Expr, 112 MCConstantExpr::Create(MO.getOffset(), Ctx), 113 Ctx); 114 return MCOperand::CreateExpr(Expr); 115 } 116 117 void MBlazeMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { 118 OutMI.setOpcode(MI->getOpcode()); 119 120 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 121 const MachineOperand &MO = MI->getOperand(i); 122 123 MCOperand MCOp; 124 switch (MO.getType()) { 125 default: llvm_unreachable("unknown operand type"); 126 case MachineOperand::MO_Register: 127 // Ignore all implicit register operands. 128 if (MO.isImplicit()) continue; 129 MCOp = MCOperand::CreateReg(MO.getReg()); 130 break; 131 case MachineOperand::MO_Immediate: 132 MCOp = MCOperand::CreateImm(MO.getImm()); 133 break; 134 case MachineOperand::MO_MachineBasicBlock: 135 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create( 136 MO.getMBB()->getSymbol(), Ctx)); 137 break; 138 case MachineOperand::MO_GlobalAddress: 139 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO)); 140 break; 141 case MachineOperand::MO_ExternalSymbol: 142 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO)); 143 break; 144 case MachineOperand::MO_JumpTableIndex: 145 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO)); 146 break; 147 case MachineOperand::MO_ConstantPoolIndex: 148 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO)); 149 break; 150 case MachineOperand::MO_BlockAddress: 151 MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO)); 152 break; 153 case MachineOperand::MO_FPImmediate: 154 bool ignored; 155 APFloat FVal = MO.getFPImm()->getValueAPF(); 156 FVal.convert(APFloat::IEEEsingle, APFloat::rmTowardZero, &ignored); 157 158 APInt IVal = FVal.bitcastToAPInt(); 159 uint64_t Val = *IVal.getRawData(); 160 MCOp = MCOperand::CreateImm(Val); 161 break; 162 } 163 164 OutMI.addOperand(MCOp); 165 } 166 } 167