Debian Codegraph @

rpython/translator/c/src/stacklet/switch_riscv_gcc.h

raw symbols SCIP
1 #if __riscv_xlen != 64 || __riscv_flen != 64
2 #error "slp_switch only supports RV64IMAD now"
3 #endif
4
5 static void *slp_switch(void *(*save_state)(void*, void*),
6 void *(*restore_state)(void*, void*),
7 void *extra) __attribute__((noinline));
8
9 static void *slp_switch(void *(*save_state)(void*, void*),
10 void *(*restore_state)(void*, void*),
11 void *extra)
12 {
13 void *result;
14 /*
15 * Registers to preserve (callee-saved registers):
16 * x8-9, x18-27, f8-9, f18-27
17 *
18 * Registers marked as clobbered (caller-saved registers):
19 * x1 (ra), x5-7, x10-17, x28-31, f0-7, f10-17, f28-f31
20 *
21 * Special registers:
22 *
23 * x0: Constant zero
24 *
25 * x2: Stack pointer is a callee-saved register. It will be preserved
26 * around function calls to `save_state` and `restore_state`. And this
27 * inline assembly preserve it by sub/add it with the same amount.
28 *
29 * x3: gp (don't use, gcc doesn't change them either)
30 * x4: tp (don't use, gcc doesn't change them either)
31 *
32 * Don't assume gcc saves any register for us when generating code for
33 * slp_switch().
34 *
35 * The values `save_state`, `restore_state` and `extra` are first moved by
36 * gcc to some registers that are not marked as clobbered (some callee-saved
37 * registers). Similarly, gcc expects `result` to be in a register between
38 * some callee-saved registers.
39 *
40 * This means that three of the values we happen to save and restore will, in
41 * fact, contain the three arguments, and one of these values will, in fact,
42 * not be restored at all but receive `result`.
43 */
44
45 __asm__ volatile (
46 /* Note: The stack is supposed to be aligned as necessary already. */
47
48 /* Save callee-saved registers. */
49 "addi sp, sp, -192\n"
50 "sd x8, 0(sp)\n"
51 "sd x9, 8(sp)\n"
52 "sd x18, 16(sp)\n"
53 "sd x19, 24(sp)\n"
54 "sd x20, 32(sp)\n"
55 "sd x21, 40(sp)\n"
56 "sd x22, 48(sp)\n"
57 "sd x23, 56(sp)\n"
58 "sd x24, 64(sp)\n"
59 "sd x25, 72(sp)\n"
60 "sd x26, 80(sp)\n"
61 "sd x27, 88(sp)\n"
62 "fsd f8, 96(sp)\n"
63 "fsd f9, 104(sp)\n"
64 "fsd f18, 112(sp)\n"
65 "fsd f19, 120(sp)\n"
66 "fsd f20, 128(sp)\n"
67 "fsd f21, 136(sp)\n"
68 "fsd f22, 144(sp)\n"
69 "fsd f23, 152(sp)\n"
70 "fsd f24, 160(sp)\n"
71 "fsd f25, 168(sp)\n"
72 "fsd f26, 176(sp)\n"
73 "fsd f27, 184(sp)\n"
74
75 "mv x10, sp\n" /* arg 1: current (old) stack pointer */
76 "mv x11, %[extra]\n" /* arg 2: extra */
77 "jalr ra, %[save_state], 0\n" /* call save_state() */
78
79 /* Skip the rest if the return value is null. */
80 "beqz x10, 0f\n"
81
82 "mv sp, x10\n" /* change the stack pointer */
83
84 /* From now on, the stack pointer is modified, but the content of the
85 * stack is not restored yet. It contains only garbage here.
86 */
87 /* arg 1: current (new) stack pointer is already in x10 */
88 "mv x11, %[extra]\n" /* arg 2: extra */
89 "jalr ra, %[restore_state], 0\n" /* call restore_state() */
90
91 /* The stack's content is now restored. */
92 "0:\n"
93
94 /* Restore all saved registers */
95 "ld x8, 0(sp)\n"
96 "ld x9, 8(sp)\n"
97 "ld x18, 16(sp)\n"
98 "ld x19, 24(sp)\n"
99 "ld x20, 32(sp)\n"
100 "ld x21, 40(sp)\n"
101 "ld x22, 48(sp)\n"
102 "ld x23, 56(sp)\n"
103 "ld x24, 64(sp)\n"
104 "ld x25, 72(sp)\n"
105 "ld x26, 80(sp)\n"
106 "ld x27, 88(sp)\n"
107 "fld f8, 96(sp)\n"
108 "fld f9, 104(sp)\n"
109 "fld f18, 112(sp)\n"
110 "fld f19, 120(sp)\n"
111 "fld f20, 128(sp)\n"
112 "fld f21, 136(sp)\n"
113 "fld f22, 144(sp)\n"
114 "fld f23, 152(sp)\n"
115 "fld f24, 160(sp)\n"
116 "fld f25, 168(sp)\n"
117 "fld f26, 176(sp)\n"
118 "fld f27, 184(sp)\n"
119 "addi sp, sp, 192\n"
120
121 /* Move x10 into the final location of `result`. */
122 "mv %[result], x10\n"
123
124 : /* Output variables */
125 [result]"=r"(result)
126 : /* Input variables */
127 [restore_state]"r"(restore_state),
128 [save_state]"r"(save_state),
129 [extra]"r"(extra)
130 : /* Clobber caller-saved core registers */
131 "x1", "x5", "x6", "x7",
132 "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17",
133 "x28", "x29", "x30", "x31",
134 /* Clobber caller-saved float registers */
135 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
136 "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17",
137 "f28", "f29", "f30", "f31",
138 "memory"
139 );
140
141 return result;
142 }