From 0ad0d305ffdebe15127a5c766ce5d9978ce1fe0f Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:43:20 +0000
Subject: [PATCH 01/12] bootstrap: Add cputype matching for m68k

---
 src/bootstrap/bootstrap.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py
index 1f1eca1c76cf1..57ade88f73306 100644
--- a/src/bootstrap/bootstrap.py
+++ b/src/bootstrap/bootstrap.py
@@ -277,6 +277,7 @@ def default_build_triple(verbose):
         'i486': 'i686',
         'i686': 'i686',
         'i786': 'i686',
+        'm68k': 'm68k',
         'powerpc': 'powerpc',
         'powerpc64': 'powerpc64',
         'powerpc64le': 'powerpc64le',

From df7ad3af210efbb42239780c62176eeaa73dcb5b Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:43:42 +0000
Subject: [PATCH 02/12] bootstrap: Add LLVM target matching for M68k

---
 config.toml.example     | 2 +-
 src/bootstrap/native.rs | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/config.toml.example b/config.toml.example
index c2d51c140b4ef..aff4e8fa82ac1 100644
--- a/config.toml.example
+++ b/config.toml.example
@@ -103,7 +103,7 @@ changelog-seen = 2
 # the same format as above, but since these targets are experimental, they are
 # not built by default and the experimental Rust compilation targets that depend
 # on them will not work unless the user opts in to building them.
-#experimental-targets = "AVR"
+#experimental-targets = "AVR;M68k"
 
 # Cap the number of parallel linker invocations when compiling LLVM.
 # This can be useful when building LLVM with debug info, which significantly
diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs
index 0e306cb7211b5..27c9bb2504f6d 100644
--- a/src/bootstrap/native.rs
+++ b/src/bootstrap/native.rs
@@ -165,7 +165,7 @@ impl Step for Llvm {
 
         let llvm_exp_targets = match builder.config.llvm_experimental_targets {
             Some(ref s) => s,
-            None => "AVR",
+            None => "AVR;M68k",
         };
 
         let assertions = if builder.config.llvm_assertions { "ON" } else { "OFF" };

From 580559129b875137e80e066bf2c87673aed9a582 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:45:06 +0000
Subject: [PATCH 03/12] compiler/rustc_target: Add support for m68k-linux-gnu

---
 compiler/rustc_target/src/abi/call/m68k.rs    | 30 +++++++++++++++++++
 compiler/rustc_target/src/abi/call/mod.rs     |  2 ++
 .../src/spec/m68k_unknown_linux_gnu.rs        | 15 ++++++++++
 compiler/rustc_target/src/spec/mod.rs         |  1 +
 4 files changed, 48 insertions(+)
 create mode 100644 compiler/rustc_target/src/abi/call/m68k.rs
 create mode 100644 compiler/rustc_target/src/spec/m68k_unknown_linux_gnu.rs

diff --git a/compiler/rustc_target/src/abi/call/m68k.rs b/compiler/rustc_target/src/abi/call/m68k.rs
new file mode 100644
index 0000000000000..58fdc00b69627
--- /dev/null
+++ b/compiler/rustc_target/src/abi/call/m68k.rs
@@ -0,0 +1,30 @@
+use crate::abi::call::{ArgAbi, FnAbi};
+
+fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
+    if ret.layout.is_aggregate() {
+        ret.make_indirect();
+    } else {
+        ret.extend_integer_width_to(32);
+    }
+}
+
+fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
+    if arg.layout.is_aggregate() {
+        arg.make_indirect_byval();
+    } else {
+        arg.extend_integer_width_to(32);
+    }
+}
+
+pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
+    if !fn_abi.ret.is_ignore() {
+        classify_ret(&mut fn_abi.ret);
+    }
+
+    for arg in &mut fn_abi.args {
+        if arg.is_ignore() {
+            continue;
+        }
+        classify_arg(arg);
+    }
+}
diff --git a/compiler/rustc_target/src/abi/call/mod.rs b/compiler/rustc_target/src/abi/call/mod.rs
index 927bebd8157a6..4f52535aa5073 100644
--- a/compiler/rustc_target/src/abi/call/mod.rs
+++ b/compiler/rustc_target/src/abi/call/mod.rs
@@ -8,6 +8,7 @@ mod arm;
 mod avr;
 mod bpf;
 mod hexagon;
