[PATCH v2 1/1] gcc/d: add LoongArch64 support for D frontend

2023-09-24 Thread Zixing Liu
gcc/ChangeLog:

* config.gcc: add loongarch-d.o to d_target_objs for LoongArch
architecture.

gcc/config/ChangeLog:

* loongarch/loongarch-d.cc
(loongarch_d_target_versions): add interface function to define builtin
D versions for LoongArch architecture.
(loongarch_d_handle_target_float_abi): add interface function to define
builtin D traits for LoongArch architecture.
(loongarch_d_register_target_info): add interface function to register
loongarch_d_handle_target_float_abi function.
* loongarch/loongarch-d.h:
(loongarch_d_target_versions): add function prototype.
(loongarch_d_register_target_info): Likewise.
* loongarch/t-loongarch: add object target for loongarch-d.cc.

libphobos/ChangeLog:

* configure.tgt: enable libphobos for LoongArch architecture.
* configure: Regenerate.
* libdruntime/gcc/sections/elf.d: add TLS_DTV_OFFSET constant for
LoongArch64.
* libdruntime/gcc/unwind/generic.d: add __aligned__ constant for
LoongArch64.
* libdruntime/Makefile.in: Regenerate.

Signed-off-by: Zixing Liu 
---
 gcc/config.gcc |  1 +
 gcc/config/loongarch/loongarch-d.cc| 77 ++
 gcc/config/loongarch/loongarch-d.h | 26 
 gcc/config/loongarch/t-loongarch   |  4 ++
 libphobos/configure.tgt|  3 +
 libphobos/libdruntime/gcc/sections/elf.d   |  2 +
 libphobos/libdruntime/gcc/unwind/generic.d |  1 +
 7 files changed, 114 insertions(+)
 create mode 100644 gcc/config/loongarch/loongarch-d.cc
 create mode 100644 gcc/config/loongarch/loongarch-d.h

diff --git a/gcc/config.gcc b/gcc/config.gcc
index ee46d96bf62..782d933b497 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -480,6 +480,7 @@ mips*-*-*)
;;
 loongarch*-*-*)
cpu_type=loongarch
+   d_target_objs="loongarch-d.o"
extra_headers="larchintrin.h lsxintrin.h lasxintrin.h"
extra_objs="loongarch-c.o loongarch-builtins.o loongarch-cpu.o 
loongarch-opts.o loongarch-def.o"
extra_gcc_objs="loongarch-driver.o loongarch-cpu.o loongarch-opts.o 
loongarch-def.o"
diff --git a/gcc/config/loongarch/loongarch-d.cc 
b/gcc/config/loongarch/loongarch-d.cc
new file mode 100644
index 000..9ac483c39a7
--- /dev/null
+++ b/gcc/config/loongarch/loongarch-d.cc
@@ -0,0 +1,77 @@
+/* Subroutines for the D front end on the LoongArch architecture.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#define IN_TARGET_CODE 1
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm_d.h"
+#include "d/d-target.h"
+#include "d/d-target-def.h"
+
+/* Implement TARGET_D_CPU_VERSIONS for LoongArch targets.  */
+
+void
+loongarch_d_target_versions (void)
+{
+  if (TARGET_64BIT)
+d_add_builtin_version ("LoongArch64");
+  else
+d_add_builtin_version ("LoongArch32");
+
+  if (TARGET_HARD_FLOAT_ABI)
+{
+  d_add_builtin_version ("LoongArch_HardFloat");
+  d_add_builtin_version ("D_HardFloat");
+}
+  else if (TARGET_SOFT_FLOAT_ABI)
+{
+  d_add_builtin_version ("LoongArch_SoftFloat");
+  d_add_builtin_version ("D_SoftFloat");
+}
+}
+
+/* Handle a call to `__traits(getTargetInfo, "floatAbi")'.  */
+
+static tree
+loongarch_d_handle_target_float_abi (void)
+{
+  const char *abi;
+
+  if (TARGET_HARD_FLOAT_ABI)
+abi = "hard";
+  else if (TARGET_SOFT_FLOAT_ABI)
+abi = "soft";
+  else
+abi = "";
+
+  return build_string_literal (strlen (abi) + 1, abi);
+}
+
+/* Implement TARGET_D_REGISTER_CPU_TARGET_INFO.  */
+
+void
+loongarch_d_register_target_info (void)
+{
+  const struct d_target_info_spec handlers[] = {
+{"floatAbi", loongarch_d_handle_target_float_abi},
+{NULL, NULL},
+  };
+
+  d_add_target_info_handlers (handlers);
+}
diff --git a/gcc/config/loongarch/loongarch-d.h 
b/gcc/config/loongarch/loongarch-d.h
new file mode 100644
index 000..5ef7bdfa9a0
--- /dev/null
+++ b/gcc/config/loongarch/loongarch-d.h
@@ -0,0 +1,26 @@
+/* Definitions for the D front end on the LoongArch architecture.
+   Copyright (C) 

[PATCH v2 0/1] Add LoongArch64 support for D frontend

2023-09-24 Thread Zixing Liu
This patch adds the LoongArch64 support for GCC D frontend.

The runtime support is submitted as a separate patch here:
https://github.com/dlang/dmd/pull/15628.

You can find more information about the LoongArch architecture on this
website:
https://loongson.github.io/LoongArch-Documentation/README-EN.html.

--

Changes since the last revision of the patch:

* Corrected copyright years in loongarch-d.cc and loongarch-d.h.
* Removed changes to the tests, the changes have been rolled into the DMD
  changes in:
  
https://github.com/dlang/dmd/pull/15628/commits/eb84b8a2bc86aa751ad6f472422e8abad63ff500
  .
* Removed D_LP32 and D_LP64 bits. Since LoongArch ABIs are somewhat
  complicated, we may introduce the ABI information in the form of target
  traits in the future.

Zixing Liu (1):

 gcc/config.gcc |  1 +
 gcc/config/loongarch/loongarch-d.cc| 77 ++
 gcc/config/loongarch/loongarch-d.h | 26 
 gcc/config/loongarch/t-loongarch   |  4 ++
 libphobos/configure.tgt|  3 +
 libphobos/libdruntime/gcc/sections/elf.d   |  2 +
 libphobos/libdruntime/gcc/unwind/generic.d |  1 +
 7 files changed, 114 insertions(+)
 create mode 100644 gcc/config/loongarch/loongarch-d.cc
 create mode 100644 gcc/config/loongarch/loongarch-d.h

-- 
2.42.0