/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Target/XCore/XCoreMCInstLower.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- XCoreMCInstLower.cpp - Convert XCore MachineInstr to 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 | | /// \file |
11 | | /// \brief This file contains code to lower XCore MachineInstrs to their |
12 | | /// corresponding MCInst records. |
13 | | /// |
14 | | //===----------------------------------------------------------------------===// |
15 | | #include "XCoreMCInstLower.h" |
16 | | #include "llvm/CodeGen/AsmPrinter.h" |
17 | | #include "llvm/CodeGen/MachineFunction.h" |
18 | | #include "llvm/CodeGen/MachineInstr.h" |
19 | | #include "llvm/CodeGen/MachineOperand.h" |
20 | | #include "llvm/IR/Mangler.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 | | |
27 | | XCoreMCInstLower::XCoreMCInstLower(class AsmPrinter &asmprinter) |
28 | 69 | : Printer(asmprinter) {} |
29 | | |
30 | 275 | void XCoreMCInstLower::Initialize(MCContext *C) { Ctx = C; } |
31 | | |
32 | | MCOperand XCoreMCInstLower::LowerSymbolOperand(const MachineOperand &MO, |
33 | | MachineOperandType MOTy, |
34 | 239 | unsigned Offset) const { |
35 | 239 | MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; |
36 | 239 | const MCSymbol *Symbol; |
37 | 239 | |
38 | 239 | switch (MOTy) { |
39 | 34 | case MachineOperand::MO_MachineBasicBlock: |
40 | 34 | Symbol = MO.getMBB()->getSymbol(); |
41 | 34 | break; |
42 | 135 | case MachineOperand::MO_GlobalAddress: |
43 | 135 | Symbol = Printer.getSymbol(MO.getGlobal()); |
44 | 135 | Offset += MO.getOffset(); |
45 | 135 | break; |
46 | 5 | case MachineOperand::MO_BlockAddress: |
47 | 5 | Symbol = Printer.GetBlockAddressSymbol(MO.getBlockAddress()); |
48 | 5 | Offset += MO.getOffset(); |
49 | 5 | break; |
50 | 35 | case MachineOperand::MO_ExternalSymbol: |
51 | 35 | Symbol = Printer.GetExternalSymbolSymbol(MO.getSymbolName()); |
52 | 35 | Offset += MO.getOffset(); |
53 | 35 | break; |
54 | 0 | case MachineOperand::MO_JumpTableIndex: |
55 | 0 | Symbol = Printer.GetJTISymbol(MO.getIndex()); |
56 | 0 | break; |
57 | 30 | case MachineOperand::MO_ConstantPoolIndex: |
58 | 30 | Symbol = Printer.GetCPISymbol(MO.getIndex()); |
59 | 30 | Offset += MO.getOffset(); |
60 | 30 | break; |
61 | 0 | default: |
62 | 0 | llvm_unreachable("<unknown operand type>"); |
63 | 239 | } |
64 | 239 | |
65 | 239 | const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Symbol, Kind, *Ctx); |
66 | 239 | |
67 | 239 | if (!Offset) |
68 | 223 | return MCOperand::createExpr(MCSym); |
69 | 16 | |
70 | 16 | // Assume offset is never negative. |
71 | 239 | assert(Offset > 0); |
72 | 16 | |
73 | 16 | const MCConstantExpr *OffsetExpr = MCConstantExpr::create(Offset, *Ctx); |
74 | 16 | const MCBinaryExpr *Add = MCBinaryExpr::createAdd(MCSym, OffsetExpr, *Ctx); |
75 | 16 | return MCOperand::createExpr(Add); |
76 | 16 | } |
77 | | |
78 | | MCOperand XCoreMCInstLower::LowerOperand(const MachineOperand &MO, |
79 | 4.99k | unsigned offset) const { |
80 | 4.99k | MachineOperandType MOTy = MO.getType(); |
81 | 4.99k | |
82 | 4.99k | switch (MOTy) { |
83 | 0 | default: 0 llvm_unreachable0 ("unknown operand type"); |
84 | 3.74k | case MachineOperand::MO_Register: |
85 | 3.74k | // Ignore all implicit register operands. |
86 | 3.74k | if (MO.isImplicit()3.74k ) break2.16k ; |
87 | 1.57k | return MCOperand::createReg(MO.getReg()); |
88 | 1.01k | case MachineOperand::MO_Immediate: |
89 | 1.01k | return MCOperand::createImm(MO.getImm() + offset); |
90 | 239 | case MachineOperand::MO_MachineBasicBlock: |
91 | 239 | case MachineOperand::MO_GlobalAddress: |
92 | 239 | case MachineOperand::MO_ExternalSymbol: |
93 | 239 | case MachineOperand::MO_JumpTableIndex: |
94 | 239 | case MachineOperand::MO_ConstantPoolIndex: |
95 | 239 | case MachineOperand::MO_BlockAddress: |
96 | 239 | return LowerSymbolOperand(MO, MOTy, offset); |
97 | 0 | case MachineOperand::MO_RegisterMask: |
98 | 0 | break; |
99 | 2.16k | } |
100 | 2.16k | |
101 | 2.16k | return MCOperand(); |
102 | 2.16k | } |
103 | | |
104 | 1.56k | void XCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { |
105 | 1.56k | OutMI.setOpcode(MI->getOpcode()); |
106 | 1.56k | |
107 | 6.55k | for (unsigned i = 0, e = MI->getNumOperands(); i != e6.55k ; ++i4.99k ) { |
108 | 4.99k | const MachineOperand &MO = MI->getOperand(i); |
109 | 4.99k | MCOperand MCOp = LowerOperand(MO); |
110 | 4.99k | |
111 | 4.99k | if (MCOp.isValid()) |
112 | 2.83k | OutMI.addOperand(MCOp); |
113 | 4.99k | } |
114 | 1.56k | } |