1 //===-- MipsMCInstLower.cpp - Convert Mips MachineInstr to 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 Mips MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "MipsAsmPrinter.h"
16 #include "MipsInstrInfo.h"
17 #include "MipsMCInstLower.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineOperand.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Target/Mangler.h"
27 MipsMCInstLower::MipsMCInstLower(Mangler *mang, const MachineFunction &mf,
28 MipsAsmPrinter &asmprinter)
29 : Ctx(mf.getContext()), Mang(mang), AsmPrinter(asmprinter) {}
31 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
32 MachineOperandType MOTy,
33 unsigned Offset) const {
34 MCSymbolRefExpr::VariantKind Kind;
35 const MCSymbol *Symbol;
37 switch(MO.getTargetFlags()) {
38 default: assert(0 && "Invalid target flag!");
39 case MipsII::MO_NO_FLAG: Kind = MCSymbolRefExpr::VK_None; break;
40 case MipsII::MO_GPREL: Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
41 case MipsII::MO_GOT_CALL: Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
42 case MipsII::MO_GOT: Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
43 case MipsII::MO_ABS_HI: Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
44 case MipsII::MO_ABS_LO: Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
45 case MipsII::MO_TLSGD: Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
46 case MipsII::MO_GOTTPREL: Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
47 case MipsII::MO_TPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
48 case MipsII::MO_TPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
49 case MipsII::MO_GPOFF_HI: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
50 case MipsII::MO_GPOFF_LO: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
51 case MipsII::MO_GOT_DISP: Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
52 case MipsII::MO_GOT_PAGE: Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
53 case MipsII::MO_GOT_OFST: Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
57 case MachineOperand::MO_MachineBasicBlock:
58 Symbol = MO.getMBB()->getSymbol();
61 case MachineOperand::MO_GlobalAddress:
62 Symbol = Mang->getSymbol(MO.getGlobal());
65 case MachineOperand::MO_BlockAddress:
66 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
69 case MachineOperand::MO_ExternalSymbol:
70 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
73 case MachineOperand::MO_JumpTableIndex:
74 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
77 case MachineOperand::MO_ConstantPoolIndex:
78 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
80 Offset += MO.getOffset();
84 llvm_unreachable("<unknown operand type>");
87 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
90 return MCOperand::CreateExpr(MCSym);
92 // Assume offset is never negative.
95 const MCConstantExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
96 const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, Ctx);
97 return MCOperand::CreateExpr(AddExpr);
100 // Lower ".cpload $reg" to
101 // "lui $gp, %hi(_gp_disp)"
102 // "addiu $gp, $gp, %lo(_gp_disp)"
103 // "addu $gp. $gp, $reg"
104 void MipsMCInstLower::LowerCPLOAD(const MachineInstr *MI,
105 SmallVector<MCInst, 4>& MCInsts) {
106 MCInst Lui, Addiu, Addu;
107 StringRef SymName("_gp_disp");
108 const MCSymbol *Symbol = Ctx.GetOrCreateSymbol(SymName);
109 const MCSymbolRefExpr *MCSym;
111 // lui $gp, %hi(_gp_disp)
112 Lui.setOpcode(Mips::LUi);
113 Lui.addOperand(MCOperand::CreateReg(Mips::GP));
114 MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_HI, Ctx);
115 Lui.addOperand(MCOperand::CreateExpr(MCSym));
116 MCInsts.push_back(Lui);
118 // addiu $gp, $gp, %lo(_gp_disp)
119 Addiu.setOpcode(Mips::ADDiu);
120 Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
121 Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
122 MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_LO, Ctx);
123 Addiu.addOperand(MCOperand::CreateExpr(MCSym));
124 MCInsts.push_back(Addiu);
126 // addu $gp. $gp, $reg
127 Addu.setOpcode(Mips::ADDu);
128 Addu.addOperand(MCOperand::CreateReg(Mips::GP));
129 Addu.addOperand(MCOperand::CreateReg(Mips::GP));
130 const MachineOperand &MO = MI->getOperand(0);
131 assert(MO.isReg() && "CPLOAD's operand must be a register.");
132 Addu.addOperand(MCOperand::CreateReg(MO.getReg()));
133 MCInsts.push_back(Addu);
136 // Lower ".cprestore offset" to "sw $gp, offset($sp)".
137 void MipsMCInstLower::LowerCPRESTORE(const MachineInstr *MI, MCInst &OutMI) {
139 OutMI.setOpcode(Mips::SW);
140 OutMI.addOperand(MCOperand::CreateReg(Mips::GP));
141 OutMI.addOperand(MCOperand::CreateReg(Mips::SP));
142 const MachineOperand &MO = MI->getOperand(0);
143 assert(MO.isImm() && "CPRESTORE's operand must be an immediate.");
144 OutMI.addOperand(MCOperand::CreateImm(MO.getImm()));
148 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO) const {
149 MachineOperandType MOTy = MO.getType();
153 assert(0 && "unknown operand type");
155 case MachineOperand::MO_Register:
156 // Ignore all implicit register operands.
157 if (MO.isImplicit()) break;
158 return MCOperand::CreateReg(MO.getReg());
159 case MachineOperand::MO_Immediate:
160 return MCOperand::CreateImm(MO.getImm());
161 case MachineOperand::MO_MachineBasicBlock:
162 case MachineOperand::MO_GlobalAddress:
163 case MachineOperand::MO_ExternalSymbol:
164 case MachineOperand::MO_JumpTableIndex:
165 case MachineOperand::MO_ConstantPoolIndex:
166 case MachineOperand::MO_BlockAddress:
167 return LowerSymbolOperand(MO, MOTy, 0);
173 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
174 OutMI.setOpcode(MI->getOpcode());
176 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
177 const MachineOperand &MO = MI->getOperand(i);
178 MCOperand MCOp = LowerOperand(MO);
181 OutMI.addOperand(MCOp);