1 //===-- M68kAsmPrinter.cpp - M68k LLVM Assembly Printer ---------*- C++ -*-===//
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 /// \file
10 /// This file contains a printer that converts from our internal representation
11 /// of machine-dependent LLVM code to GAS-format M68k assembly language.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 // TODO Conform to Motorola ASM syntax
16 
17 #include "M68kAsmPrinter.h"
18 
19 #include "M68k.h"
20 #include "M68kMachineFunction.h"
21 #include "MCTargetDesc/M68kInstPrinter.h"
22 #include "TargetInfo/M68kTargetInfo.h"
23 
24 #include "llvm/MC/TargetRegistry.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "m68k-asm-printer"
29 
30 bool M68kAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
31   MMFI = MF.getInfo<M68kMachineFunctionInfo>();
32   MCInstLowering = std::make_unique<M68kMCInstLower>(MF, *this);
33   AsmPrinter::runOnMachineFunction(MF);
34   return true;
35 }
36 
37 void M68kAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
38                                   raw_ostream &OS) {
39   const MachineOperand &MO = MI->getOperand(OpNum);
40   switch (MO.getType()) {
41   case MachineOperand::MO_Register:
42     OS << "%" << M68kInstPrinter::getRegisterName(MO.getReg());
43     break;
44   case MachineOperand::MO_Immediate:
45     OS << '#' << MO.getImm();
46     break;
47   case MachineOperand::MO_MachineBasicBlock:
48     MO.getMBB()->getSymbol()->print(OS, MAI);
49     break;
50   case MachineOperand::MO_GlobalAddress:
51     PrintSymbolOperand(MO, OS);
52     break;
53   case MachineOperand::MO_BlockAddress:
54     GetBlockAddressSymbol(MO.getBlockAddress())->print(OS, MAI);
55     break;
56   case MachineOperand::MO_ConstantPoolIndex: {
57     const DataLayout &DL = getDataLayout();
58     OS << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
59        << MO.getIndex();
60     break;
61   }
62   default:
63     llvm_unreachable("not implemented");
64   }
65 }
66 
67 bool M68kAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
68                                      const char *ExtraCode, raw_ostream &OS) {
69   // Print the operand if there is no operand modifier.
70   if (!ExtraCode || !ExtraCode[0]) {
71     printOperand(MI, OpNo, OS);
72     return false;
73   }
74 
75   // Fallback to the default implementation.
76   return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
77 }
78 
79 void M68kAsmPrinter::emitInstruction(const MachineInstr *MI) {
80   M68k_MC::verifyInstructionPredicates(MI->getOpcode(),
81                                        getSubtargetInfo().getFeatureBits());
82 
83   switch (MI->getOpcode()) {
84   default: {
85     if (MI->isPseudo()) {
86       LLVM_DEBUG(dbgs() << "Pseudo opcode(" << MI->getOpcode()
87                         << ") found in EmitInstruction()\n");
88       llvm_unreachable("Cannot proceed");
89     }
90     break;
91   }
92   case M68k::TAILJMPj:
93   case M68k::TAILJMPq:
94     // Lower these as normal, but add some comments.
95     OutStreamer->AddComment("TAILCALL");
96     break;
97   }
98 
99   MCInst TmpInst0;
100   MCInstLowering->Lower(MI, TmpInst0);
101   OutStreamer->emitInstruction(TmpInst0, getSubtargetInfo());
102 }
103 
104 void M68kAsmPrinter::emitFunctionBodyStart() {}
105 
106 void M68kAsmPrinter::emitFunctionBodyEnd() {}
107 
108 void M68kAsmPrinter::emitStartOfAsmFile(Module &M) {
109   OutStreamer->emitSyntaxDirective();
110 }
111 
112 void M68kAsmPrinter::emitEndOfAsmFile(Module &M) {}
113 
114 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kAsmPrinter() {
115   RegisterAsmPrinter<M68kAsmPrinter> X(getTheM68kTarget());
116 }
117