update vibration handling
[pcsx_rearmed.git] / deps / lightrec / lightrec-private.h
CommitLineData
98fa08a5 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d16005f8 2/*
98fa08a5 3 * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net>
d16005f8
PC
4 */
5
6#ifndef __LIGHTREC_PRIVATE_H__
7#define __LIGHTREC_PRIVATE_H__
8
02487de7 9#include "lightning-wrapper.h"
98fa08a5 10#include "lightrec-config.h"
d16005f8
PC
11#include "disassembler.h"
12#include "lightrec.h"
ba3814c1 13#include "regcache.h"
d16005f8
PC
14
15#if ENABLE_THREADED_COMPILER
16#include <stdatomic.h>
17#endif
18
13b02197
PC
19#ifdef _MSC_BUILD
20#include <immintrin.h>
21#endif
22
f5ee77ca
PC
23#include <inttypes.h>
24#include <stdint.h>
25
26#define PC_FMT "PC 0x%08"PRIx32
27
d16005f8 28#define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
d16005f8 29
13b02197
PC
30#define GENMASK(h, l) \
31 (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
32
d16005f8
PC
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
98fa08a5
PC
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
684432ad
PC
59#if __has_attribute(__fallthrough__)
60# define fallthrough __attribute__((__fallthrough__))
61#else
62# define fallthrough do {} while (0) /* fallthrough */
63#endif
d8b04acd 64
ba3814c1
PC
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)
13b02197
PC
70# define clz32(x) _lzcnt_u32(x)
71# define ctz32(x) _tzcnt_u32(x)
ba3814c1
PC
72#else
73# define popcount32(x) __builtin_popcount(x)
13b02197
PC
74# define clz32(x) __builtin_clz(x)
75# define ctz32(x) __builtin_ctz(x)
ba3814c1
PC
76#endif
77
d16005f8
PC
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)
a59e5536 82#define BLOCK_IS_DEAD BIT(3)
98fa08a5 83#define BLOCK_IS_MEMSET BIT(4)
ba3814c1 84#define BLOCK_NO_OPCODE_LIST BIT(5)
684432ad 85#define BLOCK_PRELOAD_PC BIT(6)
d16005f8
PC
86
87#define RAM_SIZE 0x200000
88#define BIOS_SIZE 0x80000
89
90#define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
91
98fa08a5
PC
92#define REG_LO 32
93#define REG_HI 33
cb72ea13 94#define REG_TEMP (offsetof(struct lightrec_state, temp_reg) / sizeof(u32))
98fa08a5 95
d16005f8
PC
96/* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
97struct jit_node;
98struct jit_state;
99typedef struct jit_state jit_state_t;
100
101struct blockcache;
102struct recompiler;
103struct regcache;
104struct opcode;
a59e5536 105struct reaper;
d16005f8 106
11357fef
PC
107struct u16x2 {
108#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
109 u16 h, l;
110#else
111 u16 l, h;
112#endif
113};
114
d16005f8
PC
115struct block {
116 jit_state_t *_jit;
d16005f8
PC
117 struct opcode *opcode_list;
118 void (*function)(void);
98fa08a5
PC
119 const u32 *code;
120 struct block *next;
d16005f8
PC
121 u32 pc;
122 u32 hash;
d8b04acd 123 u32 precompile_date;
98fa08a5
PC
124 unsigned int code_size;
125 u16 nb_ops;
d16005f8 126#if ENABLE_THREADED_COMPILER
ba3814c1
PC
127 _Atomic u8 flags;
128#else
129 u8 flags;
d16005f8 130#endif
d16005f8
PC
131};
132
133struct lightrec_branch {
134 struct jit_node *branch;
135 u32 target;
136};
137
138struct lightrec_branch_target {
139 struct jit_node *label;
140 u32 offset;
141};
142
98fa08a5
PC
143enum c_wrappers {
144 C_WRAPPER_RW,
145 C_WRAPPER_RW_GENERIC,
fdf33147 146 C_WRAPPER_MFC,
98fa08a5
PC
147 C_WRAPPER_MTC,
148 C_WRAPPER_CP,
98fa08a5
PC
149 C_WRAPPERS_COUNT,
150};
151
152struct lightrec_cstate {
153 struct lightrec_state *state;
154
d16005f8
PC
155 struct lightrec_branch local_branches[512];
156 struct lightrec_branch_target targets[512];
684432ad 157 u16 movi_temp[32];
d16005f8
PC
158 unsigned int nb_local_branches;
159 unsigned int nb_targets;
98fa08a5
PC
160 unsigned int cycles;
161
162 struct regcache *reg_cache;
cb72ea13
PC
163
164 _Bool no_load_delay;
98fa08a5
PC
165};
166
167struct lightrec_state {
168 struct lightrec_registers regs;
cb72ea13 169 u32 temp_reg;
0e720fb1 170 u32 curr_pc;
98fa08a5 171 u32 next_pc;
9259d748 172 uintptr_t wrapper_regs[NUM_TEMPS];
cb72ea13 173 u8 in_delay_slot_n;
98fa08a5
PC
174 u32 current_cycle;
175 u32 target_cycle;
176 u32 exit_flags;
177 u32 old_cycle_counter;
684432ad 178 u32 cycles_per_op;
98fa08a5 179 struct block *dispatcher, *c_wrapper_block;
fd58fa32
PC
180 void *c_wrappers[C_WRAPPERS_COUNT];
181 void *wrappers_eps[C_WRAPPERS_COUNT];
d16005f8 182 struct blockcache *block_cache;
d16005f8 183 struct recompiler *rec;
98fa08a5 184 struct lightrec_cstate *cstate;
a59e5536 185 struct reaper *reaper;
02487de7 186 void *tlsf;
d16005f8 187 void (*eob_wrapper_func)(void);
cb72ea13
PC
188 void (*interpreter_func)(void);
189 void (*ds_check_func)(void);
98fa08a5 190 void (*memset_func)(void);
d16005f8
PC
191 void (*get_next_block)(void);
192 struct lightrec_ops ops;
a59e5536 193 unsigned int nb_precompile;
cb72ea13 194 unsigned int nb_compile;
d16005f8
PC
195 unsigned int nb_maps;
196 const struct lightrec_mem_map *maps;
ba3814c1 197 uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
684432ad 198 u32 opt_flags;
02487de7 199 _Bool with_32bit_lut;
d16005f8 200 _Bool mirrors_mapped;
d16005f8
PC
201 void *code_lut[];
202};
203
cb72ea13
PC
204u32 lightrec_rw(struct lightrec_state *state, union code op, u32 addr,
205 u32 data, u32 *flags, struct block *block, u16 offset);
d16005f8 206
98fa08a5 207void lightrec_free_block(struct lightrec_state *state, struct block *block);
d16005f8
PC
208
209void remove_from_code_lut(struct blockcache *cache, struct block *block);
210
98fa08a5
PC
211const struct lightrec_mem_map *
212lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
213
d16005f8
PC
214static inline u32 kunseg(u32 addr)
215{
216 if (unlikely(addr >= 0xa0000000))
217 return addr - 0xa0000000;
218 else
219 return addr &~ 0x80000000;
220}
221
222static 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
02487de7
PC
230static inline _Bool is_big_endian(void)
231{
232 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
233}
234
235static 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
241static inline size_t lut_elm_size(const struct lightrec_state *state)
242{
243 return lut_is_32bit(state) ? 4 : sizeof(void *);
244}
245
246static 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
254static inline void * lut_read(struct lightrec_state *state, u32 offset)
255{
d8b04acd 256 void **lut_entry = lut_address(state, offset);
02487de7
PC
257
258 if (lut_is_32bit(state))
259 return (void *)(uintptr_t) *(u32 *) lut_entry;
260 else
261 return *lut_entry;
262}
263
264static 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
98fa08a5
PC
274static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
275{
276 u16 flags = block->opcode_list[offset].flags;
277
03535202 278 offset += op_flag_no_ds(flags);
98fa08a5 279
684432ad 280 return block->pc + ((offset + imm) << 2);
98fa08a5
PC
281}
282
283static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
284{
285 u16 flags = block->opcode_list[offset].flags;
286
03535202 287 offset -= op_flag_no_ds(flags);
98fa08a5 288
684432ad 289 return block->pc + ((offset + imm) << 2);
98fa08a5
PC
290}
291
9259d748 292void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data);
d16005f8 293u32 lightrec_mfc(struct lightrec_state *state, union code op);
98fa08a5
PC
294void lightrec_rfe(struct lightrec_state *state);
295void lightrec_cp(struct lightrec_state *state, union code op);
296
297struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
298void lightrec_free_cstate(struct lightrec_cstate *cstate);
d16005f8
PC
299
300union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
301
98fa08a5 302int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
ba3814c1
PC
303void lightrec_free_opcode_list(struct lightrec_state *state,
304 struct opcode *list);
98fa08a5 305
684432ad
PC
306unsigned int lightrec_cycles_of_opcode(const struct lightrec_state *state,
307 union code code);
98fa08a5
PC
308
309static 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
314static 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}
d16005f8 318
03535202
PC
319static inline s16 s16_max(s16 a, s16 b)
320{
321 return a > b ? a : b;
322}
323
ba3814c1
PC
324static 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
333static 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
347static 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
9259d748
PC
361static inline _Bool can_sign_extend(s32 value, u8 order)
362{
684432ad 363 return ((u32)(value >> (order - 1)) + 1) < 2;
9259d748
PC
364}
365
366static inline _Bool can_zero_extend(u32 value, u8 order)
367{
368 return (value >> order) == 0;
369}
370
cb72ea13
PC
371static inline const struct opcode *
372get_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
0e720fb1
PC
377static inline _Bool lightrec_store_next_pc(void)
378{
379 return NUM_REGS + NUM_TEMPS <= 4;
380}
381
d16005f8 382#endif /* __LIGHTREC_PRIVATE_H__ */