#ifndef TCG_WASM32_H
#define TCG_WASM32_H

struct wasmContext {
    // 0
    CPUArchState *env;
    // 4
    uint64_t *stack;
    // 8
    uint32_t *tb_ptr;
    // 12
    uint32_t *tci_tb_ptr;
    // 16
    uint32_t do_init;
    // 20
    uint32_t done_flag;
    // 24
    uint64_t *stack128;
    // 28
    uint32_t unwinding;
    // 32
    uint32_t tlb_guard_lo;
    // 36
    uint32_t tlb_guard_hi;
    // 40
    uint32_t tlb_guard_on;
};

#define ENV_OFF 0
#define STACK_OFF 4
#define TB_PTR_OFF 8
#define HELPER_RET_TB_PTR_OFF 12
#define DO_INIT_OFF 16
#define DONE_FLAG_OFF 20
#define STACK128_OFF 24
#define UNWINDING_OFF 28
#define TLB_GUARD_LO_OFF 32
#define TLB_GUARD_HI_OFF 36
#define TLB_GUARD_ON_OFF 40

void set_done_flag();

void set_unwinding_flag();

int get_core_nums();

void remove_tb(void *tb_ptr);

void flush_tb_instances(void);

void init_wasm32();

void wasm_dump_module(const uint8_t *p, int len, const char *name);

/* TCG compiled-vs-TCI differential (debug, opt-in via wasm_diff_enable()).
 * wasm_cur_tb_pc is the guest PC of the TB about to run, set by cpu_tb_exec. */
extern __thread uint64_t wasm_cur_tb_pc;
extern int wasm_diff_mode;
extern int wasm_store_tci_disabled;
extern int wasm_store_tci_hash;
extern int wasm_store_helper_tci;
extern int wasm_store_bisect_on;
extern int wasm_store_bisect_width;
extern uint64_t wasm_store_pc_lo;
extern uint64_t wasm_store_pc_hi;
extern uint64_t wasm_dump_pc_lo;
extern uint64_t wasm_dump_pc_hi;
extern uint32_t wasm_store_bisect_mask;
extern uint32_t wasm_store_bisect_val;
void wasm_disable_store_tci(void);
void wasm_set_store_tci_hash(int mode);
void wasm_set_store_helper_tci(void);
void wasm_set_store_bisect(int width, uint32_t mask, uint32_t val);
void wasm_set_store_pc_window(uint32_t lo, uint32_t hi);
void wasm_set_dump_pc_window(uint32_t lo, uint32_t hi);
void wasm_diff_enable(void);
void wasm_diff_enable2(void);
int wasm_diff_on(void);
int wasm_diff_needs_nochain(void);
int wasm_diff_needs_nochain_at(uint64_t pc);
void wasm_diff_register_tb(const void *rw_ptr, const void *rx_ptr, uint64_t pc,
                           bool has_store, bool safe_shadow);
bool wasm_diff_lookup_store_tb(const void *tb_ptr, uint64_t *pc);
bool wasm_diff_lookup_safe_tb(const void *tb_ptr);

extern __thread bool wasm_tci_only_tb;

#define INSTANTIATE_NUM 1500
#define WASM_TCI_ONLY_ENTRY (-2147483647 - 1)

#endif
