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