git subrepo pull --force deps/lightrec
[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
19#define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
d16005f8
PC
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
98fa08a5
PC
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
d8b04acd
PC
47#define fallthrough do {} while (0) /* fall-through */
48
ba3814c1
PC
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
d16005f8
PC
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)
a59e5536 64#define BLOCK_IS_DEAD BIT(3)
98fa08a5 65#define BLOCK_IS_MEMSET BIT(4)
ba3814c1 66#define BLOCK_NO_OPCODE_LIST BIT(5)
d16005f8
PC
67
68#define RAM_SIZE 0x200000
69#define BIOS_SIZE 0x80000
70
71#define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
72
98fa08a5
PC
73#define REG_LO 32
74#define REG_HI 33
75
d16005f8
PC
76/* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
77struct jit_node;
78struct jit_state;
79typedef struct jit_state jit_state_t;
80
81struct blockcache;
82struct recompiler;
83struct regcache;
84struct opcode;
a59e5536 85struct reaper;
d16005f8 86
11357fef
PC
87struct u16x2 {
88#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
89 u16 h, l;
90#else
91 u16 l, h;
92#endif
93};
94
d16005f8
PC
95struct block {
96 jit_state_t *_jit;
d16005f8
PC
97 struct opcode *opcode_list;
98 void (*function)(void);
98fa08a5
PC
99 const u32 *code;
100 struct block *next;
d16005f8
PC
101 u32 pc;
102 u32 hash;
d8b04acd 103 u32 precompile_date;
98fa08a5
PC
104 unsigned int code_size;
105 u16 nb_ops;
d16005f8 106#if ENABLE_THREADED_COMPILER
ba3814c1
PC
107 _Atomic u8 flags;
108#else
109 u8 flags;
d16005f8 110#endif
d16005f8
PC
111};
112
113struct lightrec_branch {
114 struct jit_node *branch;
115 u32 target;
116};
117
118struct lightrec_branch_target {
119 struct jit_node *label;
120 u32 offset;
121};
122
98fa08a5
PC
123enum c_wrappers {
124 C_WRAPPER_RW,
125 C_WRAPPER_RW_GENERIC,
fdf33147 126 C_WRAPPER_MFC,
98fa08a5
PC
127 C_WRAPPER_MTC,
128 C_WRAPPER_CP,
98fa08a5
PC
129 C_WRAPPERS_COUNT,
130};
131
132struct lightrec_cstate {
133 struct lightrec_state *state;
134
d16005f8
PC
135 struct lightrec_branch local_branches[512];
136 struct lightrec_branch_target targets[512];
d16005f8
PC
137 unsigned int nb_local_branches;
138 unsigned int nb_targets;
98fa08a5
PC
139 unsigned int cycles;
140
141 struct regcache *reg_cache;
142};
143
144struct lightrec_state {
145 struct lightrec_registers regs;
ba3814c1 146 uintptr_t wrapper_regs[NUM_TEMPS];
98fa08a5
PC
147 u32 next_pc;
148 u32 current_cycle;
149 u32 target_cycle;
150 u32 exit_flags;
151 u32 old_cycle_counter;
152 struct block *dispatcher, *c_wrapper_block;
fd58fa32
PC
153 void *c_wrappers[C_WRAPPERS_COUNT];
154 void *wrappers_eps[C_WRAPPERS_COUNT];
d16005f8 155 struct blockcache *block_cache;
d16005f8 156 struct recompiler *rec;
98fa08a5 157 struct lightrec_cstate *cstate;
a59e5536 158 struct reaper *reaper;
02487de7 159 void *tlsf;
d16005f8 160 void (*eob_wrapper_func)(void);
98fa08a5 161 void (*memset_func)(void);
d16005f8
PC
162 void (*get_next_block)(void);
163 struct lightrec_ops ops;
a59e5536 164 unsigned int nb_precompile;
d16005f8
PC
165 unsigned int nb_maps;
166 const struct lightrec_mem_map *maps;
ba3814c1 167 uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
02487de7 168 _Bool with_32bit_lut;
d16005f8
PC
169 _Bool mirrors_mapped;
170 _Bool invalidate_from_dma_only;
171 void *code_lut[];
172};
173
174u32 lightrec_rw(struct lightrec_state *state, union code op,
03535202 175 u32 addr, u32 data, u32 *flags,
98fa08a5 176 struct block *block);
d16005f8 177
98fa08a5 178void lightrec_free_block(struct lightrec_state *state, struct block *block);
d16005f8
PC
179
180void remove_from_code_lut(struct blockcache *cache, struct block *block);
181
02487de7
PC
182enum psx_map
183lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
184
98fa08a5
PC
185const struct lightrec_mem_map *
186lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
187
d16005f8
PC
188static inline u32 kunseg(u32 addr)
189{
190 if (unlikely(addr >= 0xa0000000))
191 return addr - 0xa0000000;
192 else
193 return addr &~ 0x80000000;
194}
195
196static inline u32 lut_offset(u32 pc)
197{
198 if (pc & BIT(28))
199 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
200 else
201 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
202}
203
02487de7
PC
204static inline _Bool is_big_endian(void)
205{
206 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
207}
208
209static inline _Bool lut_is_32bit(const struct lightrec_state *state)
210{
211 return __WORDSIZE == 32 ||
212 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
213}
214
215static inline size_t lut_elm_size(const struct lightrec_state *state)
216{
217 return lut_is_32bit(state) ? 4 : sizeof(void *);
218}
219
220static inline void ** lut_address(struct lightrec_state *state, u32 offset)
221{
222 if (lut_is_32bit(state))
223 return (void **) ((uintptr_t) state->code_lut + offset * 4);
224 else
225 return &state->code_lut[offset];
226}
227
228static inline void * lut_read(struct lightrec_state *state, u32 offset)
229{
d8b04acd 230 void **lut_entry = lut_address(state, offset);
02487de7
PC
231
232 if (lut_is_32bit(state))
233 return (void *)(uintptr_t) *(u32 *) lut_entry;
234 else
235 return *lut_entry;
236}
237
238static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
239{
240 void **lut_entry = lut_address(state, offset);
241
242 if (lut_is_32bit(state))
243 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
244 else
245 *lut_entry = ptr;
246}
247
98fa08a5
PC
248static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
249{
250 u16 flags = block->opcode_list[offset].flags;
251
03535202 252 offset += op_flag_no_ds(flags);
98fa08a5
PC
253
254 return block->pc + (offset + imm << 2);
255}
256
257static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
258{
259 u16 flags = block->opcode_list[offset].flags;
260
03535202 261 offset -= op_flag_no_ds(flags);
98fa08a5
PC
262
263 return block->pc + (offset + imm << 2);
264}
265
d16005f8
PC
266void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
267u32 lightrec_mfc(struct lightrec_state *state, union code op);
98fa08a5
PC
268void lightrec_rfe(struct lightrec_state *state);
269void lightrec_cp(struct lightrec_state *state, union code op);
270
271struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
272void lightrec_free_cstate(struct lightrec_cstate *cstate);
d16005f8
PC
273
274union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
275
98fa08a5 276int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
ba3814c1
PC
277void lightrec_free_opcode_list(struct lightrec_state *state,
278 struct opcode *list);
98fa08a5
PC
279
280unsigned int lightrec_cycles_of_opcode(union code code);
281
282static inline u8 get_mult_div_lo(union code c)
283{
284 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
285}
286
287static inline u8 get_mult_div_hi(union code c)
288{
289 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
290}
d16005f8 291
03535202
PC
292static inline s16 s16_max(s16 a, s16 b)
293{
294 return a > b ? a : b;
295}
296
ba3814c1
PC
297static inline _Bool block_has_flag(struct block *block, u8 flag)
298{
299#if ENABLE_THREADED_COMPILER
300 return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
301#else
302 return block->flags & flag;
303#endif
304}
305
306static inline u8 block_set_flags(struct block *block, u8 mask)
307{
308#if ENABLE_THREADED_COMPILER
309 return atomic_fetch_or_explicit(&block->flags, mask,
310 memory_order_relaxed);
311#else
312 u8 flags = block->flags;
313
314 block->flags |= mask;
315
316 return flags;
317#endif
318}
319
320static inline u8 block_clear_flags(struct block *block, u8 mask)
321{
322#if ENABLE_THREADED_COMPILER
323 return atomic_fetch_and_explicit(&block->flags, ~mask,
324 memory_order_relaxed);
325#else
326 u8 flags = block->flags;
327
328 block->flags &= ~mask;
329
330 return flags;
331#endif
332}
333
d16005f8 334#endif /* __LIGHTREC_PRIVATE_H__ */