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