2 * Copyright (C) 2016-2020 Paul Cercueil <paul@crapouillou.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
15 #ifndef __LIGHTREC_PRIVATE_H__
16 #define __LIGHTREC_PRIVATE_H__
19 #include "disassembler.h"
22 #if ENABLE_THREADED_COMPILER
23 #include <stdatomic.h>
26 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
27 #define BIT(x) (1 << (x))
30 # define likely(x) __builtin_expect(!!(x),1)
31 # define unlikely(x) __builtin_expect(!!(x),0)
33 # define likely(x) (x)
34 # define unlikely(x) (x)
37 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
38 # define LE32TOH(x) __builtin_bswap32(x)
39 # define HTOLE32(x) __builtin_bswap32(x)
40 # define LE16TOH(x) __builtin_bswap16(x)
41 # define HTOLE16(x) __builtin_bswap16(x)
43 # define LE32TOH(x) (x)
44 # define HTOLE32(x) (x)
45 # define LE16TOH(x) (x)
46 # define HTOLE16(x) (x)
49 /* Flags for (struct block *)->flags */
50 #define BLOCK_NEVER_COMPILE BIT(0)
51 #define BLOCK_SHOULD_RECOMPILE BIT(1)
52 #define BLOCK_FULLY_TAGGED BIT(2)
53 #define BLOCK_IS_DEAD BIT(3)
55 #define RAM_SIZE 0x200000
56 #define BIOS_SIZE 0x80000
58 #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 lightrec_state *state;
75 struct opcode *opcode_list;
76 void (*function)(void);
79 #if ENABLE_THREADED_COMPILER
80 atomic_flag op_list_freed;
82 unsigned int code_size;
85 const struct lightrec_mem_map *map;
89 struct lightrec_branch {
90 struct jit_node *branch;
94 struct lightrec_branch_target {
95 struct jit_node *label;
99 struct lightrec_state {
100 u32 native_reg_cache[34];
105 struct block *dispatcher, *rw_wrapper, *rw_generic_wrapper,
106 *mfc_wrapper, *mtc_wrapper, *rfe_wrapper, *cp_wrapper,
107 *syscall_wrapper, *break_wrapper;
108 void *rw_func, *rw_generic_func, *mfc_func, *mtc_func, *rfe_func,
109 *cp_func, *syscall_func, *break_func;
110 struct jit_node *branches[512];
111 struct lightrec_branch local_branches[512];
112 struct lightrec_branch_target targets[512];
113 unsigned int nb_branches;
114 unsigned int nb_local_branches;
115 unsigned int nb_targets;
116 struct tinymm *tinymm;
117 struct blockcache *block_cache;
118 struct regcache *reg_cache;
119 struct recompiler *rec;
120 struct reaper *reaper;
121 void (*eob_wrapper_func)(void);
122 void (*get_next_block)(void);
123 struct lightrec_ops ops;
124 unsigned int nb_precompile;
126 unsigned int nb_maps;
127 const struct lightrec_mem_map *maps;
128 uintptr_t offset_ram, offset_bios, offset_scratch;
129 _Bool mirrors_mapped;
130 _Bool invalidate_from_dma_only;
134 u32 lightrec_rw(struct lightrec_state *state, union code op,
135 u32 addr, u32 data, u16 *flags);
137 void lightrec_free_block(struct block *block);
139 void remove_from_code_lut(struct blockcache *cache, struct block *block);
141 static inline u32 kunseg(u32 addr)
143 if (unlikely(addr >= 0xa0000000))
144 return addr - 0xa0000000;
146 return addr &~ 0x80000000;
149 static inline u32 lut_offset(u32 pc)
152 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
154 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
157 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
158 u32 lightrec_mfc(struct lightrec_state *state, union code op);
160 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
162 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
163 int lightrec_compile_block(struct block *block);
165 #endif /* __LIGHTREC_PRIVATE_H__ */