Skip to content

Commit 0e2c556

Browse files
ekaitz-zarragajanneke
authored andcommitted
lib: Add setjmp/longjmp for riscv64.
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.
1 parent 7150684 commit 0e2c556

File tree

2 files changed

+160
-7
lines changed

2 files changed

+160
-7
lines changed

lib/riscv64-mes-gcc/setjmp.c

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* GNU Mes --- Maxwell Equations of Software
33
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
44
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
5+
* Copyright © 2023 Ekaitz Zarraga <ekaitz@elenq.tech>
56
*
67
* This file is part of GNU Mes.
78
*
@@ -22,19 +23,74 @@
2223
#include <setjmp.h>
2324
#include <stdlib.h>
2425

25-
/* This doesn't do anything at the moment.
26-
* According to setjmp.h "It is not supported to use mes' setjmp implementation together with GCC."
27-
* We cannot offer this functionality as a C function. It remains to be done (as assembly) for tcc.
28-
*/
29-
3026
void
3127
longjmp (jmp_buf env, int val)
3228
{
33-
exit (42);
29+
val = val == 0 ? 1 : val;
30+
asm(
31+
"ld s0, 0(a0)\n\t"
32+
"ld s1, 8(a0)\n\t"
33+
"ld s2, 16(a0)\n\t"
34+
"ld s3, 24(a0)\n\t"
35+
"ld s4, 32(a0)\n\t"
36+
"ld s5, 40(a0)\n\t"
37+
"ld s6, 48(a0)\n\t"
38+
"ld s7, 56(a0)\n\t"
39+
"ld s8, 64(a0)\n\t"
40+
"ld s9, 72(a0)\n\t"
41+
"ld s10, 80(a0)\n\t"
42+
"ld s11, 88(a0)\n\t"
43+
"ld sp, 96(a0)\n\t"
44+
"ld ra, 104(a0)\n\t"
45+
#ifndef __riscv_float_abi_soft
46+
"fld fs0, 112(a0)\n\t"
47+
"fld fs1, 120(a0)\n\t"
48+
"fld fs2, 128(a0)\n\t"
49+
"fld fs3, 136(a0)\n\t"
50+
"fld fs4, 144(a0)\n\t"
51+
"fld fs5, 152(a0)\n\t"
52+
"fld fs6, 160(a0)\n\t"
53+
"fld fs7, 168(a0)\n\t"
54+
"fld fs8, 176(a0)\n\t"
55+
"fld fs9, 184(a0)\n\t"
56+
"fld fs10, 192(a0)\n\t"
57+
"fld fs11, 200(a0)\n\t"
58+
#endif
59+
);
3460
}
3561

3662
int
3763
setjmp (jmp_buf env)
3864
{
39-
return -1;
65+
asm(
66+
"sd s0, 0(a0)\n\t"
67+
"sd s1, 8(a0)\n\t"
68+
"sd s2, 16(a0)\n\t"
69+
"sd s3, 24(a0)\n\t"
70+
"sd s4, 32(a0)\n\t"
71+
"sd s5, 40(a0)\n\t"
72+
"sd s6, 48(a0)\n\t"
73+
"sd s7, 56(a0)\n\t"
74+
"sd s8, 64(a0)\n\t"
75+
"sd s9, 72(a0)\n\t"
76+
"sd s10, 80(a0)\n\t"
77+
"sd s11, 88(a0)\n\t"
78+
"sd sp, 96(a0)\n\t"
79+
"sd ra, 104(a0)\n\t"
80+
#ifndef __riscv_float_abi_soft
81+
"fsd fs0, 112(a0)\n\t"
82+
"fsd fs1, 120(a0)\n\t"
83+
"fsd fs2, 128(a0)\n\t"
84+
"fsd fs3, 136(a0)\n\t"
85+
"fsd fs4, 144(a0)\n\t"
86+
"fsd fs5, 152(a0)\n\t"
87+
"fsd fs6, 160(a0)\n\t"
88+
"fsd fs7, 168(a0)\n\t"
89+
"fsd fs8, 176(a0)\n\t"
90+
"fsd fs9, 184(a0)\n\t"
91+
"fsd fs10, 192(a0)\n\t"
92+
"fsd fs11, 200(a0)\n\t"
93+
#endif
94+
);
95+
return 0;
4096
}

