gpu_neon: don't include vector_ops.h in the main header
[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 73
11357fef
PC
74struct u16x2 {
75#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
76 u16 h, l;
77#else
78 u16 l, h;
79#endif
80};
81
d16005f8
PC
82struct block {
83 jit_state_t *_jit;
d16005f8
PC
84 struct opcode *opcode_list;
85 void (*function)(void);
98fa08a5
PC
86 const u32 *code;
87 struct block *next;
d16005f8
PC
88 u32 pc;
89 u32 hash;
d8b04acd 90 u32 precompile_date;
98fa08a5
PC
91 unsigned int code_size;
92 u16 nb_ops;
93 u8 flags;
d16005f8
PC
94#if ENABLE_THREADED_COMPILER
95 atomic_flag op_list_freed;
96#endif
d16005f8
PC
97};
98
99struct lightrec_branch {
100 struct jit_node *branch;
101 u32 target;
102};
103
104struct lightrec_branch_target {
105 struct jit_node *label;
106 u32 offset;
107};
108
98fa08a5
PC
109enum c_wrappers {
110 C_WRAPPER_RW,
111 C_WRAPPER_RW_GENERIC,
98fa08a5
PC
112 C_WRAPPER_MTC,
113 C_WRAPPER_CP,
114 C_WRAPPER_SYSCALL,
115 C_WRAPPER_BREAK,
116 C_WRAPPERS_COUNT,
117};
118
119struct lightrec_cstate {
120 struct lightrec_state *state;
121
d16005f8
PC
122 struct jit_node *branches[512];
123 struct lightrec_branch local_branches[512];
124 struct lightrec_branch_target targets[512];
125 unsigned int nb_branches;
126 unsigned int nb_local_branches;
127 unsigned int nb_targets;
98fa08a5
PC
128 unsigned int cycles;
129
130 struct regcache *reg_cache;
131};
132
133struct lightrec_state {
134 struct lightrec_registers regs;
135 u32 next_pc;
136 u32 current_cycle;
137 u32 target_cycle;
138 u32 exit_flags;
139 u32 old_cycle_counter;
140 struct block *dispatcher, *c_wrapper_block;
fd58fa32
PC
141 void *c_wrappers[C_WRAPPERS_COUNT];
142 void *wrappers_eps[C_WRAPPERS_COUNT];
d16005f8 143 struct blockcache *block_cache;
d16005f8 144 struct recompiler *rec;
98fa08a5 145 struct lightrec_cstate *cstate;
a59e5536 146 struct reaper *reaper;
02487de7 147 void *tlsf;
d16005f8 148 void (*eob_wrapper_func)(void);
98fa08a5 149 void (*memset_func)(void);
d16005f8
PC
150 void (*get_next_block)(void);
151 struct lightrec_ops ops;
a59e5536 152 unsigned int nb_precompile;
d16005f8
PC
153 unsigned int nb_maps;
154 const struct lightrec_mem_map *maps;
155 uintptr_t offset_ram, offset_bios, offset_scratch;
02487de7 156 _Bool with_32bit_lut;
d16005f8
PC
157 _Bool mirrors_mapped;
158 _Bool invalidate_from_dma_only;
159 void *code_lut[];
160};
161
162u32 lightrec_rw(struct lightrec_state *state, union code op,
03535202 163 u32 addr, u32 data, u32 *flags,
98fa08a5 164 struct block *block);
d16005f8 165
98fa08a5 166void lightrec_free_block(struct lightrec_state *state, struct block *block);
d16005f8
PC
167
168void remove_from_code_lut(struct blockcache *cache, struct block *block);
169
02487de7
PC
170enum psx_map
171lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
172
98fa08a5
PC
173const struct lightrec_mem_map *
174lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
175
d16005f8
PC
176static inline u32 kunseg(u32 addr)
177{
178 if (unlikely(addr >= 0xa0000000))
179 return addr - 0xa0000000;
180 else
181 return addr &~ 0x80000000;
182}
183
184static inline u32 lut_offset(u32 pc)
185{
186 if (pc & BIT(28))
187 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
188 else
189 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
190}
191
02487de7
PC
192static inline _Bool is_big_endian(void)
193{
194 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
195}
196
197static inline _Bool lut_is_32bit(const struct lightrec_state *state)
198{
199 return __WORDSIZE == 32 ||
200 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
201}
202
203static inline size_t lut_elm_size(const struct lightrec_state *state)
204{
205 return lut_is_32bit(state) ? 4 : sizeof(void *);
206}
207
208static inline void ** lut_address(struct lightrec_state *state, u32 offset)
209{
210 if (lut_is_32bit(state))
211 return (void **) ((uintptr_t) state->code_lut + offset * 4);
212 else
213 return &state->code_lut[offset];
214}
215
216static inline void * lut_read(struct lightrec_state *state, u32 offset)
217{
d8b04acd 218 void **lut_entry = lut_address(state, offset);
02487de7
PC
219
220 if (lut_is_32bit(state))
221 return (void *)(uintptr_t) *(u32 *) lut_entry;
222 else
223 return *lut_entry;
224}
225
226static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
227{
228 void **lut_entry = lut_address(state, offset);
229
230 if (lut_is_32bit(state))
231 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
232 else
233 *lut_entry = ptr;
234}
235
98fa08a5
PC
236static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
237{
238 u16 flags = block->opcode_list[offset].flags;
239
03535202 240 offset += op_flag_no_ds(flags);
98fa08a5
PC
241
242 return block->pc + (offset + imm << 2);
243}
244
245static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
246{
247 u16 flags = block->opcode_list[offset].flags;
248
03535202 249 offset -= op_flag_no_ds(flags);
98fa08a5
PC
250
251 return block->pc + (offset + imm << 2);
252}
253
d16005f8
PC
254void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
255u32 lightrec_mfc(struct lightrec_state *state, union code op);
98fa08a5
PC
256void lightrec_rfe(struct lightrec_state *state);
257void lightrec_cp(struct lightrec_state *state, union code op);
258
259struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
260void lightrec_free_cstate(struct lightrec_cstate *cstate);
d16005f8
PC
261
262union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
263
98fa08a5
PC
264int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
265void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block);
266
267unsigned int lightrec_cycles_of_opcode(union code code);
268
269static inline u8 get_mult_div_lo(union code c)
270{
271 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
272}
273
274static inline u8 get_mult_div_hi(union code c)
275{
276 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
277}
d16005f8 278
03535202
PC
279static inline s16 s16_max(s16 a, s16 b)
280{
281 return a > b ? a : b;
282}
283
d16005f8 284#endif /* __LIGHTREC_PRIVATE_H__ */