blob: 8c6e2fe90ac7b7e3d1dd739345f4e6c4acfeb533 [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"
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000016#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000018#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"
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000024#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000025#include "llvm/Support/Mangler.h"
26#include "llvm/ADT/SmallString.h"
27using namespace llvm;
28
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000029MCSymbol *MSP430MCInstLower::
30GetGlobalAddressSymbol(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
45MCSymbol *MSP430MCInstLower::
46GetJumpTableSymbol(const MachineOperand &MO) const {
47 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000048 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
49 << Printer.getFunctionNumber() << '_'
50 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000051
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
61MCSymbol *MSP430MCInstLower::
62GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
63 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000064 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
65 << Printer.getFunctionNumber() << '_'
66 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000067
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
77MCOperand MSP430MCInstLower::
78LowerSymbolOperand(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
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000095void 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:
Anton Korobeynikov3fcbbcd2009-10-21 00:12:44 +0000107 // Ignore all implicit register operands.
108 if (MO.isImplicit()) continue;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000109 MCOp = MCOperand::CreateReg(MO.getReg());
110 break;
111 case MachineOperand::MO_Immediate:
112 MCOp = MCOperand::CreateImm(MO.getImm());
113 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000114 case MachineOperand::MO_MachineBasicBlock:
115 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000116 Printer.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000117 break;
118 case MachineOperand::MO_GlobalAddress:
119 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
120 break;
Anton Korobeynikovbaffc352009-10-21 00:12:08 +0000121#if 0
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000122 case MachineOperand::MO_ExternalSymbol:
123 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
124 break;
Anton Korobeynikovbaffc352009-10-21 00:12:08 +0000125#endif
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000126 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;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000132 }
133
134 OutMI.addOperand(MCOp);
135 }
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000136}