Home | History | Annotate | Download | only in Mips
      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 
     27 MipsMCInstLower::MipsMCInstLower(Mangler *mang, const MachineFunction &mf,
     28                                  MipsAsmPrinter &asmprinter)
     29   : Ctx(mf.getContext()), Mang(mang), AsmPrinter(asmprinter) {}
     30 
     31 MCOperand MipsMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
     32                                               MachineOperandType MOTy,
     33                                               unsigned Offset) const {
     34   MipsMCSymbolRefExpr::VariantKind Kind;
     35   const MCSymbol *Symbol;
     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   case MipsII::MO_GPOFF_HI: Kind = MipsMCSymbolRefExpr::VK_Mips_GPOFF_HI; break;
     50   case MipsII::MO_GPOFF_LO: Kind = MipsMCSymbolRefExpr::VK_Mips_GPOFF_LO; break;
     51   case MipsII::MO_GOT_DISP: Kind = MipsMCSymbolRefExpr::VK_Mips_GOT_DISP; break;
     52   case MipsII::MO_GOT_PAGE: Kind = MipsMCSymbolRefExpr::VK_Mips_GOT_PAGE; break;
     53   case MipsII::MO_GOT_OFST: Kind = MipsMCSymbolRefExpr::VK_Mips_GOT_OFST; break;
     54   }
     55 
     56   switch (MOTy) {
     57     case MachineOperand::MO_MachineBasicBlock:
     58       Symbol = MO.getMBB()->getSymbol();
     59       break;
     60 
     61     case MachineOperand::MO_GlobalAddress:
     62       Symbol = Mang->getSymbol(MO.getGlobal());
     63       break;
     64 
     65     case MachineOperand::MO_BlockAddress:
     66       Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
     67       break;
     68 
     69     case MachineOperand::MO_ExternalSymbol:
     70       Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
     71       break;
     72 
     73     case MachineOperand::MO_JumpTableIndex:
     74       Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
     75       break;
     76 
     77     case MachineOperand::MO_ConstantPoolIndex:
     78       Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
     79       if (MO.getOffset())
     80         Offset += MO.getOffset();
     81       break;
     82 
     83     default:
     84       llvm_unreachable("<unknown operand type>");
     85   }
     86 
     87   return MCOperand::CreateExpr(MipsMCSymbolRefExpr::Create(Kind, Symbol, Offset,
     88                                                            Ctx));
     89 }
     90 
     91 MCOperand MipsMCInstLower::LowerOperand(const MachineOperand& MO) const {
     92   MachineOperandType MOTy = MO.getType();
     93 
     94   switch (MOTy) {
     95   default:
     96     assert(0 && "unknown operand type");
     97     break;
     98   case MachineOperand::MO_Register:
     99     // Ignore all implicit register operands.
    100     if (MO.isImplicit()) break;
    101     return MCOperand::CreateReg(MO.getReg());
    102   case MachineOperand::MO_Immediate:
    103     return MCOperand::CreateImm(MO.getImm());
    104   case MachineOperand::MO_MachineBasicBlock:
    105   case MachineOperand::MO_GlobalAddress:
    106   case MachineOperand::MO_ExternalSymbol:
    107   case MachineOperand::MO_JumpTableIndex:
    108   case MachineOperand::MO_ConstantPoolIndex:
    109   case MachineOperand::MO_BlockAddress:
    110     return LowerSymbolOperand(MO, MOTy, 0);
    111  }
    112 
    113   return MCOperand();
    114 }
    115 
    116 void MipsMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
    117   OutMI.setOpcode(MI->getOpcode());
    118 
    119   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
    120     const MachineOperand &MO = MI->getOperand(i);
    121     MCOperand MCOp = LowerOperand(MO);
    122 
    123     if (MCOp.isValid())
    124       OutMI.addOperand(MCOp);
    125   }
    126 }
    127