1 | //=-- LanaiMCInstLower.cpp - Convert Lanai MachineInstr to an MCInst --------=// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file contains code to lower Lanai MachineInstrs to their corresponding |
10 | // MCInst records. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "LanaiMCInstLower.h" |
15 | |
16 | #include "MCTargetDesc/LanaiBaseInfo.h" |
17 | #include "MCTargetDesc/LanaiMCAsmInfo.h" |
18 | #include "llvm/ADT/SmallString.h" |
19 | #include "llvm/CodeGen/AsmPrinter.h" |
20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
21 | #include "llvm/CodeGen/MachineInstr.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/ErrorHandling.h" |
27 | #include "llvm/Support/raw_ostream.h" |
28 | |
29 | using namespace llvm; |
30 | |
31 | MCSymbol * |
32 | LanaiMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { |
33 | return Printer.getSymbol(GV: MO.getGlobal()); |
34 | } |
35 | |
36 | MCSymbol * |
37 | LanaiMCInstLower::GetBlockAddressSymbol(const MachineOperand &MO) const { |
38 | return Printer.GetBlockAddressSymbol(BA: MO.getBlockAddress()); |
39 | } |
40 | |
41 | MCSymbol * |
42 | LanaiMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const { |
43 | return Printer.GetExternalSymbolSymbol(Sym: MO.getSymbolName()); |
44 | } |
45 | |
46 | MCSymbol *LanaiMCInstLower::GetJumpTableSymbol(const MachineOperand &MO) const { |
47 | SmallString<256> Name; |
48 | raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI" |
49 | << Printer.getFunctionNumber() << '_' |
50 | << MO.getIndex(); |
51 | // Create a symbol for the name. |
52 | return Ctx.getOrCreateSymbol(Name: Name.str()); |
53 | } |
54 | |
55 | MCSymbol * |
56 | LanaiMCInstLower::GetConstantPoolIndexSymbol(const MachineOperand &MO) const { |
57 | SmallString<256> Name; |
58 | raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI" |
59 | << Printer.getFunctionNumber() << '_' |
60 | << MO.getIndex(); |
61 | // Create a symbol for the name. |
62 | return Ctx.getOrCreateSymbol(Name: Name.str()); |
63 | } |
64 | |
65 | MCOperand LanaiMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
66 | MCSymbol *Sym) const { |
67 | Lanai::Specifier Kind; |
68 | switch (MO.getTargetFlags()) { |
69 | case LanaiII::MO_NO_FLAG: |
70 | Kind = Lanai::S_None; |
71 | break; |
72 | case LanaiII::MO_ABS_HI: |
73 | Kind = Lanai::S_ABS_HI; |
74 | break; |
75 | case LanaiII::MO_ABS_LO: |
76 | Kind = Lanai::S_ABS_LO; |
77 | break; |
78 | default: |
79 | llvm_unreachable("Unknown target flag on GV operand" ); |
80 | } |
81 | |
82 | const MCExpr *Expr = MCSymbolRefExpr::create(Symbol: Sym, Ctx); |
83 | if (!MO.isJTI() && MO.getOffset()) |
84 | Expr = MCBinaryExpr::createAdd( |
85 | LHS: Expr, RHS: MCConstantExpr::create(Value: MO.getOffset(), Ctx), Ctx); |
86 | Expr = MCSpecifierExpr::create(Expr, S: Kind, Ctx); |
87 | return MCOperand::createExpr(Val: Expr); |
88 | } |
89 | |
90 | void LanaiMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { |
91 | OutMI.setOpcode(MI->getOpcode()); |
92 | |
93 | for (const MachineOperand &MO : MI->operands()) { |
94 | MCOperand MCOp; |
95 | switch (MO.getType()) { |
96 | case MachineOperand::MO_Register: |
97 | // Ignore all implicit register operands. |
98 | if (MO.isImplicit()) |
99 | continue; |
100 | MCOp = MCOperand::createReg(Reg: MO.getReg()); |
101 | break; |
102 | case MachineOperand::MO_Immediate: |
103 | MCOp = MCOperand::createImm(Val: MO.getImm()); |
104 | break; |
105 | case MachineOperand::MO_MachineBasicBlock: |
106 | MCOp = MCOperand::createExpr( |
107 | Val: MCSymbolRefExpr::create(Symbol: MO.getMBB()->getSymbol(), Ctx)); |
108 | break; |
109 | case MachineOperand::MO_RegisterMask: |
110 | continue; |
111 | case MachineOperand::MO_GlobalAddress: |
112 | MCOp = LowerSymbolOperand(MO, Sym: GetGlobalAddressSymbol(MO)); |
113 | break; |
114 | case MachineOperand::MO_BlockAddress: |
115 | MCOp = LowerSymbolOperand(MO, Sym: GetBlockAddressSymbol(MO)); |
116 | break; |
117 | case MachineOperand::MO_ExternalSymbol: |
118 | MCOp = LowerSymbolOperand(MO, Sym: GetExternalSymbolSymbol(MO)); |
119 | break; |
120 | case MachineOperand::MO_JumpTableIndex: |
121 | MCOp = LowerSymbolOperand(MO, Sym: GetJumpTableSymbol(MO)); |
122 | break; |
123 | case MachineOperand::MO_ConstantPoolIndex: |
124 | MCOp = LowerSymbolOperand(MO, Sym: GetConstantPoolIndexSymbol(MO)); |
125 | break; |
126 | default: |
127 | MI->print(OS&: errs()); |
128 | llvm_unreachable("unknown operand type" ); |
129 | } |
130 | |
131 | OutMI.addOperand(Op: MCOp); |
132 | } |
133 | } |
134 | |