+mod m68k;
 mod mips;
 mod mips64;
 mod msp430;
@@ -634,6 +635,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
             "amdgpu" => amdgpu::compute_abi_info(cx, self),
             "arm" => arm::compute_abi_info(cx, self),
             "avr" => avr::compute_abi_info(self),
+            "m68k" => m68k::compute_abi_info(self),
             "mips" => mips::compute_abi_info(cx, self),
             "mips64" => mips64::compute_abi_info(cx, self),
             "powerpc" => powerpc::compute_abi_info(self),
diff --git a/compiler/rustc_target/src/spec/m68k_unknown_linux_gnu.rs b/compiler/rustc_target/src/spec/m68k_unknown_linux_gnu.rs
new file mode 100644
index 0000000000000..fff7b25a34937
--- /dev/null
+++ b/compiler/rustc_target/src/spec/m68k_unknown_linux_gnu.rs
@@ -0,0 +1,15 @@
+use crate::abi::Endian;
+use crate::spec::{Target, TargetOptions};
+
+pub fn target() -> Target {
+    let mut base = super::linux_base::opts();
+    base.max_atomic_width = Some(32);
+
+    Target {
+        llvm_target: "m68k-unknown-linux-gnu".to_string(),
+        pointer_width: 32,
+        data_layout: "E-m:e-p:32:16:32-i8:8:8-i16:16:16-i32:16:32-n8:16:32-a:0:16-S16".to_string(),
+        arch: "m68k".to_string(),
+        options: TargetOptions { endian: Endian::Big, mcount: "_mcount".to_string(), ..base },
+    }
+}
diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs
index 920a929544abb..c947721d63d39 100644
--- a/compiler/rustc_target/src/spec/mod.rs
+++ b/compiler/rustc_target/src/spec/mod.rs
@@ -742,6 +742,7 @@ supported_targets! {
     ("x86_64-unknown-linux-gnux32", x86_64_unknown_linux_gnux32),
     ("i686-unknown-linux-gnu", i686_unknown_linux_gnu),
     ("i586-unknown-linux-gnu", i586_unknown_linux_gnu),
+    ("m68k-unknown-linux-gnu", m68k_unknown_linux_gnu),
     ("mips-unknown-linux-gnu", mips_unknown_linux_gnu),
     ("mips64-unknown-linux-gnuabi64", mips64_unknown_linux_gnuabi64),
     ("mips64el-unknown-linux-gnuabi64", mips64el_unknown_linux_gnuabi64),

From 9d177f89262ff2b50699bd31a5fb14e92cba7085 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:45:58 +0000
Subject: [PATCH 04/12] build-manifest: Add m68k-unknown-linux-gnu target

---
 src/tools/build-manifest/src/main.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs
index ab63a9e2dfa0b..c1579ae9ac54a 100644
--- a/src/tools/build-manifest/src/main.rs
+++ b/src/tools/build-manifest/src/main.rs
@@ -99,6 +99,7 @@ static TARGETS: &[&str] = &[
     "i686-unknown-freebsd",
     "i686-unknown-linux-gnu",
     "i686-unknown-linux-musl",
+    "m68k-unknown-linux-gnu",
     "mips-unknown-linux-gnu",
     "mips-unknown-linux-musl",
     "mips64-unknown-linux-gnuabi64",

From 33088b9395ad6d29670f7ffbd0a48cd0af0939e2 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:46:46 +0000
Subject: [PATCH 05/12] ci: Add Dockerfile for dist-m68k-linux

---
 .../disabled/dist-m68k-linux/Dockerfile       | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 src/ci/docker/host-x86_64/disabled/dist-m68k-linux/Dockerfile

diff --git a/src/ci/docker/host-x86_64/disabled/dist-m68k-linux/Dockerfile b/src/ci/docker/host-x86_64/disabled/dist-m68k-linux/Dockerfile
new file mode 100644
index 0000000000000..17203994cdff0
--- /dev/null
+++ b/src/ci/docker/host-x86_64/disabled/dist-m68k-linux/Dockerfile
@@ -0,0 +1,26 @@
+FROM ubuntu:20.04
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+  g++ \
+  make \
+  file \
+  curl \
+  ca-certificates \
+  python2.7 \
+  git \
+  cmake \
+  sudo \
+  gdb \
+  xz-utils \
+  g++-m68k-linux-gnu \
+  libssl-dev \
+  pkg-config
+
+
+COPY scripts/sccache.sh /scripts/
+RUN sh /scripts/sccache.sh
+
+ENV HOSTS=m68k-unknown-linux-gnu
+
+ENV RUST_CONFIGURE_ARGS --host=$HOSTS --enable-extended
+ENV SCRIPT python3 ../x.py dist --host $HOSTS --target $HOSTS

From 0e4e0ce8844c81e0e5da97920f3e7b9a8823a896 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:47:23 +0000
Subject: [PATCH 06/12] compiletest: Add m68k to ARCH_TABLE

---
 src/tools/compiletest/src/util.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/tools/compiletest/src/util.rs b/src/tools/compiletest/src/util.rs
index 37164c4e5752a..75d392951eca7 100644
--- a/src/tools/compiletest/src/util.rs
+++ b/src/tools/compiletest/src/util.rs
@@ -54,6 +54,7 @@ const ARCH_TABLE: &[(&str, &str)] = &[
     ("i386", "x86"),
     ("i586", "x86"),
     ("i686", "x86"),
+    ("m68k", "m68k"),
     ("mips", "mips"),
     ("mips64", "mips64"),
     ("mips64el", "mips64"),

From 13b029d6fc909cb4e226dac83278ceb5ccdd976d Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:47:50 +0000
Subject: [PATCH 07/12] rustdoc: Add human-readable cfg string for m68k

---
 src/librustdoc/clean/cfg.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/librustdoc/clean/cfg.rs b/src/librustdoc/clean/cfg.rs
index 592aefb6a4b01..9b5ca06848646 100644
--- a/src/librustdoc/clean/cfg.rs
+++ b/src/librustdoc/clean/cfg.rs
@@ -491,6 +491,7 @@ impl<'a> fmt::Display for Display<'a> {
                         "aarch64" => "AArch64",
                         "arm" => "ARM",
                         "asmjs" => "JavaScript",
+                        "m68k" => "M68k",
                         "mips" => "MIPS",
                         "mips64" => "MIPS-64",
                         "msp430" => "MSP430",

From 5e56778dc8b13973dde9345e23db085eeb7bbfe9 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:48:23 +0000
Subject: [PATCH 08/12] libstd: Add m68k for raw type definitions on Linux

---
 library/std/src/os/linux/raw.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/library/std/src/os/linux/raw.rs b/library/std/src/os/linux/raw.rs
index 5b68a7e126268..cd92dcabdf520 100644
--- a/library/std/src/os/linux/raw.rs
+++ b/library/std/src/os/linux/raw.rs
@@ -27,6 +27,7 @@ pub use self::arch::{blkcnt_t, blksize_t, ino_t, nlink_t, off_t, stat, time_t};
 #[cfg(any(
     target_arch = "x86",
     target_arch = "le32",
+    target_arch = "m68k",
     target_arch = "powerpc",
     target_arch = "sparc",
     target_arch = "arm",

From fa27d50caa5b0972803b957212a813b582a6c013 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:49:17 +0000
Subject: [PATCH 09/12] compiler/rustc_llvm: Enable M68k LLVM target

---
 compiler/rustc_llvm/build.rs                     | 1 +
 compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp | 7 +++++++
 compiler/rustc_llvm/src/lib.rs                   | 8 ++++++++
 3 files changed, 16 insertions(+)

diff --git a/compiler/rustc_llvm/build.rs b/compiler/rustc_llvm/build.rs
index 964b7cace9cd4..36a6d2cc33a99 100644
--- a/compiler/rustc_llvm/build.rs
+++ b/compiler/rustc_llvm/build.rs
@@ -76,6 +76,7 @@ fn main() {
         "aarch64",
         "amdgpu",
         "avr",
+        "m68k",
         "mips",
         "powerpc",
         "systemz",
diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
index b7cad1c3ba6d9..8dbacd71fc138 100644
--- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
@@ -201,6 +201,12 @@ void LLVMRustAddLastExtensionPasses(
 #define SUBTARGET_AVR
 #endif
 
+#ifdef LLVM_COMPONENT_M68k
+#define SUBTARGET_M68K SUBTARGET(M68k)
+#else
+#define SUBTARGET_M68K
+#endif
+
 #ifdef LLVM_COMPONENT_MIPS
 #define SUBTARGET_MIPS SUBTARGET(Mips)
 #else
@@ -248,6 +254,7 @@ void LLVMRustAddLastExtensionPasses(
   SUBTARGET_ARM                                                                \
   SUBTARGET_AARCH64                                                            \
   SUBTARGET_AVR                                                                \
+  SUBTARGET_M68K                                                               \
   SUBTARGET_MIPS                                                               \
   SUBTARGET_PPC                                                                \
   SUBTARGET_SYSTEMZ                                                            \
diff --git a/compiler/rustc_llvm/src/lib.rs b/compiler/rustc_llvm/src/lib.rs
index 122627eb500ad..2f199989d3b21 100644
--- a/compiler/rustc_llvm/src/lib.rs
+++ b/compiler/rustc_llvm/src/lib.rs
@@ -90,6 +90,14 @@ pub fn initialize_available_targets() {
         LLVMInitializeAVRAsmPrinter,
         LLVMInitializeAVRAsmParser
     );
+    init_target!(
+        llvm_component = "m68k",
+        LLVMInitializeM68kTargetInfo,
+        LLVMInitializeM68kTarget,
+        LLVMInitializeM68kTargetMC,
+        LLVMInitializeM68kAsmPrinter,
+        LLVMInitializeM68kAsmParser
+    );
     init_target!(
         llvm_component = "mips",
         LLVMInitializeMipsTargetInfo,

From 2cef5d809143175765934557a5a39b581b40e837 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Wed, 25 Aug 2021 07:51:34 +0000
Subject: [PATCH 10/12] library/std/env: Add 'm68k' to comment on ARCH constant

---
 library/std/src/env.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/library/std/src/env.rs b/library/std/src/env.rs
index e343073d21508..40b46878cd8ba 100644
--- a/library/std/src/env.rs
+++ b/library/std/src/env.rs
@@ -879,6 +879,7 @@ pub mod consts {
     /// - x86_64
     /// - arm
     /// - aarch64
+    /// - m68k
     /// - mips
     /// - mips64
     /// - powerpc

From c99d365d7e8334be95ad8d7b53bea28aea3862cd Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Thu, 26 Aug 2021 11:02:04 +0000
Subject: [PATCH 11/12] doc/platform-support.md: Add m68k-unknown-linux-gnu as
 Tier 3 target

---
 src/doc/rustc/src/platform-support.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md
index 3ba06e87220e3..0f106292e9670 100644
--- a/src/doc/rustc/src/platform-support.md
+++ b/src/doc/rustc/src/platform-support.md
@@ -238,6 +238,7 @@ target | std | host | notes
 `i686-uwp-windows-gnu` | ? |  |
 `i686-uwp-windows-msvc` | ? |  |
 `i686-wrs-vxworks` | ? |  |
+`m68k-unknown-linux-gnu` | ? |  | Motorola 680x0 Linux
 `mips-unknown-linux-uclibc` | ✓ |  | MIPS Linux with uClibc
 `mipsel-sony-psp` | * |  | MIPS (LE) Sony PlayStation Portable (PSP)
 `mipsel-unknown-linux-uclibc` | ✓ |  | MIPS (LE) Linux with uClibc

From 5d22b1afe2ff3adb9903cb71ff386828c16fd805 Mon Sep 17 00:00:00 2001
From: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Date: Mon, 30 Aug 2021 18:36:39 +0000
Subject: [PATCH 12/12] doc/platform-support: Add documentation for
 m68k-unknown-linux-gnu

---
 .../m68k-unknown-linux-gnu.md                 | 97 +++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 src/doc/rustc/src/platform-support/m68k-unknown-linux-gnu.md

diff --git a/src/doc/rustc/src/platform-support/m68k-unknown-linux-gnu.md b/src/doc/rustc/src/platform-support/m68k-unknown-linux-gnu.md
new file mode 100644
index 0000000000000..d325ba3346ab1
--- /dev/null
+++ b/src/doc/rustc/src/platform-support/m68k-unknown-linux-gnu.md
@@ -0,0 +1,97 @@
+# m68k-unknown-linux-gnu
+
+**Tier: 3**
+
+Motorola 680x0 Linux
+
+## Designated Developers
+
+* [@glaubitz](https://github.com/glaubitz)
+* [@ricky26](https://github.com/ricky26)
+
+## Requirements
+
+This target requires a Linux/m68k build environment for cross-compilation which
+is available on Debian and Debian-based systems, openSUSE and other distributions.
+
+On Debian, it should be sufficient to install a g++ cross-compiler for the m68k
+architecture which will automatically pull in additional dependencies such as
+the glibc cross development package:
+
+```text
+# apt install g++-m68k-linux-gnu
+```
+
+Binaries can be run using QEMU user emulation. On Debian-based systems, it should be
+sufficient to install the package `qemu-user-static` to be able to run simple static
+binaries:
+
+```text
+# apt install qemu-user-static
+```
+
+To run more complex programs, it will be necessary to set up a Debian/m68k chroot with
+the help of the command `debootstrap`:
+
+```text
+# apt install debootstrap debian-ports-archive-keyring
+# debootstrap --keyring=/usr/share/keyrings/debian-ports-archive-keyring.gpg --arch=m68k unstable debian-68k http://ftp.ports.debian.org/debian-ports
+```
+
+This chroot can then seamlessly entered using the normal `chroot` command thanks to
+QEMU user emulation:
+
+```text
+# chroot /path/to/debian-68k
+```
+
+To get started with native builds, which are currently untested, a native Debian/m68k
+system can be installed either on real hardware such as 68k-based Commodore Amiga or
+Atari systems or emulated environments such as QEMU version 4.2 or newer or ARAnyM.
+
+ISO images for installation are provided by the Debian Ports team and can be obtained
+from the Debian CD image server available at:
+
+[https://cdimage.debian.org/cdimage/ports/current](https://cdimage.debian.org/cdimage/ports/current/)
+
+Documentation for Debian/m68k is available on the Debian Wiki at:
+
+[https://wiki.debian.org/M68k](https://wiki.debian.org/M68k)
+
+Support is available either through the `debian-68k` mailing list:
+
+[https://lists.debian.org/debian-68k/](https://lists.debian.org/debian-68k/)
+
+or the `#debian-68k` IRC channel on OFTC network.
+
+## Building
+
+The codegen for this target should be built by default. However, core and std
+are currently missing but are being worked on and should become available in
+the near future.
+
+## Cross-compilation
+
+This target can be cross-compiled from a standard Debian or Debian-based, openSUSE or any
+other distribution which has a basic m68k cross-toolchain available.
+
+## Testing
+
+Currently there is no support to run the rustc test suite for this target.
+
+## Building Rust programs
+
+Rust programs can be built for that target:
+
+```text
+rustc --target m68k-unknown-linux-gnu your-code.rs
+```
+
+Very simple progams can be run using the `qemu-m68k-static` program:
+
+```text
+$ qemu-m68k-static your-code
+```
+
+For more complex applications, a chroot or native (emulated) Debian/m68k system are required
+for testing.