From d40520af55878c35ba4a4fb4421cca2b5f315b2a Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sat, 11 Nov 2023 02:08:07 +0100 Subject: [PATCH] riscv64: Cater for M2-Planet. * kaem.riscv64: New file. * lib/linux/riscv64-mes-m2/crt1.M1, lib/m2/riscv64/ELF-riscv64.hex2, lib/m2/riscv64/riscv64_defs.M1: New files, imported from M2Lib.c * lib/linux/riscv64-mes-m2/_exit.c, lib/linux/riscv64-mes-m2/_write.c, lib/linux/riscv64-mes-m2/crt1.c, lib/linux/riscv64-mes-m2/syscall.c: New files, adapted for M2-Planet calling convention from ... * lib/linux/riscv64-mes-mescc: ...here * kaem.run: Move fcntl.h up. Include signal.h. * build-aux/build.sh.in: Also allow kaem build for riscv64. --- build-aux/build.sh.in | 4 +- build-aux/cflags.sh | 4 + kaem.riscv64 | 26 ++++ kaem.run | 4 +- lib/linux/open.c | 6 + lib/linux/riscv64-mes-m2/_exit.c | 31 ++++ lib/linux/riscv64-mes-m2/_write.c | 33 ++++ lib/linux/riscv64-mes-m2/crt1.M1 | 74 +++++++++ lib/linux/riscv64-mes-m2/crt1.c | 34 +++++ lib/linux/riscv64-mes-m2/syscall.c | 182 ++++++++++++++++++++++ lib/m2/riscv64/ELF-riscv64.hex2 | 69 +++++++++ lib/m2/riscv64/riscv64_defs.M1 | 237 +++++++++++++++++++++++++++++ lib/mes/__init_io.c | 30 ++++ lib/riscv64-mes/riscv64.M1 | 15 +- scaffold/argv.c | 6 +- scaffold/argv.kaem | 1 + scaffold/env.kaem | 1 + scaffold/global-array.kaem | 1 + scaffold/hello.kaem | 1 + scaffold/local-array.kaem | 1 + scaffold/main.kaem | 1 + scaffold/read.kaem | 2 + 22 files changed, 758 insertions(+), 5 deletions(-) create mode 100644 kaem.riscv64 create mode 100644 lib/linux/riscv64-mes-m2/_exit.c create mode 100644 lib/linux/riscv64-mes-m2/_write.c create mode 100644 lib/linux/riscv64-mes-m2/crt1.M1 create mode 100644 lib/linux/riscv64-mes-m2/crt1.c create mode 100644 lib/linux/riscv64-mes-m2/syscall.c create mode 100644 lib/m2/riscv64/ELF-riscv64.hex2 create mode 100644 lib/m2/riscv64/riscv64_defs.M1 create mode 100644 lib/mes/__init_io.c diff --git a/build-aux/build.sh.in b/build-aux/build.sh.in index c1cb04f3..f43bc10e 100644 --- a/build-aux/build.sh.in +++ b/build-aux/build.sh.in @@ -103,7 +103,9 @@ fi if test -n "$M2_PLANET" && (test "$mes_cpu" == "x86" \ || test "$mes_cpu" == "x86_64" \ - || test "$mes_cpu" == "arm" ); then + || test "$mes_cpu" == "arm" \ + || test "$mes_cpu" == "riscv64" \ + ); then ( . ${srcdest}build-aux/trace.sh if [ -z "$V" -o "$V" = 0 ]; then diff --git a/build-aux/cflags.sh b/build-aux/cflags.sh index e19bd1dc..58be5587 100644 --- a/build-aux/cflags.sh +++ b/build-aux/cflags.sh @@ -29,6 +29,10 @@ case "$mes_cpu" in cc_cpu=i386 stage0_cpu=x86 ;; + riscv64) + cc_cpu=riscv64 + stage0_cpu=riscv64 + ;; *) stage0_cpu=$mes_cpu cc_cpu=$mes_cpu diff --git a/kaem.riscv64 b/kaem.riscv64 new file mode 100644 index 00000000..cdee7236 --- /dev/null +++ b/kaem.riscv64 @@ -0,0 +1,26 @@ +#! /bin/sh +# Copyright © 2022 Jan (janneke) Nieuwenhuizen +# +# This file is part of GNU Mes. +# +# GNU Mes 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 of the License, or (at +# your option) any later version. +# +# GNU Mes 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 GNU Mes. If not, see . + +# Usage: +# kaem --verbose --strict + +cc_cpu=riscv64 +mes_cpu=riscv64 +stage0_cpu=riscv64 +blood_elf_flag=--64 +kaem --verbose --strict diff --git a/kaem.run b/kaem.run index b87fb97d..011773d6 100644 --- a/kaem.run +++ b/kaem.run @@ -36,6 +36,7 @@ M2-Planet \ -f include/mes/lib-mini.h \ -f include/mes/lib.h \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ -f lib/mes/globals.c \ @@ -53,6 +54,7 @@ M2-Planet \ -f lib/stdio/getchar.c \ -f lib/stdio/putchar.c \ -f lib/stub/__buffered_read.c \ + -f include/fcntl.h \ -f lib/linux/_open3.c \ -f lib/linux/open.c \ -f lib/mes/mes_open.c \ @@ -99,6 +101,7 @@ M2-Planet \ -f lib/linux/ioctl3.c \ -f include/sys/ioctl.h \ -f lib/m2/isatty.c \ + -f include/signal.h \ -f lib/linux/fork.c \ -f lib/m2/execve.c \ -f lib/m2/execv.c \ @@ -120,7 +123,6 @@ M2-Planet \ -f src/core.c \ -f src/display.c \ -f src/eval-apply.c \ - -f include/fcntl.h \ -f src/gc.c \ -f src/hash.c \ -f src/lib.c \ diff --git a/lib/linux/open.c b/lib/linux/open.c index 1806dfa6..6da15d7e 100644 --- a/lib/linux/open.c +++ b/lib/linux/open.c @@ -28,7 +28,13 @@ int open (char *file_name, int flags, int mask) { +#if defined (SYS_open) int r = _sys_call3 (SYS_open, file_name, flags, mask); +#elif defined (SYS_openat) + int r = _sys_call4 (SYS_openat, AT_FDCWD, file_name, flags, mask); +#else +#error No usable open syscall +#endif if (r > 2) __ungetc_clear (r); return r; diff --git a/lib/linux/riscv64-mes-m2/_exit.c b/lib/linux/riscv64-mes-m2/_exit.c new file mode 100644 index 00000000..b9e27c0b --- /dev/null +++ b/lib/linux/riscv64-mes-m2/_exit.c @@ -0,0 +1,31 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020,2023 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * This file is part of GNU Mes. + * + * GNU Mes 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 of the License, or (at + * your option) any later version. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +void +_exit (int status) +{ + asm ("ld_____%a0,-0x08(%fp)"); + asm ("li_____%a7,SYS_exit"); + asm ("ecall"); + // no need to read return value +} diff --git a/lib/linux/riscv64-mes-m2/_write.c b/lib/linux/riscv64-mes-m2/_write.c new file mode 100644 index 00000000..63299cea --- /dev/null +++ b/lib/linux/riscv64-mes-m2/_write.c @@ -0,0 +1,33 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2020,2023 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * This file is part of GNU Mes. + * + * GNU Mes 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 of the License, or (at + * your option) any later version. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +void +_write (int filedes, void const *buffer, size_t size) +{ + asm ("ld_____%a0,-0x08(%fp)"); + asm ("ld_____%a1,-0x10(%fp)"); + asm ("ld_____%a2,-0x18(%fp)"); + asm ("li_____%a7,SYS_write"); + asm ("ecall"); + asm ("mv_____%t0,%a0"); +} diff --git a/lib/linux/riscv64-mes-m2/crt1.M1 b/lib/linux/riscv64-mes-m2/crt1.M1 new file mode 100644 index 00000000..14175cb0 --- /dev/null +++ b/lib/linux/riscv64-mes-m2/crt1.M1 @@ -0,0 +1,74 @@ +## Copyright (C) 2021 Andrius Štikonas +## Copyright (C) 2023 Janneke Nieuwenhuizen +## +## This file is part of stage0. +## +## stage0 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 of the License, or +## (at your option) any later version. +## +## stage0 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 stage0. If not, see . + +:_start + RD_FP RS1_SP MV ; Protect stack pointer + + ; Prepare argv + RD_A0 RS1_FP !8 ADDI ; ARGV_address = FP + 8 + RD_SP RS1_SP !-8 ADDI + RS1_SP RS2_A0 SD ; Put argv on the stack + + ; Prepare envp + RD_A0 RS1_FP MV ; Address we need to load from + RD_A0 RS1_A0 LD ; Get ARGC + RD_A0 RS1_A0 !2 ADDI ; OFFSET = ARGC + 2 + RD_A0 RS1_A0 RS2_X3 SLLI ; OFFSET = OFFSET * WORDSIZE + RD_A0 RS1_FP RS2_A0 ADD ; ENVP_address = RSP + OFFSET + RD_SP RS1_SP !-8 ADDI + RS1_SP RS2_A0 SD ; Put envp on the stack + + # AAARG, this also does not work!? + # pop____%t0 ; envp + # push___%t0 + # li_____%t0,$i32 &GLOBAL_environ + + # li_____%t0,$i16_0000 @0 + # XXX avoid Illegal instruction + # li_____%t1,$i32 &GLOBAL___stdin + # sw_____%t0,0(%t1) + + # li_____%t0,$i16_0000 @1 + # srai___%t0,16 + # li_____%t1,$i32 &GLOBAL___stdout + # sw_____%t0,0(%t1) + + # li_____%t0,$i16_0000 @2 + # XXX avoid Segmentation fault + # srai___%t0,16 + # li_____%t1,$i32 &GLOBAL___stderr + # sw_____%t0,0(%t1) + + ; Stack offset + RD_FP RS1_FP !8 ADDI + + ; Init libc + RD_RA $FUNCTION___init_io JAL + + ; Call main function + RD_RA $FUNCTION_main JAL + + ; Put return value on the stack so that _exit gets it + RD_SP RS1_SP !-16 ADDI + RS1_SP RS2_A0 SD + + ; Exit to kernel + + RD_A0 RS1_SP LD + RD_A7 !93 ADDI ; Syscall for exit + ECALL ; Exit with code in a0 diff --git a/lib/linux/riscv64-mes-m2/crt1.c b/lib/linux/riscv64-mes-m2/crt1.c new file mode 100644 index 00000000..da4c4824 --- /dev/null +++ b/lib/linux/riscv64-mes-m2/crt1.c @@ -0,0 +1,34 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2018,2022 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes 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 of the License, or (at + * your option) any later version. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +int __stdin; +int __stdout; +int __stderr; +char **environ; +int main (int argc, char **argv, char **envp); + +/* FIXME: this is going to be called `FUNCTION__start' */ +//#int +//#_start () +//#{ +//# .. +//#} diff --git a/lib/linux/riscv64-mes-m2/syscall.c b/lib/linux/riscv64-mes-m2/syscall.c new file mode 100644 index 00000000..13d5a60a --- /dev/null +++ b/lib/linux/riscv64-mes-m2/syscall.c @@ -0,0 +1,182 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2020,2023 Jan (janneke) Nieuwenhuizen + * Copyright © 2021 W. J. van der Laan + * + * This file is part of GNU Mes. + * + * GNU Mes 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 of the License, or (at + * your option) any later version. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include +#include + +long +__sys_call (long sys_call) +{ + asm ("ld_____%a7,-0x08(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call1 (long sys_call, long one) +{ + asm ("ld_____%a7,-0x08(%fp)"); + asm ("ld_____%a0,-0x10(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call2 (long sys_call, long one, long two) +{ + asm ("ld_____%a7,-0x08(%fp)"); + asm ("ld_____%a0,-0x10(%fp)"); + asm ("ld_____%a1,-0x18(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call3 (long sys_call, long one, long two, long three) +{ + asm ("ld_____%a7,-0x08(%fp)"); + asm ("ld_____%a0,-0x10(%fp)"); + asm ("ld_____%a1,-0x18(%fp)"); + asm ("ld_____%a2,-0x20(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call4 (long sys_call, long one, long two, long three, long four) +{ + asm ("ld_____%a7,-0x08(%fp)"); + asm ("ld_____%a0,-0x10(%fp)"); + asm ("ld_____%a1,-0x18(%fp)"); + asm ("ld_____%a2,-0x20(%fp)"); + asm ("ld_____%a3,-0x28(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +__sys_call5 (long sys_call, long one, long two, long three, long four, long five) +{ + asm ("ld_____%a7,-0x08(%fp)"); + asm ("ld_____%a0,-0x10(%fp)"); + asm ("ld_____%a1,-0x18(%fp)"); + asm ("ld_____%a2,-0x20(%fp)"); + asm ("ld_____%a3,-0x28(%fp)"); + asm ("ld_____%a4,-0x30(%fp)"); + + asm ("ecall"); + + asm ("mv_____%t0,%a0"); +} + +long +_sys_call (long sys_call) +{ + long r = __sys_call (sys_call); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call1 (long sys_call, long one) +{ + long r = __sys_call1 (sys_call, one); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call2 (long sys_call, long one, long two) +{ + long r = __sys_call2 (sys_call, one, two); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call3 (long sys_call, long one, long two, long three) +{ + long r = __sys_call3 (sys_call, one, two, three); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call4 (long sys_call, long one, long two, long three, long four) +{ + long r = __sys_call4 (sys_call, one, two, three, four); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} + +long +_sys_call5 (long sys_call, long one, long two, long three, long four, long five) +{ + long r = __sys_call5 (sys_call, one, two, three, four, five); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} diff --git a/lib/m2/riscv64/ELF-riscv64.hex2 b/lib/m2/riscv64/ELF-riscv64.hex2 new file mode 100644 index 00000000..8844d37a --- /dev/null +++ b/lib/m2/riscv64/ELF-riscv64.hex2 @@ -0,0 +1,69 @@ +### Copyright (C) 2016 Jeremiah Orians +### Copyright (C) 2017 Jan Nieuwenhuizen +### This file is part of M2-Planet. +### +### M2-Planet 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 of the License, or +### (at your option) any later version. +### +### M2-Planet 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 M2-Planet. If not, see . + +### 64 bit elf header in hex2 +### if you wish to use this header, you need to add :ELF_end to the end of your +### M1 or hex2 files. + +## ELF Header + +:ELF_base +7F 45 4C 46 ## e_ident[EI_MAG0-3] ELF's magic number + +02 ## e_ident[EI_CLASS] Indicating 64 bit +01 ## e_ident[EI_DATA] Indicating little endianness +01 ## e_ident[EI_VERSION] Indicating original elf + +03 ## e_ident[EI_OSABI] Set at 3 because FreeBSD is strict +00 ## e_ident[EI_ABIVERSION] Set at 0 because none cares + +00 00 00 00 00 00 00 ## e_ident[EI_PAD] +02 00 ## e_type Indicating Executable +F3 00 ## e_machine Indicating RISC-V +01 00 00 00 ## e_version Indicating original elf + +&_start 00 00 00 00 ## e_entry Address of the entry point (Number of bytes this header is + Base Address) +%ELF_program_headers>ELF_base 00 00 00 00 ## e_phoff Address of program header table +%ELF_section_headers>ELF_base 00 00 00 00 ## e_shoff Address of section header table + +00 00 00 00 ## e_flags +40 00 ## e_ehsize Indicating our 64 Byte header + +38 00 ## e_phentsize size of a program header table +01 00 ## e_phnum number of entries in program table + +40 00 ## e_shentsize size of a section header table +05 00 ## e_shnum number of entries in section table + +02 00 ## e_shstrndx index of the section names + +## Program Header +:ELF_program_headers +:ELF_program_header__text +01 00 00 00 ## p_type +07 00 00 00 ## ph_flags: PF-X|PF-W|PF-R = 7 +00 00 00 00 00 00 00 00 ## p_offset + +&ELF_base 00 00 00 00 ## p_vaddr +&ELF_base 00 00 00 00 ## p_physaddr + +%ELF_end>ELF_base 00 00 00 00 ## p_filesz +%ELF_end>ELF_base 00 00 00 00 ## p_memsz + +01 00 00 00 00 00 00 00 ## Required alignment + +:ELF_text diff --git a/lib/m2/riscv64/riscv64_defs.M1 b/lib/m2/riscv64/riscv64_defs.M1 new file mode 100644 index 00000000..0f39c17c --- /dev/null +++ b/lib/m2/riscv64/riscv64_defs.M1 @@ -0,0 +1,237 @@ +## Copyright (C) 2021 Andrius Štikonas +## This file is part of M2-Planet. +## +## M2-Planet 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 of the License, or +## (at your option) any later version. +## +## M2-Planet 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 M2-Planet. If not, see . + +DEFINE NULL 0000000000000000 + +;; Opcodes + +;; RV32I Base Instruction Set +DEFINE LUI 37000000 +DEFINE AUIPC 17000000 +DEFINE JAL 6F000000 +DEFINE JALR 67000000 +DEFINE BEQ 63000000 +DEFINE BNE 63100000 +DEFINE BLT 63400000 +DEFINE BGE 63500000 +DEFINE BLTU 63600000 +DEFINE BGEU 63700000 +DEFINE LB 03000000 +DEFINE LH 03100000 +DEFINE LW 03200000 +DEFINE LBU 03400000 +DEFINE LHU 03500000 +DEFINE SB 23000000 +DEFINE SH 23100000 +DEFINE SW 23200000 +DEFINE ADDI 13000000 +DEFINE SLTI 13200000 +DEFINE SLTIU 13300000 +DEFINE XORI 13400000 +DEFINE ORI 13600000 +DEFINE ANDI 13700000 +DEFINE SLLI 13100000 +DEFINE SRLI 13500000 +DEFINE SRAI 13500040 +DEFINE ADD 33000000 +DEFINE SUB 33000040 +DEFINE SLL 33100000 +DEFINE SLT 33200000 +DEFINE SLTU 33300000 +DEFINE XOR 33400000 +DEFINE SRL 33500000 +DEFINE SRA 33500040 +DEFINE OR 33600000 +DEFINE AND 33700000 +DEFINE ECALL 73000000 + +;; RV64I Base Instruction set +DEFINE LWU 03600000 +DEFINE LD 03300000 +DEFINE SD 23300000 +DEFINE ADDIW 1B000000 +DEFINE SLLIW 1B100000 +DEFINE SRLIW 1B500000 +DEFINE SRAIW 1B500040 +DEFINE ADDW 3B000000 +DEFINE SUBW 3B000040 +DEFINE SLLW 3B100000 +DEFINE SRLW 3B500000 +DEFINE SRAW 3B500040 + +;; RV32M Standard Extensions +DEFINE MUL 33000002 +DEFINE MULH 33100002 +DEFINE MULHSU 33200002 +DEFINE MULHU 33300002 +DEFINE DIV 33400002 +DEFINE DIVU 33500002 +DEFINE REM 33600002 +DEFINE REMU 33700002 + +;; RV64M Standard Extensions +DEFINE MULW 3B000002 +DEFINE DIVW 3B400002 +DEFINE DIVUW 3B500002 +DEFINE REMW 3B600002 +DEFINE REMUW 3B700002 + +;; Pseudoinstructions +DEFINE NOP 13000000 # ADDI +DEFINE MV 13000000 # ADDI +DEFINE NOT 1340F0FF # XORI, RD, RS, -1 +DEFINE BEQZ 63000000 # BEQ +DEFINE BNEZ 63100000 # BNE +DEFINE BLTZ 63400000 # BLT +DEFINE RETURN 67800000 # RS1_RA JALR + +;; Destination registers +;; register_number << 7 +DEFINE RD_RA .80000000 +DEFINE RD_SP .00010000 +DEFINE RD_GP .80010000 +DEFINE RD_TP .00020000 +DEFINE RD_T0 .80020000 +DEFINE RD_T1 .00030000 +DEFINE RD_T2 .80030000 +DEFINE RD_S0 .00040000 +DEFINE RD_FP .00040000 +DEFINE RD_S1 .80040000 +DEFINE RD_A0 .00050000 +DEFINE RD_A1 .80050000 +DEFINE RD_A2 .00060000 +DEFINE RD_A3 .80060000 +DEFINE RD_A4 .00070000 +DEFINE RD_A5 .80070000 +DEFINE RD_A6 .00080000 +DEFINE RD_A7 .80080000 +DEFINE RD_S2 .00090000 +DEFINE RD_S3 .80090000 +DEFINE RD_S4 .000A0000 +DEFINE RD_S5 .800A0000 +DEFINE RD_S6 .000B0000 +DEFINE RD_S7 .800B0000 +DEFINE RD_S8 .000C0000 +DEFINE RD_S9 .800C0000 +DEFINE RD_S10 .000D0000 +DEFINE RD_S11 .800D0000 +DEFINE RD_T3 .000E0000 +DEFINE RD_T4 .800E0000 +DEFINE RD_T5 .000F0000 +DEFINE RD_T6 .800F0000 + +;; First source registers +;; register_number << 15 +DEFINE RS1_RA .00800000 +DEFINE RS1_SP .00000100 +DEFINE RS1_GP .00800100 +DEFINE RS1_TP .00000200 +DEFINE RS1_T0 .00800200 +DEFINE RS1_T1 .00000300 +DEFINE RS1_T2 .00800300 +DEFINE RS1_S0 .00000400 +DEFINE RS1_FP .00000400 +DEFINE RS1_S1 .00800400 +DEFINE RS1_A0 .00000500 +DEFINE RS1_A1 .00800500 +DEFINE RS1_A2 .00000600 +DEFINE RS1_A3 .00800600 +DEFINE RS1_A4 .00000700 +DEFINE RS1_A5 .00800700 +DEFINE RS1_A6 .00000800 +DEFINE RS1_A7 .00800800 +DEFINE RS1_S2 .00000900 +DEFINE RS1_S3 .00800900 +DEFINE RS1_S4 .00000A00 +DEFINE RS1_S5 .00800A00 +DEFINE RS1_S6 .00000B00 +DEFINE RS1_S7 .00800B00 +DEFINE RS1_S8 .00000C00 +DEFINE RS1_S9 .00800C00 +DEFINE RS1_S10 .00000D00 +DEFINE RS1_S11 .00800D00 +DEFINE RS1_T3 .00000E00 +DEFINE RS1_T4 .00800E00 +DEFINE RS1_T5 .00000F00 +DEFINE RS1_T6 .00800F00 + +;; Second source registers +;; register_number << 20 +DEFINE RS2_RA .00001000 +DEFINE RS2_SP .00002000 +DEFINE RS2_GP .00003000 +DEFINE RS2_TP .00004000 +DEFINE RS2_T0 .00005000 +DEFINE RS2_T1 .00006000 +DEFINE RS2_T2 .00007000 +DEFINE RS2_S0 .00008000 +DEFINE RS2_FP .00008000 +DEFINE RS2_S1 .00009000 +DEFINE RS2_A0 .0000A000 +DEFINE RS2_A1 .0000B000 +DEFINE RS2_A2 .0000C000 +DEFINE RS2_A3 .0000D000 +DEFINE RS2_A4 .0000E000 +DEFINE RS2_A5 .0000F000 +DEFINE RS2_A6 .00000001 +DEFINE RS2_A7 .00001001 +DEFINE RS2_S2 .00002001 +DEFINE RS2_S3 .00003001 +DEFINE RS2_S4 .00004001 +DEFINE RS2_S5 .00005001 +DEFINE RS2_S6 .00006001 +DEFINE RS2_S7 .00007001 +DEFINE RS2_S8 .00008001 +DEFINE RS2_S9 .00009001 +DEFINE RS2_S10 .0000A001 +DEFINE RS2_S11 .0000B001 +DEFINE RS2_T3 .0000C001 +DEFINE RS2_T4 .0000D001 +DEFINE RS2_T5 .0000E001 +DEFINE RS2_T6 .0000F001 + +DEFINE RS2_X0 .00000000 +DEFINE RS2_X1 .00001000 +DEFINE RS2_X2 .00002000 +DEFINE RS2_X3 .00003000 +DEFINE RS2_X4 .00004000 +DEFINE RS2_X5 .00005000 +DEFINE RS2_X6 .00006000 +DEFINE RS2_X7 .00007000 +DEFINE RS2_X8 .00008000 +DEFINE RS2_X9 .00009000 +DEFINE RS2_X10 .0000A000 +DEFINE RS2_X11 .0000B000 +DEFINE RS2_X12 .0000C000 +DEFINE RS2_X13 .0000D000 +DEFINE RS2_X14 .0000E000 +DEFINE RS2_X15 .0000F000 +DEFINE RS2_X16 .00000001 +DEFINE RS2_X17 .00001001 +DEFINE RS2_X18 .00002001 +DEFINE RS2_X19 .00003001 +DEFINE RS2_X20 .00004001 +DEFINE RS2_X21 .00005001 +DEFINE RS2_X22 .00006001 +DEFINE RS2_X23 .00007001 +DEFINE RS2_X24 .00008001 +DEFINE RS2_X25 .00009001 +DEFINE RS2_X26 .0000A001 +DEFINE RS2_X27 .0000B001 +DEFINE RS2_X28 .0000C001 +DEFINE RS2_X29 .0000D001 +DEFINE RS2_X30 .0000E001 +DEFINE RS2_X31 .0000F001 diff --git a/lib/mes/__init_io.c b/lib/mes/__init_io.c new file mode 100644 index 00000000..dd1e8e01 --- /dev/null +++ b/lib/mes/__init_io.c @@ -0,0 +1,30 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2023 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes 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 of the License, or (at + * your option) any later version. + * + * GNU Mes 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 GNU Mes. If not, see . + */ + +#include "mes/lib-mini.h" + +void +__init_io (int argc, char** argv, char** envp) +{ + __stdin = 0; + __stdout = 1; + __stderr = 2; + environ = envp; +} diff --git a/lib/riscv64-mes/riscv64.M1 b/lib/riscv64-mes/riscv64.M1 index 6b571c92..5ff91494 100644 --- a/lib/riscv64-mes/riscv64.M1 +++ b/lib/riscv64-mes/riscv64.M1 @@ -1,5 +1,5 @@ ### GNU Mes --- Maxwell Equations of Software -### Copyright © 2017,2018,2020 Jan (janneke) Nieuwenhuizen +### Copyright © 2017,2018,2020,2023 Jan (janneke) Nieuwenhuizen ### Copyright © 2019,2020 Danny Milosavljevic ### Copyright © 2021 W. J. van der Laan ### @@ -326,6 +326,7 @@ DEFINE mv_____%s10,%x0 130d0000 DEFINE mv_____%s11,%t0 938d0200 DEFINE mv_____%s11,%t1 930d0300 DEFINE mv_____%s11,%x0 930d0000 + DEFINE ld_____%a7,0x10(%fp) 83380401 DEFINE ld_____%a0,0x18(%fp) 03358401 DEFINE ld_____%a1,0x20(%fp) 83350402 @@ -335,6 +336,18 @@ DEFINE ld_____%a4,0x38(%fp) 03378403 DEFINE ld_____%a0,0x10(%fp) 03350401 DEFINE ld_____%a1,0x18(%fp) 83358401 DEFINE ld_____%a2,0x20(%fp) 03360402 + +DEFINE ld_____%a0,-0x08(%fp) 033584ff +DEFINE ld_____%a1,-0x10(%fp) 833504ff +DEFINE ld_____%a2,-0x18(%fp) 033684fe + +DEFINE ld_____%a7,-0x08(%fp) 833884ff +DEFINE ld_____%a0,-0x10(%fp) 033504ff +DEFINE ld_____%a1,-0x18(%fp) 833584fe +DEFINE ld_____%a2,-0x20(%fp) 033604fe +DEFINE ld_____%a3,-0x28(%fp) 833684fd +DEFINE ld_____%a4,-0x30(%fp) 033704fd + DEFINE ld_____%fp,0x10(%fp) 03340401 DEFINE ld_____%t0,0x8(%fp) 83328400 DEFINE ld_____%sp,0x10(%fp) 03310401 diff --git a/scaffold/argv.c b/scaffold/argv.c index 3a352034..134c98d8 100644 --- a/scaffold/argv.c +++ b/scaffold/argv.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2016,2017,2018,2023 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -25,7 +25,9 @@ int main (int argc, char **argv) { eputs ("Hi Mes!\n"); -#if __MESC_MES__ +#if __M2__ + eputs ("MESC.M2\n"); +#elif __MESC_MES__ eputs ("MESC.MES\n"); #else eputs ("MESC.GUILE\n"); diff --git a/scaffold/argv.kaem b/scaffold/argv.kaem index 3797bc68..35700f61 100644 --- a/scaffold/argv.kaem +++ b/scaffold/argv.kaem @@ -31,6 +31,7 @@ M2-Planet \ -D __linux__=1 \ -f include/mes/lib-mini.h \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ -f lib/string/strlen.c \ diff --git a/scaffold/env.kaem b/scaffold/env.kaem index 13aeaaaf..d0f3f195 100644 --- a/scaffold/env.kaem +++ b/scaffold/env.kaem @@ -31,6 +31,7 @@ M2-Planet \ -D __linux__=1 \ -f include/mes/lib-mini.h \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ -f lib/string/strlen.c \ diff --git a/scaffold/global-array.kaem b/scaffold/global-array.kaem index 8ada16d9..3aaaa384 100644 --- a/scaffold/global-array.kaem +++ b/scaffold/global-array.kaem @@ -31,6 +31,7 @@ M2-Planet \ -D __${cc_cpu}__=1 \ -D __linux__=1 \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/mes/globals.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ diff --git a/scaffold/hello.kaem b/scaffold/hello.kaem index 7b45ca03..cff0976b 100644 --- a/scaffold/hello.kaem +++ b/scaffold/hello.kaem @@ -31,6 +31,7 @@ M2-Planet \ -D __linux__=1 \ -f include/mes/lib-mini.h \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ -f lib/string/strlen.c \ diff --git a/scaffold/local-array.kaem b/scaffold/local-array.kaem index 4a7e3a72..55d4948c 100644 --- a/scaffold/local-array.kaem +++ b/scaffold/local-array.kaem @@ -31,6 +31,7 @@ M2-Planet \ -D __${cc_cpu}__=1 \ -D __linux__=1 \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f lib/mes/globals.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ diff --git a/scaffold/main.kaem b/scaffold/main.kaem index 3fa38fb0..14c358e6 100644 --- a/scaffold/main.kaem +++ b/scaffold/main.kaem @@ -30,6 +30,7 @@ M2-Planet \ -D __${cc_cpu}__=1 \ -D __linux__=1 \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f scaffold/main.c \ -o scaffold/main.M1 diff --git a/scaffold/read.kaem b/scaffold/read.kaem index 55a981f7..46f8be7a 100644 --- a/scaffold/read.kaem +++ b/scaffold/read.kaem @@ -30,6 +30,7 @@ M2-Planet \ -D __${cc_cpu}__=1 \ -D __linux__=1 \ -f lib/linux/${mes_cpu}-mes-m2/crt1.c \ + -f lib/mes/__init_io.c \ -f include/mes/lib-mini.h \ -f lib/linux/${mes_cpu}-mes-m2/_write.c \ -f lib/linux/${mes_cpu}-mes-m2/_exit.c \ @@ -47,6 +48,7 @@ M2-Planet \ -f lib/mes/fdgetc.c \ -f lib/stdio/getchar.c \ -f lib/stdio/putchar.c \ + -f include/fcntl.h \ -f lib/linux/open.c \ -f include/m2/types.h \ -f include/sys/types.h \