1 //===-- MSP430MCInstLower.cpp - Convert MSP430 MachineInstr to an MCInst --===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains code to lower MSP430 MachineInstrs to their corresponding
12 //===----------------------------------------------------------------------===//
14 #include "MSP430MCInstLower.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetMachine.h"
30 MCSymbol
*MSP430MCInstLower::
31 GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
32 switch (MO
.getTargetFlags()) {
33 default: llvm_unreachable("Unknown target flag on GV operand");
37 return Printer
.getSymbol(MO
.getGlobal());
40 MCSymbol
*MSP430MCInstLower::
41 GetExternalSymbolSymbol(const MachineOperand
&MO
) const {
42 switch (MO
.getTargetFlags()) {
43 default: llvm_unreachable("Unknown target flag on GV operand");
47 return Printer
.GetExternalSymbolSymbol(MO
.getSymbolName());
50 MCSymbol
*MSP430MCInstLower::
51 GetJumpTableSymbol(const MachineOperand
&MO
) const {
52 const DataLayout
&DL
= Printer
.getDataLayout();
53 SmallString
<256> Name
;
54 raw_svector_ostream(Name
) << DL
.getPrivateGlobalPrefix() << "JTI"
55 << Printer
.getFunctionNumber() << '_'
58 switch (MO
.getTargetFlags()) {
59 default: llvm_unreachable("Unknown target flag on GV operand");
63 // Create a symbol for the name.
64 return Ctx
.getOrCreateSymbol(Name
);
67 MCSymbol
*MSP430MCInstLower::
68 GetConstantPoolIndexSymbol(const MachineOperand
&MO
) const {
69 const DataLayout
&DL
= Printer
.getDataLayout();
70 SmallString
<256> Name
;
71 raw_svector_ostream(Name
) << DL
.getPrivateGlobalPrefix() << "CPI"
72 << Printer
.getFunctionNumber() << '_'
75 switch (MO
.getTargetFlags()) {
76 default: llvm_unreachable("Unknown target flag on GV operand");
80 // Create a symbol for the name.
81 return Ctx
.getOrCreateSymbol(Name
);
84 MCSymbol
*MSP430MCInstLower::
85 GetBlockAddressSymbol(const MachineOperand
&MO
) const {
86 switch (MO
.getTargetFlags()) {
87 default: llvm_unreachable("Unknown target flag on GV operand");
91 return Printer
.GetBlockAddressSymbol(MO
.getBlockAddress());
94 MCOperand
MSP430MCInstLower::
95 LowerSymbolOperand(const MachineOperand
&MO
, MCSymbol
*Sym
) const {
96 // FIXME: We would like an efficient form for this, so we don't have to do a
97 // lot of extra uniquing.
98 const MCExpr
*Expr
= MCSymbolRefExpr::create(Sym
, Ctx
);
100 switch (MO
.getTargetFlags()) {
101 default: llvm_unreachable("Unknown target flag on GV operand");
105 if (!MO
.isJTI() && MO
.getOffset())
106 Expr
= MCBinaryExpr::createAdd(Expr
,
107 MCConstantExpr::create(MO
.getOffset(), Ctx
),
109 return MCOperand::createExpr(Expr
);
112 #define GET_REGINFO_ENUM
113 #include "MSP430GenRegisterInfo.inc"
115 void MSP430MCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
116 OutMI
.setOpcode(MI
->getOpcode());
118 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
119 const MachineOperand
&MO
= MI
->getOperand(i
);
122 switch (MO
.getType()) {
125 llvm_unreachable("unknown operand type");
126 case MachineOperand::MO_Register
:
127 // Ignore all implicit register operands.
128 if (MO
.isImplicit()) continue;
129 MCOp
= MCOperand::createReg(MO
.getReg());
131 case MachineOperand::MO_Immediate
:
132 MCOp
= MCOperand::createImm(MO
.getImm());
134 case MachineOperand::MO_MachineBasicBlock
:
135 MCOp
= MCOperand::createExpr(MCSymbolRefExpr::create(
136 MO
.getMBB()->getSymbol(), Ctx
));
138 case MachineOperand::MO_GlobalAddress
:
139 MCOp
= LowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
141 case MachineOperand::MO_ExternalSymbol
:
142 MCOp
= LowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
144 case MachineOperand::MO_JumpTableIndex
:
145 MCOp
= LowerSymbolOperand(MO
, GetJumpTableSymbol(MO
));
147 case MachineOperand::MO_ConstantPoolIndex
:
148 MCOp
= LowerSymbolOperand(MO
, GetConstantPoolIndexSymbol(MO
));
150 case MachineOperand::MO_BlockAddress
:
151 MCOp
= LowerSymbolOperand(MO
, GetBlockAddressSymbol(MO
));
153 case MachineOperand::MO_RegisterMask
:
157 OutMI
.addOperand(MCOp
);