1 //===- HexagonMCInstLower.cpp - Convert Hexagon 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 Hexagon MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
16 #include "HexagonAsmPrinter.h"
17 #include "HexagonMachineFunctionInfo.h"
18 #include "MCTargetDesc/HexagonMCInst.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
27 static MCOperand GetSymbolRef(const MachineOperand& MO, const MCSymbol* Symbol,
28 HexagonAsmPrinter& Printer) {
29 MCContext &MC = Printer.OutContext;
32 ME = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, MC);
34 if (!MO.isJTI() && MO.getOffset())
35 ME = MCBinaryExpr::CreateAdd(ME, MCConstantExpr::Create(MO.getOffset(), MC),
38 return (MCOperand::CreateExpr(ME));
41 // Create an MCInst from a MachineInstr
42 void llvm::HexagonLowerToMC(const MachineInstr* MI, HexagonMCInst& MCI,
43 HexagonAsmPrinter& AP) {
44 MCI.setOpcode(MI->getOpcode());
45 MCI.setDesc(MI->getDesc());
47 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
48 const MachineOperand &MO = MI->getOperand(i);
51 switch (MO.getType()) {
54 llvm_unreachable("unknown operand type");
55 case MachineOperand::MO_Register:
56 // Ignore all implicit register operands.
57 if (MO.isImplicit()) continue;
58 MCO = MCOperand::CreateReg(MO.getReg());
60 case MachineOperand::MO_FPImmediate: {
61 APFloat Val = MO.getFPImm()->getValueAPF();
62 // FP immediates are used only when setting GPRs, so they may be dealt
63 // with like regular immediates from this point on.
64 MCO = MCOperand::CreateImm(*Val.bitcastToAPInt().getRawData());
67 case MachineOperand::MO_Immediate:
68 MCO = MCOperand::CreateImm(MO.getImm());
70 case MachineOperand::MO_MachineBasicBlock:
71 MCO = MCOperand::CreateExpr
72 (MCSymbolRefExpr::Create(MO.getMBB()->getSymbol(),
75 case MachineOperand::MO_GlobalAddress:
76 MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP);
78 case MachineOperand::MO_ExternalSymbol:
79 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
82 case MachineOperand::MO_JumpTableIndex:
83 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
85 case MachineOperand::MO_ConstantPoolIndex:
86 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
88 case MachineOperand::MO_BlockAddress:
89 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP);