de65881b79484bd1d10b67a85a03122f6711f3f2
[oota-llvm.git] / lib / Target / Mips / MipsMCInstLower.cpp
1 //===-- MipsMCInstLower.cpp - Convert Mips MachineInstr to 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 Mips MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
26
27 using namespace llvm;
28
29 MipsMCInstLower::MipsMCInstLower(Mangler *mang, const MachineFunction &mf,
30                                  MipsAsmPrinter &asmprinter)
31   : Ctx(mf.getContext()), Mang(mang), AsmPrinter(asmprinter) {}
32
33 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
34                                               MachineOperandType MOTy,
35                                               unsigned Offset) const {
36   MCSymbolRefExpr::VariantKind Kind;
37   const MCSymbol *Symbol;
38
39   switch(MO.getTargetFlags()) {
40   default:                  assert(0 && "Invalid target flag!");
41   case MipsII::MO_NO_FLAG:  Kind = MCSymbolRefExpr::VK_None; break;
42   case MipsII::MO_GPREL:    Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
43   case MipsII::MO_GOT_CALL: Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
44   case MipsII::MO_GOT16:    Kind = MCSymbolRefExpr::VK_Mips_GOT16; break;
45   case MipsII::MO_GOT:      Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
46   case MipsII::MO_ABS_HI:   Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
47   case MipsII::MO_ABS_LO:   Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
48   case MipsII::MO_TLSGD:    Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
49   case MipsII::MO_GOTTPREL: Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
50   case MipsII::MO_TPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
51   case MipsII::MO_TPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
52   case MipsII::MO_GPOFF_HI: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
53   case MipsII::MO_GPOFF_LO: Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
54   case MipsII::MO_GOT_DISP: Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
55   case MipsII::MO_GOT_PAGE: Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
56   case MipsII::MO_GOT_OFST: Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
57   }
58
59   switch (MOTy) {
60   case MachineOperand::MO_MachineBasicBlock:
61     Symbol = MO.getMBB()->getSymbol();
62     break;
63
64   case MachineOperand::MO_GlobalAddress:
65     Symbol = Mang->getSymbol(MO.getGlobal());
66     break;
67
68   case MachineOperand::MO_BlockAddress:
69     Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
70     break;
71
72   case MachineOperand::MO_ExternalSymbol:
73     Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
74     break;
75
76   case MachineOperand::MO_JumpTableIndex:
77     Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
78     break;
79
80   case MachineOperand::MO_ConstantPoolIndex:
81     Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
82     if (MO.getOffset())
83       Offset += MO.getOffset();
84     break;
85
86   default:
87     llvm_unreachable("<unknown operand type>");
88   }
89   
90   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
91
92   if (!Offset)
93     return MCOperand::CreateExpr(MCSym);
94
95   // Assume offset is never negative.
96   assert(Offset > 0);
97    
98   const MCConstantExpr *OffsetExpr =  MCConstantExpr::Create(Offset, Ctx);
99   const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, Ctx);
100   return MCOperand::CreateExpr(AddExpr);
101 }
102
103 // Lower ".cpload $reg" to
104 //  "lui   $gp, %hi(_gp_disp)"
105 //  "addiu $gp, $gp, %lo(_gp_disp)"
106 //  "addu  $gp. $gp, $reg"
107 void MipsMCInstLower::LowerCPLOAD(const MachineInstr *MI,
108                                   SmallVector<MCInst, 4>& MCInsts) {
109   MCInst Lui, Addiu, Addu;
110   StringRef SymName("_gp_disp");
111   const MCSymbol *Symbol = Ctx.GetOrCreateSymbol(SymName);
112   const MCSymbolRefExpr *MCSym;
113
114   // lui   $gp, %hi(_gp_disp)
115   Lui.setOpcode(Mips::LUi);
116   Lui.addOperand(MCOperand::CreateReg(Mips::GP));
117   MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_HI, Ctx);
118   Lui.addOperand(MCOperand::CreateExpr(MCSym));
119   MCInsts.push_back(Lui);
120
121   // addiu $gp, $gp, %lo(_gp_disp)
122   Addiu.setOpcode(Mips::ADDiu);
123   Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
124   Addiu.addOperand(MCOperand::CreateReg(Mips::GP));
125   MCSym = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_Mips_ABS_LO, Ctx);
126   Addiu.addOperand(MCOperand::CreateExpr(MCSym));
127   MCInsts.push_back(Addiu);
128
129   // addu  $gp. $gp, $reg
130   Addu.setOpcode(Mips::ADDu);
131   Addu.addOperand(MCOperand::CreateReg(Mips::GP));
132   Addu.addOperand(MCOperand::CreateReg(Mips::GP));
133   const MachineOperand &MO = MI->getOperand(0);
134   assert(MO.isReg() && "CPLOAD's operand must be a register.");
135   Addu.addOperand(MCOperand::CreateReg(MO.getReg()));
136   MCInsts.push_back(Addu);
137 }
138
139 // Lower ".cprestore offset" to "sw $gp, offset($sp)".
140 void MipsMCInstLower::LowerCPRESTORE(const MachineInstr *MI,
141                                      SmallVector<MCInst, 4>& MCInsts) {
142   const MachineOperand &MO = MI->getOperand(0);
143   assert(MO.isImm() && "CPRESTORE's operand must be an immediate.");
144   unsigned Offset = MO.getImm(), Reg = Mips::SP;
145   MCInst Sw;
146
147   if (Offset >= 0x8000) {
148     unsigned Hi = (Offset >> 16) + ((Offset & 0x8000) != 0); 
149     Offset &= 0xffff;
150     Reg = Mips::AT;
151
152     // lui   at,hi
153     // addu  at,at,sp
154     MCInsts.resize(2);
155     MCInsts[0].setOpcode(Mips::LUi);
156     MCInsts[0].addOperand(MCOperand::CreateReg(Mips::AT));
157     MCInsts[0].addOperand(MCOperand::CreateImm(Hi));
158     MCInsts[1].setOpcode(Mips::ADDu);
159     MCInsts[1].addOperand(MCOperand::CreateReg(Mips::AT));
160     MCInsts[1].addOperand(MCOperand::CreateReg(Mips::AT));
161     MCInsts[1].addOperand(MCOperand::CreateReg(Mips::SP));
162   }
163   
164   Sw.setOpcode(Mips::SW);
165   Sw.addOperand(MCOperand::CreateReg(Mips::GP));
166   Sw.addOperand(MCOperand::CreateReg(Reg));
167   Sw.addOperand(MCOperand::CreateImm(Offset));
168   MCInsts.push_back(Sw);
169 }
170
171 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO,
172                                                     unsigned offset) const {
173   MachineOperandType MOTy = MO.getType();
174   
175   switch (MOTy) {
176   default:
177     assert(0 && "unknown operand type");
178     break;
179   case MachineOperand::MO_Register:
180     // Ignore all implicit register operands.
181     if (MO.isImplicit()) break;
182     return MCOperand::CreateReg(MO.getReg());
183   case MachineOperand::MO_Immediate:
184     return MCOperand::CreateImm(MO.getImm() + offset);
185   case MachineOperand::MO_MachineBasicBlock:
186   case MachineOperand::MO_GlobalAddress:
187   case MachineOperand::MO_ExternalSymbol:
188   case MachineOperand::MO_JumpTableIndex:
189   case MachineOperand::MO_ConstantPoolIndex:
190   case MachineOperand::MO_BlockAddress:
191     return LowerSymbolOperand(MO, MOTy, offset);
192  }
193
194   return MCOperand();
195 }
196
197 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
198   OutMI.setOpcode(MI->getOpcode());
199   
200   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
201     const MachineOperand &MO = MI->getOperand(i);
202     MCOperand MCOp = LowerOperand(MO);
203
204     if (MCOp.isValid())
205       OutMI.addOperand(MCOp);
206   }
207 }
208
209 void MipsMCInstLower::LowerUnalignedLoadStore(const MachineInstr *MI,
210                                                           SmallVector<MCInst,
211                                                           4>& MCInsts) {
212   unsigned Opc = MI->getOpcode();
213   MCInst instr1, instr2, instr3, move;
214
215   bool two_instructions = false;
216
217   assert(MI->getNumOperands() == 3);
218   assert(MI->getOperand(0).isReg());
219   assert(MI->getOperand(1).isReg());
220
221   MCOperand target = LowerOperand(MI->getOperand(0));
222   MCOperand base = LowerOperand(MI->getOperand(1));
223   MCOperand atReg = MCOperand::CreateReg(Mips::AT);
224   MCOperand zeroReg = MCOperand::CreateReg(Mips::ZERO);
225
226   MachineOperand unloweredName = MI->getOperand(2);
227   MCOperand name = LowerOperand(unloweredName);
228
229   move.setOpcode(Mips::ADDu);
230   move.addOperand(target);
231   move.addOperand(atReg);
232   move.addOperand(zeroReg);
233
234   switch (Opc) {
235   case Mips::ULW: {
236     // FIXME: only works for little endian right now
237     MCOperand adj_name = LowerOperand(unloweredName, 3);
238     if (base.getReg() == (target.getReg())) {
239       instr1.setOpcode(Mips::LWL);
240       instr1.addOperand(atReg);
241       instr1.addOperand(base);
242       instr1.addOperand(adj_name);
243       instr2.setOpcode(Mips::LWR);
244       instr2.addOperand(atReg);
245       instr2.addOperand(base);
246       instr2.addOperand(name);
247       instr3 = move;
248     } else {
249       two_instructions = true;
250       instr1.setOpcode(Mips::LWL);
251       instr1.addOperand(target);
252       instr1.addOperand(base);
253       instr1.addOperand(adj_name);
254       instr2.setOpcode(Mips::LWR);
255       instr2.addOperand(target);
256       instr2.addOperand(base);
257       instr2.addOperand(name);
258     }
259     break;
260   }
261   case Mips::ULHu: {
262     // FIXME: only works for little endian right now
263     MCOperand adj_name = LowerOperand(unloweredName, 1);
264     instr1.setOpcode(Mips::LBu);
265     instr1.addOperand(atReg);
266     instr1.addOperand(base);
267     instr1.addOperand(adj_name);
268     instr2.setOpcode(Mips::LBu);
269     instr2.addOperand(target);
270     instr2.addOperand(base);
271     instr2.addOperand(name);
272     instr3.setOpcode(Mips::INS);
273     instr3.addOperand(target);
274     instr3.addOperand(atReg);
275     instr3.addOperand(MCOperand::CreateImm(0x8));
276     instr3.addOperand(MCOperand::CreateImm(0x18));
277     break;
278   }
279
280   case Mips::USW: {
281     // FIXME: only works for little endian right now
282     assert (base.getReg() != target.getReg());
283     two_instructions = true;
284     MCOperand adj_name = LowerOperand(unloweredName, 3);
285     instr1.setOpcode(Mips::SWL);
286     instr1.addOperand(target);
287     instr1.addOperand(base);
288     instr1.addOperand(adj_name);
289     instr2.setOpcode(Mips::SWR);
290     instr2.addOperand(target);
291     instr2.addOperand(base);
292     instr2.addOperand(name);
293     break;
294   }
295   case Mips::USH: {
296     MCOperand adj_name = LowerOperand(unloweredName, 1);
297     instr1.setOpcode(Mips::SB);
298     instr1.addOperand(target);
299     instr1.addOperand(base);
300     instr1.addOperand(name);
301     instr2.setOpcode(Mips::SRL);
302     instr2.addOperand(atReg);
303     instr2.addOperand(target);
304     instr2.addOperand(MCOperand::CreateImm(8));
305     instr3.setOpcode(Mips::SB);
306     instr3.addOperand(atReg);
307     instr3.addOperand(base);
308     instr3.addOperand(adj_name);
309     break;
310   }
311   default:
312     // FIXME: need to add others
313     assert(0 && "unaligned instruction not processed");
314   }
315
316   MCInsts.push_back(instr1);
317   MCInsts.push_back(instr2);
318   if (!two_instructions) MCInsts.push_back(instr3);
319 }
320