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/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"
29 MCSymbol *MSP430MCInstLower::
30 GetGlobalAddressSymbol(const MachineOperand &MO) const {
31 const GlobalValue *GV = MO.getGlobal();
33 SmallString<128> Name;
34 Mang.getNameWithPrefix(Name, GV, false);
36 switch (MO.getTargetFlags()) {
37 default: llvm_unreachable(0 && "Unknown target flag on GV operand");
41 return Ctx.GetOrCreateSymbol(Name.str());
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() << '_'
52 switch (MO.getTargetFlags()) {
53 default: llvm_unreachable("Unknown target flag on GV operand");
57 // Create a symbol for the name.
58 return Ctx.GetOrCreateSymbol(Name.str());
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() << '_'
68 switch (MO.getTargetFlags()) {
69 default: llvm_unreachable("Unknown target flag on GV operand");
73 // Create a symbol for the name.
74 return Ctx.GetOrCreateSymbol(Name.str());
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);
83 switch (MO.getTargetFlags()) {
84 default: llvm_unreachable("Unknown target flag on GV operand");
88 if (!MO.isJTI() && MO.getOffset())
89 Expr = MCBinaryExpr::CreateAdd(Expr,
90 MCConstantExpr::Create(MO.getOffset(), Ctx),
92 return MCOperand::CreateExpr(Expr);
95 void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
96 OutMI.setOpcode(MI->getOpcode());
98 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
99 const MachineOperand &MO = MI->getOperand(i);
102 switch (MO.getType()) {
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());
111 case MachineOperand::MO_Immediate:
112 MCOp = MCOperand::CreateImm(MO.getImm());
114 case MachineOperand::MO_MachineBasicBlock:
115 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
116 Printer.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
118 case MachineOperand::MO_GlobalAddress:
119 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
122 case MachineOperand::MO_ExternalSymbol:
123 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
126 case MachineOperand::MO_JumpTableIndex:
127 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
129 case MachineOperand::MO_ConstantPoolIndex:
130 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
134 OutMI.addOperand(MCOp);