lib/riscv64-mes-tcc/setjmp.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* -*-comment-start: "//";comment-end:""-*-
2+
* GNU Mes --- Maxwell Equations of Software
3+
* Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4+
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
5+
* Copyright © 2023 Ekaitz Zarraga <ekaitz@elenq.tech>
6+
*
7+
* This file is part of GNU Mes.
8+
*
9+
* GNU Mes is free software; you can redistribute it and/or modify it
10+
* under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 3 of the License, or (at
12+
* your option) any later version.
13+
*
14+
* GNU Mes is distributed in the hope that it will be useful, but
15+
* WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
#include <setjmp.h>
24+
#include <stdlib.h>
25+
26+
void
27+
longjmp (jmp_buf env, int val)
28+
{
29+
val = val == 0 ? 1 : val;
30+
asm(
31+
"ld s0, a0,0\n\t"
32+
"ld s1, a0,8\n\t"
33+
"ld s2, a0,16\n\t"
34+
"ld s3, a0,24\n\t"
35+
"ld s4, a0,32\n\t"
36+
"ld s5, a0,40\n\t"
37+
"ld s6, a0,48\n\t"
38+
"ld s7, a0,56\n\t"
39+
"ld s8, a0,64\n\t"
40+
"ld s9, a0,72\n\t"
41+
"ld s10,a0,80\n\t"
42+
"ld s11,a0,88\n\t"
43+
"ld sp, a0,96\n\t"
44+
"ld ra, a0,104\n\t"
45+
#if HAVE_FLOAT_ASM && HAVE_FLOAT && ! __riscv_float_abi_soft
46+
"fld fs0, a0,112\n\t"
47+
"fld fs1, a0,120\n\t"
48+
"fld fs2, a0,128\n\t"
49+
"fld fs3, a0,136\n\t"
50+
"fld fs4, a0,144\n\t"
51+
"fld fs5, a0,152\n\t"
52+
"fld fs6, a0,160\n\t"
53+
"fld fs7, a0,168\n\t"
54+
"fld fs8, a0,176\n\t"
55+
"fld fs9, a0,184\n\t"
56+
"fld fs10,a0,192\n\t"
57+
"fld fs11,a0,200\n\t"
58+
#endif
59+
);
60+
}
61+
62+
int
63+
setjmp (jmp_buf env)
64+
{
65+
asm(
66+
"sd a0, s0,0\n\t"
67+
"sd a0, s1,8\n\t"
68+
"sd a0, s2,16\n\t"
69+
"sd a0, s3,24\n\t"
70+
"sd a0, s4,32\n\t"
71+
"sd a0, s5,40\n\t"
72+
"sd a0, s6,48\n\t"
73+
"sd a0, s7,56\n\t"
74+
"sd a0, s8,64\n\t"
75+
"sd a0, s9,72\n\t"
76+
"sd a0, s10,80\n\t"
77+
"sd a0, s11,88\n\t"
78+
"sd a0, sp,96\n\t"
79+
"sd a0, ra,104\n\t"
80+
#if HAVE_FLOAT_ASM && HAVE_FLOAT && ! __riscv_float_abi_soft
81+
"fsd a0, fs0,112\n\t"
82+
"fsd a0, fs1,120\n\t"
83+
"fsd a0, fs2,128\n\t"
84+
"fsd a0, fs3,136\n\t"
85+
"fsd a0, fs4,144\n\t"
86+
"fsd a0, fs5,152\n\t"
87+
"fsd a0, fs6,160\n\t"
88+
"fsd a0, fs7,168\n\t"
89+
"fsd a0, fs8,176\n\t"
90+
"fsd a0, fs9,184\n\t"
91+
"fsd a0, fs10,192\n\t"
92+
"fsd a0, fs11,200\n\t"
93+
#endif
94+
);
95+
return 0;
96+
}
97+

0 commit comments

Comments
 (0)