Implement fastcc calling convention for MIPS.
[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 "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"
26
27 using namespace llvm;
28
29 MipsMCInstLower::MipsMCInstLower(MipsAsmPrinter &asmprinter)
30   : AsmPrinter(asmprinter) {}
31
32 void MipsMCInstLower::Initialize(Mangler *M, MCContext* C) {
33   Mang = M;
34   Ctx = C;
35 }
36
37 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
38                                               MachineOperandType MOTy,
39                                               unsigned Offset) const {
40   MCSymbolRefExpr::VariantKind Kind;
41   const MCSymbol *Symbol;
42
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;
64   }
65
66   switch (MOTy) {
67   case MachineOperand::MO_MachineBasicBlock:
68     Symbol = MO.getMBB()->getSymbol();
69     break;
70
71   case MachineOperand::MO_GlobalAddress:
72     Symbol = Mang->getSymbol(MO.getGlobal());
73     Offset += MO.getOffset();
74     break;
75
76   case MachineOperand::MO_BlockAddress:
77     Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
78     Offset += MO.getOffset();
79     break;
80
81   case MachineOperand::MO_ExternalSymbol:
82     Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
83     Offset += MO.getOffset();
84     break;
85
86   case MachineOperand::MO_JumpTableIndex:
87     Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
88     break;
89
90   case MachineOperand::MO_ConstantPoolIndex:
91     Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
92     Offset += MO.getOffset();
93     break;
94
95   default:
96     llvm_unreachable("<unknown operand type>");
97   }
98
99   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, *Ctx);
100
101   if (!Offset)
102     return MCOperand::CreateExpr(MCSym);
103
104   // Assume offset is never negative.
105   assert(Offset > 0);
106
107   const MCConstantExpr *OffsetExpr =  MCConstantExpr::Create(Offset, *Ctx);
108   const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, *Ctx);
109   return MCOperand::CreateExpr(AddExpr);
110 }
111
112 static void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand& Opnd0,
113                          const MCOperand& Opnd1,
114                          const MCOperand& Opnd2 = MCOperand()) {
115   Inst.setOpcode(Opc);
116   Inst.addOperand(Opnd0);
117   Inst.addOperand(Opnd1);
118   if (Opnd2.isValid())
119     Inst.addOperand(Opnd2);
120 }
121
122 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO,
123                                         unsigned offset) const {
124   MachineOperandType MOTy = MO.getType();
125
126   switch (MOTy) {
127   default: llvm_unreachable("unknown operand type");
128   case MachineOperand::MO_Register:
129     // Ignore all implicit register operands.
130     if (MO.isImplicit()) break;
131     return MCOperand::CreateReg(MO.getReg());
132   case MachineOperand::MO_Immediate:
133     return MCOperand::CreateImm(MO.getImm() + offset);
134   case MachineOperand::MO_MachineBasicBlock:
135   case MachineOperand::MO_GlobalAddress:
136   case MachineOperand::MO_ExternalSymbol:
137   case MachineOperand::MO_JumpTableIndex:
138   case MachineOperand::MO_ConstantPoolIndex:
139   case MachineOperand::MO_BlockAddress:
140     return LowerSymbolOperand(MO, MOTy, offset);
141   case MachineOperand::MO_RegisterMask:
142     break;
143  }
144
145   return MCOperand();
146 }
147
148 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
149   OutMI.setOpcode(MI->getOpcode());
150
151   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
152     const MachineOperand &MO = MI->getOperand(i);
153     MCOperand MCOp = LowerOperand(MO);
154
155     if (MCOp.isValid())
156       OutMI.addOperand(MCOp);
157   }
158 }
159
160 // Create the following two instructions:
161 //  "lui   $2, %hi(_gp_disp)"
162 //  "addiu $2, $2, %lo(_gp_disp)"
163 void MipsMCInstLower::LowerSETGP01(SmallVector<MCInst, 4>& MCInsts) {
164   MCOperand RegOpnd = MCOperand::CreateReg(Mips::V0);
165   StringRef SymName("_gp_disp");
166   const MCSymbol *Sym = Ctx->GetOrCreateSymbol(SymName);
167   const MCSymbolRefExpr *MCSym;
168
169   MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_HI, *Ctx);
170   MCOperand SymHi = MCOperand::CreateExpr(MCSym);
171   MCSym = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_Mips_ABS_LO, *Ctx);
172   MCOperand SymLo = MCOperand::CreateExpr(MCSym);
173
174   MCInsts.resize(2);
175
176   CreateMCInst(MCInsts[0], Mips::LUi, RegOpnd, SymHi);
177   CreateMCInst(MCInsts[1], Mips::ADDiu, RegOpnd, RegOpnd, SymLo);
178 }