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