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 "MipsMCSymbolRefExpr.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/MCInst.h"
24 #include "llvm/Target/Mangler.h"
27 MipsMCInstLower::MipsMCInstLower(Mangler
*mang
, const MachineFunction
&mf
,
28 MipsAsmPrinter
&asmprinter
)
29 : Ctx(mf
.getContext()), Mang(mang
), AsmPrinter(asmprinter
) {}
31 MCOperand
MipsMCInstLower::LowerSymbolOperand(const MachineOperand
&MO
,
32 MachineOperandType MOTy
) const {
33 MipsMCSymbolRefExpr::VariantKind Kind
;
34 const MCSymbol
*Symbol
;
37 switch(MO
.getTargetFlags()) {
38 default: assert(0 && "Invalid target flag!");
39 case MipsII::MO_NO_FLAG
: Kind
= MipsMCSymbolRefExpr::VK_Mips_None
; break;
40 case MipsII::MO_GPREL
: Kind
= MipsMCSymbolRefExpr::VK_Mips_GPREL
; break;
41 case MipsII::MO_GOT_CALL
: Kind
= MipsMCSymbolRefExpr::VK_Mips_GOT_CALL
; break;
42 case MipsII::MO_GOT
: Kind
= MipsMCSymbolRefExpr::VK_Mips_GOT
; break;
43 case MipsII::MO_ABS_HI
: Kind
= MipsMCSymbolRefExpr::VK_Mips_ABS_HI
; break;
44 case MipsII::MO_ABS_LO
: Kind
= MipsMCSymbolRefExpr::VK_Mips_ABS_LO
; break;
45 case MipsII::MO_TLSGD
: Kind
= MipsMCSymbolRefExpr::VK_Mips_TLSGD
; break;
46 case MipsII::MO_GOTTPREL
: Kind
= MipsMCSymbolRefExpr::VK_Mips_GOTTPREL
; break;
47 case MipsII::MO_TPREL_HI
: Kind
= MipsMCSymbolRefExpr::VK_Mips_TPREL_HI
; break;
48 case MipsII::MO_TPREL_LO
: Kind
= MipsMCSymbolRefExpr::VK_Mips_TPREL_LO
; break;
52 case MachineOperand::MO_MachineBasicBlock
:
53 Symbol
= MO
.getMBB()->getSymbol();
56 case MachineOperand::MO_GlobalAddress
:
57 Symbol
= Mang
->getSymbol(MO
.getGlobal());
60 case MachineOperand::MO_BlockAddress
:
61 Symbol
= AsmPrinter
.GetBlockAddressSymbol(MO
.getBlockAddress());
64 case MachineOperand::MO_ExternalSymbol
:
65 Symbol
= AsmPrinter
.GetExternalSymbolSymbol(MO
.getSymbolName());
68 case MachineOperand::MO_JumpTableIndex
:
69 Symbol
= AsmPrinter
.GetJTISymbol(MO
.getIndex());
72 case MachineOperand::MO_ConstantPoolIndex
:
73 Symbol
= AsmPrinter
.GetCPISymbol(MO
.getIndex());
75 Offset
= MO
.getOffset();
79 llvm_unreachable("<unknown operand type>");
82 return MCOperand::CreateExpr(MipsMCSymbolRefExpr::Create(Kind
, Symbol
, Offset
,
86 void MipsMCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
87 OutMI
.setOpcode(MI
->getOpcode());
89 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
90 const MachineOperand
&MO
= MI
->getOperand(i
);
92 MachineOperandType MOTy
= MO
.getType();
97 llvm_unreachable("unknown operand type");
98 case MachineOperand::MO_Register
:
99 // Ignore all implicit register operands.
100 if (MO
.isImplicit()) continue;
101 MCOp
= MCOperand::CreateReg(MO
.getReg());
103 case MachineOperand::MO_Immediate
:
104 MCOp
= MCOperand::CreateImm(MO
.getImm());
106 case MachineOperand::MO_MachineBasicBlock
:
107 case MachineOperand::MO_GlobalAddress
:
108 case MachineOperand::MO_ExternalSymbol
:
109 case MachineOperand::MO_JumpTableIndex
:
110 case MachineOperand::MO_ConstantPoolIndex
:
111 case MachineOperand::MO_BlockAddress
:
112 MCOp
= LowerSymbolOperand(MO
, MOTy
);
116 OutMI
.addOperand(MCOp
);