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