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