1//===-- 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/ADT/SmallString.h"
17#include "llvm/CodeGen/AsmPrinter.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/Mangler.h"
27using namespace llvm;
28
29MCSymbol *MSP430MCInstLower::
30GetGlobalAddressSymbol(const MachineOperand &MO) const {
31  switch (MO.getTargetFlags()) {
32  default: llvm_unreachable("Unknown target flag on GV operand");
33  case 0: break;
34  }
35
36  return Printer.Mang->getSymbol(MO.getGlobal());
37}
38
39MCSymbol *MSP430MCInstLower::
40GetExternalSymbolSymbol(const MachineOperand &MO) const {
41  switch (MO.getTargetFlags()) {
42  default: llvm_unreachable("Unknown target flag on GV operand");
43  case 0: break;
44  }
45
46  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
47}
48
49MCSymbol *MSP430MCInstLower::
50GetJumpTableSymbol(const MachineOperand &MO) const {
51  SmallString<256> Name;
52  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "JTI"
53                            << Printer.getFunctionNumber() << '_'
54                            << MO.getIndex();
55
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.
62  return Ctx.GetOrCreateSymbol(Name.str());
63}
64
65MCSymbol *MSP430MCInstLower::
66GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
67  SmallString<256> Name;
68  raw_svector_ostream(Name) << Printer.MAI->getPrivateGlobalPrefix() << "CPI"
69                            << Printer.getFunctionNumber() << '_'
70                            << MO.getIndex();
71
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.
78  return Ctx.GetOrCreateSymbol(Name.str());
79}
80
81MCSymbol *MSP430MCInstLower::
82GetBlockAddressSymbol(const MachineOperand &MO) const {
83  switch (MO.getTargetFlags()) {
84  default: llvm_unreachable("Unknown target flag on GV operand");
85  case 0: break;
86  }
87
88  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
89}
90
91MCOperand MSP430MCInstLower::
92LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
93  // FIXME: We would like an efficient form for this, so we don't have to do a
94  // lot of extra uniquing.
95  const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
96
97  switch (MO.getTargetFlags()) {
98  default: llvm_unreachable("Unknown target flag on GV operand");
99  case 0: break;
100  }
101
102  if (!MO.isJTI() && MO.getOffset())
103    Expr = MCBinaryExpr::CreateAdd(Expr,
104                                   MCConstantExpr::Create(MO.getOffset(), Ctx),
105                                   Ctx);
106  return MCOperand::CreateExpr(Expr);
107}
108
109void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
110  OutMI.setOpcode(MI->getOpcode());
111
112  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
113    const MachineOperand &MO = MI->getOperand(i);
114
115    MCOperand MCOp;
116    switch (MO.getType()) {
117    default:
118      MI->dump();
119      llvm_unreachable("unknown operand type");
120    case MachineOperand::MO_Register:
121      // Ignore all implicit register operands.
122      if (MO.isImplicit()) continue;
123      MCOp = MCOperand::CreateReg(MO.getReg());
124      break;
125    case MachineOperand::MO_Immediate:
126      MCOp = MCOperand::CreateImm(MO.getImm());
127      break;
128    case MachineOperand::MO_MachineBasicBlock:
129      MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
130                         MO.getMBB()->getSymbol(), Ctx));
131      break;
132    case MachineOperand::MO_GlobalAddress:
133      MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
134      break;
135    case MachineOperand::MO_ExternalSymbol:
136      MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
137      break;
138    case MachineOperand::MO_JumpTableIndex:
139      MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
140      break;
141    case MachineOperand::MO_ConstantPoolIndex:
142      MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
143      break;
144    case MachineOperand::MO_BlockAddress:
145      MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
146      break;
147    case MachineOperand::MO_RegisterMask:
148      continue;
149    }
150
151    OutMI.addOperand(MCOp);
152  }
153}
154