• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
25 using namespace llvm;
26 
MipsMCInstLower(Mangler * mang,const MachineFunction & mf,MipsAsmPrinter & asmprinter)27 MipsMCInstLower::MipsMCInstLower(Mangler *mang, const MachineFunction &mf,
28                                  MipsAsmPrinter &asmprinter)
29   : Ctx(mf.getContext()), Mang(mang), AsmPrinter(asmprinter) {}
30 
LowerSymbolOperand(const MachineOperand & MO,MachineOperandType MOTy) const31 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
32                                               MachineOperandType MOTy) const {
33   MipsMCSymbolRefExpr::VariantKind Kind;
34   const MCSymbol *Symbol;
35   int Offset = 0;
36 
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;
49   }
50 
51   switch (MOTy) {
52     case MachineOperand::MO_MachineBasicBlock:
53       Symbol = MO.getMBB()->getSymbol();
54       break;
55 
56     case MachineOperand::MO_GlobalAddress:
57       Symbol = Mang->getSymbol(MO.getGlobal());
58       break;
59 
60     case MachineOperand::MO_BlockAddress:
61       Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
62       break;
63 
64     case MachineOperand::MO_ExternalSymbol:
65       Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
66       break;
67 
68     case MachineOperand::MO_JumpTableIndex:
69       Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
70       break;
71 
72     case MachineOperand::MO_ConstantPoolIndex:
73       Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
74       if (MO.getOffset())
75         Offset = MO.getOffset();
76       break;
77 
78     default:
79       llvm_unreachable("<unknown operand type>");
80   }
81 
82   return MCOperand::CreateExpr(MipsMCSymbolRefExpr::Create(Kind, Symbol, Offset,
83                                                            Ctx));
84 }
85 
Lower(const MachineInstr * MI,MCInst & OutMI) const86 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
87   OutMI.setOpcode(MI->getOpcode());
88 
89   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
90     const MachineOperand &MO = MI->getOperand(i);
91     MCOperand MCOp;
92     MachineOperandType MOTy = MO.getType();
93 
94     switch (MOTy) {
95     default:
96       MI->dump();
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());
102       break;
103     case MachineOperand::MO_Immediate:
104       MCOp = MCOperand::CreateImm(MO.getImm());
105       break;
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);
113       break;
114     }
115 
116     OutMI.addOperand(MCOp);
117   }
118 }
119