1 //===-- M68kMCTargetDesc.cpp - M68k Target Descriptions ---------*- 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 provides M68k target specific descriptions.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "M68kMCTargetDesc.h"
15 #include "M68kInstPrinter.h"
16 #include "M68kMCAsmInfo.h"
17 #include "TargetInfo/M68kTargetInfo.h"
18 
19 #include "llvm/MC/MCELFStreamer.h"
20 #include "llvm/MC/MCInstPrinter.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MachineLocation.h"
26 #include "llvm/MC/TargetRegistry.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/FormattedStream.h"
30 
31 using namespace llvm;
32 
33 #define GET_INSTRINFO_MC_DESC
34 #define ENABLE_INSTR_PREDICATE_VERIFIER
35 #include "M68kGenInstrInfo.inc"
36 
37 #define GET_SUBTARGETINFO_MC_DESC
38 #include "M68kGenSubtargetInfo.inc"
39 
40 #define GET_REGINFO_MC_DESC
41 #include "M68kGenRegisterInfo.inc"
42 
43 // TODO Implement feature set parsing logics
44 static std::string ParseM68kTriple(const Triple &TT, StringRef CPU) {
45   return "";
46 }
47 
48 static MCInstrInfo *createM68kMCInstrInfo() {
49   MCInstrInfo *X = new MCInstrInfo();
50   InitM68kMCInstrInfo(X); // defined in M68kGenInstrInfo.inc
51   return X;
52 }
53 
54 static MCRegisterInfo *createM68kMCRegisterInfo(const Triple &TT) {
55   MCRegisterInfo *X = new MCRegisterInfo();
56   InitM68kMCRegisterInfo(X, llvm::M68k::A0, 0, 0, llvm::M68k::PC);
57   return X;
58 }
59 
60 static MCSubtargetInfo *createM68kMCSubtargetInfo(const Triple &TT,
61                                                   StringRef CPU, StringRef FS) {
62   std::string ArchFS = ParseM68kTriple(TT, CPU);
63   if (!FS.empty()) {
64     if (!ArchFS.empty()) {
65       ArchFS = (ArchFS + "," + FS).str();
66     } else {
67       ArchFS = FS.str();
68     }
69   }
70   return createM68kMCSubtargetInfoImpl(TT, CPU, /*TuneCPU=*/CPU, ArchFS);
71 }
72 
73 static MCAsmInfo *createM68kMCAsmInfo(const MCRegisterInfo &MRI,
74                                       const Triple &TT,
75                                       const MCTargetOptions &TO) {
76   MCAsmInfo *MAI = new M68kELFMCAsmInfo(TT);
77 
78   // Initialize initial frame state.
79   // Calculate amount of bytes used for return address storing
80   int StackGrowth = -4;
81 
82   // Initial state of the frame pointer is SP+StackGrowth.
83   // TODO: Add tests for `cfi_*` directives
84   MCCFIInstruction Inst = MCCFIInstruction::cfiDefCfa(
85       nullptr, MRI.getDwarfRegNum(llvm::M68k::SP, true), -StackGrowth);
86   MAI->addInitialFrameState(Inst);
87 
88   // Add return address to move list
89   Inst = MCCFIInstruction::createOffset(
90       nullptr, MRI.getDwarfRegNum(M68k::PC, true), StackGrowth);
91   MAI->addInitialFrameState(Inst);
92 
93   return MAI;
94 }
95 
96 static MCRelocationInfo *createM68kMCRelocationInfo(const Triple &TheTriple,
97                                                     MCContext &Ctx) {
98   // Default to the stock relocation info.
99   return llvm::createMCRelocationInfo(TheTriple, Ctx);
100 }
101 
102 static MCInstPrinter *createM68kMCInstPrinter(const Triple &T,
103                                               unsigned SyntaxVariant,
104                                               const MCAsmInfo &MAI,
105                                               const MCInstrInfo &MII,
106                                               const MCRegisterInfo &MRI) {
107   return new M68kInstPrinter(MAI, MII, MRI);
108 }
109 
110 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kTargetMC() {
111   Target &T = getTheM68kTarget();
112 
113   // Register the MC asm info.
114   RegisterMCAsmInfoFn X(T, createM68kMCAsmInfo);
115 
116   // Register the MC instruction info.
117   TargetRegistry::RegisterMCInstrInfo(T, createM68kMCInstrInfo);
118 
119   // Register the MC register info.
120   TargetRegistry::RegisterMCRegInfo(T, createM68kMCRegisterInfo);
121 
122   // Register the MC subtarget info.
123   TargetRegistry::RegisterMCSubtargetInfo(T, createM68kMCSubtargetInfo);
124 
125   // Register the code emitter.
126   TargetRegistry::RegisterMCCodeEmitter(T, createM68kMCCodeEmitter);
127 
128   // Register the MCInstPrinter.
129   TargetRegistry::RegisterMCInstPrinter(T, createM68kMCInstPrinter);
130 
131   // Register the MC relocation info.
132   TargetRegistry::RegisterMCRelocationInfo(T, createM68kMCRelocationInfo);
133 
134   // Register the asm backend.
135   TargetRegistry::RegisterMCAsmBackend(T, createM68kAsmBackend);
136 }
137