1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
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 a printer that converts from our internal representation
10 // of machine-dependent LLVM code to the MSP430 assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/MSP430InstPrinter.h"
16 #include "MSP430InstrInfo.h"
17 #include "MSP430MCInstLower.h"
18 #include "MSP430TargetMachine.h"
19 #include "TargetInfo/MSP430TargetInfo.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Mangler.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCInst.h"
32 #include "llvm/MC/MCSectionELF.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
39 #define DEBUG_TYPE "asm-printer"
42 class MSP430AsmPrinter
: public AsmPrinter
{
44 MSP430AsmPrinter(TargetMachine
&TM
, std::unique_ptr
<MCStreamer
> Streamer
)
45 : AsmPrinter(TM
, std::move(Streamer
)) {}
47 StringRef
getPassName() const override
{ return "MSP430 Assembly Printer"; }
49 bool runOnMachineFunction(MachineFunction
&MF
) override
;
51 void PrintSymbolOperand(const MachineOperand
&MO
, raw_ostream
&O
) override
;
52 void printOperand(const MachineInstr
*MI
, int OpNum
,
53 raw_ostream
&O
, const char* Modifier
= nullptr);
54 void printSrcMemOperand(const MachineInstr
*MI
, int OpNum
,
56 bool PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
57 const char *ExtraCode
, raw_ostream
&O
) override
;
58 bool PrintAsmMemoryOperand(const MachineInstr
*MI
, unsigned OpNo
,
59 const char *ExtraCode
, raw_ostream
&O
) override
;
60 void EmitInstruction(const MachineInstr
*MI
) override
;
62 void EmitInterruptVectorSection(MachineFunction
&ISR
);
64 } // end of anonymous namespace
66 void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand
&MO
,
68 uint64_t Offset
= MO
.getOffset();
70 O
<< '(' << Offset
<< '+';
72 getSymbol(MO
.getGlobal())->print(O
, MAI
);
78 void MSP430AsmPrinter::printOperand(const MachineInstr
*MI
, int OpNum
,
79 raw_ostream
&O
, const char *Modifier
) {
80 const MachineOperand
&MO
= MI
->getOperand(OpNum
);
81 switch (MO
.getType()) {
82 default: llvm_unreachable("Not implemented yet!");
83 case MachineOperand::MO_Register
:
84 O
<< MSP430InstPrinter::getRegisterName(MO
.getReg());
86 case MachineOperand::MO_Immediate
:
87 if (!Modifier
|| strcmp(Modifier
, "nohash"))
91 case MachineOperand::MO_MachineBasicBlock
:
92 MO
.getMBB()->getSymbol()->print(O
, MAI
);
94 case MachineOperand::MO_GlobalAddress
: {
95 // If the global address expression is a part of displacement field with a
96 // register base, we should not emit any prefix symbol here, e.g.
98 // Otherwise (!) msp430-as will silently miscompile the output :(
99 if (!Modifier
|| strcmp(Modifier
, "nohash"))
101 PrintSymbolOperand(MO
, O
);
107 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr
*MI
, int OpNum
,
109 const MachineOperand
&Base
= MI
->getOperand(OpNum
);
110 const MachineOperand
&Disp
= MI
->getOperand(OpNum
+1);
112 // Print displacement first
114 // Imm here is in fact global address - print extra modifier.
115 if (Disp
.isImm() && Base
.getReg() == MSP430::SR
)
117 printOperand(MI
, OpNum
+1, O
, "nohash");
119 // Print register base field
120 if (Base
.getReg() != MSP430::SR
&& Base
.getReg() != MSP430::PC
) {
122 printOperand(MI
, OpNum
, O
);
127 /// PrintAsmOperand - Print out an operand for an inline asm expression.
129 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr
*MI
, unsigned OpNo
,
130 const char *ExtraCode
, raw_ostream
&O
) {
131 // Does this asm operand have a single letter operand modifier?
132 if (ExtraCode
&& ExtraCode
[0])
133 return AsmPrinter::PrintAsmOperand(MI
, OpNo
, ExtraCode
, O
);
135 printOperand(MI
, OpNo
, O
);
139 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr
*MI
,
141 const char *ExtraCode
,
143 if (ExtraCode
&& ExtraCode
[0]) {
144 return true; // Unknown modifier.
146 printSrcMemOperand(MI
, OpNo
, O
);
150 //===----------------------------------------------------------------------===//
151 void MSP430AsmPrinter::EmitInstruction(const MachineInstr
*MI
) {
152 MSP430MCInstLower
MCInstLowering(OutContext
, *this);
155 MCInstLowering
.Lower(MI
, TmpInst
);
156 EmitToStreamer(*OutStreamer
, TmpInst
);
159 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction
&ISR
) {
160 MCSection
*Cur
= OutStreamer
->getCurrentSectionOnly();
161 const auto *F
= &ISR
.getFunction();
162 assert(F
->hasFnAttribute("interrupt") &&
163 "Functions with MSP430_INTR CC should have 'interrupt' attribute");
164 StringRef IVIdx
= F
->getFnAttribute("interrupt").getValueAsString();
165 MCSection
*IV
= OutStreamer
->getContext().getELFSection(
166 "__interrupt_vector_" + IVIdx
,
167 ELF::SHT_PROGBITS
, ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
);
168 OutStreamer
->SwitchSection(IV
);
170 const MCSymbol
*FunctionSymbol
= getSymbol(F
);
171 OutStreamer
->EmitSymbolValue(FunctionSymbol
, TM
.getProgramPointerSize());
172 OutStreamer
->SwitchSection(Cur
);
175 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction
&MF
) {
176 // Emit separate section for an interrupt vector if ISR
177 if (MF
.getFunction().getCallingConv() == CallingConv::MSP430_INTR
)
178 EmitInterruptVectorSection(MF
);
180 SetupMachineFunction(MF
);
185 // Force static initialization.
186 extern "C" void LLVMInitializeMSP430AsmPrinter() {
187 RegisterAsmPrinter
<MSP430AsmPrinter
> X(getTheMSP430Target());