Merge pull request #836 from pcercuei/update-lightrec-20240611
[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 #ifdef _MSC_BUILD
20 #include <immintrin.h>
21 #endif
22
23 #include <inttypes.h>
24 #include <stdint.h>
25
26 #define X32_FMT "0x%08"PRIx32
27 #define PC_FMT "PC "X32_FMT
28
29 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
30
31 #define GENMASK(h, l) \
32         (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
33
34 #ifdef __GNUC__
35 #       define likely(x)       __builtin_expect(!!(x),1)
36 #       define unlikely(x)     __builtin_expect(!!(x),0)
37 #else
38 #       define likely(x)       (x)
39 #       define unlikely(x)     (x)
40 #endif
41
42 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
43 #       define LE32TOH(x)       __builtin_bswap32(x)
44 #       define HTOLE32(x)       __builtin_bswap32(x)
45 #       define LE16TOH(x)       __builtin_bswap16(x)
46 #       define HTOLE16(x)       __builtin_bswap16(x)
47 #else
48 #       define LE32TOH(x)       (x)
49 #       define HTOLE32(x)       (x)
50 #       define LE16TOH(x)       (x)
51 #       define HTOLE16(x)       (x)
52 #endif
53
54 #if HAS_DEFAULT_ELM
55 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
56 #else
57 #define SET_DEFAULT_ELM(table, value) [0] = NULL
58 #endif
59
60 #if __has_attribute(__fallthrough__)
61 #       define fallthrough      __attribute__((__fallthrough__))
62 #else
63 #       define fallthrough      do {} while (0)  /* fallthrough */
64 #endif
65
66 #define container_of(ptr, type, member) \
67         ((type *)((void *)(ptr) - offsetof(type, member)))
68
69 #ifdef _MSC_BUILD
70 #       define popcount32(x)    __popcnt(x)
71 #       define clz32(x)         _lzcnt_u32(x)
72 #       define ctz32(x)         _tzcnt_u32(x)
73 #else
74 #       define popcount32(x)    __builtin_popcount(x)
75 #       define clz32(x)         __builtin_clz(x)
76 #       define ctz32(x)         __builtin_ctz(x)
77 #endif
78
79 /* Flags for (struct block *)->flags */
80 #define BLOCK_NEVER_COMPILE     BIT(0)
81 #define BLOCK_SHOULD_RECOMPILE  BIT(1)
82 #define BLOCK_FULLY_TAGGED      BIT(2)
83 #define BLOCK_IS_DEAD           BIT(3)
84 #define BLOCK_IS_MEMSET         BIT(4)
85 #define BLOCK_NO_OPCODE_LIST    BIT(5)
86 #define BLOCK_PRELOAD_PC        BIT(6)
87
88 #define RAM_SIZE        0x200000
89 #define BIOS_SIZE       0x80000
90
91 #define CODE_LUT_SIZE   ((RAM_SIZE + BIOS_SIZE) >> 2)
92
93 #define REG_LO 32
94 #define REG_HI 33
95 #define REG_TEMP (offsetof(struct lightrec_state, temp_reg) / sizeof(u32))
96
97 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
98 struct jit_node;
99 struct jit_state;
100 typedef struct jit_state jit_state_t;
101
102 struct blockcache;
103 struct recompiler;
104 struct regcache;
105 struct opcode;
106 struct reaper;
107
108 struct u16x2 {
109 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
110         u16 h, l;
111 #else
112         u16 l, h;
113 #endif
114 };
115
116 struct block {
117         jit_state_t *_jit;
118         struct opcode *opcode_list;
119         void (*function)(void);
120         const u32 *code;
121         struct block *next;
122         u32 pc;
123         u32 hash;
124         u32 precompile_date;
125         unsigned int code_size;
126         u16 nb_ops;
127 #if ENABLE_THREADED_COMPILER
128         _Atomic u8 flags;
129 #else
130         u8 flags;
131 #endif
132 };
133
134 struct lightrec_branch {
135         struct jit_node *branch;
136         u32 target;
137 };
138
139 struct lightrec_branch_target {
140         struct jit_node *label;
141         u32 offset;
142 };
143
144 enum c_wrappers {
145         C_WRAPPER_RW,
146         C_WRAPPER_RW_GENERIC,
147         C_WRAPPER_MFC,
148         C_WRAPPER_MTC,
149         C_WRAPPER_CP,
150         C_WRAPPERS_COUNT,
151 };
152
153 struct lightrec_cstate {
154         struct lightrec_state *state;
155
156         struct lightrec_branch local_branches[512];
157         struct lightrec_branch_target targets[512];
158         u16 movi_temp[32];
159         unsigned int nb_local_branches;
160         unsigned int nb_targets;
161         unsigned int cycles;
162
163         struct regcache *reg_cache;
164
165         _Bool no_load_delay;
166 };
167
168 struct lightrec_state {
169         struct lightrec_registers regs;
170         u32 temp_reg;
171         u32 curr_pc;
172         u32 next_pc;
173         uintptr_t wrapper_regs[NUM_TEMPS];
174         u8 in_delay_slot_n;
175         u32 current_cycle;
176         u32 target_cycle;
177         u32 exit_flags;
178         u32 old_cycle_counter;
179         u32 cycles_per_op;
180         void *c_wrapper;
181         struct block *dispatcher, *c_wrapper_block;
182         void *c_wrappers[C_WRAPPERS_COUNT];
183         struct blockcache *block_cache;
184         struct recompiler *rec;
185         struct lightrec_cstate *cstate;
186         struct reaper *reaper;
187         void *tlsf;
188         void (*eob_wrapper_func)(void);
189         void (*interpreter_func)(void);
190         void (*ds_check_func)(void);
191         void (*memset_func)(void);
192         void (*get_next_block)(void);
193         struct lightrec_ops ops;
194         unsigned int nb_precompile;
195         unsigned int nb_compile;
196         unsigned int nb_maps;
197         const struct lightrec_mem_map *maps;
198         uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
199         u32 opt_flags;
200         _Bool with_32bit_lut;
201         _Bool mirrors_mapped;
202         void *code_lut[];
203 };
204
205 #define lightrec_offset(ptr) \
206         offsetof(struct lightrec_state, ptr)
207
208 u32 lightrec_rw(struct lightrec_state *state, union code op, u32 addr,
209                 u32 data, u32 *flags, struct block *block, u16 offset);
210
211 void lightrec_free_block(struct lightrec_state *state, struct block *block);
212
213 void remove_from_code_lut(struct blockcache *cache, struct block *block);
214
215 const struct lightrec_mem_map *
216 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
217
218 static inline u32 kunseg(u32 addr)
219 {
220         if (unlikely(addr >= 0xa0000000))
221                 return addr - 0xa0000000;
222         else
223                 return addr &~ 0x80000000;
224 }
225
226 static inline u32 lut_offset(u32 pc)
227 {
228         if (pc & BIT(28))
229                 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
230         else
231                 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
232 }
233
234 static inline _Bool is_big_endian(void)
235 {
236         return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
237 }
238
239 static inline _Bool lut_is_32bit(const struct lightrec_state *state)
240 {
241         return __WORDSIZE == 32 ||
242                 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
243 }
244
245 static inline size_t lut_elm_size(const struct lightrec_state *state)
246 {
247         return lut_is_32bit(state) ? 4 : sizeof(void *);
248 }
249
250 static inline void ** lut_address(struct lightrec_state *state, u32 offset)
251 {
252         if (lut_is_32bit(state))
253                 return (void **) ((uintptr_t) state->code_lut + offset * 4);
254         else
255                 return &state->code_lut[offset];
256 }
257
258 static inline void * lut_read(struct lightrec_state *state, u32 offset)
259 {
260         void **lut_entry = lut_address(state, offset);
261
262         if (lut_is_32bit(state))
263                 return (void *)(uintptr_t) *(u32 *) lut_entry;
264         else
265                 return *lut_entry;
266 }
267
268 static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
269 {
270         void **lut_entry = lut_address(state, offset);
271
272         if (lut_is_32bit(state))
273                 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
274         else
275                 *lut_entry = ptr;
276 }
277
278 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
279 {
280         u16 flags = block->opcode_list[offset].flags;
281
282         offset += op_flag_no_ds(flags);
283
284         return block->pc + ((offset + imm) << 2);
285 }
286
287 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
288 {
289         u16 flags = block->opcode_list[offset].flags;
290
291         offset -= op_flag_no_ds(flags);
292
293         return block->pc + ((offset + imm) << 2);
294 }
295
296 void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data);
297 u32 lightrec_mfc(struct lightrec_state *state, union code op);
298 void lightrec_rfe(struct lightrec_state *state);
299 void lightrec_cp(struct lightrec_state *state, union code op);
300
301 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
302 void lightrec_free_cstate(struct lightrec_cstate *cstate);
303
304 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
305
306 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
307 void lightrec_free_opcode_list(struct lightrec_state *state,
308                                struct opcode *list);
309
310 unsigned int lightrec_cycles_of_opcode(const struct lightrec_state *state,
311                                        union code code);
312
313 static inline u8 get_mult_div_lo(union code c)
314 {
315         return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
316 }
317
318 static inline u8 get_mult_div_hi(union code c)
319 {
320         return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
321 }
322
323 static inline s16 s16_max(s16 a, s16 b)
324 {
325         return a > b ? a : b;
326 }
327
328 static inline _Bool block_has_flag(struct block *block, u8 flag)
329 {
330 #if ENABLE_THREADED_COMPILER
331         return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
332 #else
333         return block->flags & flag;
334 #endif
335 }
336
337 static inline u8 block_set_flags(struct block *block, u8 mask)
338 {
339 #if ENABLE_THREADED_COMPILER
340         return atomic_fetch_or_explicit(&block->flags, mask,
341                                         memory_order_relaxed);
342 #else
343         u8 flags = block->flags;
344
345         block->flags |= mask;
346
347         return flags;
348 #endif
349 }
350
351 static inline u8 block_clear_flags(struct block *block, u8 mask)
352 {
353 #if ENABLE_THREADED_COMPILER
354         return atomic_fetch_and_explicit(&block->flags, ~mask,
355                                          memory_order_relaxed);
356 #else
357         u8 flags = block->flags;
358
359         block->flags &= ~mask;
360
361         return flags;
362 #endif
363 }
364
365 static inline _Bool can_sign_extend(s32 value, u8 order)
366 {
367       return ((u32)(value >> (order - 1)) + 1) < 2;
368 }
369
370 static inline _Bool can_zero_extend(u32 value, u8 order)
371 {
372       return (value >> order) == 0;
373 }
374
375 static inline const struct opcode *
376 get_delay_slot(const struct opcode *list, u16 i)
377 {
378         return op_flag_no_ds(list[i].flags) ? &list[i - 1] : &list[i + 1];
379 }
380
381 static inline _Bool lightrec_store_next_pc(void)
382 {
383         return NUM_REGS + NUM_TEMPS <= 4;
384 }
385
386 #endif /* __LIGHTREC_PRIVATE_H__ */