Ignore all implicit reg operands
[oota-llvm.git] / lib / Target / MSP430 / AsmPrinter / MSP430MCInstLower.cpp
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/CodeGen/MachineInstr.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/ADT/SmallString.h"
25 using namespace llvm;
26
27 MCSymbol *MSP430MCInstLower::
28 GetGlobalAddressSymbol(const MachineOperand &MO) const {
29   const GlobalValue *GV = MO.getGlobal();
30
31   SmallString<128> Name;
32   Mang.getNameWithPrefix(Name, GV, false);
33
34   switch (MO.getTargetFlags()) {
35   default: llvm_unreachable(0 && "Unknown target flag on GV operand");
36   case 0: break;
37   }
38
39   return Ctx.GetOrCreateSymbol(Name.str());
40 }
41
42
43 MCSymbol *MSP430MCInstLower::
44 GetJumpTableSymbol(const MachineOperand &MO) const {
45   SmallString<256> Name;
46   raw_svector_ostream(Name) << MAI.getPrivateGlobalPrefix() << "JTI"
47     << CurFunctionNumber << '_' << MO.getIndex();
48
49   switch (MO.getTargetFlags()) {
50   default: llvm_unreachable("Unknown target flag on GV operand");
51   case 0: break;
52   }
53
54   // Create a symbol for the name.
55   return Ctx.GetOrCreateSymbol(Name.str());
56 }
57
58 MCSymbol *MSP430MCInstLower::
59 GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
60   SmallString<256> Name;
61   raw_svector_ostream(Name) << MAI.getPrivateGlobalPrefix() << "CPI"
62     << CurFunctionNumber << '_' << MO.getIndex();
63
64   switch (MO.getTargetFlags()) {
65   default: llvm_unreachable("Unknown target flag on GV operand");
66   case 0: break;
67   }
68
69   // Create a symbol for the name.
70   return Ctx.GetOrCreateSymbol(Name.str());
71 }
72
73 MCOperand MSP430MCInstLower::
74 LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
75   // FIXME: We would like an efficient form for this, so we don't have to do a
76   // lot of extra uniquing.
77   const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
78
79   switch (MO.getTargetFlags()) {
80   default: llvm_unreachable("Unknown target flag on GV operand");
81   case 0: break;
82   }
83
84   if (!MO.isJTI() && MO.getOffset())
85     Expr = MCBinaryExpr::CreateAdd(Expr,
86                                    MCConstantExpr::Create(MO.getOffset(), Ctx),
87                                    Ctx);
88   return MCOperand::CreateExpr(Expr);
89 }
90
91 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
92   OutMI.setOpcode(MI->getOpcode());
93
94   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
95     const MachineOperand &MO = MI->getOperand(i);
96
97     MCOperand MCOp;
98     switch (MO.getType()) {
99     default:
100       MI->dump();
101       assert(0 && "unknown operand type");
102     case MachineOperand::MO_Register:
103       // Ignore all implicit register operands.
104       if (MO.isImplicit()) continue;
105       MCOp = MCOperand::CreateReg(MO.getReg());
106       break;
107     case MachineOperand::MO_Immediate:
108       MCOp = MCOperand::CreateImm(MO.getImm());
109       break;
110 #if 0
111     case MachineOperand::MO_MachineBasicBlock:
112       MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
113                        AsmPrinter.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
114       break;
115 #endif
116     case MachineOperand::MO_GlobalAddress:
117       MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
118       break;
119 #if 0
120     case MachineOperand::MO_ExternalSymbol:
121       MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
122       break;
123 #endif
124     case MachineOperand::MO_JumpTableIndex:
125       MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
126       break;
127     case MachineOperand::MO_ConstantPoolIndex:
128       MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
129       break;
130     }
131
132     OutMI.addOperand(MCOp);
133   }
134 }