LLVM API Documentation

HexagonMCInstLower.cpp
Go to the documentation of this file.
00001 //===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains code to lower Hexagon MachineInstrs to their corresponding
00011 // MCInst records.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "Hexagon.h"
00016 #include "HexagonAsmPrinter.h"
00017 #include "HexagonMachineFunctionInfo.h"
00018 #include "MCTargetDesc/HexagonMCInst.h"
00019 #include "llvm/CodeGen/MachineBasicBlock.h"
00020 #include "llvm/IR/Constants.h"
00021 #include "llvm/IR/Mangler.h"
00022 #include "llvm/MC/MCExpr.h"
00023 #include "llvm/MC/MCInst.h"
00024 
00025 using namespace llvm;
00026 
00027 static MCOperand GetSymbolRef(const MachineOperand& MO, const MCSymbol* Symbol,
00028                               HexagonAsmPrinter& Printer) {
00029   MCContext &MC = Printer.OutContext;
00030   const MCExpr *ME;
00031 
00032   ME = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, MC);
00033 
00034   if (!MO.isJTI() && MO.getOffset())
00035     ME = MCBinaryExpr::CreateAdd(ME, MCConstantExpr::Create(MO.getOffset(), MC),
00036                                  MC);
00037 
00038   return (MCOperand::CreateExpr(ME));
00039 }
00040 
00041 // Create an MCInst from a MachineInstr
00042 void llvm::HexagonLowerToMC(const MachineInstr* MI, HexagonMCInst& MCI,
00043                             HexagonAsmPrinter& AP) {
00044   MCI.setOpcode(MI->getOpcode());
00045   MCI.setDesc(MI->getDesc());
00046 
00047   for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
00048     const MachineOperand &MO = MI->getOperand(i);
00049     MCOperand MCO;
00050 
00051     switch (MO.getType()) {
00052     default:
00053       MI->dump();
00054       llvm_unreachable("unknown operand type");
00055     case MachineOperand::MO_Register:
00056       // Ignore all implicit register operands.
00057       if (MO.isImplicit()) continue;
00058       MCO = MCOperand::CreateReg(MO.getReg());
00059       break;
00060     case MachineOperand::MO_FPImmediate: {
00061       APFloat Val = MO.getFPImm()->getValueAPF();
00062       // FP immediates are used only when setting GPRs, so they may be dealt
00063       // with like regular immediates from this point on.
00064       MCO = MCOperand::CreateImm(*Val.bitcastToAPInt().getRawData());
00065       break;
00066     }
00067     case MachineOperand::MO_Immediate:
00068       MCO = MCOperand::CreateImm(MO.getImm());
00069       break;
00070     case MachineOperand::MO_MachineBasicBlock:
00071       MCO = MCOperand::CreateExpr
00072               (MCSymbolRefExpr::Create(MO.getMBB()->getSymbol(),
00073                AP.OutContext));
00074       break;
00075     case MachineOperand::MO_GlobalAddress:
00076       MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP);
00077       break;
00078     case MachineOperand::MO_ExternalSymbol:
00079       MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
00080                          AP);
00081       break;
00082     case MachineOperand::MO_JumpTableIndex:
00083       MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
00084       break;
00085     case MachineOperand::MO_ConstantPoolIndex:
00086       MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
00087       break;
00088     case MachineOperand::MO_BlockAddress:
00089       MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP);
00090       break;
00091     }
00092 
00093     MCI.addOperand(MCO);
00094   }
00095 }