xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/Mips/MipsMCInstLower.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- MipsMCInstLower.cpp - Convert Mips MachineInstr to MCInst ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains code to lower Mips MachineInstrs to their corresponding
11f4a2713aSLionel Sambuc // MCInst records.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc #include "MipsMCInstLower.h"
15f4a2713aSLionel Sambuc #include "MCTargetDesc/MipsBaseInfo.h"
16f4a2713aSLionel Sambuc #include "MipsAsmPrinter.h"
17f4a2713aSLionel Sambuc #include "MipsInstrInfo.h"
18f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
19f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstr.h"
20f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineOperand.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/IR/Mangler.h"
22f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
23f4a2713aSLionel Sambuc #include "llvm/MC/MCExpr.h"
24f4a2713aSLionel Sambuc #include "llvm/MC/MCInst.h"
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc using namespace llvm;
27f4a2713aSLionel Sambuc 
MipsMCInstLower(MipsAsmPrinter & asmprinter)28f4a2713aSLionel Sambuc MipsMCInstLower::MipsMCInstLower(MipsAsmPrinter &asmprinter)
29f4a2713aSLionel Sambuc   : AsmPrinter(asmprinter) {}
30f4a2713aSLionel Sambuc 
Initialize(MCContext * C)31f4a2713aSLionel Sambuc void MipsMCInstLower::Initialize(MCContext *C) {
32f4a2713aSLionel Sambuc   Ctx = C;
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
LowerSymbolOperand(const MachineOperand & MO,MachineOperandType MOTy,unsigned Offset) const35f4a2713aSLionel Sambuc MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
36f4a2713aSLionel Sambuc                                               MachineOperandType MOTy,
37f4a2713aSLionel Sambuc                                               unsigned Offset) const {
38f4a2713aSLionel Sambuc   MCSymbolRefExpr::VariantKind Kind;
39f4a2713aSLionel Sambuc   const MCSymbol *Symbol;
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc   switch(MO.getTargetFlags()) {
42f4a2713aSLionel Sambuc   default:                   llvm_unreachable("Invalid target flag!");
43f4a2713aSLionel Sambuc   case MipsII::MO_NO_FLAG:   Kind = MCSymbolRefExpr::VK_None; break;
44f4a2713aSLionel Sambuc   case MipsII::MO_GPREL:     Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
45f4a2713aSLionel Sambuc   case MipsII::MO_GOT_CALL:  Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
46f4a2713aSLionel Sambuc   case MipsII::MO_GOT16:     Kind = MCSymbolRefExpr::VK_Mips_GOT16; break;
47f4a2713aSLionel Sambuc   case MipsII::MO_GOT:       Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
48f4a2713aSLionel Sambuc   case MipsII::MO_ABS_HI:    Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
49f4a2713aSLionel Sambuc   case MipsII::MO_ABS_LO:    Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
50f4a2713aSLionel Sambuc   case MipsII::MO_TLSGD:     Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
51f4a2713aSLionel Sambuc   case MipsII::MO_TLSLDM:    Kind = MCSymbolRefExpr::VK_Mips_TLSLDM; break;
52f4a2713aSLionel Sambuc   case MipsII::MO_DTPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_HI; break;
53f4a2713aSLionel Sambuc   case MipsII::MO_DTPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_LO; break;
54f4a2713aSLionel Sambuc   case MipsII::MO_GOTTPREL:  Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
55f4a2713aSLionel Sambuc   case MipsII::MO_TPREL_HI:  Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
56f4a2713aSLionel Sambuc   case MipsII::MO_TPREL_LO:  Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
57f4a2713aSLionel Sambuc   case MipsII::MO_GPOFF_HI:  Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
58f4a2713aSLionel Sambuc   case MipsII::MO_GPOFF_LO:  Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
59f4a2713aSLionel Sambuc   case MipsII::MO_GOT_DISP:  Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
60f4a2713aSLionel Sambuc   case MipsII::MO_GOT_PAGE:  Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
61f4a2713aSLionel Sambuc   case MipsII::MO_GOT_OFST:  Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
62f4a2713aSLionel Sambuc   case MipsII::MO_HIGHER:    Kind = MCSymbolRefExpr::VK_Mips_HIGHER; break;
63f4a2713aSLionel Sambuc   case MipsII::MO_HIGHEST:   Kind = MCSymbolRefExpr::VK_Mips_HIGHEST; break;
64f4a2713aSLionel Sambuc   case MipsII::MO_GOT_HI16:  Kind = MCSymbolRefExpr::VK_Mips_GOT_HI16; break;
65f4a2713aSLionel Sambuc   case MipsII::MO_GOT_LO16:  Kind = MCSymbolRefExpr::VK_Mips_GOT_LO16; break;
66f4a2713aSLionel Sambuc   case MipsII::MO_CALL_HI16: Kind = MCSymbolRefExpr::VK_Mips_CALL_HI16; break;
67f4a2713aSLionel Sambuc   case MipsII::MO_CALL_LO16: Kind = MCSymbolRefExpr::VK_Mips_CALL_LO16; break;
68f4a2713aSLionel Sambuc   }
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc   switch (MOTy) {
71f4a2713aSLionel Sambuc   case MachineOperand::MO_MachineBasicBlock:
72f4a2713aSLionel Sambuc     Symbol = MO.getMBB()->getSymbol();
73f4a2713aSLionel Sambuc     break;
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc   case MachineOperand::MO_GlobalAddress:
76f4a2713aSLionel Sambuc     Symbol = AsmPrinter.getSymbol(MO.getGlobal());
77f4a2713aSLionel Sambuc     Offset += MO.getOffset();
78f4a2713aSLionel Sambuc     break;
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc   case MachineOperand::MO_BlockAddress:
81f4a2713aSLionel Sambuc     Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
82f4a2713aSLionel Sambuc     Offset += MO.getOffset();
83f4a2713aSLionel Sambuc     break;
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   case MachineOperand::MO_ExternalSymbol:
86f4a2713aSLionel Sambuc     Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
87f4a2713aSLionel Sambuc     Offset += MO.getOffset();
88f4a2713aSLionel Sambuc     break;
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc   case MachineOperand::MO_JumpTableIndex:
91f4a2713aSLionel Sambuc     Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
92f4a2713aSLionel Sambuc     break;
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   case MachineOperand::MO_ConstantPoolIndex:
95f4a2713aSLionel Sambuc     Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
96f4a2713aSLionel Sambuc     Offset += MO.getOffset();
97f4a2713aSLionel Sambuc     break;
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   default:
100f4a2713aSLionel Sambuc     llvm_unreachable("<unknown operand type>");
101f4a2713aSLionel Sambuc   }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, *Ctx);
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   if (!Offset)
106f4a2713aSLionel Sambuc     return MCOperand::CreateExpr(MCSym);
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc   // Assume offset is never negative.
109f4a2713aSLionel Sambuc   assert(Offset > 0);
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   const MCConstantExpr *OffsetExpr =  MCConstantExpr::Create(Offset, *Ctx);
112f4a2713aSLionel Sambuc   const MCBinaryExpr *Add = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, *Ctx);
113f4a2713aSLionel Sambuc   return MCOperand::CreateExpr(Add);
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc /*
117f4a2713aSLionel Sambuc static void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand &Opnd0,
118f4a2713aSLionel Sambuc                          const MCOperand &Opnd1,
119f4a2713aSLionel Sambuc                          const MCOperand &Opnd2 = MCOperand()) {
120f4a2713aSLionel Sambuc   Inst.setOpcode(Opc);
121f4a2713aSLionel Sambuc   Inst.addOperand(Opnd0);
122f4a2713aSLionel Sambuc   Inst.addOperand(Opnd1);
123f4a2713aSLionel Sambuc   if (Opnd2.isValid())
124f4a2713aSLionel Sambuc     Inst.addOperand(Opnd2);
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc */
127f4a2713aSLionel Sambuc 
LowerOperand(const MachineOperand & MO,unsigned offset) const128f4a2713aSLionel Sambuc MCOperand MipsMCInstLower::LowerOperand(const MachineOperand &MO,
129f4a2713aSLionel Sambuc                                         unsigned offset) const {
130f4a2713aSLionel Sambuc   MachineOperandType MOTy = MO.getType();
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   switch (MOTy) {
133f4a2713aSLionel Sambuc   default: llvm_unreachable("unknown operand type");
134f4a2713aSLionel Sambuc   case MachineOperand::MO_Register:
135f4a2713aSLionel Sambuc     // Ignore all implicit register operands.
136f4a2713aSLionel Sambuc     if (MO.isImplicit()) break;
137f4a2713aSLionel Sambuc     return MCOperand::CreateReg(MO.getReg());
138f4a2713aSLionel Sambuc   case MachineOperand::MO_Immediate:
139f4a2713aSLionel Sambuc     return MCOperand::CreateImm(MO.getImm() + offset);
140f4a2713aSLionel Sambuc   case MachineOperand::MO_MachineBasicBlock:
141f4a2713aSLionel Sambuc   case MachineOperand::MO_GlobalAddress:
142f4a2713aSLionel Sambuc   case MachineOperand::MO_ExternalSymbol:
143f4a2713aSLionel Sambuc   case MachineOperand::MO_JumpTableIndex:
144f4a2713aSLionel Sambuc   case MachineOperand::MO_ConstantPoolIndex:
145f4a2713aSLionel Sambuc   case MachineOperand::MO_BlockAddress:
146f4a2713aSLionel Sambuc     return LowerSymbolOperand(MO, MOTy, offset);
147f4a2713aSLionel Sambuc   case MachineOperand::MO_RegisterMask:
148f4a2713aSLionel Sambuc     break;
149f4a2713aSLionel Sambuc  }
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   return MCOperand();
152f4a2713aSLionel Sambuc }
153f4a2713aSLionel Sambuc 
createSub(MachineBasicBlock * BB1,MachineBasicBlock * BB2,MCSymbolRefExpr::VariantKind Kind) const154*0a6a1f1dSLionel Sambuc MCOperand MipsMCInstLower::createSub(MachineBasicBlock *BB1,
155*0a6a1f1dSLionel Sambuc                                      MachineBasicBlock *BB2,
156*0a6a1f1dSLionel Sambuc                                      MCSymbolRefExpr::VariantKind Kind) const {
157*0a6a1f1dSLionel Sambuc   const MCSymbolRefExpr *Sym1 = MCSymbolRefExpr::Create(BB1->getSymbol(), *Ctx);
158*0a6a1f1dSLionel Sambuc   const MCSymbolRefExpr *Sym2 = MCSymbolRefExpr::Create(BB2->getSymbol(), *Ctx);
159*0a6a1f1dSLionel Sambuc   const MCBinaryExpr *Sub = MCBinaryExpr::CreateSub(Sym1, Sym2, *Ctx);
160*0a6a1f1dSLionel Sambuc 
161*0a6a1f1dSLionel Sambuc   return MCOperand::CreateExpr(MipsMCExpr::Create(Kind, Sub, *Ctx));
162*0a6a1f1dSLionel Sambuc }
163*0a6a1f1dSLionel Sambuc 
164*0a6a1f1dSLionel Sambuc void MipsMCInstLower::
lowerLongBranchLUi(const MachineInstr * MI,MCInst & OutMI) const165*0a6a1f1dSLionel Sambuc lowerLongBranchLUi(const MachineInstr *MI, MCInst &OutMI) const {
166*0a6a1f1dSLionel Sambuc   OutMI.setOpcode(Mips::LUi);
167*0a6a1f1dSLionel Sambuc 
168*0a6a1f1dSLionel Sambuc   // Lower register operand.
169*0a6a1f1dSLionel Sambuc   OutMI.addOperand(LowerOperand(MI->getOperand(0)));
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc   // Create %hi($tgt-$baltgt).
172*0a6a1f1dSLionel Sambuc   OutMI.addOperand(createSub(MI->getOperand(1).getMBB(),
173*0a6a1f1dSLionel Sambuc                              MI->getOperand(2).getMBB(),
174*0a6a1f1dSLionel Sambuc                              MCSymbolRefExpr::VK_Mips_ABS_HI));
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc void MipsMCInstLower::
lowerLongBranchADDiu(const MachineInstr * MI,MCInst & OutMI,int Opcode,MCSymbolRefExpr::VariantKind Kind) const178*0a6a1f1dSLionel Sambuc lowerLongBranchADDiu(const MachineInstr *MI, MCInst &OutMI, int Opcode,
179*0a6a1f1dSLionel Sambuc                      MCSymbolRefExpr::VariantKind Kind) const {
180*0a6a1f1dSLionel Sambuc   OutMI.setOpcode(Opcode);
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc   // Lower two register operands.
183*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = 2; I != E; ++I) {
184*0a6a1f1dSLionel Sambuc     const MachineOperand &MO = MI->getOperand(I);
185*0a6a1f1dSLionel Sambuc     OutMI.addOperand(LowerOperand(MO));
186*0a6a1f1dSLionel Sambuc   }
187*0a6a1f1dSLionel Sambuc 
188*0a6a1f1dSLionel Sambuc   // Create %lo($tgt-$baltgt) or %hi($tgt-$baltgt).
189*0a6a1f1dSLionel Sambuc   OutMI.addOperand(createSub(MI->getOperand(2).getMBB(),
190*0a6a1f1dSLionel Sambuc                              MI->getOperand(3).getMBB(), Kind));
191*0a6a1f1dSLionel Sambuc }
192*0a6a1f1dSLionel Sambuc 
lowerLongBranch(const MachineInstr * MI,MCInst & OutMI) const193*0a6a1f1dSLionel Sambuc bool MipsMCInstLower::lowerLongBranch(const MachineInstr *MI,
194*0a6a1f1dSLionel Sambuc                                       MCInst &OutMI) const {
195*0a6a1f1dSLionel Sambuc   switch (MI->getOpcode()) {
196*0a6a1f1dSLionel Sambuc   default:
197*0a6a1f1dSLionel Sambuc     return false;
198*0a6a1f1dSLionel Sambuc   case Mips::LONG_BRANCH_LUi:
199*0a6a1f1dSLionel Sambuc     lowerLongBranchLUi(MI, OutMI);
200*0a6a1f1dSLionel Sambuc     return true;
201*0a6a1f1dSLionel Sambuc   case Mips::LONG_BRANCH_ADDiu:
202*0a6a1f1dSLionel Sambuc     lowerLongBranchADDiu(MI, OutMI, Mips::ADDiu,
203*0a6a1f1dSLionel Sambuc                          MCSymbolRefExpr::VK_Mips_ABS_LO);
204*0a6a1f1dSLionel Sambuc     return true;
205*0a6a1f1dSLionel Sambuc   case Mips::LONG_BRANCH_DADDiu:
206*0a6a1f1dSLionel Sambuc     unsigned TargetFlags = MI->getOperand(2).getTargetFlags();
207*0a6a1f1dSLionel Sambuc     if (TargetFlags == MipsII::MO_ABS_HI)
208*0a6a1f1dSLionel Sambuc       lowerLongBranchADDiu(MI, OutMI, Mips::DADDiu,
209*0a6a1f1dSLionel Sambuc                            MCSymbolRefExpr::VK_Mips_ABS_HI);
210*0a6a1f1dSLionel Sambuc     else if (TargetFlags == MipsII::MO_ABS_LO)
211*0a6a1f1dSLionel Sambuc       lowerLongBranchADDiu(MI, OutMI, Mips::DADDiu,
212*0a6a1f1dSLionel Sambuc                            MCSymbolRefExpr::VK_Mips_ABS_LO);
213*0a6a1f1dSLionel Sambuc     else
214*0a6a1f1dSLionel Sambuc       report_fatal_error("Unexpected flags for LONG_BRANCH_DADDiu");
215*0a6a1f1dSLionel Sambuc     return true;
216*0a6a1f1dSLionel Sambuc   }
217*0a6a1f1dSLionel Sambuc }
218*0a6a1f1dSLionel Sambuc 
Lower(const MachineInstr * MI,MCInst & OutMI) const219f4a2713aSLionel Sambuc void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
220*0a6a1f1dSLionel Sambuc   if (lowerLongBranch(MI, OutMI))
221*0a6a1f1dSLionel Sambuc     return;
222*0a6a1f1dSLionel Sambuc 
223f4a2713aSLionel Sambuc   OutMI.setOpcode(MI->getOpcode());
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
226f4a2713aSLionel Sambuc     const MachineOperand &MO = MI->getOperand(i);
227f4a2713aSLionel Sambuc     MCOperand MCOp = LowerOperand(MO);
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc     if (MCOp.isValid())
230f4a2713aSLionel Sambuc       OutMI.addOperand(MCOp);
231f4a2713aSLionel Sambuc   }
232f4a2713aSLionel Sambuc }
233f4a2713aSLionel Sambuc 
234