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 "InstPrinter/MSP430InstPrinter.h" 15 #include "MSP430.h" 16 #include "MSP430InstrInfo.h" 17 #include "MSP430MCInstLower.h" 18 #include "MSP430TargetMachine.h" 19 #include "llvm/BinaryFormat/ELF.h" 20 #include "llvm/CodeGen/AsmPrinter.h" 21 #include "llvm/CodeGen/MachineConstantPool.h" 22 #include "llvm/CodeGen/MachineFunctionPass.h" 23 #include "llvm/CodeGen/MachineInstr.h" 24 #include "llvm/CodeGen/MachineModuleInfo.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/DerivedTypes.h" 27 #include "llvm/IR/Mangler.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/MC/MCAsmInfo.h" 30 #include "llvm/MC/MCInst.h" 31 #include "llvm/MC/MCSectionELF.h" 32 #include "llvm/MC/MCStreamer.h" 33 #include "llvm/MC/MCSymbol.h" 34 #include "llvm/Support/TargetRegistry.h" 35 #include "llvm/Support/raw_ostream.h" 36 using namespace llvm; 37 38 #define DEBUG_TYPE "asm-printer" 39 40 namespace { 41 class MSP430AsmPrinter : public AsmPrinter { 42 public: 43 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) 44 : AsmPrinter(TM, std::move(Streamer)) {} 45 46 StringRef getPassName() const override { return "MSP430 Assembly Printer"; } 47 48 bool runOnMachineFunction(MachineFunction &MF) override; 49 50 void printOperand(const MachineInstr *MI, int OpNum, 51 raw_ostream &O, const char* Modifier = nullptr); 52 void printSrcMemOperand(const MachineInstr *MI, int OpNum, 53 raw_ostream &O); 54 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 55 const char *ExtraCode, raw_ostream &O) override; 56 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 57 const char *ExtraCode, raw_ostream &O) override; 58 void EmitInstruction(const MachineInstr *MI) override; 59 60 void EmitInterruptVectorSection(MachineFunction &ISR); 61 }; 62 } // end of anonymous namespace 63 64 65 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum, 66 raw_ostream &O, const char *Modifier) { 67 const MachineOperand &MO = MI->getOperand(OpNum); 68 switch (MO.getType()) { 69 default: llvm_unreachable("Not implemented yet!"); 70 case MachineOperand::MO_Register: 71 O << MSP430InstPrinter::getRegisterName(MO.getReg()); 72 return; 73 case MachineOperand::MO_Immediate: 74 if (!Modifier || strcmp(Modifier, "nohash")) 75 O << '#'; 76 O << MO.getImm(); 77 return; 78 case MachineOperand::MO_MachineBasicBlock: 79 MO.getMBB()->getSymbol()->print(O, MAI); 80 return; 81 case MachineOperand::MO_GlobalAddress: { 82 bool isMemOp = Modifier && !strcmp(Modifier, "mem"); 83 uint64_t Offset = MO.getOffset(); 84 85 // If the global address expression is a part of displacement field with a 86 // register base, we should not emit any prefix symbol here, e.g. 87 // mov.w &foo, r1 88 // vs 89 // mov.w glb(r1), r2 90 // Otherwise (!) msp430-as will silently miscompile the output :( 91 if (!Modifier || strcmp(Modifier, "nohash")) 92 O << (isMemOp ? '&' : '#'); 93 if (Offset) 94 O << '(' << Offset << '+'; 95 96 getSymbol(MO.getGlobal())->print(O, MAI); 97 98 if (Offset) 99 O << ')'; 100 101 return; 102 } 103 } 104 } 105 106 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum, 107 raw_ostream &O) { 108 const MachineOperand &Base = MI->getOperand(OpNum); 109 const MachineOperand &Disp = MI->getOperand(OpNum+1); 110 111 // Print displacement first 112 113 // Imm here is in fact global address - print extra modifier. 114 if (Disp.isImm() && Base.getReg() == MSP430::SR) 115 O << '&'; 116 printOperand(MI, OpNum+1, O, "nohash"); 117 118 // Print register base field 119 if (Base.getReg() != MSP430::SR && Base.getReg() != MSP430::PC) { 120 O << '('; 121 printOperand(MI, OpNum, O); 122 O << ')'; 123 } 124 } 125 126 /// PrintAsmOperand - Print out an operand for an inline asm expression. 127 /// 128 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 129 const char *ExtraCode, raw_ostream &O) { 130 // Does this asm operand have a single letter operand modifier? 131 if (ExtraCode && ExtraCode[0]) 132 return true; // Unknown modifier. 133 134 printOperand(MI, OpNo, O); 135 return false; 136 } 137 138 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 139 unsigned OpNo, 140 const char *ExtraCode, 141 raw_ostream &O) { 142 if (ExtraCode && ExtraCode[0]) { 143 return true; // Unknown modifier. 144 } 145 printSrcMemOperand(MI, OpNo, O); 146 return false; 147 } 148 149 //===----------------------------------------------------------------------===// 150 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) { 151 MSP430MCInstLower MCInstLowering(OutContext, *this); 152 153 MCInst TmpInst; 154 MCInstLowering.Lower(MI, TmpInst); 155 EmitToStreamer(*OutStreamer, TmpInst); 156 } 157 158 void MSP430AsmPrinter::EmitInterruptVectorSection(MachineFunction &ISR) { 159 MCSection *Cur = OutStreamer->getCurrentSectionOnly(); 160 const auto *F = &ISR.getFunction(); 161 assert(F->hasFnAttribute("interrupt") && 162 "Functions with MSP430_INTR CC should have 'interrupt' attribute"); 163 StringRef IVIdx = F->getFnAttribute("interrupt").getValueAsString(); 164 MCSection *IV = OutStreamer->getContext().getELFSection( 165 "__interrupt_vector_" + IVIdx, 166 ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_EXECINSTR); 167 OutStreamer->SwitchSection(IV); 168 169 const MCSymbol *FunctionSymbol = getSymbol(F); 170 OutStreamer->EmitSymbolValue(FunctionSymbol, TM.getProgramPointerSize()); 171 OutStreamer->SwitchSection(Cur); 172 } 173 174 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 175 // Emit separate section for an interrupt vector if ISR 176 if (MF.getFunction().getCallingConv() == CallingConv::MSP430_INTR) 177 EmitInterruptVectorSection(MF); 178 179 SetupMachineFunction(MF); 180 EmitFunctionBody(); 181 return false; 182 } 183 184 // Force static initialization. 185 extern "C" void LLVMInitializeMSP430AsmPrinter() { 186 RegisterAsmPrinter<MSP430AsmPrinter> X(getTheMSP430Target()); 187 } 188