1c5707112e7635d1dd2f2cc9c4f42e79a51302ccaJia Liu//===-- MipsMCInstLower.cpp - Convert Mips MachineInstr to MCInst ---------===//
217a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//
317a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//                     The LLVM Compiler Infrastructure
417a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//
517a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka// This file is distributed under the University of Illinois Open Source
617a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka// License. See LICENSE.TXT for details.
717a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//
817a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//===----------------------------------------------------------------------===//
917a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//
1017a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka// This file contains code to lower Mips MachineInstrs to their corresponding
1117a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka// MCInst records.
1217a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//
1317a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka//===----------------------------------------------------------------------===//
1479aa3417eb6f58d668aadfedf075240a41d35a26Craig Topper#include "MipsMCInstLower.h"
15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "MCTargetDesc/MipsBaseInfo.h"
1617a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "MipsAsmPrinter.h"
1717a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "MipsInstrInfo.h"
1817a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "llvm/CodeGen/MachineFunction.h"
1917a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "llvm/CodeGen/MachineInstr.h"
2017a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "llvm/CodeGen/MachineOperand.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Mangler.h"
2217a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "llvm/MC/MCContext.h"
23ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes#include "llvm/MC/MCExpr.h"
2417a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka#include "llvm/MC/MCInst.h"
254c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/MC/MCStreamer.h"
26421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
2717a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanakausing namespace llvm;
2817a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka
29f93b86306683f8e860c8824efb717995cb072a70Akira HatanakaMipsMCInstLower::MipsMCInstLower(MipsAsmPrinter &asmprinter)
30f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  : AsmPrinter(asmprinter) {}
31f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka
32a300b1cc29020b7dfaf7bfe443d38af8fbec7433Rafael Espindolavoid MipsMCInstLower::Initialize(MCContext *C) {
33f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  Ctx = C;
34f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka}
3517a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka
3617a2f8e551dd4a772d389d1a193235ae56f8a399Akira HatanakaMCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
3778d1b11aa57e5a2683e4be3baaaa9576ac04eea2Akira Hatanaka                                              MachineOperandType MOTy,
3878d1b11aa57e5a2683e4be3baaaa9576ac04eea2Akira Hatanaka                                              unsigned Offset) const {
39ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes  MCSymbolRefExpr::VariantKind Kind;
4017a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  const MCSymbol *Symbol;
4117a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka
4217a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  switch(MO.getTargetFlags()) {
43bc2198133a1836598b54b943420748e75d5dea94Craig Topper  default:                   llvm_unreachable("Invalid target flag!");
4482099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_NO_FLAG:   Kind = MCSymbolRefExpr::VK_None; break;
4582099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GPREL:     Kind = MCSymbolRefExpr::VK_Mips_GPREL; break;
4682099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT_CALL:  Kind = MCSymbolRefExpr::VK_Mips_GOT_CALL; break;
4782099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT16:     Kind = MCSymbolRefExpr::VK_Mips_GOT16; break;
4882099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT:       Kind = MCSymbolRefExpr::VK_Mips_GOT; break;
4982099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_ABS_HI:    Kind = MCSymbolRefExpr::VK_Mips_ABS_HI; break;
5082099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_ABS_LO:    Kind = MCSymbolRefExpr::VK_Mips_ABS_LO; break;
5182099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_TLSGD:     Kind = MCSymbolRefExpr::VK_Mips_TLSGD; break;
5282099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_TLSLDM:    Kind = MCSymbolRefExpr::VK_Mips_TLSLDM; break;
5382099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_DTPREL_HI: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_HI; break;
5482099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_DTPREL_LO: Kind = MCSymbolRefExpr::VK_Mips_DTPREL_LO; break;
5582099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOTTPREL:  Kind = MCSymbolRefExpr::VK_Mips_GOTTPREL; break;
5682099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_TPREL_HI:  Kind = MCSymbolRefExpr::VK_Mips_TPREL_HI; break;
5782099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_TPREL_LO:  Kind = MCSymbolRefExpr::VK_Mips_TPREL_LO; break;
5882099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GPOFF_HI:  Kind = MCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
5982099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GPOFF_LO:  Kind = MCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
6082099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT_DISP:  Kind = MCSymbolRefExpr::VK_Mips_GOT_DISP; break;
6182099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT_PAGE:  Kind = MCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
6282099683060abb1f74453d06e78a3729a75ef7eeAkira Hatanaka  case MipsII::MO_GOT_OFST:  Kind = MCSymbolRefExpr::VK_Mips_GOT_OFST; break;
63b22c9289b0dd8255f63038e9bb8229111eb082aeAkira Hatanaka  case MipsII::MO_HIGHER:    Kind = MCSymbolRefExpr::VK_Mips_HIGHER; break;
64b22c9289b0dd8255f63038e9bb8229111eb082aeAkira Hatanaka  case MipsII::MO_HIGHEST:   Kind = MCSymbolRefExpr::VK_Mips_HIGHEST; break;
65f09a03776dbbc882c9b15eeccb8ec847058fbfa0Akira Hatanaka  case MipsII::MO_GOT_HI16:  Kind = MCSymbolRefExpr::VK_Mips_GOT_HI16; break;
66f09a03776dbbc882c9b15eeccb8ec847058fbfa0Akira Hatanaka  case MipsII::MO_GOT_LO16:  Kind = MCSymbolRefExpr::VK_Mips_GOT_LO16; break;
67f09a03776dbbc882c9b15eeccb8ec847058fbfa0Akira Hatanaka  case MipsII::MO_CALL_HI16: Kind = MCSymbolRefExpr::VK_Mips_CALL_HI16; break;
68f09a03776dbbc882c9b15eeccb8ec847058fbfa0Akira Hatanaka  case MipsII::MO_CALL_LO16: Kind = MCSymbolRefExpr::VK_Mips_CALL_LO16; break;
6917a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  }
7017a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka
7117a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  switch (MOTy) {
72421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_MachineBasicBlock:
73421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    Symbol = MO.getMBB()->getSymbol();
74421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
75421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
76421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_GlobalAddress:
77ffc7dca885151ed42642c2d6733e8db75d276621Rafael Espindola    Symbol = AsmPrinter.getSymbol(MO.getGlobal());
78bed5b0db452f9f94facf260ca8667cf670c26c5cAkira Hatanaka    Offset += MO.getOffset();
79421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
80421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
81421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_BlockAddress:
82421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
83bed5b0db452f9f94facf260ca8667cf670c26c5cAkira Hatanaka    Offset += MO.getOffset();
84421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
85421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
86421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_ExternalSymbol:
87421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
88bed5b0db452f9f94facf260ca8667cf670c26c5cAkira Hatanaka    Offset += MO.getOffset();
89421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
90421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
91cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  case MachineOperand::MO_MCSymbol:
92cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Symbol = MO.getMCSymbol();
93cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Offset += MO.getOffset();
94cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    break;
95cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
96421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_JumpTableIndex:
97421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
98421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
99421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
100421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  case MachineOperand::MO_ConstantPoolIndex:
101421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
102bed5b0db452f9f94facf260ca8667cf670c26c5cAkira Hatanaka    Offset += MO.getOffset();
103421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    break;
104421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka
105421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka  default:
106421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    llvm_unreachable("<unknown operand type>");
10717a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  }
108bb481f882093fb738d2bb15610c79364bada5496Jia Liu
1096948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
110ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes
111ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes  if (!Offset)
1126948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return MCOperand::createExpr(MCSym);
113ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes
114ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes  // Assume offset is never negative.
115ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes  assert(Offset > 0);
116bb481f882093fb738d2bb15610c79364bada5496Jia Liu
1176948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCConstantExpr *OffsetExpr =  MCConstantExpr::create(Offset, *Ctx);
1186948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
1196948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  return MCOperand::createExpr(Add);
120ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes}
121ce1a538ab5b7ae7e0ed48d18c02571280fe105aaBruno Cardoso Lopes
12263b37f122d592c7451090ea32281686de967fcd4Akira Hatanaka/*
123864f66085cd9543070ef01b9f7371c110ecd7898Akira Hatanakastatic void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand &Opnd0,
124864f66085cd9543070ef01b9f7371c110ecd7898Akira Hatanaka                         const MCOperand &Opnd1,
125864f66085cd9543070ef01b9f7371c110ecd7898Akira Hatanaka                         const MCOperand &Opnd2 = MCOperand()) {
126f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  Inst.setOpcode(Opc);
127f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  Inst.addOperand(Opnd0);
128f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  Inst.addOperand(Opnd1);
129f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka  if (Opnd2.isValid())
130f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka    Inst.addOperand(Opnd2);
131f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka}
13263b37f122d592c7451090ea32281686de967fcd4Akira Hatanaka*/
133f93b86306683f8e860c8824efb717995cb072a70Akira Hatanaka
134864f66085cd9543070ef01b9f7371c110ecd7898Akira HatanakaMCOperand MipsMCInstLower::LowerOperand(const MachineOperand &MO,
135bb481f882093fb738d2bb15610c79364bada5496Jia Liu                                        unsigned offset) const {
1368957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  MachineOperandType MOTy = MO.getType();
137bb481f882093fb738d2bb15610c79364bada5496Jia Liu
1388957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  switch (MOTy) {
139bc2198133a1836598b54b943420748e75d5dea94Craig Topper  default: llvm_unreachable("unknown operand type");
1408957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_Register:
1418957481e6a3a4217499f739bae24401576ade078Akira Hatanaka    // Ignore all implicit register operands.
1428957481e6a3a4217499f739bae24401576ade078Akira Hatanaka    if (MO.isImplicit()) break;
1436948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return MCOperand::createReg(MO.getReg());
1448957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_Immediate:
1456948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar    return MCOperand::createImm(MO.getImm() + offset);
1468957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_MachineBasicBlock:
1478957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_GlobalAddress:
1488957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_ExternalSymbol:
149cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  case MachineOperand::MO_MCSymbol:
1508957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_JumpTableIndex:
1518957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_ConstantPoolIndex:
1528957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  case MachineOperand::MO_BlockAddress:
153421455f1ea081e2e1767e782ac0d57ca55976e9bAkira Hatanaka    return LowerSymbolOperand(MO, MOTy, offset);
15471f0fc1ca88965b69b4b2c8794a7144bc93d4bbaJakob Stoklund Olesen  case MachineOperand::MO_RegisterMask:
15571f0fc1ca88965b69b4b2c8794a7144bc93d4bbaJakob Stoklund Olesen    break;
1568957481e6a3a4217499f739bae24401576ade078Akira Hatanaka }
1578957481e6a3a4217499f739bae24401576ade078Akira Hatanaka
1588957481e6a3a4217499f739bae24401576ade078Akira Hatanaka  return MCOperand();
1598957481e6a3a4217499f739bae24401576ade078Akira Hatanaka}
1608957481e6a3a4217499f739bae24401576ade078Akira Hatanaka
161dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesMCOperand MipsMCInstLower::createSub(MachineBasicBlock *BB1,
162dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                     MachineBasicBlock *BB2,
163dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                     MCSymbolRefExpr::VariantKind Kind) const {
1646948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCSymbolRefExpr *Sym1 = MCSymbolRefExpr::create(BB1->getSymbol(), *Ctx);
1656948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCSymbolRefExpr *Sym2 = MCSymbolRefExpr::create(BB2->getSymbol(), *Ctx);
1666948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  const MCBinaryExpr *Sub = MCBinaryExpr::createSub(Sym1, Sym2, *Ctx);
167dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
1686948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  return MCOperand::createExpr(MipsMCExpr::create(Kind, Sub, *Ctx));
169dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
170dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
171dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesvoid MipsMCInstLower::
172dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HineslowerLongBranchLUi(const MachineInstr *MI, MCInst &OutMI) const {
173dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  OutMI.setOpcode(Mips::LUi);
174dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
175dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Lower register operand.
176dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  OutMI.addOperand(LowerOperand(MI->getOperand(0)));
177dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
178dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Create %hi($tgt-$baltgt).
179dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  OutMI.addOperand(createSub(MI->getOperand(1).getMBB(),
180dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                             MI->getOperand(2).getMBB(),
181dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                             MCSymbolRefExpr::VK_Mips_ABS_HI));
182dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
183dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
184dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesvoid MipsMCInstLower::
185dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HineslowerLongBranchADDiu(const MachineInstr *MI, MCInst &OutMI, int Opcode,
186dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                     MCSymbolRefExpr::VariantKind Kind) const {
187dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  OutMI.setOpcode(Opcode);
188dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
189dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Lower two register operands.
190dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (unsigned I = 0, E = 2; I != E; ++I) {
191dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MachineOperand &MO = MI->getOperand(I);
192dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    OutMI.addOperand(LowerOperand(MO));
193dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
194dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
195dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Create %lo($tgt-$baltgt) or %hi($tgt-$baltgt).
196dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  OutMI.addOperand(createSub(MI->getOperand(2).getMBB(),
197dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                             MI->getOperand(3).getMBB(), Kind));
198dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
199dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
200dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesbool MipsMCInstLower::lowerLongBranch(const MachineInstr *MI,
201dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                      MCInst &OutMI) const {
202dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  switch (MI->getOpcode()) {
203dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  default:
204dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return false;
205dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case Mips::LONG_BRANCH_LUi:
206dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    lowerLongBranchLUi(MI, OutMI);
207dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return true;
208dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case Mips::LONG_BRANCH_ADDiu:
209dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    lowerLongBranchADDiu(MI, OutMI, Mips::ADDiu,
210dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         MCSymbolRefExpr::VK_Mips_ABS_LO);
211dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return true;
212dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case Mips::LONG_BRANCH_DADDiu:
213dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    unsigned TargetFlags = MI->getOperand(2).getTargetFlags();
214dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (TargetFlags == MipsII::MO_ABS_HI)
215dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      lowerLongBranchADDiu(MI, OutMI, Mips::DADDiu,
216dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                           MCSymbolRefExpr::VK_Mips_ABS_HI);
217dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    else if (TargetFlags == MipsII::MO_ABS_LO)
218dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      lowerLongBranchADDiu(MI, OutMI, Mips::DADDiu,
219dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                           MCSymbolRefExpr::VK_Mips_ABS_LO);
220dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    else
221dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      report_fatal_error("Unexpected flags for LONG_BRANCH_DADDiu");
222dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return true;
223dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
224dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
225dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
22617a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanakavoid MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
227dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (lowerLongBranch(MI, OutMI))
228dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return;
229dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
23017a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  OutMI.setOpcode(MI->getOpcode());
231bb481f882093fb738d2bb15610c79364bada5496Jia Liu
23217a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
23317a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka    const MachineOperand &MO = MI->getOperand(i);
2348957481e6a3a4217499f739bae24401576ade078Akira Hatanaka    MCOperand MCOp = LowerOperand(MO);
23517a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka
2368957481e6a3a4217499f739bae24401576ade078Akira Hatanaka    if (MCOp.isValid())
2378957481e6a3a4217499f739bae24401576ade078Akira Hatanaka      OutMI.addOperand(MCOp);
23817a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka  }
23917a2f8e551dd4a772d389d1a193235ae56f8a399Akira Hatanaka}
240e035f65b16956cdb7ba29e741b7e3c04a8ce4d24Jack Carter
241