1 //===-- ARMMCInstLower.cpp - Convert ARM MachineInstr to an 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 ARM MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "ARMMCInstLower.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/Constants.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/Target/Mangler.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/ADT/SmallString.h"
29 MCSymbol *ARMMCInstLower::GetGlobalAddressSymbol(const GlobalValue *GV) const {
30 return Printer.Mang->getSymbol(GV);
33 MCOperand ARMMCInstLower::
34 GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol) const {
36 switch (MO.getTargetFlags()) {
37 default: assert(0 && "Unknown target flag on symbol operand");
39 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, Ctx);
42 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_ARM_LO16, Ctx);
45 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_ARM_HI16, Ctx);
48 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_ARM_PLT, Ctx);
52 if (!MO.isJTI() && MO.getOffset())
53 Expr = MCBinaryExpr::CreateAdd(Expr,
54 MCConstantExpr::Create(MO.getOffset(), Ctx),
56 return MCOperand::CreateExpr(Expr);
60 void ARMMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
61 OutMI.setOpcode(MI->getOpcode());
63 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
64 const MachineOperand &MO = MI->getOperand(i);
67 switch (MO.getType()) {
70 assert(0 && "unknown operand type");
71 case MachineOperand::MO_Register:
72 // Ignore all non-CPSR implicit register operands.
73 if (MO.isImplicit() && MO.getReg() != ARM::CPSR) continue;
74 assert(!MO.getSubReg() && "Subregs should be eliminated!");
75 MCOp = MCOperand::CreateReg(MO.getReg());
77 case MachineOperand::MO_Immediate:
78 MCOp = MCOperand::CreateImm(MO.getImm());
80 case MachineOperand::MO_MachineBasicBlock:
81 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
82 MO.getMBB()->getSymbol(), Ctx));
84 case MachineOperand::MO_GlobalAddress:
85 MCOp = GetSymbolRef(MO, GetGlobalAddressSymbol(MO.getGlobal()));
87 case MachineOperand::MO_ExternalSymbol:
88 MCOp = GetSymbolRef(MO,
89 Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
91 case MachineOperand::MO_JumpTableIndex:
92 MCOp = GetSymbolRef(MO, Printer.GetJTISymbol(MO.getIndex()));
94 case MachineOperand::MO_ConstantPoolIndex:
95 MCOp = GetSymbolRef(MO, Printer.GetCPISymbol(MO.getIndex()));
97 case MachineOperand::MO_BlockAddress:
98 MCOp = GetSymbolRef(MO,
99 Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
101 case MachineOperand::MO_FPImmediate:
102 APFloat Val = MO.getFPImm()->getValueAPF();
104 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
105 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
109 OutMI.addOperand(MCOp);