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 "MipsMCInstLower.h"
16 #include "MipsAsmPrinter.h"
17 #include "MipsInstrInfo.h"
18 #include "MCTargetDesc/MipsBaseInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineOperand.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"
29 MipsMCInstLower::MipsMCInstLower(MipsAsmPrinter &asmprinter)
30 : AsmPrinter(asmprinter) {}
32 void MipsMCInstLower::Initialize(Mangler *M, MCContext* C) {
37 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
38 MachineOperandType MOTy,
39 unsigned Offset) const {
40 MCSymbolRefExpr::VariantKind Kind;
41 const MCSymbol *Symbol;
43 switch(MO.getTargetFlags()) {
44 default: llvm_unreachable("Invalid target flag!");
45 case MipsII::MO_NO_FLAG: Kind = MCSymbolRefExpr::VK_None; break;
46 case MipsII::MO_GPREL: Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
47 case MipsII::MO_GOT_CALL: Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
48 case MipsII::MO_GOT16: Kind = MCSymbolRefExpr::VK_Mips_GOT16; break;
49 case MipsII::MO_GOT: Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
50 case MipsII::MO_ABS_HI: Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
51 case MipsII::MO_ABS_LO: Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
52 case MipsII::MO_TLSGD: Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
53 case MipsII::MO_TLSLDM: Kind = MCSymbolRefExpr::VK_Mips_TLSLDM; break;
54 case MipsII::MO_DTPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_HI; break;
55 case MipsII::MO_DTPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_LO; break;
56 case MipsII::MO_GOTTPREL: Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
57 case MipsII::MO_TPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
58 case MipsII::MO_TPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
59 case MipsII::MO_GPOFF_HI: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
60 case MipsII::MO_GPOFF_LO: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
61 case MipsII::MO_GOT_DISP: Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
62 case MipsII::MO_GOT_PAGE: Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
63 case MipsII::MO_GOT_OFST: Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
67 case MachineOperand::MO_MachineBasicBlock:
68 Symbol = MO.getMBB()->getSymbol();
71 case MachineOperand::MO_GlobalAddress:
72 Symbol = Mang->getSymbol(MO.getGlobal());
75 case MachineOperand::MO_BlockAddress:
76 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
79 case MachineOperand::MO_ExternalSymbol:
80 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
83 case MachineOperand::MO_JumpTableIndex:
84 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
87 case MachineOperand::MO_ConstantPoolIndex:
88 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
90 Offset += MO.getOffset();
94 llvm_unreachable("<unknown operand type>");
97 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, *Ctx);
100 return MCOperand::CreateExpr(MCSym);
102 // Assume offset is never negative.
105 const MCConstantExpr *OffsetExpr = MCConstantExpr::Create(Offset, *Ctx);
106 const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, *Ctx);
107 return MCOperand::CreateExpr(AddExpr);
110 static void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand& Opnd0,
111 const MCOperand& Opnd1,
112 const MCOperand& Opnd2 = MCOperand()) {
114 Inst.addOperand(Opnd0);
115 Inst.addOperand(Opnd1);
117 Inst.addOperand(Opnd2);
120 // Lower ".cpload $reg" to
121 // "lui $gp, %hi(_gp_disp)"
122 // "addiu $gp, $gp, %lo(_gp_disp)"
123 // "addu $gp, $gp, $t9"
124 void MipsMCInstLower::LowerCPLOAD(SmallVector<MCInst, 4>& MCInsts) {
125 MCOperand GPReg = MCOperand::CreateReg(Mips::GP);
126 MCOperand T9Reg = MCOperand::CreateReg(Mips::T9);
127 StringRef SymName("_gp_disp");
128 const MCSymbol *Sym = Ctx->GetOrCreateSymbol(SymName);
129 const MCSymbolRefExpr *MCSym;
131 MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_HI, *Ctx);
132 MCOperand SymHi = MCOperand::CreateExpr(MCSym);
133 MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_LO, *Ctx);
134 MCOperand SymLo = MCOperand::CreateExpr(MCSym);
138 CreateMCInst(MCInsts[0], Mips::LUi, GPReg, SymHi);
139 CreateMCInst(MCInsts[1], Mips::ADDiu, GPReg, GPReg, SymLo);
140 CreateMCInst(MCInsts[2], Mips::ADDu, GPReg, GPReg, T9Reg);
143 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO,
144 unsigned offset) const {
145 MachineOperandType MOTy = MO.getType();
148 default: llvm_unreachable("unknown operand type");
149 case MachineOperand::MO_Register:
150 // Ignore all implicit register operands.
151 if (MO.isImplicit()) break;
152 return MCOperand::CreateReg(MO.getReg());
153 case MachineOperand::MO_Immediate:
154 return MCOperand::CreateImm(MO.getImm() + offset);
155 case MachineOperand::MO_MachineBasicBlock:
156 case MachineOperand::MO_GlobalAddress:
157 case MachineOperand::MO_ExternalSymbol:
158 case MachineOperand::MO_JumpTableIndex:
159 case MachineOperand::MO_ConstantPoolIndex:
160 case MachineOperand::MO_BlockAddress:
161 return LowerSymbolOperand(MO, MOTy, offset);
162 case MachineOperand::MO_RegisterMask:
169 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
170 OutMI.setOpcode(MI->getOpcode());
172 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
173 const MachineOperand &MO = MI->getOperand(i);
174 MCOperand MCOp = LowerOperand(MO);
177 OutMI.addOperand(MCOp);
181 void MipsMCInstLower::LowerUnalignedLoadStore(const MachineInstr *MI,
184 unsigned Opc = MI->getOpcode();
185 MCInst Instr1, Instr2, Instr3, Move;
187 bool TwoInstructions = false;
189 assert(MI->getNumOperands() == 3);
190 assert(MI->getOperand(0).isReg());
191 assert(MI->getOperand(1).isReg());
193 MCOperand Target = LowerOperand(MI->getOperand(0));
194 MCOperand Base = LowerOperand(MI->getOperand(1));
195 MCOperand ATReg = MCOperand::CreateReg(Mips::AT);
196 MCOperand ZeroReg = MCOperand::CreateReg(Mips::ZERO);
198 MachineOperand UnLoweredName = MI->getOperand(2);
199 MCOperand Name = LowerOperand(UnLoweredName);
201 Move.setOpcode(Mips::ADDu);
202 Move.addOperand(Target);
203 Move.addOperand(ATReg);
204 Move.addOperand(ZeroReg);
208 // FIXME: only works for little endian right now
209 MCOperand AdjName = LowerOperand(UnLoweredName, 3);
210 if (Base.getReg() == (Target.getReg())) {
211 Instr1.setOpcode(Mips::LWL);
212 Instr1.addOperand(ATReg);
213 Instr1.addOperand(Base);
214 Instr1.addOperand(AdjName);
215 Instr2.setOpcode(Mips::LWR);
216 Instr2.addOperand(ATReg);
217 Instr2.addOperand(Base);
218 Instr2.addOperand(Name);
221 TwoInstructions = true;
222 Instr1.setOpcode(Mips::LWL);
223 Instr1.addOperand(Target);
224 Instr1.addOperand(Base);
225 Instr1.addOperand(AdjName);
226 Instr2.setOpcode(Mips::LWR);
227 Instr2.addOperand(Target);
228 Instr2.addOperand(Base);
229 Instr2.addOperand(Name);
234 // FIXME: only works for little endian right now
235 MCOperand AdjName = LowerOperand(UnLoweredName, 1);
236 Instr1.setOpcode(Mips::LBu);
237 Instr1.addOperand(ATReg);
238 Instr1.addOperand(Base);
239 Instr1.addOperand(AdjName);
240 Instr2.setOpcode(Mips::LBu);
241 Instr2.addOperand(Target);
242 Instr2.addOperand(Base);
243 Instr2.addOperand(Name);
244 Instr3.setOpcode(Mips::INS);
245 Instr3.addOperand(Target);
246 Instr3.addOperand(ATReg);
247 Instr3.addOperand(MCOperand::CreateImm(0x8));
248 Instr3.addOperand(MCOperand::CreateImm(0x18));
253 // FIXME: only works for little endian right now
254 assert (Base.getReg() != Target.getReg());
255 TwoInstructions = true;
256 MCOperand AdjName = LowerOperand(UnLoweredName, 3);
257 Instr1.setOpcode(Mips::SWL);
258 Instr1.addOperand(Target);
259 Instr1.addOperand(Base);
260 Instr1.addOperand(AdjName);
261 Instr2.setOpcode(Mips::SWR);
262 Instr2.addOperand(Target);
263 Instr2.addOperand(Base);
264 Instr2.addOperand(Name);
268 MCOperand AdjName = LowerOperand(UnLoweredName, 1);
269 Instr1.setOpcode(Mips::SB);
270 Instr1.addOperand(Target);
271 Instr1.addOperand(Base);
272 Instr1.addOperand(Name);
273 Instr2.setOpcode(Mips::SRL);
274 Instr2.addOperand(ATReg);
275 Instr2.addOperand(Target);
276 Instr2.addOperand(MCOperand::CreateImm(8));
277 Instr3.setOpcode(Mips::SB);
278 Instr3.addOperand(ATReg);
279 Instr3.addOperand(Base);
280 Instr3.addOperand(AdjName);
284 // FIXME: need to add others
285 llvm_unreachable("unaligned instruction not processed");
288 MCInsts.push_back(Instr1);
289 MCInsts.push_back(Instr2);
290 if (!TwoInstructions) MCInsts.push_back(Instr3);
293 // Create the following two instructions:
294 // "lui $2, %hi(_gp_disp)"
295 // "addiu $2, $2, %lo(_gp_disp)"
296 void MipsMCInstLower::LowerSETGP01(SmallVector<MCInst, 4>& MCInsts) {
297 MCOperand RegOpnd = MCOperand::CreateReg(Mips::V0);
298 StringRef SymName("_gp_disp");
299 const MCSymbol *Sym = Ctx->GetOrCreateSymbol(SymName);
300 const MCSymbolRefExpr *MCSym;
302 MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_HI, *Ctx);
303 MCOperand SymHi = MCOperand::CreateExpr(MCSym);
304 MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_LO, *Ctx);
305 MCOperand SymLo = MCOperand::CreateExpr(MCSym);
309 CreateMCInst(MCInsts[0], Mips::LUi, RegOpnd, SymHi);
310 CreateMCInst(MCInsts[1], Mips::ADDiu, RegOpnd, RegOpnd, SymLo);