Merge pull request #661 from pcercuei/update-lightrec-20220604
[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"
13
14#if ENABLE_THREADED_COMPILER
15#include <stdatomic.h>
16#endif
17
18#define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
d16005f8
PC
19
20#ifdef __GNUC__
21# define likely(x) __builtin_expect(!!(x),1)
22# define unlikely(x) __builtin_expect(!!(x),0)
23#else
24# define likely(x) (x)
25# define unlikely(x) (x)
26#endif
27
28#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
29# define LE32TOH(x) __builtin_bswap32(x)
30# define HTOLE32(x) __builtin_bswap32(x)
31# define LE16TOH(x) __builtin_bswap16(x)
32# define HTOLE16(x) __builtin_bswap16(x)
33#else
34# define LE32TOH(x) (x)
35# define HTOLE32(x) (x)
36# define LE16TOH(x) (x)
37# define HTOLE16(x) (x)
38#endif
39
98fa08a5
PC
40#if HAS_DEFAULT_ELM
41#define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
42#else
43#define SET_DEFAULT_ELM(table, value) [0] = NULL
44#endif
45
d8b04acd
PC
46#define fallthrough do {} while (0) /* fall-through */
47
d16005f8
PC
48/* Flags for (struct block *)->flags */
49#define BLOCK_NEVER_COMPILE BIT(0)
50#define BLOCK_SHOULD_RECOMPILE BIT(1)
51#define BLOCK_FULLY_TAGGED BIT(2)
a59e5536 52#define BLOCK_IS_DEAD BIT(3)
98fa08a5 53#define BLOCK_IS_MEMSET BIT(4)
d16005f8
PC
54
55#define RAM_SIZE 0x200000
56#define BIOS_SIZE 0x80000
57
58#define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
59
98fa08a5
PC
60#define REG_LO 32
61#define REG_HI 33
62
d16005f8
PC
63/* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
64struct jit_node;
65struct jit_state;
66typedef struct jit_state jit_state_t;
67
68struct blockcache;
69struct recompiler;
70struct regcache;
71struct opcode;
a59e5536 72struct reaper;
d16005f8
PC
73
74struct block {
75 jit_state_t *_jit;
d16005f8
PC
76 struct opcode *opcode_list;
77 void (*function)(void);
98fa08a5
PC
78 const u32 *code;
79 struct block *next;
d16005f8
PC
80 u32 pc;
81 u32 hash;
d8b04acd 82 u32 precompile_date;
98fa08a5
PC
83 unsigned int code_size;
84 u16 nb_ops;
85 u8 flags;
d16005f8
PC
86#if ENABLE_THREADED_COMPILER
87 atomic_flag op_list_freed;
88#endif
d16005f8
PC
89};
90
91struct lightrec_branch {
92 struct jit_node *branch;
93 u32 target;
94};
95
96struct lightrec_branch_target {
97 struct jit_node *label;
98 u32 offset;
99};
100
98fa08a5
PC
101enum c_wrappers {
102 C_WRAPPER_RW,
103 C_WRAPPER_RW_GENERIC,
98fa08a5
PC
104 C_WRAPPER_MTC,
105 C_WRAPPER_CP,
106 C_WRAPPER_SYSCALL,
107 C_WRAPPER_BREAK,
108 C_WRAPPERS_COUNT,
109};
110
111struct lightrec_cstate {
112 struct lightrec_state *state;
113
d16005f8
PC
114 struct jit_node *branches[512];
115 struct lightrec_branch local_branches[512];
116 struct lightrec_branch_target targets[512];
117 unsigned int nb_branches;
118 unsigned int nb_local_branches;
119 unsigned int nb_targets;
98fa08a5
PC
120 unsigned int cycles;
121
122 struct regcache *reg_cache;
123};
124
125struct lightrec_state {
126 struct lightrec_registers regs;
127 u32 next_pc;
128 u32 current_cycle;
129 u32 target_cycle;
130 u32 exit_flags;
131 u32 old_cycle_counter;
22eee2ac 132 u32 c_wrapper_arg;
98fa08a5 133 struct block *dispatcher, *c_wrapper_block;
fd58fa32
PC
134 void *c_wrappers[C_WRAPPERS_COUNT];
135 void *wrappers_eps[C_WRAPPERS_COUNT];
d16005f8 136 struct blockcache *block_cache;
d16005f8 137 struct recompiler *rec;
98fa08a5 138 struct lightrec_cstate *cstate;
a59e5536 139 struct reaper *reaper;
02487de7 140 void *tlsf;
d16005f8 141 void (*eob_wrapper_func)(void);
98fa08a5 142 void (*memset_func)(void);
d16005f8
PC
143 void (*get_next_block)(void);
144 struct lightrec_ops ops;
a59e5536 145 unsigned int nb_precompile;
d16005f8
PC
146 unsigned int nb_maps;
147 const struct lightrec_mem_map *maps;
148 uintptr_t offset_ram, offset_bios, offset_scratch;
02487de7 149 _Bool with_32bit_lut;
d16005f8
PC
150 _Bool mirrors_mapped;
151 _Bool invalidate_from_dma_only;
152 void *code_lut[];
153};
154
155u32 lightrec_rw(struct lightrec_state *state, union code op,
98fa08a5
PC
156 u32 addr, u32 data, u16 *flags,
157 struct block *block);
d16005f8 158
98fa08a5 159void lightrec_free_block(struct lightrec_state *state, struct block *block);
d16005f8
PC
160
161void remove_from_code_lut(struct blockcache *cache, struct block *block);
162
02487de7
PC
163enum psx_map
164lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
165
98fa08a5
PC
166const struct lightrec_mem_map *
167lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
168
d16005f8
PC
169static inline u32 kunseg(u32 addr)
170{
171 if (unlikely(addr >= 0xa0000000))
172 return addr - 0xa0000000;
173 else
174 return addr &~ 0x80000000;
175}
176
177static inline u32 lut_offset(u32 pc)
178{
179 if (pc & BIT(28))
180 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
181 else
182 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
183}
184
02487de7
PC
185static inline _Bool is_big_endian(void)
186{
187 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
188}
189
190static inline _Bool lut_is_32bit(const struct lightrec_state *state)
191{
192 return __WORDSIZE == 32 ||
193 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
194}
195
196static inline size_t lut_elm_size(const struct lightrec_state *state)
197{
198 return lut_is_32bit(state) ? 4 : sizeof(void *);
199}
200
201static inline void ** lut_address(struct lightrec_state *state, u32 offset)
202{
203 if (lut_is_32bit(state))
204 return (void **) ((uintptr_t) state->code_lut + offset * 4);
205 else
206 return &state->code_lut[offset];
207}
208
209static inline void * lut_read(struct lightrec_state *state, u32 offset)
210{
d8b04acd 211 void **lut_entry = lut_address(state, offset);
02487de7
PC
212
213 if (lut_is_32bit(state))
214 return (void *)(uintptr_t) *(u32 *) lut_entry;
215 else
216 return *lut_entry;
217}
218
219static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
220{
221 void **lut_entry = lut_address(state, offset);
222
223 if (lut_is_32bit(state))
224 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
225 else
226 *lut_entry = ptr;
227}
228
98fa08a5
PC
229static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
230{
231 u16 flags = block->opcode_list[offset].flags;
232
233 offset += !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
234
235 return block->pc + (offset + imm << 2);
236}
237
238static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
239{
240 u16 flags = block->opcode_list[offset].flags;
241
242 offset -= !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
243
244 return block->pc + (offset + imm << 2);
245}
246
d16005f8
PC
247void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
248u32 lightrec_mfc(struct lightrec_state *state, union code op);
98fa08a5
PC
249void lightrec_rfe(struct lightrec_state *state);
250void lightrec_cp(struct lightrec_state *state, union code op);
251
252struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
253void lightrec_free_cstate(struct lightrec_cstate *cstate);
d16005f8
PC
254
255union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
256
257struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
98fa08a5
PC
258int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
259void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block);
260
261unsigned int lightrec_cycles_of_opcode(union code code);
262
263static inline u8 get_mult_div_lo(union code c)
264{
265 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
266}
267
268static inline u8 get_mult_div_hi(union code c)
269{
270 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
271}
d16005f8
PC
272
273#endif /* __LIGHTREC_PRIVATE_H__ */