1 //===- ARCMCInstLower.cpp - ARC MachineInstr to MCInst ----------*- C++ -*-===//
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 /// \file
11 /// This file contains code to lower ARC MachineInstrs to their
12 /// corresponding MCInst records.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "ARCMCInstLower.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineOperand.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24
25 using namespace llvm;
26
ARCMCInstLower(MCContext * C,AsmPrinter & AsmPrinter)27 ARCMCInstLower::ARCMCInstLower(MCContext *C, AsmPrinter &AsmPrinter)
28 : Ctx(C), Printer(AsmPrinter) {}
29
LowerSymbolOperand(const MachineOperand & MO,MachineOperandType MOTy,unsigned Offset) const30 MCOperand ARCMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
31 MachineOperandType MOTy,
32 unsigned Offset) const {
33 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
34 const MCSymbol *Symbol;
35
36 switch (MOTy) {
37 case MachineOperand::MO_MachineBasicBlock:
38 Symbol = MO.getMBB()->getSymbol();
39 break;
40 case MachineOperand::MO_GlobalAddress:
41 Symbol = Printer.getSymbol(MO.getGlobal());
42 Offset += MO.getOffset();
43 break;
44 case MachineOperand::MO_BlockAddress:
45 Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress());
46 Offset += MO.getOffset();
47 break;
48 case MachineOperand::MO_ExternalSymbol:
49 Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName());
50 Offset += MO.getOffset();
51 break;
52 case MachineOperand::MO_JumpTableIndex:
53 Symbol = Printer.GetJTISymbol(MO.getIndex());
54 break;
55 case MachineOperand::MO_ConstantPoolIndex:
56 Symbol = Printer.GetCPISymbol(MO.getIndex());
57 Offset += MO.getOffset();
58 break;
59 default:
60 llvm_unreachable("<unknown operand type>");
61 }
62
63 assert(Symbol && "Symbol creation failed.\n");
64 const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx);
65
66 if (!Offset)
67 return MCOperand::createExpr(MCSym);
68
69 // Assume offset is never negative.
70 assert(Offset > 0);
71
72 const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx);
73 const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx);
74 return MCOperand::createExpr(Add);
75 }
76
LowerOperand(const MachineOperand & MO,unsigned Offset) const77 MCOperand ARCMCInstLower::LowerOperand(const MachineOperand &MO,
78 unsigned Offset) const {
79 MachineOperandType MOTy = MO.getType();
80
81 switch (MOTy) {
82 default:
83 llvm_unreachable("unknown operand type");
84 case MachineOperand::MO_Register:
85 // Ignore all implicit register operands.
86 if (MO.isImplicit())
87 break;
88 return MCOperand::createReg(MO.getReg());
89 case MachineOperand::MO_Immediate:
90 return MCOperand::createImm(MO.getImm() + Offset);
91 case MachineOperand::MO_MachineBasicBlock:
92 case MachineOperand::MO_GlobalAddress:
93 case MachineOperand::MO_ExternalSymbol:
94 case MachineOperand::MO_JumpTableIndex:
95 case MachineOperand::MO_ConstantPoolIndex:
96 case MachineOperand::MO_BlockAddress:
97 return LowerSymbolOperand(MO, MOTy, Offset);
98 case MachineOperand::MO_RegisterMask:
99 break;
100 }
101
102 return {};
103 }
104
Lower(const MachineInstr * MI,MCInst & OutMI) const105 void ARCMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
106 OutMI.setOpcode(MI->getOpcode());
107
108 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
109 const MachineOperand &MO = MI->getOperand(i);
110 MCOperand MCOp = LowerOperand(MO);
111
112 if (MCOp.isValid())
113 OutMI.addOperand(MCOp);
114 }
115 }
116