[llvm] Add SuperH (1-4a) target (PR #181287)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 3 01:24:50 PDT 2026


https://github.com/LunaTheFoxgirl updated https://github.com/llvm/llvm-project/pull/181287

>From 63a5058d7763eaf2d9be5db4ff04d5f8dd30664d Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Fri, 13 Feb 2026 02:37:18 +0100
Subject: [PATCH 1/7] sh4: initial commit

---
 llvm/CMakeLists.txt                           |   1 +
 llvm/lib/Target/SuperH/CMakeLists.txt         |  25 ++++
 .../Target/SuperH/MCTargetDesc/CMakeLists.txt |  12 ++
 .../MCTargetDesc/SuperHMCTargetDesc.cpp       |  34 ++++++
 .../SuperH/MCTargetDesc/SuperHMCTargetDesc.h  |  38 ++++++
 llvm/lib/Target/SuperH/SuperH.td              |  59 +++++++++
 llvm/lib/Target/SuperH/SuperHInstrFormats.td  |  21 ++++
 llvm/lib/Target/SuperH/SuperHInstrInfo.td     |  21 ++++
 llvm/lib/Target/SuperH/SuperHRegisterInfo.td  | 113 ++++++++++++++++++
 llvm/lib/Target/SuperH/SuperHSchedule.td      |  22 ++++
 .../lib/Target/SuperH/SuperHTargetMachine.cpp |  40 +++++++
 llvm/lib/Target/SuperH/SuperHTargetMachine.h  |  38 ++++++
 .../Target/SuperH/TargetInfo/CMakeLists.txt   |  10 ++
 .../SuperH/TargetInfo/SuperHTargetInfo.cpp    |  23 ++++
 .../SuperH/TargetInfo/SuperHTargetInfo.h      |  20 ++++
 15 files changed, 477 insertions(+)
 create mode 100644 llvm/lib/Target/SuperH/CMakeLists.txt
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
 create mode 100644 llvm/lib/Target/SuperH/SuperH.td
 create mode 100644 llvm/lib/Target/SuperH/SuperHInstrFormats.td
 create mode 100644 llvm/lib/Target/SuperH/SuperHInstrInfo.td
 create mode 100644 llvm/lib/Target/SuperH/SuperHRegisterInfo.td
 create mode 100644 llvm/lib/Target/SuperH/SuperHSchedule.td
 create mode 100644 llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperHTargetMachine.h
 create mode 100644 llvm/lib/Target/SuperH/TargetInfo/CMakeLists.txt
 create mode 100644 llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
 create mode 100644 llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h

diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 2e2c269c0abbc..7b36ea47d04c9 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -540,6 +540,7 @@ set(LLVM_ALL_EXPERIMENTAL_TARGETS
   DirectX
   M68k
   Xtensa
+  SuperH
 )
 
 # List of targets with JIT support:
diff --git a/llvm/lib/Target/SuperH/CMakeLists.txt b/llvm/lib/Target/SuperH/CMakeLists.txt
new file mode 100644
index 0000000000000..a12dcbc2e101d
--- /dev/null
+++ b/llvm/lib/Target/SuperH/CMakeLists.txt
@@ -0,0 +1,25 @@
+add_llvm_component_group(SuperH)
+
+set(LLVM_TARGET_DEFINITIONS SuperH.td)
+
+# add_public_tablegen_target(SuperHCommonTableGen)
+
+add_llvm_target(SuperHCodeGen
+  SuperHTargetMachine.cpp
+
+  LINK_COMPONENTS
+  AsmPrinter
+  CodeGen
+  Core
+  MC
+  SelectionDAG
+  Support
+  Target
+  TargetParser
+
+  ADD_TO_COMPONENT
+  SuperH
+  )
+
+add_subdirectory(TargetInfo)
+add_subdirectory(MCTargetDesc)
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
new file mode 100644
index 0000000000000..a9a69ea22dc05
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_llvm_component_library(LLVMSuperHDesc
+  SuperHMCTargetDesc.cpp
+
+  LINK_COMPONENTS
+  MC
+  MCDisassembler
+  Support
+  TargetParser
+
+  ADD_TO_COMPONENT
+  SuperH
+)
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
new file mode 100644
index 0000000000000..d7ac60efb4563
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
@@ -0,0 +1,34 @@
+//===-- SuperHMCTargetDesc.cpp - SuperH Target Descriptions ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file provides SuperH target specific descriptions.
+///
+//===----------------------------------------------------------------------===//
+
+#include "SuperHMCTargetDesc.h"
+#include "TargetInfo/SuperHTargetInfo.h"
+
+#include "llvm/MC/MCELFStreamer.h"
+#include "llvm/MC/MCInstPrinter.h"
+#include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MachineLocation.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormattedStream.h"
+
+using namespace llvm;
+
+extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY
+void LLVMInitializeSuperHTargetMC() {
+
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
new file mode 100644
index 0000000000000..b1428a0b33366
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
@@ -0,0 +1,38 @@
+//===-- SuperHMCTargetDesc.h - SuperH Target Descriptions -----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file provides SuperH specific target descriptions.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCTARGETDESC_H
+#define LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCTARGETDESC_H
+
+#include "llvm/MC/MCInstrDesc.h"
+#include "llvm/MC/MCObjectWriter.h"
+#include "llvm/Support/DataTypes.h"
+
+namespace llvm {
+class MCAsmBackend;
+class MCCodeEmitter;
+class MCContext;
+class MCInstrInfo;
+class MCRegisterInfo;
+class MCSubtargetInfo;
+class MCRelocationInfo;
+class MCTargetOptions;
+class Target;
+class Triple;
+class StringRef;
+class raw_ostream;
+class raw_pwrite_stream;
+
+}
+
+#endif // LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCTARGETDESC_H
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperH.td b/llvm/lib/Target/SuperH/SuperH.td
new file mode 100644
index 0000000000000..ea768a5b8dc9d
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperH.td
@@ -0,0 +1,59 @@
+//===- SuperH.td - Describe the SuperH Target Machine ------*- tablegen -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+// Target-independent interfaces
+//===----------------------------------------------------------------------===//
+
+include "llvm/Target/Target.td"
+
+//===----------------------------------------------------------------------===//
+// SuperH Subtarget features
+//===----------------------------------------------------------------------===//
+
+
+//===----------------------------------------------------------------------===//
+// SuperH Processors
+//===----------------------------------------------------------------------===//
+
+include "SuperHSchedule.td"
+
+class Proc<string Name, list<SubtargetFeature> Features>
+    : ProcessorModel<Name, GenericSHModel, Features>;
+
+def : Proc<"generic", [ ]>;
+def : Proc<"sh1", []>;
+def : Proc<"sh2", []>;
+def : Proc<"sh3", []>;
+def : Proc<"sh4", []>;
+def : Proc<"sh4a", []>;
+
+//===----------------------------------------------------------------------===//
+// Register File Description
+//===----------------------------------------------------------------------===//
+
+include "SuperHRegisterInfo.td"
+
+//===----------------------------------------------------------------------===//
+// Instruction Descriptions
+//===----------------------------------------------------------------------===//
+
+// include "SuperHInstrInfo.td"
+
+def SuperHInstrInfo : InstrInfo;
+
+//===----------------------------------------------------------------------===//
+// Target Declaration
+//===----------------------------------------------------------------------===//
+
+def SuperH : Target {
+  let InstructionSet = SuperHInstrInfo;
+
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrFormats.td b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
new file mode 100644
index 0000000000000..d3bacb075c9ba
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
@@ -0,0 +1,21 @@
+//===-- SuperHInstrFormats.td - SuperH Instruction Formats ---------*- tablegen -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+class SHInst<dag outs, dag ins, string asmstr, list<dag> pattern>
+    : Instruction {
+  let Namespace = "SH";
+
+  dag OutOperandList = outs;
+  dag InOperandList = ins;
+  let AsmString = asmtr;
+  let Pattern = pattern;
+  
+  field bits<16> Inst;
+  let Size = 2;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.td b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
new file mode 100644
index 0000000000000..a4451d7a502d5
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
@@ -0,0 +1,21 @@
+//===-- SuperHInstrInfo.td - Main SuperH Instruction Definition -*- tablegen -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file describes the SuperH instruction set, defining the instructions
+/// and properties of the instructions which are needed for code generation,
+/// machine code emission, and analysis.
+///
+//===----------------------------------------------------------------------===//
+
+include "SuperHInstrFormats.td"
+
+//===----------------------------------------------------------------------===//
+// SuperH Type Profiles
+//===----------------------------------------------------------------------===//
+
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
new file mode 100644
index 0000000000000..325d6520c1512
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
@@ -0,0 +1,113 @@
+//==-- SuperHRegisterInfo.td - SuperH register definitions ------*- tablegen -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file describes the SuperH Register file, defining the registers
+/// aliases between the registers, and the register classes built out of the
+/// registers.
+///
+//===----------------------------------------------------------------------===//
+
+// SuperH Registers
+class SHRegister<string N, bits<16> ENC, 
+            list<Register> SUBREGS = [], list<SubRegIndex> SUBIDX,
+            list<string> ALTNAMES = []>
+            : Register<N, ALTNAMES> {
+  let Namespace     = "SH";
+  let HWEncoding    = ENC;
+  let SubRegs       = SUBREGS;
+  let SubRegIndices = SUBIDX;
+}
+
+let Namespace = "SH" in {
+
+  // MAC registers
+  def sub_16bit_lo : SubRegIndex<16>;
+  def sub_16bit_hi : SubRegIndex<16, 16>;
+
+  // FP Registers
+  def sub_32bit_lo : SubRegIndex<32>;
+  def sub_32bit_hi : SubRegIndex<32, 32>;
+}
+
+//===----------------------------------------------------------------------===//
+// Registers
+//===----------------------------------------------------------------------===//
+
+let Namespace = "SH" in {
+
+  // GP Registers
+  def R0 : SHRegister<0, "r0>, DwarfRegNum<[ 0 ]>;
+  def R1 : SHRegister<1, "r1>, DwarfRegNum<[ 1 ]>;
+  def R2 : SHRegister<2, "r2>, DwarfRegNum<[ 2 ]>;
+  def R3 : SHRegister<3, "r3>, DwarfRegNum<[ 3 ]>;
+  def R4 : SHRegister<4, "r4>, DwarfRegNum<[ 4 ]>;
+  def R5 : SHRegister<5, "r5>, DwarfRegNum<[ 5 ]>;
+  def R6 : SHRegister<6, "r6>, DwarfRegNum<[ 6 ]>;
+  def R7 : SHRegister<7, "r7>, DwarfRegNum<[ 7 ]>;
+  def R8 : SHRegister<8, "r8>, DwarfRegNum<[ 8 ]>;
+  def R9 : SHRegister<9, "r9>, DwarfRegNum<[ 9 ]>;
+  def R10 : SHRegister<10, "r10>, DwarfRegNum<[ 10 ]>;
+  def R11 : SHRegister<11, "r11>, DwarfRegNum<[ 11 ]>;
+  def R12 : SHRegister<12, "r12>, DwarfRegNum<[ 12 ]>;
+  def R13 : SHRegister<13, "r13>, DwarfRegNum<[ 13 ]>;
+  def R14 : SHRegister<14, "r14, [ "fp" ]>, DwarfRegNum<[ 14 ]>;
+  def R15 : SHRegister<15, "r15, [ "sp" ]>, DwarfRegNum<[ 15 ]>;
+
+  // Control Registers
+  def SR : SHRegister<16, "sr">, DwarfRegNum<[ 22 ]>;
+  def GBR : SHRegister<17, "gbr">, DwarfRegNum<[ 18 ]>;
+  def VBR : SHRegister<18, "vbr">, DwarfRegNum<[ 19 ]>;
+
+  // System Registers
+  def MACH : SHRegister<19, "mach">, DwarfRegNum<[ 21 ]>;
+  def MACL : SHRegister<20, "macl">, DwarfRegNum<[ 21 ]>;
+  let SubRegIndices = [sub_16bit_lo, sub_16bit_hi] in {
+    def MAC : SHRegister<21, "mac", [], [MACL, MACH]>;
+  }
+  def PC : SHRegister<22, "pc">, DwarfRegNum<[ 16 ]>;
+  def PR : SHRegister<23, "pr">, DwarfRegNum<[ 17 ]>;
+
+  // FR Registers
+
+  // TODO: Add float registers
+}
+
+//===----------------------------------------------------------------------===//
+// Register Classes
+//===----------------------------------------------------------------------===//
+
+def GPR : RegisterClass<"SH", [i32], 32, (
+  add(
+    R0, R1, R2,  R3,  R4,  R5,  R6,  R7,  // Banked memory
+    R8, R9, R10, R11, R12, R13, R14, R15, // Non-banked memory.
+  ) 
+)>;
+
+// Control registers
+def CR : RegisterClass<"SH", [i32], 32, (add SR, GBR, VBR>) {
+  let CopyCost = -1;
+  let isAllocatable = 0;
+}
+
+// System registers.
+def SYSR : RegisterClass<"SH", [ i32 ], 32, (add MACH, MACL, PR, PC)> {
+  let CopyCost = -1;
+  let isAllocatable = 0;
+}
+
+// MAC register.
+def MACR : RegisterClass<"SH", [ i64 ], 64, (add MAC)> {
+  let CopyCost = -1;
+  let isAllocatable = 0;
+}
+
+def PCR : RegisterClass<"SH", [ i32 ], 32, (add PC)> {
+  let CopyCost = -1;
+  let isAllocatable = 0;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHSchedule.td b/llvm/lib/Target/SuperH/SuperHSchedule.td
new file mode 100644
index 0000000000000..fe0d13648349f
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHSchedule.td
@@ -0,0 +1,22 @@
+//===-- SuperHSchedule.td - SuperH Scheduling Definitions ------*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains SuperH scheduler definitions.
+///
+//===----------------------------------------------------------------------===//
+
+/// SuperH Base Scheduling Model.
+class SuperHSchedModel : SchedMachineModel {
+  let LoadLatency = 4;  // Word (Rn)
+  let HighLatency = 16; // Long ABS
+  let PostRAScheduler = 0;
+  let CompleteModel = 0;
+}
+
+def GenericSuperHModel : SuperHSchedModel;
diff --git a/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp b/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
new file mode 100644
index 0000000000000..91673573b22b2
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
@@ -0,0 +1,40 @@
+//===-- SuperHTargetMachine.cpp - Define TargetMachine for SuperH -----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuperHTargetMachine.h"
+#include "TargetInfo/SuperHTargetInfo.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/Compiler.h"
+#include <optional>
+
+using namespace llvm;
+
+extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSuperHTarget() {
+    RegisterTargetMachine<SuperHTargetMachine> SH(getTheSuperHTarget());
+}
+
+SuperHTargetMachine::~SuperHTargetMachine() { }
+
+/// Create a SuperH architecture model.
+SuperHTargetMachine::SuperHTargetMachine(const Target &T, const Triple &TT,
+                                           StringRef CPU, StringRef FS,
+                                           const TargetOptions &Options,
+                                           std::optional<Reloc::Model> RM,
+                                           std::optional<CodeModel::Model> CM,
+                                           CodeGenOptLevel OL, bool JIT)
+    : CodeGenTargetMachineImpl(
+        T, TT.computeDataLayout(), TT, CPU, FS, Options,
+        RM.value_or(Reloc::Static), getEffectiveCodeModel(CM, CodeModel::Small), 
+        OL) {
+
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHTargetMachine.h b/llvm/lib/Target/SuperH/SuperHTargetMachine.h
new file mode 100644
index 0000000000000..5da814b766f4a
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHTargetMachine.h
@@ -0,0 +1,38 @@
+//===-- SuperHTargetMachine.h - Define TargetMachine for SuperH ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the SuperH specific subclass of TargetMachine.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SPARC_SPARCTARGETMACHINE_H
+#define LLVM_LIB_TARGET_SPARC_SPARCTARGETMACHINE_H
+
+#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
+#include "llvm/Target/TargetMachine.h"
+#include <optional>
+
+namespace llvm {
+
+class SuperHTargetMachine : public CodeGenTargetMachineImpl {
+private:
+
+protected:
+
+public:
+  SuperHTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
+                     StringRef FS, const TargetOptions &Options,
+                     std::optional<Reloc::Model> RM,
+                     std::optional<CodeModel::Model> CM, CodeGenOptLevel OL,
+                     bool JIT);
+  ~SuperHTargetMachine() override;
+
+};
+} // end namespace llvm
+
+#endif
diff --git a/llvm/lib/Target/SuperH/TargetInfo/CMakeLists.txt b/llvm/lib/Target/SuperH/TargetInfo/CMakeLists.txt
new file mode 100644
index 0000000000000..73abb4dbba519
--- /dev/null
+++ b/llvm/lib/Target/SuperH/TargetInfo/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_llvm_component_library(LLVMSuperHInfo
+  SuperHTargetInfo.cpp
+
+  LINK_COMPONENTS
+  MC
+  Support
+
+  ADD_TO_COMPONENT
+  SuperH
+  )
diff --git a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
new file mode 100644
index 0000000000000..b676f36dc8d55
--- /dev/null
+++ b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
@@ -0,0 +1,23 @@
+//===-- SuperHTargetInfo.cpp - SuperH Target Implementation -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "TargetInfo/SuperHTargetInfo.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/Compiler.h"
+using namespace llvm;
+
+Target &llvm::getTheSuperHTarget() {
+  static Target TheSuperHTarget;
+  return TheSuperHTarget;
+}
+
+extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY
+void LLVMInitializeSuperHTargetInfo() {
+  RegisterTarget<Triple::superh, /*HasJIT=*/false> X(getTheSuperHTarget(),
+                                                    "sh", "SuperH", "SuperH");
+}
diff --git a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h
new file mode 100644
index 0000000000000..38b501e05d46e
--- /dev/null
+++ b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h
@@ -0,0 +1,20 @@
+//===-- SuperHTargetInfo.h - SuperH Target Implementation ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_TARGETINFO_SUPERHTARGETINFO_H
+#define LLVM_LIB_TARGET_SUPERH_TARGETINFO_SUPERHTARGETINFO_H
+
+namespace llvm {
+
+class Target;
+
+Target &getTheSuperHTarget();
+
+} // namespace llvm
+
+#endif // LLVM_LIB_TARGET_SUPERH_TARGETINFO_SUPERHTARGETINFO_H

>From e31622952c80e4579aba246c32c29e514104d1af Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Fri, 29 May 2026 02:41:44 +0200
Subject: [PATCH 2/7] Add self to Maintainers.md

---
 llvm/Maintainers.md | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/Maintainers.md b/llvm/Maintainers.md
index 1b84d12fa51f3..911a0e9674856 100644
--- a/llvm/Maintainers.md
+++ b/llvm/Maintainers.md
@@ -354,6 +354,11 @@ nigelp at xmos.com (email), [nigelp-xmos](https://github.com/nigelp-xmos) (GitHub)
 Andrei Safronov \
 andrei.safronov at espressif.com (email), [andreisfr](https://github.com/andreisfr) (GitHub)
 
+#### SuperH backend
+
+Luna Nielsen \
+luna at foxgirls.gay (email), [LunaTheFoxgirl](https://github.com/LunaTheFoxgirl) (GitHub)
+
 ### Libraries and shared infrastructure
 
 #### ADT, Support

>From 5bdf0fb053af407aa5df9281c138a300e9e5ad0c Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Fri, 29 May 2026 05:14:53 +0200
Subject: [PATCH 3/7] Flesh out register file and target cpu

---
 llvm/include/llvm/TargetParser/Triple.h       |   1 +
 llvm/lib/Target/SuperH/SuperH.td              |  33 ++-
 llvm/lib/Target/SuperH/SuperHRegisterInfo.td  | 220 ++++++++++++------
 .../SuperH/TargetInfo/SuperHTargetInfo.cpp    |   2 +-
 llvm/lib/TargetParser/Triple.cpp              |   6 +
 5 files changed, 187 insertions(+), 75 deletions(-)

diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 7b24db121818f..25b35bb99edb7 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -79,6 +79,7 @@ class Triple {
     riscv64,     // RISC-V (64-bit, little endian): riscv64
     riscv32be,   // RISC-V (32-bit, big endian): riscv32be
     riscv64be,   // RISC-V (64-bit, big endian): riscv64be
+    sh,          // SuperH: sh
     sparc,       // Sparc: sparc
     sparcv9,     // Sparcv9: Sparcv9
     sparcel,     // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
diff --git a/llvm/lib/Target/SuperH/SuperH.td b/llvm/lib/Target/SuperH/SuperH.td
index ea768a5b8dc9d..cd7ef7e54f403 100644
--- a/llvm/lib/Target/SuperH/SuperH.td
+++ b/llvm/lib/Target/SuperH/SuperH.td
@@ -7,6 +7,8 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+// This is the top level entry point for the SuperH target.
+//===----------------------------------------------------------------------===//
 
 //===----------------------------------------------------------------------===//
 // Target-independent interfaces
@@ -18,6 +20,24 @@ include "llvm/Target/Target.td"
 // SuperH Subtarget features
 //===----------------------------------------------------------------------===//
 
+def FeatureFPU      : SubtargetFeature<"fpu", "HasFPU", "false",
+                          "Enable FPU Support">;
+def FeatureVFPU     : SubtargetFeature<"vfpu", "HasVFPU", "true",
+                          "Enable vector FPU instructions">;
+def FeatureSH1      : SubtargetFeature<"sh1", "SHArchVersion", "SH1",
+                          "SH-1 ISA Support">;
+def FeatureSH2      : SubtargetFeature<"sh2", "SHArchVersion", "SH2",
+                          "SH-2 ISA Support",
+                          [FeatureSH1]>;
+def FeatureSH3      : SubtargetFeature<"sh3", "SHArchVersion", "SH3",
+                          "SH-3 ISA Support",
+                          [FeatureSH1, FeatureSH2]>;
+def FeatureSH4      : SubtargetFeature<"sh4", "SHArchVersion", "SH3",
+                          "SH-4 ISA Support",
+                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureFPU]>;
+def FeatureSH4A     : SubtargetFeature<"sh4a", "SHArchVersion", "SH4A",
+                          "SH-4A ISA Support",
+                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureSH4, FeatureFPU]>;
 
 //===----------------------------------------------------------------------===//
 // SuperH Processors
@@ -28,12 +48,12 @@ include "SuperHSchedule.td"
 class Proc<string Name, list<SubtargetFeature> Features>
     : ProcessorModel<Name, GenericSHModel, Features>;
 
-def : Proc<"generic", [ ]>;
-def : Proc<"sh1", []>;
-def : Proc<"sh2", []>;
-def : Proc<"sh3", []>;
-def : Proc<"sh4", []>;
-def : Proc<"sh4a", []>;
+def : Proc<"generic", [FeatureSH4]>;
+def : Proc<"sh1", [FeatureSH1]>;
+def : Proc<"sh2", [FeatureSH2]>;
+def : Proc<"sh3", [FeatureSH3]>;
+def : Proc<"sh4", [FeatureSH4]>;
+def : Proc<"sh4a", [FeatureSH4A]>;
 
 //===----------------------------------------------------------------------===//
 // Register File Description
@@ -55,5 +75,4 @@ def SuperHInstrInfo : InstrInfo;
 
 def SuperH : Target {
   let InstructionSet = SuperHInstrInfo;
-
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
index 325d6520c1512..abf4cbfcc3ba2 100644
--- a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
@@ -13,101 +13,187 @@
 ///
 //===----------------------------------------------------------------------===//
 
-// SuperH Registers
-class SHRegister<string N, bits<16> ENC, 
-            list<Register> SUBREGS = [], list<SubRegIndex> SUBIDX,
-            list<string> ALTNAMES = []>
-            : Register<N, ALTNAMES> {
-  let Namespace     = "SH";
-  let HWEncoding    = ENC;
-  let SubRegs       = SUBREGS;
-  let SubRegIndices = SUBIDX;
-}
+//===----------------------------------------------------------------------===//
+//  Declarations that describe the SuperH register file
+//===----------------------------------------------------------------------===//
 
 let Namespace = "SH" in {
+  def sub_64_lo  : SubRegIndex<64>;
+  def sub_64_hi  : SubRegIndex<64, 64>;
+  def sub_32_lo  : SubRegIndex<32>;
+  def sub_32_hi  : SubRegIndex<32, 32>;
+
+  // Vector Registers
+  def sub_fv_x   : SubRegIndex<32>;
+  def sub_fv_y   : SubRegIndex<32, 32>;
+  def sub_fv_z   : SubRegIndex<32, 64>;
+  def sub_fv_w   : SubRegIndex<32, 96>;
+
+  // Matrix Register
+}
+
+class Unallocatable {
+  int CopyCost = -1;
+  bit isAllocatable = false;
+}
+
+class SHReg<bits<16> Enc, string n> : Register<n> {
+  let HWEncoding = Enc;
+  let Namespace = "SH";
+}
 
-  // MAC registers
-  def sub_16bit_lo : SubRegIndex<16>;
-  def sub_16bit_hi : SubRegIndex<16, 16>;
+class SHRegWithSubRegs<bits<16> Enc, string n, list<Register> subregs>
+  : RegisterWithSubRegs<n, subregs> {
+  let HWEncoding = Enc;
+  let Namespace = "SH";
+}
+
+// SH General Purpose Registers.
+class GPRReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
+
+// Control Registers
+class CtrlReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
+
+// SH System Registers.
+class SysReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
 
-  // FP Registers
-  def sub_32bit_lo : SubRegIndex<32>;
-  def sub_32bit_hi : SubRegIndex<32, 32>;
+// 32-bit floating point registers
+class FRReg<bits<16> Enc, string n> : SHReg<Enc, n>;
+
+// 64-bit (alias) FPU registers
+class DRReg<bits<16> Enc, string n, list<Register> subregs>
+  : SHRegWithSubRegs<Enc, n, subregs> {
+  let SubRegIndices = [sub_32_lo, sub_32_hi];
+  let CoveredBySubRegs = 1;
+}
+
+// 32-bit (alias) VFPU registers
+class FVReg<bits<16> Enc, string n, list<Register> subregs>
+  : SHRegWithSubRegs<Enc, n, subregs> {
+  let SubRegIndices = [sub_fv_x, sub_fv_y, sub_fv_z, sub_fv_w];
+  let CoveredBySubRegs = 1;
 }
 
+// 512-bit Matrix Register
+class MTRXReg<bits<16> Enc, string n> : SHReg<Enc, n>;
+
 //===----------------------------------------------------------------------===//
 // Registers
 //===----------------------------------------------------------------------===//
 
 let Namespace = "SH" in {
 
-  // GP Registers
-  def R0 : SHRegister<0, "r0>, DwarfRegNum<[ 0 ]>;
-  def R1 : SHRegister<1, "r1>, DwarfRegNum<[ 1 ]>;
-  def R2 : SHRegister<2, "r2>, DwarfRegNum<[ 2 ]>;
-  def R3 : SHRegister<3, "r3>, DwarfRegNum<[ 3 ]>;
-  def R4 : SHRegister<4, "r4>, DwarfRegNum<[ 4 ]>;
-  def R5 : SHRegister<5, "r5>, DwarfRegNum<[ 5 ]>;
-  def R6 : SHRegister<6, "r6>, DwarfRegNum<[ 6 ]>;
-  def R7 : SHRegister<7, "r7>, DwarfRegNum<[ 7 ]>;
-  def R8 : SHRegister<8, "r8>, DwarfRegNum<[ 8 ]>;
-  def R9 : SHRegister<9, "r9>, DwarfRegNum<[ 9 ]>;
-  def R10 : SHRegister<10, "r10>, DwarfRegNum<[ 10 ]>;
-  def R11 : SHRegister<11, "r11>, DwarfRegNum<[ 11 ]>;
-  def R12 : SHRegister<12, "r12>, DwarfRegNum<[ 12 ]>;
-  def R13 : SHRegister<13, "r13>, DwarfRegNum<[ 13 ]>;
-  def R14 : SHRegister<14, "r14, [ "fp" ]>, DwarfRegNum<[ 14 ]>;
-  def R15 : SHRegister<15, "r15, [ "sp" ]>, DwarfRegNum<[ 15 ]>;
+  // General Purpose Registers
+  def R0  : SHGPRReg<0,  "r0">,   DwarfRegNum<[0]>;
+  def R1  : SHGPRReg<1,  "r1">,   DwarfRegNum<[1]>;
+  def R2  : SHGPRReg<2,  "r2">,   DwarfRegNum<[2]>;
+  def R3  : SHGPRReg<3,  "r3">,   DwarfRegNum<[3]>;
+  def R4  : SHGPRReg<4,  "r4">,   DwarfRegNum<[4]>;
+  def R5  : SHGPRReg<5,  "r5">,   DwarfRegNum<[5]>;
+  def R6  : SHGPRReg<6,  "r6">,   DwarfRegNum<[6]>;
+  def R7  : SHGPRReg<7,  "r7">,   DwarfRegNum<[7]>;
+  def R8  : SHGPRReg<8,  "r8">,   DwarfRegNum<[8]>;
+  def R9  : SHGPRReg<9,  "r9">,   DwarfRegNum<[9]>;
+  def R10 : SHGPRReg<10, "r10">,  DwarfRegNum<[10]>;
+  def R11 : SHGPRReg<11, "r11">,  DwarfRegNum<[11]>;
+  def R12 : SHGPRReg<12, "r12">,  DwarfRegNum<[12]>;
+  def R13 : SHGPRReg<13, "r13">,  DwarfRegNum<[13]>;
+  def R14 : SHGPRReg<14, "r14">,  DwarfRegNum<[14]>;
+  def R15 : SHGPRReg<15, "r15">,  DwarfRegNum<[15]>;
 
   // Control Registers
-  def SR : SHRegister<16, "sr">, DwarfRegNum<[ 22 ]>;
-  def GBR : SHRegister<17, "gbr">, DwarfRegNum<[ 18 ]>;
-  def VBR : SHRegister<18, "vbr">, DwarfRegNum<[ 19 ]>;
+  def SR : CtrlReg<0, "sr">;    // Status Register
+  def GBR : CtrlReg<0, "gbr";   // Global Base Register
+  def VBR : CtrlReg<0, "vbr">;  // Vector Base Register
+  def SGR : CtrlReg<0, "sgr">;  // Saved General Register
+  def DBR : CtrlReg<0, "dbr">;  // Debug Base Register
+  def SSR : CtrlReg<0, "ssr">;  // Saved Status Register
+  def SPC : CtrlReg<0, "spc">;  // Saved Program Counter
 
   // System Registers
-  def MACH : SHRegister<19, "mach">, DwarfRegNum<[ 21 ]>;
-  def MACL : SHRegister<20, "macl">, DwarfRegNum<[ 21 ]>;
-  let SubRegIndices = [sub_16bit_lo, sub_16bit_hi] in {
-    def MAC : SHRegister<21, "mac", [], [MACL, MACH]>;
-  }
-  def PC : SHRegister<22, "pc">, DwarfRegNum<[ 16 ]>;
-  def PR : SHRegister<23, "pr">, DwarfRegNum<[ 17 ]>;
-
-  // FR Registers
-
-  // TODO: Add float registers
+  def PR : SysReg<0, "pr">;       // Procedure Register
+  def PC : SysReg<0, "pc">;       // Program Counter
+  def MACL : SysReg<0, "macl">;   // Mult & Accum Low
+  def MACH : SysReg<1, "mach">;   // Mult & Accum Hi
+  def FPSCR : SysReg<0, "fpscr">; // FPU Status/Control Register
+  def FPUL : SysReg<0, "fpul">;   // FPU Comms Register
+
+  // 32-bit floating point registers
+  foreach I = 0-15 in
+    def FR#I : FRReg<I, "fr"#I>, DwarfRegNum<[!add(I, 16)]>;
+  foreach I = 0-15 in
+    def XF#I : FRReg<!add(I, 16), "xf"#I>, DwarfRegNum<[!add(I, 32)]>;
+
+  // 64-bit floating point registers
+  foreach I = 0-7 in
+    def DR#I : DRReg<!shl(I, 1), "dr"#!shl(I, 1),
+    [!cast<FPReg>("FR"#!shl(I, 1)),
+     !cast<FPReg>("FR"#!add(!shl(I, 1), 1)]>;
+  foreach I = 0-7 in
+    def XD#I : DRReg<!shl(I, 1), "xd"#!shl(I, 1),
+    [!cast<FPReg>("XF"#!shl(I, 1)),
+     !cast<FPReg>("XF"#!add(!shl(I, 1), 1)]>;
+
+  // 128-bit floating point vector registers
+  foreach I = 0-3 in
+    def FV#I : FVReg<!shl(I, 2), "fv"#!shl(I, 2),
+    [!cast<FPReg>("FR"#!shl(I, 2)),
+     !cast<FPReg>("FR"#!add(!shl(I, 2), 1)),
+     !cast<FPReg>("FR"#!add(!shl(I, 2), 2)),
+     !cast<FPReg>("XF"#!add(!shl(I, 2), 3)]>;
+  
+  // 512-bit matrix register (shadows XF registers)
+  // TODO: Add proper subregisters here.
+  def MtrxReg : MTRXReg<0, "xmtrx">;
 }
 
 //===----------------------------------------------------------------------===//
 // Register Classes
 //===----------------------------------------------------------------------===//
 
-def GPR : RegisterClass<"SH", [i32], 32, (
+def GPR : RegisterClass<"SH", [ i32 ], 32, (
   add(
     R0, R1, R2,  R3,  R4,  R5,  R6,  R7,  // Banked memory
     R8, R9, R10, R11, R12, R13, R14, R15, // Non-banked memory.
   ) 
 )>;
 
-// Control registers
-def CR : RegisterClass<"SH", [i32], 32, (add SR, GBR, VBR>) {
-  let CopyCost = -1;
-  let isAllocatable = 0;
-}
+// 32-bit floating point registers.
+def FGR32 : RegisterClass<"SH", [f32], 32, (
+  add(
+    // FR
+    FR0,  FR1,  FR2,  FR3,  FR4,  FR5,  FR6,  FR7,
+    FR8,  FR9, FR10, FR11, FR12, FR13, FR14, FR15,
+
+    // XF
+    XF0,  XF1,  XF2,  XF3,  XF4,  XF5,  XF6,  XF7,
+    XF8,  XF9, XF10, XF11, XF12, XF13, XF14, XF15,
+  )
+)>;
 
-// System registers.
-def SYSR : RegisterClass<"SH", [ i32 ], 32, (add MACH, MACL, PR, PC)> {
-  let CopyCost = -1;
-  let isAllocatable = 0;
-}
+// 64-bit floating point registers.
+def FGR64 : RegisterClass<"SH", [f64], 64, (add
+  // DR
+  DR0,  DR1,  DR2,  DR3,  DR4,  DR5,  DR6,  DR7,
+  DR8,  DR9, DR10, DR11, DR12, DR13, DR14, DR15,
 
-// MAC register.
-def MACR : RegisterClass<"SH", [ i64 ], 64, (add MAC)> {
-  let CopyCost = -1;
-  let isAllocatable = 0;
-}
+  // XD
+  XD0,  XD1,  XD2,  XD3,  XD4,  XD5,  XD6,  XD7,
+  XD8,  XD9, XD10, XD11, XD12, XD13, XD14, XD15,
+)>;
+
+// Vector registers
+def VEC128 : RegisterClass<"SH", [v4f32], 128, (add
+  FV0, FV4, FV8, FV12)>;
+
+// Matrix register
+def MTRX512 : RegisterClass<"SH", [v16f32],
+  (add (sequence "FR%u", 0, 15), (sequence "XF%u", 0, 15))>;
 
-def PCR : RegisterClass<"SH", [ i32 ], 32, (add PC)> {
-  let CopyCost = -1;
-  let isAllocatable = 0;
-}
\ No newline at end of file
+// Control registers
+def CR : RegisterClass<"SH", [ i32 ], 32, 
+  (add SR, GBR, VBR, SGR, DBR, SSR, SPC)>, Unallocatable;
+
+// System registers.
+def SR : RegisterClass<"SH", [ i32 ], 32, 
+  (add PR, PC, MACL, MACH, FPSCR, FPUL)>, Unallocatable;
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
index b676f36dc8d55..4045218737a15 100644
--- a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
+++ b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
@@ -18,6 +18,6 @@ Target &llvm::getTheSuperHTarget() {
 
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY
 void LLVMInitializeSuperHTargetInfo() {
-  RegisterTarget<Triple::superh, /*HasJIT=*/false> X(getTheSuperHTarget(),
+  RegisterTarget<Triple::sh, /*HasJIT=*/false> X(getTheSuperHTarget(),
                                                     "sh", "SuperH", "SuperH");
 }
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index c6515425b7eb5..593af6c736a70 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -106,6 +106,8 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
     return "riscv32be";
   case riscv64be:
     return "riscv64be";
+  case sh:
+    return "sh";
   case shave:
     return "shave";
   case sparc:
@@ -1229,6 +1231,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
   case Triple::riscv64:
   case Triple::riscv32be:
   case Triple::riscv64be:
+  case Triple::sh:
   case Triple::shave:
   case Triple::sparc:
   case Triple::sparcel:
@@ -1992,6 +1995,7 @@ unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
   case llvm::Triple::renderscript32:
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv32be:
+  case llvm::Triple::sh:
   case llvm::Triple::shave:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcel:
@@ -2103,6 +2107,7 @@ Triple Triple::get32BitArchVariant() const {
   case Triple::renderscript32:
   case Triple::riscv32:
   case Triple::riscv32be:
+  case Triple::sh:
   case Triple::shave:
   case Triple::sparc:
   case Triple::sparcel:
@@ -2195,6 +2200,7 @@ Triple Triple::get64BitArchVariant() const {
   case Triple::m68k:
   case Triple::msp430:
   case Triple::r600:
+  case Triple::sh:
   case Triple::shave:
   case Triple::sparcel:
   case Triple::tce:

>From ce4eae98f73971bfbbeff78b7db7cd2d1df67e55 Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Sun, 31 May 2026 04:20:05 +0200
Subject: [PATCH 4/7] Set up asm parser scaffolding

---
 llvm/include/llvm/TargetParser/Triple.h       |   3 +-
 .../Target/SuperH/AsmParser/CMakeLists.txt    |  14 +++
 .../SuperH/AsmParser/SuperHAsmParser.cpp      |  23 ++++
 llvm/lib/Target/SuperH/CMakeLists.txt         |  12 +-
 .../Target/SuperH/MCTargetDesc/CMakeLists.txt |   4 +-
 .../SuperH/MCTargetDesc/SuperHInstPrinter.cpp |  69 ++++++++++
 .../SuperH/MCTargetDesc/SuperHInstPrinter.h   |  48 +++++++
 .../SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp   |  25 ++++
 .../SuperH/MCTargetDesc/SuperHMCAsmInfo.h     |  39 ++++++
 .../MCTargetDesc/SuperHMCTargetDesc.cpp       |  82 +++++++++++-
 .../SuperH/MCTargetDesc/SuperHMCTargetDesc.h  |   6 +
 llvm/lib/Target/SuperH/SuperH.td              |  78 ++++++++++--
 llvm/lib/Target/SuperH/SuperHInstrData.td     |  13 ++
 llvm/lib/Target/SuperH/SuperHInstrFormats.td  |  25 +++-
 llvm/lib/Target/SuperH/SuperHInstrInfo.td     |  37 ++++++
 llvm/lib/Target/SuperH/SuperHRegisterInfo.td  | 118 ++++++++----------
 .../lib/Target/SuperH/SuperHTargetMachine.cpp |  27 ++--
 llvm/lib/Target/SuperH/SuperHTargetMachine.h  |   5 +-
 .../SuperH/TargetInfo/SuperHTargetInfo.cpp    |  10 +-
 .../SuperH/TargetInfo/SuperHTargetInfo.h      |   1 +
 llvm/lib/TargetParser/TargetDataLayout.cpp    |  28 +++++
 llvm/lib/TargetParser/Triple.cpp              |  13 ++
 22 files changed, 581 insertions(+), 99 deletions(-)
 create mode 100644 llvm/lib/Target/SuperH/AsmParser/CMakeLists.txt
 create mode 100644 llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.h
 create mode 100644 llvm/lib/Target/SuperH/SuperHInstrData.td

diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 25b35bb99edb7..375e96c6486f0 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -79,7 +79,8 @@ class Triple {
     riscv64,     // RISC-V (64-bit, little endian): riscv64
     riscv32be,   // RISC-V (32-bit, big endian): riscv32be
     riscv64be,   // RISC-V (64-bit, big endian): riscv64be
-    sh,          // SuperH: sh
+    sh,          // SuperH (big endian): sh
+    sh_le,       // SuperH (little endian): sh_le
     sparc,       // Sparc: sparc
     sparcv9,     // Sparcv9: Sparcv9
     sparcel,     // Sparc: (endianness = little). NB: 'Sparcle' is a CPU variant
diff --git a/llvm/lib/Target/SuperH/AsmParser/CMakeLists.txt b/llvm/lib/Target/SuperH/AsmParser/CMakeLists.txt
new file mode 100644
index 0000000000000..5f75407e92d4c
--- /dev/null
+++ b/llvm/lib/Target/SuperH/AsmParser/CMakeLists.txt
@@ -0,0 +1,14 @@
+add_llvm_component_library(LLVMSuperHAsmParser
+  SuperHAsmParser.cpp
+
+  LINK_COMPONENTS
+  MC
+  MCParser
+  SuperHDesc
+  SuperHInfo
+  Support
+  TargetParser
+
+  ADD_TO_COMPONENT
+  SuperH
+  )
diff --git a/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp b/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
new file mode 100644
index 0000000000000..6ed41223b23eb
--- /dev/null
+++ b/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
@@ -0,0 +1,23 @@
+//===-- SuperHAsmParser.cpp - Parse SH assembly to MCInst instructions ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCTargetDesc/SuperHMCAsmInfo.h"
+#include "TargetInfo/SuperHTargetInfo.h"
+#include "llvm/MC/MCELFStreamer.h"
+#include "llvm/MC/MCInstrAnalysis.h"
+#include "llvm/MC/MCInstPrinter.h"
+#include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/TargetRegistry.h"
+#include <sstream>
+
+using namespace llvm;
+
+extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
+LLVMInitializeSuperHAsmParser() { }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/CMakeLists.txt b/llvm/lib/Target/SuperH/CMakeLists.txt
index a12dcbc2e101d..64d7df4fb153e 100644
--- a/llvm/lib/Target/SuperH/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/CMakeLists.txt
@@ -2,24 +2,34 @@ add_llvm_component_group(SuperH)
 
 set(LLVM_TARGET_DEFINITIONS SuperH.td)
 
-# add_public_tablegen_target(SuperHCommonTableGen)
+tablegen(LLVM SuperHGenRegisterInfo.inc     -gen-register-info)
+#tablegen(LLVM SuperHGenInstrInfo.inc        -gen-instr-info)
+tablegen(LLVM SuperHGenSubtargetInfo.inc    -gen-subtarget)
+
+add_public_tablegen_target(SuperHCommonTableGen)
 
 add_llvm_target(SuperHCodeGen
   SuperHTargetMachine.cpp
 
   LINK_COMPONENTS
+  Analysis
   AsmPrinter
   CodeGen
+  CodeGenTypes
   Core
   MC
   SelectionDAG
+  SuperHDesc
+  SuperHInfo
   Support
   Target
   TargetParser
+  TransformUtils
 
   ADD_TO_COMPONENT
   SuperH
   )
 
+add_subdirectory(AsmParser)
 add_subdirectory(TargetInfo)
 add_subdirectory(MCTargetDesc)
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
index a9a69ea22dc05..181ec4b281c14 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
@@ -1,9 +1,11 @@
 add_llvm_component_library(LLVMSuperHDesc
   SuperHMCTargetDesc.cpp
+  SuperHMCAsmInfo.cpp
+  SuperHInstPrinter.cpp
 
   LINK_COMPONENTS
   MC
-  MCDisassembler
+  SuperHInfo
   Support
   TargetParser
 
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
new file mode 100644
index 0000000000000..2b32ae9c389e0
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
@@ -0,0 +1,69 @@
+//===-- SuperHMCTargetDesc.cpp - SuperH assembly syntax printer -----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+// This file provides the ability to write SuperH instructions to a .s file.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuperHInstPrinter.h"
+// #include "SuperHInstrInfo.h"
+#include "llvm/ADT/StringExtras.h"
+#include <llvm/MC/MCInst.h>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "sh-isel"
+
+#define PRINT_ALIAS_INSTR
+// #include "SuperHGenAsmWriter.inc"
+
+SuperHInstPrinter::SuperHInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
+                    const MCRegisterInfo &MRI) : MCInstPrinter(MAI, MII, MRI) {}
+
+void SuperHInstPrinter::printRegName(raw_ostream &OS, MCRegister Reg) {
+	OS << StringRef(getRegisterName(Reg)).lower();
+}
+
+void SuperHInstPrinter::printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
+                 const MCSubtargetInfo &STI, raw_ostream &OS) {
+	printInstruction(MI, Address, OS);
+}
+
+void SuperHInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
+	const MCOperand &Op = MI->getOperand(OpNo);
+	
+    // Print Register
+	if (Op.isReg()) {
+		printRegName(O, Op.getReg());
+		return;
+	}
+
+	// Print immediates
+	if (Op.isImm()) {
+		assert(Op.getImm() <= 255 && "Only 8-bit immediates are supported.");
+		O << "#" << Op.getImm();
+		return;
+	}
+}
+
+// TODO: Delete this placeholder.
+void SuperHInstPrinter::printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O) {
+
+}
+
+// TODO: Delete this placeholder.
+const char *SuperHInstPrinter::getRegisterName(MCRegister Reg) {
+	return "r";
+}
+
+std::pair<const char *, uint64_t> SuperHInstPrinter::getMnemonic(const MCInst &MI) const {
+	std::pair<const char *, uint64_t> Ret;
+
+	return Ret;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
new file mode 100644
index 0000000000000..d1b26b0e2f93d
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
@@ -0,0 +1,48 @@
+//===-- SuperHMCTargetDesc.h - SuperH assembly syntax printer -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// \file
+// This file provides the ability to write SuperH instructions to a .s file.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHINSTPRINTER_H
+#define LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHINSTPRINTER_H
+
+#include "llvm/MC/MCInstPrinter.h"
+#include <llvm/MC/MCExpr.h>
+#include <llvm/MC/MCInstrInfo.h>
+
+namespace llvm {
+
+//===----------------------------------------------------------------------===//
+//
+// Class which provides the ability to write SuperH instructions to a .s file.
+//
+//===----------------------------------------------------------------------===//
+class SuperHInstPrinter : public MCInstPrinter {
+public:
+  SuperHInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
+                    const MCRegisterInfo &MRI);
+
+  // Autogenerated by tablegen
+  void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
+  static const char *getRegisterName(MCRegister Reg);
+
+  void printRegName(raw_ostream &OS, MCRegister Reg) override;
+  void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
+                 const MCSubtargetInfo &STI, raw_ostream &OS) override;
+  std::pair<const char *, uint64_t> getMnemonic(const MCInst &MI) const override;
+
+private:
+  void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
+};
+
+} // end namespace llvm
+
+#endif
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
new file mode 100644
index 0000000000000..323bb8e703ed8
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
@@ -0,0 +1,25 @@
+//===-- SuperHMCAsmInfo.cpp - SuperH Asm Info -----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file provides the SuperHAsmInfo class.
+///
+//===----------------------------------------------------------------------===//
+
+#include "SuperHMCAsmInfo.h"
+#include "llvm/TargetParser/Triple.h"
+
+using namespace llvm;
+
+void SuperHMCAsmInfo::anchor() {}
+
+SuperHMCAsmInfo::SuperHMCAsmInfo(const Triple &TheTriple,
+                                 const MCTargetOptions &Options)
+    : MCAsmInfoELF(Options) {
+  this->IsLittleEndian = TheTriple.isLittleEndian();
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.h
new file mode 100644
index 0000000000000..25cd78f53becc
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.h
@@ -0,0 +1,39 @@
+//===-- SuperHMCAsmInfo.h - SuperH Asm Info -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file provides the SuperHAsmInfo class.
+///
+//===----------------------------------------------------------------------===//
+
+
+#ifndef LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCASMINFO_H
+#define LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCASMINFO_H
+
+#include "llvm/MC/MCAsmInfoELF.h"
+
+namespace llvm {
+class Triple;
+
+//===----------------------------------------------------------------------===//
+//
+// Class which provides the information needed to emit a SuperH ELF file.
+//
+//===----------------------------------------------------------------------===//
+class SuperHMCAsmInfo : public MCAsmInfoELF {
+private:
+	void anchor() override;
+
+public:
+	explicit SuperHMCAsmInfo(const Triple &TheTriple,
+                             const MCTargetOptions &Options);
+};
+
+} // end namespace llvm
+
+#endif
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
index d7ac60efb4563..4a8a31532c0e4 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
@@ -1,4 +1,5 @@
-//===-- SuperHMCTargetDesc.cpp - SuperH Target Descriptions ---------*- C++ -*-===//
+//===-- SuperHMCTargetDesc.cpp - SuperH Target Descriptions ---------*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -12,6 +13,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "SuperHMCTargetDesc.h"
+#include "SuperHInstPrinter.h"
+#include "SuperHMCAsmInfo.h"
 #include "TargetInfo/SuperHTargetInfo.h"
 
 #include "llvm/MC/MCELFStreamer.h"
@@ -28,7 +31,80 @@
 
 using namespace llvm;
 
-extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY
-void LLVMInitializeSuperHTargetMC() {
+#define GET_REGINFO_MC_DESC
+#include "SuperHGenRegisterInfo.inc"
 
+#define GET_SUBTARGETINFO_MC_DESC
+#include "SuperHGenSubtargetInfo.inc"
+
+static MCInstrInfo *createSuperHMCInstrInfo() {
+  MCInstrInfo *X = new MCInstrInfo();
+  // InitSuperHMCInstrInfo(X);
+  return X;
+}
+
+static MCRegisterInfo *createSuperHMCRegisterInfo(const Triple &TT) {
+  MCRegisterInfo *X = new MCRegisterInfo();
+  // InitSuperHRegisterInfo(X);
+  return X;
+}
+
+static MCSubtargetInfo *
+createSuperHMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) {
+  return createSuperHMCSubtargetInfoImpl(TT, CPU, CPU, FS);
+}
+
+static MCInstPrinter *createSuperHMCInstPrinter(const Triple &T,
+                                                unsigned SyntaxVariant,
+                                                const MCAsmInfo &MAI,
+                                                const MCInstrInfo &MII,
+                                                const MCRegisterInfo &MRI) {
+  return new SuperHInstPrinter(MAI, MII, MRI);
+}
+
+static MCAsmInfo *createSuperHMCAsmInfo(const MCRegisterInfo &MRI,
+                                        const Triple &TT,
+                                        const MCTargetOptions &Options) {
+  MCAsmInfo *MAI = new SuperHMCAsmInfo(TT, Options);
+  return MAI;
+}
+
+extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
+LLVMInitializeSuperHTargetMC() {
+
+  // SuperH (big-endian)
+  for (Target *T : {&getTheSuperHTarget()}) {
+    // Register the MC asm info.
+    TargetRegistry::RegisterMCAsmInfo(*T, createSuperHMCAsmInfo);
+
+    // Register the MC instruction info.
+    TargetRegistry::RegisterMCInstrInfo(*T, createSuperHMCInstrInfo);
+
+    // Register the MC register info.
+    TargetRegistry::RegisterMCRegInfo(*T, createSuperHMCRegisterInfo);
+
+    // Register the MC subtarget info.
+    TargetRegistry::RegisterMCSubtargetInfo(*T, createSuperHMCSubtargetInfo);
+
+    // Register the MCInstPrinter.
+    TargetRegistry::RegisterMCInstPrinter(*T, createSuperHMCInstPrinter);
+  }
+
+  // SuperH (little-endian)
+  for (Target *T : {&getTheSuperHLETarget()}) {
+    // Register the MC asm info.
+    TargetRegistry::RegisterMCAsmInfo(*T, createSuperHMCAsmInfo);
+
+    // Register the MC instruction info.
+    TargetRegistry::RegisterMCInstrInfo(*T, createSuperHMCInstrInfo);
+
+    // Register the MC register info.
+    TargetRegistry::RegisterMCRegInfo(*T, createSuperHMCRegisterInfo);
+
+    // Register the MC subtarget info.
+    TargetRegistry::RegisterMCSubtargetInfo(*T, createSuperHMCSubtargetInfo);
+
+    // Register the MCInstPrinter.
+    TargetRegistry::RegisterMCInstPrinter(*T, createSuperHMCInstPrinter);
+  }
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
index b1428a0b33366..6bb8d438df3ff 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
@@ -18,6 +18,12 @@
 #include "llvm/MC/MCObjectWriter.h"
 #include "llvm/Support/DataTypes.h"
 
+#define GET_REGINFO_ENUM
+#include "SuperHGenRegisterInfo.inc"
+
+#define GET_SUBTARGETINFO_ENUM
+#include "SuperHGenSubtargetInfo.inc"
+
 namespace llvm {
 class MCAsmBackend;
 class MCCodeEmitter;
diff --git a/llvm/lib/Target/SuperH/SuperH.td b/llvm/lib/Target/SuperH/SuperH.td
index cd7ef7e54f403..8ec78f923ee4e 100644
--- a/llvm/lib/Target/SuperH/SuperH.td
+++ b/llvm/lib/Target/SuperH/SuperH.td
@@ -20,24 +20,48 @@ include "llvm/Target/Target.td"
 // SuperH Subtarget features
 //===----------------------------------------------------------------------===//
 
-def FeatureFPU      : SubtargetFeature<"fpu", "HasFPU", "false",
-                          "Enable FPU Support">;
-def FeatureVFPU     : SubtargetFeature<"vfpu", "HasVFPU", "true",
-                          "Enable vector FPU instructions">;
+// NOTE:  Some SH4 CPUs support FSCA and FSRRA despite their
+//        ISA manuals not specifying so.
+def FeatureFSCA     : SubtargetFeature<"fsca", "HasFSCA", "true",
+                                       "Enable use of fsca instruction.">;
+def FeatureFSRRA    : SubtargetFeature<"fsrra", "HasFSRRA", "true",
+                                       "Enable use of fsrra instruction.">;
+def FeatureFP32     : SubtargetFeature<"fp32", "HasFP32", "true",
+                                       "Enable use of 32-bit floating point instructions.">;
+def FeatureFP64     : SubtargetFeature<"fp64", "HasFP64", "true",
+                                       "Enable use of 64-bit floating point instructions.">;
+def FeatureDSP      : SubtargetFeature<"dsp", "HasDSP", "true",
+                                       "Enable SuperH DSP Extensions">;
+
+//===----------------------------------------------------------------------===//
+// SuperH CPU Family features
+//===----------------------------------------------------------------------===//
+
 def FeatureSH1      : SubtargetFeature<"sh1", "SHArchVersion", "SH1",
                           "SH-1 ISA Support">;
 def FeatureSH2      : SubtargetFeature<"sh2", "SHArchVersion", "SH2",
                           "SH-2 ISA Support",
                           [FeatureSH1]>;
+def FeatureSH2E     : SubtargetFeature<"sh2e", "SHArchVersion", "SH2E",
+                          "SH-2E ISA Support",
+                          [FeatureSH1, FeatureSH2, FeatureFP32]>;
+def FeatureSH2A     : SubtargetFeature<"sh2a", "SHArchVersion", "SH2A",
+                          "SH-2A ISA Support",
+                          [FeatureSH1, FeatureSH2, FeatureFP32, FeatureFP64]>;
 def FeatureSH3      : SubtargetFeature<"sh3", "SHArchVersion", "SH3",
                           "SH-3 ISA Support",
                           [FeatureSH1, FeatureSH2]>;
-def FeatureSH4      : SubtargetFeature<"sh4", "SHArchVersion", "SH3",
+def FeatureSH3E     : SubtargetFeature<"sh3e", "SHArchVersion", "SH3E",
+                          "SH-3E ISA Support",
+                          [FeatureSH1, FeatureSH2, FeatureFP32]>;
+def FeatureSH4      : SubtargetFeature<"sh4", "SHArchVersion", "SH4",
                           "SH-4 ISA Support",
-                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureFPU]>;
+                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureFP32, 
+                           FeatureFP64]>;
 def FeatureSH4A     : SubtargetFeature<"sh4a", "SHArchVersion", "SH4A",
                           "SH-4A ISA Support",
-                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureSH4, FeatureFPU]>;
+                          [FeatureSH1, FeatureSH2, FeatureSH3, FeatureSH4, 
+                           FeatureFP32, FeatureFP64, FeatureFSCA, FeatureFSRRA]>;
 
 //===----------------------------------------------------------------------===//
 // SuperH Processors
@@ -46,7 +70,7 @@ def FeatureSH4A     : SubtargetFeature<"sh4a", "SHArchVersion", "SH4A",
 include "SuperHSchedule.td"
 
 class Proc<string Name, list<SubtargetFeature> Features>
-    : ProcessorModel<Name, GenericSHModel, Features>;
+    : ProcessorModel<Name, GenericSuperHModel, Features>;
 
 def : Proc<"generic", [FeatureSH4]>;
 def : Proc<"sh1", [FeatureSH1]>;
@@ -55,6 +79,12 @@ def : Proc<"sh3", [FeatureSH3]>;
 def : Proc<"sh4", [FeatureSH4]>;
 def : Proc<"sh4a", [FeatureSH4A]>;
 
+// NOTE:  SEGA got some special variants of the SuperH family
+//        produced with some extra features enabled, defining them here.
+def : Proc<"dreamcast", [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
+def : Proc<"naomi",     [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
+def : Proc<"saturn",    [FeatureSH2]>;
+
 //===----------------------------------------------------------------------===//
 // Register File Description
 //===----------------------------------------------------------------------===//
@@ -65,14 +95,42 @@ include "SuperHRegisterInfo.td"
 // Instruction Descriptions
 //===----------------------------------------------------------------------===//
 
-// include "SuperHInstrInfo.td"
-
+include "SuperHInstrInfo.td"
 def SuperHInstrInfo : InstrInfo;
 
+//===----------------------------------------------------------------------===//
+// Calling Conventions
+//===----------------------------------------------------------------------===//
+
+
+//===---------------------------------------------------------------------===//
+// Assembly Printers
+//===---------------------------------------------------------------------===//
+
+def SuperHAsmWriter : AsmWriter {
+ string AsmWriterClassName = "InstPrinter";
+ bit isMCAsmWriter = 1;
+}
+
+//===---------------------------------------------------------------------===//
+// Assembly Parsers
+//===---------------------------------------------------------------------===//
+
+def SuperHAsmParser : AsmParser {
+  let ShouldEmitMatchRegisterName = 0;
+  let ShouldEmitMatchRegisterAltName = 0;
+}
+
+def SuperHAsmParserVariant : AsmParserVariant {
+  int Variant = 0;
+}
+
 //===----------------------------------------------------------------------===//
 // Target Declaration
 //===----------------------------------------------------------------------===//
 
 def SuperH : Target {
   let InstructionSet = SuperHInstrInfo;
+  let AssemblyParsers = [SuperHAsmParser];
+  let AssemblyWriters = [SuperHAsmWriter];
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrData.td b/llvm/lib/Target/SuperH/SuperHInstrData.td
new file mode 100644
index 0000000000000..d272437fff6b0
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHInstrData.td
@@ -0,0 +1,13 @@
+//===-- SuperHInstrData.td - SuperH Data Transfer Instructions -*- tablegen -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file describes all of the data transfer instructions in the SuperH
+/// ISA.
+///
+//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/SuperH/SuperHInstrFormats.td b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
index d3bacb075c9ba..de042add3c8c8 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrFormats.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
@@ -9,13 +9,32 @@
 
 class SHInst<dag outs, dag ins, string asmstr, list<dag> pattern>
     : Instruction {
-  let Namespace = "SH";
 
+  let Namespace = "SH";
   dag OutOperandList = outs;
   dag InOperandList = ins;
-  let AsmString = asmtr;
+  let AsmString = asmstr;
   let Pattern = pattern;
   
   field bits<16> Inst;
+  let Size = 1;
+}
+
+class SHDSPInst<dag outs, dag ins, string asmstr, list<dag> pattern>
+    : Instruction {
+
+  let Namespace = "SH";
+  dag OutOperandList = outs;
+  dag InOperandList = ins;
+  let AsmString = asmstr;
+  let Pattern = pattern;
+  
+  field bits<32> Inst;
   let Size = 2;
-}
\ No newline at end of file
+}
+
+// SH PSEUDO INSTRUCTION
+class SHPseudo<dag outs, dag ins, list<dag> pattern = []>
+    : SHInst<outs, ins, "; error: this should not be emitted", pattern> {
+  let isPseudo = 1;
+}
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.td b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
index a4451d7a502d5..f085dd1ec52bc 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
@@ -11,6 +11,11 @@
 /// and properties of the instructions which are needed for code generation,
 /// machine code emission, and analysis.
 ///
+/// The subsystems are ordered after the instruction classification that
+/// The SuperH ISA manuals use.
+///
+/// See: https://www.shared-ptr.com/sh_insns.html
+///
 //===----------------------------------------------------------------------===//
 
 include "SuperHInstrFormats.td"
@@ -19,3 +24,35 @@ include "SuperHInstrFormats.td"
 // SuperH Type Profiles
 //===----------------------------------------------------------------------===//
 
+def SHSDT_CallSeqStart 	: SDCallSeqStart<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
+def SHSDT_CallSeqEnd	: SDCallSeqEnd<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
+def SHSDT_Call    		: SDTypeProfile<0, -1, [SDTCisVT<0, iPTR>]>;
+def SHSDT_Ret           : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
+
+//===----------------------------------------------------------------------===//
+// Nodes
+//===----------------------------------------------------------------------===//
+
+def SHCallSeqStart  : SDNode<"ISD::CALLSEQ_START", SHSDT_CallSeqStart,
+							 [SDNPHasChain, SDNPOutGlue]>;
+
+def SHCallSeqEnd    : SDNode<"ISD::CALLSEQ_END", SHSDT_CallSeqEnd,
+                             [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
+
+def SHCall          : SDNode<"SHISD::CALL", SHSDT_Call,
+                             [SDNPHasChain, SDNPOutGlue,
+                             SDNPOptInGlue, SDNPVariadic]>;
+
+def SHRet 		    : SDNode<"SHISD::RET", SHSDT_Ret,
+                             [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
+
+//===----------------------------------------------------------------------===//
+// Operands
+//===----------------------------------------------------------------------===//
+
+
+//===----------------------------------------------------------------------===//
+// Subsystems
+//===----------------------------------------------------------------------===//
+
+include "SuperHInstrData.td"
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
index abf4cbfcc3ba2..d8767915dfb45 100644
--- a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
@@ -49,13 +49,13 @@ class SHRegWithSubRegs<bits<16> Enc, string n, list<Register> subregs>
 }
 
 // SH General Purpose Registers.
-class GPRReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
+class GPRReg<bits<16> Enc, string n> : SHReg<Enc, n>;
 
 // Control Registers
-class CtrlReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
+class CtrlReg<bits<16> Enc, string n> : SHReg<Enc, n>;
 
 // SH System Registers.
-class SysReg<bits<16> Enc, string n> : MipsReg<Enc, n>;
+class SysReg<bits<16> Enc, string n> : SHReg<Enc, n>;
 
 // 32-bit floating point registers
 class FRReg<bits<16> Enc, string n> : SHReg<Enc, n>;
@@ -84,26 +84,26 @@ class MTRXReg<bits<16> Enc, string n> : SHReg<Enc, n>;
 let Namespace = "SH" in {
 
   // General Purpose Registers
-  def R0  : SHGPRReg<0,  "r0">,   DwarfRegNum<[0]>;
-  def R1  : SHGPRReg<1,  "r1">,   DwarfRegNum<[1]>;
-  def R2  : SHGPRReg<2,  "r2">,   DwarfRegNum<[2]>;
-  def R3  : SHGPRReg<3,  "r3">,   DwarfRegNum<[3]>;
-  def R4  : SHGPRReg<4,  "r4">,   DwarfRegNum<[4]>;
-  def R5  : SHGPRReg<5,  "r5">,   DwarfRegNum<[5]>;
-  def R6  : SHGPRReg<6,  "r6">,   DwarfRegNum<[6]>;
-  def R7  : SHGPRReg<7,  "r7">,   DwarfRegNum<[7]>;
-  def R8  : SHGPRReg<8,  "r8">,   DwarfRegNum<[8]>;
-  def R9  : SHGPRReg<9,  "r9">,   DwarfRegNum<[9]>;
-  def R10 : SHGPRReg<10, "r10">,  DwarfRegNum<[10]>;
-  def R11 : SHGPRReg<11, "r11">,  DwarfRegNum<[11]>;
-  def R12 : SHGPRReg<12, "r12">,  DwarfRegNum<[12]>;
-  def R13 : SHGPRReg<13, "r13">,  DwarfRegNum<[13]>;
-  def R14 : SHGPRReg<14, "r14">,  DwarfRegNum<[14]>;
-  def R15 : SHGPRReg<15, "r15">,  DwarfRegNum<[15]>;
+  def R0  : GPRReg<0,  "r0">,   DwarfRegNum<[0]>;
+  def R1  : GPRReg<1,  "r1">,   DwarfRegNum<[1]>;
+  def R2  : GPRReg<2,  "r2">,   DwarfRegNum<[2]>;
+  def R3  : GPRReg<3,  "r3">,   DwarfRegNum<[3]>;
+  def R4  : GPRReg<4,  "r4">,   DwarfRegNum<[4]>;
+  def R5  : GPRReg<5,  "r5">,   DwarfRegNum<[5]>;
+  def R6  : GPRReg<6,  "r6">,   DwarfRegNum<[6]>;
+  def R7  : GPRReg<7,  "r7">,   DwarfRegNum<[7]>;
+  def R8  : GPRReg<8,  "r8">,   DwarfRegNum<[8]>;
+  def R9  : GPRReg<9,  "r9">,   DwarfRegNum<[9]>;
+  def R10 : GPRReg<10, "r10">,  DwarfRegNum<[10]>;
+  def R11 : GPRReg<11, "r11">,  DwarfRegNum<[11]>;
+  def R12 : GPRReg<12, "r12">,  DwarfRegNum<[12]>;
+  def R13 : GPRReg<13, "r13">,  DwarfRegNum<[13]>;
+  def R14 : GPRReg<14, "r14">,  DwarfRegNum<[14]>;
+  def R15 : GPRReg<15, "r15">,  DwarfRegNum<[15]>;
 
   // Control Registers
-  def SR : CtrlReg<0, "sr">;    // Status Register
-  def GBR : CtrlReg<0, "gbr";   // Global Base Register
+  def SR  : CtrlReg<0, "sr">;   // Status Register
+  def GBR : CtrlReg<0, "gbr">;  // Global Base Register
   def VBR : CtrlReg<0, "vbr">;  // Vector Base Register
   def SGR : CtrlReg<0, "sgr">;  // Saved General Register
   def DBR : CtrlReg<0, "dbr">;  // Debug Base Register
@@ -111,12 +111,12 @@ let Namespace = "SH" in {
   def SPC : CtrlReg<0, "spc">;  // Saved Program Counter
 
   // System Registers
-  def PR : SysReg<0, "pr">;       // Procedure Register
-  def PC : SysReg<0, "pc">;       // Program Counter
-  def MACL : SysReg<0, "macl">;   // Mult & Accum Low
-  def MACH : SysReg<1, "mach">;   // Mult & Accum Hi
+  def PR    : SysReg<0, "pr">;    // Procedure Register
+  def PC    : SysReg<0, "pc">;    // Program Counter
+  def MACL  : SysReg<0, "macl">;  // Mult & Accum Low
+  def MACH  : SysReg<1, "mach">;  // Mult & Accum Hi
   def FPSCR : SysReg<0, "fpscr">; // FPU Status/Control Register
-  def FPUL : SysReg<0, "fpul">;   // FPU Comms Register
+  def FPUL  : SysReg<0, "fpul">;  // FPU Comms Register
 
   // 32-bit floating point registers
   foreach I = 0-15 in
@@ -126,21 +126,21 @@ let Namespace = "SH" in {
 
   // 64-bit floating point registers
   foreach I = 0-7 in
-    def DR#I : DRReg<!shl(I, 1), "dr"#!shl(I, 1),
-    [!cast<FPReg>("FR"#!shl(I, 1)),
-     !cast<FPReg>("FR"#!add(!shl(I, 1), 1)]>;
+    def DR#!shl(I, 1) : DRReg<!shl(I, 1), "dr"#!shl(I, 1),
+    [!cast<FRReg>("FR"#!shl(I, 1)),
+     !cast<FRReg>("FR"#!add(!shl(I, 1), 1))]>;
   foreach I = 0-7 in
-    def XD#I : DRReg<!shl(I, 1), "xd"#!shl(I, 1),
-    [!cast<FPReg>("XF"#!shl(I, 1)),
-     !cast<FPReg>("XF"#!add(!shl(I, 1), 1)]>;
+    def XD#!shl(I, 1) : DRReg<!shl(I, 1), "xd"#!shl(I, 1),
+    [!cast<FRReg>("XF"#!shl(I, 1)),
+     !cast<FRReg>("XF"#!add(!shl(I, 1), 1))]>;
 
   // 128-bit floating point vector registers
   foreach I = 0-3 in
-    def FV#I : FVReg<!shl(I, 2), "fv"#!shl(I, 2),
-    [!cast<FPReg>("FR"#!shl(I, 2)),
-     !cast<FPReg>("FR"#!add(!shl(I, 2), 1)),
-     !cast<FPReg>("FR"#!add(!shl(I, 2), 2)),
-     !cast<FPReg>("XF"#!add(!shl(I, 2), 3)]>;
+    def FV#!shl(I, 2) : FVReg<!shl(I, 2), "fv"#!shl(I, 2),
+    [!cast<FRReg>("FR"#!shl(I, 2)),
+     !cast<FRReg>("FR"#!add(!shl(I, 2), 1)),
+     !cast<FRReg>("FR"#!add(!shl(I, 2), 2)),
+     !cast<FRReg>("XF"#!add(!shl(I, 2), 3))]>;
   
   // 512-bit matrix register (shadows XF registers)
   // TODO: Add proper subregisters here.
@@ -151,49 +151,41 @@ let Namespace = "SH" in {
 // Register Classes
 //===----------------------------------------------------------------------===//
 
-def GPR : RegisterClass<"SH", [ i32 ], 32, (
-  add(
-    R0, R1, R2,  R3,  R4,  R5,  R6,  R7,  // Banked memory
-    R8, R9, R10, R11, R12, R13, R14, R15, // Non-banked memory.
-  ) 
+def GPR : RegisterClass<"SH", [ i32 ], 32, (add
+  R0, R1, R2,  R3,  R4,  R5,  R6,  R7,  // Banked memory
+  R8, R9, R10, R11, R12, R13, R14, R15  // Non-banked memory.
 )>;
 
 // 32-bit floating point registers.
-def FGR32 : RegisterClass<"SH", [f32], 32, (
-  add(
-    // FR
-    FR0,  FR1,  FR2,  FR3,  FR4,  FR5,  FR6,  FR7,
-    FR8,  FR9, FR10, FR11, FR12, FR13, FR14, FR15,
-
-    // XF
-    XF0,  XF1,  XF2,  XF3,  XF4,  XF5,  XF6,  XF7,
-    XF8,  XF9, XF10, XF11, XF12, XF13, XF14, XF15,
-  )
+def FR32 : RegisterClass<"SH", [f32], 32, (add
+  // FR
+  FR0,  FR1,  FR2,  FR3,  FR4,  FR5,  FR6,  FR7,
+  FR8,  FR9, FR10, FR11, FR12, FR13, FR14, FR15,
+
+  // XF
+  XF0,  XF1,  XF2,  XF3,  XF4,  XF5,  XF6,  XF7,
+  XF8,  XF9, XF10, XF11, XF12, XF13, XF14, XF15
 )>;
 
 // 64-bit floating point registers.
-def FGR64 : RegisterClass<"SH", [f64], 64, (add
+def FR64 : RegisterClass<"SH", [f64], 64, (add
   // DR
-  DR0,  DR1,  DR2,  DR3,  DR4,  DR5,  DR6,  DR7,
-  DR8,  DR9, DR10, DR11, DR12, DR13, DR14, DR15,
+  DR0, DR2, DR4, DR6, DR8, DR10, DR12, DR14,
 
   // XD
-  XD0,  XD1,  XD2,  XD3,  XD4,  XD5,  XD6,  XD7,
-  XD8,  XD9, XD10, XD11, XD12, XD13, XD14, XD15,
+  XD0, XD2, XD4, XD6, XD8, XD10, XD12, XD14
 )>;
 
 // Vector registers
-def VEC128 : RegisterClass<"SH", [v4f32], 128, (add
-  FV0, FV4, FV8, FV12)>;
+def VEC128 : RegisterClass<"SH", [v4f32], 128, (add FV0, FV4, FV8, FV12)>;
 
 // Matrix register
-def MTRX512 : RegisterClass<"SH", [v16f32],
-  (add (sequence "FR%u", 0, 15), (sequence "XF%u", 0, 15))>;
+def MTRX512 : RegisterClass<"SH", [v16f32], 512, (add (sequence "FR%u", 0, 15), (sequence "XF%u", 0, 15))>;
 
 // Control registers
-def CR : RegisterClass<"SH", [ i32 ], 32, 
+def CTRL : RegisterClass<"SH", [ i32 ], 32, 
   (add SR, GBR, VBR, SGR, DBR, SSR, SPC)>, Unallocatable;
 
 // System registers.
-def SR : RegisterClass<"SH", [ i32 ], 32, 
+def SYS : RegisterClass<"SH", [ i32 ], 32, 
   (add PR, PC, MACL, MACH, FPSCR, FPUL)>, Unallocatable;
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp b/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
index 91673573b22b2..7001640d4dc15 100644
--- a/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
+++ b/llvm/lib/Target/SuperH/SuperHTargetMachine.cpp
@@ -1,4 +1,5 @@
-//===-- SuperHTargetMachine.cpp - Define TargetMachine for SuperH -----------===//
+//===-- SuperHTargetMachine.cpp - Define TargetMachine for SuperH
+//-----------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -20,21 +21,23 @@
 using namespace llvm;
 
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSuperHTarget() {
-    RegisterTargetMachine<SuperHTargetMachine> SH(getTheSuperHTarget());
+  RegisterTargetMachine<SuperHTargetMachine> SH(getTheSuperHTarget());
+  RegisterTargetMachine<SuperHTargetMachine> SHLE(getTheSuperHLETarget());
 }
 
-SuperHTargetMachine::~SuperHTargetMachine() { }
+SuperHTargetMachine::~SuperHTargetMachine() {}
 
 /// Create a SuperH architecture model.
 SuperHTargetMachine::SuperHTargetMachine(const Target &T, const Triple &TT,
-                                           StringRef CPU, StringRef FS,
-                                           const TargetOptions &Options,
-                                           std::optional<Reloc::Model> RM,
-                                           std::optional<CodeModel::Model> CM,
-                                           CodeGenOptLevel OL, bool JIT)
-    : CodeGenTargetMachineImpl(
-        T, TT.computeDataLayout(), TT, CPU, FS, Options,
-        RM.value_or(Reloc::Static), getEffectiveCodeModel(CM, CodeModel::Small), 
-        OL) {
+                                         StringRef CPU, StringRef FS,
+                                         const TargetOptions &Options,
+                                         std::optional<Reloc::Model> RM,
+                                         std::optional<CodeModel::Model> CM,
+                                         CodeGenOptLevel OL, bool JIT)
+    : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+                               RM.value_or(Reloc::Static),
+                               getEffectiveCodeModel(CM, CodeModel::Small),
+                               OL) {
 
+  initAsmInfo();
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHTargetMachine.h b/llvm/lib/Target/SuperH/SuperHTargetMachine.h
index 5da814b766f4a..2b090d24ac13f 100644
--- a/llvm/lib/Target/SuperH/SuperHTargetMachine.h
+++ b/llvm/lib/Target/SuperH/SuperHTargetMachine.h
@@ -20,10 +20,6 @@
 namespace llvm {
 
 class SuperHTargetMachine : public CodeGenTargetMachineImpl {
-private:
-
-protected:
-
 public:
   SuperHTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
                      StringRef FS, const TargetOptions &Options,
@@ -33,6 +29,7 @@ class SuperHTargetMachine : public CodeGenTargetMachineImpl {
   ~SuperHTargetMachine() override;
 
 };
+
 } // end namespace llvm
 
 #endif
diff --git a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
index 4045218737a15..83c5970fc6783 100644
--- a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
+++ b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.cpp
@@ -9,6 +9,7 @@
 #include "TargetInfo/SuperHTargetInfo.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/Compiler.h"
+
 using namespace llvm;
 
 Target &llvm::getTheSuperHTarget() {
@@ -16,8 +17,15 @@ Target &llvm::getTheSuperHTarget() {
   return TheSuperHTarget;
 }
 
+Target &llvm::getTheSuperHLETarget() {
+  static Target TheSuperHLETarget;
+  return TheSuperHLETarget;
+}
+
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY
 void LLVMInitializeSuperHTargetInfo() {
   RegisterTarget<Triple::sh, /*HasJIT=*/false> X(getTheSuperHTarget(),
-                                                    "sh", "SuperH", "SuperH");
+                                                    "sh", "SuperH (big endian)", "SuperH");
+  RegisterTarget<Triple::sh_le, /*HasJIT=*/false> Y(getTheSuperHLETarget(),
+                                                    "sh_le", "SuperH (little endian)", "SuperH");
 }
diff --git a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h
index 38b501e05d46e..9dbef1aa07841 100644
--- a/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h
+++ b/llvm/lib/Target/SuperH/TargetInfo/SuperHTargetInfo.h
@@ -14,6 +14,7 @@ namespace llvm {
 class Target;
 
 Target &getTheSuperHTarget();
+Target &getTheSuperHLETarget();
 
 } // namespace llvm
 
diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
index a2125eeb82932..d0a5ae9544b7e 100644
--- a/llvm/lib/TargetParser/TargetDataLayout.cpp
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -552,6 +552,31 @@ static std::string computeVEDataLayout(const Triple &T) {
   return Ret;
 }
 
+static std::string computeSuperHDataLayout(const Triple &T) {
+
+  // Mixed-endian
+  std::string Ret = T.getArch() == Triple::sh_le ? "e" : "E";
+
+  // ELF name manging
+  Ret += "-m:e";
+
+  // 32-bit pointers, 32 bit aligned
+  Ret += "-p:32:32";
+
+  // 32 bit integers, 32 bit aligned
+  Ret += "-i32:32";
+
+  // 32 bit alignment of objects of aggregate type
+  Ret += "-a:0:32";
+
+  // 32 bit native integer width
+  Ret += "-n32";
+
+  // 32 bit natural stack alignment
+  Ret += "-S32";
+  return Ret;
+}
+
 std::string Triple::computeDataLayout(StringRef ABIName) const {
   switch (getArch()) {
   case Triple::arm:
@@ -611,6 +636,9 @@ std::string Triple::computeDataLayout(StringRef ABIName) const {
     return computeSparcDataLayout(*this);
   case Triple::systemz:
     return computeSystemZDataLayout(*this);
+  case Triple::sh:
+  case Triple::sh_le:
+    return computeSuperHDataLayout(*this);
   case Triple::tce:
     return "E-p:32:32:32-i1:8:8-i8:8:32-i16:16:32-i32:32:32-i64:32:32-"
            "f16:16:16-f32:32:32-f64:32:32-v64:64:64-i128:128-v128:128:128-"
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 593af6c736a70..1e9d675ff5162 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -108,6 +108,8 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
     return "riscv64be";
   case sh:
     return "sh";
+  case sh_le:
+    return "sh_le";
   case shave:
     return "shave";
   case sparc:
@@ -1232,6 +1234,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
   case Triple::riscv32be:
   case Triple::riscv64be:
   case Triple::sh:
+  case Triple::sh_le:
   case Triple::shave:
   case Triple::sparc:
   case Triple::sparcel:
@@ -1996,6 +1999,7 @@ unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv32be:
   case llvm::Triple::sh:
+  case llvm::Triple::sh_le:
   case llvm::Triple::shave:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcel:
@@ -2108,6 +2112,7 @@ Triple Triple::get32BitArchVariant() const {
   case Triple::riscv32:
   case Triple::riscv32be:
   case Triple::sh:
+  case Triple::sh_le:
   case Triple::shave:
   case Triple::sparc:
   case Triple::sparcel:
@@ -2201,6 +2206,7 @@ Triple Triple::get64BitArchVariant() const {
   case Triple::msp430:
   case Triple::r600:
   case Triple::sh:
+  case Triple::sh_le:
   case Triple::shave:
   case Triple::sparcel:
   case Triple::tce:
@@ -2381,6 +2387,9 @@ Triple Triple::getBigEndianArchVariant() const {
   case Triple::sparcel:
     T.setArch(Triple::sparc);
     break;
+  case Triple::sh_le:
+    T.setArch(Triple::sh);
+    break;
   case Triple::tcele:
     T.setArch(Triple::tce);
     break;
@@ -2436,6 +2445,9 @@ Triple Triple::getLittleEndianArchVariant() const {
   case Triple::sparc:
     T.setArch(Triple::sparcel);
     break;
+  case Triple::sh:
+    T.setArch(Triple::sh_le);
+    break;
   case Triple::tce:
     T.setArch(Triple::tcele);
     break;
@@ -2476,6 +2488,7 @@ bool Triple::isLittleEndian() const {
   case Triple::riscv32:
   case Triple::riscv64:
   case Triple::shave:
+  case Triple::sh_le:
   case Triple::sparcel:
   case Triple::spir64:
   case Triple::spir:

>From ef94ab52dbd725ceca140a7a92e42265d066647e Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Wed, 3 Jun 2026 07:14:07 +0200
Subject: [PATCH 5/7] Add SuperH ELF reloc info

---
 llvm/include/llvm/BinaryFormat/ELF.h          |  5 +++
 .../llvm/BinaryFormat/ELFRelocs/SuperH.def    | 38 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 llvm/include/llvm/BinaryFormat/ELFRelocs/SuperH.def

diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 72cded68463a6..6bc9e2a6168d2 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -727,6 +727,11 @@ enum {
 #undef ELF_RISCV_NONSTANDARD_RELOC
 };
 
+// ELF Relocation types for SuperH
+enum {
+#include "ELFRelocs/SuperH.def"
+};
+
 enum {
   // Symbol may follow different calling convention than the standard calling
   // convention.
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/SuperH.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/SuperH.def
new file mode 100644
index 0000000000000..031ecbc6a6448
--- /dev/null
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/SuperH.def
@@ -0,0 +1,38 @@
+
+#ifndef ELF_RELOC
+#error "ELF_RELOC must be defined"
+#endif
+
+// IDs are taken from the Renesas C ABI Specification.
+// See: https://www.renesas.com/en/document/mat/superh-cc-compiler-package-v904-users-manual?r=1169516
+
+ELF_RELOC(R_SH_NONE,                  0)
+ELF_RELOC(R_SH_GOT32,               160)
+ELF_RELOC(R_SH_GOT_LOW16,           169)
+ELF_RELOC(R_SH_GOT_MEDLOW16,        170)
+ELF_RELOC(R_SH_GOT_MEDHI16,         171)
+ELF_RELOC(R_SH_GOT_HI16,            172)
+ELF_RELOC(R_SH_GOT10BY4,            189)
+ELF_RELOC(R_SH_GOT10BY8,            191)
+ELF_RELOC(R_SH_PLT32,               161)
+ELF_RELOC(R_SH_PLT_LOW16,           177)
+ELF_RELOC(R_SH_PLT_MEWLOW16,        178)
+ELF_RELOC(R_SH_PLT_MEDHI16,         179)
+ELF_RELOC(R_SH_PLT_HI16,            180)
+ELF_RELOC(R_SH_GOTPLT32,            168)
+ELF_RELOC(R_SH_GOTPLT_LOW16,        169)
+ELF_RELOC(R_SH_GOTPLT_MEDLOW16,     170)
+ELF_RELOC(R_SH_GOTPLT_MEDHI16,      171)
+ELF_RELOC(R_SH_GOTPLT_HI16,         172)
+ELF_RELOC(R_SH_GOTPLT10BY4,         189)
+ELF_RELOC(R_SH_GOTPLT10BY8,         191)
+ELF_RELOC(R_SH_GOTOFF,              166)
+ELF_RELOC(R_SH_GOTOFF_LOW16,        181)
+ELF_RELOC(R_SH_GOTOFF_MEWLOW16,     182)
+ELF_RELOC(R_SH_GOTOFF_MEDHI16,      183)
+ELF_RELOC(R_SH_GOTOFF_HI16,         184)
+ELF_RELOC(R_SH_GOTPC,               167)
+ELF_RELOC(R_SH_GOTPC_LOW16,         185)
+ELF_RELOC(R_SH_GOTPC_MEDLOW16,      186)
+ELF_RELOC(R_SH_GOTPC_MEDHI16,       187)
+ELF_RELOC(R_SH_GOTPC_HI16,          188)
\ No newline at end of file

>From c266c8b01521a3994efcb7df862ae9fb6ae2a116 Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Wed, 3 Jun 2026 07:15:18 +0200
Subject: [PATCH 6/7] Implement assembler groundwork

---
 .../SuperH/AsmParser/SuperHAsmParser.cpp      | 418 +++++++++++++++++-
 llvm/lib/Target/SuperH/CMakeLists.txt         |  22 +-
 .../Target/SuperH/MCTargetDesc/CMakeLists.txt |   2 +
 .../SuperH/MCTargetDesc/SuperHAsmBackend.cpp  |  80 ++++
 .../MCTargetDesc/SuperHELFObjectWriter.cpp    |  55 +++
 .../SuperH/MCTargetDesc/SuperHInstPrinter.cpp |  39 +-
 .../SuperH/MCTargetDesc/SuperHInstPrinter.h   |  12 +-
 .../SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp   |   1 +
 .../MCTargetDesc/SuperHMCTargetDesc.cpp       |  34 +-
 .../SuperH/MCTargetDesc/SuperHMCTargetDesc.h  |  22 +-
 llvm/lib/Target/SuperH/SuperH.h               |  19 +
 llvm/lib/Target/SuperH/SuperH.td              |  35 +-
 .../lib/Target/SuperH/SuperHFrameLowering.cpp |  44 ++
 llvm/lib/Target/SuperH/SuperHFrameLowering.h  |  50 +++
 llvm/lib/Target/SuperH/SuperHInstrData.td     |  43 ++
 llvm/lib/Target/SuperH/SuperHInstrFormats.td  | 205 ++++++++-
 llvm/lib/Target/SuperH/SuperHInstrInfo.cpp    |  35 ++
 llvm/lib/Target/SuperH/SuperHInstrInfo.h      |  35 ++
 llvm/lib/Target/SuperH/SuperHInstrInfo.td     |  56 ++-
 llvm/lib/Target/SuperH/SuperHRegisterInfo.cpp |  78 ++++
 llvm/lib/Target/SuperH/SuperHRegisterInfo.h   |  53 +++
 llvm/lib/Target/SuperH/SuperHRegisterInfo.td  |   4 +-
 llvm/lib/Target/SuperH/SuperHSubtarget.cpp    |  22 +
 llvm/lib/Target/SuperH/SuperHSubtarget.h      |  49 ++
 24 files changed, 1320 insertions(+), 93 deletions(-)
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHAsmBackend.cpp
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHELFObjectWriter.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperH.h
 create mode 100644 llvm/lib/Target/SuperH/SuperHFrameLowering.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperHFrameLowering.h
 create mode 100644 llvm/lib/Target/SuperH/SuperHInstrInfo.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperHInstrInfo.h
 create mode 100644 llvm/lib/Target/SuperH/SuperHRegisterInfo.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperHRegisterInfo.h
 create mode 100644 llvm/lib/Target/SuperH/SuperHSubtarget.cpp
 create mode 100644 llvm/lib/Target/SuperH/SuperHSubtarget.h

diff --git a/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp b/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
index 6ed41223b23eb..722c81cbcd0ea 100644
--- a/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
+++ b/llvm/lib/Target/SuperH/AsmParser/SuperHAsmParser.cpp
@@ -7,17 +7,433 @@
 //===----------------------------------------------------------------------===//
 
 #include "MCTargetDesc/SuperHMCAsmInfo.h"
+#include "MCTargetDesc/SuperHMCTargetDesc.h"
+#include "SuperHRegisterInfo.h"
 #include "TargetInfo/SuperHTargetInfo.h"
+#include "llvm/Analysis/Utils/TrainingLogger.h"
 #include "llvm/MC/MCELFStreamer.h"
 #include "llvm/MC/MCInstrAnalysis.h"
 #include "llvm/MC/MCInstPrinter.h"
 #include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCRegister.h"
 #include "llvm/MC/MCRegisterInfo.h"
 #include "llvm/MC/MCSubtargetInfo.h"
 #include "llvm/MC/TargetRegistry.h"
+#include "llvm/MC/MCAsmMacro.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCInstBuilder.h"
+#include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCObjectFileInfo.h"
+#include "llvm/MC/MCParser/AsmLexer.h"
+#include "llvm/MC/MCParser/MCAsmParser.h"
+#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
+#include "llvm/MC/MCParser/MCTargetAsmParser.h"
+#include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/SMLoc.h"
+#include "llvm/Support/raw_ostream.h"
+#include "iostream"
 #include <sstream>
+#include <system_error>
 
 using namespace llvm;
 
+// The generated AsmMatcher SparcGenAsmMatcher uses "SuperH" as the target
+// namespace. But SPARC backend uses "SH" as its namespace.
+namespace llvm {
+namespace SuperH {
+
+    using namespace SH;
+
+} // end namespace SuperH
+} // end namespace llvm
+
+namespace {
+class SuperHOperand;
+
+class SuperHAsmParser : public MCTargetAsmParser {
+  MCAsmParser &Parser;
+  const MCRegisterInfo &MRI;
+
+#define GET_ASSEMBLER_HEADER
+#include "SuperHGenAsmMatcher.inc"
+
+  bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
+  ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
+  MCRegister matchRegisterName(const AsmToken &Tok, unsigned &RegKind);
+  bool parseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc, OperandVector &Operands) override;
+  ParseStatus parseDirective(AsmToken DirectiveID) override;
+  bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
+                                       OperandVector &Operands, MCStreamer &Out,
+                                       uint64_t &ErrorInfo,
+                                       bool MatchingInlineAsm) override;
+  ParseStatus parseOperand(OperandVector &Operands);
+  ParseStatus parseRegister(MCRegister &Reg, unsigned &RegKind, SMLoc &StartLoc, SMLoc &EndLoc);
+  ParseStatus parseImm(int64_t &Imm, SMLoc &StartLoc, SMLoc &EndLoc);
+
+public:
+  SuperHAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser, const MCInstrInfo &MII) 
+    : MCTargetAsmParser(sti, MII), Parser(parser),
+        MRI(*Parser.getContext().getRegisterInfo()) {
+
+    setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
+  }
+};
+
+} // end anonymous namespace
+
+namespace {
+
+class SuperHOperand : public MCParsedAsmOperand {
+public:
+  enum RegisterKind {
+    rk_None,
+    rk_GPR,
+    rk_FR32,
+    rk_FR64,
+    rk_VEC128,
+    rk_XMTRX
+  };
+
+private:
+  enum KindTy {
+    k_Token,
+    k_Register,
+    k_Immediate,
+  } Kind;
+
+  SMLoc StartLoc, EndLoc;
+
+  struct Token {
+    const char *Data;
+    unsigned Length;
+  };
+
+  struct RegOp {
+    MCRegister Reg;
+    RegisterKind Kind;
+  };
+
+  struct ImmOp {
+    const MCExpr *Val;
+  };
+
+  struct MemOp {
+    MCRegister Base;
+    MCRegister OffsetReg;
+    const MCExpr *Off;
+  };
+
+  union {
+    struct Token Tok;
+    struct RegOp Reg;
+    struct ImmOp Imm;
+    struct MemOp Mem;
+    unsigned ASI;
+    unsigned Prefetch;
+  };
+
+public:
+  SuperHOperand(KindTy K) : Kind(K) {}
+
+  bool isToken() const override { return Kind == k_Token; }
+  bool isImm() const override { return Kind == k_Immediate; }
+  bool isReg() const override { return Kind == k_Register; }
+  bool isMem() const override { return false; }
+  bool isImm8() const { return Kind == k_Immediate; }
+
+  SMLoc getStartLoc() const override { return StartLoc; }
+  SMLoc getEndLoc() const override { return EndLoc; }
+
+  void addRegOperands(MCInst &Inst, unsigned N) const {
+    assert(N == 1 && "Invalid number of operands!");
+    Inst.addOperand(MCOperand::createReg(getReg()));
+  }
+
+  void addImmOperands(MCInst &Inst, unsigned N) const {
+    assert(N == 1 && "Invalid number of operands!");
+    const MCExpr *Expr = getImm();
+    addExpr(Inst, Expr);
+  }
+
+  void addExpr(MCInst &Inst, const MCExpr *Expr) const{
+    // Add as immediate when possible.  Null MCExpr = 0.
+    if (!Expr)
+      Inst.addOperand(MCOperand::createImm(0));
+    else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
+      Inst.addOperand(MCOperand::createImm(CE->getValue()));
+    else
+      Inst.addOperand(MCOperand::createExpr(Expr));
+  }
+
+  StringRef getToken() const {
+    assert(Kind == k_Token && "Invalid access!");
+    return StringRef(Tok.Data, Tok.Length);
+  }
+
+  MCRegister getReg() const override {
+    assert((Kind == k_Register) && "Invalid access!");
+    return Reg.Reg;
+  }
+
+  const MCExpr *getImm() const {
+    assert((Kind == k_Immediate) && "Invalid access!");
+    return Imm.Val;
+  }
+
+  static std::unique_ptr<SuperHOperand> CreateToken(StringRef Str, SMLoc S) {
+    auto Op = std::make_unique<SuperHOperand>(k_Token);
+    Op->Tok.Data = Str.data();
+    Op->Tok.Length = Str.size();
+    Op->StartLoc = S;
+    Op->EndLoc = S;
+    return Op;
+  }
+
+  static std::unique_ptr<SuperHOperand> CreateReg(MCRegister Reg, unsigned Kind,
+                                                 SMLoc S, SMLoc E) {
+    auto Op = std::make_unique<SuperHOperand>(k_Register);
+    Op->Reg.Reg = Reg;
+    Op->Reg.Kind = (SuperHOperand::RegisterKind)Kind;
+    Op->StartLoc = S;
+    Op->EndLoc = E;
+    return Op;
+  }
+
+  static std::unique_ptr<SuperHOperand> CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
+    auto Op = std::make_unique<SuperHOperand>(k_Immediate);
+    Op->Imm.Val = Val;
+    Op->StartLoc = S;
+    Op->EndLoc = E;
+    return Op;
+  }
+
+  void print(raw_ostream &, const MCAsmInfo &) const override {
+
+  }
+};
+
+} // end anonymous namespace
+
+#define GET_MATCHER_IMPLEMENTATION
+#define GET_REGISTER_MATCHER
+#define GET_MNEMONIC_SPELL_CHECKER
+#define GET_MNEMONIC_CHECKER
+#include "SuperHGenAsmMatcher.inc"
+
+MCRegister SuperHAsmParser::matchRegisterName(const AsmToken &Tok, unsigned &RegKind) {
+  RegKind = SuperHOperand::rk_None;
+  if(!Tok.is(AsmToken::Identifier))
+    return SH::NoRegister;
+
+  StringRef Name = Tok.getString();
+  MCRegister Reg = MatchRegisterName(Name.lower());
+  if (Reg) {
+
+    // XMTRX register.
+    if (Reg == SH::XMTRX) {
+      RegKind = SuperHOperand::rk_XMTRX;
+      return Reg;
+    }
+
+    // General purpose register class.
+    if (MRI.getRegClass(SH::GPRRegClassID).contains(Reg)) {
+      RegKind = SuperHOperand::rk_GPR;
+      return Reg;
+    }
+
+    // 32-bit float registers.
+    if (MRI.getRegClass(SH::FR32RegClassID).contains(Reg)) {
+      RegKind = SuperHOperand::rk_FR32;
+      return Reg;
+    }
+
+    // 64-bit float registers.
+    if (MRI.getRegClass(SH::FR64RegClassID).contains(Reg)) {
+      RegKind = SuperHOperand::rk_FR64;
+      return Reg;
+    }
+
+    // 128-bit vector registers.
+    if (MRI.getRegClass(SH::VEC128RegClassID).contains(Reg)) {
+      RegKind = SuperHOperand::rk_VEC128;
+      return Reg;
+    }
+  }
+  return SH::NoRegister;
+}
+
+bool SuperHAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) {
+  if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
+      return Error(StartLoc, "invalid register name");
+  return false;
+}
+
+ParseStatus SuperHAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) {
+  unsigned RegKind;
+  const AsmToken &Tok = Parser.getTok();
+  StartLoc = Tok.getLoc();
+  EndLoc = Tok.getEndLoc();
+
+  // SuperH Registers are in the form of an identifier.
+  Reg = SH::NoRegister;
+  if (getLexer().getKind() != AsmToken::Identifier)
+    return ParseStatus::NoMatch;
+
+  // Match.
+  Reg = matchRegisterName(Tok, RegKind);
+  if (RegKind == SuperHOperand::rk_None)
+    return ParseStatus::NoMatch;
+
+  // Consume the register.
+  Parser.Lex();
+  return ParseStatus::Success;
+}
+
+ParseStatus SuperHAsmParser::parseImm(int64_t &Imm, SMLoc &StartLoc, SMLoc &EndLoc) {
+  const AsmToken &Tok = Parser.getTok();
+  StartLoc = Tok.getLoc();
+  EndLoc = Tok.getEndLoc();
+
+  if (Tok.is(AsmToken::Integer)) {
+    Imm = Tok.getIntVal();
+    Parser.Lex();
+    return ParseStatus::Success;
+  }
+  return ParseStatus::Failure;
+}
+
+ParseStatus SuperHAsmParser::parseRegister(MCRegister &Reg, unsigned &RegKind, SMLoc &StartLoc, SMLoc &EndLoc) {
+  const AsmToken &Tok = Parser.getTok();
+  StartLoc = Tok.getLoc();
+  EndLoc = Tok.getEndLoc();
+
+  Reg = matchRegisterName(Tok, RegKind);
+  if (Reg) {
+    Parser.Lex();
+    return ParseStatus::Success;
+  }
+  return ParseStatus::Failure;
+}
+
+ParseStatus SuperHAsmParser::parseOperand(OperandVector &Operands) {
+  const AsmToken &Tok = Parser.getTok();
+  SMLoc StartLoc = getLexer().getLoc();
+  SMLoc EndLoc = getLexer().getLoc();
+
+  switch(Tok.getKind()) {
+  default: {
+    return ParseStatus::Failure;
+  }
+
+  // Immediates.
+  case llvm::AsmToken::Hash: {
+    Parser.Lex();
+    int64_t Imm;
+    if (parseImm(Imm, StartLoc, EndLoc).isSuccess()) {
+      const MCExpr *Val = MCConstantExpr::create(Imm, getContext());
+      Operands.push_back(SuperHOperand::CreateImm(Val, StartLoc, EndLoc));
+      return ParseStatus::Success;
+    }
+
+    // Un-lex on error
+    getLexer().UnLex(Tok);
+    return ParseStatus::Failure;
+  }
+
+  // Registers.
+  case AsmToken::Identifier: {
+    unsigned RegKind;
+    MCRegister Reg;
+    if (parseRegister(Reg, RegKind, StartLoc, EndLoc).isSuccess()) {
+      Operands.push_back(SuperHOperand::CreateReg(Reg, RegKind, StartLoc, EndLoc));
+      return ParseStatus::Success;
+    }
+    return ParseStatus::Failure;
+  }
+  }
+}
+
+bool SuperHAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc, OperandVector &Operands) {
+
+  // Match mnemonic.
+  bool MS = SuperHCheckMnemonic(Name, this->getAvailableFeatures(), 0);
+  if (!MS) {
+    return Error(NameLoc, "invalid instruction mnemonic" + 
+      SuperHMnemonicSpellCheck(Name, getAvailableFeatures(), 0));
+  }
+
+  // Chomp name and add it to the operands.
+  Operands.push_back(SuperHOperand::CreateToken(Name, NameLoc));
+  if (getLexer().isNot(AsmToken::EndOfStatement)) {
+    
+    // Initial operand
+    if (!parseOperand(Operands).isSuccess()) {
+      SMLoc Loc = getLexer().getLoc();
+      return Error(Loc, "unexpected token");
+    }
+
+    // Followup operands.
+    while (getLexer().is(AsmToken::Comma)) {
+      Parser.Lex();
+
+      // Parse and remember operand.
+      if (!parseOperand(Operands).isSuccess()) {
+        SMLoc Loc = getLexer().getLoc();
+        return Error(Loc, "unexpected token");
+      }
+    }
+  }
+
+  // Consume EndOfStatement
+  Parser.Lex();
+  return false;
+}
+
+ParseStatus SuperHAsmParser::parseDirective(AsmToken DirectiveID) {
+  return ParseStatus::NoMatch;
+}
+
+bool SuperHAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
+                                     OperandVector &Operands, MCStreamer &Out,
+                                     uint64_t &ErrorInfo,
+                                     bool MatchingInlineAsm) {
+  MCInst Inst;
+  unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
+  switch(MatchResult) {
+  case Match_Success:
+    Inst.setLoc(IDLoc);
+    Out.emitInstruction(Inst, getSTI());
+    return false;
+  case Match_MissingFeature:
+    return Error(IDLoc, "instruction requires a CPU feature not currently enabled.");
+  case Match_InvalidOperand: {
+    SMLoc ErrorLoc = IDLoc;
+    if (ErrorInfo != ~0ULL) {
+      if (ErrorInfo >= Operands.size())
+        return Error(IDLoc, "too few operands for instruction");
+
+      ErrorLoc = ((SuperHOperand &)*Operands[ErrorInfo]).getStartLoc();
+      if (ErrorLoc == SMLoc())
+        ErrorLoc = IDLoc;
+    }
+
+    return Error(ErrorLoc, "invalid operand for instruction");
+  }
+  case Match_MnemonicFail:
+    return Error(IDLoc, "invalid instruction mnemonic");
+  }
+  return false;
+}
+
+
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
-LLVMInitializeSuperHAsmParser() { }
\ No newline at end of file
+LLVMInitializeSuperHAsmParser() {
+  RegisterMCAsmParser<SuperHAsmParser> A(getTheSuperHTarget());
+  RegisterMCAsmParser<SuperHAsmParser> B(getTheSuperHLETarget());
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/CMakeLists.txt b/llvm/lib/Target/SuperH/CMakeLists.txt
index 64d7df4fb153e..df685cc0dbddb 100644
--- a/llvm/lib/Target/SuperH/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/CMakeLists.txt
@@ -2,14 +2,26 @@ add_llvm_component_group(SuperH)
 
 set(LLVM_TARGET_DEFINITIONS SuperH.td)
 
-tablegen(LLVM SuperHGenRegisterInfo.inc     -gen-register-info)
-#tablegen(LLVM SuperHGenInstrInfo.inc        -gen-instr-info)
-tablegen(LLVM SuperHGenSubtargetInfo.inc    -gen-subtarget)
+tablegen(LLVM SuperHGenAsmMatcher.inc         -gen-asm-matcher)
+tablegen(LLVM SuperHGenAsmWriter.inc          -gen-asm-writer)
+tablegen(LLVM SuperHGenCallingConv.inc        -gen-callingconv)
+tablegen(LLVM SuperHGenDAGISel.inc            -gen-dag-isel)
+tablegen(LLVM SuperHGenDisassemblerTables.inc -gen-disassembler)
+tablegen(LLVM SuperHGenRegisterInfo.inc       -gen-register-info)
+tablegen(LLVM SuperHGenInstrInfo.inc          -gen-instr-info)
+#tablegen(LLVM SuperHGenMCCodeEmitter.inc      -gen-emitter)
+tablegen(LLVM SuperHGenSDNodeInfo.inc         -gen-sd-node-info -sdnode-namespace=SHISD)
+tablegen(LLVM SuperHGenSearchableTables.inc   -gen-searchable-tables)
+tablegen(LLVM SuperHGenSubtargetInfo.inc      -gen-subtarget)
 
 add_public_tablegen_target(SuperHCommonTableGen)
 
 add_llvm_target(SuperHCodeGen
   SuperHTargetMachine.cpp
+  SuperHFrameLowering.cpp
+  SuperHRegisterInfo.cpp
+  SuperHInstrInfo.cpp
+  SuperHSubtarget.cpp
 
   LINK_COMPONENTS
   Analysis
@@ -31,5 +43,5 @@ add_llvm_target(SuperHCodeGen
   )
 
 add_subdirectory(AsmParser)
-add_subdirectory(TargetInfo)
-add_subdirectory(MCTargetDesc)
\ No newline at end of file
+add_subdirectory(MCTargetDesc)
+add_subdirectory(TargetInfo)
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
index 181ec4b281c14..a612d81272d0e 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
@@ -2,6 +2,8 @@ add_llvm_component_library(LLVMSuperHDesc
   SuperHMCTargetDesc.cpp
   SuperHMCAsmInfo.cpp
   SuperHInstPrinter.cpp
+  SuperHELFObjectWriter.cpp
+  SuperHAsmBackend.cpp
 
   LINK_COMPONENTS
   MC
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHAsmBackend.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHAsmBackend.cpp
new file mode 100644
index 0000000000000..9e30207f6b32c
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHAsmBackend.cpp
@@ -0,0 +1,80 @@
+//===-- SparcAsmBackend.cpp - Sparc Assembler Backend ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCTargetDesc/SuperHMCTargetDesc.h"
+#include "llvm/ADT/StringSwitch.h"
+#include "llvm/MC/MCAsmBackend.h"
+#include "llvm/MC/MCELFObjectWriter.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCObjectWriter.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCValue.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/EndianStream.h"
+
+using namespace llvm;
+
+namespace {
+class SuperHAsmBackend : public MCAsmBackend {
+public:
+  SuperHAsmBackend(const MCSubtargetInfo &STI)
+      : MCAsmBackend(STI.getTargetTriple().isLittleEndian()
+                         ? llvm::endianness::little
+                         : llvm::endianness::big) {}
+
+  std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
+  MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
+  void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
+                  uint8_t *Data, uint64_t Value, bool IsResolved) override;
+
+  bool writeNopData(raw_ostream &OS, uint64_t Count,
+                    const MCSubtargetInfo *STI) const override {
+
+    // If the count is not 4-byte aligned, we must be writing data into the
+    // text section (otherwise we have unaligned instructions, and thus have
+    // far bigger problems), so just write zeros instead.
+    OS.write_zeros(Count % 2);
+    return true;
+  }
+};
+
+class ELFSuperHAsmBackend : public SuperHAsmBackend {
+  Triple::OSType OSType;
+
+public:
+  ELFSuperHAsmBackend(const MCSubtargetInfo &STI, Triple::OSType OSType)
+      : SuperHAsmBackend(STI), OSType(OSType) {}
+
+  std::unique_ptr<MCObjectTargetWriter>
+  createObjectTargetWriter() const override {
+    uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
+    return createSuperHELFObjectWriter(OSABI);
+  }
+};
+} // end anonymous namespace
+
+std::optional<MCFixupKind> SuperHAsmBackend::getFixupKind(StringRef Name) const {
+  return std::nullopt;
+}
+
+MCFixupKindInfo SuperHAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
+  return {"", 0, 2, 0};
+}
+
+void SuperHAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
+                                 const MCValue &Target, uint8_t *Data,
+                                 uint64_t Value, bool IsResolved) {
+
+}
+
+MCAsmBackend *llvm::createSuperHAsmBackend(const Target &T,
+                                          const MCSubtargetInfo &STI,
+                                          const MCRegisterInfo &MRI,
+                                          const MCTargetOptions &Options) {
+  return new ELFSuperHAsmBackend(STI, STI.getTargetTriple().getOS());
+}
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHELFObjectWriter.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHELFObjectWriter.cpp
new file mode 100644
index 0000000000000..8df58460dc2a9
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHELFObjectWriter.cpp
@@ -0,0 +1,55 @@
+//===-- SuperHELFObjectWriter.cpp - SuperH ELF Writer ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCTargetDesc/SuperHMCTargetDesc.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCELFObjectWriter.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCObjectFileInfo.h"
+#include "llvm/MC/MCObjectWriter.h"
+#include "llvm/MC/MCValue.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace llvm;
+
+namespace {
+  class SuperHELFObjectWriter : public MCELFObjectTargetWriter {
+  public:
+    SuperHELFObjectWriter(uint8_t OSABI)
+        : MCELFObjectTargetWriter(
+              false, OSABI,
+              ELF::EM_SH,
+              /*HasRelocationAddend*/ true) {}
+
+    ~SuperHELFObjectWriter() override = default;
+
+  protected:
+    unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target,
+                          bool IsPCRel) const override;
+
+    bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override;
+  };
+}
+
+unsigned SuperHELFObjectWriter::getRelocType(const MCFixup &Fixup,
+                                            const MCValue &Target,
+                                            bool IsPCRel) const {
+  return ELF::R_SH_NONE;
+}
+
+bool SuperHELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
+                                                   unsigned Type) const {
+  
+  return false;
+}
+
+std::unique_ptr<MCObjectTargetWriter>
+llvm::createSuperHELFObjectWriter(uint8_t OSABI) {
+  return std::make_unique<SuperHELFObjectWriter>(OSABI);
+}
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
index 2b32ae9c389e0..95d0618ad6478 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.cpp
@@ -12,16 +12,25 @@
 //===----------------------------------------------------------------------===//
 
 #include "SuperHInstPrinter.h"
-// #include "SuperHInstrInfo.h"
 #include "llvm/ADT/StringExtras.h"
-#include <llvm/MC/MCInst.h>
+#include "llvm/MC/MCInst.h"
+#include "llvm/Support/Debug.h"
 
 using namespace llvm;
 
-#define DEBUG_TYPE "sh-isel"
+#define DEBUG_TYPE "sh-asmprinter"
 
+// The generated AsmMatcher SparcGenAsmWriter uses "SuperH" as the target
+// namespace. But SuperH backend uses "SH" as its namespace.
+namespace llvm {
+	namespace SuperH {
+	  using namespace SH;
+	}
+}
+
+#define GET_INSTRUCTION_NAME
 #define PRINT_ALIAS_INSTR
-// #include "SuperHGenAsmWriter.inc"
+#include "SuperHGenAsmWriter.inc"
 
 SuperHInstPrinter::SuperHInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
                     const MCRegisterInfo &MRI) : MCInstPrinter(MAI, MII, MRI) {}
@@ -30,11 +39,6 @@ void SuperHInstPrinter::printRegName(raw_ostream &OS, MCRegister Reg) {
 	OS << StringRef(getRegisterName(Reg)).lower();
 }
 
-void SuperHInstPrinter::printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
-                 const MCSubtargetInfo &STI, raw_ostream &OS) {
-	printInstruction(MI, Address, OS);
-}
-
 void SuperHInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
 	const MCOperand &Op = MI->getOperand(OpNo);
 	
@@ -52,18 +56,7 @@ void SuperHInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, raw_ostrea
 	}
 }
 
-// TODO: Delete this placeholder.
-void SuperHInstPrinter::printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O) {
-
-}
-
-// TODO: Delete this placeholder.
-const char *SuperHInstPrinter::getRegisterName(MCRegister Reg) {
-	return "r";
-}
-
-std::pair<const char *, uint64_t> SuperHInstPrinter::getMnemonic(const MCInst &MI) const {
-	std::pair<const char *, uint64_t> Ret;
-
-	return Ret;
+void SuperHInstPrinter::printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
+                 const MCSubtargetInfo &STI, raw_ostream &OS) {
+	printInstruction(MI, Address, OS);
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
index d1b26b0e2f93d..ebdaca0d64915 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHInstPrinter.h
@@ -14,9 +14,8 @@
 #ifndef LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHINSTPRINTER_H
 #define LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHINSTPRINTER_H
 
+#include "SuperHMCTargetDesc.h"
 #include "llvm/MC/MCInstPrinter.h"
-#include <llvm/MC/MCExpr.h>
-#include <llvm/MC/MCInstrInfo.h>
 
 namespace llvm {
 
@@ -31,13 +30,20 @@ class SuperHInstPrinter : public MCInstPrinter {
                     const MCRegisterInfo &MRI);
 
   // Autogenerated by tablegen
+  std::pair<const char *, uint64_t>
+  getMnemonic(const MCInst &MI) const override;
   void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
+  void printCTILabel(const MCInst *MI, uint64_t Address, unsigned OpNum,
+                     const MCSubtargetInfo &STI, raw_ostream &O);
+  bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &OS);
+  void printCustomAliasOperand(const MCInst *MI, uint64_t Address,
+                               unsigned OpIdx, unsigned PrintMethodIdx,
+                               const MCSubtargetInfo &STI, raw_ostream &O);
   static const char *getRegisterName(MCRegister Reg);
 
   void printRegName(raw_ostream &OS, MCRegister Reg) override;
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
-  std::pair<const char *, uint64_t> getMnemonic(const MCInst &MI) const override;
 
 private:
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
index 323bb8e703ed8..7ee9d35209e9a 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
@@ -22,4 +22,5 @@ SuperHMCAsmInfo::SuperHMCAsmInfo(const Triple &TheTriple,
                                  const MCTargetOptions &Options)
     : MCAsmInfoELF(Options) {
   this->IsLittleEndian = TheTriple.isLittleEndian();
+  this->CommentString = ";";
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
index 4a8a31532c0e4..b99fb3387dbc5 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
@@ -31,6 +31,10 @@
 
 using namespace llvm;
 
+#define GET_INSTRINFO_MC_DESC
+#define ENABLE_INSTR_PREDICATE_VERIFIER
+#include "SuperHGenInstrInfo.inc"
+
 #define GET_REGINFO_MC_DESC
 #include "SuperHGenRegisterInfo.inc"
 
@@ -39,13 +43,13 @@ using namespace llvm;
 
 static MCInstrInfo *createSuperHMCInstrInfo() {
   MCInstrInfo *X = new MCInstrInfo();
-  // InitSuperHMCInstrInfo(X);
+  InitSuperHMCInstrInfo(X);
   return X;
 }
 
 static MCRegisterInfo *createSuperHMCRegisterInfo(const Triple &TT) {
   MCRegisterInfo *X = new MCRegisterInfo();
-  // InitSuperHRegisterInfo(X);
+  InitSuperHMCRegisterInfo(X, SH::R0);
   return X;
 }
 
@@ -71,11 +75,7 @@ static MCAsmInfo *createSuperHMCAsmInfo(const MCRegisterInfo &MRI,
 
 extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
 LLVMInitializeSuperHTargetMC() {
-
-  // SuperH (big-endian)
-  for (Target *T : {&getTheSuperHTarget()}) {
-    // Register the MC asm info.
-    TargetRegistry::RegisterMCAsmInfo(*T, createSuperHMCAsmInfo);
+  for (Target *T : {&getTheSuperHTarget(), &getTheSuperHLETarget()}) {
 
     // Register the MC instruction info.
     TargetRegistry::RegisterMCInstrInfo(*T, createSuperHMCInstrInfo);
@@ -85,26 +85,14 @@ LLVMInitializeSuperHTargetMC() {
 
     // Register the MC subtarget info.
     TargetRegistry::RegisterMCSubtargetInfo(*T, createSuperHMCSubtargetInfo);
-
-    // Register the MCInstPrinter.
-    TargetRegistry::RegisterMCInstPrinter(*T, createSuperHMCInstPrinter);
-  }
-
-  // SuperH (little-endian)
-  for (Target *T : {&getTheSuperHLETarget()}) {
+    
     // Register the MC asm info.
     TargetRegistry::RegisterMCAsmInfo(*T, createSuperHMCAsmInfo);
 
-    // Register the MC instruction info.
-    TargetRegistry::RegisterMCInstrInfo(*T, createSuperHMCInstrInfo);
-
-    // Register the MC register info.
-    TargetRegistry::RegisterMCRegInfo(*T, createSuperHMCRegisterInfo);
-
-    // Register the MC subtarget info.
-    TargetRegistry::RegisterMCSubtargetInfo(*T, createSuperHMCSubtargetInfo);
-
     // Register the MCInstPrinter.
     TargetRegistry::RegisterMCInstPrinter(*T, createSuperHMCInstPrinter);
+
+    // Register the AsmBackend
+    TargetRegistry::RegisterMCAsmBackend(*T, createSuperHAsmBackend);
   }
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
index 6bb8d438df3ff..cee382a0aeab6 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
@@ -18,12 +18,6 @@
 #include "llvm/MC/MCObjectWriter.h"
 #include "llvm/Support/DataTypes.h"
 
-#define GET_REGINFO_ENUM
-#include "SuperHGenRegisterInfo.inc"
-
-#define GET_SUBTARGETINFO_ENUM
-#include "SuperHGenSubtargetInfo.inc"
-
 namespace llvm {
 class MCAsmBackend;
 class MCCodeEmitter;
@@ -39,6 +33,22 @@ class StringRef;
 class raw_ostream;
 class raw_pwrite_stream;
 
+MCAsmBackend *createSuperHAsmBackend(const Target &T, const MCSubtargetInfo &STI,
+                                    const MCRegisterInfo &MRI,
+                                    const MCTargetOptions &Options);
+std::unique_ptr<MCObjectTargetWriter>
+createSuperHELFObjectWriter(uint8_t OSABI);
+
 }
 
+#define GET_REGINFO_ENUM
+#include "SuperHGenRegisterInfo.inc"
+
+#define GET_INSTRINFO_ENUM
+#define GET_INSTRINFO_MC_HELPER_DECLS
+#include "SuperHGenInstrInfo.inc"
+
+#define GET_SUBTARGETINFO_ENUM
+#include "SuperHGenSubtargetInfo.inc"
+
 #endif // LLVM_LIB_TARGET_SUPERH_MCTARGETDESC_SUPERHMCTARGETDESC_H
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperH.h b/llvm/lib/Target/SuperH/SuperH.h
new file mode 100644
index 0000000000000..931aeffd2c142
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperH.h
@@ -0,0 +1,19 @@
+//===-- SuperH.h - Top-level interface for SuperH representation *- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the entry points for global functions defined in the LLVM
+// SuperH back-end.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_SUPERH_H
+#define LLVM_LIB_TARGET_SUPERH_SUPERH_H
+
+#include "MCTargetDesc/SuperHMCTargetDesc.h"
+
+#endif
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperH.td b/llvm/lib/Target/SuperH/SuperH.td
index 8ec78f923ee4e..a035fbdaff85d 100644
--- a/llvm/lib/Target/SuperH/SuperH.td
+++ b/llvm/lib/Target/SuperH/SuperH.td
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 include "llvm/Target/Target.td"
+include "llvm/TableGen/SearchableTable.td"
 
 //===----------------------------------------------------------------------===//
 // SuperH Subtarget features
@@ -69,21 +70,24 @@ def FeatureSH4A     : SubtargetFeature<"sh4a", "SHArchVersion", "SH4A",
 
 include "SuperHSchedule.td"
 
-class Proc<string Name, list<SubtargetFeature> Features>
+class ProcModel<string Name, list<SubtargetFeature> Features>
     : ProcessorModel<Name, GenericSuperHModel, Features>;
 
-def : Proc<"generic", [FeatureSH4]>;
-def : Proc<"sh1", [FeatureSH1]>;
-def : Proc<"sh2", [FeatureSH2]>;
-def : Proc<"sh3", [FeatureSH3]>;
-def : Proc<"sh4", [FeatureSH4]>;
-def : Proc<"sh4a", [FeatureSH4A]>;
+def : ProcModel<"generic", [FeatureSH4]>;
+def : ProcModel<"sh1", [FeatureSH1]>;
+def : ProcModel<"sh2", [FeatureSH2]>;
+def : ProcModel<"sh2e", [FeatureSH2E]>;
+def : ProcModel<"sh2a", [FeatureSH2A]>;
+def : ProcModel<"sh3", [FeatureSH3]>;
+def : ProcModel<"sh3e", [FeatureSH3E]>;
+def : ProcModel<"sh4", [FeatureSH4]>;
+def : ProcModel<"sh4a", [FeatureSH4A]>;
 
 // NOTE:  SEGA got some special variants of the SuperH family
 //        produced with some extra features enabled, defining them here.
-def : Proc<"dreamcast", [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
-def : Proc<"naomi",     [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
-def : Proc<"saturn",    [FeatureSH2]>;
+def : ProcModel<"dreamcast", [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
+def : ProcModel<"naomi",     [FeatureSH4, FeatureFSCA, FeatureFSRRA]>;
+def : ProcModel<"saturn",    [FeatureSH2]>;
 
 //===----------------------------------------------------------------------===//
 // Register File Description
@@ -96,6 +100,9 @@ include "SuperHRegisterInfo.td"
 //===----------------------------------------------------------------------===//
 
 include "SuperHInstrInfo.td"
+
+defm : RemapAllTargetPseudoPointerOperands<sh_ptr_rc>;
+
 def SuperHInstrInfo : InstrInfo;
 
 //===----------------------------------------------------------------------===//
@@ -117,12 +124,15 @@ def SuperHAsmWriter : AsmWriter {
 //===---------------------------------------------------------------------===//
 
 def SuperHAsmParser : AsmParser {
-  let ShouldEmitMatchRegisterName = 0;
+  let ShouldEmitMatchRegisterName = 1;
   let ShouldEmitMatchRegisterAltName = 0;
 }
 
 def SuperHAsmParserVariant : AsmParserVariant {
-  int Variant = 0;
+  let Variant = 0;
+  let Name = "Hitachi";
+  let TokenizingCharacters = "[]*!#";
+  let CommentDelimiter = ";";
 }
 
 //===----------------------------------------------------------------------===//
@@ -132,5 +142,6 @@ def SuperHAsmParserVariant : AsmParserVariant {
 def SuperH : Target {
   let InstructionSet = SuperHInstrInfo;
   let AssemblyParsers = [SuperHAsmParser];
+  let AssemblyParserVariants = [SuperHAsmParserVariant];
   let AssemblyWriters = [SuperHAsmWriter];
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHFrameLowering.cpp b/llvm/lib/Target/SuperH/SuperHFrameLowering.cpp
new file mode 100644
index 0000000000000..ccd3340a45856
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHFrameLowering.cpp
@@ -0,0 +1,44 @@
+//===- SuperHFrameLowering.cpp - SuperH Frame Information ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperHTargetFrameLowering class.
+//
+//===----------------------------------------------------------------------===//
+
+
+#include "SuperHFrameLowering.h"
+#include "SuperHSubtarget.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/RegisterScavenging.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm;
+
+void SuperHFrameLowering::emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const {
+
+}
+
+void SuperHFrameLowering::emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
+
+}
+
+bool SuperHFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
+	return true;
+}
+
+MachineBasicBlock::iterator
+SuperHFrameLowering::eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
+                            MachineBasicBlock::iterator I) const {
+	return MBB.erase(I);
+}
+
+void SuperHFrameLowering::determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
+                        RegScavenger *RS) const {
+
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHFrameLowering.h b/llvm/lib/Target/SuperH/SuperHFrameLowering.h
new file mode 100644
index 0000000000000..97eece4398642
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHFrameLowering.h
@@ -0,0 +1,50 @@
+//===- SuperHFrameLowering.h - Define frame lowering for SuperH -*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperHTargetFrameLowering class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_SUPERHFRAMELOWERING_H
+#define LLVM_LIB_TARGET_SUPERH_SUPERHFRAMELOWERING_H
+
+#include "llvm/CodeGen/TargetFrameLowering.h"
+
+namespace llvm {
+
+class SuperHSubtarget;
+
+class SuperHFrameLowering : public TargetFrameLowering {
+protected:
+  const SuperHSubtarget &STI;
+
+public:
+  explicit SuperHFrameLowering(const SuperHSubtarget &STI)
+    : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
+                          /*StackAlignment*/Align(4),
+                          /*LocalAreaOffset*/0,
+                          /*TransAl*/Align(4)),
+      STI(STI) {}
+
+  void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
+  void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
+
+  bool hasReservedCallFrame(const MachineFunction &MF) const override;
+  MachineBasicBlock::iterator
+  eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
+                                MachineBasicBlock::iterator I) const override;
+
+  void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
+                            RegScavenger *RS) const override;
+};
+
+} // end namespace llvm
+
+
+#endif // end LLVM_LIB_TARGET_SUPERH_SUPERHFRAMELOWERING_H
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrData.td b/llvm/lib/Target/SuperH/SuperHInstrData.td
index d272437fff6b0..b66be593e634d 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrData.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrData.td
@@ -11,3 +11,46 @@
 /// ISA.
 ///
 //===----------------------------------------------------------------------===//
+
+
+//===----------------------------------------------------------------------===//
+// Instruction Class Templates
+//===----------------------------------------------------------------------===//
+
+let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
+
+// imm -> Sign Extension -> Rn
+class Move_ir<bits<16> op, string opcodestr>
+	: SHInstOP_N4_I8<op, (outs GPR:$n), (ins imm8:$imm),
+					 !strconcat(opcodestr, " $imm, $n")>;
+
+// Rm -> Rn
+class Move_rr<bits<16> op, string opcodestr>
+	: SHInstOP_N4_M4<op, (outs GPR:$n), (ins GPR:$m),
+					 !strconcat(opcodestr, " $m, $n")>;
+
+// Rm -> (Rn)
+class Move_rri<bits<16> op, string opcodestr>
+	: SHInstOP_N4_M4<op, (outs GPR:$n), (ins GPR:$m),
+					 !strconcat(opcodestr, " $m, $n")>;
+
+// (Rm) -> Sign Extension -> Rn
+class Move_rir<bits<16> op, string opcodestr>
+	: SHInstOP_N4_M4<op, (outs GPR:$n), (ins GPR:$m),
+					 !strconcat(opcodestr, " $m, $n")>;
+}
+
+//===----------------------------------------------------------------------===//
+// Instructions
+//===----------------------------------------------------------------------===//
+
+let Namespace = "SH" in {
+  def MOV_ir 		:  Move_ir<0b1110000000000000, "mov">;
+  def MOV_rr 		:  Move_rr<0b0110000000000011, "mov">;
+  def MOVB_rri	: Move_rri<0b0010000000000000, "mov.b">;
+  def MOVW_rri	: Move_rri<0b0010000000000001, "mov.w">;
+  def MOVL_rri	: Move_rri<0b0010000000000010, "mov.l">;
+  def MOVB_rir	: Move_rir<0b0110000000000000, "mov.b">;
+  def MOVW_rir	: Move_rir<0b0110000000000001, "mov.w">;
+  def MOVL_rir	: Move_rir<0b0110000000000010, "mov.l">;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrFormats.td b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
index de042add3c8c8..63f1d9cb76132 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrFormats.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
@@ -7,34 +7,205 @@
 //
 //===----------------------------------------------------------------------===//
 
-class SHInst<dag outs, dag ins, string asmstr, list<dag> pattern>
-    : Instruction {
+//===----------------------------------------------------------------------===//
+//
+//  The instruction formats in this file are defined based on the instruction
+//  notation defined in the SuperH 4a specification, table 3.3.
+//  
+//  The SuperH architecture uses a "compressed" instruction set where all
+//  instructions (except DSP instructions) are 16 bits wide, as such opcodes
+//  often embed the registers, displacement, etc. within the instruction opcode
+//  itself.
+//
+//  SuperH Instructions are generally split up in the following categories:
+//   * 0000nnnniiiiiiii (OP, Rn, imm)         OP_N4_I8
+//   * 0000nnnndddddddd (OP, Rn, disp)        OP_N4_D8
+//   * 0000nnnnmmmm0000 (OP, Rn, Rm, OP)      OP_N4_M4
+//   * 00000000nnnndddd (OP, Rn, disp)        OP_N4_D4
+//   * 00000000mmmmdddd (OP, Rm, disp)        OP_M4_D4
+//   * 0000nnnnmmmmdddd (OP, Rn, Rm, disp)    OP_N4_M4_D4
+//   * 00000000dddddddd (OP, disp)            OP_D8
+//   * 00000000iiiiiiii (OP, imm)             OP_I8
+//   * 0000nnnn00000000 (OP, Rn, OP)          OP_N4
+//   * 0000mmmm00000000 (OP, Rm, OP)          OP_M4
+//   * 0000000000000000 (OP)                  OP
+//
+//  These classes are simplified by giving them a naming scheme based on their
+//  encoding, eg. 0000nnnniiiiiiii is SHInstOP_N4_I8
+//
+//  TODO: Implement the DSP extensions into this scheme.
+//
+//===----------------------------------------------------------------------===//
+
+// Base of all 16-bit SuperH instructions
+class SHInst <dag outs, dag ins, string asmstr, list<dag> pattern> : Instruction {
+  field bits<16> Inst;
+  field bits<16> SoftFail = 0;
+  
+  bits<16> Opcode = 0;
 
   let Namespace = "SH";
   dag OutOperandList = outs;
   dag InOperandList = ins;
   let AsmString = asmstr;
   let Pattern = pattern;
+  let Size = 2;
+}
+
+// SuperH Psuedo Instruction
+class SHPseudo<dag outs, dag ins, string asmstr="; error: this should not be emitted", list<dag> pattern = []>
+    : SHInst<outs, ins, asmstr, pattern> {
+  let isPseudo = 1;
+  let isCodeGenOnly = 1;
+}
+
+//===----------------------------------------------------------------------===//
+//  Instruction Formats
+//===----------------------------------------------------------------------===//
+
+class SHInstOP_N4_I8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = Opcode{3-0};
+
+  // Operands
+  bits<4> n;
+  bits<8> imm;
+  let Inst{7-4} = n;
+  let Inst{15-8} = imm;
+}
+
+class SHInstOP_N4_D8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
   
-  field bits<16> Inst;
-  let Size = 1;
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = Opcode{3-0};
+
+  // Operands
+  bits<4> n;
+  bits<8> disp;
+  let Inst{7-4} = n;
+  let Inst{15-8} = disp;
 }
 
-class SHDSPInst<dag outs, dag ins, string asmstr, list<dag> pattern>
-    : Instruction {
+class SHInstOP_N4_M4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = Opcode{3-0};
+  let Inst{15-11} = Opcode{15-11};
 
-  let Namespace = "SH";
-  dag OutOperandList = outs;
-  dag InOperandList = ins;
-  let AsmString = asmstr;
-  let Pattern = pattern;
+  // Operands
+  bits<4> n;
+  bits<4> m;
+  let Inst{7-4} = n;
+  let Inst{11-8} = m;
+}
+
+class SHInstOP_N4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
   
-  field bits<32> Inst;
-  let Size = 2;
+  // Opcode
+  let Opcode = opcode;
+  let Inst{7-0} = Opcode{7-0};
+
+  // Operands
+  bits<4> n;
+  bits<4> disp;
+  let Inst{11-8} = n;
+  let Inst{15-12} = disp;
 }
 
-// SH PSEUDO INSTRUCTION
-class SHPseudo<dag outs, dag ins, list<dag> pattern = []>
-    : SHInst<outs, ins, "; error: this should not be emitted", pattern> {
-  let isPseudo = 1;
+class SHInstOP_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{7-0} = Opcode{7-0};
+
+  // Operands
+  bits<4> m;
+  bits<4> disp;
+  let Inst{11-8} = m;
+  let Inst{15-12} = disp;
+}
+
+class SHInstOP_N4_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = opcode{3-0};
+
+  // Operands
+  bits<4> n;
+  bits<4> m;
+  bits<4> disp;
+  let Inst{7-4} = n;
+  let Inst{11-8} = m;
+  let Inst{15-12} = disp;
+}
+
+class SHInstOP_D8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{7-0} = Opcode{7-0};
+
+  // Operands
+  bits<8> disp;
+  let Inst{15-8} = disp;
 }
+
+class SHInstOP_I8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{7-0} = Opcode{7-0};
+
+  // Operands
+  bits<8> imm;
+  let Inst{15-8} = imm;
+}
+
+class SHInstOP_N4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = Opcode{3-0};
+  let Inst{15-8} = Opcode{15-8};
+
+  // Operands
+  bits<4> n;
+  let Inst{7-4} = n;
+}
+
+class SHInstOP_M4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst{3-0} = Opcode{3-0};
+  let Inst{15-8} = Opcode{15-8};
+
+  // Operands
+  bits<4> m;
+  let Inst{7-4} = m;
+}
+
+class SHInstOP <bits<16> opcode, dag outs, dag ins, string asmstr> 
+  : SHInst<outs, ins, asmstr, []> {
+  
+  // Opcode
+  let Opcode = opcode;
+  let Inst = Opcode;
+}
+
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.cpp b/llvm/lib/Target/SuperH/SuperHInstrInfo.cpp
new file mode 100644
index 0000000000000..a4cc7447e2e48
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.cpp
@@ -0,0 +1,35 @@
+//===-- SuperHInstrInfo.cpp - SuperH Instruction Information --------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperH implementation of the TargetInstrInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuperHInstrInfo.h"
+#include "SuperHSubtarget.h"
+#include "SuperHTargetMachine.h"
+#include "SuperH.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "sh-instrinfo"
+
+#define GET_INSTRINFO_CTOR_DTOR
+#include "SuperHGenInstrInfo.inc"
+
+void SuperHInstrInfo::anchor() {}
+
+SuperHInstrInfo::SuperHInstrInfo(const SuperHSubtarget &ST)
+    : SuperHGenInstrInfo(ST, RI, SH::ADJCALLSTACKDOWN, SH::ADJCALLSTACKUP),
+      RI(ST), Subtarget(ST) { }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.h b/llvm/lib/Target/SuperH/SuperHInstrInfo.h
new file mode 100644
index 0000000000000..458bde9bfc64a
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.h
@@ -0,0 +1,35 @@
+//===-- SuperHInstrInfo.h - SuperH Instruction Information ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperH implementation of the TargetInstrInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_SUPERHINSTRINFO_H
+#define LLVM_LIB_TARGET_SUPERH_SUPERHINSTRINFO_H
+
+#include "SuperHRegisterInfo.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/TargetInstrInfo.h"
+
+#define GET_INSTRINFO_HEADER
+#include "SuperHGenInstrInfo.inc"
+
+namespace llvm {
+
+class SuperHInstrInfo : public SuperHGenInstrInfo {
+  const SuperHRegisterInfo RI;
+  const SuperHSubtarget &Subtarget;
+  virtual void anchor();
+public:
+  explicit SuperHInstrInfo(const SuperHSubtarget &STI);
+};
+}
+
+#endif // end LLVM_LIB_TARGET_SUPERH_SUPERHINSTRINFO_H
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.td b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
index f085dd1ec52bc..0b455ed4190f2 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
@@ -18,8 +18,26 @@
 ///
 //===----------------------------------------------------------------------===//
 
+//===----------------------------------------------------------------------===//
+// Instruction Formats
+//===----------------------------------------------------------------------===//
+
 include "SuperHInstrFormats.td"
 
+//===----------------------------------------------------------------------===//
+// Instruction Patterns
+//===----------------------------------------------------------------------===//
+
+defvar SH = DefaultMode;
+def sh_ptr_rc : RegClassByHwMode<[SH], [GPR]>;
+
+// Both cases can use the same decoder method, so avoid the dispatch
+// by hwmode by setting an explicit DecoderMethod
+def ptr_op : RegisterOperand<sh_ptr_rc> {
+  let DecoderMethod = "DecodeIntRegsRegisterClass";
+}
+
+
 //===----------------------------------------------------------------------===//
 // SuperH Type Profiles
 //===----------------------------------------------------------------------===//
@@ -50,9 +68,45 @@ def SHRet 		    : SDNode<"SHISD::RET", SHSDT_Ret,
 // Operands
 //===----------------------------------------------------------------------===//
 
+class ImmAsmOperand<int width> : AsmOperandClass {
+    let Name = "Imm" # width;
+    let RenderMethod = "addImmOperands";
+}
+
+def imm8 : Operand<i8>, ImmLeaf<i32, [{return isInt<8>(Imm);}]> {
+    let ParserMatchClass = ImmAsmOperand<8>;
+    let EncoderMethod = "getImm";
+    let DecoderMethod = "decodeImmOperand<8>";
+    let MCOperandPredicate = [{
+        int64_t Imm;
+        if (MCOp.evaluateAsConstantImm(Imm))
+            return isInt<8>(Imm);
+        return MCOp.isBareSymbolRef();
+    }];
+}
+
+//===----------------------------------------------------------------------===//
+// Basic Instructions
+//===----------------------------------------------------------------------===//
+
+// NOP instruction, does nothing.
+def NOP : SHInstOP<0b0000000000000000, (outs), (ins), "nop">;
 
 //===----------------------------------------------------------------------===//
 // Subsystems
 //===----------------------------------------------------------------------===//
 
-include "SuperHInstrData.td"
\ No newline at end of file
+include "SuperHInstrData.td"
+
+//===----------------------------------------------------------------------===//
+// Pseudo instructions
+//===----------------------------------------------------------------------===//
+
+let Defs = [R0], Uses = [R0] in {
+def ADJCALLSTACKDOWN : SHPseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
+                               "!ADJCALLSTACKDOWN $amt1, $amt2",
+                               [(SHCallSeqStart timm:$amt1, timm:$amt2)]>;
+def ADJCALLSTACKUP : SHPseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
+                            "!ADJCALLSTACKUP $amt1",
+                            [(SHCallSeqEnd timm:$amt1, timm:$amt2)]>;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.cpp b/llvm/lib/Target/SuperH/SuperHRegisterInfo.cpp
new file mode 100644
index 0000000000000..32bdf20b8dd95
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.cpp
@@ -0,0 +1,78 @@
+//===-- SuperHRegisterInfo.h - SuperH Register Information ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperH implementation of the TargetRegisterInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuperHRegisterInfo.h"
+#include "SuperHFrameLowering.h"
+#include "SuperHSubtarget.h"
+#include "SuperH.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "sh-reginfo"
+
+#define GET_REGINFO_TARGET_DESC
+#include "SuperHGenRegisterInfo.inc"
+
+
+
+SuperHRegisterInfo::SuperHRegisterInfo(const SuperHSubtarget &ST)
+  : SuperHGenRegisterInfo(SH::R0, /*DwarfFlavour*/0, /*EHFlavor*/0,
+                         /*PC*/SH::PC), Subtarget(ST) {}
+
+const TargetRegisterClass *SuperHRegisterInfo::intRegClass(unsigned Size) const {
+  return &SH::GPRRegClass;
+}
+
+BitVector SuperHRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
+  BitVector Reserved(getNumRegs());
+
+  return Reserved;
+}
+
+bool SuperHRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
+                                           int SPAdj,
+                                           unsigned FIOperandNum,
+                                           RegScavenger *RS) const {
+  llvm_unreachable("Unsupported eliminateFrameIndex");
+  return true;
+}
+
+bool
+SuperHRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
+  return true;
+}
+
+bool
+SuperHRegisterInfo::requiresFrameIndexScavenging(
+                                            const MachineFunction &MF) const {
+  return true;
+}
+
+bool
+SuperHRegisterInfo::requiresFrameIndexReplacementScavenging(
+                                            const MachineFunction &MF) const {
+  return true;
+}
+
+bool
+SuperHRegisterInfo::trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
+  return true;
+}
+
+Register SuperHRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
+  llvm_unreachable("Unsupported getFrameRegister");
+}
+const MCPhysReg *SuperHRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
+	return nullptr;
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.h b/llvm/lib/Target/SuperH/SuperHRegisterInfo.h
new file mode 100644
index 0000000000000..ed3200f7b0101
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.h
@@ -0,0 +1,53 @@
+//===-- SuperHRegisterInfo.h - SuperH Register Information ------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the SuperH implementation of the TargetRegisterInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_SUPERHREGISTERINFO_H
+#define LLVM_LIB_TARGET_SUPERH_SUPERHREGISTERINFO_H
+
+#include "llvm/CodeGen/TargetRegisterInfo.h"
+
+#define GET_REGINFO_HEADER
+#include "SuperHGenRegisterInfo.inc"
+
+namespace llvm {
+class SuperHSubtarget;
+
+class SuperHRegisterInfo : public SuperHGenRegisterInfo {
+protected:
+  const SuperHSubtarget &Subtarget;
+
+public:
+  SuperHRegisterInfo(const SuperHSubtarget &Subtarget);
+
+  const MCPhysReg *getCalleeSavedRegs(const MachineFunction *MF) const override;
+  BitVector getReservedRegs(const MachineFunction &MF) const override;
+
+  bool requiresRegisterScavenging(const MachineFunction &MF) const override;
+  bool requiresFrameIndexScavenging(const MachineFunction &MF) const override;
+  bool requiresFrameIndexReplacementScavenging(
+                                    const MachineFunction &MF) const override;
+
+  bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const override;
+
+  bool eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
+                           unsigned FIOperandNum,
+                           RegScavenger *RS = nullptr) const override;
+
+  Register getFrameRegister(const MachineFunction &MF) const override;
+
+  const TargetRegisterClass *intRegClass(unsigned Size) const;
+};
+
+} // end namespace llvm
+
+#endif // end LLVM_LIB_TARGET_SUPERH_SUPERHREGISTERINFO_H
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
index d8767915dfb45..fc4c05dadb994 100644
--- a/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHRegisterInfo.td
@@ -144,7 +144,7 @@ let Namespace = "SH" in {
   
   // 512-bit matrix register (shadows XF registers)
   // TODO: Add proper subregisters here.
-  def MtrxReg : MTRXReg<0, "xmtrx">;
+  def XMTRX : MTRXReg<0, "xmtrx">;
 }
 
 //===----------------------------------------------------------------------===//
@@ -180,7 +180,7 @@ def FR64 : RegisterClass<"SH", [f64], 64, (add
 def VEC128 : RegisterClass<"SH", [v4f32], 128, (add FV0, FV4, FV8, FV12)>;
 
 // Matrix register
-def MTRX512 : RegisterClass<"SH", [v16f32], 512, (add (sequence "FR%u", 0, 15), (sequence "XF%u", 0, 15))>;
+def XMTRX512 : RegisterClass<"SH", [v16f32], 512, (add (sequence "FR%u", 0, 15), (sequence "XF%u", 0, 15))>;
 
 // Control registers
 def CTRL : RegisterClass<"SH", [ i32 ], 32, 
diff --git a/llvm/lib/Target/SuperH/SuperHSubtarget.cpp b/llvm/lib/Target/SuperH/SuperHSubtarget.cpp
new file mode 100644
index 0000000000000..1f831a528a15a
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHSubtarget.cpp
@@ -0,0 +1,22 @@
+//===-- SuperHSubtarget.h - Define Subtarget for SuperH ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the SuperH specific subclass of TargetSubtargetInfo.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SuperHSubtarget.h"
+#include "MCTargetDesc/SuperHMCTargetDesc.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "sh-subtarget"
+
+#define GET_SUBTARGETINFO_TARGET_DESC
+#define GET_SUBTARGETINFO_CTOR
+#include "SuperHGenSubtargetInfo.inc"
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/SuperHSubtarget.h b/llvm/lib/Target/SuperH/SuperHSubtarget.h
new file mode 100644
index 0000000000000..b5c38b7cb7ba4
--- /dev/null
+++ b/llvm/lib/Target/SuperH/SuperHSubtarget.h
@@ -0,0 +1,49 @@
+//===-- SuperHSubtarget.h - Define Subtarget for SuperH ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the SuperH specific subclass of TargetSubtargetInfo.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_SUPERH_SUPERHSUBTARGET_H
+#define LLVM_LIB_TARGET_SUPERH_SUPERHSUBTARGET_H
+
+#define GET_SUBTARGETINFO_HEADER
+#include "SuperHGenSubtargetInfo.inc"
+
+
+namespace llvm {
+class SuperHSubtarget : public SuperHGenSubtargetInfo {
+  enum SuperHArchEnum { 
+    SHDefault,
+    SH1, 
+    SH2, SH2A, SH2E, 
+    SH3, SH3E, 
+    SH4, SH4A
+  };
+
+  SuperHArchEnum SHArchVersion;
+
+#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER)                    \
+  bool ATTRIBUTE = DEFAULT;
+#include "SuperHGenSubtargetInfo.inc"
+
+#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER)                    \
+  bool GETTER() const { return ATTRIBUTE; }
+#include "SuperHGenSubtargetInfo.inc"
+
+public:
+
+  /// ParseSubtargetFeatures - Parses features string setting specified
+  /// subtarget options.  Definition of function is auto generated by tblgen.
+  void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
+};
+
+} // end namespace llvm
+
+#endif // end LLVM_LIB_TARGET_SUPERH_SUPERHSUBTARGET_H
\ No newline at end of file

>From 4a6aad64a0aea5bbdb4feffce35752f844fd2830 Mon Sep 17 00:00:00 2001
From: LunaTheFoxgirl <luna at foxgirls.gay>
Date: Wed, 3 Jun 2026 10:23:34 +0200
Subject: [PATCH 7/7] Add MCCodeEmitter

---
 llvm/lib/Target/SuperH/CMakeLists.txt         |   2 +-
 .../Target/SuperH/MCTargetDesc/CMakeLists.txt |   1 +
 .../SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp   |   1 +
 .../MCTargetDesc/SuperHMCCodeEmitter.cpp      | 101 ++++++++++++++++++
 .../MCTargetDesc/SuperHMCTargetDesc.cpp       |   3 +
 .../SuperH/MCTargetDesc/SuperHMCTargetDesc.h  |   6 +-
 llvm/lib/Target/SuperH/SuperHInstrFormats.td  |  52 ++++-----
 llvm/lib/Target/SuperH/SuperHInstrInfo.td     |   2 -
 llvm/lib/TargetParser/Triple.cpp              |   4 +
 9 files changed, 141 insertions(+), 31 deletions(-)
 create mode 100644 llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCCodeEmitter.cpp

diff --git a/llvm/lib/Target/SuperH/CMakeLists.txt b/llvm/lib/Target/SuperH/CMakeLists.txt
index df685cc0dbddb..f0d4a594242a9 100644
--- a/llvm/lib/Target/SuperH/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/CMakeLists.txt
@@ -9,7 +9,7 @@ tablegen(LLVM SuperHGenDAGISel.inc            -gen-dag-isel)
 tablegen(LLVM SuperHGenDisassemblerTables.inc -gen-disassembler)
 tablegen(LLVM SuperHGenRegisterInfo.inc       -gen-register-info)
 tablegen(LLVM SuperHGenInstrInfo.inc          -gen-instr-info)
-#tablegen(LLVM SuperHGenMCCodeEmitter.inc      -gen-emitter)
+tablegen(LLVM SuperHGenMCCodeEmitter.inc      -gen-emitter)
 tablegen(LLVM SuperHGenSDNodeInfo.inc         -gen-sd-node-info -sdnode-namespace=SHISD)
 tablegen(LLVM SuperHGenSearchableTables.inc   -gen-searchable-tables)
 tablegen(LLVM SuperHGenSubtargetInfo.inc      -gen-subtarget)
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
index a612d81272d0e..c0bbc3c43df5b 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_llvm_component_library(LLVMSuperHDesc
+  SuperHMCCodeEmitter.cpp
   SuperHMCTargetDesc.cpp
   SuperHMCAsmInfo.cpp
   SuperHInstPrinter.cpp
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
index 7ee9d35209e9a..a98647bc36f26 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCAsmInfo.cpp
@@ -23,4 +23,5 @@ SuperHMCAsmInfo::SuperHMCAsmInfo(const Triple &TheTriple,
     : MCAsmInfoELF(Options) {
   this->IsLittleEndian = TheTriple.isLittleEndian();
   this->CommentString = ";";
+  this->SeparatorString = "\n";
 }
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCCodeEmitter.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCCodeEmitter.cpp
new file mode 100644
index 0000000000000..380247543dff6
--- /dev/null
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCCodeEmitter.cpp
@@ -0,0 +1,101 @@
+//===-- SuperHGenMCCodeEmitter.cpp - Convert SuperH code to machine code --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the SuperHMCCodeEmitter class.
+//
+//===----------------------------------------------------------------------===//
+
+
+#include "SuperHMCTargetDesc.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/bit.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCFixup.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCObjectFileInfo.h"
+#include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/EndianStream.h"
+
+
+using namespace llvm;
+
+#define DEBUG_TYPE "mccodeemitter"
+
+STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
+
+namespace {
+
+class SuperHMCCodeEmitter : public MCCodeEmitter {
+  MCContext &Ctx;
+
+public:
+  SuperHMCCodeEmitter(const MCInstrInfo &, MCContext &ctx)
+    : Ctx(ctx) {}
+  SuperHMCCodeEmitter(const SuperHMCCodeEmitter &) = delete;
+  SuperHMCCodeEmitter &operator=(const SuperHMCCodeEmitter &) = delete;
+  ~SuperHMCCodeEmitter() override = default;
+
+  void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
+                         SmallVectorImpl<MCFixup> &Fixups,
+                         const MCSubtargetInfo &STI) const override;
+
+  // getBinaryCodeForInstr - TableGen'erated function for getting the
+  // binary encoding for an instruction.
+  uint64_t getBinaryCodeForInstr(const MCInst &MI,
+                                 SmallVectorImpl<MCFixup> &Fixups,
+                                 const MCSubtargetInfo &STI) const;
+
+  /// getMachineOpValue - Return binary encoding of operand. If the machine
+  /// operand requires relocation, record the relocation and return zero.
+  unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
+                             SmallVectorImpl<MCFixup> &Fixups,
+                             const MCSubtargetInfo &STI) const;
+};
+
+} // end namespace
+
+#include "SuperHGenMCCodeEmitter.inc"
+
+void SuperHMCCodeEmitter::encodeInstruction(const MCInst &MI,
+                                           SmallVectorImpl<char> &CB,
+                                           SmallVectorImpl<MCFixup> &Fixups,
+                                           const MCSubtargetInfo &STI) const {
+  
+  // All base instructions are 16-bit in SuperH asm
+  uint16_t Bits = (uint16_t)getBinaryCodeForInstr(MI, Fixups, STI);
+  support::endian::write(CB, Bits, Ctx.getAsmInfo().isLittleEndian()
+                                      ? llvm::endianness::little
+                                      : llvm::endianness::big);
+
+  ++MCNumEmitted;
+}
+
+unsigned SuperHMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
+                             SmallVectorImpl<MCFixup> &Fixups,
+                             const MCSubtargetInfo &STI) const {
+  if (MO.isReg())
+      return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
+
+  if (MO.isImm())
+    return MO.getImm();
+
+  return 0;
+}
+
+MCCodeEmitter *llvm::createSuperHMCCodeEmitter(const MCInstrInfo &MCII,
+                                              MCContext &Ctx) {
+  return new SuperHMCCodeEmitter(MCII, Ctx);
+}
\ No newline at end of file
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
index b99fb3387dbc5..15f6cc8195b54 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.cpp
@@ -92,6 +92,9 @@ LLVMInitializeSuperHTargetMC() {
     // Register the MCInstPrinter.
     TargetRegistry::RegisterMCInstPrinter(*T, createSuperHMCInstPrinter);
 
+    // Register the MCCodeEmitter.
+    TargetRegistry::RegisterMCCodeEmitter(*T, createSuperHMCCodeEmitter);
+
     // Register the AsmBackend
     TargetRegistry::RegisterMCAsmBackend(*T, createSuperHAsmBackend);
   }
diff --git a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
index cee382a0aeab6..5d060e597c149 100644
--- a/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
+++ b/llvm/lib/Target/SuperH/MCTargetDesc/SuperHMCTargetDesc.h
@@ -33,12 +33,14 @@ class StringRef;
 class raw_ostream;
 class raw_pwrite_stream;
 
+MCCodeEmitter *createSuperHMCCodeEmitter(const MCInstrInfo &MCII,
+                                              MCContext &Ctx);
+
 MCAsmBackend *createSuperHAsmBackend(const Target &T, const MCSubtargetInfo &STI,
                                     const MCRegisterInfo &MRI,
                                     const MCTargetOptions &Options);
-std::unique_ptr<MCObjectTargetWriter>
-createSuperHELFObjectWriter(uint8_t OSABI);
 
+std::unique_ptr<MCObjectTargetWriter> createSuperHELFObjectWriter(uint8_t OSABI);
 }
 
 #define GET_REGINFO_ENUM
diff --git a/llvm/lib/Target/SuperH/SuperHInstrFormats.td b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
index 63f1d9cb76132..fbe8b2f0fd501 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrFormats.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrFormats.td
@@ -68,13 +68,13 @@ class SHInstOP_N4_I8 <bits<16> opcode, dag outs, dag ins, string asmstr>
 
   // Opcode
   let Opcode = opcode;
-  let Inst{3-0} = Opcode{3-0};
+  let Inst{15-12} = Opcode{15-12};
 
   // Operands
   bits<4> n;
   bits<8> imm;
-  let Inst{7-4} = n;
-  let Inst{15-8} = imm;
+  let Inst{11-8} = n;
+  let Inst{7-0} = imm;
 }
 
 class SHInstOP_N4_D8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -83,13 +83,13 @@ class SHInstOP_N4_D8 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{3-0} = Opcode{3-0};
+  let Inst{15-12} = Opcode{15-12};
 
   // Operands
   bits<4> n;
   bits<8> disp;
-  let Inst{7-4} = n;
-  let Inst{15-8} = disp;
+  let Inst{11-8} = n;
+  let Inst{7-0} = disp;
 }
 
 class SHInstOP_N4_M4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -97,14 +97,14 @@ class SHInstOP_N4_M4 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{3-0} = Opcode{3-0};
   let Inst{15-11} = Opcode{15-11};
+  let Inst{3-0} = Opcode{3-0};
 
   // Operands
   bits<4> n;
   bits<4> m;
-  let Inst{7-4} = n;
-  let Inst{11-8} = m;
+  let Inst{11-8} = n;
+  let Inst{7-4} = m;
 }
 
 class SHInstOP_N4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -112,13 +112,13 @@ class SHInstOP_N4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{7-0} = Opcode{7-0};
+  let Inst{15-8} = Opcode{15-8};
 
   // Operands
   bits<4> n;
   bits<4> disp;
-  let Inst{11-8} = n;
-  let Inst{15-12} = disp;
+  let Inst{11-8} = disp;
+  let Inst{15-12} = n;
 }
 
 class SHInstOP_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -126,13 +126,13 @@ class SHInstOP_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{7-0} = Opcode{7-0};
+  let Inst{15-8} = Opcode{15-8};
 
   // Operands
   bits<4> m;
   bits<4> disp;
-  let Inst{11-8} = m;
-  let Inst{15-12} = disp;
+  let Inst{11-8} = disp;
+  let Inst{15-12} = m;
 }
 
 class SHInstOP_N4_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -140,15 +140,15 @@ class SHInstOP_N4_M4_D4 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{3-0} = opcode{3-0};
+  let Inst{15-12} = Opcode{15-12};
 
   // Operands
   bits<4> n;
   bits<4> m;
   bits<4> disp;
-  let Inst{7-4} = n;
-  let Inst{11-8} = m;
-  let Inst{15-12} = disp;
+  let Inst{11-8} = n;
+  let Inst{7-4} = m;
+  let Inst{0-3} = disp;
 }
 
 class SHInstOP_D8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -156,11 +156,11 @@ class SHInstOP_D8 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{7-0} = Opcode{7-0};
+  let Inst{15-8} = Opcode{15-8};
 
   // Operands
   bits<8> disp;
-  let Inst{15-8} = disp;
+  let Inst{7-0} = disp;
 }
 
 class SHInstOP_I8 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -168,11 +168,11 @@ class SHInstOP_I8 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{7-0} = Opcode{7-0};
+  let Inst{15-8} = Opcode{15-8};
 
   // Operands
   bits<8> imm;
-  let Inst{15-8} = imm;
+  let Inst{7-0} = imm;
 }
 
 class SHInstOP_N4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -180,12 +180,12 @@ class SHInstOP_N4 <bits<16> opcode, dag outs, dag ins, string asmstr>
   
   // Opcode
   let Opcode = opcode;
-  let Inst{3-0} = Opcode{3-0};
   let Inst{15-8} = Opcode{15-8};
+  let Inst{3-0} = Opcode{3-0};
 
   // Operands
   bits<4> n;
-  let Inst{7-4} = n;
+  let Inst{11-8} = n;
 }
 
 class SHInstOP_M4 <bits<16> opcode, dag outs, dag ins, string asmstr> 
@@ -198,7 +198,7 @@ class SHInstOP_M4 <bits<16> opcode, dag outs, dag ins, string asmstr>
 
   // Operands
   bits<4> m;
-  let Inst{7-4} = m;
+  let Inst{11-8} = m;
 }
 
 class SHInstOP <bits<16> opcode, dag outs, dag ins, string asmstr> 
diff --git a/llvm/lib/Target/SuperH/SuperHInstrInfo.td b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
index 0b455ed4190f2..e137158b352d5 100644
--- a/llvm/lib/Target/SuperH/SuperHInstrInfo.td
+++ b/llvm/lib/Target/SuperH/SuperHInstrInfo.td
@@ -75,8 +75,6 @@ class ImmAsmOperand<int width> : AsmOperandClass {
 
 def imm8 : Operand<i8>, ImmLeaf<i32, [{return isInt<8>(Imm);}]> {
     let ParserMatchClass = ImmAsmOperand<8>;
-    let EncoderMethod = "getImm";
-    let DecoderMethod = "decodeImmOperand<8>";
     let MCOperandPredicate = [{
         int64_t Imm;
         if (MCOp.evaluateAsConstantImm(Imm))
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 1e9d675ff5162..3e62300a726de 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -692,6 +692,8 @@ Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
       .Case("riscv32be", riscv32be)
       .Case("riscv64be", riscv64be)
       .Case("hexagon", hexagon)
+      .Case("sh", sh)
+      .Case("sh_le", sh_le)
       .Case("sparc", sparc)
       .Case("sparcel", sparcel)
       .Case("sparcv9", sparcv9)
@@ -845,6 +847,8 @@ Triple::ArchType Triple::parseArch(StringRef ArchName) {
           .Case("riscv64be", Triple::riscv64be)
           .Case("hexagon", Triple::hexagon)
           .Cases({"s390x", "systemz"}, Triple::systemz)
+          .Case("sh", Triple::sh)
+          .Case("sh_le", Triple::sh_le)
           .Case("sparc", Triple::sparc)
           .Case("sparcel", Triple::sparcel)
           .Cases({"sparcv9", "sparc64"}, Triple::sparcv9)



More information about the llvm-commits mailing list