git subrepo pull --force deps/lightning
[pcsx_rearmed.git] / deps / lightning / lib / jit_rewind.c
1 /*
2  * Copyright (C) 2015-2022  Free Software Foundation, Inc.
3  *
4  * This file is part of GNU lightning.
5  *
6  * GNU lightning is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU lightning is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * Authors:
17  *      Paulo Cesar Pereira de Andrade
18  */
19
20 #include <lightning.h>
21 #include <lightning/jit_private.h>
22
23 #if PROTO
24 #  define free_synth_list(node)         _free_synth_list(_jit,node)
25 static jit_node_t *_free_synth_list(jit_state_t*,jit_node_t*);
26 #define rewind_prolog()                 _rewind_prolog(_jit)
27 static void _rewind_prolog(jit_state_t*);
28 #define rewind_prepare()                _rewind_prepare(_jit)
29 static void _rewind_prepare(jit_state_t*);
30 #endif
31
32 #if CODE
33 /*
34  * Implementation
35  */
36 static jit_node_t *
37 _free_synth_list(jit_state_t *_jit, jit_node_t *node)
38 {
39     jit_node_t          *next;
40     next = node->next;
41     free_node(node);
42     for (node = next; node && (node->flag & jit_flag_synth); node = next) {
43         next = node->next;
44         free_node(node);
45     }
46     return (next);
47 }
48
49 static void
50 _rewind_prolog(jit_state_t *_jit)
51 {
52     jit_node_t          *node;
53     jit_node_t          *next;
54     _jitc->function->self.size = stack_framesize;
55 #if __arm__
56     assert(jit_cpu.abi);
57     _jitc->function->self.size += 64;
58 #endif
59 #if __mips__ && NEW_ABI
60     /* Only add extra stack space if there are varargs
61      * arguments in registers. */
62     assert(jit_arg_reg_p(_jitc->function->self.argi));
63     _jitc->function->self.size += 64;
64 #endif
65     _jitc->function->self.argi =
66         _jitc->function->self.argf = _jitc->function->self.argn = 0;
67     _jitc->tail = _jitc->function->prolog;
68     node = _jitc->tail->next;
69     _jitc->tail->next = (jit_node_t *)0;
70     _jitc->tail->link = (jit_node_t *)0;
71     for (; node; node = next) {
72         next = node->next;
73         switch (node->code) {
74             case jit_code_arg:
75                 node->next = (jit_node_t *)0;
76                 jit_make_arg(node);
77                 break;
78             case jit_code_arg_f:
79                 node->next = (jit_node_t *)0;
80                 jit_make_arg_f(node);
81                 break;
82             case jit_code_arg_d:
83                 node->next = (jit_node_t *)0;
84                 jit_make_arg_d(node);
85                 break;
86             case jit_code_getarg_c:
87                 jit_getarg_c(node->u.w, node->v.n);
88                 next = free_synth_list(node);
89                 break;
90             case jit_code_getarg_uc:
91                 jit_getarg_uc(node->u.w, node->v.n);
92                 next = free_synth_list(node);
93                 break;
94             case jit_code_getarg_s:
95                 jit_getarg_s(node->u.w, node->v.n);
96                 next = free_synth_list(node);
97                 break;
98             case jit_code_getarg_us:
99                 jit_getarg_us(node->u.w, node->v.n);
100                 next = free_synth_list(node);
101                 break;
102             case jit_code_getarg_i:
103                 jit_getarg_i(node->u.w, node->v.n);
104                 next = free_synth_list(node);
105                 break;
106             case jit_code_getarg_f:
107                 jit_getarg_f(node->u.w, node->v.n);
108                 next = free_synth_list(node);
109                 break;
110             case jit_code_getarg_d:
111                 jit_getarg_d(node->u.w, node->v.n);
112                 next = free_synth_list(node);
113                 break;
114             case jit_code_putargr:
115                 jit_putargr(node->u.w, node->v.n);
116                 next = free_synth_list(node);
117                 break;
118             case jit_code_putargi:
119                 jit_putargi(node->u.w, node->v.n);
120                 next = free_synth_list(node);
121                 break;
122             case jit_code_putargr_f:
123                 jit_putargr_f(node->u.w, node->v.n);
124                 next = free_synth_list(node);
125                 break;
126             case jit_code_putargi_f:
127                 jit_putargi_f(node->u.f, node->v.n);
128                 next = free_synth_list(node);
129                 break;
130             case jit_code_putargr_d:
131                 jit_putargr_d(node->u.w, node->v.n);
132                 next = free_synth_list(node);
133                 break;
134             case jit_code_putargi_d:
135                 jit_putargi_d(node->u.d, node->v.n);
136                 next = free_synth_list(node);
137                 break;
138             default:
139                 node->next = (jit_node_t *)0;
140                 link_node(node);
141                 break;
142         }
143     }
144 }
145
146 static void
147 _rewind_prepare(jit_state_t *_jit)
148 {
149     jit_node_t          *node;
150     jit_node_t          *next;
151     _jitc->function->call.argi =
152         _jitc->function->call.argf =
153         _jitc->function->call.size = 0;
154     _jitc->tail = _jitc->prepare;
155     node = _jitc->tail->next;
156     _jitc->tail->next = (jit_node_t *)0;
157     _jitc->tail->link = (jit_node_t *)0;
158     for (; node; node = next) {
159         next = node->next;
160         switch (node->code) {
161             case jit_code_pushargr:
162                 jit_pushargr(node->u.w);
163                 next = free_synth_list(node);
164                 break;
165             case jit_code_pushargi:
166                 jit_pushargi(node->u.w);
167                 next = free_synth_list(node);
168                 break;
169             case jit_code_pushargr_f:
170                 jit_pushargr_f(node->u.w);
171                 next = free_synth_list(node);
172                 break;
173             case jit_code_pushargi_f:
174                 jit_pushargi_f(node->u.f);
175                 next = free_synth_list(node);
176                 break;
177             case jit_code_pushargr_d:
178                 jit_pushargr_d(node->u.w);
179                 next = free_synth_list(node);
180                 break;
181             case jit_code_pushargi_d:
182                 jit_pushargi_d(node->u.d);
183                 next = free_synth_list(node);
184                 break;
185             default:
186                 node->next = (jit_node_t *)0;
187                 link_node(node);
188                 break;
189         }
190     }
191 }
192 #endif