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