1 //===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to lower Hexagon MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Hexagon.h"
16 #include "HexagonAsmPrinter.h"
17 #include "HexagonMachineFunctionInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/Mangler.h"
23
24 using namespace llvm;
25
GetSymbolRef(const MachineOperand & MO,const MCSymbol * Symbol,HexagonAsmPrinter & Printer)26 static MCOperand GetSymbolRef(const MachineOperand& MO, const MCSymbol* Symbol,
27 HexagonAsmPrinter& Printer) {
28 MCContext &MC = Printer.OutContext;
29 const MCExpr *ME;
30
31 ME = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, MC);
32
33 if (!MO.isJTI() && MO.getOffset())
34 ME = MCBinaryExpr::CreateAdd(ME, MCConstantExpr::Create(MO.getOffset(), MC),
35 MC);
36
37 return (MCOperand::CreateExpr(ME));
38 }
39
40 // Create an MCInst from a MachineInstr
HexagonLowerToMC(const MachineInstr * MI,MCInst & MCI,HexagonAsmPrinter & AP)41 void llvm::HexagonLowerToMC(const MachineInstr* MI, MCInst& MCI,
42 HexagonAsmPrinter& AP) {
43 MCI.setOpcode(MI->getOpcode());
44
45 for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
46 const MachineOperand &MO = MI->getOperand(i);
47 MCOperand MCO;
48
49 switch (MO.getType()) {
50 default:
51 MI->dump();
52 llvm_unreachable("unknown operand type");
53 case MachineOperand::MO_Register:
54 // Ignore all implicit register operands.
55 if (MO.isImplicit()) continue;
56 MCO = MCOperand::CreateReg(MO.getReg());
57 break;
58 case MachineOperand::MO_FPImmediate: {
59 APFloat Val = MO.getFPImm()->getValueAPF();
60 // FP immediates are used only when setting GPRs, so they may be dealt
61 // with like regular immediates from this point on.
62 MCO = MCOperand::CreateImm(*Val.bitcastToAPInt().getRawData());
63 break;
64 }
65 case MachineOperand::MO_Immediate:
66 MCO = MCOperand::CreateImm(MO.getImm());
67 break;
68 case MachineOperand::MO_MachineBasicBlock:
69 MCO = MCOperand::CreateExpr
70 (MCSymbolRefExpr::Create(MO.getMBB()->getSymbol(),
71 AP.OutContext));
72 break;
73 case MachineOperand::MO_GlobalAddress:
74 MCO = GetSymbolRef(MO, AP.Mang->getSymbol(MO.getGlobal()), AP);
75 break;
76 case MachineOperand::MO_ExternalSymbol:
77 MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
78 AP);
79 break;
80 case MachineOperand::MO_JumpTableIndex:
81 MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP);
82 break;
83 case MachineOperand::MO_ConstantPoolIndex:
84 MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP);
85 break;
86 case MachineOperand::MO_BlockAddress:
87 MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()),AP);
88 break;
89 }
90
91 MCI.addOperand(MCO);
92 }
93 }
94