1 //===-- MipsMCInstLower.cpp - Convert Mips MachineInstr to MCInst ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower Mips MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
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"
29 MipsMCInstLower::MipsMCInstLower(MipsAsmPrinter &asmprinter)
30 : AsmPrinter(asmprinter) {}
32 void MipsMCInstLower::Initialize(Mangler *M, MCContext* C) {
37 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
38 MachineOperandType MOTy,
39 unsigned Offset) const {
40 MCSymbolRefExpr::VariantKind Kind;
41 const MCSymbol *Symbol;
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;
67 case MachineOperand::MO_MachineBasicBlock:
68 Symbol = MO.getMBB()->getSymbol();
71 case MachineOperand::MO_GlobalAddress:
72 Symbol = Mang->getSymbol(MO.getGlobal());
73 Offset += MO.getOffset();
76 case MachineOperand::MO_BlockAddress:
77 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
78 Offset += MO.getOffset();
81 case MachineOperand::MO_ExternalSymbol:
82 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
83 Offset += MO.getOffset();
86 case MachineOperand::MO_JumpTableIndex:
87 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
90 case MachineOperand::MO_ConstantPoolIndex:
91 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
92 Offset += MO.getOffset();
96 llvm_unreachable("<unknown operand type>");
99 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol, Kind, *Ctx);
102 return MCOperand::CreateExpr(MCSym);
104 // Assume offset is never negative.
107 const MCConstantExpr *OffsetExpr = MCConstantExpr::Create(Offset, *Ctx);
108 const MCBinaryExpr *AddExpr = MCBinaryExpr::CreateAdd(MCSym, OffsetExpr, *Ctx);
109 return MCOperand::CreateExpr(AddExpr);
112 static void CreateMCInst(MCInst& Inst, unsigned Opc, const MCOperand& Opnd0,
113 const MCOperand& Opnd1,
114 const MCOperand& Opnd2 = MCOperand()) {
116 Inst.addOperand(Opnd0);
117 Inst.addOperand(Opnd1);
119 Inst.addOperand(Opnd2);
122 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO,
123 unsigned offset) const {
124 MachineOperandType MOTy = MO.getType();
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:
148 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
149 OutMI.setOpcode(MI->getOpcode());
151 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
152 const MachineOperand &MO = MI->getOperand(i);
153 MCOperand MCOp = LowerOperand(MO);
156 OutMI.addOperand(MCOp);
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;
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);
176 CreateMCInst(MCInsts[0], Mips::LUi, RegOpnd, SymHi);
177 CreateMCInst(MCInsts[1], Mips::ADDiu, RegOpnd, RegOpnd, SymLo);