Download src/php/php-src/ext/opcache/jit/ir/ir.h from meddyrat/dis-bench: direct link, hf CLI and curl.
- Browser
- Download file 41.9 kB
-
https://huggingface.co/datasets/meddyrat/dis-bench/resolve/main/src/php/php-src/ext/opcache/jit/ir/ir.h
- Command line
-
hf download hf://datasets/meddyrat/dis-bench/src/php/php-src/ext/opcache/jit/ir/ir.h
-
curl -L -o ir.h https://huggingface.co/datasets/meddyrat/dis-bench/resolve/main/src/php/php-src/ext/opcache/jit/ir/ir.h
41.9 kB
| /* | |
| * IR - Lightweight JIT Compilation Framework | |
| * (Public API) | |
| * Copyright (C) 2022 Zend by Perforce. | |
| * Authors: Dmitry Stogov <dmitry@php.net> | |
| */ | |
| extern "C" { | |
| /* TODO Handle ARM, too. */ | |
| /* Only supported is little endian for any arch on Windows, | |
| so just fake the same for all. */ | |
| /* target auto detection */ | |
| /* IR Type flags (low 4 bits are used for type size) */ | |
| /* List of IR types */ | |
| typedef enum _ir_type { | |
| IR_VOID, | |
| IR_TYPES(IR_TYPE_ENUM) | |
| IR_LAST_TYPE | |
| } ir_type; | |
| /* List of IR opcodes | |
| * ================== | |
| * | |
| * Each instruction is described by a type (opcode, flags, op1_type, op2_type, op3_type) | |
| * | |
| * flags | |
| * ----- | |
| * v - void | |
| * d - data IR_OP_FLAG_DATA | |
| * r - ref IR_OP_FLAG_DATA alias | |
| * p - pinned IR_OP_FLAG_DATA + IR_OP_FLAG_PINNED | |
| * c - control IR_OP_FLAG_CONTROL | |
| * S - control IR_OP_FLAG_CONTROL + IR_OP_FLAG_BB_START | |
| * E - control IR_OP_FLAG_CONTROL + IR_OP_FLAG_BB_END | |
| * T - control IR_OP_FLAG_CONTROL + IR_OP_FLAG_BB_END + IR_OP_FLAG_TERMINATOR | |
| * l - load IR_OP_FLAG_MEM + IR_OP_FLAG_MEM_LOAD | |
| * s - store IR_OP_FLAG_MEM + IR_OP_FLAG_STORE | |
| * x - call IR_OP_FLAG_MEM + IR_OP_FLAG_CALL | |
| * a - alloc IR_OP_FLAG_MEM + IR_OP_FLAG_ALLOC | |
| * 0-3 - number of input edges | |
| * N - number of arguments is defined in the insn->inputs_count (MERGE, PHI, CALL) | |
| * X1-X3 - number of extra data ops | |
| * C - commutative operation ("d2C" => IR_OP_FLAG_DATA + IR_OP_FLAG_COMMUTATIVE) | |
| * | |
| * operand types | |
| * ------------- | |
| * ___ - unused | |
| * def - reference to a definition op (data-flow use-def dependency edge) | |
| * ref - memory reference (data-flow use-def dependency edge) | |
| * var - variable reference (data-flow use-def dependency edge) | |
| * arg - argument reference CALL/TAILCALL/CARG->CARG | |
| * src - reference to a previous control region (IF, IF_TRUE, IF_FALSE, MERGE, LOOP_BEGIN, LOOP_END, RETURN) | |
| * reg - data-control dependency on region (PHI, VAR, PARAM) | |
| * ret - reference to a previous RETURN instruction (RETURN) | |
| * str - string: variable/argument name (VAR, PARAM, CALL, TAILCALL) | |
| * num - number: argument number (PARAM) | |
| * prb - branch probability 1-99 (0 - unspecified): (IF_TRUE, IF_FALSE, CASE_VAL, CASE_DEFAULT) | |
| * opt - optional number | |
| * pro - function prototype | |
| * | |
| * The order of IR opcodes is carefully selected for efficient folding. | |
| * - foldable instruction go first | |
| * - NOP is never used (code 0 is used as ANY pattern) | |
| * - CONST is the most often used instruction (encode with 1 bit) | |
| * - equality inversion: EQ <-> NE => op =^ 1 | |
| * - comparison inversion: [U]LT <-> [U]GT, [U]LE <-> [U]GE => op =^ 3 | |
| */ | |
| typedef enum _ir_op { | |
| IR_OPS(IR_OP_ENUM) | |
| IR_PHP_OPS(IR_OP_ENUM) | |
| IR_LAST_OP | |
| } ir_op; | |
| /* IR Opcode and Type Union */ | |
| /* IR References */ | |
| typedef int32_t ir_ref; | |
| /* IR Constant Value */ | |
| /* ADDR_MEMBER is neccessary to workaround MSVC C preprocessor bug */ | |
| typedef union _ir_val { | |
| double d; | |
| uint64_t u64; | |
| int64_t i64; | |
| uintptr_t addr; | |
| void *ptr; | |
| IR_STRUCT_LOHI( | |
| union { | |
| uint32_t u32; | |
| int32_t i32; | |
| float f; | |
| ADDR_MEMBER | |
| ir_ref name; | |
| ir_ref str; | |
| IR_STRUCT_LOHI( | |
| union { | |
| uint16_t u16; | |
| int16_t i16; | |
| IR_STRUCT_LOHI( | |
| union { | |
| uint8_t u8; | |
| int8_t i8; | |
| bool b; | |
| char c; | |
| }, | |
| uint8_t u8_hi | |
| ); | |
| }, | |
| uint16_t u16_hi | |
| ); | |
| }, | |
| uint32_t u32_hi | |
| ); | |
| } ir_val; | |
| /* IR Instruction */ | |
| typedef struct _ir_insn { | |
| IR_STRUCT_LOHI( | |
| union { | |
| IR_STRUCT_LOHI( | |
| union { | |
| IR_STRUCT_LOHI( | |
| uint8_t op, | |
| uint8_t type | |
| ); | |
| uint16_t opt; | |
| }, | |
| union { | |
| uint16_t inputs_count; /* number of input control edges for MERGE, PHI, CALL, TAILCALL */ | |
| uint16_t prev_insn_offset; /* 16-bit backward offset from current instruction for CSE */ | |
| uint16_t proto; | |
| } | |
| ); | |
| uint32_t optx; | |
| ir_ref ops[1]; | |
| }, | |
| union { | |
| ir_ref op1; | |
| ir_ref ref; | |
| ir_ref prev_const; | |
| } | |
| ); | |
| union { | |
| IR_STRUCT_LOHI( | |
| ir_ref op2, | |
| ir_ref op3 | |
| ); | |
| ir_val val; | |
| }; | |
| } ir_insn; | |
| /* IR Hash Tables API (private) */ | |
| typedef struct _ir_hashtab ir_hashtab; | |
| /* IR String Tables API (implementation in ir_strtab.c) */ | |
| typedef struct _ir_strtab { | |
| void *data; | |
| uint32_t mask; | |
| uint32_t size; | |
| uint32_t count; | |
| uint32_t pos; | |
| char *buf; | |
| uint32_t buf_size; | |
| uint32_t buf_top; | |
| } ir_strtab; | |
| typedef void (*ir_strtab_apply_t)(const char *str, uint32_t len, ir_ref val); | |
| void ir_strtab_init(ir_strtab *strtab, uint32_t count, uint32_t buf_size); | |
| ir_ref ir_strtab_lookup(ir_strtab *strtab, const char *str, uint32_t len, ir_ref val); | |
| ir_ref ir_strtab_find(const ir_strtab *strtab, const char *str, uint32_t len); | |
| ir_ref ir_strtab_update(ir_strtab *strtab, const char *str, uint32_t len, ir_ref val); | |
| const char *ir_strtab_str(const ir_strtab *strtab, ir_ref idx); | |
| const char *ir_strtab_strl(const ir_strtab *strtab, ir_ref idx, size_t *len); | |
| void ir_strtab_apply(const ir_strtab *strtab, ir_strtab_apply_t func); | |
| void ir_strtab_free(ir_strtab *strtab); | |
| /* IR Context Flags */ | |
| /* debug related */ | |
| typedef struct _ir_ctx ir_ctx; | |
| typedef struct _ir_use_list ir_use_list; | |
| typedef struct _ir_block ir_block; | |
| typedef struct _ir_arena ir_arena; | |
| typedef struct _ir_live_interval ir_live_interval; | |
| typedef struct _ir_live_range ir_live_range; | |
| typedef struct _ir_loader ir_loader; | |
| typedef int8_t ir_regs[4]; | |
| typedef void (*ir_snapshot_create_t)(ir_ctx *ctx, ir_ref addr); | |
| typedef const void *(*ir_get_exit_addr_t)(uint32_t exit_num); | |
| typedef const void *(*ir_get_veneer_t)(ir_ctx *ctx, const void *addr); | |
| typedef bool (*ir_set_veneer_t)(ir_ctx *ctx, const void *addr, const void *veneer); | |
| typedef struct _ir_code_buffer { | |
| void *start; | |
| void *end; | |
| void *pos; | |
| } ir_code_buffer; | |
| struct _ir_ctx { | |
| ir_insn *ir_base; /* two directional array - instructions grow down, constants grow up */ | |
| ir_ref insns_count; /* number of instructions stored in instructions buffer */ | |
| ir_ref insns_limit; /* size of allocated instructions buffer (it's extended when overflow) */ | |
| ir_ref consts_count; /* number of constants stored in constants buffer */ | |
| ir_ref consts_limit; /* size of allocated constants buffer (it's extended when overflow) */ | |
| uint32_t flags; /* IR context flags (see IR_* defines above) */ | |
| uint32_t flags2; /* IR context private flags (see IR_* defines in ir_private.h) */ | |
| ir_type ret_type; /* Function return type */ | |
| uint32_t mflags; /* CPU specific flags (see IR_X86_... macros below) */ | |
| int32_t status; /* non-zero error code (see IR_ERROR_... macros), app may use negative codes */ | |
| ir_ref fold_cse_limit; /* CSE finds identical insns backward from "insn_count" to "fold_cse_limit" */ | |
| ir_insn fold_insn; /* temporary storage for folding engine */ | |
| ir_hashtab *binding; | |
| ir_use_list *use_lists; /* def->use lists for each instruction */ | |
| ir_ref *use_edges; /* the actual uses: use = ctx->use_edges[ctx->use_lists[def].refs + n] */ | |
| ir_ref use_edges_count; /* number of elements in use_edges[] array */ | |
| uint32_t cfg_blocks_count; /* number of elements in cfg_blocks[] array */ | |
| uint32_t cfg_edges_count; /* number of elements in cfg_edges[] array */ | |
| ir_block *cfg_blocks; /* list of basic blocks (starts from 1) */ | |
| uint32_t *cfg_edges; /* the actual basic blocks predecessors and successors edges */ | |
| uint32_t *cfg_map; /* map of instructions to basic block number */ | |
| uint32_t *cfg_schedule; /* BB order for code generation */ | |
| uint32_t *rules; /* array of target specific code-generation rules (for each instruction) */ | |
| uint32_t *vregs; | |
| ir_ref vregs_count; | |
| int32_t spill_base; /* base register for special spill area (e.g. PHP VM frame pointer) */ | |
| uint64_t fixed_regset; /* fixed registers, excluded for regular register allocation */ | |
| int32_t fixed_stack_red_zone; /* reusable stack allocated by caller (default 0) */ | |
| int32_t fixed_stack_frame_size; /* fixed stack allocated by generated code for spills and registers save/restore */ | |
| int32_t fixed_call_stack_size; /* fixed preallocated stack for parameter passing (default 0) */ | |
| uint64_t fixed_save_regset; /* registers that always saved/restored in prologue/epilogue */ | |
| uint32_t locals_area_size; | |
| uint32_t gp_reg_params; | |
| uint32_t fp_reg_params; | |
| int32_t param_stack_size; | |
| ir_live_interval **live_intervals; | |
| ir_arena *arena; | |
| ir_live_range *unused_ranges; | |
| ir_regs *regs; | |
| ir_strtab *fused_regs; | |
| ir_ref *prev_ref; | |
| union { | |
| void *data; | |
| ir_ref control; /* used by IR construction API (see ir_builder.h) */ | |
| ir_ref bb_start; /* used by target CPU instruction matcher */ | |
| ir_ref vars; /* list of VARs (used by register allocator) */ | |
| }; | |
| ir_snapshot_create_t snapshot_create; | |
| int32_t stack_frame_alignment; | |
| int32_t stack_frame_size; /* spill stack frame size (used by register allocator and code generator) */ | |
| int32_t call_stack_size; /* stack for parameter passing (used by register allocator and code generator) */ | |
| uint64_t used_preserved_regs; | |
| int32_t ret_slot; | |
| uint32_t rodata_offset; | |
| uint32_t jmp_table_offset; | |
| uint32_t entries_count; | |
| uint32_t *entries; /* array of ENTRY blocks */ | |
| void *osr_entry_loads; | |
| ir_code_buffer *code_buffer; | |
| int32_t deoptimization_exits; | |
| const void *deoptimization_exits_base; | |
| ir_get_exit_addr_t get_exit_addr; | |
| ir_get_veneer_t get_veneer; | |
| ir_set_veneer_t set_veneer; | |
| ir_loader *loader; | |
| ir_strtab strtab; | |
| ir_ref prev_insn_chain[IR_LAST_FOLDABLE_OP + 1]; | |
| ir_ref prev_const_chain[IR_LAST_TYPE]; | |
| }; | |
| /* Basic IR Construction API (implementation in ir.c) */ | |
| void ir_init(ir_ctx *ctx, uint32_t flags, ir_ref consts_limit, ir_ref insns_limit); | |
| void ir_free(ir_ctx *ctx); | |
| void ir_truncate(ir_ctx *ctx); | |
| ir_ref ir_const(ir_ctx *ctx, ir_val val, uint8_t type); | |
| ir_ref ir_const_i8(ir_ctx *ctx, int8_t c); | |
| ir_ref ir_const_i16(ir_ctx *ctx, int16_t c); | |
| ir_ref ir_const_i32(ir_ctx *ctx, int32_t c); | |
| ir_ref ir_const_i64(ir_ctx *ctx, int64_t c); | |
| ir_ref ir_const_u8(ir_ctx *ctx, uint8_t c); | |
| ir_ref ir_const_u16(ir_ctx *ctx, uint16_t c); | |
| ir_ref ir_const_u32(ir_ctx *ctx, uint32_t c); | |
| ir_ref ir_const_u64(ir_ctx *ctx, uint64_t c); | |
| ir_ref ir_const_bool(ir_ctx *ctx, bool c); | |
| ir_ref ir_const_char(ir_ctx *ctx, char c); | |
| ir_ref ir_const_float(ir_ctx *ctx, float c); | |
| ir_ref ir_const_double(ir_ctx *ctx, double c); | |
| ir_ref ir_const_addr(ir_ctx *ctx, uintptr_t c); | |
| ir_ref ir_const_func_addr(ir_ctx *ctx, uintptr_t c, ir_ref proto); | |
| ir_ref ir_const_func(ir_ctx *ctx, ir_ref str, ir_ref proto); | |
| ir_ref ir_const_sym(ir_ctx *ctx, ir_ref str); | |
| ir_ref ir_const_str(ir_ctx *ctx, ir_ref str); | |
| ir_ref ir_unique_const_addr(ir_ctx *ctx, uintptr_t c); | |
| void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted); | |
| ir_ref ir_str(ir_ctx *ctx, const char *s); | |
| ir_ref ir_strl(ir_ctx *ctx, const char *s, size_t len); | |
| const char *ir_get_str(const ir_ctx *ctx, ir_ref idx); | |
| const char *ir_get_strl(const ir_ctx *ctx, ir_ref idx, size_t *len); | |
| typedef struct _ir_proto_t { | |
| uint8_t flags; | |
| uint8_t ret_type; | |
| uint8_t params_count; | |
| uint8_t param_types[5]; | |
| } ir_proto_t; | |
| ir_ref ir_proto_0(ir_ctx *ctx, uint8_t flags, ir_type ret_type); | |
| ir_ref ir_proto_1(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1); | |
| ir_ref ir_proto_2(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2); | |
| ir_ref ir_proto_3(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3); | |
| ir_ref ir_proto_4(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3, | |
| ir_type t4); | |
| ir_ref ir_proto_5(ir_ctx *ctx, uint8_t flags, ir_type ret_type, ir_type t1, ir_type t2, ir_type t3, | |
| ir_type t4, ir_type t5); | |
| ir_ref ir_proto(ir_ctx *ctx, uint8_t flags, ir_type ret_type, uint32_t params_counts, uint8_t *param_types); | |
| ir_ref ir_emit(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3); | |
| ir_ref ir_emit0(ir_ctx *ctx, uint32_t opt); | |
| ir_ref ir_emit1(ir_ctx *ctx, uint32_t opt, ir_ref op1); | |
| ir_ref ir_emit2(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2); | |
| ir_ref ir_emit3(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3); | |
| ir_ref ir_emit_N(ir_ctx *ctx, uint32_t opt, int32_t count); | |
| void ir_set_op(ir_ctx *ctx, ir_ref ref, int32_t n, ir_ref val); | |
| ir_ref ir_get_op(ir_ctx *ctx, ir_ref ref, int32_t n); | |
| IR_ALWAYS_INLINE void ir_set_op1(ir_ctx *ctx, ir_ref ref, ir_ref val) | |
| { | |
| ctx->ir_base[ref].op1 = val; | |
| } | |
| IR_ALWAYS_INLINE void ir_set_op2(ir_ctx *ctx, ir_ref ref, ir_ref val) | |
| { | |
| ctx->ir_base[ref].op2 = val; | |
| } | |
| IR_ALWAYS_INLINE void ir_set_op3(ir_ctx *ctx, ir_ref ref, ir_ref val) | |
| { | |
| ctx->ir_base[ref].op3 = val; | |
| } | |
| IR_ALWAYS_INLINE ir_ref ir_insn_op(const ir_insn *insn, int32_t n) | |
| { | |
| const ir_ref *p = insn->ops + n; | |
| return *p; | |
| } | |
| IR_ALWAYS_INLINE void ir_insn_set_op(ir_insn *insn, int32_t n, ir_ref val) | |
| { | |
| ir_ref *p = insn->ops + n; | |
| *p = val; | |
| } | |
| IR_ALWAYS_INLINE uint32_t ir_insn_find_op(const ir_insn *insn, ir_ref val) | |
| { | |
| int i, n = insn->inputs_count; | |
| for (i = 1; i <= n; i++) { | |
| if (ir_insn_op(insn, i) == val) { | |
| return i; | |
| } | |
| } | |
| return 0; | |
| } | |
| ir_ref ir_fold(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3); | |
| ir_ref ir_fold0(ir_ctx *ctx, uint32_t opt); | |
| ir_ref ir_fold1(ir_ctx *ctx, uint32_t opt, ir_ref op1); | |
| ir_ref ir_fold2(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2); | |
| ir_ref ir_fold3(ir_ctx *ctx, uint32_t opt, ir_ref op1, ir_ref op2, ir_ref op3); | |
| ir_ref ir_param(ir_ctx *ctx, ir_type type, ir_ref region, const char *name, int pos); | |
| ir_ref ir_var(ir_ctx *ctx, ir_type type, ir_ref region, const char *name); | |
| /* IR Binding */ | |
| ir_ref ir_bind(ir_ctx *ctx, ir_ref var, ir_ref def); | |
| ir_ref ir_binding_find(const ir_ctx *ctx, ir_ref ref); | |
| /* Def -> Use lists */ | |
| void ir_build_def_use_lists(ir_ctx *ctx); | |
| /* SSA Construction */ | |
| int ir_mem2ssa(ir_ctx *ctx); | |
| /* CFG - Control Flow Graph (implementation in ir_cfg.c) */ | |
| int ir_build_cfg(ir_ctx *ctx); | |
| int ir_build_dominators_tree(ir_ctx *ctx); | |
| int ir_find_loops(ir_ctx *ctx); | |
| int ir_schedule_blocks(ir_ctx *ctx); | |
| void ir_reset_cfg(ir_ctx *ctx); | |
| /* SCCP - Sparse Conditional Constant Propagation (implementation in ir_sccp.c) */ | |
| int ir_sccp(ir_ctx *ctx); | |
| /* GCM - Global Code Motion and scheduling (implementation in ir_gcm.c) */ | |
| int ir_gcm(ir_ctx *ctx); | |
| int ir_schedule(ir_ctx *ctx); | |
| /* Liveness & Register Allocation (implementation in ir_ra.c) */ | |
| int ir_assign_virtual_registers(ir_ctx *ctx); | |
| int ir_compute_live_ranges(ir_ctx *ctx); | |
| int ir_coalesce(ir_ctx *ctx); | |
| int ir_compute_dessa_moves(ir_ctx *ctx); | |
| int ir_reg_alloc(ir_ctx *ctx); | |
| int ir_regs_number(void); | |
| bool ir_reg_is_int(int32_t reg); | |
| const char *ir_reg_name(int8_t reg, ir_type type); | |
| int32_t ir_get_spill_slot_offset(ir_ctx *ctx, ir_ref ref); | |
| /* Target CPU instruction selection and code generation (see ir_x86.c) */ | |
| int ir_match(ir_ctx *ctx); | |
| void *ir_emit_code(ir_ctx *ctx, size_t *size); | |
| bool ir_needs_thunk(ir_code_buffer *code_buffer, void *addr); | |
| void *ir_emit_thunk(ir_code_buffer *code_buffer, void *addr, size_t *size_ptr); | |
| void ir_fix_thunk(void *thunk_entry, void *addr); | |
| /* Target address resolution (implementation in ir_emit.c) */ | |
| void *ir_resolve_sym_name(const char *name); | |
| /* Target CPU disassembler (implementation in ir_disasm.c) */ | |
| int ir_disasm_init(void); | |
| void ir_disasm_free(void); | |
| void ir_disasm_add_symbol(const char *name, uint64_t addr, uint64_t size); | |
| const char* ir_disasm_find_symbol(uint64_t addr, int64_t *offset); | |
| int ir_disasm(const char *name, | |
| const void *start, | |
| size_t size, | |
| bool asm_addr, | |
| ir_ctx *ctx, | |
| FILE *f); | |
| /* Linux perf interface (implementation in ir_perf.c) */ | |
| int ir_perf_jitdump_open(void); | |
| int ir_perf_jitdump_close(void); | |
| int ir_perf_jitdump_register(const char *name, const void *start, size_t size); | |
| void ir_perf_map_register(const char *name, const void *start, size_t size); | |
| /* GDB JIT interface (implementation in ir_gdb.c) */ | |
| int ir_gdb_register(const char *name, | |
| const void *start, | |
| size_t size, | |
| uint32_t sp_offset, | |
| uint32_t sp_adjustment); | |
| void ir_gdb_unregister_all(void); | |
| bool ir_gdb_present(void); | |
| /* IR load API (implementation in ir_load.c) */ | |
| struct _ir_loader { | |
| uint32_t default_func_flags; | |
| bool (*init_module) (ir_loader *loader, const char *name, const char *filename, const char *target); | |
| bool (*external_sym_dcl) (ir_loader *loader, const char *name, uint32_t flags); | |
| bool (*external_func_dcl) (ir_loader *loader, const char *name, | |
| uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types); | |
| bool (*forward_func_dcl) (ir_loader *loader, const char *name, | |
| uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types); | |
| bool (*sym_dcl) (ir_loader *loader, const char *name, uint32_t flags, size_t size); | |
| bool (*sym_data) (ir_loader *loader, ir_type type, uint32_t count, const void *data); | |
| bool (*sym_data_str) (ir_loader *loader, const char *str, size_t len); | |
| bool (*sym_data_pad) (ir_loader *loader, size_t offset); | |
| bool (*sym_data_ref) (ir_loader *loader, ir_op op, const char *ref, uintptr_t offset); | |
| bool (*sym_data_end) (ir_loader *loader, uint32_t flags); | |
| bool (*func_init) (ir_loader *loader, ir_ctx *ctx, const char *name); | |
| bool (*func_process) (ir_loader *loader, ir_ctx *ctx, const char *name); | |
| void*(*resolve_sym_name) (ir_loader *loader, const char *name, bool add_thunk); | |
| bool (*has_sym) (ir_loader *loader, const char *name); | |
| bool (*add_sym) (ir_loader *loader, const char *name, void *addr); | |
| }; | |
| void ir_loader_init(void); | |
| void ir_loader_free(void); | |
| int ir_load(ir_loader *loader, FILE *f); | |
| /* IR LLVM load API (implementation in ir_load_llvm.c) */ | |
| int ir_load_llvm_bitcode(ir_loader *loader, const char *filename); | |
| int ir_load_llvm_asm(ir_loader *loader, const char *filename); | |
| /* IR save API (implementation in ir_save.c) */ | |
| void ir_print_proto(const ir_ctx *ctx, ir_ref proto, FILE *f); | |
| void ir_save(const ir_ctx *ctx, uint32_t save_flags, FILE *f); | |
| /* IR debug dump API (implementation in ir_dump.c) */ | |
| void ir_dump(const ir_ctx *ctx, FILE *f); | |
| void ir_dump_dot(const ir_ctx *ctx, const char *name, FILE *f); | |
| void ir_dump_use_lists(const ir_ctx *ctx, FILE *f); | |
| void ir_dump_cfg(ir_ctx *ctx, FILE *f); | |
| void ir_dump_cfg_map(const ir_ctx *ctx, FILE *f); | |
| void ir_dump_live_ranges(const ir_ctx *ctx, FILE *f); | |
| void ir_dump_codegen(const ir_ctx *ctx, FILE *f); | |
| /* IR to C conversion (implementation in ir_emit_c.c) */ | |
| int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f); | |
| void ir_emit_c_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f); | |
| void ir_emit_c_sym_decl(const char *name, uint32_t flags, FILE *f); | |
| /* IR to LLVM conversion (implementation in ir_emit_llvm.c) */ | |
| int ir_emit_llvm(ir_ctx *ctx, const char *name, FILE *f); | |
| void ir_emit_llvm_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f); | |
| void ir_emit_llvm_sym_decl(const char *name, uint32_t flags, FILE *f); | |
| /* IR verification API (implementation in ir_check.c) */ | |
| bool ir_check(const ir_ctx *ctx); | |
| void ir_consistency_check(void); | |
| /* Code patching (implementation in ir_patch.c) */ | |
| int ir_patch(const void *code, size_t size, uint32_t jmp_table_size, const void *from_addr, const void *to_addr); | |
| /* CPU information (implementation in ir_cpuinfo.c) */ | |
| uint32_t ir_cpuinfo(void); | |
| /* Deoptimization helpers */ | |
| const void *ir_emit_exitgroup(uint32_t first_exit_point, uint32_t exit_points_per_group, const void *exit_addr, ir_code_buffer *code_buffer, size_t *size_ptr); | |
| /* A reference IR JIT compiler */ | |
| IR_ALWAYS_INLINE void *ir_jit_compile(ir_ctx *ctx, int opt_level, size_t *size) | |
| { | |
| if (opt_level == 0) { | |
| if (ctx->flags & IR_OPT_FOLDING) { | |
| // IR_ASSERT(0 && "IR_OPT_FOLDING is incompatible with -O0"); | |
| return NULL; | |
| } | |
| ctx->flags &= ~(IR_OPT_CFG | IR_OPT_CODEGEN); | |
| ir_build_def_use_lists(ctx); | |
| if (!ir_build_cfg(ctx) | |
| || !ir_match(ctx) | |
| || !ir_assign_virtual_registers(ctx) | |
| || !ir_compute_dessa_moves(ctx)) { | |
| return NULL; | |
| } | |
| return ir_emit_code(ctx, size); | |
| } else if (opt_level > 0) { | |
| if (!(ctx->flags & IR_OPT_FOLDING)) { | |
| // IR_ASSERT(0 && "IR_OPT_FOLDING must be set in ir_init() for -O1 and -O2"); | |
| return NULL; | |
| } | |
| ctx->flags |= IR_OPT_CFG | IR_OPT_CODEGEN; | |
| ir_build_def_use_lists(ctx); | |
| if (ctx->flags & IR_OPT_MEM2SSA) { | |
| if (!ir_build_cfg(ctx) | |
| || !ir_build_dominators_tree(ctx) | |
| || !ir_mem2ssa(ctx)) { | |
| return NULL; | |
| } | |
| ir_reset_cfg(ctx); | |
| } | |
| if (opt_level > 1) { | |
| if (!ir_sccp(ctx)) { | |
| return NULL; | |
| } | |
| } | |
| if (!ctx->cfg_blocks) { | |
| if (!ir_build_cfg(ctx) | |
| || !ir_build_dominators_tree(ctx)) { | |
| return NULL; | |
| } | |
| } | |
| if (!ir_find_loops(ctx) | |
| || !ir_gcm(ctx) | |
| || !ir_schedule(ctx) | |
| || !ir_match(ctx) | |
| || !ir_assign_virtual_registers(ctx) | |
| || !ir_compute_live_ranges(ctx) | |
| || !ir_coalesce(ctx) | |
| || !ir_reg_alloc(ctx) | |
| || !ir_schedule_blocks(ctx)) { | |
| return NULL; | |
| } | |
| return ir_emit_code(ctx, size); | |
| } else { | |
| // IR_ASSERT(0 && "wrong optimization level"); | |
| return NULL; | |
| } | |
| } | |
| /* IR Memmory Allocation */ | |
| void *ir_mem_mmap(size_t size); | |
| int ir_mem_unmap(void *ptr, size_t size); | |
| int ir_mem_protect(void *ptr, size_t size); | |
| int ir_mem_unprotect(void *ptr, size_t size); | |
| int ir_mem_flush(void *ptr, size_t size); | |
| } /* extern "C" */ | |