blob: 65b8984ee1250735980188163614a262e3b688ff [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"
Chris Lattner2acc03c2010-03-12 21:19:23 +000023#include "llvm/Target/Mangler.h"
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000024#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000025#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000026#include "llvm/ADT/SmallString.h"
27using namespace llvm;
28
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000029MCSymbol *MSP430MCInstLower::
30GetGlobalAddressSymbol(const MachineOperand &MO) const {
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000031 switch (MO.getTargetFlags()) {
John McCallb5586a12009-12-18 00:27:18 +000032 default: llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000033 case 0: break;
34 }
35
Chris Lattner2acc03c2010-03-12 21:19:23 +000036 return Printer.Mang->getSymbol(MO.getGlobal());
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000037}
38
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000039MCSymbol *MSP430MCInstLower::
40GetExternalSymbolSymbol(const MachineOperand &MO) const {
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000041 switch (MO.getTargetFlags()) {
42 default: assert(0 && "Unknown target flag on GV operand");
43 case 0: break;
44 }
45
Chris Lattner86974d42010-01-16 01:40:07 +000046 return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000047}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000048
49MCSymbol *MSP430MCInstLower::
50GetJumpTableSymbol(const MachineOperand &MO) const {
51 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000052 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
53 << Printer.getFunctionNumber() << '_'
54 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000055
56 switch (MO.getTargetFlags()) {
57 default: llvm_unreachable("Unknown target flag on GV operand");
58 case 0: break;
59 }
60
61 // Create a symbol for the name.
Chris Lattner72121e42010-03-10 02:25:11 +000062 return Ctx.GetOrCreateTemporarySymbol(Name.str());
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000063}
64
65MCSymbol *MSP430MCInstLower::
66GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
67 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000068 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
69 << Printer.getFunctionNumber() << '_'
70 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000071
72 switch (MO.getTargetFlags()) {
73 default: llvm_unreachable("Unknown target flag on GV operand");
74 case 0: break;
75 }
76
77 // Create a symbol for the name.
Chris Lattner72121e42010-03-10 02:25:11 +000078 return Ctx.GetOrCreateTemporarySymbol(Name.str());
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000079}
80
81MCOperand MSP430MCInstLower::
82LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
83 // FIXME: We would like an efficient form for this, so we don't have to do a
84 // lot of extra uniquing.
85 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
86
87 switch (MO.getTargetFlags()) {
88 default: llvm_unreachable("Unknown target flag on GV operand");
89 case 0: break;
90 }
91
92 if (!MO.isJTI() && MO.getOffset())
93 Expr = MCBinaryExpr::CreateAdd(Expr,
94 MCConstantExpr::Create(MO.getOffset(), Ctx),
95 Ctx);
96 return MCOperand::CreateExpr(Expr);
97}
98
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +000099void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
100 OutMI.setOpcode(MI->getOpcode());
101
102 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
103 const MachineOperand &MO = MI->getOperand(i);
104
105 MCOperand MCOp;
106 switch (MO.getType()) {
107 default:
108 MI->dump();
109 assert(0 && "unknown operand type");
110 case MachineOperand::MO_Register:
Anton Korobeynikov3fcbbcd2009-10-21 00:12:44 +0000111 // Ignore all implicit register operands.
112 if (MO.isImplicit()) continue;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000113 MCOp = MCOperand::CreateReg(MO.getReg());
114 break;
115 case MachineOperand::MO_Immediate:
116 MCOp = MCOperand::CreateImm(MO.getImm());
117 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000118 case MachineOperand::MO_MachineBasicBlock:
119 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
Chris Lattner84d5ca92010-01-26 04:55:51 +0000120 MO.getMBB()->getSymbol(Printer.OutContext), Ctx));
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000121 break;
122 case MachineOperand::MO_GlobalAddress:
123 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
124 break;
125 case MachineOperand::MO_ExternalSymbol:
126 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
127 break;
128 case MachineOperand::MO_JumpTableIndex:
129 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
130 break;
131 case MachineOperand::MO_ConstantPoolIndex:
132 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
133 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000134 }
135
136 OutMI.addOperand(MCOp);
137 }
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000138}