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 "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"
28 MipsMCInstLower::MipsMCInstLower(Mangler *mang, const MachineFunction &mf,
29 MipsAsmPrinter &asmprinter)
30 : Ctx(mf.getContext()), Mang(mang), AsmPrinter(asmprinter) {}
32 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
33 MachineOperandType MOTy,
34 unsigned Offset) const {
35 MCSymbolRefExpr::VariantKind Kind;
36 const MCSymbol *Symbol;
38 switch(MO.getTargetFlags()) {
39 default: assert(0 && "Invalid target flag!");
40 case MipsII::MO_NO_FLAG: Kind = MCSymbolRefExpr::VK_None; break;
41 case MipsII::MO_GPREL: Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
42 case MipsII::MO_GOT_CALL: Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
43 case MipsII::MO_GOT: Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
44 case MipsII::MO_ABS_HI: Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
45 case MipsII::MO_ABS_LO: Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
46 case MipsII::MO_TLSGD: Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
47 case MipsII::MO_GOTTPREL: Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
48 case MipsII::MO_TPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
49 case MipsII::MO_TPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
50 case MipsII::MO_GPOFF_HI: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
51 case MipsII::MO_GPOFF_LO: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
52 case MipsII::MO_GOT_DISP: Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
53 case MipsII::MO_GOT_PAGE: Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
54 case MipsII::MO_GOT_OFST: Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
58 case MachineOperand::MO_MachineBasicBlock:
59 Symbol = MO.getMBB()->getSymbol();
62 case MachineOperand::MO_GlobalAddress:
63 Symbol = Mang->getSymbol(MO.getGlobal());
66 case MachineOperand::MO_BlockAddress:
67 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
70 case MachineOperand::MO_ExternalSymbol:
71 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
74 case MachineOperand::MO_JumpTableIndex:
75 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
78 case MachineOperand::MO_ConstantPoolIndex:
79 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
81 Offset += MO.getOffset();
85 llvm_unreachable("<unknown operand type>");
88 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
91 return MCOperand::CreateExpr(MCSym);
93 // Assume offset is never negative.
96 const MCConstantExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
97 const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, Ctx);
98 return MCOperand::CreateExpr(AddExpr);
101 // Lower ".cpload $reg" to
102 // "lui $gp, %hi(_gp_disp)"
103 // "addiu $gp, $gp, %lo(_gp_disp)"
104 // "addu $gp. $gp, $reg"
105 void MipsMCInstLower::LowerCPLOAD(const MachineInstr *MI,
106 SmallVector<MCInst, 4>& MCInsts) {
107 MCInst Lui, Addiu, Addu;
108 StringRef SymName("_gp_disp");
109 const MCSymbol *Symbol = Ctx.GetOrCreateSymbol(SymName);
110 const MCSymbolRefExpr *MCSym;
112 // lui $gp, %hi(_gp_disp)
113 Lui.setOpcode(Mips::LUi);
114 Lui.addOperand(MCOperand::CreateReg(Mips::GP));
115 MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_HI, Ctx);
116 Lui.addOperand(MCOperand::CreateExpr(MCSym));
117 MCInsts.push_back(Lui);
119 // addiu $gp, $gp, %lo(_gp_disp)
120 Addiu.setOpcode(Mips::ADDiu);
121 Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
122 Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
123 MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_LO, Ctx);
124 Addiu.addOperand(MCOperand::CreateExpr(MCSym));
125 MCInsts.push_back(Addiu);
127 // addu $gp. $gp, $reg
128 Addu.setOpcode(Mips::ADDu);
129 Addu.addOperand(MCOperand::CreateReg(Mips::GP));
130 Addu.addOperand(MCOperand::CreateReg(Mips::GP));
131 const MachineOperand &MO = MI->getOperand(0);
132 assert(MO.isReg() && "CPLOAD's operand must be a register.");
133 Addu.addOperand(MCOperand::CreateReg(MO.getReg()));
134 MCInsts.push_back(Addu);
137 // Lower ".cprestore offset" to "sw $gp, offset($sp)".
138 void MipsMCInstLower::LowerCPRESTORE(const MachineInstr *MI, MCInst &OutMI) {
140 OutMI.setOpcode(Mips::SW);
141 OutMI.addOperand(MCOperand::CreateReg(Mips::GP));
142 OutMI.addOperand(MCOperand::CreateReg(Mips::SP));
143 const MachineOperand &MO = MI->getOperand(0);
144 assert(MO.isImm() && "CPRESTORE's operand must be an immediate.");
145 OutMI.addOperand(MCOperand::CreateImm(MO.getImm()));
149 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO) const {
150 MachineOperandType MOTy = MO.getType();
154 assert(0 && "unknown operand type");
156 case MachineOperand::MO_Register:
157 // Ignore all implicit register operands.
158 if (MO.isImplicit()) break;
159 return MCOperand::CreateReg(MO.getReg());
160 case MachineOperand::MO_Immediate:
161 return MCOperand::CreateImm(MO.getImm());
162 case MachineOperand::MO_MachineBasicBlock:
163 case MachineOperand::MO_GlobalAddress:
164 case MachineOperand::MO_ExternalSymbol:
165 case MachineOperand::MO_JumpTableIndex:
166 case MachineOperand::MO_ConstantPoolIndex:
167 case MachineOperand::MO_BlockAddress:
168 return LowerSymbolOperand(MO, MOTy, 0);
174 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
175 OutMI.setOpcode(MI->getOpcode());
177 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178 const MachineOperand &MO = MI->getOperand(i);
179 MCOperand MCOp = LowerOperand(MO);
182 OutMI.addOperand(MCOp);