1//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to the MSP430 assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MCTargetDesc/MSP430InstPrinter.h"
15#include "MSP430.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/MC/TargetRegistry.h"
36#include "llvm/Support/raw_ostream.h"
37using namespace llvm;
38
39#define DEBUG_TYPE "asm-printer"
40
41namespace {
42 class MSP430AsmPrinter : public AsmPrinter {
43 public:
44 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
45 : AsmPrinter(TM, std::move(Streamer)) {}
46
47 StringRef getPassName() const override { return "MSP430 Assembly Printer"; }
48
49 bool runOnMachineFunction(MachineFunction &MF) override;
50
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,
55 raw_ostream &O);
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;
61
62 void EmitInterruptVectorSection(MachineFunction &ISR);
63 };
64} // end of anonymous namespace
65
66void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
67 raw_ostream &O) {
68 uint64_t Offset = MO.getOffset();
69 if (Offset)
70 O << '(' << Offset << '+';
71
72 getSymbol(GV: MO.getGlobal())->print(OS&: O, MAI);
73
74 if (Offset)
75 O << ')';
76}
77
78void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
79 raw_ostream &O, const char *Modifier) {
80 const MachineOperand &MO = MI->getOperand(i: OpNum);
81 switch (MO.getType()) {
82 default: llvm_unreachable("Not implemented yet!");
83 case MachineOperand::MO_Register:
84 O << MSP430InstPrinter::getRegisterName(Reg: MO.getReg());
85 return;
86 case MachineOperand::MO_Immediate:
87 if (!Modifier || strcmp(s1: Modifier, s2: "nohash"))
88 O << '#';
89 O << MO.getImm();
90 return;
91 case MachineOperand::MO_MachineBasicBlock:
92 MO.getMBB()->getSymbol()->print(OS&: O, MAI);
93 return;
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.
97 // mov.w glb(r1), r2
98 // Otherwise (!) msp430-as will silently miscompile the output :(
99 if (!Modifier || strcmp(s1: Modifier, s2: "nohash"))
100 O << '#';
101 PrintSymbolOperand(MO, O);
102 return;
103 }
104 }
105}
106
107void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
108 raw_ostream &O) {
109 const MachineOperand &Base = MI->getOperand(i: OpNum);
110 const MachineOperand &Disp = MI->getOperand(i: OpNum+1);
111
112 // Print displacement first
113
114 // Imm here is in fact global address - print extra modifier.
115 if (Disp.isImm() && Base.getReg() == MSP430::SR)
116 O << '&';
117 printOperand(MI, OpNum: OpNum+1, O, Modifier: "nohash");
118
119 // Print register base field
120 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) {
121 O << '(';
122 printOperand(MI, OpNum, O);
123 O << ')';
124 }
125}
126
127/// PrintAsmOperand - Print out an operand for an inline asm expression.
128///
129bool 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, OS&: O);
134
135 printOperand(MI, OpNum: OpNo, O);
136 return false;
137}
138
139bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
140 unsigned OpNo,
141 const char *ExtraCode,
142 raw_ostream &O) {
143 if (ExtraCode && ExtraCode[0]) {
144 return true; // Unknown modifier.
145 }
146 printSrcMemOperand(MI, OpNum: OpNo, O);
147 return false;
148}
149
150//===----------------------------------------------------------------------===//
151void MSP430AsmPrinter::emitInstruction(const MachineInstr *MI) {
152 MSP430_MC::verifyInstructionPredicates(MI->getOpcode(),
153 getSubtargetInfo().getFeatureBits());
154
155 MSP430MCInstLower MCInstLowering(OutContext, *this);
156
157 MCInst TmpInst;
158 MCInstLowering.Lower(MI, OutMI&: TmpInst);
159 EmitToStreamer(S&: *OutStreamer, Inst: TmpInst);
160}
161
162void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
163 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
164 const auto *F = &ISR.getFunction();
165 if (F->getCallingConv() != CallingConv::MSP430_INTR) {
166 report_fatal_error(reason: "Functions with 'interrupt' attribute must have msp430_intrcc CC");
167 }
168 StringRef IVIdx = F->getFnAttribute(Kind: "interrupt").getValueAsString();
169 MCSection *IV = OutStreamer->getContext().getELFSection(
170 Section: "__interrupt_vector_" + IVIdx,
171 Type: ELF::SHT_PROGBITS, Flags: ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
172 OutStreamer->switchSection(Section: IV);
173
174 const MCSymbol *FunctionSymbol = getSymbol(GV: F);
175 OutStreamer->emitSymbolValue(Sym: FunctionSymbol, Size: TM.getProgramPointerSize());
176 OutStreamer->switchSection(Section: Cur);
177}
178
179bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
180 // Emit separate section for an interrupt vector if ISR
181 if (MF.getFunction().hasFnAttribute(Kind: "interrupt")) {
182 EmitInterruptVectorSection(ISR&: MF);
183 }
184
185 SetupMachineFunction(MF);
186 emitFunctionBody();
187 return false;
188}
189
190// Force static initialization.
191extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430AsmPrinter() {
192 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());
193}
194

source code of llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp