1 //===-- MSP430MCInstLower.cpp - Convert MSP430 MachineInstr to an MCInst---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to lower MSP430 MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
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"
27 MCSymbol *MSP430MCInstLower::
28 GetGlobalAddressSymbol(const MachineOperand &MO) const {
29 const GlobalValue *GV = MO.getGlobal();
31 SmallString<128> Name;
32 Mang.getNameWithPrefix(Name, GV, false);
34 switch (MO.getTargetFlags()) {
35 default: llvm_unreachable(0 && "Unknown target flag on GV operand");
39 return Ctx.GetOrCreateSymbol(Name.str());
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();
49 switch (MO.getTargetFlags()) {
50 default: llvm_unreachable("Unknown target flag on GV operand");
54 // Create a symbol for the name.
55 return Ctx.GetOrCreateSymbol(Name.str());
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();
64 switch (MO.getTargetFlags()) {
65 default: llvm_unreachable("Unknown target flag on GV operand");
69 // Create a symbol for the name.
70 return Ctx.GetOrCreateSymbol(Name.str());
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);
79 switch (MO.getTargetFlags()) {
80 default: llvm_unreachable("Unknown target flag on GV operand");
84 if (!MO.isJTI() && MO.getOffset())
85 Expr = MCBinaryExpr::CreateAdd(Expr,
86 MCConstantExpr::Create(MO.getOffset(), Ctx),
88 return MCOperand::CreateExpr(Expr);
91 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
92 OutMI.setOpcode(MI->getOpcode());
94 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
95 const MachineOperand &MO = MI->getOperand(i);
98 switch (MO.getType()) {
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());
107 case MachineOperand::MO_Immediate:
108 MCOp = MCOperand::CreateImm(MO.getImm());
111 case MachineOperand::MO_MachineBasicBlock:
112 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
113 AsmPrinter.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
116 case MachineOperand::MO_GlobalAddress:
117 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
120 case MachineOperand::MO_ExternalSymbol:
121 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
124 case MachineOperand::MO_JumpTableIndex:
125 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
127 case MachineOperand::MO_ConstantPoolIndex:
128 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
132 OutMI.addOperand(MCOp);