Skip to content

Commit

Permalink
lib: Add setjmp/longjmp for riscv64.
Browse files Browse the repository at this point in the history
Note about the -tcc file:

    HAVE_FLOAT_ASM defines if there's assembly support for float
    instructions.

    HAVE_FLOAT defines if there's floating point arithmetic support.

    This is important for TinyCC because the assembler and the C
    compiler are split. TinyCC can HAVE_FLOAT but no assembler support
    for them.

* lib/riscv64-mes-gcc/setjmp.c,
lib/riscv64-mes-tcc/setjmp.c: New files.
  • Loading branch information
ekaitz-zarraga authored and janneke committed Oct 20, 2023
1 parent 7150684 commit 0e2c556
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 7 deletions.
70 changes: 63 additions & 7 deletions lib/riscv64-mes-gcc/setjmp.c
Expand Up @@ -2,6 +2,7 @@
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
* Copyright © 2023 Ekaitz Zarraga <ekaitz@elenq.tech>
*
* This file is part of GNU Mes.
*
Expand All @@ -22,19 +23,74 @@
#include <setjmp.h>
#include <stdlib.h>

/* This doesn't do anything at the moment.
* According to setjmp.h "It is not supported to use mes' setjmp implementation together with GCC."
* We cannot offer this functionality as a C function. It remains to be done (as assembly) for tcc.
*/

void
longjmp (jmp_buf env, int val)
{
exit (42);
val = val == 0 ? 1 : val;
asm(
"ld s0, 0(a0)\n\t"
"ld s1, 8(a0)\n\t"
"ld s2, 16(a0)\n\t"
"ld s3, 24(a0)\n\t"
"ld s4, 32(a0)\n\t"
"ld s5, 40(a0)\n\t"
"ld s6, 48(a0)\n\t"
"ld s7, 56(a0)\n\t"
"ld s8, 64(a0)\n\t"
"ld s9, 72(a0)\n\t"
"ld s10, 80(a0)\n\t"
"ld s11, 88(a0)\n\t"
"ld sp, 96(a0)\n\t"
"ld ra, 104(a0)\n\t"
#ifndef __riscv_float_abi_soft
"fld fs0, 112(a0)\n\t"
"fld fs1, 120(a0)\n\t"
"fld fs2, 128(a0)\n\t"
"fld fs3, 136(a0)\n\t"
"fld fs4, 144(a0)\n\t"
"fld fs5, 152(a0)\n\t"
"fld fs6, 160(a0)\n\t"
"fld fs7, 168(a0)\n\t"
"fld fs8, 176(a0)\n\t"
"fld fs9, 184(a0)\n\t"
"fld fs10, 192(a0)\n\t"
"fld fs11, 200(a0)\n\t"
#endif
);
}

int
setjmp (jmp_buf env)
{
return -1;
asm(
"sd s0, 0(a0)\n\t"
"sd s1, 8(a0)\n\t"
"sd s2, 16(a0)\n\t"
"sd s3, 24(a0)\n\t"
"sd s4, 32(a0)\n\t"
"sd s5, 40(a0)\n\t"
"sd s6, 48(a0)\n\t"
"sd s7, 56(a0)\n\t"
"sd s8, 64(a0)\n\t"
"sd s9, 72(a0)\n\t"
"sd s10, 80(a0)\n\t"
"sd s11, 88(a0)\n\t"
"sd sp, 96(a0)\n\t"
"sd ra, 104(a0)\n\t"
#ifndef __riscv_float_abi_soft
"fsd fs0, 112(a0)\n\t"
"fsd fs1, 120(a0)\n\t"
"fsd fs2, 128(a0)\n\t"
"fsd fs3, 136(a0)\n\t"
"fsd fs4, 144(a0)\n\t"
"fsd fs5, 152(a0)\n\t"
"fsd fs6, 160(a0)\n\t"
"fsd fs7, 168(a0)\n\t"
"fsd fs8, 176(a0)\n\t"
"fsd fs9, 184(a0)\n\t"
"fsd fs10, 192(a0)\n\t"
"fsd fs11, 200(a0)\n\t"
#endif
);
return 0;
}
97 changes: 97 additions & 0 deletions lib/riscv64-mes-tcc/setjmp.c
@@ -0,0 +1,97 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
* Copyright © 2023 Ekaitz Zarraga <ekaitz@elenq.tech>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include <setjmp.h>
#include <stdlib.h>

void
longjmp (jmp_buf env, int val)
{
val = val == 0 ? 1 : val;
asm(
"ld s0, a0,0\n\t"
"ld s1, a0,8\n\t"
"ld s2, a0,16\n\t"
"ld s3, a0,24\n\t"
"ld s4, a0,32\n\t"
"ld s5, a0,40\n\t"
"ld s6, a0,48\n\t"
"ld s7, a0,56\n\t"
"ld s8, a0,64\n\t"
"ld s9, a0,72\n\t"
"ld s10,a0,80\n\t"
"ld s11,a0,88\n\t"
"ld sp, a0,96\n\t"
"ld ra, a0,104\n\t"
#if HAVE_FLOAT_ASM && HAVE_FLOAT && ! __riscv_float_abi_soft
"fld fs0, a0,112\n\t"
"fld fs1, a0,120\n\t"
"fld fs2, a0,128\n\t"
"fld fs3, a0,136\n\t"
"fld fs4, a0,144\n\t"
"fld fs5, a0,152\n\t"
"fld fs6, a0,160\n\t"
"fld fs7, a0,168\n\t"
"fld fs8, a0,176\n\t"
"fld fs9, a0,184\n\t"
"fld fs10,a0,192\n\t"
"fld fs11,a0,200\n\t"
#endif
);
}

int
setjmp (jmp_buf env)
{
asm(
"sd a0, s0,0\n\t"
"sd a0, s1,8\n\t"
"sd a0, s2,16\n\t"
"sd a0, s3,24\n\t"
"sd a0, s4,32\n\t"
"sd a0, s5,40\n\t"
"sd a0, s6,48\n\t"
"sd a0, s7,56\n\t"
"sd a0, s8,64\n\t"
"sd a0, s9,72\n\t"
"sd a0, s10,80\n\t"
"sd a0, s11,88\n\t"
"sd a0, sp,96\n\t"
"sd a0, ra,104\n\t"
#if HAVE_FLOAT_ASM && HAVE_FLOAT && ! __riscv_float_abi_soft
"fsd a0, fs0,112\n\t"
"fsd a0, fs1,120\n\t"
"fsd a0, fs2,128\n\t"
"fsd a0, fs3,136\n\t"
"fsd a0, fs4,144\n\t"
"fsd a0, fs5,152\n\t"
"fsd a0, fs6,160\n\t"
"fsd a0, fs7,168\n\t"
"fsd a0, fs8,176\n\t"
"fsd a0, fs9,184\n\t"
"fsd a0, fs10,192\n\t"
"fsd a0, fs11,200\n\t"
#endif
);
return 0;
}

0 comments on commit 0e2c556

Please sign in to comment.