• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ADT/SmallString.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/IR/Constants.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 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/Mangler.h"
30 using namespace llvm;
31 
32 MCSymbol *MBlazeMCInstLower::
GetGlobalAddressSymbol(const MachineOperand & MO) const33 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::
GetExternalSymbolSymbol(const MachineOperand & MO) const43 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::
GetJumpTableSymbol(const MachineOperand & MO) const53 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::
GetConstantPoolIndexSymbol(const MachineOperand & MO) const68 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::
GetBlockAddressSymbol(const MachineOperand & MO) const86 GetBlockAddressSymbol(const MachineOperand &MO) const {
87   switch (MO.getTargetFlags()) {
88   default: llvm_unreachable("Unknown target flag on GV operand");
89   case 0: break;
90   }
91 
92   return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
93 }
94 
95 MCOperand MBlazeMCInstLower::
LowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym) const96 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
97   // FIXME: We would like an efficient form for this, so we don't have to do a
98   // lot of extra uniquing.
99   const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
100 
101   switch (MO.getTargetFlags()) {
102   default:
103       llvm_unreachable("Unknown target flag on GV operand");
104 
105   case 0: break;
106   }
107 
108   if (!MO.isJTI() && MO.getOffset())
109     Expr = MCBinaryExpr::CreateAdd(Expr,
110                                    MCConstantExpr::Create(MO.getOffset(), Ctx),
111                                    Ctx);
112   return MCOperand::CreateExpr(Expr);
113 }
114 
Lower(const MachineInstr * MI,MCInst & OutMI) const115 void MBlazeMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
116   OutMI.setOpcode(MI->getOpcode());
117 
118   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
119     const MachineOperand &MO = MI->getOperand(i);
120 
121     MCOperand MCOp;
122     switch (MO.getType()) {
123     default: llvm_unreachable("unknown operand type");
124     case MachineOperand::MO_Register:
125       // Ignore all implicit register operands.
126       if (MO.isImplicit()) continue;
127       MCOp = MCOperand::CreateReg(MO.getReg());
128       break;
129     case MachineOperand::MO_Immediate:
130       MCOp = MCOperand::CreateImm(MO.getImm());
131       break;
132     case MachineOperand::MO_MachineBasicBlock:
133       MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
134                          MO.getMBB()->getSymbol(), Ctx));
135       break;
136     case MachineOperand::MO_GlobalAddress:
137       MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
138       break;
139     case MachineOperand::MO_ExternalSymbol:
140       MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
141       break;
142     case MachineOperand::MO_JumpTableIndex:
143       MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
144       break;
145     case MachineOperand::MO_ConstantPoolIndex:
146       MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
147       break;
148     case MachineOperand::MO_BlockAddress:
149       MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
150       break;
151     case MachineOperand::MO_FPImmediate: {
152       bool ignored;
153       APFloat FVal = MO.getFPImm()->getValueAPF();
154       FVal.convert(APFloat::IEEEsingle, APFloat::rmTowardZero, &ignored);
155 
156       APInt IVal = FVal.bitcastToAPInt();
157       uint64_t Val = *IVal.getRawData();
158       MCOp = MCOperand::CreateImm(Val);
159       break;
160     }
161     case MachineOperand::MO_RegisterMask:
162       continue;
163     }
164 
165     OutMI.addOperand(MCOp);
166   }
167 }
168