1 //===-- MBlazeMCInstLower.cpp - Convert MBlaze MachineInstr to an 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 MBlaze MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
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"
32 MCSymbol *MBlazeMCInstLower::
33 GetGlobalAddressSymbol(const MachineOperand &MO) const {
34 switch (MO.getTargetFlags()) {
35 default: llvm_unreachable("Unknown target flag on GV operand");
39 return Printer.Mang->getSymbol(MO.getGlobal());
42 MCSymbol *MBlazeMCInstLower::
43 GetExternalSymbolSymbol(const MachineOperand &MO) const {
44 switch (MO.getTargetFlags()) {
45 default: llvm_unreachable("Unknown target flag on GV operand");
49 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
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() << '_'
58 switch (MO.getTargetFlags()) {
59 default: llvm_unreachable("Unknown target flag on GV operand");
63 // Create a symbol for the name.
64 return Ctx.GetOrCreateSymbol(Name.str());
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() << '_'
74 switch (MO.getTargetFlags()) {
76 llvm_unreachable("Unknown target flag on GV operand");
81 // Create a symbol for the name.
82 return Ctx.GetOrCreateSymbol(Name.str());
85 MCSymbol *MBlazeMCInstLower::
86 GetBlockAddressSymbol(const MachineOperand &MO) const {
87 switch (MO.getTargetFlags()) {
89 assert(0 && "Unknown target flag on GV operand");
94 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
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);
103 switch (MO.getTargetFlags()) {
105 llvm_unreachable("Unknown target flag on GV operand");
110 if (!MO.isJTI() && MO.getOffset())
111 Expr = MCBinaryExpr::CreateAdd(Expr,
112 MCConstantExpr::Create(MO.getOffset(), Ctx),
114 return MCOperand::CreateExpr(Expr);
117 void MBlazeMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
118 OutMI.setOpcode(MI->getOpcode());
120 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
121 const MachineOperand &MO = MI->getOperand(i);
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());
131 case MachineOperand::MO_Immediate:
132 MCOp = MCOperand::CreateImm(MO.getImm());
134 case MachineOperand::MO_MachineBasicBlock:
135 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
136 MO.getMBB()->getSymbol(), Ctx));
138 case MachineOperand::MO_GlobalAddress:
139 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
141 case MachineOperand::MO_ExternalSymbol:
142 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
144 case MachineOperand::MO_JumpTableIndex:
145 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
147 case MachineOperand::MO_ConstantPoolIndex:
148 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
150 case MachineOperand::MO_BlockAddress:
151 MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
153 case MachineOperand::MO_FPImmediate:
155 APFloat FVal = MO.getFPImm()->getValueAPF();
156 FVal.convert(APFloat::IEEEsingle, APFloat::rmTowardZero, &ignored);
158 APInt IVal = FVal.bitcastToAPInt();
159 uint64_t Val = *IVal.getRawData();
160 MCOp = MCOperand::CreateImm(Val);
164 OutMI.addOperand(MCOp);