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 "lightning-wrapper.h"
10 #include "lightrec-config.h"
11 #include "disassembler.h"
15 #if ENABLE_THREADED_COMPILER
16 #include <stdatomic.h>
20 #include <immintrin.h>
26 #define PC_FMT "PC 0x%08"PRIx32
28 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
30 #define GENMASK(h, l) \
31 (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
34 # define likely(x) __builtin_expect(!!(x),1)
35 # define unlikely(x) __builtin_expect(!!(x),0)
37 # define likely(x) (x)
38 # define unlikely(x) (x)
41 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
42 # define LE32TOH(x) __builtin_bswap32(x)
43 # define HTOLE32(x) __builtin_bswap32(x)
44 # define LE16TOH(x) __builtin_bswap16(x)
45 # define HTOLE16(x) __builtin_bswap16(x)
47 # define LE32TOH(x) (x)
48 # define HTOLE32(x) (x)
49 # define LE16TOH(x) (x)
50 # define HTOLE16(x) (x)
54 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
56 #define SET_DEFAULT_ELM(table, value) [0] = NULL
59 #if __has_attribute(__fallthrough__)
60 # define fallthrough __attribute__((__fallthrough__))
62 # define fallthrough do {} while (0) /* fallthrough */
65 #define container_of(ptr, type, member) \
66 ((type *)((void *)(ptr) - offsetof(type, member)))
69 # define popcount32(x) __popcnt(x)
70 # define clz32(x) _lzcnt_u32(x)
71 # define ctz32(x) _tzcnt_u32(x)
73 # define popcount32(x) __builtin_popcount(x)
74 # define clz32(x) __builtin_clz(x)
75 # define ctz32(x) __builtin_ctz(x)
78 /* Flags for (struct block *)->flags */
79 #define BLOCK_NEVER_COMPILE BIT(0)
80 #define BLOCK_SHOULD_RECOMPILE BIT(1)
81 #define BLOCK_FULLY_TAGGED BIT(2)
82 #define BLOCK_IS_DEAD BIT(3)
83 #define BLOCK_IS_MEMSET BIT(4)
84 #define BLOCK_NO_OPCODE_LIST BIT(5)
85 #define BLOCK_PRELOAD_PC BIT(6)
87 #define RAM_SIZE 0x200000
88 #define BIOS_SIZE 0x80000
90 #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
94 #define REG_TEMP (offsetof(struct lightrec_state, temp_reg) / sizeof(u32))
96 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
99 typedef struct jit_state jit_state_t;
108 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
117 struct opcode *opcode_list;
118 void (*function)(void);
124 unsigned int code_size;
126 #if ENABLE_THREADED_COMPILER
133 struct lightrec_branch {
134 struct jit_node *branch;
138 struct lightrec_branch_target {
139 struct jit_node *label;
145 C_WRAPPER_RW_GENERIC,
152 struct lightrec_cstate {
153 struct lightrec_state *state;
155 struct lightrec_branch local_branches[512];
156 struct lightrec_branch_target targets[512];
158 unsigned int nb_local_branches;
159 unsigned int nb_targets;
162 struct regcache *reg_cache;
167 struct lightrec_state {
168 struct lightrec_registers regs;
172 uintptr_t wrapper_regs[NUM_TEMPS];
177 u32 old_cycle_counter;
179 struct block *dispatcher, *c_wrapper_block;
180 void *c_wrappers[C_WRAPPERS_COUNT];
181 void *wrappers_eps[C_WRAPPERS_COUNT];
182 struct blockcache *block_cache;
183 struct recompiler *rec;
184 struct lightrec_cstate *cstate;
185 struct reaper *reaper;
187 void (*eob_wrapper_func)(void);
188 void (*interpreter_func)(void);
189 void (*ds_check_func)(void);
190 void (*memset_func)(void);
191 void (*get_next_block)(void);
192 struct lightrec_ops ops;
193 unsigned int nb_precompile;
194 unsigned int nb_compile;
195 unsigned int nb_maps;
196 const struct lightrec_mem_map *maps;
197 uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
199 _Bool with_32bit_lut;
200 _Bool mirrors_mapped;
204 u32 lightrec_rw(struct lightrec_state *state, union code op, u32 addr,
205 u32 data, u32 *flags, struct block *block, u16 offset);
207 void lightrec_free_block(struct lightrec_state *state, struct block *block);
209 void remove_from_code_lut(struct blockcache *cache, struct block *block);
211 const struct lightrec_mem_map *
212 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
214 static inline u32 kunseg(u32 addr)
216 if (unlikely(addr >= 0xa0000000))
217 return addr - 0xa0000000;
219 return addr &~ 0x80000000;
222 static inline u32 lut_offset(u32 pc)
225 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
227 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
230 static inline _Bool is_big_endian(void)
232 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
235 static inline _Bool lut_is_32bit(const struct lightrec_state *state)
237 return __WORDSIZE == 32 ||
238 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
241 static inline size_t lut_elm_size(const struct lightrec_state *state)
243 return lut_is_32bit(state) ? 4 : sizeof(void *);
246 static inline void ** lut_address(struct lightrec_state *state, u32 offset)
248 if (lut_is_32bit(state))
249 return (void **) ((uintptr_t) state->code_lut + offset * 4);
251 return &state->code_lut[offset];
254 static inline void * lut_read(struct lightrec_state *state, u32 offset)
256 void **lut_entry = lut_address(state, offset);
258 if (lut_is_32bit(state))
259 return (void *)(uintptr_t) *(u32 *) lut_entry;
264 static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
266 void **lut_entry = lut_address(state, offset);
268 if (lut_is_32bit(state))
269 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
274 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
276 u16 flags = block->opcode_list[offset].flags;
278 offset += op_flag_no_ds(flags);
280 return block->pc + ((offset + imm) << 2);
283 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
285 u16 flags = block->opcode_list[offset].flags;
287 offset -= op_flag_no_ds(flags);
289 return block->pc + ((offset + imm) << 2);
292 void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data);
293 u32 lightrec_mfc(struct lightrec_state *state, union code op);
294 void lightrec_rfe(struct lightrec_state *state);
295 void lightrec_cp(struct lightrec_state *state, union code op);
297 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
298 void lightrec_free_cstate(struct lightrec_cstate *cstate);
300 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
302 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
303 void lightrec_free_opcode_list(struct lightrec_state *state,
304 struct opcode *list);
306 unsigned int lightrec_cycles_of_opcode(const struct lightrec_state *state,
309 static inline u8 get_mult_div_lo(union code c)
311 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
314 static inline u8 get_mult_div_hi(union code c)
316 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
319 static inline s16 s16_max(s16 a, s16 b)
321 return a > b ? a : b;
324 static inline _Bool block_has_flag(struct block *block, u8 flag)
326 #if ENABLE_THREADED_COMPILER
327 return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
329 return block->flags & flag;
333 static inline u8 block_set_flags(struct block *block, u8 mask)
335 #if ENABLE_THREADED_COMPILER
336 return atomic_fetch_or_explicit(&block->flags, mask,
337 memory_order_relaxed);
339 u8 flags = block->flags;
341 block->flags |= mask;
347 static inline u8 block_clear_flags(struct block *block, u8 mask)
349 #if ENABLE_THREADED_COMPILER
350 return atomic_fetch_and_explicit(&block->flags, ~mask,
351 memory_order_relaxed);
353 u8 flags = block->flags;
355 block->flags &= ~mask;
361 static inline _Bool can_sign_extend(s32 value, u8 order)
363 return ((u32)(value >> (order - 1)) + 1) < 2;
366 static inline _Bool can_zero_extend(u32 value, u8 order)
368 return (value >> order) == 0;
371 static inline const struct opcode *
372 get_delay_slot(const struct opcode *list, u16 i)
374 return op_flag_no_ds(list[i].flags) ? &list[i - 1] : &list[i + 1];
377 static inline _Bool lightrec_store_next_pc(void)
379 return NUM_REGS + NUM_TEMPS <= 4;
382 #endif /* __LIGHTREC_PRIVATE_H__ */