1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
13 //===----------------------------------------------------------------------===//
15 #include "InstPrinter/MSP430InstPrinter.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCInstLower.h"
19 #include "MSP430TargetMachine.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 printOperand(const MachineInstr *MI, int OpNum,
52 raw_ostream &O, const char* Modifier = nullptr);
53 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
55 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode,
57 raw_ostream &O) override;
58 bool PrintAsmMemoryOperand(const MachineInstr *MI,
59 unsigned OpNo, unsigned AsmVariant,
60 const char *ExtraCode, raw_ostream &O) override;
61 void EmitInstruction(const MachineInstr *MI) override;
63 void EmitInterruptVectorSection(MachineFunction &ISR);
65 } // end of anonymous namespace
68 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
69 raw_ostream &O, const char *Modifier) {
70 const MachineOperand &MO = MI->getOperand(OpNum);
71 switch (MO.getType()) {
72 default: llvm_unreachable("Not implemented yet!");
73 case MachineOperand::MO_Register:
74 O << MSP430InstPrinter::getRegisterName(MO.getReg());
76 case MachineOperand::MO_Immediate:
77 if (!Modifier || strcmp(Modifier, "nohash"))
81 case MachineOperand::MO_MachineBasicBlock:
82 MO.getMBB()->getSymbol()->print(O, MAI);
84 case MachineOperand::MO_GlobalAddress: {
85 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
86 uint64_t Offset = MO.getOffset();
88 // If the global address expression is a part of displacement field with a
89 // register base, we should not emit any prefix symbol here, e.g.
93 // Otherwise (!) msp430-as will silently miscompile the output :(
94 if (!Modifier || strcmp(Modifier, "nohash"))
95 O << (isMemOp ? '&' : '#');
97 O << '(' << Offset << '+';
99 getSymbol(MO.getGlobal())->print(O, MAI);
109 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
111 const MachineOperand &Base = MI->getOperand(OpNum);
112 const MachineOperand &Disp = MI->getOperand(OpNum+1);
114 // Print displacement first
116 // Imm here is in fact global address - print extra modifier.
117 if (Disp.isImm() && !Base.getReg())
119 printOperand(MI, OpNum+1, O, "nohash");
121 // Print register base field
124 printOperand(MI, OpNum, O);
129 /// PrintAsmOperand - Print out an operand for an inline asm expression.
131 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
133 const char *ExtraCode, raw_ostream &O) {
134 // Does this asm operand have a single letter operand modifier?
135 if (ExtraCode && ExtraCode[0])
136 return true; // Unknown modifier.
138 printOperand(MI, OpNo, O);
142 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
143 unsigned OpNo, unsigned AsmVariant,
144 const char *ExtraCode,
146 if (ExtraCode && ExtraCode[0]) {
147 return true; // Unknown modifier.
149 printSrcMemOperand(MI, OpNo, O);
153 //===----------------------------------------------------------------------===//
154 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
155 MSP430MCInstLower MCInstLowering(OutContext, *this);
158 MCInstLowering.Lower(MI, TmpInst);
159 EmitToStreamer(*OutStreamer, TmpInst);
162 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) {
163 MCSection *Cur = OutStreamer->getCurrentSectionOnly();
164 const auto *F = &ISR.getFunction();
165 assert(F->hasFnAttribute("interrupt") &&
166 "Functions with MSP430_INTR CC should have 'interrupt' attribute");
167 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString();
168 MCSection *IV = OutStreamer->getContext().getELFSection(
169 "__interrupt_vector_" + IVIdx,
170 ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
171 OutStreamer->SwitchSection(IV);
173 const MCSymbol *FunctionSymbol = getSymbol(F);
174 OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize());
175 OutStreamer->SwitchSection(Cur);
178 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
179 // Emit separate section for an interrupt vector if ISR
180 if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR)
181 EmitInterruptVectorSection(MF);
183 SetupMachineFunction(MF);
188 // Force static initialization.
189 extern "C" void LLVMInitializeMSP430AsmPrinter() {
190 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target());