1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net>
6 #ifndef __LIGHTREC_PRIVATE_H__
7 #define __LIGHTREC_PRIVATE_H__
9 #include "lightrec-config.h"
10 #include "disassembler.h"
13 #if ENABLE_THREADED_COMPILER
14 #include <stdatomic.h>
17 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
20 # define likely(x) __builtin_expect(!!(x),1)
21 # define unlikely(x) __builtin_expect(!!(x),0)
23 # define likely(x) (x)
24 # define unlikely(x) (x)
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)
33 # define LE32TOH(x) (x)
34 # define HTOLE32(x) (x)
35 # define LE16TOH(x) (x)
36 # define HTOLE16(x) (x)
40 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
42 #define SET_DEFAULT_ELM(table, value) [0] = NULL
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)
49 #define BLOCK_IS_DEAD BIT(3)
50 #define BLOCK_IS_MEMSET BIT(4)
52 #define RAM_SIZE 0x200000
53 #define BIOS_SIZE 0x80000
55 #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
60 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
63 typedef struct jit_state jit_state_t;
74 struct opcode *opcode_list;
75 void (*function)(void);
80 unsigned int code_size;
83 #if ENABLE_THREADED_COMPILER
84 atomic_flag op_list_freed;
88 struct lightrec_branch {
89 struct jit_node *branch;
93 struct lightrec_branch_target {
94 struct jit_node *label;
100 C_WRAPPER_RW_GENERIC,
108 struct lightrec_cstate {
109 struct lightrec_state *state;
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;
119 struct regcache *reg_cache;
122 struct lightrec_state {
123 struct lightrec_registers regs;
128 u32 old_cycle_counter;
130 struct block *dispatcher, *c_wrapper_block;
131 void *c_wrappers[C_WRAPPERS_COUNT];
132 void *wrappers_eps[C_WRAPPERS_COUNT];
133 struct tinymm *tinymm;
134 struct blockcache *block_cache;
135 struct recompiler *rec;
136 struct lightrec_cstate *cstate;
137 struct reaper *reaper;
138 void (*eob_wrapper_func)(void);
139 void (*memset_func)(void);
140 void (*get_next_block)(void);
141 struct lightrec_ops ops;
142 unsigned int nb_precompile;
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;
151 u32 lightrec_rw(struct lightrec_state *state, union code op,
152 u32 addr, u32 data, u16 *flags,
153 struct block *block);
155 void lightrec_free_block(struct lightrec_state *state, struct block *block);
157 void remove_from_code_lut(struct blockcache *cache, struct block *block);
159 const struct lightrec_mem_map *
160 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
162 static inline u32 kunseg(u32 addr)
164 if (unlikely(addr >= 0xa0000000))
165 return addr - 0xa0000000;
167 return addr &~ 0x80000000;
170 static inline u32 lut_offset(u32 pc)
173 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
175 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
178 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
180 u16 flags = block->opcode_list[offset].flags;
182 offset += !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
184 return block->pc + (offset + imm << 2);
187 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
189 u16 flags = block->opcode_list[offset].flags;
191 offset -= !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
193 return block->pc + (offset + imm << 2);
196 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
197 u32 lightrec_mfc(struct lightrec_state *state, union code op);
198 void lightrec_rfe(struct lightrec_state *state);
199 void lightrec_cp(struct lightrec_state *state, union code op);
201 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
202 void lightrec_free_cstate(struct lightrec_cstate *cstate);
204 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
206 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
207 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
208 void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block);
210 unsigned int lightrec_cycles_of_opcode(union code code);
212 static inline u8 get_mult_div_lo(union code c)
214 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
217 static inline u8 get_mult_div_hi(union code c)
219 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
222 #endif /* __LIGHTREC_PRIVATE_H__ */