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 //===----------------------------------------------------------------------===//
16 #include "ARMAsmPrinter.h"
17 #include "MCTargetDesc/ARMMCExpr.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/Mangler.h"
26 MCOperand ARMAsmPrinter::GetSymbolRef(const MachineOperand &MO,
27 const MCSymbol *Symbol) {
29 switch (MO.getTargetFlags()) {
31 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
33 switch (MO.getTargetFlags()) {
34 default: llvm_unreachable("Unknown target flag on symbol operand");
38 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
40 Expr = ARMMCExpr::CreateLower16(Expr, OutContext);
43 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
45 Expr = ARMMCExpr::CreateUpper16(Expr, OutContext);
52 Expr = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_ARM_PLT,
57 if (!MO.isJTI() && MO.getOffset())
58 Expr = MCBinaryExpr::CreateAdd(Expr,
59 MCConstantExpr::Create(MO.getOffset(),
62 return MCOperand::CreateExpr(Expr);
66 bool ARMAsmPrinter::lowerOperand(const MachineOperand &MO,
68 switch (MO.getType()) {
69 default: llvm_unreachable("unknown operand type");
70 case MachineOperand::MO_Register:
71 // Ignore all non-CPSR implicit register operands.
72 if (MO.isImplicit() && MO.getReg() != ARM::CPSR)
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(), OutContext));
84 case MachineOperand::MO_GlobalAddress:
85 MCOp = GetSymbolRef(MO, Mang->getSymbol(MO.getGlobal()));
87 case MachineOperand::MO_ExternalSymbol:
88 MCOp = GetSymbolRef(MO,
89 GetExternalSymbolSymbol(MO.getSymbolName()));
91 case MachineOperand::MO_JumpTableIndex:
92 MCOp = GetSymbolRef(MO, GetJTISymbol(MO.getIndex()));
94 case MachineOperand::MO_ConstantPoolIndex:
95 MCOp = GetSymbolRef(MO, GetCPISymbol(MO.getIndex()));
97 case MachineOperand::MO_BlockAddress:
98 MCOp = GetSymbolRef(MO, GetBlockAddressSymbol(MO.getBlockAddress()));
100 case MachineOperand::MO_FPImmediate: {
101 APFloat Val = MO.getFPImm()->getValueAPF();
103 Val.convert(APFloat::IEEEdouble, APFloat::rmTowardZero, &ignored);
104 MCOp = MCOperand::CreateFPImm(Val.convertToDouble());
107 case MachineOperand::MO_RegisterMask:
108 // Ignore call clobbers.
114 void llvm::LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
116 OutMI.setOpcode(MI->getOpcode());
118 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
119 const MachineOperand &MO = MI->getOperand(i);
122 if (AP.lowerOperand(MO, MCOp))
123 OutMI.addOperand(MCOp);