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"
14 #if ENABLE_THREADED_COMPILER
15 #include <stdatomic.h>
18 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
21 # define likely(x) __builtin_expect(!!(x),1)
22 # define unlikely(x) __builtin_expect(!!(x),0)
24 # define likely(x) (x)
25 # define unlikely(x) (x)
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)
34 # define LE32TOH(x) (x)
35 # define HTOLE32(x) (x)
36 # define LE16TOH(x) (x)
37 # define HTOLE16(x) (x)
41 #define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
43 #define SET_DEFAULT_ELM(table, value) [0] = NULL
46 #define fallthrough do {} while (0) /* fall-through */
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)
52 #define BLOCK_IS_DEAD BIT(3)
53 #define BLOCK_IS_MEMSET BIT(4)
55 #define RAM_SIZE 0x200000
56 #define BIOS_SIZE 0x80000
58 #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
63 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
66 typedef struct jit_state jit_state_t;
76 struct opcode *opcode_list;
77 void (*function)(void);
83 unsigned int code_size;
86 #if ENABLE_THREADED_COMPILER
87 atomic_flag op_list_freed;
91 struct lightrec_branch {
92 struct jit_node *branch;
96 struct lightrec_branch_target {
97 struct jit_node *label;
103 C_WRAPPER_RW_GENERIC,
111 struct lightrec_cstate {
112 struct lightrec_state *state;
114 struct jit_node *branches[512];
115 struct lightrec_branch local_branches[512];
116 struct lightrec_branch_target targets[512];
117 unsigned int nb_branches;
118 unsigned int nb_local_branches;
119 unsigned int nb_targets;
122 struct regcache *reg_cache;
125 struct lightrec_state {
126 struct lightrec_registers regs;
131 u32 old_cycle_counter;
133 struct block *dispatcher, *c_wrapper_block;
134 void *c_wrappers[C_WRAPPERS_COUNT];
135 void *wrappers_eps[C_WRAPPERS_COUNT];
136 struct blockcache *block_cache;
137 struct recompiler *rec;
138 struct lightrec_cstate *cstate;
139 struct reaper *reaper;
141 void (*eob_wrapper_func)(void);
142 void (*memset_func)(void);
143 void (*get_next_block)(void);
144 struct lightrec_ops ops;
145 unsigned int nb_precompile;
146 unsigned int nb_maps;
147 const struct lightrec_mem_map *maps;
148 uintptr_t offset_ram, offset_bios, offset_scratch;
149 _Bool with_32bit_lut;
150 _Bool mirrors_mapped;
151 _Bool invalidate_from_dma_only;
155 u32 lightrec_rw(struct lightrec_state *state, union code op,
156 u32 addr, u32 data, u16 *flags,
157 struct block *block);
159 void lightrec_free_block(struct lightrec_state *state, struct block *block);
161 void remove_from_code_lut(struct blockcache *cache, struct block *block);
164 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
166 const struct lightrec_mem_map *
167 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
169 static inline u32 kunseg(u32 addr)
171 if (unlikely(addr >= 0xa0000000))
172 return addr - 0xa0000000;
174 return addr &~ 0x80000000;
177 static inline u32 lut_offset(u32 pc)
180 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
182 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
185 static inline _Bool is_big_endian(void)
187 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
190 static inline _Bool lut_is_32bit(const struct lightrec_state *state)
192 return __WORDSIZE == 32 ||
193 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
196 static inline size_t lut_elm_size(const struct lightrec_state *state)
198 return lut_is_32bit(state) ? 4 : sizeof(void *);
201 static inline void ** lut_address(struct lightrec_state *state, u32 offset)
203 if (lut_is_32bit(state))
204 return (void **) ((uintptr_t) state->code_lut + offset * 4);
206 return &state->code_lut[offset];
209 static inline void * lut_read(struct lightrec_state *state, u32 offset)
211 void **lut_entry = lut_address(state, offset);
213 if (lut_is_32bit(state))
214 return (void *)(uintptr_t) *(u32 *) lut_entry;
219 static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
221 void **lut_entry = lut_address(state, offset);
223 if (lut_is_32bit(state))
224 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
229 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
231 u16 flags = block->opcode_list[offset].flags;
233 offset += !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
235 return block->pc + (offset + imm << 2);
238 static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
240 u16 flags = block->opcode_list[offset].flags;
242 offset -= !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
244 return block->pc + (offset + imm << 2);
247 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
248 u32 lightrec_mfc(struct lightrec_state *state, union code op);
249 void lightrec_rfe(struct lightrec_state *state);
250 void lightrec_cp(struct lightrec_state *state, union code op);
252 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
253 void lightrec_free_cstate(struct lightrec_cstate *cstate);
255 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
257 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
258 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
259 void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block);
261 unsigned int lightrec_cycles_of_opcode(union code code);
263 static inline u8 get_mult_div_lo(union code c)
265 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
268 static inline u8 get_mult_div_hi(union code c)
270 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
273 #endif /* __LIGHTREC_PRIVATE_H__ */