blob: f505b239b6313728052c30f6f9d6ecc103ff715e [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
Anton Korobeynikov616b9bb2009-10-21 00:13:42 +000044MCSymbol *MSP430MCInstLower::
45GetExternalSymbolSymbol(const MachineOperand &MO) const {
46 SmallString<128> Name;
47 Name += Printer.MAI->getGlobalPrefix();
48 Name += MO.getSymbolName();
49
50 switch (MO.getTargetFlags()) {
51 default: assert(0 && "Unknown target flag on GV operand");
52 case 0: break;
53 }
54
55 return Ctx.GetOrCreateSymbol(Name.str());
56}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000057
58MCSymbol *MSP430MCInstLower::
59GetJumpTableSymbol(const MachineOperand &MO) const {
60 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000061 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
62 << Printer.getFunctionNumber() << '_'
63 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000064
65 switch (MO.getTargetFlags()) {
66 default: llvm_unreachable("Unknown target flag on GV operand");
67 case 0: break;
68 }
69
70 // Create a symbol for the name.
71 return Ctx.GetOrCreateSymbol(Name.str());
72}
73
74MCSymbol *MSP430MCInstLower::
75GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
76 SmallString<256> Name;
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000077 raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
78 << Printer.getFunctionNumber() << '_'
79 << MO.getIndex();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000080
81 switch (MO.getTargetFlags()) {
82 default: llvm_unreachable("Unknown target flag on GV operand");
83 case 0: break;
84 }
85
86 // Create a symbol for the name.
87 return Ctx.GetOrCreateSymbol(Name.str());
88}
89
90MCOperand MSP430MCInstLower::
91LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
92 // FIXME: We would like an efficient form for this, so we don't have to do a
93 // lot of extra uniquing.
94 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
95
96 switch (MO.getTargetFlags()) {
97 default: llvm_unreachable("Unknown target flag on GV operand");
98 case 0: break;
99 }
100
101 if (!MO.isJTI() && MO.getOffset())
102 Expr = MCBinaryExpr::CreateAdd(Expr,
103 MCConstantExpr::Create(MO.getOffset(), Ctx),
104 Ctx);
105 return MCOperand::CreateExpr(Expr);
106}
107
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000108void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
109 OutMI.setOpcode(MI->getOpcode());
110
111 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
112 const MachineOperand &MO = MI->getOperand(i);
113
114 MCOperand MCOp;
115 switch (MO.getType()) {
116 default:
117 MI->dump();
118 assert(0 && "unknown operand type");
119 case MachineOperand::MO_Register:
Anton Korobeynikov3fcbbcd2009-10-21 00:12:44 +0000120 // Ignore all implicit register operands.
121 if (MO.isImplicit()) continue;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000122 MCOp = MCOperand::CreateReg(MO.getReg());
123 break;
124 case MachineOperand::MO_Immediate:
125 MCOp = MCOperand::CreateImm(MO.getImm());
126 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000127 case MachineOperand::MO_MachineBasicBlock:
128 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000129 Printer.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000130 break;
131 case MachineOperand::MO_GlobalAddress:
132 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
133 break;
134 case MachineOperand::MO_ExternalSymbol:
135 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
136 break;
137 case MachineOperand::MO_JumpTableIndex:
138 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
139 break;
140 case MachineOperand::MO_ConstantPoolIndex:
141 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
142 break;
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000143 }
144
145 OutMI.addOperand(MCOp);
146 }
Anton Korobeynikov1c7ceed2009-10-21 00:11:08 +0000147}