1 //===-- MSP430MCInstLower.cpp - Convert MSP430 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 MSP430 MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "MSP430MCInstLower.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetMachine.h"
29 using namespace llvm;
30
31 MCSymbol *MSP430MCInstLower::
GetGlobalAddressSymbol(const MachineOperand & MO) const32 GetGlobalAddressSymbol(const MachineOperand &MO) const {
33 switch (MO.getTargetFlags()) {
34 default: llvm_unreachable("Unknown target flag on GV operand");
35 case 0: break;
36 }
37
38 return Printer.getSymbol(MO.getGlobal());
39 }
40
41 MCSymbol *MSP430MCInstLower::
GetExternalSymbolSymbol(const MachineOperand & MO) const42 GetExternalSymbolSymbol(const MachineOperand &MO) const {
43 switch (MO.getTargetFlags()) {
44 default: llvm_unreachable("Unknown target flag on GV operand");
45 case 0: break;
46 }
47
48 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
49 }
50
51 MCSymbol *MSP430MCInstLower::
GetJumpTableSymbol(const MachineOperand & MO) const52 GetJumpTableSymbol(const MachineOperand &MO) const {
53 const DataLayout *DL = Printer.TM.getDataLayout();
54 SmallString<256> Name;
55 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "JTI"
56 << Printer.getFunctionNumber() << '_'
57 << MO.getIndex();
58
59 switch (MO.getTargetFlags()) {
60 default: llvm_unreachable("Unknown target flag on GV operand");
61 case 0: break;
62 }
63
64 // Create a symbol for the name.
65 return Ctx.GetOrCreateSymbol(Name.str());
66 }
67
68 MCSymbol *MSP430MCInstLower::
GetConstantPoolIndexSymbol(const MachineOperand & MO) const69 GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
70 const DataLayout *DL = Printer.TM.getDataLayout();
71 SmallString<256> Name;
72 raw_svector_ostream(Name) << DL->getPrivateGlobalPrefix() << "CPI"
73 << Printer.getFunctionNumber() << '_'
74 << MO.getIndex();
75
76 switch (MO.getTargetFlags()) {
77 default: llvm_unreachable("Unknown target flag on GV operand");
78 case 0: break;
79 }
80
81 // Create a symbol for the name.
82 return Ctx.GetOrCreateSymbol(Name.str());
83 }
84
85 MCSymbol *MSP430MCInstLower::
GetBlockAddressSymbol(const MachineOperand & MO) const86 GetBlockAddressSymbol(const MachineOperand &MO) const {
87 switch (MO.getTargetFlags()) {
88 default: llvm_unreachable("Unknown target flag on GV operand");
89 case 0: break;
90 }
91
92 return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
93 }
94
95 MCOperand MSP430MCInstLower::
LowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym) const96 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
97 // FIXME: We would like an efficient form for this, so we don't have to do a
98 // lot of extra uniquing.
99 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
100
101 switch (MO.getTargetFlags()) {
102 default: llvm_unreachable("Unknown target flag on GV operand");
103 case 0: break;
104 }
105
106 if (!MO.isJTI() && MO.getOffset())
107 Expr = MCBinaryExpr::CreateAdd(Expr,
108 MCConstantExpr::Create(MO.getOffset(), Ctx),
109 Ctx);
110 return MCOperand::CreateExpr(Expr);
111 }
112
Lower(const MachineInstr * MI,MCInst & OutMI) const113 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
114 OutMI.setOpcode(MI->getOpcode());
115
116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
117 const MachineOperand &MO = MI->getOperand(i);
118
119 MCOperand MCOp;
120 switch (MO.getType()) {
121 default:
122 MI->dump();
123 llvm_unreachable("unknown operand type");
124 case MachineOperand::MO_Register:
125 // Ignore all implicit register operands.
126 if (MO.isImplicit()) continue;
127 MCOp = MCOperand::CreateReg(MO.getReg());
128 break;
129 case MachineOperand::MO_Immediate:
130 MCOp = MCOperand::CreateImm(MO.getImm());
131 break;
132 case MachineOperand::MO_MachineBasicBlock:
133 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
134 MO.getMBB()->getSymbol(), Ctx));
135 break;
136 case MachineOperand::MO_GlobalAddress:
137 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
138 break;
139 case MachineOperand::MO_ExternalSymbol:
140 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
141 break;
142 case MachineOperand::MO_JumpTableIndex:
143 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
144 break;
145 case MachineOperand::MO_ConstantPoolIndex:
146 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
147 break;
148 case MachineOperand::MO_BlockAddress:
149 MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
150 break;
151 case MachineOperand::MO_RegisterMask:
152 continue;
153 }
154
155 OutMI.addOperand(MCOp);
156 }
157 }
158