blob: 4eb7f3d9cf53fc44ab7628c9cc78cc94b9e7c4bb [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/ADT/SmallString.h"
26using namespace llvm;
27
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000028MCSymbol *MSP430MCInstLower::
29GetGlobalAddressSymbol(const MachineOperand &MO) const {
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000030 switch (MO.getTargetFlags()) {
John McCallb5586a12009-12-18 00:27:18 +000031 default: llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000032 case 0: break;
33 }
34
Chris Lattner86974d42010-01-16 01:40:07 +000035 return Printer.GetGlobalValueSymbol(MO.getGlobal());
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000036}
37
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000038MCSymbol *MSP430MCInstLower::
39GetExternalSymbolSymbol(const MachineOperand &MO) const {
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000040 switch (MO.getTargetFlags()) {
41 default: assert(0 && "Unknown target flag on GV operand");
42 case 0: break;
43 }
44
Chris Lattner86974d42010-01-16 01:40:07 +000045 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000046}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000047
48MCSymbol *MSP430MCInstLower::
49GetJumpTableSymbol(const MachineOperand &MO) const {
50 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000051 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
52 << Printer.getFunctionNumber() << '_'
53 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000054
55 switch (MO.getTargetFlags()) {
56 default: llvm_unreachable("Unknown target flag on GV operand");
57 case 0: break;
58 }
59
60 // Create a symbol for the name.
61 return Ctx.GetOrCreateSymbol(Name.str());
62}
63
64MCSymbol *MSP430MCInstLower::
65GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
66 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000067 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
68 << Printer.getFunctionNumber() << '_'
69 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000070
71 switch (MO.getTargetFlags()) {
72 default: llvm_unreachable("Unknown target flag on GV operand");
73 case 0: break;
74 }
75
76 // Create a symbol for the name.
77 return Ctx.GetOrCreateSymbol(Name.str());
78}
79
80MCOperand MSP430MCInstLower::
81LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
82 // FIXME: We would like an efficient form for this, so we don't have to do a
83 // lot of extra uniquing.
84 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
85
86 switch (MO.getTargetFlags()) {
87 default: llvm_unreachable("Unknown target flag on GV operand");
88 case 0: break;
89 }
90
91 if (!MO.isJTI() && MO.getOffset())
92 Expr = MCBinaryExpr::CreateAdd(Expr,
93 MCConstantExpr::Create(MO.getOffset(), Ctx),
94 Ctx);
95 return MCOperand::CreateExpr(Expr);
96}
97
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000098void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
99 OutMI.setOpcode(MI->getOpcode());
100
101 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
102 const MachineOperand &MO = MI->getOperand(i);
103
104 MCOperand MCOp;
105 switch (MO.getType()) {
106 default:
107 MI->dump();
108 assert(0 && "unknown operand type");
109 case MachineOperand::MO_Register:
Anton Korobeynikov3fcbbcd2009-10-21 00:12:44 +0000110 // Ignore all implicit register operands.
111 if (MO.isImplicit()) continue;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000112 MCOp = MCOperand::CreateReg(MO.getReg());
113 break;
114 case MachineOperand::MO_Immediate:
115 MCOp = MCOperand::CreateImm(MO.getImm());
116 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000117 case MachineOperand::MO_MachineBasicBlock:
118 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
Chris Lattner84d5ca92010-01-26 04:55:51 +0000119 MO.getMBB()->getSymbol(Printer.OutContext), Ctx));
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000120 break;
121 case MachineOperand::MO_GlobalAddress:
122 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
123 break;
124 case MachineOperand::MO_ExternalSymbol:
125 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
126 break;
127 case MachineOperand::MO_JumpTableIndex:
128 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
129 break;
130 case MachineOperand::MO_ConstantPoolIndex:
131 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
132 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000133 }
134
135 OutMI.addOperand(MCOp);
136 }
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000137}