Checkpoint MCInst printer. We (almostly) able to print global / JT / constpool entries
[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       MCOp = MCOperand::CreateReg(MO.getReg());
104       break;
105     case MachineOperand::MO_Immediate:
106       MCOp = MCOperand::CreateImm(MO.getImm());
107       break;
108 #if 0
109     case MachineOperand::MO_MachineBasicBlock:
110       MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
111                        AsmPrinter.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
112       break;
113 #endif
114     case MachineOperand::MO_GlobalAddress:
115       MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
116       break;
117 #if 0
118     case MachineOperand::MO_ExternalSymbol:
119       MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
120       break;
121 #endif
122     case MachineOperand::MO_JumpTableIndex:
123       MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
124       break;
125     case MachineOperand::MO_ConstantPoolIndex:
126       MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
127       break;
128     }
129
130     OutMI.addOperand(MCOp);
131   }
132 }