2 * Copyright (C) 2016 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)
54 #define RAM_SIZE 0x200000
55 #define BIOS_SIZE 0x80000
57 #define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
59 /* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
62 typedef struct jit_state jit_state_t;
72 struct lightrec_state *state;
73 struct opcode *opcode_list;
74 void (*function)(void);
77 #if ENABLE_THREADED_COMPILER
78 atomic_flag op_list_freed;
80 unsigned int code_size;
83 const struct lightrec_mem_map *map;
87 struct lightrec_branch {
88 struct jit_node *branch;
92 struct lightrec_branch_target {
93 struct jit_node *label;
97 struct lightrec_state {
98 u32 native_reg_cache[34];
103 struct block *dispatcher, *rw_wrapper, *rw_generic_wrapper,
104 *mfc_wrapper, *mtc_wrapper, *rfe_wrapper, *cp_wrapper,
105 *syscall_wrapper, *break_wrapper;
106 void *rw_func, *rw_generic_func, *mfc_func, *mtc_func, *rfe_func,
107 *cp_func, *syscall_func, *break_func;
108 struct jit_node *branches[512];
109 struct lightrec_branch local_branches[512];
110 struct lightrec_branch_target targets[512];
111 unsigned int nb_branches;
112 unsigned int nb_local_branches;
113 unsigned int nb_targets;
114 struct tinymm *tinymm;
115 struct blockcache *block_cache;
116 struct regcache *reg_cache;
117 struct recompiler *rec;
118 void (*eob_wrapper_func)(void);
119 void (*get_next_block)(void);
120 struct lightrec_ops ops;
122 unsigned int nb_maps;
123 const struct lightrec_mem_map *maps;
124 uintptr_t offset_ram, offset_bios, offset_scratch;
125 _Bool mirrors_mapped;
126 _Bool invalidate_from_dma_only;
130 u32 lightrec_rw(struct lightrec_state *state, union code op,
131 u32 addr, u32 data, u16 *flags);
133 void lightrec_free_block(struct block *block);
135 void remove_from_code_lut(struct blockcache *cache, struct block *block);
137 static inline u32 kunseg(u32 addr)
139 if (unlikely(addr >= 0xa0000000))
140 return addr - 0xa0000000;
142 return addr &~ 0x80000000;
145 static inline u32 lut_offset(u32 pc)
148 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
150 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
153 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
154 u32 lightrec_mfc(struct lightrec_state *state, union code op);
156 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
158 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
159 int lightrec_compile_block(struct block *block);
161 #endif /* __LIGHTREC_PRIVATE_H__ */