LLVM API Documentation

SparcMCInstLower.cpp
Go to the documentation of this file.
00001 //===-- SparcMCInstLower.cpp - Convert Sparc MachineInstr to 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 Sparc MachineInstrs to their corresponding
00011 // MCInst records.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "Sparc.h"
00016 #include "MCTargetDesc/SparcMCExpr.h"
00017 #include "llvm/ADT/SmallString.h"
00018 #include "llvm/CodeGen/AsmPrinter.h"
00019 #include "llvm/CodeGen/MachineFunction.h"
00020 #include "llvm/CodeGen/MachineInstr.h"
00021 #include "llvm/CodeGen/MachineOperand.h"
00022 #include "llvm/IR/Mangler.h"
00023 #include "llvm/MC/MCAsmInfo.h"
00024 #include "llvm/MC/MCContext.h"
00025 #include "llvm/MC/MCExpr.h"
00026 #include "llvm/MC/MCInst.h"
00027 
00028 using namespace llvm;
00029 
00030 
00031 static MCOperand LowerSymbolOperand(const MachineInstr *MI,
00032                                     const MachineOperand &MO,
00033                                     AsmPrinter &AP) {
00034 
00035   SparcMCExpr::VariantKind Kind =
00036     (SparcMCExpr::VariantKind)MO.getTargetFlags();
00037   const MCSymbol *Symbol = nullptr;
00038 
00039   switch(MO.getType()) {
00040   default: llvm_unreachable("Unknown type in LowerSymbolOperand");
00041   case MachineOperand::MO_MachineBasicBlock:
00042     Symbol = MO.getMBB()->getSymbol();
00043     break;
00044 
00045   case MachineOperand::MO_GlobalAddress:
00046     Symbol = AP.getSymbol(MO.getGlobal());
00047     break;
00048 
00049   case MachineOperand::MO_BlockAddress:
00050     Symbol = AP.GetBlockAddressSymbol(MO.getBlockAddress());
00051     break;
00052 
00053   case MachineOperand::MO_ExternalSymbol:
00054     Symbol = AP.GetExternalSymbolSymbol(MO.getSymbolName());
00055     break;
00056 
00057   case MachineOperand::MO_ConstantPoolIndex:
00058     Symbol = AP.GetCPISymbol(MO.getIndex());
00059     break;
00060   }
00061 
00062   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol,
00063                                                          AP.OutContext);
00064   const SparcMCExpr *expr = SparcMCExpr::Create(Kind, MCSym,
00065                                                 AP.OutContext);
00066   return MCOperand::CreateExpr(expr);
00067 }
00068 
00069 static MCOperand LowerOperand(const MachineInstr *MI,
00070                               const MachineOperand &MO,
00071                               AsmPrinter &AP) {
00072   switch(MO.getType()) {
00073   default: llvm_unreachable("unknown operand type"); break;
00074   case MachineOperand::MO_Register:
00075     if (MO.isImplicit())
00076       break;
00077     return MCOperand::CreateReg(MO.getReg());
00078 
00079   case MachineOperand::MO_Immediate:
00080     return MCOperand::CreateImm(MO.getImm());
00081 
00082   case MachineOperand::MO_MachineBasicBlock:
00083   case MachineOperand::MO_GlobalAddress:
00084   case MachineOperand::MO_BlockAddress:
00085   case MachineOperand::MO_ExternalSymbol:
00086   case MachineOperand::MO_ConstantPoolIndex:
00087     return LowerSymbolOperand(MI, MO, AP);
00088 
00089   case MachineOperand::MO_RegisterMask:   break;
00090 
00091   }
00092   return MCOperand();
00093 }
00094 
00095 void llvm::LowerSparcMachineInstrToMCInst(const MachineInstr *MI,
00096                                           MCInst &OutMI,
00097                                           AsmPrinter &AP)
00098 {
00099 
00100   OutMI.setOpcode(MI->getOpcode());
00101 
00102   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00103     const MachineOperand &MO = MI->getOperand(i);
00104     MCOperand MCOp = LowerOperand(MI, MO, AP);
00105 
00106     if (MCOp.isValid())
00107       OutMI.addOperand(MCOp);
00108   }
00109 }