Update lightrec 20220910 (#686)
[pcsx_rearmed.git] / deps / lightrec / lightrec-private.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net>
4  */
5
6 #ifndef __LIGHTREC_PRIVATE_H__
7 #define __LIGHTREC_PRIVATE_H__
8
9 #include "lightning-wrapper.h"
10 #include "lightrec-config.h"
11 #include "disassembler.h"
12 #include "lightrec.h"
13 #include "regcache.h"
14
15 #if ENABLE_THREADED_COMPILER
16 #include <stdatomic.h>
17 #endif
18
19 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
20
21 #ifdef __GNUC__
22 #       define likely(x)       __builtin_expect(!!(x),1)
23 #       define unlikely(x)     __builtin_expect(!!(x),0)
24 #else
25 #       define likely(x)       (x)
26 #       define unlikely(x)     (x)
27 #endif
28
29 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
30 #       define LE32TOH(x)       __builtin_bswap32(x)
31 #       define HTOLE32(x)       __builtin_bswap32(x)
32 #       define LE16TOH(x)       __builtin_bswap16(x)
33 #       define HTOLE16(x)       __builtin_bswap16(x)
34 #else
35 #       define LE32TOH(x)       (x)
36 #       define HTOLE32(x)       (x)
37 #       define LE16TOH(x)       (x)
38 #       define HTOLE16(x)       (x)
39 #endif
40
41 #if HAS_DEFAULT_ELM
42 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
43 #else
44 #define SET_DEFAULT_ELM(table, value) [0] = NULL
45 #endif
46
47 #define fallthrough do {} while (0) /* fall-through */
48
49 #define container_of(ptr, type, member) \
50         ((type *)((void *)(ptr) - offsetof(type, member)))
51
52 #ifdef _MSC_BUILD
53 #       define popcount32(x)    __popcnt(x)
54 #       define ffs32(x)         (31 - __lzcnt(x))
55 #else
56 #       define popcount32(x)    __builtin_popcount(x)
57 #       define ffs32(x)         (__builtin_ffs(x) - 1)
58 #endif
59
60 /* Flags for (struct block *)->flags */
61 #define BLOCK_NEVER_COMPILE     BIT(0)
62 #define BLOCK_SHOULD_RECOMPILE  BIT(1)
63 #define BLOCK_FULLY_TAGGED      BIT(2)
64 #define BLOCK_IS_DEAD           BIT(3)
65 #define BLOCK_IS_MEMSET         BIT(4)
66 #define BLOCK_NO_OPCODE_LIST    BIT(5)
67
68 #define RAM_SIZE        0x200000
69 #define BIOS_SIZE       0x80000
70
71 #define CODE_LUT_SIZE   ((RAM_SIZE + BIOS_SIZE) >> 2)
72
73 #define REG_LO 32
74 #define REG_HI 33
75
76 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
77 struct jit_node;
78 struct jit_state;
79 typedef struct jit_state jit_state_t;
80
81 struct blockcache;
82 struct recompiler;
83 struct regcache;
84 struct opcode;
85 struct reaper;
86
87 struct u16x2 {
88 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
89         u16 h, l;
90 #else
91         u16 l, h;
92 #endif
93 };
94
95 struct block {
96         jit_state_t *_jit;
97         struct opcode *opcode_list;
98         void (*function)(void);
99         const u32 *code;
100         struct block *next;
101         u32 pc;
102         u32 hash;
103         u32 precompile_date;
104         unsigned int code_size;
105         u16 nb_ops;
106 #if ENABLE_THREADED_COMPILER
107         _Atomic u8 flags;
108 #else
109         u8 flags;
110 #endif
111 };
112
113 struct lightrec_branch {
114         struct jit_node *branch;
115         u32 target;
116 };
117
118 struct lightrec_branch_target {
119         struct jit_node *label;
120         u32 offset;
121 };
122
123 enum c_wrappers {
124         C_WRAPPER_RW,
125         C_WRAPPER_RW_GENERIC,
126         C_WRAPPER_MTC,
127         C_WRAPPER_CP,
128         C_WRAPPERS_COUNT,
129 };
130
131 struct lightrec_cstate {
132         struct lightrec_state *state;
133
134         struct lightrec_branch local_branches[512];
135         struct lightrec_branch_target targets[512];
136         unsigned int nb_local_branches;
137         unsigned int nb_targets;
138         unsigned int cycles;
139
140         struct regcache *reg_cache;
141 };
142
143 struct lightrec_state {
144         struct lightrec_registers regs;
145         uintptr_t wrapper_regs[NUM_TEMPS];
146         u32 next_pc;
147         u32 current_cycle;
148         u32 target_cycle;
149         u32 exit_flags;
150         u32 old_cycle_counter;
151         struct block *dispatcher, *c_wrapper_block;
152         void *c_wrappers[C_WRAPPERS_COUNT];
153         void *wrappers_eps[C_WRAPPERS_COUNT];
154         struct blockcache *block_cache;
155         struct recompiler *rec;
156         struct lightrec_cstate *cstate;
157         struct reaper *reaper;
158         void *tlsf;
159         void (*eob_wrapper_func)(void);
160         void (*memset_func)(void);
161         void (*get_next_block)(void);
162         struct lightrec_ops ops;
163         unsigned int nb_precompile;
164         unsigned int nb_maps;
165         const struct lightrec_mem_map *maps;
166         uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
167         _Bool with_32bit_lut;
168         _Bool mirrors_mapped;
169         _Bool invalidate_from_dma_only;
170         void *code_lut[];
171 };
172
173 u32 lightrec_rw(struct lightrec_state *state, union code op,
174                 u32 addr, u32 data, u32 *flags,
175                 struct block *block);
176
177 void lightrec_free_block(struct lightrec_state *state, struct block *block);
178
179 void remove_from_code_lut(struct blockcache *cache, struct block *block);
180
181 enum psx_map
182 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
183
184 const struct lightrec_mem_map *
185 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
186
187 static inline u32 kunseg(u32 addr)
188 {
189         if (unlikely(addr >= 0xa0000000))
190                 return addr - 0xa0000000;
191         else
192                 return addr &~ 0x80000000;
193 }
194
195 static inline u32 lut_offset(u32 pc)
196 {
197         if (pc & BIT(28))
198                 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
199         else
200                 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
201 }
202
203 static inline _Bool is_big_endian(void)
204 {
205         return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
206 }
207
208 static inline _Bool lut_is_32bit(const struct lightrec_state *state)
209 {
210         return __WORDSIZE == 32 ||
211                 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
212 }
213
214 static inline size_t lut_elm_size(const struct lightrec_state *state)
215 {
216         return lut_is_32bit(state) ? 4 : sizeof(void *);
217 }
218
219 static inline void ** lut_address(struct lightrec_state *state, u32 offset)
220 {
221         if (lut_is_32bit(state))
222                 return (void **) ((uintptr_t) state->code_lut + offset * 4);
223         else
224                 return &state->code_lut[offset];
225 }
226
227 static inline void * lut_read(struct lightrec_state *state, u32 offset)
228 {
229         void **lut_entry = lut_address(state, offset);
230
231         if (lut_is_32bit(state))
232                 return (void *)(uintptr_t) *(u32 *) lut_entry;
233         else
234                 return *lut_entry;
235 }
236
237 static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
238 {
239         void **lut_entry = lut_address(state, offset);
240
241         if (lut_is_32bit(state))
242                 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
243         else
244                 *lut_entry = ptr;
245 }
246
247 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
248 {
249         u16 flags = block->opcode_list[offset].flags;
250
251         offset += op_flag_no_ds(flags);
252
253         return block->pc + (offset + imm << 2);
254 }
255
256 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
257 {
258         u16 flags = block->opcode_list[offset].flags;
259
260         offset -= op_flag_no_ds(flags);
261
262         return block->pc + (offset + imm << 2);
263 }
264
265 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
266 u32 lightrec_mfc(struct lightrec_state *state, union code op);
267 void lightrec_rfe(struct lightrec_state *state);
268 void lightrec_cp(struct lightrec_state *state, union code op);
269
270 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
271 void lightrec_free_cstate(struct lightrec_cstate *cstate);
272
273 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
274
275 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
276 void lightrec_free_opcode_list(struct lightrec_state *state,
277                                struct opcode *list);
278
279 unsigned int lightrec_cycles_of_opcode(union code code);
280
281 static inline u8 get_mult_div_lo(union code c)
282 {
283         return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
284 }
285
286 static inline u8 get_mult_div_hi(union code c)
287 {
288         return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
289 }
290
291 static inline s16 s16_max(s16 a, s16 b)
292 {
293         return a > b ? a : b;
294 }
295
296 static inline _Bool block_has_flag(struct block *block, u8 flag)
297 {
298 #if ENABLE_THREADED_COMPILER
299         return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
300 #else
301         return block->flags & flag;
302 #endif
303 }
304
305 static inline u8 block_set_flags(struct block *block, u8 mask)
306 {
307 #if ENABLE_THREADED_COMPILER
308         return atomic_fetch_or_explicit(&block->flags, mask,
309                                         memory_order_relaxed);
310 #else
311         u8 flags = block->flags;
312
313         block->flags |= mask;
314
315         return flags;
316 #endif
317 }
318
319 static inline u8 block_clear_flags(struct block *block, u8 mask)
320 {
321 #if ENABLE_THREADED_COMPILER
322         return atomic_fetch_and_explicit(&block->flags, ~mask,
323                                          memory_order_relaxed);
324 #else
325         u8 flags = block->flags;
326
327         block->flags &= ~mask;
328
329         return flags;
330 #endif
331 }
332
333 #endif /* __LIGHTREC_PRIVATE_H__ */