From 25fab8599059c7bbd497dc25f540a083e3b8870e Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 9 Feb 2025 20:07:57 +1300 Subject: [PATCH] first commit --- README.md | 3 + libgccjit.odin | 285 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 README.md create mode 100644 libgccjit.odin diff --git a/README.md b/README.md new file mode 100644 index 0000000..51f2aff --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# odin-libgccjit + +Odin bindings for libgccjit, idk if it works on Windows but you will need the libgccjit.lib file and probably a dll diff --git a/libgccjit.odin b/libgccjit.odin new file mode 100644 index 0000000..9a6bb15 --- /dev/null +++ b/libgccjit.odin @@ -0,0 +1,285 @@ +package libgccjit + +when ODIN_OS == .Linux { + foreign import libgccjit "system:gccjit" +} else when ODIN_OS == .Windows { + foreign import libgccjit "libgccjit.lib" +} + +@(default_calling_convention = "c", link_prefix = "gcc_jit_") +foreign libgccjit { + context_acquire :: proc() -> ^Context --- + context_release :: proc(ctxt: ^Context) --- + context_set_str_option :: proc(ctxt: ^Context, opt: Str_Option, value: cstring) --- + context_set_int_option :: proc(ctxt: ^Context, opt: Int_Option, value: i32) --- + context_set_bool_option :: proc(ctxt: ^Context, opt: Bool_Option, value: b32) --- + context_set_bool_allow_unreachable_blocks :: proc(ctxt: ^Context, bool_value: b32) --- + context_set_bool_print_errors_to_stderr :: proc(ctxt: ^Context, enabled: b32) --- + context_set_bool_use_external_driver :: proc(ctxt: ^Context, bool_value: b32) --- + context_add_command_line_option :: proc(ctxt: ^Context, optname: cstring) --- + context_add_driver_option :: proc(ctxt: ^Context, optname: cstring) --- + context_compile :: proc(ctxt: ^Context) -> ^Result --- + context_compile_to_file :: proc(ctxt: ^Context, output_kind_p: Output_Kind, output_path: cstring) --- + context_dump_to_file :: proc(ctxt: ^Context, path: cstring, update_locations: i32) --- + context_set_logfile :: proc(ctxt: ^Context, logfile: rawptr, flags: i32, verbosity: i32) --- + context_get_first_error :: proc(ctxt: ^Context) -> cstring --- + context_get_last_error :: proc(ctxt: ^Context) -> cstring --- + result_get_code :: proc(result_p: ^Result, funcname: cstring) -> rawptr --- + result_get_global :: proc(result_p: ^Result, name: cstring) -> rawptr --- + result_release :: proc(result_p: ^Result) --- + object_get_context :: proc(obj: ^Object) -> ^Context --- + object_get_debug_string :: proc(obj: ^Object) -> cstring --- + context_new_location :: proc(ctxt: ^Context, filename: cstring, line: i32, column: i32) -> ^Location --- + location_as_object :: proc(loc: ^Location) -> ^Object --- + type_as_object :: proc(type_p: ^Type) -> ^Object --- + context_get_type :: proc(ctxt: ^Context, type_: Types) -> ^Type --- + context_get_int_type :: proc(ctxt: ^Context, num_bytes: i32, is_signed: i32) -> ^Type --- + type_get_pointer :: proc(type_p: ^Type) -> ^Type --- + type_get_const :: proc(type_p: ^Type) -> ^Type --- + type_get_volatile :: proc(type_p: ^Type) -> ^Type --- + compatible_types :: proc(ltype: ^Type, rtype: ^Type) -> i32 --- + type_get_size :: proc(type_p: ^Type) -> i32 --- + context_new_array_type :: proc(ctxt: ^Context, loc: ^Location, element_type: ^Type, num_elements: i32) -> ^Type --- + context_new_field :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, name: cstring) -> ^Field --- + context_new_bitfield :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, width: i32, name: cstring) -> ^Field --- + field_as_object :: proc(field_p: ^Field) -> ^Object --- + context_new_struct_type :: proc(ctxt: ^Context, loc: ^Location, name: cstring, num_fields: i32, fields: [^]^Field) -> ^Struct --- + context_new_opaque_struct :: proc(ctxt: ^Context, loc: ^Location, name: cstring) -> ^Struct --- + struct_as_type :: proc(struct_type: ^Struct) -> ^Type --- + struct_set_fields :: proc(struct_type: ^Struct, loc: ^Location, num_fields: i32, fields: [^]^Field) --- + struct_get_field :: proc(struct_type: ^Struct, index: u64) -> ^Field --- + struct_get_field_count :: proc(struct_type: ^Struct) -> u64 --- + context_new_union_type :: proc(ctxt: ^Context, loc: ^Location, name: cstring, num_fields: i32, fields: [^]^Field) -> ^Type --- + context_new_function_ptr_type :: proc(ctxt: ^Context, loc: ^Location, return_type: ^Type, num_params: i32, param_types: [^]^Type, is_variadic: b32) -> ^Type --- + context_new_param :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, name: cstring) -> ^Param --- + param_as_object :: proc(param_p: ^Param) -> ^Object --- + param_as_lvalue :: proc(param_p: ^Param) -> ^Lvalue --- + param_as_rvalue :: proc(param_p: ^Param) -> ^Rvalue --- + context_new_function :: proc(ctxt: ^Context, loc: ^Location, kind: Function_Kind, return_type: ^Type, name: cstring, num_params: i32, params: [^]^Param, is_variadic: b32) -> ^Function --- + context_get_builtin_function :: proc(ctxt: ^Context, name: cstring) -> ^Function --- + function_as_object :: proc(func: ^Function) -> ^Object --- + function_get_param :: proc(func: ^Function, index: i32) -> ^Param --- + function_dump_to_dot :: proc(func: ^Function, path: cstring) --- + function_new_block :: proc(func: ^Function, name: cstring) -> ^Block --- + block_as_object :: proc(block_p: ^Block) -> ^Object --- + block_get_function :: proc(block_p: ^Block) -> ^Function --- + context_new_global :: proc(ctxt: ^Context, loc: ^Location, kind: Global_Kind, type_p: ^Type, name: cstring) -> ^Lvalue --- + context_new_struct_constructor :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, num_values: u64, fields: [^]^Field, values: [^]^Rvalue) -> ^Rvalue --- + context_new_union_constructor :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, field_p: ^Field, value: ^Rvalue) -> ^Rvalue --- + context_new_array_constructor :: proc(ctxt: ^Context, loc: ^Location, type_p: ^Type, num_values: u64, values: [^]^Rvalue) -> ^Rvalue --- + global_set_initializer_rvalue :: proc(global: ^Lvalue, init_value: ^Rvalue) -> ^Lvalue --- + global_set_initializer :: proc(global: ^Lvalue, blob: rawptr, num_bytes: u64) -> ^Lvalue --- + lvalue_as_object :: proc(lvalue_p: ^Lvalue) -> ^Object --- + lvalue_as_rvalue :: proc(lvalue_p: ^Lvalue) -> ^Rvalue --- + rvalue_as_object :: proc(rvalue_p: ^Rvalue) -> ^Object --- + rvalue_get_type :: proc(rvalue_p: ^Rvalue) -> ^Type --- + context_new_rvalue_from_int :: proc(ctxt: ^Context, numeric_type: ^Type, value: i32) -> ^Rvalue --- + context_new_rvalue_from_long :: proc(ctxt: ^Context, numeric_type: ^Type, value: i64) -> ^Rvalue --- + context_zero :: proc(ctxt: ^Context, numeric_type: ^Type) -> ^Rvalue --- + context_one :: proc(ctxt: ^Context, numeric_type: ^Type) -> ^Rvalue --- + context_new_rvalue_from_double :: proc(ctxt: ^Context, numeric_type: ^Type, value: f64) -> ^Rvalue --- + context_new_rvalue_from_ptr :: proc(ctxt: ^Context, pointer_type: ^Type, value: rawptr) -> ^Rvalue --- + context_null :: proc(ctxt: ^Context, pointer_type: ^Type) -> ^Rvalue --- + context_new_string_literal :: proc(ctxt: ^Context, value: cstring) -> ^Rvalue --- + context_new_unary_op :: proc(ctxt: ^Context, loc: ^Location, op: Unary_Op, result_type: ^Type, rvalue_p: ^Rvalue) -> ^Rvalue --- + context_new_binary_op :: proc(ctxt: ^Context, loc: ^Location, op: Binary_Op, result_type: ^Type, a: ^Rvalue, b: ^Rvalue) -> ^Rvalue --- + context_new_comparison :: proc(ctxt: ^Context, loc: ^Location, op: Comparison, a: ^Rvalue, b: ^Rvalue) -> ^Rvalue --- + context_new_call :: proc(ctxt: ^Context, loc: ^Location, func: ^Function, numargs: i32, args: [^]^Rvalue) -> ^Rvalue --- + context_new_call_through_ptr :: proc(ctxt: ^Context, loc: ^Location, fn_ptr: ^Rvalue, numargs: i32, args: [^]^Rvalue) -> ^Rvalue --- + context_new_cast :: proc(ctxt: ^Context, loc: ^Location, rvalue_p: ^Rvalue, type_p: ^Type) -> ^Rvalue --- + context_new_bitcast :: proc(ctxt: ^Context, loc: ^Location, rvalue_p: ^Rvalue, type_p: ^Type) -> ^Rvalue --- + lvalue_set_alignment :: proc(lvalue_p: ^Lvalue, bytes: u32) --- + lvalue_get_alignment :: proc(lvalue_p: ^Lvalue) -> u32 --- + context_new_array_access :: proc(ctxt: ^Context, loc: ^Location, ptr: ^Rvalue, index: ^Rvalue) -> ^Lvalue --- + lvalue_access_field :: proc(struct_or_union: ^Lvalue, loc: ^Location, field_p: ^Field) -> ^Lvalue --- + rvalue_access_field :: proc(struct_or_union: ^Rvalue, loc: ^Location, field_p: ^Field) -> ^Rvalue --- + rvalue_dereference_field :: proc(ptr: ^Rvalue, loc: ^Location, field_p: ^Field) -> ^Lvalue --- + rvalue_dereference :: proc(rvalue_p: ^Rvalue, loc: ^Location) -> ^Lvalue --- + lvalue_get_address :: proc(lvalue_p: ^Lvalue, loc: ^Location) -> ^Rvalue --- + lvalue_set_tls_model :: proc(lvalue_p: ^Lvalue, model: Tls_Model) --- + lvalue_set_link_section :: proc(lvalue_p: ^Lvalue, section_name: cstring) --- + lvalue_set_register_name :: proc(lvalue_p: ^Lvalue, reg_name: cstring) --- + function_new_local :: proc(func: ^Function, loc: ^Location, type_p: ^Type, name: cstring) -> ^Lvalue --- + block_add_eval :: proc(block_p: ^Block, loc: ^Location, rvalue_p: ^Rvalue) --- + block_add_assignment :: proc(block_p: ^Block, loc: ^Location, lvalue_p: ^Lvalue, rvalue_p: ^Rvalue) --- + block_add_assignment_op :: proc(block_p: ^Block, loc: ^Location, lvalue_p: ^Lvalue, op: Binary_Op, rvalue_p: ^Rvalue) --- + block_add_comment :: proc(block_p: ^Block, loc: ^Location, text: cstring) --- + block_end_with_conditional :: proc(block_p: ^Block, loc: ^Location, boolval: ^Rvalue, on_true: ^Block, on_false: ^Block) --- + block_end_with_jump :: proc(block_p: ^Block, loc: ^Location, target: ^Block) --- + block_end_with_return :: proc(block_p: ^Block, loc: ^Location, rvalue_p: ^Rvalue) --- + block_end_with_void_return :: proc(block_p: ^Block, loc: ^Location) --- + context_new_case :: proc(ctxt: ^Context, min_value: ^Rvalue, max_value: ^Rvalue, dest_block: ^Block) -> ^Case --- + case_as_object :: proc(case_: ^Case) -> ^Object --- + block_end_with_switch :: proc(block_p: ^Block, loc: ^Location, expr: ^Rvalue, default_block: ^Block, num_cases: i32, cases: [^]^Case) --- + context_new_child_context :: proc(parent_ctxt: ^Context) -> ^Context --- + context_dump_reproducer_to_file :: proc(ctxt: ^Context, path: cstring) --- + context_enable_dump :: proc(ctxt: ^Context, dumpname: cstring, out_ptr: ^cstring) --- + timer_new :: proc() -> ^Timer --- + timer_release :: proc(timer_p: ^Timer) --- + context_set_timer :: proc(ctxt: ^Context, timer_p: ^Timer) --- + context_get_timer :: proc(ctxt: ^Context) -> ^Timer --- + timer_push :: proc(timer_p: ^Timer, item_name: cstring) --- + timer_pop :: proc(timer_p: ^Timer, item_name: cstring) --- + timer_print :: proc(timer_p: ^Timer, f_out: rawptr) --- + rvalue_set_bool_require_tail_call :: proc(call: ^Rvalue, require_tail_call: i32) --- + type_get_aligned :: proc(type_p: ^Type, alignment_in_bytes: u64) -> ^Type --- + type_get_vector :: proc(type_p: ^Type, num_units: u64) -> ^Type --- + function_get_address :: proc(fn: ^Function, loc: ^Location) -> ^Rvalue --- + context_new_rvalue_from_vector :: proc(ctxt: ^Context, loc: ^Location, vec_type: ^Type, num_elements: u64, elements: [^]^Rvalue) -> ^Rvalue --- + version_major :: proc() -> i32 --- + version_minor :: proc() -> i32 --- + version_patchlevel :: proc() -> i32 --- + block_add_extended_asm :: proc(block_p: ^Block, loc: ^Location, asm_template: cstring) -> ^Extended_Asm --- + block_end_with_extended_asm_goto :: proc(block_p: ^Block, loc: ^Location, asm_template: cstring, num_goto_blocks: i32, goto_blocks: [^]^Block, fallthrough_block: ^Block) -> ^Extended_Asm --- + extended_asm_as_object :: proc(ext_asm: ^Extended_Asm) -> ^Object --- + extended_asm_set_volatile_flag :: proc(ext_asm: ^Extended_Asm, flag: i32) --- + extended_asm_set_inline_flag :: proc(ext_asm: ^Extended_Asm, flag: i32) --- + extended_asm_add_output_operand :: proc(ext_asm: ^Extended_Asm, asm_symbolic_name: cstring, constraint: cstring, dest: ^Lvalue) --- + extended_asm_add_input_operand :: proc(ext_asm: ^Extended_Asm, asm_symbolic_name: cstring, constraint: cstring, src: ^Rvalue) --- + extended_asm_add_clobber :: proc(ext_asm: ^Extended_Asm, victim: cstring) --- + context_add_top_level_asm :: proc(ctxt: ^Context, loc: ^Location, asm_stmts: cstring) --- + function_get_return_type :: proc(func: ^Function) -> ^Type --- + function_get_param_count :: proc(func: ^Function) -> u64 --- + type_dyncast_array :: proc(type_p: ^Type) -> ^Type --- + type_is_bool :: proc(type_p: ^Type) -> i32 --- + type_dyncast_function_ptr_type :: proc(type_p: ^Type) -> ^Function_Type --- + function_type_get_return_type :: proc(function_type_p: ^Function_Type) -> ^Type --- + function_type_get_param_count :: proc(function_type_p: ^Function_Type) -> u64 --- + function_type_get_param_type :: proc(function_type_p: ^Function_Type, index: u64) -> ^Type --- + type_is_integral :: proc(type_p: ^Type) -> i32 --- + type_is_pointer :: proc(type_p: ^Type) -> ^Type --- + type_dyncast_vector :: proc(type_p: ^Type) -> ^Vector_Type --- + type_is_struct :: proc(type_p: ^Type) -> ^Struct --- + vector_type_get_num_units :: proc(vector_type_p: ^Vector_Type) -> u64 --- + vector_type_get_element_type :: proc(vector_type_p: ^Vector_Type) -> ^Type --- + type_unqualified :: proc(type_p: ^Type) -> ^Type --- +} + +Str_Option :: enum u32 { + PROGNAME = 0, +} + +Int_Option :: enum u32 { + OPTIMIZATION_LEVEL = 0, +} + +Bool_Option :: enum u32 { + DEBUGINFO = 0, + DUMP_INITIAL_TREE = 1, + DUMP_INITIAL_GIMPLE = 2, + DUMP_GENERATED_CODE = 3, + DUMP_SUMMARY = 4, + DUMP_EVERYTHING = 5, + SELFCHECK_GC = 6, + KEEP_INTERMEDIATES = 7, +} + +Output_Kind :: enum u32 { + ASSEMBLER = 0, + OBJECT_FILE = 1, + DYNAMIC_LIBRARY = 2, + EXECUTABLE = 3, +} + +Types :: enum u32 { + VOID = 0, + VOID_PTR = 1, + BOOL = 2, + CHAR = 3, + SIGNED_CHAR = 4, + UNSIGNED_CHAR = 5, + SHORT = 6, + UNSIGNED_SHORT = 7, + INT = 8, + UNSIGNED_INT = 9, + LONG = 10, + UNSIGNED_LONG = 11, + LONG_LONG = 12, + UNSIGNED_LONG_LONG = 13, + FLOAT = 14, + DOUBLE = 15, + LONG_DOUBLE = 16, + CONST_CHAR_PTR = 17, + SIZE_T = 18, + FILE_PTR = 19, + COMPLEX_FLOAT = 20, + COMPLEX_DOUBLE = 21, + COMPLEX_LONG_DOUBLE = 22, + UINT8_T = 23, + UINT16_T = 24, + UINT32_T = 25, + UINT64_T = 26, + UINT128_T = 27, + INT8_T = 28, + INT16_T = 29, + INT32_T = 30, + INT64_T = 31, + INT128_T = 32, +} + +Function_Kind :: enum u32 { + EXPORTED = 0, + INTERNAL = 1, + IMPORTED = 2, + ALWAYS_INLINE = 3, +} + +Tls_Model :: enum u32 { + NONE = 0, + GLOBAL_DYNAMIC = 1, + LOCAL_DYNAMIC = 2, + INITIAL_EXEC = 3, + LOCAL_EXEC = 4, +} + +Global_Kind :: enum u32 { + EXPORTED = 0, + INTERNAL = 1, + IMPORTED = 2, +} +Unary_Op :: enum u32 { + MINUS = 0, + BITWISE_NEGATE = 1, + LOGICAL_NEGATE = 2, + ABS = 3, +} +Binary_Op :: enum u32 { + PLUS = 0, + MINUS = 1, + MULT = 2, + DIVIDE = 3, + MODULO = 4, + BITWISE_AND = 5, + BITWISE_XOR = 6, + BITWISE_OR = 7, + LOGICAL_AND = 8, + LOGICAL_OR = 9, + LSHIFT = 10, + RSHIFT = 11, +} +Comparison :: enum u32 { + EQ = 0, + NE = 1, + LT = 2, + LE = 3, + GT = 4, + GE = 5, +} + +Context :: struct {} +Result :: struct {} +Object :: struct {} +Location :: struct {} +Type :: struct {} +Field :: struct {} +Struct :: struct {} +Function_Type :: struct {} +Vector_Type :: struct {} +Function :: struct {} +Block :: struct {} +Rvalue :: struct {} +Lvalue :: struct {} +Param :: struct {} +Case :: struct {} +Extended_Asm :: struct {} +Timer :: struct {}