blob: 175b6b3f3f7330b7ee67688a21635509da114b5d [file] [log] [blame]
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +00001//===-- 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"
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000022#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000023#include "llvm/Support/Mangler.h"
24#include "llvm/ADT/SmallString.h"
25using namespace llvm;
26
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000027MCSymbol *MSP430MCInstLower::
28GetGlobalAddressSymbol(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
43MCSymbol *MSP430MCInstLower::
44GetJumpTableSymbol(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
58MCSymbol *MSP430MCInstLower::
59GetConstantPoolIndexSymbol(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
73MCOperand MSP430MCInstLower::
74LowerSymbolOperand(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
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000091void 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:
Anton Korobeynikov3fcbbcd2009-10-21 00:12:44 +0000103 // Ignore all implicit register operands.
104 if (MO.isImplicit()) continue;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000105 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;
Anton Korobeynikovbaffc352009-10-21 00:12:08 +0000115#endif
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000116 case MachineOperand::MO_GlobalAddress:
117 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
118 break;
Anton Korobeynikovbaffc352009-10-21 00:12:08 +0000119#if 0
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000120 case MachineOperand::MO_ExternalSymbol:
121 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
122 break;
Anton Korobeynikovbaffc352009-10-21 00:12:08 +0000123#endif
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000124 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;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000130 }
131
132 OutMI.addOperand(MCOp);
133 }
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000134}