Update lightrec 20220716 (#672)
[pcsx_rearmed.git] / deps / lightrec / lightrec-private.h
index e9efcb5..4eedef2 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef __LIGHTREC_PRIVATE_H__
 #define __LIGHTREC_PRIVATE_H__
 
+#include "lightning-wrapper.h"
 #include "lightrec-config.h"
 #include "disassembler.h"
 #include "lightrec.h"
@@ -42,6 +43,8 @@
 #define SET_DEFAULT_ELM(table, value) [0] = NULL
 #endif
 
+#define fallthrough do {} while (0) /* fall-through */
+
 /* Flags for (struct block *)->flags */
 #define BLOCK_NEVER_COMPILE    BIT(0)
 #define BLOCK_SHOULD_RECOMPILE BIT(1)
@@ -66,9 +69,16 @@ struct blockcache;
 struct recompiler;
 struct regcache;
 struct opcode;
-struct tinymm;
 struct reaper;
 
+struct u16x2 {
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+       u16 h, l;
+#else
+       u16 l, h;
+#endif
+};
+
 struct block {
        jit_state_t *_jit;
        struct opcode *opcode_list;
@@ -77,6 +87,7 @@ struct block {
        struct block *next;
        u32 pc;
        u32 hash;
+       u32 precompile_date;
        unsigned int code_size;
        u16 nb_ops;
        u8 flags;
@@ -98,7 +109,6 @@ struct lightrec_branch_target {
 enum c_wrappers {
        C_WRAPPER_RW,
        C_WRAPPER_RW_GENERIC,
-       C_WRAPPER_MFC,
        C_WRAPPER_MTC,
        C_WRAPPER_CP,
        C_WRAPPER_SYSCALL,
@@ -128,12 +138,13 @@ struct lightrec_state {
        u32 exit_flags;
        u32 old_cycle_counter;
        struct block *dispatcher, *c_wrapper_block;
-       void *c_wrapper, *c_wrappers[C_WRAPPERS_COUNT];
-       struct tinymm *tinymm;
+       void *c_wrappers[C_WRAPPERS_COUNT];
+       void *wrappers_eps[C_WRAPPERS_COUNT];
        struct blockcache *block_cache;
        struct recompiler *rec;
        struct lightrec_cstate *cstate;
        struct reaper *reaper;
+       void *tlsf;
        void (*eob_wrapper_func)(void);
        void (*memset_func)(void);
        void (*get_next_block)(void);
@@ -142,19 +153,23 @@ struct lightrec_state {
        unsigned int nb_maps;
        const struct lightrec_mem_map *maps;
        uintptr_t offset_ram, offset_bios, offset_scratch;
+       _Bool with_32bit_lut;
        _Bool mirrors_mapped;
        _Bool invalidate_from_dma_only;
        void *code_lut[];
 };
 
 u32 lightrec_rw(struct lightrec_state *state, union code op,
-               u32 addr, u32 data, u16 *flags,
+               u32 addr, u32 data, u32 *flags,
                struct block *block);
 
 void lightrec_free_block(struct lightrec_state *state, struct block *block);
 
 void remove_from_code_lut(struct blockcache *cache, struct block *block);
 
+enum psx_map
+lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
+
 const struct lightrec_mem_map *
 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
 
@@ -174,11 +189,55 @@ static inline u32 lut_offset(u32 pc)
                return (pc & (RAM_SIZE - 1)) >> 2; // RAM
 }
 
+static inline _Bool is_big_endian(void)
+{
+       return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
+}
+
+static inline _Bool lut_is_32bit(const struct lightrec_state *state)
+{
+       return __WORDSIZE == 32 ||
+               (ENABLE_CODE_BUFFER && state->with_32bit_lut);
+}
+
+static inline size_t lut_elm_size(const struct lightrec_state *state)
+{
+       return lut_is_32bit(state) ? 4 : sizeof(void *);
+}
+
+static inline void ** lut_address(struct lightrec_state *state, u32 offset)
+{
+       if (lut_is_32bit(state))
+               return (void **) ((uintptr_t) state->code_lut + offset * 4);
+       else
+               return &state->code_lut[offset];
+}
+
+static inline void * lut_read(struct lightrec_state *state, u32 offset)
+{
+       void **lut_entry = lut_address(state, offset);
+
+       if (lut_is_32bit(state))
+               return (void *)(uintptr_t) *(u32 *) lut_entry;
+       else
+               return *lut_entry;
+}
+
+static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
+{
+       void **lut_entry = lut_address(state, offset);
+
+       if (lut_is_32bit(state))
+               *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
+       else
+               *lut_entry = ptr;
+}
+
 static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
 {
        u16 flags = block->opcode_list[offset].flags;
 
-       offset += !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
+       offset += op_flag_no_ds(flags);
 
        return block->pc + (offset + imm << 2);
 }
@@ -187,7 +246,7 @@ static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
 {
        u16 flags = block->opcode_list[offset].flags;
 
-       offset -= !!(OPT_SWITCH_DELAY_SLOTS && (flags & LIGHTREC_NO_DS));
+       offset -= op_flag_no_ds(flags);
 
        return block->pc + (offset + imm << 2);
 }
@@ -202,7 +261,6 @@ void lightrec_free_cstate(struct lightrec_cstate *cstate);
 
 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
 
-struct block * lightrec_get_block(struct lightrec_state *state, u32 pc);
 int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
 void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block);
 
@@ -218,4 +276,9 @@ static inline u8 get_mult_div_hi(union code c)
        return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
 }
 
+static inline s16 s16_max(s16 a, s16 b)
+{
+       return a > b ? a : b;
+}
+
 #endif /* __LIGHTREC_PRIVATE_H__ */