1 //=-- LanaiMCInstLower.cpp - Convert Lanai 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 Lanai MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "LanaiMCInstLower.h"
16
17 #include "MCTargetDesc/LanaiBaseInfo.h"
18 #include "MCTargetDesc/LanaiMCExpr.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Mangler.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31
32 using namespace llvm;
33
34 MCSymbol *
GetGlobalAddressSymbol(const MachineOperand & MO) const35 LanaiMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
36 return Printer.getSymbol(MO.getGlobal());
37 }
38
39 MCSymbol *
GetBlockAddressSymbol(const MachineOperand & MO) const40 LanaiMCInstLower::GetBlockAddressSymbol(const MachineOperand &MO) const {
41 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
42 }
43
44 MCSymbol *
GetExternalSymbolSymbol(const MachineOperand & MO) const45 LanaiMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {
46 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
47 }
48
GetJumpTableSymbol(const MachineOperand & MO) const49 MCSymbol *LanaiMCInstLower::GetJumpTableSymbol(const MachineOperand &MO) const {
50 SmallString<256> Name;
51 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
52 << Printer.getFunctionNumber() << '_'
53 << MO.getIndex();
54 // Create a symbol for the name.
55 return Ctx.getOrCreateSymbol(Name.str());
56 }
57
58 MCSymbol *
GetConstantPoolIndexSymbol(const MachineOperand & MO) const59 LanaiMCInstLower::GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
60 SmallString<256> Name;
61 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
62 << Printer.getFunctionNumber() << '_'
63 << MO.getIndex();
64 // Create a symbol for the name.
65 return Ctx.getOrCreateSymbol(Name.str());
66 }
67
LowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym) const68 MCOperand LanaiMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
69 MCSymbol *Sym) const {
70 LanaiMCExpr::VariantKind Kind;
71
72 switch (MO.getTargetFlags()) {
73 case LanaiII::MO_NO_FLAG:
74 Kind = LanaiMCExpr::VK_Lanai_None;
75 break;
76 case LanaiII::MO_ABS_HI:
77 Kind = LanaiMCExpr::VK_Lanai_ABS_HI;
78 break;
79 case LanaiII::MO_ABS_LO:
80 Kind = LanaiMCExpr::VK_Lanai_ABS_LO;
81 break;
82 default:
83 llvm_unreachable("Unknown target flag on GV operand");
84 }
85
86 const MCExpr *Expr =
87 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
88 if (!MO.isJTI() && MO.getOffset())
89 Expr = MCBinaryExpr::createAdd(
90 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
91 Expr = LanaiMCExpr::create(Kind, Expr, Ctx);
92 return MCOperand::createExpr(Expr);
93 }
94
Lower(const MachineInstr * MI,MCInst & OutMI) const95 void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
96 OutMI.setOpcode(MI->getOpcode());
97
98 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
99 const MachineOperand &MO = MI->getOperand(I);
100
101 MCOperand MCOp;
102 switch (MO.getType()) {
103 case MachineOperand::MO_Register:
104 // Ignore all implicit register operands.
105 if (MO.isImplicit())
106 continue;
107 MCOp = MCOperand::createReg(MO.getReg());
108 break;
109 case MachineOperand::MO_Immediate:
110 MCOp = MCOperand::createImm(MO.getImm());
111 break;
112 case MachineOperand::MO_MachineBasicBlock:
113 MCOp = MCOperand::createExpr(
114 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
115 break;
116 case MachineOperand::MO_RegisterMask:
117 continue;
118 case MachineOperand::MO_GlobalAddress:
119 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
120 break;
121 case MachineOperand::MO_BlockAddress:
122 MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
123 break;
124 case MachineOperand::MO_ExternalSymbol:
125 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
126 break;
127 case MachineOperand::MO_JumpTableIndex:
128 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
129 break;
130 case MachineOperand::MO_ConstantPoolIndex:
131 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
132 break;
133 default:
134 MI->dump();
135 llvm_unreachable("unknown operand type");
136 }
137
138 OutMI.addOperand(MCOp);
139 }
140 }
141