From 6e45a1dc4796591fa0812b4f1c703cfcfbf6bb09 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 8 May 2020 21:48:58 -0400 Subject: [PATCH 001/127] Add context functions --- src/context.rs | 98 ++++++++++++++++++++++++++++++++++++++++++-------- src/lib.rs | 3 +- 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/src/context.rs b/src/context.rs index 605238f..20789d1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,10 +1,12 @@ use std::default::Default; use std::ops::Drop; -use std::ffi::CString; +use std::ffi::{CStr, CString}; use std::marker::PhantomData; use std::mem; use std::ptr; +use std::str::Utf8Error; +use Type; use location::{self, Location}; use structs::{self, Struct}; use types; @@ -19,6 +21,13 @@ use gccjit_sys::gcc_jit_int_option::*; use gccjit_sys::gcc_jit_str_option::*; use gccjit_sys::gcc_jit_bool_option::*; +#[repr(C)] +pub enum GlobalKind { + Exported, + Internal, + Imported, +} + /// Represents an optimization level that the JIT compiler /// will use when compiling your code. #[repr(C)] @@ -29,7 +38,7 @@ pub enum OptimizationLevel { /// any optimizations that take extended periods of time. Limited, /// Performs all optimizations that do not involve a tradeoff - /// of code size for speed. + /// of code size for speed. Standard, /// Performs all optimizations at the Standard level, as well /// as function inlining, loop vectorization, some loop unrolling, @@ -137,7 +146,7 @@ impl<'ctx> Context<'ctx> { c_str.as_ptr()); } } - + /// Sets the optimization level that the JIT compiler will use. /// The higher the optimization level, the longer compilation will /// take. @@ -148,7 +157,39 @@ impl<'ctx> Context<'ctx> { level as i32); } } - + + pub fn set_debug_info(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, + GCC_JIT_BOOL_OPTION_DEBUGINFO, + value as i32); + } + } + + pub fn set_keep_intermediates(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, + GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, + value as i32); + } + } + + pub fn set_dump_everything(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, + GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, + value as i32); + } + } + + pub fn set_dump_initial_gimple(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, + GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, + value as i32); + } + } + /// When set to true, dumps the code that the JIT generates to standard /// out during compilation. pub fn set_dump_code_on_compile(&self, value: bool) { @@ -169,7 +210,7 @@ impl<'ctx> Context<'ctx> { } } } - + /// Compiles the context and saves the result to a file. The /// type of the file is controlled by the OutputKind parameter. pub fn compile_to_file>(&self, kind: OutputKind, file: S) { @@ -181,9 +222,7 @@ impl<'ctx> Context<'ctx> { cstr.as_ptr()); } } - - - + /// Creates a new child context from this context. The child context /// is a fully-featured context, but it has a lifetime that is strictly /// less than the lifetime that spawned it. @@ -195,7 +234,7 @@ impl<'ctx> Context<'ctx> { } } } - + /// Creates a new location for use by gdb when debugging a JIT compiled /// program. The filename, line, and col are used by gdb to "show" your /// source when in a debugger. @@ -213,7 +252,24 @@ impl<'ctx> Context<'ctx> { location::from_ptr(ptr) } } - + + pub fn new_global<'a, S: AsRef>(&self, loc: Option>, kind: GlobalKind, ty: Type<'a>, name: S) -> LValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let cstr = CString::new(name.as_ref()).unwrap(); + let ptr = gccjit_sys::gcc_jit_context_new_global( + self.ptr, + loc_ptr, + mem::transmute(kind), + types::get_ptr(&ty), + cstr.as_ptr()); + lvalue::from_ptr(ptr) + } + } + /// Constructs a new type for any type that implements the Typeable trait. /// This library only provides a handful of implementations of Typeable /// for some primitive types - utilizers of this library are encouraged @@ -222,7 +278,7 @@ impl<'ctx> Context<'ctx> { pub fn new_type<'a, T: types::Typeable>(&'a self) -> types::Type<'a> { ::get_type(self) } - + /// Constructs a new field with an optional source location, type, and name. /// This field can be used to compose unions or structs. pub fn new_field<'a, S: AsRef>(&'a self, @@ -243,7 +299,7 @@ impl<'ctx> Context<'ctx> { field::from_ptr(ptr) } } - + /// Constructs a new array type with a given base element type and a /// size. pub fn new_array_type<'a>(&'a self, @@ -289,7 +345,7 @@ impl<'ctx> Context<'ctx> { structs::from_ptr(ptr) } } - + /// Constructs a new struct type whose fields are not known. Fields can /// be added to this struct later, but only once. pub fn new_opaque_struct_type<'a, S: AsRef>(&'a self, @@ -308,7 +364,7 @@ impl<'ctx> Context<'ctx> { structs::from_ptr(ptr) } } - + /// Creates a new union type from a set of fields. pub fn new_union_type<'a, S: AsRef>(&'a self, loc: Option>, @@ -333,7 +389,7 @@ impl<'ctx> Context<'ctx> { types::from_ptr(ptr) } } - + /// Creates a new function pointer type with the given return type /// parameter types, and an optional location. The last flag can /// make the function variadic, although Rust can't really handle @@ -693,6 +749,18 @@ impl<'ctx> Context<'ctx> { function::from_ptr(ptr) } } + + pub fn get_first_error(&self) -> Result, Utf8Error> { + unsafe { + let str = gccjit_sys::gcc_jit_context_get_first_error(self.ptr); + if str.is_null() { + Ok(None) + } + else { + Ok(Some(CStr::from_ptr(str).to_str()?)) + } + } + } } impl<'ctx> Drop for Context<'ctx> { diff --git a/src/lib.rs b/src/lib.rs index 45db6d3..b5c0a2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,8 +14,6 @@ //! never outlive the Context object from which they came, a requirement //! to using libgccjit correctly. -#![allow(raw_pointer_derive)] - extern crate gccjit_sys; mod types; @@ -31,6 +29,7 @@ mod function; mod block; pub use context::Context; +pub use context::GlobalKind; pub use context::OptimizationLevel; pub use context::CompileResult; pub use context::OutputKind; From 188b2f6570494739e0c1d4b2efb5b54be71c33d1 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 10 May 2020 20:10:11 -0400 Subject: [PATCH 002/127] Use correct size for integer types --- src/types.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/types.rs b/src/types.rs index 967f6e2..e210c70 100644 --- a/src/types.rs +++ b/src/types.rs @@ -84,18 +84,35 @@ macro_rules! typeable_def { typeable_def!((), GCC_JIT_TYPE_VOID); typeable_def!(bool, GCC_JIT_TYPE_BOOL); typeable_def!(char, GCC_JIT_TYPE_CHAR); -typeable_def!(i8, GCC_JIT_TYPE_SIGNED_CHAR); -typeable_def!(u8, GCC_JIT_TYPE_UNSIGNED_CHAR); -typeable_def!(i16, GCC_JIT_TYPE_SHORT); -typeable_def!(u16, GCC_JIT_TYPE_UNSIGNED_SHORT); -typeable_def!(i32, GCC_JIT_TYPE_INT); -typeable_def!(u32, GCC_JIT_TYPE_UNSIGNED_INT); -typeable_def!(i64, GCC_JIT_TYPE_LONG); -typeable_def!(u64, GCC_JIT_TYPE_UNSIGNED_LONG); typeable_def!(f32, GCC_JIT_TYPE_FLOAT); typeable_def!(f64, GCC_JIT_TYPE_DOUBLE); typeable_def!(usize, GCC_JIT_TYPE_SIZE_T); +macro_rules! typeable_int_def { + ($ty:ty, $num_bytes:expr, $signed:expr) => { + impl Typeable for $ty { + fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a> { + unsafe { + let ctx_ptr = context::get_ptr(ctx); + let ptr = gccjit_sys::gcc_jit_context_get_int_type(ctx_ptr, $num_bytes, $signed as i32); + from_ptr(ptr) + } + } + } + } +} + +typeable_int_def!(i8, 1, true); +typeable_int_def!(u8, 1, false); +typeable_int_def!(i16, 2, true); +typeable_int_def!(u16, 2, false); +typeable_int_def!(i32, 4, true); +typeable_int_def!(u32, 4, false); +typeable_int_def!(i64, 8, true); +typeable_int_def!(u64, 8, false); +//typeable_int_def!(i128, 16, true); // FIXME: unsupported by libgccjit for now. +//typeable_int_def!(u128, 16, false); // FIXME: unsupported by libgccjit for now. + /// Specific implementations of Typeable for *mut T and *const T that /// represent void* and const void*, respectively. These impls should /// only be used to expose opaque pointers to gccjit, not to create From 0834bbced5aebdc5f86ee42575c177d6b32b7cec Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 17 May 2020 18:40:29 -0400 Subject: [PATCH 003/127] Add Eq and Hash implementations --- src/field.rs | 2 +- src/rvalue.rs | 2 +- src/types.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/field.rs b/src/field.rs index 311e229..5a4164f 100644 --- a/src/field.rs +++ b/src/field.rs @@ -9,7 +9,7 @@ use object; /// Field represents a field that composes structs or unions. A number of fields /// can be combined to create either a struct or a union. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct Field<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_field diff --git a/src/rvalue.rs b/src/rvalue.rs index a380366..b46ad97 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -20,7 +20,7 @@ use block::BinaryOp; /// An RValue is a value that may or may not have a storage address in gccjit. /// RValues can be dereferenced, used for field accesses, and are the parameters /// given to a majority of the gccjit API calls. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq)] pub struct RValue<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_rvalue diff --git a/src/types.rs b/src/types.rs index 967f6e2..8e2b888 100644 --- a/src/types.rs +++ b/src/types.rs @@ -13,7 +13,7 @@ use gccjit_sys::gcc_jit_types::*; /// A representation of a type, as it is known to the JIT compiler. /// Types can be created through the Typeable trait or they can /// be created dynamically by composing Field types. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct Type<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_type From a566260496fbd46c461c31a259a5b42d4714bac3 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 17 May 2020 18:43:43 -0400 Subject: [PATCH 004/127] Add method to get int type from its size --- src/context.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/context.rs b/src/context.rs index 605238f..8e00d2e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -29,7 +29,7 @@ pub enum OptimizationLevel { /// any optimizations that take extended periods of time. Limited, /// Performs all optimizations that do not involve a tradeoff - /// of code size for speed. + /// of code size for speed. Standard, /// Performs all optimizations at the Standard level, as well /// as function inlining, loop vectorization, some loop unrolling, @@ -137,7 +137,7 @@ impl<'ctx> Context<'ctx> { c_str.as_ptr()); } } - + /// Sets the optimization level that the JIT compiler will use. /// The higher the optimization level, the longer compilation will /// take. @@ -148,7 +148,7 @@ impl<'ctx> Context<'ctx> { level as i32); } } - + /// When set to true, dumps the code that the JIT generates to standard /// out during compilation. pub fn set_dump_code_on_compile(&self, value: bool) { @@ -169,7 +169,7 @@ impl<'ctx> Context<'ctx> { } } } - + /// Compiles the context and saves the result to a file. The /// type of the file is controlled by the OutputKind parameter. pub fn compile_to_file>(&self, kind: OutputKind, file: S) { @@ -181,9 +181,9 @@ impl<'ctx> Context<'ctx> { cstr.as_ptr()); } } - - - + + + /// Creates a new child context from this context. The child context /// is a fully-featured context, but it has a lifetime that is strictly /// less than the lifetime that spawned it. @@ -195,7 +195,7 @@ impl<'ctx> Context<'ctx> { } } } - + /// Creates a new location for use by gdb when debugging a JIT compiled /// program. The filename, line, and col are used by gdb to "show" your /// source when in a debugger. @@ -213,7 +213,7 @@ impl<'ctx> Context<'ctx> { location::from_ptr(ptr) } } - + /// Constructs a new type for any type that implements the Typeable trait. /// This library only provides a handful of implementations of Typeable /// for some primitive types - utilizers of this library are encouraged @@ -222,7 +222,15 @@ impl<'ctx> Context<'ctx> { pub fn new_type<'a, T: types::Typeable>(&'a self) -> types::Type<'a> { ::get_type(self) } - + + pub fn new_int_type<'a>(&'a self, num_bytes: i32, signed: bool) -> types::Type<'a> { + unsafe { + let ctx_ptr = get_ptr(self); + let ptr = gccjit_sys::gcc_jit_context_get_int_type(ctx_ptr, num_bytes, signed as i32); + types::from_ptr(ptr) + } + } + /// Constructs a new field with an optional source location, type, and name. /// This field can be used to compose unions or structs. pub fn new_field<'a, S: AsRef>(&'a self, @@ -243,7 +251,7 @@ impl<'ctx> Context<'ctx> { field::from_ptr(ptr) } } - + /// Constructs a new array type with a given base element type and a /// size. pub fn new_array_type<'a>(&'a self, @@ -289,7 +297,7 @@ impl<'ctx> Context<'ctx> { structs::from_ptr(ptr) } } - + /// Constructs a new struct type whose fields are not known. Fields can /// be added to this struct later, but only once. pub fn new_opaque_struct_type<'a, S: AsRef>(&'a self, @@ -308,7 +316,7 @@ impl<'ctx> Context<'ctx> { structs::from_ptr(ptr) } } - + /// Creates a new union type from a set of fields. pub fn new_union_type<'a, S: AsRef>(&'a self, loc: Option>, @@ -333,7 +341,7 @@ impl<'ctx> Context<'ctx> { types::from_ptr(ptr) } } - + /// Creates a new function pointer type with the given return type /// parameter types, and an optional location. The last flag can /// make the function variadic, although Rust can't really handle From 839ac616b69c002e28680f437ed9d16247c212d2 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 25 Jul 2020 14:16:35 -0400 Subject: [PATCH 005/127] Add new wrapper functions --- Cargo.toml | 2 +- gccjit_sys/Cargo.toml | 2 +- gccjit_sys/src/lib.rs | 18 +++- src/block.rs | 28 ++++++- src/context.rs | 187 +++++++++++++++++++++++++++++++++++++++--- src/function.rs | 31 +++++-- src/lib.rs | 1 + src/lvalue.rs | 2 +- src/object.rs | 27 ++++++ src/parameter.rs | 2 +- src/rvalue.rs | 12 ++- src/structs.rs | 10 +-- src/types.rs | 6 ++ 13 files changed, 297 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 793c49a..e75f8b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,5 @@ readme = "README.md" [dependencies] -gccjit_sys = "0.0.1" +gccjit_sys = { version = "0.0.1", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 77999bd..1a61f71 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -2,7 +2,7 @@ name = "gccjit_sys" version = "0.0.1" authors = ["Sean Gillespie "] -links = "gccjit" +#links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." keywords = ["compiler", "jit", "gcc"] license = "GPL-3.0" diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 7944867..9ba1115 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -2,7 +2,7 @@ extern crate libc; -use libc::{c_char, c_int, FILE, c_void, c_long, c_double}; +use libc::{c_char, c_int, FILE, c_void, c_long, c_double, size_t}; // opaque pointers pub enum gcc_jit_context {} @@ -17,6 +17,7 @@ pub enum gcc_jit_block {} pub enum gcc_jit_rvalue {} pub enum gcc_jit_lvalue {} pub enum gcc_jit_param {} +pub enum gcc_jit_case {} #[repr(C)] pub enum gcc_jit_str_option { @@ -252,6 +253,7 @@ extern { flags: c_int, verbosity: c_int); pub fn gcc_jit_context_get_first_error(ctx: *mut gcc_jit_context) -> *const c_char; + pub fn gcc_jit_context_get_last_error(ctx: *mut gcc_jit_context) -> *const c_char; // result operations pub fn gcc_jit_result_get_code(result: *mut gcc_jit_result, @@ -468,4 +470,18 @@ extern { pub fn gcc_jit_context_new_child_context(parent: *mut gcc_jit_context) -> *mut gcc_jit_context; pub fn gcc_jit_context_dump_reproducer_to_file(parent: *mut gcc_jit_context, path: *const c_char); + + pub fn gcc_jit_context_new_case(ctxt: *mut gcc_jit_context, min_value: *mut gcc_jit_rvalue, max_value: *mut gcc_jit_rvalue, dest_block: *mut gcc_jit_block) -> *mut gcc_jit_case; + pub fn gcc_jit_block_end_with_switch(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, expr: *mut gcc_jit_rvalue, default_block: *mut gcc_jit_block, num_cases: c_int, cases: *mut *mut gcc_jit_case); + pub fn gcc_jit_case_as_object(case_: *mut gcc_jit_case) -> *mut gcc_jit_object; + + pub fn gcc_jit_function_get_address(fun: *mut gcc_jit_function, loc: *mut gcc_jit_location) -> *mut gcc_jit_rvalue; + + pub fn gcc_jit_type_get_vector(typ: *mut gcc_jit_type, num_units: size_t) -> *mut gcc_jit_type; + pub fn gcc_jit_context_new_rvalue_from_vector(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vec_type: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + + pub fn gcc_jit_context_add_command_line_option(ctxt: *mut gcc_jit_context, optname: *const c_char); + pub fn gcc_jit_context_add_driver_option(ctxt: *mut gcc_jit_context, optname: *const c_char); + + pub fn gcc_jit_type_get_aligned(typ: *mut gcc_jit_type, alignment_in_bytes: size_t) -> *mut gcc_jit_type; } diff --git a/src/block.rs b/src/block.rs index e5713d8..3f69436 100644 --- a/src/block.rs +++ b/src/block.rs @@ -3,7 +3,10 @@ use std::ffi::CString; use std::fmt; use std::ptr; use std::mem; -use context::Context; +use std::os::raw::c_int; + +use block; +use context::{Case, Context}; use gccjit_sys; use object::{self, ToObject, Object}; use function::{self, Function}; @@ -120,6 +123,11 @@ impl<'ctx> Block<'ctx> { lvalue::get_ptr(&lvalue), rvalue::get_ptr(&rvalue)); } + + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } /// Performs a binary operation on an LValue and an RValue, assigning @@ -232,11 +240,29 @@ impl<'ctx> Block<'ctx> { loc_ptr); } } + + pub fn end_with_switch>(&self, loc: Option>, expr: T, default_block: Block<'ctx>, cases: &[Case]) { + let expr = expr.to_rvalue(); + let loc_ptr = match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut() + }; + unsafe { + gccjit_sys::gcc_jit_block_end_with_switch(self.ptr, loc_ptr, rvalue::get_ptr(&expr), block::get_ptr(&default_block), + cases.len() as c_int, cases.as_ptr() as *mut *mut _); + } + } } + + pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_block) -> Block<'ctx> { Block { marker: PhantomData, ptr: ptr } } + +pub unsafe fn get_ptr<'ctx>(block: &Block<'ctx>) -> *mut gccjit_sys::gcc_jit_block { + block.ptr +} diff --git a/src/context.rs b/src/context.rs index 3af421e..4e703fb 100644 --- a/src/context.rs +++ b/src/context.rs @@ -6,22 +6,25 @@ use std::mem; use std::ptr; use std::str::Utf8Error; -use Type; -use location::{self, Location}; -use structs::{self, Struct}; -use types; -use field::{self, Field}; -use rvalue::{self, RValue, ToRValue}; -use function::{self, Function, FunctionType}; -use block::{BinaryOp, UnaryOp, ComparisonOp}; -use parameter::{self, Parameter}; -use lvalue::{self, LValue}; use gccjit_sys; use gccjit_sys::gcc_jit_int_option::*; use gccjit_sys::gcc_jit_str_option::*; use gccjit_sys::gcc_jit_bool_option::*; +use block::{self, BinaryOp, Block, UnaryOp, ComparisonOp}; +use field::{self, Field}; +use function::{self, Function, FunctionType}; +use location::{self, Location}; +use lvalue::{self, LValue}; +use object::{self, Object, ToObject}; +use parameter::{self, Parameter}; +use rvalue::{self, RValue, ToRValue}; +use structs::{self, Struct}; +use Type; +use types; + #[repr(C)] +#[derive(Debug)] pub enum GlobalKind { Exported, Internal, @@ -31,6 +34,7 @@ pub enum GlobalKind { /// Represents an optimization level that the JIT compiler /// will use when compiling your code. #[repr(C)] +#[derive(Debug)] pub enum OptimizationLevel { /// No optimizations are applied. None, @@ -110,6 +114,20 @@ impl Drop for CompileResult { } } +pub struct Case<'ctx> { + marker: PhantomData<&'ctx Case<'ctx>>, + ptr: *mut gccjit_sys::gcc_jit_case, +} + +impl<'ctx> ToObject<'ctx> for Case<'ctx> { + fn to_object(&self) -> Object<'ctx> { + unsafe { + let ptr = gccjit_sys::gcc_jit_case_as_object(self.ptr); + object::from_ptr(ptr) + } + } +} + /// Wrapper around a GCC JIT context object that keeps /// the state of the JIT compiler. In GCCJIT, this object /// is responsible for all memory management of JIT data @@ -147,6 +165,20 @@ impl<'ctx> Context<'ctx> { } } + pub fn add_command_line_option>(&self, name: S) { + let c_str = CString::new(name.as_ref()).unwrap(); + unsafe { + gccjit_sys::gcc_jit_context_add_command_line_option(self.ptr, c_str.as_ptr()) + } + } + + pub fn add_driver_option>(&self, name: S) { + let c_str = CString::new(name.as_ref()).unwrap(); + unsafe { + gccjit_sys::gcc_jit_context_add_driver_option(self.ptr, c_str.as_ptr()) + } + } + /// Sets the optimization level that the JIT compiler will use. /// The higher the optimization level, the longer compilation will /// take. @@ -235,6 +267,18 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_case, T: ToRValue<'ctx>>(&self, min_value: S, max_value: T, dest_block: Block<'ctx>) -> Case { + let min_value = min_value.to_rvalue(); + let max_value = max_value.to_rvalue(); + unsafe { + Case { + marker: PhantomData, + ptr: gccjit_sys::gcc_jit_context_new_case(self.ptr, rvalue::get_ptr(&min_value), rvalue::get_ptr(&max_value), + block::get_ptr(&dest_block)), + } + } + } + /// Creates a new location for use by gdb when debugging a JIT compiled /// program. The filename, line, and col are used by gdb to "show" your /// source when in a debugger. @@ -279,6 +323,13 @@ impl<'ctx> Context<'ctx> { ::get_type(self) } + pub fn new_c_type<'a>(&'a self, c_type: CType) -> types::Type<'a> { + unsafe { + let ptr = gccjit_sys::gcc_jit_context_get_type(get_ptr(self), c_type.to_sys()); + types::from_ptr(ptr) + } + } + pub fn new_int_type<'a>(&'a self, num_bytes: i32, signed: bool) -> types::Type<'a> { unsafe { let ctx_ptr = get_ptr(self); @@ -327,6 +378,13 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_vector_type<'a>(&'a self, ty: types::Type<'a>, num_units: u64) -> types::Type<'a> { + unsafe { + let ptr = gccjit_sys::gcc_jit_type_get_vector(types::get_ptr(&ty), num_units); + types::from_ptr(ptr) + } + } + /// Constructs a new struct type with the given name, optional source location, /// and a list of fields. The returned struct is concrete and new fields cannot /// be added to it. @@ -478,6 +536,10 @@ impl<'ctx> Context<'ctx> { types::get_ptr(&ty), rvalue::get_ptr(&left_rvalue), rvalue::get_ptr(&right_rvalue)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -520,6 +582,10 @@ impl<'ctx> Context<'ctx> { mem::transmute(op), rvalue::get_ptr(&left_rvalue), rvalue::get_ptr(&right_rvalue)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -549,6 +615,10 @@ impl<'ctx> Context<'ctx> { function::get_ptr(&func), num_params, params_ptrs.as_mut_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -595,6 +665,10 @@ impl<'ctx> Context<'ctx> { loc_ptr, rvalue::get_ptr(&rvalue), types::get_ptr(&dest_type)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -628,6 +702,21 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_long(self.ptr, types::get_ptr(&ty), value); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + + pub fn new_rvalue_from_vector<'a>(&'a self, loc: Option>, vec_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_vector(self.ptr, loc_ptr, types::get_ptr(&vec_type), elements.len() as _, elements.as_ptr() as *mut *mut _); rvalue::from_ptr(ptr) } } @@ -697,6 +786,10 @@ impl<'ctx> Context<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_context_null(self.ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -754,6 +847,10 @@ impl<'ctx> Context<'ctx> { let cstr = CString::new(name_ref).unwrap(); let ptr = gccjit_sys::gcc_jit_context_get_builtin_function(self.ptr, cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } function::from_ptr(ptr) } } @@ -769,6 +866,30 @@ impl<'ctx> Context<'ctx> { } } } + + pub fn get_last_error(&self) -> Result, Utf8Error> { + unsafe { + let str = gccjit_sys::gcc_jit_context_get_last_error(self.ptr); + if str.is_null() { + Ok(None) + } + else { + Ok(Some(CStr::from_ptr(str).to_str()?)) + } + } + } + + pub fn set_logfile>(&self, logfile: S) { + use std::os::raw::c_void; + + extern { + static stderr: *mut c_void; + } + + unsafe { + gccjit_sys::gcc_jit_context_set_logfile(self.ptr, stderr as *mut _, 0, 0); + } + } } impl<'ctx> Drop for Context<'ctx> { @@ -784,6 +905,13 @@ pub unsafe fn get_ptr<'ctx>(ctx: &'ctx Context<'ctx>) -> *mut gccjit_sys::gcc_ji ctx.ptr } +pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_context) -> Context<'ctx> { + Context { + marker: PhantomData, + ptr: ptr + } +} + #[cfg(test)] mod tests { use super::super::*; @@ -863,3 +991,42 @@ mod tests { }; }*/ } + +pub enum CType { + Bool, + Char, + UChar, + SChar, + Short, + UShort, + Int, + UInt, + Long, + ULong, + LongLong, + ULongLong, + SizeT, +} + +impl CType { + fn to_sys(&self) -> gccjit_sys::gcc_jit_types { + use gccjit_sys::gcc_jit_types::*; + use self::CType::*; + + match *self { + Bool => GCC_JIT_TYPE_BOOL, + Char => GCC_JIT_TYPE_CHAR, + UChar => GCC_JIT_TYPE_UNSIGNED_CHAR, + SChar => GCC_JIT_TYPE_SIGNED_CHAR, + Short => GCC_JIT_TYPE_SHORT, + UShort => GCC_JIT_TYPE_UNSIGNED_SHORT, + Int => GCC_JIT_TYPE_INT, + UInt => GCC_JIT_TYPE_UNSIGNED_INT, + Long => GCC_JIT_TYPE_LONG, + ULong => GCC_JIT_TYPE_UNSIGNED_LONG, + LongLong => GCC_JIT_TYPE_LONG_LONG, + ULongLong => GCC_JIT_TYPE_UNSIGNED_LONG_LONG, + SizeT => GCC_JIT_TYPE_SIZE_T, + } + } +} diff --git a/src/function.rs b/src/function.rs index 22505da..7941ac1 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,19 +1,22 @@ use std::marker::PhantomData; use std::fmt; use std::ptr; -use context::Context; + use gccjit_sys; + +use block::Block; +use block; +use context::Context; +use location::Location; +use location; +use lvalue::LValue; +use lvalue; use object::{ToObject, Object}; use object; use parameter::Parameter; use parameter; +use rvalue::{self, RValue}; use std::ffi::CString; -use block::Block; -use block; -use lvalue::LValue; -use lvalue; -use location::Location; -use location; use types::Type; use types; @@ -24,6 +27,7 @@ use types; /// is a function with external linkage, and always inline is a function that is /// always inlined wherever it is called and cannot be accessed outside of the jit. #[repr(C)] +#[derive(Clone, Copy, Debug)] pub enum FunctionType { /// Defines a function that is "exported" by the JIT and can be called from /// Rust. @@ -43,7 +47,7 @@ pub enum FunctionType { /// Function is gccjit's representation of a function. Functions are constructed /// by constructing basic blocks and connecting them together. Locals are declared /// at the function level. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct Function<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_function @@ -73,6 +77,17 @@ impl<'ctx> Function<'ctx> { } } + pub fn get_address(&self, loc: Option>) -> RValue<'ctx> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_function_get_address(self.ptr, loc_ptr); + rvalue::from_ptr(ptr) + } + } + pub fn dump_to_dot>(&self, path: S) { unsafe { let cstr = CString::new(path.as_ref()).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index b5c0a2b..8f03a6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ mod function; mod block; pub use context::Context; +pub use context::CType; pub use context::GlobalKind; pub use context::OptimizationLevel; pub use context::CompileResult; diff --git a/src/lvalue.rs b/src/lvalue.rs index 84e193f..8ac34e7 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -16,7 +16,7 @@ use location; /// location in memory. A LValue can be converted into an RValue /// through the ToRValue trait. /// It is also possible to get the address of an LValue. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct LValue<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_lvalue diff --git a/src/object.rs b/src/object.rs index 992a114..c20b3f4 100644 --- a/src/object.rs +++ b/src/object.rs @@ -5,6 +5,8 @@ use std::fmt; use std::ffi::CStr; use std::str; +use crate::context; + /// Object represents the root of all objects in gccjit. It is not useful /// in and of itself, but it provides the implementation for Debug /// used by most objects in this library. @@ -25,6 +27,31 @@ impl<'ctx> fmt::Debug for Object<'ctx> { } } +use std::mem::ManuallyDrop; +use std::ops::Deref; + +pub struct ContextRef<'ctx> { + context: ManuallyDrop>, +} + +impl<'ctx> Deref for ContextRef<'ctx> { + type Target = Context<'ctx>; + + fn deref(&self) -> &Self::Target { + &self.context + } +} + +impl<'ctx> Object<'ctx> { + pub fn get_context(&self) -> ContextRef<'ctx> { + unsafe { + ContextRef { + context: ManuallyDrop::new(context::from_ptr(gccjit_sys::gcc_jit_object_get_context(self.ptr))), + } + } + } +} + /// ToObject is a trait implemented by types that can be upcast to Object. pub trait ToObject<'ctx> { fn to_object(&self) -> Object<'ctx>; diff --git a/src/parameter.rs b/src/parameter.rs index 38394e4..bfaab25 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -11,7 +11,7 @@ use lvalue; /// Parameter represents a parameter to a function. A series of parameteres /// can be combined to form a function signature. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq)] pub struct Parameter<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_param diff --git a/src/rvalue.rs b/src/rvalue.rs index b46ad97..de6664f 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -20,7 +20,7 @@ use block::BinaryOp; /// An RValue is a value that may or may not have a storage address in gccjit. /// RValues can be dereferenced, used for field accesses, and are the parameters /// given to a majority of the gccjit API calls. -#[derive(Copy, Clone, PartialEq)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct RValue<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_rvalue @@ -70,6 +70,10 @@ macro_rules! binary_operator_for { types::get_ptr(&ty), self.ptr, rhs_rvalue.ptr); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } from_ptr(ptr) } } @@ -142,7 +146,11 @@ impl<'ctx> RValue<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_rvalue_dereference(self.ptr, loc_ptr); - + + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } lvalue::from_ptr(ptr) } } diff --git a/src/structs.rs b/src/structs.rs index afd3a41..ec11ef9 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -15,7 +15,7 @@ use object::{ToObject, Object}; /// A Struct is gccjit's representation of a composite type. Despite the name, /// Struct can represent either a struct, an union, or an opaque named type. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct Struct<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_struct @@ -46,6 +46,10 @@ impl<'ctx> Struct<'ctx> { num_fields, fields_ptrs.as_mut_ptr()); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } } @@ -69,7 +73,3 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_struct) -> Struct<'ct ptr: ptr } } - - - - diff --git a/src/types.rs b/src/types.rs index 2a83a96..51b6e6e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -57,6 +57,12 @@ impl<'ctx> Type<'ctx> { from_ptr(gccjit_sys::gcc_jit_type_get_volatile(self.ptr)) } } + + pub fn get_aligned(self, alignment_in_bytes: u64) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_type_get_aligned(self.ptr, alignment_in_bytes as _)) + } + } } /// Typeable is a trait for types that have a corresponding type within From 74dc7b3095a215d9fba3f6b54cc47ebf1a82d1e8 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 1 Sep 2020 21:32:56 -0400 Subject: [PATCH 006/127] Add reflection functions --- gccjit_sys/src/lib.rs | 3 +++ src/function.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 9ba1115..8f8f1a6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -484,4 +484,7 @@ extern { pub fn gcc_jit_context_add_driver_option(ctxt: *mut gcc_jit_context, optname: *const c_char); pub fn gcc_jit_type_get_aligned(typ: *mut gcc_jit_type, alignment_in_bytes: size_t) -> *mut gcc_jit_type; + + pub fn gcc_jit_function_get_return_type(func: *mut gcc_jit_function) -> *mut gcc_jit_type; + pub fn gcc_jit_function_get_param_count(func: *mut gcc_jit_function) -> usize; } diff --git a/src/function.rs b/src/function.rs index 7941ac1..945e521 100644 --- a/src/function.rs +++ b/src/function.rs @@ -77,6 +77,18 @@ impl<'ctx> Function<'ctx> { } } + pub fn get_param_count(&self) -> usize { + unsafe { + gccjit_sys::gcc_jit_function_get_param_count(self.ptr) + } + } + + pub fn get_return_type(&self) -> Type<'ctx> { + unsafe { + types::from_ptr(gccjit_sys::gcc_jit_function_get_return_type(self.ptr)) + } + } + pub fn get_address(&self, loc: Option>) -> RValue<'ctx> { unsafe { let loc_ptr = match loc { From 1641dc180f94005a669ac202f15d9484f40e95df Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 17 Oct 2020 10:14:02 -0400 Subject: [PATCH 007/127] Add reflection functions --- gccjit_sys/src/lib.rs | 22 +++++++- src/function.rs | 2 +- src/structs.rs | 12 ++++ src/types.rs | 125 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 8f8f1a6..ae2ea5d 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -2,7 +2,7 @@ extern crate libc; -use libc::{c_char, c_int, FILE, c_void, c_long, c_double, size_t}; +use libc::{c_char, c_int, FILE, c_void, c_long, c_double, size_t, ssize_t}; // opaque pointers pub enum gcc_jit_context {} @@ -18,6 +18,8 @@ pub enum gcc_jit_rvalue {} pub enum gcc_jit_lvalue {} pub enum gcc_jit_param {} pub enum gcc_jit_case {} +pub enum gcc_jit_function_type {} +pub enum gcc_jit_vector_type {} #[repr(C)] pub enum gcc_jit_str_option { @@ -486,5 +488,21 @@ extern { pub fn gcc_jit_type_get_aligned(typ: *mut gcc_jit_type, alignment_in_bytes: size_t) -> *mut gcc_jit_type; pub fn gcc_jit_function_get_return_type(func: *mut gcc_jit_function) -> *mut gcc_jit_type; - pub fn gcc_jit_function_get_param_count(func: *mut gcc_jit_function) -> usize; + pub fn gcc_jit_function_get_param_count(func: *mut gcc_jit_function) -> ssize_t; + + pub fn gcc_jit_type_is_array(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_is_int(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; + pub fn gcc_jit_type_is_pointer(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; + pub fn gcc_jit_type_is_function_ptr_type(typ: *mut gcc_jit_type) -> *mut gcc_jit_function_type; + pub fn gcc_jit_function_type_get_return_type(function_type: *mut gcc_jit_function_type) -> *mut gcc_jit_type; + pub fn gcc_jit_function_type_get_param_count(function_type: *mut gcc_jit_function_type) -> ssize_t; + pub fn gcc_jit_type_is_vector(typ: *mut gcc_jit_type) -> *mut gcc_jit_vector_type; + pub fn gcc_jit_function_type_get_param_type(function_type: *mut gcc_jit_function_type, index: c_int) -> *mut gcc_jit_type; + pub fn gcc_jit_vector_type_get_num_units(vector_type: *mut gcc_jit_vector_type) -> ssize_t; + pub fn gcc_jit_vector_type_get_element_type(vector_type: *mut gcc_jit_vector_type) -> *mut gcc_jit_type; + pub fn gcc_jit_struct_get_field(struct_type: *mut gcc_jit_struct, index: c_int) -> *mut gcc_jit_field; + pub fn gcc_jit_type_is_struct(typ: *mut gcc_jit_type) -> *mut gcc_jit_struct; + pub fn gcc_jit_struct_get_field_count(struct_type: *mut gcc_jit_struct) -> ssize_t; } diff --git a/src/function.rs b/src/function.rs index 945e521..3bcfc7a 100644 --- a/src/function.rs +++ b/src/function.rs @@ -79,7 +79,7 @@ impl<'ctx> Function<'ctx> { pub fn get_param_count(&self) -> usize { unsafe { - gccjit_sys::gcc_jit_function_get_param_count(self.ptr) + gccjit_sys::gcc_jit_function_get_param_count(self.ptr) as usize } } diff --git a/src/structs.rs b/src/structs.rs index ec11ef9..c3effff 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -51,6 +51,18 @@ impl<'ctx> Struct<'ctx> { panic!("{}", error); } } + + pub fn get_field(&self, index: i32) -> Field<'ctx> { + unsafe { + field::from_ptr(gccjit_sys::gcc_jit_struct_get_field(self.ptr, index)) + } + } + + pub fn get_field_count(&self) -> usize { + unsafe { + gccjit_sys::gcc_jit_struct_get_field_count(self.ptr) as usize + } + } } impl<'ctx> ToObject<'ctx> for Struct<'ctx> { diff --git a/src/types.rs b/src/types.rs index 51b6e6e..0ba284c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,6 +7,7 @@ use context::Context; use context; use object; use object::{Object, ToObject}; +use structs::{self, Struct}; use gccjit_sys::gcc_jit_types::*; @@ -19,6 +20,66 @@ pub struct Type<'ctx> { ptr: *mut gccjit_sys::gcc_jit_type } +#[derive(Copy, Clone, Eq, Hash, PartialEq)] +pub struct VectorType<'ctx> { + marker: PhantomData<&'ctx Context<'ctx>>, + ptr: *mut gccjit_sys::gcc_jit_vector_type +} + +impl<'ctx> VectorType<'ctx> { + unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_vector_type) -> VectorType<'ctx> { + VectorType { + marker: PhantomData, + ptr: ptr + } + } + + pub fn get_element_type(&self) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_vector_type_get_element_type(self.ptr)) + } + } + + pub fn get_num_units(&self) -> usize { + unsafe { + gccjit_sys::gcc_jit_vector_type_get_num_units(self.ptr) as usize + } + } +} + +#[derive(Copy, Clone, Eq, Hash, PartialEq)] +pub struct FunctionPtrType<'ctx> { + marker: PhantomData<&'ctx Context<'ctx>>, + ptr: *mut gccjit_sys::gcc_jit_function_type +} + +impl<'ctx> FunctionPtrType<'ctx> { + unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_function_type) -> FunctionPtrType<'ctx> { + FunctionPtrType { + marker: PhantomData, + ptr: ptr + } + } + + pub fn get_return_type(&self) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_function_type_get_return_type(self.ptr)) + } + } + + pub fn get_param_count(&self) -> usize { + unsafe { + gccjit_sys::gcc_jit_function_type_get_param_count(self.ptr) as usize + } + } + + pub fn get_param_type(&self, index: usize) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_function_type_get_param_type(self.ptr, index as _)) + } + } +} + impl<'ctx> ToObject<'ctx> for Type<'ctx> { fn to_object(&self) -> Object<'ctx> { unsafe { @@ -63,6 +124,70 @@ impl<'ctx> Type<'ctx> { from_ptr(gccjit_sys::gcc_jit_type_get_aligned(self.ptr, alignment_in_bytes as _)) } } + + pub fn is_array(self) -> bool { + unsafe { + gccjit_sys::gcc_jit_type_is_array(self.ptr) != 0 + } + } + + pub fn is_bool(self) -> bool { + unsafe { + gccjit_sys::gcc_jit_type_is_bool(self.ptr) != 0 + } + } + + pub fn is_int(self) -> bool { + unsafe { + gccjit_sys::gcc_jit_type_is_int(self.ptr) != 0 + } + } + + pub fn is_vector(self) -> Option> { + unsafe { + let vector_type = gccjit_sys::gcc_jit_type_is_vector(self.ptr); + if vector_type.is_null() { + return None; + } + Some(VectorType::from_ptr(vector_type)) + } + } + + pub fn is_struct(self) -> Option> { + unsafe { + let struct_type = gccjit_sys::gcc_jit_type_is_struct(self.ptr); + if struct_type.is_null() { + return None; + } + Some(structs::from_ptr(struct_type)) + } + } + + pub fn is_function_ptr_type(self) -> Option> { + unsafe { + let function_ptr_type = gccjit_sys::gcc_jit_type_is_function_ptr_type(self.ptr); + if function_ptr_type.is_null() { + return None; + } + Some(FunctionPtrType::from_ptr(function_ptr_type)) + } + } + + pub fn unqualified(&self) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_type_unqualified(self.ptr)) + } + } + + pub fn get_pointee(&self) -> Option> { + unsafe { + let value = gccjit_sys::gcc_jit_type_is_pointer(self.ptr); + if value.is_null() { + return None; + } + Some(from_ptr(value)) + } + } } /// Typeable is a trait for types that have a corresponding type within From 3f30cdd07b7e385ffa48a569755f57a2cdada83e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 17 Oct 2020 11:07:25 -0400 Subject: [PATCH 008/127] Add initializer function --- gccjit_sys/src/lib.rs | 2 ++ src/lvalue.rs | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index ae2ea5d..6bd3c23 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -505,4 +505,6 @@ extern { pub fn gcc_jit_struct_get_field(struct_type: *mut gcc_jit_struct, index: c_int) -> *mut gcc_jit_field; pub fn gcc_jit_type_is_struct(typ: *mut gcc_jit_type) -> *mut gcc_jit_struct; pub fn gcc_jit_struct_get_field_count(struct_type: *mut gcc_jit_struct) -> ssize_t; + + pub fn gcc_jit_global_set_initializer(global: *mut gcc_jit_lvalue, blob: *const c_void, num_bytes: size_t) -> *mut gcc_jit_lvalue; } diff --git a/src/lvalue.rs b/src/lvalue.rs index 8ac34e7..b966a57 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -89,6 +89,13 @@ impl<'ctx> LValue<'ctx> { rvalue::from_ptr(ptr) } } + + /// Set the initialization value for a global variable. + pub fn global_set_initializer(&self, blob: &[u8]) { + unsafe { + gccjit_sys::gcc_jit_global_set_initializer(self.ptr, blob.as_ptr() as _, blob.len() as _); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From d47429baf195f599667b18585bc29603c0abd48a Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 18 Oct 2020 12:59:20 -0400 Subject: [PATCH 009/127] Rename is_int to is_integral --- gccjit_sys/src/lib.rs | 2 +- src/types.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 6bd3c23..317680b 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -492,7 +492,7 @@ extern { pub fn gcc_jit_type_is_array(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; - pub fn gcc_jit_type_is_int(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_is_integral(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_pointer(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_function_ptr_type(typ: *mut gcc_jit_type) -> *mut gcc_jit_function_type; diff --git a/src/types.rs b/src/types.rs index 0ba284c..3381ca7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -137,9 +137,9 @@ impl<'ctx> Type<'ctx> { } } - pub fn is_int(self) -> bool { + pub fn is_integral(self) -> bool { unsafe { - gccjit_sys::gcc_jit_type_is_int(self.ptr) != 0 + gccjit_sys::gcc_jit_type_is_integral(self.ptr) != 0 } } From 391cda18bcdf7653c1d4450939f8a08d1cd92e20 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 3 Nov 2020 14:58:44 -0500 Subject: [PATCH 010/127] Add checks --- src/structs.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/structs.rs b/src/structs.rs index c3effff..5b45a71 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -53,9 +53,19 @@ impl<'ctx> Struct<'ctx> { } pub fn get_field(&self, index: i32) -> Field<'ctx> { - unsafe { - field::from_ptr(gccjit_sys::gcc_jit_struct_get_field(self.ptr, index)) + let field = unsafe { + let ptr = gccjit_sys::gcc_jit_struct_get_field(self.ptr, index); + #[cfg(debug_assertions)] + if ptr.is_null() { + panic!("Null ptr in get_field() from struct: {:?}", self); + } + field::from_ptr(ptr) + }; + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); } + field } pub fn get_field_count(&self) -> usize { From f6e1b6f26b38eb26c625403840d5fab8030f8b53 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 13 Nov 2020 21:14:58 -0500 Subject: [PATCH 011/127] Add new inline asm API --- gccjit_sys/src/lib.rs | 11 ++++++++ src/asm.rs | 64 +++++++++++++++++++++++++++++++++++++++++++ src/block.rs | 20 +++++++++++++- src/context.rs | 12 ++++++++ src/lib.rs | 1 + 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/asm.rs diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 317680b..e83fdf0 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -20,6 +20,7 @@ pub enum gcc_jit_param {} pub enum gcc_jit_case {} pub enum gcc_jit_function_type {} pub enum gcc_jit_vector_type {} +pub enum gcc_jit_extended_asm {} #[repr(C)] pub enum gcc_jit_str_option { @@ -507,4 +508,14 @@ extern { pub fn gcc_jit_struct_get_field_count(struct_type: *mut gcc_jit_struct) -> ssize_t; pub fn gcc_jit_global_set_initializer(global: *mut gcc_jit_lvalue, blob: *const c_void, num_bytes: size_t) -> *mut gcc_jit_lvalue; + + + pub fn gcc_jit_block_end_with_extended_asm_goto(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char, num_goto_blocks: c_int, goto_blocks: *mut *mut gcc_jit_block, fallthrough_block: *mut gcc_jit_block) -> *mut gcc_jit_extended_asm; + pub fn gcc_jit_extended_asm_as_object(ext_asm: *mut gcc_jit_extended_asm) -> *mut gcc_jit_object; + pub fn gcc_jit_extended_asm_set_volatile_flag(ext_asm: *mut gcc_jit_extended_asm, flag: c_int); + pub fn gcc_jit_extended_asm_set_inline_flag(ext_asm: *mut gcc_jit_extended_asm, flag: c_int); + pub fn gcc_jit_extended_asm_add_output_operand(ext_asm: *mut gcc_jit_extended_asm, asm_symbolic_name: *const c_char, constraint: *const c_char, dest: *mut gcc_jit_lvalue); + pub fn gcc_jit_extended_asm_add_input_operand(ext_asm: *mut gcc_jit_extended_asm, asm_symbolic_name: *const c_char, constraint: *const c_char, src: *mut gcc_jit_rvalue); + pub fn gcc_jit_extended_asm_add_clobber(ext_asm: *mut gcc_jit_extended_asm, victim: *const c_char); + pub fn gcc_jit_context_add_top_level_asm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, asm_stmts: *const c_char); } diff --git a/src/asm.rs b/src/asm.rs new file mode 100644 index 0000000..5ce71a4 --- /dev/null +++ b/src/asm.rs @@ -0,0 +1,64 @@ +use std::ffi::CStr; +use std::marker::PhantomData; +use std::os::raw::c_int; + +use {Context, LValue, Object, RValue, ToObject, lvalue, object, rvalue}; + +#[derive(Copy, Clone)] +pub struct ExtendedAsm<'ctx> { + marker: PhantomData<&'ctx Context<'ctx>>, + ptr: *mut gccjit_sys::gcc_jit_extended_asm +} + +impl<'ctx> ToObject<'ctx> for ExtendedAsm<'ctx> { + fn to_object(&self) -> Object<'ctx> { + unsafe { + let ptr = gccjit_sys::gcc_jit_extended_asm_as_object(self.ptr); + object::from_ptr(ptr) + } + } +} + +impl<'ctx> ExtendedAsm<'ctx> { + pub fn gcc_jit_extended_asm_set_volatile_flag(&self, flag: bool) { + unsafe { + gccjit_sys::gcc_jit_extended_asm_set_volatile_flag(self.ptr, flag as c_int); + } + } + + pub fn set_inline_flag(&self, flag: bool) { + unsafe { + gccjit_sys::gcc_jit_extended_asm_set_inline_flag(self.ptr, flag as c_int); + } + } + + pub fn add_output_operand(&self, asm_symbolic_name: &str, constraint: &str, dest: LValue<'ctx>) { + let asm_symbolic_name = CStr::from_bytes_with_nul(asm_symbolic_name.as_bytes()).expect("asm symbolic name to cstring"); + let constraint = CStr::from_bytes_with_nul(constraint.as_bytes()).expect("constraint to cstring"); + unsafe { + gccjit_sys::gcc_jit_extended_asm_add_output_operand(self.ptr, asm_symbolic_name.as_ptr(), constraint.as_ptr(), lvalue::get_ptr(&dest)); + } + } + + pub fn add_input_operand(&self, asm_symbolic_name: &str, constraint: &str, src: RValue<'ctx>) { + let asm_symbolic_name = CStr::from_bytes_with_nul(asm_symbolic_name.as_bytes()).expect("asm symbolic name to cstring"); + let constraint = CStr::from_bytes_with_nul(constraint.as_bytes()).expect("constraint to cstring"); + unsafe { + gccjit_sys::gcc_jit_extended_asm_add_input_operand(self.ptr, asm_symbolic_name.as_ptr(), constraint.as_ptr(), rvalue::get_ptr(&src)); + } + } + + pub fn add_clobber(&self, victim: &str) { + let victim = CStr::from_bytes_with_nul(victim.as_bytes()).expect("victim to cstring"); + unsafe { + gccjit_sys::gcc_jit_extended_asm_add_clobber(self.ptr, victim.as_ptr()); + } + } + + pub unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_extended_asm) -> Self { + Self { + marker: PhantomData, + ptr: ptr + } + } +} diff --git a/src/block.rs b/src/block.rs index 3f69436..a916e4d 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,10 +1,11 @@ use std::marker::PhantomData; -use std::ffi::CString; +use std::ffi::{CStr, CString}; use std::fmt; use std::ptr; use std::mem; use std::os::raw::c_int; +use asm::ExtendedAsm; use block; use context::{Case, Context}; use gccjit_sys; @@ -252,6 +253,23 @@ impl<'ctx> Block<'ctx> { cases.len() as c_int, cases.as_ptr() as *mut *mut _); } } + + pub fn end_with_extended_asm_goto(&self, loc: Option>, asm_template: &str, goto_blocks: &[Block<'ctx>], fallthrough_block: Option>) -> ExtendedAsm { + let asm_template = CStr::from_bytes_with_nul(asm_template.as_bytes()).expect("asm template to cstring"); + let loc_ptr = + match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut(), + }; + let fallthrough_block_ptr = + match fallthrough_block { + Some(ref block) => unsafe { get_ptr(block) }, + None => ptr::null_mut(), + }; + unsafe { + ExtendedAsm::from_ptr(gccjit_sys::gcc_jit_block_end_with_extended_asm_goto(self.ptr, loc_ptr, asm_template.as_ptr(), goto_blocks.len() as c_int, goto_blocks.as_ptr() as *mut _, fallthrough_block_ptr)) + } + } } diff --git a/src/context.rs b/src/context.rs index 4e703fb..bb50aa0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -890,6 +890,18 @@ impl<'ctx> Context<'ctx> { gccjit_sys::gcc_jit_context_set_logfile(self.ptr, stderr as *mut _, 0, 0); } } + + pub fn add_top_level_asm(&self, loc: Option>, asm_stmts: &str) { + let asm_stmts = CStr::from_bytes_with_nul(asm_stmts.as_bytes()).expect("asm_stmts to cstring"); + let loc_ptr = + match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut(), + }; + unsafe { + gccjit_sys::gcc_jit_context_add_top_level_asm(self.ptr, loc_ptr, asm_stmts.as_ptr()); + } + } } impl<'ctx> Drop for Context<'ctx> { diff --git a/src/lib.rs b/src/lib.rs index 8f03a6e..c177a70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ extern crate gccjit_sys; +mod asm; mod types; mod context; mod object; From cb32f127e780995d91790272daca61b40e07aaca Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 24 Apr 2021 20:29:48 -0400 Subject: [PATCH 012/127] Fix wrong type in FFI --- gccjit_sys/src/lib.rs | 2 +- src/rvalue.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index e83fdf0..f4c5e0d 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -430,7 +430,7 @@ extern { pub fn gcc_jit_rvalue_access_field(struct_or_union: *mut gcc_jit_rvalue, loc: *mut gcc_jit_location, - field: *mut gcc_jit_field) -> *mut gcc_jit_lvalue; + field: *mut gcc_jit_field) -> *mut gcc_jit_rvalue; pub fn gcc_jit_rvalue_dereference_field(ptr: *mut gcc_jit_rvalue, loc: *mut gcc_jit_location, field: *mut gcc_jit_field) -> *mut gcc_jit_lvalue; diff --git a/src/rvalue.rs b/src/rvalue.rs index de6664f..218addc 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -106,7 +106,7 @@ impl<'ctx> RValue<'ctx> { /// C's x.f. pub fn access_field(&self, loc: Option>, - field: Field<'ctx>) -> LValue<'ctx> { + field: Field<'ctx>) -> RValue<'ctx> { let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, None => ptr::null_mut() @@ -115,7 +115,7 @@ impl<'ctx> RValue<'ctx> { let ptr = gccjit_sys::gcc_jit_rvalue_access_field(self.ptr, loc_ptr, field::get_ptr(&field)); - lvalue::from_ptr(ptr) + from_ptr(ptr) } } From 33a8c93c3faf1b7757c9e58085bb3b267e07c05d Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 24 Apr 2021 20:30:29 -0400 Subject: [PATCH 013/127] Add asm functions --- examples/factorial/factorial.dot | 2 +- examples/factorial/src/main.rs | 1 - gccjit_sys/src/lib.rs | 1 + src/asm.rs | 23 ++++++++++++++--------- src/block.rs | 16 ++++++++++++++-- src/context.rs | 4 ++-- src/function.rs | 2 -- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/examples/factorial/factorial.dot b/examples/factorial/factorial.dot index f3c5c6f..33f9113 100644 --- a/examples/factorial/factorial.dot +++ b/examples/factorial/factorial.dot @@ -4,7 +4,7 @@ if\ (n\ ==\ (int)0)\ goto\ ret_block;\ else\ goto\ recurse_block;\l\ }"]; block_1 [shape=record,style=filled,fillcolor=white,label="{recurse_block:\l\ -return\ n\ *\ factorial\ (n\ -\ (int)1);\l\ +return\ n\ *\ factorial\ ((n\ -\ (int)1));\l\ }"]; block_2 [shape=record,style=filled,fillcolor=white,label="{ret_block:\l\ diff --git a/examples/factorial/src/main.rs b/examples/factorial/src/main.rs index e36df8e..f0a0e29 100644 --- a/examples/factorial/src/main.rs +++ b/examples/factorial/src/main.rs @@ -9,7 +9,6 @@ use gccjit::ComparisonOp; use std::default::Default; use std::mem; - fn main() { let context = Context::default(); context.set_dump_code_on_compile(true); diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index f4c5e0d..7164957 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -518,4 +518,5 @@ extern { pub fn gcc_jit_extended_asm_add_input_operand(ext_asm: *mut gcc_jit_extended_asm, asm_symbolic_name: *const c_char, constraint: *const c_char, src: *mut gcc_jit_rvalue); pub fn gcc_jit_extended_asm_add_clobber(ext_asm: *mut gcc_jit_extended_asm, victim: *const c_char); pub fn gcc_jit_context_add_top_level_asm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, asm_stmts: *const c_char); + pub fn gcc_jit_block_add_extended_asm(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char) -> *mut gcc_jit_extended_asm; } diff --git a/src/asm.rs b/src/asm.rs index 5ce71a4..a9c17b8 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -1,4 +1,4 @@ -use std::ffi::CStr; +use std::ffi::CString; use std::marker::PhantomData; use std::os::raw::c_int; @@ -20,7 +20,7 @@ impl<'ctx> ToObject<'ctx> for ExtendedAsm<'ctx> { } impl<'ctx> ExtendedAsm<'ctx> { - pub fn gcc_jit_extended_asm_set_volatile_flag(&self, flag: bool) { + pub fn set_volatile_flag(&self, flag: bool) { unsafe { gccjit_sys::gcc_jit_extended_asm_set_volatile_flag(self.ptr, flag as c_int); } @@ -32,24 +32,29 @@ impl<'ctx> ExtendedAsm<'ctx> { } } - pub fn add_output_operand(&self, asm_symbolic_name: &str, constraint: &str, dest: LValue<'ctx>) { - let asm_symbolic_name = CStr::from_bytes_with_nul(asm_symbolic_name.as_bytes()).expect("asm symbolic name to cstring"); - let constraint = CStr::from_bytes_with_nul(constraint.as_bytes()).expect("constraint to cstring"); + pub fn add_output_operand(&self, asm_symbolic_name: Option<&str>, constraint: &str, dest: LValue<'ctx>) { + let asm_symbolic_name = asm_symbolic_name.map(|name| CString::new(name).unwrap()); + let asm_symbolic_name = + match asm_symbolic_name { + Some(name) => name.as_ptr(), + None => std::ptr::null_mut(), + }; + let constraint = CString::new(constraint).unwrap(); unsafe { - gccjit_sys::gcc_jit_extended_asm_add_output_operand(self.ptr, asm_symbolic_name.as_ptr(), constraint.as_ptr(), lvalue::get_ptr(&dest)); + gccjit_sys::gcc_jit_extended_asm_add_output_operand(self.ptr, asm_symbolic_name, constraint.as_ptr(), lvalue::get_ptr(&dest)); } } pub fn add_input_operand(&self, asm_symbolic_name: &str, constraint: &str, src: RValue<'ctx>) { - let asm_symbolic_name = CStr::from_bytes_with_nul(asm_symbolic_name.as_bytes()).expect("asm symbolic name to cstring"); - let constraint = CStr::from_bytes_with_nul(constraint.as_bytes()).expect("constraint to cstring"); + let asm_symbolic_name = CString::new(asm_symbolic_name).unwrap(); + let constraint = CString::new(constraint).unwrap(); unsafe { gccjit_sys::gcc_jit_extended_asm_add_input_operand(self.ptr, asm_symbolic_name.as_ptr(), constraint.as_ptr(), rvalue::get_ptr(&src)); } } pub fn add_clobber(&self, victim: &str) { - let victim = CStr::from_bytes_with_nul(victim.as_bytes()).expect("victim to cstring"); + let victim = CString::new(victim).unwrap(); unsafe { gccjit_sys::gcc_jit_extended_asm_add_clobber(self.ptr, victim.as_ptr()); } diff --git a/src/block.rs b/src/block.rs index a916e4d..53c96fa 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,5 +1,5 @@ use std::marker::PhantomData; -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::fmt; use std::ptr; use std::mem; @@ -254,8 +254,20 @@ impl<'ctx> Block<'ctx> { } } + pub fn add_extended_asm(&self, loc: Option>, asm_template: &str) -> ExtendedAsm { + let asm_template = CString::new(asm_template).unwrap(); + let loc_ptr = + match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut(), + }; + unsafe { + ExtendedAsm::from_ptr(gccjit_sys::gcc_jit_block_add_extended_asm(self.ptr, loc_ptr, asm_template.as_ptr())) + } + } + pub fn end_with_extended_asm_goto(&self, loc: Option>, asm_template: &str, goto_blocks: &[Block<'ctx>], fallthrough_block: Option>) -> ExtendedAsm { - let asm_template = CStr::from_bytes_with_nul(asm_template.as_bytes()).expect("asm template to cstring"); + let asm_template = CString::new(asm_template).unwrap(); let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, diff --git a/src/context.rs b/src/context.rs index bb50aa0..e1c348a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -147,7 +147,7 @@ impl Default for Context<'static> { unsafe { Context { marker: PhantomData, - ptr: gccjit_sys::gcc_jit_context_acquire() + ptr: gccjit_sys::gcc_jit_context_acquire(), } } } @@ -892,7 +892,7 @@ impl<'ctx> Context<'ctx> { } pub fn add_top_level_asm(&self, loc: Option>, asm_stmts: &str) { - let asm_stmts = CStr::from_bytes_with_nul(asm_stmts.as_bytes()).expect("asm_stmts to cstring"); + let asm_stmts = CString::new(asm_stmts).unwrap(); let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, diff --git a/src/function.rs b/src/function.rs index 3bcfc7a..95da090 100644 --- a/src/function.rs +++ b/src/function.rs @@ -145,5 +145,3 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_function) -> Function pub unsafe fn get_ptr<'ctx>(loc: &Function<'ctx>) -> *mut gccjit_sys::gcc_jit_function { loc.ptr } - - From fe95d4a87a663e6041b3d144a88c49d8597b193e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 2 May 2021 13:24:40 -0400 Subject: [PATCH 014/127] Fix type of argument in add_input_operand --- src/asm.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/asm.rs b/src/asm.rs index a9c17b8..501c618 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -45,11 +45,16 @@ impl<'ctx> ExtendedAsm<'ctx> { } } - pub fn add_input_operand(&self, asm_symbolic_name: &str, constraint: &str, src: RValue<'ctx>) { - let asm_symbolic_name = CString::new(asm_symbolic_name).unwrap(); + pub fn add_input_operand(&self, asm_symbolic_name: Option<&str>, constraint: &str, src: RValue<'ctx>) { + let asm_symbolic_name = asm_symbolic_name.map(|name| CString::new(name).unwrap()); + let asm_symbolic_name = + match asm_symbolic_name { + Some(name) => name.as_ptr(), + None => std::ptr::null_mut(), + }; let constraint = CString::new(constraint).unwrap(); unsafe { - gccjit_sys::gcc_jit_extended_asm_add_input_operand(self.ptr, asm_symbolic_name.as_ptr(), constraint.as_ptr(), rvalue::get_ptr(&src)); + gccjit_sys::gcc_jit_extended_asm_add_input_operand(self.ptr, asm_symbolic_name, constraint.as_ptr(), rvalue::get_ptr(&src)); } } From a7b8372f8b4e33af77e1a4742766a16a591a5752 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 10 May 2021 19:53:12 -0400 Subject: [PATCH 015/127] Add support for sized integer types --- gccjit_sys/src/lib.rs | 11 +++++++++++ src/context.rs | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 7164957..fe9d789 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -81,6 +81,17 @@ pub enum gcc_jit_types { /* C99's "long long" and "unsigned long long". */ GCC_JIT_TYPE_LONG_LONG, /* signed */ GCC_JIT_TYPE_UNSIGNED_LONG_LONG, + GCC_JIT_TYPE_UINT8_T, + GCC_JIT_TYPE_UINT16_T, + GCC_JIT_TYPE_UINT32_T, + GCC_JIT_TYPE_UINT64_T, + GCC_JIT_TYPE_UINT128_T, + GCC_JIT_TYPE_INT8_T, + GCC_JIT_TYPE_INT16_T, + GCC_JIT_TYPE_INT32_T, + GCC_JIT_TYPE_INT64_T, + GCC_JIT_TYPE_INT128_T, + /* Floating-point types */ GCC_JIT_TYPE_FLOAT, GCC_JIT_TYPE_DOUBLE, diff --git a/src/context.rs b/src/context.rs index e1c348a..17502f2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1018,6 +1018,16 @@ pub enum CType { LongLong, ULongLong, SizeT, + Int8t, + Int16t, + Int32t, + Int64t, + Int128t, + UInt8t, + UInt16t, + UInt32t, + UInt64t, + UInt128t, } impl CType { @@ -1039,6 +1049,16 @@ impl CType { LongLong => GCC_JIT_TYPE_LONG_LONG, ULongLong => GCC_JIT_TYPE_UNSIGNED_LONG_LONG, SizeT => GCC_JIT_TYPE_SIZE_T, + Int8t => GCC_JIT_TYPE_INT8_T, + Int16t => GCC_JIT_TYPE_INT16_T, + Int32t => GCC_JIT_TYPE_INT32_T, + Int64t => GCC_JIT_TYPE_INT64_T, + Int128t => GCC_JIT_TYPE_INT128_T, + UInt8t => GCC_JIT_TYPE_UINT8_T, + UInt16t => GCC_JIT_TYPE_UINT16_T, + UInt32t => GCC_JIT_TYPE_UINT32_T, + UInt64t => GCC_JIT_TYPE_UINT64_T, + UInt128t => GCC_JIT_TYPE_UINT128_T, } } } From 991dcc8b570193722163d0c601d6e5008e41749d Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 12 May 2021 06:59:17 -0400 Subject: [PATCH 016/127] Add support for TLS variables --- gccjit_sys/src/lib.rs | 11 +++++++++++ src/lib.rs | 2 +- src/lvalue.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index fe9d789..bcd03a2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -22,6 +22,15 @@ pub enum gcc_jit_function_type {} pub enum gcc_jit_vector_type {} pub enum gcc_jit_extended_asm {} +#[repr(C)] +pub enum gcc_jit_tls_model { + GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC, + GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC, + GCC_JIT_TLS_MODEL_INITIAL_EXEC, + GCC_JIT_TLS_MODEL_LOCAL_EXEC, + GCC_JIT_TLS_MODEL_DEFAULT, +} + #[repr(C)] pub enum gcc_jit_str_option { GCC_JIT_STR_OPTION_PROGNAME, @@ -530,4 +539,6 @@ extern { pub fn gcc_jit_extended_asm_add_clobber(ext_asm: *mut gcc_jit_extended_asm, victim: *const c_char); pub fn gcc_jit_context_add_top_level_asm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, asm_stmts: *const c_char); pub fn gcc_jit_block_add_extended_asm(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char) -> *mut gcc_jit_extended_asm; + + pub fn gcc_jit_lvalue_set_tls_model(lvalue: *mut gcc_jit_lvalue, model: gcc_jit_tls_model); } diff --git a/src/lib.rs b/src/lib.rs index c177a70..7223b3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ pub use types::Type; pub use types::Typeable; pub use field::Field; pub use structs::Struct; -pub use lvalue::{LValue, ToLValue}; +pub use lvalue::{LValue, TlsModel, ToLValue}; pub use rvalue::{RValue, ToRValue}; pub use parameter::Parameter; pub use function::{Function, FunctionType}; diff --git a/src/lvalue.rs b/src/lvalue.rs index b966a57..61f3c81 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -12,6 +12,29 @@ use field; use location::Location; use location; +#[derive(Clone, Copy, Debug)] +pub enum TlsModel { + GlobalDynamic, + LocalDynamic, + InitialExec, + LocalExec, + Default, +} + +impl TlsModel { + fn to_sys(&self) -> gccjit_sys::gcc_jit_tls_model { + use gccjit_sys::gcc_jit_tls_model::*; + + match *self { + TlsModel::GlobalDynamic => GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC, + TlsModel::LocalDynamic => GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC, + TlsModel::InitialExec => GCC_JIT_TLS_MODEL_INITIAL_EXEC, + TlsModel::LocalExec => GCC_JIT_TLS_MODEL_LOCAL_EXEC, + TlsModel::Default => GCC_JIT_TLS_MODEL_DEFAULT, + } + } +} + /// An LValue in gccjit represents a value that has a concrete /// location in memory. A LValue can be converted into an RValue /// through the ToRValue trait. @@ -96,6 +119,12 @@ impl<'ctx> LValue<'ctx> { gccjit_sys::gcc_jit_global_set_initializer(self.ptr, blob.as_ptr() as _, blob.len() as _); } } + + pub fn set_tls_model(&self, model: TlsModel) { + unsafe { + gccjit_sys::gcc_jit_lvalue_set_tls_model(self.ptr, model.to_sys()); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From d4b659dfc4db5411c282b4913f8e599d528d0ee7 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 16 May 2021 11:26:49 -0400 Subject: [PATCH 017/127] Add support for setting link section --- gccjit_sys/src/lib.rs | 1 + src/function.rs | 4 ++++ src/lvalue.rs | 9 ++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index bcd03a2..5fadd10 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -541,4 +541,5 @@ extern { pub fn gcc_jit_block_add_extended_asm(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char) -> *mut gcc_jit_extended_asm; pub fn gcc_jit_lvalue_set_tls_model(lvalue: *mut gcc_jit_lvalue, model: gcc_jit_tls_model); + pub fn gcc_jit_lvalue_set_link_section(lvalue: *mut gcc_jit_lvalue, name: *const c_char); } diff --git a/src/function.rs b/src/function.rs index 95da090..f376b02 100644 --- a/src/function.rs +++ b/src/function.rs @@ -112,6 +112,10 @@ impl<'ctx> Function<'ctx> { let cstr = CString::new(name.as_ref()).unwrap(); let ptr = gccjit_sys::gcc_jit_function_new_block(self.ptr, cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } block::from_ptr(ptr) } } diff --git a/src/lvalue.rs b/src/lvalue.rs index 61f3c81..1b90711 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use std::{ffi::CString, marker::PhantomData}; use std::fmt; use std::ptr; use gccjit_sys; @@ -125,6 +125,13 @@ impl<'ctx> LValue<'ctx> { gccjit_sys::gcc_jit_lvalue_set_tls_model(self.ptr, model.to_sys()); } } + + pub fn set_link_section(&self, name: &str) { + let name = CString::new(name).unwrap(); + unsafe { + gccjit_sys::gcc_jit_lvalue_set_link_section(self.ptr, name.as_ptr()); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From 77f3fa9d66a32fd7995e67bb427d9231dc52fd98 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 16 May 2021 12:36:35 -0400 Subject: [PATCH 018/127] Add function to set the initial value of a global variable --- gccjit_sys/src/lib.rs | 1 + src/lvalue.rs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 5fadd10..589a65e 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -528,6 +528,7 @@ extern { pub fn gcc_jit_struct_get_field_count(struct_type: *mut gcc_jit_struct) -> ssize_t; pub fn gcc_jit_global_set_initializer(global: *mut gcc_jit_lvalue, blob: *const c_void, num_bytes: size_t) -> *mut gcc_jit_lvalue; + pub fn gcc_jit_global_set_initializer_value(global: *mut gcc_jit_lvalue, value: *mut gcc_jit_rvalue); pub fn gcc_jit_block_end_with_extended_asm_goto(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char, num_goto_blocks: c_int, goto_blocks: *mut *mut gcc_jit_block, fallthrough_block: *mut gcc_jit_block) -> *mut gcc_jit_extended_asm; diff --git a/src/lvalue.rs b/src/lvalue.rs index 1b90711..7fd932f 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -120,6 +120,13 @@ impl<'ctx> LValue<'ctx> { } } + /// Set the initialization value for a global variable. + pub fn global_set_initializer_value(&self, value: RValue<'ctx>) { + unsafe { + gccjit_sys::gcc_jit_global_set_initializer_value(self.ptr, rvalue::get_ptr(&value)); + } + } + pub fn set_tls_model(&self, model: TlsModel) { unsafe { gccjit_sys::gcc_jit_lvalue_set_tls_model(self.ptr, model.to_sys()); From 72c89b168a13f3bd44a4631850a703f0f6ddc5f1 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 23 May 2021 12:07:09 -0400 Subject: [PATCH 019/127] Print error for get_array_type --- src/context.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/context.rs b/src/context.rs index 17502f2..8ee8e8f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -374,6 +374,10 @@ impl<'ctx> Context<'ctx> { loc_ptr, types::get_ptr(&ty), num_elements); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } From 8dba12288a71cb546803d59ec61adebaf438aae0 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 3 Jun 2021 18:29:13 -0400 Subject: [PATCH 020/127] Rename to match the new reflection API --- gccjit_sys/src/lib.rs | 6 +++--- src/types.rs | 13 +++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 589a65e..0237719 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -511,15 +511,15 @@ extern { pub fn gcc_jit_function_get_return_type(func: *mut gcc_jit_function) -> *mut gcc_jit_type; pub fn gcc_jit_function_get_param_count(func: *mut gcc_jit_function) -> ssize_t; - pub fn gcc_jit_type_is_array(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_dyncast_array(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_integral(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_pointer(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; - pub fn gcc_jit_type_is_function_ptr_type(typ: *mut gcc_jit_type) -> *mut gcc_jit_function_type; + pub fn gcc_jit_type_dyncast_function_ptr_type(typ: *mut gcc_jit_type) -> *mut gcc_jit_function_type; pub fn gcc_jit_function_type_get_return_type(function_type: *mut gcc_jit_function_type) -> *mut gcc_jit_type; pub fn gcc_jit_function_type_get_param_count(function_type: *mut gcc_jit_function_type) -> ssize_t; - pub fn gcc_jit_type_is_vector(typ: *mut gcc_jit_type) -> *mut gcc_jit_vector_type; + pub fn gcc_jit_type_dyncast_vector(typ: *mut gcc_jit_type) -> *mut gcc_jit_vector_type; pub fn gcc_jit_function_type_get_param_type(function_type: *mut gcc_jit_function_type, index: c_int) -> *mut gcc_jit_type; pub fn gcc_jit_vector_type_get_num_units(vector_type: *mut gcc_jit_vector_type) -> ssize_t; pub fn gcc_jit_vector_type_get_element_type(vector_type: *mut gcc_jit_vector_type) -> *mut gcc_jit_type; diff --git a/src/types.rs b/src/types.rs index 3381ca7..fce5b68 100644 --- a/src/types.rs +++ b/src/types.rs @@ -74,6 +74,7 @@ impl<'ctx> FunctionPtrType<'ctx> { } pub fn get_param_type(&self, index: usize) -> Type<'ctx> { + // TODO: return Option? unsafe { from_ptr(gccjit_sys::gcc_jit_function_type_get_param_type(self.ptr, index as _)) } @@ -125,9 +126,9 @@ impl<'ctx> Type<'ctx> { } } - pub fn is_array(self) -> bool { + pub fn dyncast_array(self) -> bool { unsafe { - gccjit_sys::gcc_jit_type_is_array(self.ptr) != 0 + gccjit_sys::gcc_jit_type_dyncast_array(self.ptr) != 0 } } @@ -143,9 +144,9 @@ impl<'ctx> Type<'ctx> { } } - pub fn is_vector(self) -> Option> { + pub fn dyncast_vector(self) -> Option> { unsafe { - let vector_type = gccjit_sys::gcc_jit_type_is_vector(self.ptr); + let vector_type = gccjit_sys::gcc_jit_type_dyncast_vector(self.ptr); if vector_type.is_null() { return None; } @@ -163,9 +164,9 @@ impl<'ctx> Type<'ctx> { } } - pub fn is_function_ptr_type(self) -> Option> { + pub fn dyncast_function_ptr_type(self) -> Option> { unsafe { - let function_ptr_type = gccjit_sys::gcc_jit_type_is_function_ptr_type(self.ptr); + let function_ptr_type = gccjit_sys::gcc_jit_type_dyncast_function_ptr_type(self.ptr); if function_ptr_type.is_null() { return None; } From 0fa0e9864114072562d5d2a8d7471ad24cb135ad Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 9 Jun 2021 18:29:57 -0400 Subject: [PATCH 021/127] Add bitcast and try_catch --- gccjit_sys/src/lib.rs | 5 +++++ src/block.rs | 14 ++++++++++++++ src/context.rs | 23 +++++++++++++++++++++++ src/function.rs | 6 ++++++ 4 files changed, 48 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 589a65e..5492cda 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -543,4 +543,9 @@ extern { pub fn gcc_jit_lvalue_set_tls_model(lvalue: *mut gcc_jit_lvalue, model: gcc_jit_tls_model); pub fn gcc_jit_lvalue_set_link_section(lvalue: *mut gcc_jit_lvalue, name: *const c_char); + + pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); + pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block); + + pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; } diff --git a/src/block.rs b/src/block.rs index 53c96fa..4e2f6c8 100644 --- a/src/block.rs +++ b/src/block.rs @@ -104,6 +104,20 @@ impl<'ctx> Block<'ctx> { loc_ptr, rvalue::get_ptr(&rvalue)); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } + } + + pub fn add_try_finally(&self, loc: Option>, try_block: Block<'ctx>, finally_block: Block<'ctx>) { + let loc_ptr = match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut() + }; + unsafe { + gccjit_sys::gcc_jit_block_add_try_finally(self.ptr, loc_ptr, try_block.ptr, finally_block.ptr); + } } /// Assigns the value of an rvalue to an lvalue directly. Equivalent diff --git a/src/context.rs b/src/context.rs index 8ee8e8f..356ccbf 100644 --- a/src/context.rs +++ b/src/context.rs @@ -677,6 +677,29 @@ impl<'ctx> Context<'ctx> { } } + /// Bitcast an RValue to a specific type. + pub fn new_bitcast<'a, T: ToRValue<'a>>(&'a self, + loc: Option>, + value: T, + dest_type: types::Type<'a>) -> RValue<'a> { + let rvalue = value.to_rvalue(); + let loc_ptr = match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut() + }; + unsafe { + let ptr = gccjit_sys::gcc_jit_context_new_bitcast(self.ptr, + loc_ptr, + rvalue::get_ptr(&rvalue), + types::get_ptr(&dest_type)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Creates an LValue from an array pointer and an offset. The LValue can be the target /// of an assignment, or it can be converted into an RValue (i.e. loaded). pub fn new_array_access<'a, A: ToRValue<'a>, I: ToRValue<'a>>(&'a self, diff --git a/src/function.rs b/src/function.rs index f376b02..9c21324 100644 --- a/src/function.rs +++ b/src/function.rs @@ -120,6 +120,12 @@ impl<'ctx> Function<'ctx> { } } + pub fn set_personality_function(&self, personality_func: Function<'ctx>) { + unsafe { + gccjit_sys::gcc_jit_function_set_personality_function(self.ptr, personality_func.ptr); + } + } + pub fn new_local>(&self, loc: Option>, ty: Type<'ctx>, From fe2d6ffc524b99761d121bb4f2d033d260d6659b Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 9 Jun 2021 19:00:33 -0400 Subject: [PATCH 022/127] Comment unreleased code --- gccjit_sys/src/lib.rs | 4 ++-- src/block.rs | 4 ++-- src/function.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 5492cda..7d35080 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -544,8 +544,8 @@ extern { pub fn gcc_jit_lvalue_set_tls_model(lvalue: *mut gcc_jit_lvalue, model: gcc_jit_tls_model); pub fn gcc_jit_lvalue_set_link_section(lvalue: *mut gcc_jit_lvalue, name: *const c_char); - pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); - pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block); + /*pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); + pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block);*/ pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; } diff --git a/src/block.rs b/src/block.rs index 4e2f6c8..44c7d7f 100644 --- a/src/block.rs +++ b/src/block.rs @@ -110,7 +110,7 @@ impl<'ctx> Block<'ctx> { } } - pub fn add_try_finally(&self, loc: Option>, try_block: Block<'ctx>, finally_block: Block<'ctx>) { + /*pub fn add_try_finally(&self, loc: Option>, try_block: Block<'ctx>, finally_block: Block<'ctx>) { let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, None => ptr::null_mut() @@ -118,7 +118,7 @@ impl<'ctx> Block<'ctx> { unsafe { gccjit_sys::gcc_jit_block_add_try_finally(self.ptr, loc_ptr, try_block.ptr, finally_block.ptr); } - } + }*/ /// Assigns the value of an rvalue to an lvalue directly. Equivalent /// to = in C. diff --git a/src/function.rs b/src/function.rs index 9c21324..4af4567 100644 --- a/src/function.rs +++ b/src/function.rs @@ -120,11 +120,11 @@ impl<'ctx> Function<'ctx> { } } - pub fn set_personality_function(&self, personality_func: Function<'ctx>) { + /*pub fn set_personality_function(&self, personality_func: Function<'ctx>) { unsafe { gccjit_sys::gcc_jit_function_set_personality_function(self.ptr, personality_func.ptr); } - } + }*/ pub fn new_local>(&self, loc: Option>, From dbefd50d1f213b02dcad9fc56d21ff8a1b001026 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 17 Jun 2021 12:24:12 -0400 Subject: [PATCH 023/127] Fix return type of is_array and add dump_to_file function --- gccjit_sys/src/lib.rs | 2 +- src/context.rs | 9 +++++++++ src/types.rs | 8 ++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 7d35080..b123a37 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -511,7 +511,7 @@ extern { pub fn gcc_jit_function_get_return_type(func: *mut gcc_jit_function) -> *mut gcc_jit_type; pub fn gcc_jit_function_get_param_count(func: *mut gcc_jit_function) -> ssize_t; - pub fn gcc_jit_type_is_array(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_is_array(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_integral(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; diff --git a/src/context.rs b/src/context.rs index 356ccbf..027e461 100644 --- a/src/context.rs +++ b/src/context.rs @@ -3,6 +3,7 @@ use std::ops::Drop; use std::ffi::{CStr, CString}; use std::marker::PhantomData; use std::mem; +use std::os::raw::c_int; use std::ptr; use std::str::Utf8Error; @@ -845,6 +846,14 @@ impl<'ctx> Context<'ctx> { } } + pub fn dump_to_file>(&self, path: S, update_locations: bool) { + unsafe { + let path_ref = path.as_ref(); + let cstr = CString::new(path_ref).unwrap(); + gccjit_sys::gcc_jit_context_dump_to_file(self.ptr, cstr.as_ptr(), update_locations as c_int); + } + } + /// Creates a new parameter with a given type, name, and location. pub fn new_parameter<'a, S: AsRef>(&'a self, loc: Option>, diff --git a/src/types.rs b/src/types.rs index 3381ca7..b8c336b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -125,9 +125,13 @@ impl<'ctx> Type<'ctx> { } } - pub fn is_array(self) -> bool { + pub fn is_array(self) -> Option> { unsafe { - gccjit_sys::gcc_jit_type_is_array(self.ptr) != 0 + let array_type = gccjit_sys::gcc_jit_type_is_array(self.ptr); + if array_type.is_null() { + return None; + } + Some(from_ptr(array_type)) } } From d88b6525804fb5ef812775fb3669772c81d49810 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 17 Jun 2021 19:59:16 -0400 Subject: [PATCH 024/127] Add support for setting the inline attribute --- gccjit_sys/src/lib.rs | 11 +++++++++++ src/function.rs | 15 +++++++++++++++ src/lib.rs | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index b123a37..135589b 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -250,6 +250,15 @@ pub enum gcc_jit_comparison GCC_JIT_COMPARISON_GE } +#[repr(C)] +pub enum gcc_jit_inline_mode +{ + GCC_JIT_INLINE_MODE_DEFAULT, + GCC_JIT_INLINE_MODE_ALWAYS_INLINE, + GCC_JIT_INLINE_MODE_NO_INLINE, + GCC_JIT_INLINE_MODE_INLINE, +} + #[link(name = "gccjit")] extern { // context operations @@ -548,4 +557,6 @@ extern { pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block);*/ pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; + + pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); } diff --git a/src/function.rs b/src/function.rs index 4af4567..0cd2493 100644 --- a/src/function.rs +++ b/src/function.rs @@ -44,6 +44,15 @@ pub enum FunctionType { AlwaysInline } +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum InlineMode { + Default, + AlwaysInline, + NoInline, + Inline, +} + /// Function is gccjit's representation of a function. Functions are constructed /// by constructing basic blocks and connecting them together. Locals are declared /// at the function level. @@ -77,6 +86,12 @@ impl<'ctx> Function<'ctx> { } } + pub fn set_inline_mode(&self, inline_mode: InlineMode) { + unsafe { + gccjit_sys::gcc_jit_function_set_inline_mode(self.ptr, std::mem::transmute(inline_mode)); + } + } + pub fn get_param_count(&self) -> usize { unsafe { gccjit_sys::gcc_jit_function_get_param_count(self.ptr) as usize diff --git a/src/lib.rs b/src/lib.rs index 7223b3c..67a15b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,5 +45,5 @@ pub use structs::Struct; pub use lvalue::{LValue, TlsModel, ToLValue}; pub use rvalue::{RValue, ToRValue}; pub use parameter::Parameter; -pub use function::{Function, FunctionType}; +pub use function::{Function, FunctionType, InlineMode}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; From b97f0b5e792bbe63f954832ef2bdcf24b7c19da0 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 24 Jun 2021 18:34:52 -0400 Subject: [PATCH 025/127] Fix warnings --- src/block.rs | 4 ++++ src/context.rs | 3 ++- src/types.rs | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/block.rs b/src/block.rs index 44c7d7f..1c0bdde 100644 --- a/src/block.rs +++ b/src/block.rs @@ -238,6 +238,10 @@ impl<'ctx> Block<'ctx> { gccjit_sys::gcc_jit_block_end_with_return(self.ptr, loc_ptr, rvalue::get_ptr(&ret_rvalue)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } } diff --git a/src/context.rs b/src/context.rs index 027e461..af2347e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -915,7 +915,8 @@ impl<'ctx> Context<'ctx> { } } - pub fn set_logfile>(&self, logfile: S) { + // TODO: use the logfile argument. + pub fn set_logfile>(&self, _logfile: S) { use std::os::raw::c_void; extern { diff --git a/src/types.rs b/src/types.rs index b8c336b..6ab7ba4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -199,7 +199,7 @@ impl<'ctx> Type<'ctx> { /// but it's also possible to implement this trait for more complex types /// that will use the API on Context to construct analagous struct/union types. pub trait Typeable { - fn get_type<'a, 'ctx>(&'a Context<'ctx>) -> Type<'a>; + fn get_type<'a, 'ctx>(ctx: &'a Context<'ctx>) -> Type<'a>; } macro_rules! typeable_def { From c736ae6cb91abdc58168d2847761f89105c2b6a2 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 24 Jun 2021 18:43:54 -0400 Subject: [PATCH 026/127] Comment unreleased code --- gccjit_sys/src/lib.rs | 2 +- src/function.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 135589b..91050e6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -558,5 +558,5 @@ extern { pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); + //pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); } diff --git a/src/function.rs b/src/function.rs index 0cd2493..56bd21d 100644 --- a/src/function.rs +++ b/src/function.rs @@ -86,11 +86,11 @@ impl<'ctx> Function<'ctx> { } } - pub fn set_inline_mode(&self, inline_mode: InlineMode) { + /*pub fn set_inline_mode(&self, inline_mode: InlineMode) { unsafe { gccjit_sys::gcc_jit_function_set_inline_mode(self.ptr, std::mem::transmute(inline_mode)); } - } + }*/ pub fn get_param_count(&self) -> usize { unsafe { From 0572117c7ffdfcb0e6c6526d45266c3f34796bea Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 22 Jul 2021 20:19:55 -0400 Subject: [PATCH 027/127] Add missing cast --- Cargo.toml | 1 - src/context.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e75f8b4..a88ffa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,3 @@ readme = "README.md" [dependencies] gccjit_sys = { version = "0.0.1", path = "gccjit_sys" } - diff --git a/src/context.rs b/src/context.rs index af2347e..46d4f88 100644 --- a/src/context.rs +++ b/src/context.rs @@ -385,7 +385,7 @@ impl<'ctx> Context<'ctx> { pub fn new_vector_type<'a>(&'a self, ty: types::Type<'a>, num_units: u64) -> types::Type<'a> { unsafe { - let ptr = gccjit_sys::gcc_jit_type_get_vector(types::get_ptr(&ty), num_units); + let ptr = gccjit_sys::gcc_jit_type_get_vector(types::get_ptr(&ty), num_units as _); types::from_ptr(ptr) } } @@ -729,7 +729,7 @@ impl<'ctx> Context<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_long(self.ptr, types::get_ptr(&ty), - value); + value as _); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); From 54be27e41fff7b6ab532e2e21a82df50a12b9ad3 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 29 Aug 2021 10:57:31 -0400 Subject: [PATCH 028/127] Add support for register variables --- gccjit_sys/src/lib.rs | 2 ++ src/lvalue.rs | 7 +++++++ src/types.rs | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 91050e6..1370ea2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -559,4 +559,6 @@ extern { pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; //pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); + + pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); } diff --git a/src/lvalue.rs b/src/lvalue.rs index 7fd932f..5f47f59 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -139,6 +139,13 @@ impl<'ctx> LValue<'ctx> { gccjit_sys::gcc_jit_lvalue_set_link_section(self.ptr, name.as_ptr()); } } + + pub fn set_register_name(&self, reg_name: &str) { + let name = CString::new(reg_name).unwrap(); + unsafe { + gccjit_sys::gcc_jit_lvalue_set_register_name(self.ptr, name.as_ptr()); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { diff --git a/src/types.rs b/src/types.rs index 6ab7ba4..87cb472 100644 --- a/src/types.rs +++ b/src/types.rs @@ -209,6 +209,10 @@ macro_rules! typeable_def { unsafe { let ctx_ptr = context::get_ptr(ctx); let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, $expr); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = ctx.get_last_error() { + panic!("{}", error); + } from_ptr(ptr) } } @@ -258,6 +262,10 @@ impl Typeable for *mut T { unsafe { let ctx_ptr = context::get_ptr(ctx); let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, GCC_JIT_TYPE_VOID_PTR); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = ctx.get_last_error() { + panic!("{}", error); + } from_ptr(ptr) } } @@ -268,6 +276,10 @@ impl Typeable for *const T { unsafe { let ctx_ptr = context::get_ptr(ctx); let ptr = gccjit_sys::gcc_jit_context_get_type(ctx_ptr, GCC_JIT_TYPE_VOID_PTR); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = ctx.get_last_error() { + panic!("{}", error); + } from_ptr(ptr).make_const() } } From 2d4fea7319f80531b2e5d264fca9f1c498a3a62e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 25 Sep 2021 17:21:03 -0400 Subject: [PATCH 029/127] Add new_rvalue_from_struct and new_rvalue_from_array --- gccjit_sys/src/lib.rs | 3 +++ src/context.rs | 32 ++++++++++++++++++++++++++++++++ src/function.rs | 2 +- src/structs.rs | 4 ++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 1370ea2..23e1f64 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -561,4 +561,7 @@ extern { //pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); + + pub fn gcc_jit_context_new_rvalue_from_struct(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, struct_type: *mut gcc_jit_struct, num_fields: size_t, fields: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_rvalue_from_array(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; } diff --git a/src/context.rs b/src/context.rs index 46d4f88..90a8f51 100644 --- a/src/context.rs +++ b/src/context.rs @@ -749,6 +749,36 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_rvalue_from_struct<'a>(&'a self, loc: Option>, struct_type: Struct<'a>, fields: &[RValue<'a>]) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_struct(self.ptr, loc_ptr, structs::get_ptr(&struct_type), fields.len() as _, fields.as_ptr() as *mut *mut _); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + + pub fn new_rvalue_from_array<'a>(&'a self, loc: Option>, array_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_array(self.ptr, loc_ptr, types::get_ptr(&array_type), elements.len() as _, elements.as_ptr() as *mut *mut _); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Creates a new RValue from a given int value. pub fn new_rvalue_from_int<'a>(&'a self, ty: types::Type<'a>, @@ -1065,6 +1095,7 @@ pub enum CType { UInt32t, UInt64t, UInt128t, + ConstCharPtr, } impl CType { @@ -1096,6 +1127,7 @@ impl CType { UInt32t => GCC_JIT_TYPE_UINT32_T, UInt64t => GCC_JIT_TYPE_UINT64_T, UInt128t => GCC_JIT_TYPE_UINT128_T, + ConstCharPtr => GCC_JIT_TYPE_CONST_CHAR_PTR, } } } diff --git a/src/function.rs b/src/function.rs index 56bd21d..77ce85a 100644 --- a/src/function.rs +++ b/src/function.rs @@ -129,7 +129,7 @@ impl<'ctx> Function<'ctx> { cstr.as_ptr()); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { - panic!("{}", error); + panic!("{} ({:?})", error, self); } block::from_ptr(ptr) } diff --git a/src/structs.rs b/src/structs.rs index 5b45a71..85ba375 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -95,3 +95,7 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_struct) -> Struct<'ct ptr: ptr } } + +pub unsafe fn get_ptr<'ctx>(ty: &Struct<'ctx>) -> *mut gccjit_sys::gcc_jit_struct { + ty.ptr +} From ab7e276aa7b8c861d68cfc015795c763e0cd8508 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 19 Oct 2021 16:56:02 -0400 Subject: [PATCH 030/127] Add get_size() --- gccjit_sys/src/lib.rs | 2 ++ src/types.rs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 23e1f64..334fb8a 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -564,4 +564,6 @@ extern { pub fn gcc_jit_context_new_rvalue_from_struct(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, struct_type: *mut gcc_jit_struct, num_fields: size_t, fields: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_rvalue_from_array(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + + pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; } diff --git a/src/types.rs b/src/types.rs index 87cb472..2494508 100644 --- a/src/types.rs +++ b/src/types.rs @@ -177,6 +177,14 @@ impl<'ctx> Type<'ctx> { } } + pub fn get_size(&self) -> u32 { + unsafe { + let size = gccjit_sys::gcc_jit_type_get_size(self.ptr); + assert_ne!(size, -1, "called get_size of unsupported type"); + size as u32 + } + } + pub fn unqualified(&self) -> Type<'ctx> { unsafe { from_ptr(gccjit_sys::gcc_jit_type_unqualified(self.ptr)) From a09d6e28177ada7b2a811cd7a72803deb814fcae Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 Oct 2021 16:45:07 -0400 Subject: [PATCH 031/127] Add Type::is_compatible_with --- gccjit_sys/src/lib.rs | 1 + src/block.rs | 1 + src/context.rs | 8 ++++++++ src/types.rs | 7 +++++++ 4 files changed, 17 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 334fb8a..7cd5589 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -566,4 +566,5 @@ extern { pub fn gcc_jit_context_new_rvalue_from_array(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; + pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; } diff --git a/src/block.rs b/src/block.rs index 1c0bdde..3a3005b 100644 --- a/src/block.rs +++ b/src/block.rs @@ -36,6 +36,7 @@ pub enum BinaryOp { /// UnaryOp is an enum representing the various unary operations /// that gccjit knows how to codegen. #[repr(C)] +#[derive(Clone, Copy)] pub enum UnaryOp { Minus, BitwiseNegate, diff --git a/src/context.rs b/src/context.rs index 90a8f51..91ea24a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -566,6 +566,10 @@ impl<'ctx> Context<'ctx> { mem::transmute(op), types::get_ptr(&ty), rvalue::get_ptr(&rvalue)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -718,6 +722,10 @@ impl<'ctx> Context<'ctx> { loc_ptr, rvalue::get_ptr(&array_rvalue), rvalue::get_ptr(&idx_rvalue)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } lvalue::from_ptr(ptr) } } diff --git a/src/types.rs b/src/types.rs index 2494508..f34af42 100644 --- a/src/types.rs +++ b/src/types.rs @@ -200,6 +200,13 @@ impl<'ctx> Type<'ctx> { Some(from_ptr(value)) } } + + + pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { + unsafe { + gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) + } + } } /// Typeable is a trait for types that have a corresponding type within From 27ec047b13feb4e39ce32b5eae9991fee844a0be Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 2 Dec 2021 16:55:41 -0500 Subject: [PATCH 032/127] Add panic on error --- src/block.rs | 1 + src/context.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/block.rs b/src/block.rs index 3a3005b..e606329 100644 --- a/src/block.rs +++ b/src/block.rs @@ -47,6 +47,7 @@ pub enum UnaryOp { /// ComparisonOp is an enum representing the various comparisons that /// gccjit is capable of doing. #[repr(C)] +#[derive(Debug)] pub enum ComparisonOp { Equals, NotEquals, diff --git a/src/context.rs b/src/context.rs index 91ea24a..5497240 100644 --- a/src/context.rs +++ b/src/context.rs @@ -796,6 +796,10 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_int(self.ptr, types::get_ptr(&ty), value); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } From c2ec6fb2174e31ab96a0f5e7ed97e2a8889366c6 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 12 Dec 2021 15:36:02 -0500 Subject: [PATCH 033/127] Update for latest TLS patch --- gccjit_sys/src/lib.rs | 6 +++--- src/lvalue.rs | 4 ++-- src/types.rs | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 7cd5589..1e6ea44 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -24,11 +24,11 @@ pub enum gcc_jit_extended_asm {} #[repr(C)] pub enum gcc_jit_tls_model { + GCC_JIT_TLS_MODEL_NONE, GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC, GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC, GCC_JIT_TLS_MODEL_INITIAL_EXEC, GCC_JIT_TLS_MODEL_LOCAL_EXEC, - GCC_JIT_TLS_MODEL_DEFAULT, } #[repr(C)] @@ -565,6 +565,6 @@ extern { pub fn gcc_jit_context_new_rvalue_from_struct(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, struct_type: *mut gcc_jit_struct, num_fields: size_t, fields: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_rvalue_from_array(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; - pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; + //pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; + //pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; } diff --git a/src/lvalue.rs b/src/lvalue.rs index 5f47f59..50a5ac8 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -18,7 +18,7 @@ pub enum TlsModel { LocalDynamic, InitialExec, LocalExec, - Default, + None, } impl TlsModel { @@ -30,7 +30,7 @@ impl TlsModel { TlsModel::LocalDynamic => GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC, TlsModel::InitialExec => GCC_JIT_TLS_MODEL_INITIAL_EXEC, TlsModel::LocalExec => GCC_JIT_TLS_MODEL_LOCAL_EXEC, - TlsModel::Default => GCC_JIT_TLS_MODEL_DEFAULT, + TlsModel::None => GCC_JIT_TLS_MODEL_NONE, } } } diff --git a/src/types.rs b/src/types.rs index f34af42..49926ea 100644 --- a/src/types.rs +++ b/src/types.rs @@ -177,13 +177,13 @@ impl<'ctx> Type<'ctx> { } } - pub fn get_size(&self) -> u32 { + /*pub fn get_size(&self) -> u32 { unsafe { let size = gccjit_sys::gcc_jit_type_get_size(self.ptr); assert_ne!(size, -1, "called get_size of unsupported type"); size as u32 } - } + }*/ pub fn unqualified(&self) -> Type<'ctx> { unsafe { @@ -202,11 +202,11 @@ impl<'ctx> Type<'ctx> { } - pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { + /*pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { unsafe { gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) } - } + }*/ } /// Typeable is a trait for types that have a corresponding type within From 6e4ea3474d5f14ac8245343c3369242b1707f281 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 2 Dec 2021 17:49:58 -0500 Subject: [PATCH 034/127] Add new initialization functions --- gccjit_sys/src/lib.rs | 10 +++++----- src/context.rs | 16 ++++++++++++---- src/lvalue.rs | 4 ++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 596f9aa..1d4cf61 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -537,8 +537,6 @@ extern { pub fn gcc_jit_struct_get_field_count(struct_type: *mut gcc_jit_struct) -> ssize_t; pub fn gcc_jit_global_set_initializer(global: *mut gcc_jit_lvalue, blob: *const c_void, num_bytes: size_t) -> *mut gcc_jit_lvalue; - pub fn gcc_jit_global_set_initializer_value(global: *mut gcc_jit_lvalue, value: *mut gcc_jit_rvalue); - pub fn gcc_jit_block_end_with_extended_asm_goto(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, asm_template: *const c_char, num_goto_blocks: c_int, goto_blocks: *mut *mut gcc_jit_block, fallthrough_block: *mut gcc_jit_block) -> *mut gcc_jit_extended_asm; pub fn gcc_jit_extended_asm_as_object(ext_asm: *mut gcc_jit_extended_asm) -> *mut gcc_jit_object; @@ -562,9 +560,11 @@ extern { pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); - pub fn gcc_jit_context_new_rvalue_from_struct(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, struct_type: *mut gcc_jit_struct, num_fields: size_t, fields: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_context_new_rvalue_from_array(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_elements: size_t, elements: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - //pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; //pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; + + pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_array_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_global_set_initializer_rvalue(global: *mut gcc_jit_lvalue, init_value: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; } diff --git a/src/context.rs b/src/context.rs index 5497240..71f0a74 100644 --- a/src/context.rs +++ b/src/context.rs @@ -757,13 +757,21 @@ impl<'ctx> Context<'ctx> { } } - pub fn new_rvalue_from_struct<'a>(&'a self, loc: Option>, struct_type: Struct<'a>, fields: &[RValue<'a>]) -> RValue<'a> { + //pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + + pub fn new_struct_constructor<'a>(&'a self, loc: Option>, struct_type: types::Type<'a>, fields: Option<&[Field<'a>]>, values: &[RValue<'a>]) -> RValue<'a> { unsafe { let loc_ptr = match loc { Some(loc) => location::get_ptr(&loc), None => ptr::null_mut() }; - let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_struct(self.ptr, loc_ptr, structs::get_ptr(&struct_type), fields.len() as _, fields.as_ptr() as *mut *mut _); + let fields_ptr = + match fields { + Some(fields) => fields.as_ptr(), + None => ptr::null_mut(), + }; + + let ptr = gccjit_sys::gcc_jit_context_new_struct_constructor(self.ptr, loc_ptr, types::get_ptr(&struct_type), values.len() as _, fields_ptr as *mut *mut _, values.as_ptr() as *mut *mut _); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); @@ -772,13 +780,13 @@ impl<'ctx> Context<'ctx> { } } - pub fn new_rvalue_from_array<'a>(&'a self, loc: Option>, array_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { + pub fn new_array_constructor<'a>(&'a self, loc: Option>, array_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { unsafe { let loc_ptr = match loc { Some(loc) => location::get_ptr(&loc), None => ptr::null_mut() }; - let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_array(self.ptr, loc_ptr, types::get_ptr(&array_type), elements.len() as _, elements.as_ptr() as *mut *mut _); + let ptr = gccjit_sys::gcc_jit_context_new_array_constructor(self.ptr, loc_ptr, types::get_ptr(&array_type), elements.len() as _, elements.as_ptr() as *mut *mut _); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); diff --git a/src/lvalue.rs b/src/lvalue.rs index 50a5ac8..032c69a 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -121,9 +121,9 @@ impl<'ctx> LValue<'ctx> { } /// Set the initialization value for a global variable. - pub fn global_set_initializer_value(&self, value: RValue<'ctx>) { + pub fn global_set_initializer_rvalue(&self, value: RValue<'ctx>) -> LValue<'ctx> { unsafe { - gccjit_sys::gcc_jit_global_set_initializer_value(self.ptr, rvalue::get_ptr(&value)); + from_ptr(gccjit_sys::gcc_jit_global_set_initializer_rvalue(self.ptr, rvalue::get_ptr(&value))) } } From 7e13497121915316b864f945170009f0a5a34ee1 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 19 Dec 2021 17:43:23 -0500 Subject: [PATCH 035/127] Uncomment code --- gccjit_sys/src/lib.rs | 6 +++--- src/types.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 1d4cf61..ea72640 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -560,11 +560,11 @@ extern { pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); - //pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; - //pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; - pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_array_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_global_set_initializer_rvalue(global: *mut gcc_jit_lvalue, init_value: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; + + pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; + pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; } diff --git a/src/types.rs b/src/types.rs index 3990e22..663abad 100644 --- a/src/types.rs +++ b/src/types.rs @@ -178,13 +178,13 @@ impl<'ctx> Type<'ctx> { } } - /*pub fn get_size(&self) -> u32 { + pub fn get_size(&self) -> u32 { unsafe { let size = gccjit_sys::gcc_jit_type_get_size(self.ptr); assert_ne!(size, -1, "called get_size of unsupported type"); size as u32 } - }*/ + } pub fn unqualified(&self) -> Type<'ctx> { unsafe { @@ -203,11 +203,11 @@ impl<'ctx> Type<'ctx> { } - /*pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { + pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { unsafe { gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) } - }*/ + } } /// Typeable is a trait for types that have a corresponding type within From d3a9f29ec5545bf5c21e5563057515816e26dcb9 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 9 Jan 2022 13:46:06 -0500 Subject: [PATCH 036/127] Add new set_hide_log_stderr function --- gccjit_sys/src/lib.rs | 1 + src/context.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index ea72640..ee1464a 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -53,6 +53,7 @@ pub enum gcc_jit_bool_option { GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, GCC_JIT_BOOL_OPTION_SELFCHECK_GC, GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, + GCC_JIT_BOOL_OPTION_HIDE_LOG_STDERR, GCC_JIT_NUM_BOOL_OPTIONS } diff --git a/src/context.rs b/src/context.rs index 71f0a74..1094575 100644 --- a/src/context.rs +++ b/src/context.rs @@ -207,6 +207,14 @@ impl<'ctx> Context<'ctx> { } } + pub fn set_hide_log_stderr(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, + GCC_JIT_BOOL_OPTION_HIDE_LOG_STDERR, + value as i32); + } + } + pub fn set_dump_everything(&self, value: bool) { unsafe { gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, @@ -335,6 +343,10 @@ impl<'ctx> Context<'ctx> { unsafe { let ctx_ptr = get_ptr(self); let ptr = gccjit_sys::gcc_jit_context_get_int_type(ctx_ptr, num_bytes, signed as i32); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } @@ -920,6 +932,10 @@ impl<'ctx> Context<'ctx> { loc_ptr, types::get_ptr(&ty), cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } parameter::from_ptr(ptr) } } From 69b0f198ab18ed954d03ac4971ad3b1d2f21e784 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 10 Jan 2022 22:33:17 -0500 Subject: [PATCH 037/127] Add Debug implementation for Context and ContextRef --- src/context.rs | 1 + src/object.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/context.rs b/src/context.rs index 1094575..8ea4b25 100644 --- a/src/context.rs +++ b/src/context.rs @@ -138,6 +138,7 @@ impl<'ctx> ToObject<'ctx> for Case<'ctx> { /// It's possible to create a child context from a parent context. /// In that case, the child context must have a lifetime strictly /// less than the parent context. +#[derive(Debug)] pub struct Context<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_context diff --git a/src/object.rs b/src/object.rs index c20b3f4..ee71905 100644 --- a/src/object.rs +++ b/src/object.rs @@ -30,6 +30,7 @@ impl<'ctx> fmt::Debug for Object<'ctx> { use std::mem::ManuallyDrop; use std::ops::Deref; +#[derive(Debug)] pub struct ContextRef<'ctx> { context: ManuallyDrop>, } From de1ae5c4aecff0f16a7e72b12bea6f17bde6e113 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 23 Jan 2022 11:31:27 -0500 Subject: [PATCH 038/127] Numerous fixes --- gccjit_sys/src/lib.rs | 23 ++++++++++++----------- src/block.rs | 12 ++++++++---- src/context.rs | 2 +- src/field.rs | 2 +- src/function.rs | 2 +- src/location.rs | 2 +- src/lvalue.rs | 2 +- src/object.rs | 2 +- src/parameter.rs | 2 +- src/rvalue.rs | 2 +- src/structs.rs | 6 +----- src/types.rs | 15 +++++++-------- 12 files changed, 36 insertions(+), 36 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index ee1464a..77f3c18 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -91,16 +91,6 @@ pub enum gcc_jit_types { /* C99's "long long" and "unsigned long long". */ GCC_JIT_TYPE_LONG_LONG, /* signed */ GCC_JIT_TYPE_UNSIGNED_LONG_LONG, - GCC_JIT_TYPE_UINT8_T, - GCC_JIT_TYPE_UINT16_T, - GCC_JIT_TYPE_UINT32_T, - GCC_JIT_TYPE_UINT64_T, - GCC_JIT_TYPE_UINT128_T, - GCC_JIT_TYPE_INT8_T, - GCC_JIT_TYPE_INT16_T, - GCC_JIT_TYPE_INT32_T, - GCC_JIT_TYPE_INT64_T, - GCC_JIT_TYPE_INT128_T, /* Floating-point types */ GCC_JIT_TYPE_FLOAT, @@ -115,7 +105,18 @@ pub enum gcc_jit_types { /* Complex numbers. */ GCC_JIT_TYPE_COMPLEX_FLOAT, GCC_JIT_TYPE_COMPLEX_DOUBLE, - GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE + GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE, + + GCC_JIT_TYPE_UINT8_T, + GCC_JIT_TYPE_UINT16_T, + GCC_JIT_TYPE_UINT32_T, + GCC_JIT_TYPE_UINT64_T, + GCC_JIT_TYPE_UINT128_T, + GCC_JIT_TYPE_INT8_T, + GCC_JIT_TYPE_INT16_T, + GCC_JIT_TYPE_INT32_T, + GCC_JIT_TYPE_INT64_T, + GCC_JIT_TYPE_INT128_T, } #[repr(C)] diff --git a/src/block.rs b/src/block.rs index e606329..092ed1b 100644 --- a/src/block.rs +++ b/src/block.rs @@ -77,7 +77,7 @@ impl<'ctx> ToObject<'ctx> for Block<'ctx> { } impl<'ctx> fmt::Debug for Block<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } @@ -207,6 +207,10 @@ impl<'ctx> Block<'ctx> { on_true.ptr, on_false.ptr); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } /// Terminates a block by unconditionally jumping to another block. @@ -262,7 +266,7 @@ impl<'ctx> Block<'ctx> { } } - pub fn end_with_switch>(&self, loc: Option>, expr: T, default_block: Block<'ctx>, cases: &[Case]) { + pub fn end_with_switch>(&self, loc: Option>, expr: T, default_block: Block<'ctx>, cases: &[Case<'ctx>]) { let expr = expr.to_rvalue(); let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, @@ -274,7 +278,7 @@ impl<'ctx> Block<'ctx> { } } - pub fn add_extended_asm(&self, loc: Option>, asm_template: &str) -> ExtendedAsm { + pub fn add_extended_asm(&self, loc: Option>, asm_template: &str) -> ExtendedAsm<'ctx> { let asm_template = CString::new(asm_template).unwrap(); let loc_ptr = match loc { @@ -286,7 +290,7 @@ impl<'ctx> Block<'ctx> { } } - pub fn end_with_extended_asm_goto(&self, loc: Option>, asm_template: &str, goto_blocks: &[Block<'ctx>], fallthrough_block: Option>) -> ExtendedAsm { + pub fn end_with_extended_asm_goto(&self, loc: Option>, asm_template: &str, goto_blocks: &[Block<'ctx>], fallthrough_block: Option>) -> ExtendedAsm<'ctx> { let asm_template = CString::new(asm_template).unwrap(); let loc_ptr = match loc { diff --git a/src/context.rs b/src/context.rs index 8ea4b25..43155e9 100644 --- a/src/context.rs +++ b/src/context.rs @@ -277,7 +277,7 @@ impl<'ctx> Context<'ctx> { } } - pub fn new_case, T: ToRValue<'ctx>>(&self, min_value: S, max_value: T, dest_block: Block<'ctx>) -> Case { + pub fn new_case, T: ToRValue<'ctx>>(&self, min_value: S, max_value: T, dest_block: Block<'ctx>) -> Case<'ctx> { let min_value = min_value.to_rvalue(); let max_value = max_value.to_rvalue(); unsafe { diff --git a/src/field.rs b/src/field.rs index 5a4164f..29b72e1 100644 --- a/src/field.rs +++ b/src/field.rs @@ -24,7 +24,7 @@ impl<'ctx> ToObject<'ctx> for Field<'ctx> { } impl<'ctx> fmt::Debug for Field<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/function.rs b/src/function.rs index 77ce85a..3b90584 100644 --- a/src/function.rs +++ b/src/function.rs @@ -72,7 +72,7 @@ impl<'ctx> ToObject<'ctx> for Function<'ctx> { } impl<'ctx> fmt::Debug for Function<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/location.rs b/src/location.rs index f35669a..1cabc18 100644 --- a/src/location.rs +++ b/src/location.rs @@ -21,7 +21,7 @@ impl<'ctx> ToObject<'ctx> for Location<'ctx> { } impl<'ctx> fmt::Debug for Location<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/lvalue.rs b/src/lvalue.rs index 032c69a..e9100d4 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -60,7 +60,7 @@ impl<'ctx> ToObject<'ctx> for LValue<'ctx> { } impl<'ctx> fmt::Debug for LValue<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/object.rs b/src/object.rs index ee71905..b922681 100644 --- a/src/object.rs +++ b/src/object.rs @@ -17,7 +17,7 @@ pub struct Object<'ctx> { } impl<'ctx> fmt::Debug for Object<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { unsafe { let ptr = gccjit_sys::gcc_jit_object_get_debug_string(self.ptr); let cstr = CStr::from_ptr(ptr); diff --git a/src/parameter.rs b/src/parameter.rs index bfaab25..0c26074 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -26,7 +26,7 @@ impl<'ctx> ToObject<'ctx> for Parameter<'ctx> { } impl<'ctx> fmt::Debug for Parameter<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/rvalue.rs b/src/rvalue.rs index 218addc..54d4c8e 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -41,7 +41,7 @@ impl<'ctx> ToObject<'ctx> for RValue<'ctx> { } impl<'ctx> fmt::Debug for RValue<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } diff --git a/src/structs.rs b/src/structs.rs index 85ba375..da1264f 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -83,7 +83,7 @@ impl<'ctx> ToObject<'ctx> for Struct<'ctx> { } impl<'ctx> fmt::Debug for Struct<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.as_type(); obj.fmt(fmt) } @@ -95,7 +95,3 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_struct) -> Struct<'ct ptr: ptr } } - -pub unsafe fn get_ptr<'ctx>(ty: &Struct<'ctx>) -> *mut gccjit_sys::gcc_jit_struct { - ty.ptr -} diff --git a/src/types.rs b/src/types.rs index 663abad..a329b02 100644 --- a/src/types.rs +++ b/src/types.rs @@ -91,7 +91,7 @@ impl<'ctx> ToObject<'ctx> for Type<'ctx> { } impl<'ctx> fmt::Debug for Type<'ctx> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { let obj = self.to_object(); obj.fmt(fmt) } @@ -181,7 +181,7 @@ impl<'ctx> Type<'ctx> { pub fn get_size(&self) -> u32 { unsafe { let size = gccjit_sys::gcc_jit_type_get_size(self.ptr); - assert_ne!(size, -1, "called get_size of unsupported type"); + assert_ne!(size, -1, "called get_size of unsupported type: {:?}", self); size as u32 } } @@ -202,12 +202,11 @@ impl<'ctx> Type<'ctx> { } } - - pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { - unsafe { - gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) - } - } + pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { + unsafe { + gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) + } + } } /// Typeable is a trait for types that have a corresponding type within From 96d2801709ee14b3b46ec5c52bfa9eb2090d731a Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 23 Jan 2022 11:51:47 -0500 Subject: [PATCH 039/127] Add Context::set_print_errors_to_stderr --- gccjit_sys/src/lib.rs | 15 ++++++++------- src/context.rs | 6 ++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 77f3c18..5f084de 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -53,7 +53,6 @@ pub enum gcc_jit_bool_option { GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, GCC_JIT_BOOL_OPTION_SELFCHECK_GC, GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, - GCC_JIT_BOOL_OPTION_HIDE_LOG_STDERR, GCC_JIT_NUM_BOOL_OPTIONS } @@ -562,11 +561,13 @@ extern { pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); - pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_context_new_array_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - pub fn gcc_jit_global_set_initializer_rvalue(global: *mut gcc_jit_lvalue, init_value: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; + pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_array_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_global_set_initializer_rvalue(global: *mut gcc_jit_lvalue, init_value: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; - pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; - pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; + pub fn gcc_jit_type_get_size(typ: *mut gcc_jit_type) -> ssize_t; + pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; + + pub fn gcc_jit_context_set_bool_print_errors_to_stderr(ctxt: *mut gcc_jit_context, enabled: c_int); } diff --git a/src/context.rs b/src/context.rs index 43155e9..3bfc81a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -208,11 +208,9 @@ impl<'ctx> Context<'ctx> { } } - pub fn set_hide_log_stderr(&self, value: bool) { + pub fn set_print_errors_to_stderr(&self, enabled: bool) { unsafe { - gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, - GCC_JIT_BOOL_OPTION_HIDE_LOG_STDERR, - value as i32); + gccjit_sys::gcc_jit_context_set_bool_print_errors_to_stderr(self.ptr, enabled as c_int); } } From e68fce53af18dce4d40e6b7090f881ff86a2e892 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 25 Jan 2022 22:01:48 -0500 Subject: [PATCH 040/127] Add new methods to get and set the alignment --- gccjit_sys/src/lib.rs | 3 +++ src/lvalue.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 5f084de..2ff1bd7 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -570,4 +570,7 @@ extern { pub fn gcc_jit_compatible_types(ltype: *mut gcc_jit_type, rtype: *mut gcc_jit_type) -> bool; pub fn gcc_jit_context_set_bool_print_errors_to_stderr(ctxt: *mut gcc_jit_context, enabled: c_int); + + pub fn gcc_jit_lvalue_set_alignment(lvalue: *mut gcc_jit_lvalue, alignment: c_int); + pub fn gcc_jit_lvalue_get_alignment(lvalue: *mut gcc_jit_lvalue) -> c_int; } diff --git a/src/lvalue.rs b/src/lvalue.rs index e9100d4..eef2084 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -146,6 +146,18 @@ impl<'ctx> LValue<'ctx> { gccjit_sys::gcc_jit_lvalue_set_register_name(self.ptr, name.as_ptr()); } } + + pub fn set_alignment(&self, alignment: i32) { + unsafe { + gccjit_sys::gcc_jit_lvalue_set_alignment(self.ptr, alignment); + } + } + + pub fn get_alignment(&self) -> i32 { + unsafe { + gccjit_sys::gcc_jit_lvalue_get_alignment(self.ptr) + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From 87dd32ba07613c27776fd4643a5e8ac671374806 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 28 Jan 2022 19:38:17 -0500 Subject: [PATCH 041/127] Make BinaryOp Copy --- src/block.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/block.rs b/src/block.rs index 092ed1b..35a24b7 100644 --- a/src/block.rs +++ b/src/block.rs @@ -18,6 +18,7 @@ use lvalue::{self, ToLValue}; /// BinaryOp is a enum representing the various binary operations /// that gccjit knows how to codegen. #[repr(C)] +#[derive(Clone, Copy)] pub enum BinaryOp { Plus, Minus, From 0811e300b75d63dc79c8c2f07913bfd2ae61c97e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 28 Jan 2022 20:11:48 -0500 Subject: [PATCH 042/127] Make BinaryOp Debug --- src/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/block.rs b/src/block.rs index 35a24b7..78a5f49 100644 --- a/src/block.rs +++ b/src/block.rs @@ -18,7 +18,7 @@ use lvalue::{self, ToLValue}; /// BinaryOp is a enum representing the various binary operations /// that gccjit knows how to codegen. #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub enum BinaryOp { Plus, Minus, From cbb07c6601ba4246fc2967c4d770403c57192ca2 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 28 Jan 2022 21:39:16 -0500 Subject: [PATCH 043/127] Add more panic on errors --- src/context.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/src/context.rs b/src/context.rs index 3bfc81a..df280fe 100644 --- a/src/context.rs +++ b/src/context.rs @@ -278,13 +278,18 @@ impl<'ctx> Context<'ctx> { pub fn new_case, T: ToRValue<'ctx>>(&self, min_value: S, max_value: T, dest_block: Block<'ctx>) -> Case<'ctx> { let min_value = min_value.to_rvalue(); let max_value = max_value.to_rvalue(); - unsafe { + let result = unsafe { Case { marker: PhantomData, ptr: gccjit_sys::gcc_jit_context_new_case(self.ptr, rvalue::get_ptr(&min_value), rvalue::get_ptr(&max_value), block::get_ptr(&dest_block)), } + }; + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); } + result } /// Creates a new location for use by gdb when debugging a JIT compiled @@ -301,6 +306,10 @@ impl<'ctx> Context<'ctx> { cstr.as_ptr(), line, col); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } location::from_ptr(ptr) } } @@ -318,6 +327,10 @@ impl<'ctx> Context<'ctx> { mem::transmute(kind), types::get_ptr(&ty), cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } lvalue::from_ptr(ptr) } } @@ -334,6 +347,10 @@ impl<'ctx> Context<'ctx> { pub fn new_c_type<'a>(&'a self, c_type: CType) -> types::Type<'a> { unsafe { let ptr = gccjit_sys::gcc_jit_context_get_type(get_ptr(self), c_type.to_sys()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } @@ -367,6 +384,10 @@ impl<'ctx> Context<'ctx> { loc_ptr, types::get_ptr(&ty), cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } field::from_ptr(ptr) } } @@ -397,6 +418,10 @@ impl<'ctx> Context<'ctx> { pub fn new_vector_type<'a>(&'a self, ty: types::Type<'a>, num_units: u64) -> types::Type<'a> { unsafe { let ptr = gccjit_sys::gcc_jit_type_get_vector(types::get_ptr(&ty), num_units as _); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } @@ -424,6 +449,10 @@ impl<'ctx> Context<'ctx> { cname.as_ptr(), num_fields, fields_ptrs.as_mut_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } structs::from_ptr(ptr) } } @@ -443,6 +472,10 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_opaque_struct(self.ptr, loc_ptr, cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } structs::from_ptr(ptr) } } @@ -468,6 +501,10 @@ impl<'ctx> Context<'ctx> { cname.as_ptr(), num_fields, fields_ptrs.as_mut_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } @@ -496,6 +533,10 @@ impl<'ctx> Context<'ctx> { num_types, types_ptrs.as_mut_ptr(), is_variadic as i32); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } types::from_ptr(ptr) } } @@ -528,6 +569,10 @@ impl<'ctx> Context<'ctx> { num_params, params_ptrs.as_mut_ptr(), is_variadic as i32); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } function::from_ptr(ptr) } } @@ -666,6 +711,10 @@ impl<'ctx> Context<'ctx> { rvalue::get_ptr(&fun_ptr_rvalue), num_params, params_ptrs.as_mut_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -764,6 +813,10 @@ impl<'ctx> Context<'ctx> { None => ptr::null_mut() }; let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_vector(self.ptr, loc_ptr, types::get_ptr(&vec_type), elements.len() as _, elements.as_ptr() as *mut *mut _); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -831,6 +884,10 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_double(self.ptr, types::get_ptr(&ty), value); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -841,6 +898,10 @@ impl<'ctx> Context<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_context_zero(self.ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -851,6 +912,10 @@ impl<'ctx> Context<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_context_one(self.ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -865,6 +930,10 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_ptr(self.ptr, types::get_ptr(&ty), mem::transmute(value)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -890,6 +959,10 @@ impl<'ctx> Context<'ctx> { let cstr = CString::new(value.as_ref()).unwrap(); let ptr = gccjit_sys::gcc_jit_context_new_string_literal(self.ptr, cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } rvalue::from_ptr(ptr) } } @@ -1003,6 +1076,10 @@ impl<'ctx> Context<'ctx> { unsafe { gccjit_sys::gcc_jit_context_add_top_level_asm(self.ptr, loc_ptr, asm_stmts.as_ptr()); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } } } From 3c45b92f431bf29c28a1287be32b7aad4d2c5892 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 26 Feb 2022 11:23:37 +0100 Subject: [PATCH 044/127] Add set_allow_unreachable_blocks method to Context --- gccjit_sys/src/lib.rs | 2 ++ src/context.rs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 2ff1bd7..23e34e9 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -274,6 +274,8 @@ extern { pub fn gcc_jit_context_set_bool_option(ctx: *mut gcc_jit_context, option: gcc_jit_bool_option, value: c_int); + pub fn gcc_jit_context_set_bool_allow_unreachable_blocks(ctx: *mut gcc_jit_context, + value: c_int); pub fn gcc_jit_context_compile(ctx: *mut gcc_jit_context) -> *mut gcc_jit_result; pub fn gcc_jit_context_compile_to_file(ctx: *mut gcc_jit_context, kind: gcc_jit_output_kind, diff --git a/src/context.rs b/src/context.rs index df280fe..1201daf 100644 --- a/src/context.rs +++ b/src/context.rs @@ -240,6 +240,12 @@ impl<'ctx> Context<'ctx> { } } + pub fn set_allow_unreachable_blocks(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_allow_unreachable_blocks(self.ptr, value as i32); + } + } + /// Compiles the context and returns a CompileResult that contains /// the means to access functions and globals that have currently /// been JIT compiled. From bdecdecfb8a02ec861a39a350f990faa33bd31c3 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 27 Feb 2022 17:01:02 -0500 Subject: [PATCH 045/127] Export FunctionPtrType --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 67a15b1..7b7b3d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,7 @@ pub use context::OutputKind; pub use location::Location; pub use object::Object; pub use object::ToObject; +pub use types::FunctionPtrType; pub use types::Type; pub use types::Typeable; pub use field::Field; From 07219a4085c49d29beae648f91590ede3fb209ad Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 27 Feb 2022 10:35:58 -0500 Subject: [PATCH 046/127] Add functions for target builtins --- gccjit_sys/src/lib.rs | 6 ++++++ src/context.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 23e34e9..e007903 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -575,4 +575,10 @@ extern { pub fn gcc_jit_lvalue_set_alignment(lvalue: *mut gcc_jit_lvalue, alignment: c_int); pub fn gcc_jit_lvalue_get_alignment(lvalue: *mut gcc_jit_lvalue) -> c_int; + + + pub fn gcc_jit_context_get_target_builtin_function(ctxt: *mut gcc_jit_context, name: *const c_char) -> *mut gcc_jit_function; + + pub fn gcc_jit_context_new_rvalue_vector_perm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, elements1: *mut gcc_jit_rvalue, elements2: *mut gcc_jit_rvalue, mask: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_vector_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: size_t, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; } diff --git a/src/context.rs b/src/context.rs index 1201daf..9152f7d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -827,6 +827,21 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_rvalue_vector_perm<'a>(&'a self, loc: Option>, elements1: RValue<'a>, elements2: RValue<'a>, mask: RValue<'a>) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_context_new_rvalue_vector_perm(self.ptr, loc_ptr, rvalue::get_ptr(&elements1), rvalue::get_ptr(&elements2), rvalue::get_ptr(&mask)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + //pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn new_struct_constructor<'a>(&'a self, loc: Option>, struct_type: types::Type<'a>, fields: Option<&[Field<'a>]>, values: &[RValue<'a>]) -> RValue<'a> { @@ -865,6 +880,22 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_vector_constructor<'a>(&'a self, loc: Option>, vector_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + + let ptr = gccjit_sys::gcc_jit_context_new_vector_constructor(self.ptr, loc_ptr, types::get_ptr(&vector_type), elements.len() as _, elements.as_ptr() as *mut *mut _); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Creates a new RValue from a given int value. pub fn new_rvalue_from_int<'a>(&'a self, ty: types::Type<'a>, @@ -1035,6 +1066,23 @@ impl<'ctx> Context<'ctx> { } } + /// Get a target-dependant builtin function from gcc. It's not clear what functions are + /// builtin and you'll likely need to consult the GCC internal docs + /// for a full list. + pub fn get_target_builtin_function<'a, S: AsRef>(&'a self, name: S) -> Function<'a> { + let name_ref = name.as_ref(); + unsafe { + let cstr = CString::new(name_ref).unwrap(); + let ptr = gccjit_sys::gcc_jit_context_get_target_builtin_function(self.ptr, + cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + function::from_ptr(ptr) + } + } + pub fn get_first_error(&self) -> Result, Utf8Error> { unsafe { let str = gccjit_sys::gcc_jit_context_get_first_error(self.ptr); From f809068c15c01f3b011b2e9da1abf47fa88c9003 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 21 Mar 2022 23:15:16 -0400 Subject: [PATCH 047/127] Add vector access operation --- gccjit_sys/src/lib.rs | 1 + src/context.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index e007903..581b456 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -581,4 +581,5 @@ extern { pub fn gcc_jit_context_new_rvalue_vector_perm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, elements1: *mut gcc_jit_rvalue, elements2: *mut gcc_jit_rvalue, mask: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_vector_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: size_t, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_vector_access(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, index: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; } diff --git a/src/context.rs b/src/context.rs index 9152f7d..6647aaa 100644 --- a/src/context.rs +++ b/src/context.rs @@ -896,6 +896,22 @@ impl<'ctx> Context<'ctx> { } } + pub fn new_vector_access<'a>(&'a self, loc: Option>, vector: RValue<'a>, index: RValue<'a>) -> LValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + + let ptr = gccjit_sys::gcc_jit_context_new_vector_access(self.ptr, loc_ptr, rvalue::get_ptr(&vector), rvalue::get_ptr(&index)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + lvalue::from_ptr(ptr) + } + } + /// Creates a new RValue from a given int value. pub fn new_rvalue_from_int<'a>(&'a self, ty: types::Type<'a>, From d37af14404fcf93281e20289dff222c9f94d7c0f Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 28 Mar 2022 21:22:05 -0400 Subject: [PATCH 048/127] Add support for packed struct --- gccjit_sys/src/lib.rs | 2 ++ src/types.rs | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 581b456..761dd8f 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -582,4 +582,6 @@ extern { pub fn gcc_jit_context_new_rvalue_vector_perm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, elements1: *mut gcc_jit_rvalue, elements2: *mut gcc_jit_rvalue, mask: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_vector_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: size_t, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_vector_access(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, index: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; + + pub fn gcc_jit_type_set_packed(typ: *mut gcc_jit_type); } diff --git a/src/types.rs b/src/types.rs index a329b02..2563fc7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -105,6 +105,12 @@ impl<'ctx> Type<'ctx> { } } + pub fn set_packed(&self) { + unsafe { + gccjit_sys::gcc_jit_type_set_packed(self.ptr); + } + } + /// Given a type T, creates a type of const T. pub fn make_const(self) -> Type<'ctx> { unsafe { From d2d2d8993cdde72366d097009d921ebd6405d205 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 13 Apr 2022 22:49:07 -0400 Subject: [PATCH 049/127] Add feature for future libgccjit 12 release --- Cargo.toml | 2 ++ gccjit_sys/Cargo.toml | 3 +++ gccjit_sys/src/lib.rs | 5 +++++ src/context.rs | 4 ++++ src/types.rs | 1 + 5 files changed, 15 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a88ffa4..63dd4ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ repository = "https://github.com/swgillespie/gccjit.rs" documentation = "http://swgillespie.github.io/gccjit.rs/gccjit/" readme = "README.md" +[features] +master = ["gccjit_sys/master"] [dependencies] gccjit_sys = { version = "0.0.1", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 1a61f71..71fc7c9 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -8,5 +8,8 @@ keywords = ["compiler", "jit", "gcc"] license = "GPL-3.0" repository = "https://github.com/swgillespie/gccjit.rs" +[features] +master = [] + [dependencies] libc = "0.1.6" diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 761dd8f..ef72eb0 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -577,11 +577,16 @@ extern { pub fn gcc_jit_lvalue_get_alignment(lvalue: *mut gcc_jit_lvalue) -> c_int; + #[cfg(feature="master")] pub fn gcc_jit_context_get_target_builtin_function(ctxt: *mut gcc_jit_context, name: *const c_char) -> *mut gcc_jit_function; + #[cfg(feature="master")] pub fn gcc_jit_context_new_rvalue_vector_perm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, elements1: *mut gcc_jit_rvalue, elements2: *mut gcc_jit_rvalue, mask: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + #[cfg(feature="master")] pub fn gcc_jit_context_new_vector_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: size_t, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + #[cfg(feature="master")] pub fn gcc_jit_context_new_vector_access(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, index: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; + #[cfg(feature="master")] pub fn gcc_jit_type_set_packed(typ: *mut gcc_jit_type); } diff --git a/src/context.rs b/src/context.rs index 6647aaa..6ddabe1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -827,6 +827,7 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] pub fn new_rvalue_vector_perm<'a>(&'a self, loc: Option>, elements1: RValue<'a>, elements2: RValue<'a>, mask: RValue<'a>) -> RValue<'a> { unsafe { let loc_ptr = match loc { @@ -880,6 +881,7 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] pub fn new_vector_constructor<'a>(&'a self, loc: Option>, vector_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { unsafe { let loc_ptr = match loc { @@ -896,6 +898,7 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] pub fn new_vector_access<'a>(&'a self, loc: Option>, vector: RValue<'a>, index: RValue<'a>) -> LValue<'a> { unsafe { let loc_ptr = match loc { @@ -1082,6 +1085,7 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] /// Get a target-dependant builtin function from gcc. It's not clear what functions are /// builtin and you'll likely need to consult the GCC internal docs /// for a full list. diff --git a/src/types.rs b/src/types.rs index 2563fc7..c839d10 100644 --- a/src/types.rs +++ b/src/types.rs @@ -105,6 +105,7 @@ impl<'ctx> Type<'ctx> { } } + #[cfg(feature="master")] pub fn set_packed(&self) { unsafe { gccjit_sys::gcc_jit_type_set_packed(self.ptr); From 383523c29c9dde572d2079ad21decb572da24cc9 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 29 Apr 2022 23:23:30 -0400 Subject: [PATCH 050/127] Add Type::is_const --- gccjit_sys/src/lib.rs | 3 +++ src/types.rs | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index ef72eb0..5245a42 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -589,4 +589,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_type_set_packed(typ: *mut gcc_jit_type); + + #[cfg(feature="master")] + pub fn gcc_jit_type_is_const(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; } diff --git a/src/types.rs b/src/types.rs index c839d10..0fe9280 100644 --- a/src/types.rs +++ b/src/types.rs @@ -209,6 +209,17 @@ impl<'ctx> Type<'ctx> { } } + #[cfg(feature="master")] + pub fn is_const(&self) -> Option> { + unsafe { + let value = gccjit_sys::gcc_jit_type_is_const(self.ptr); + if value.is_null() { + return None; + } + Some(from_ptr(value)) + } + } + pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { unsafe { gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) From ecd3560dc7b9626529855c2f29379dcdbfac92c4 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 24 May 2022 17:46:01 -0400 Subject: [PATCH 051/127] Add new functions --- gccjit_sys/src/lib.rs | 6 ++++++ src/context.rs | 16 ++++++++++++++++ src/lvalue.rs | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 5245a42..2d6fad6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -592,4 +592,10 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_type_is_const(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; + + #[cfg(feature="master")] + pub fn gcc_jit_context_convert_vector(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; + + #[cfg(feature="master")] + pub fn gcc_jit_global_set_readonly(global: *mut gcc_jit_lvalue); } diff --git a/src/context.rs b/src/context.rs index 6ddabe1..96fff74 100644 --- a/src/context.rs +++ b/src/context.rs @@ -915,6 +915,22 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + pub fn convert_vector<'a>(&'a self, loc: Option>, vector: RValue<'a>, type_: Type<'a>) -> RValue<'a> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_context_convert_vector(self.ptr, loc_ptr, rvalue::get_ptr(&vector), types::get_ptr(&type_)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Creates a new RValue from a given int value. pub fn new_rvalue_from_int<'a>(&'a self, ty: types::Type<'a>, diff --git a/src/lvalue.rs b/src/lvalue.rs index eef2084..0ddbf45 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -140,6 +140,12 @@ impl<'ctx> LValue<'ctx> { } } + pub fn global_set_readonly(&self) { + unsafe { + gccjit_sys::gcc_jit_global_set_readonly(self.ptr); + } + } + pub fn set_register_name(&self, reg_name: &str) { let name = CString::new(reg_name).unwrap(); unsafe { From 0ced87952473a445dd5cc1ce1045acd5f1a74533 Mon Sep 17 00:00:00 2001 From: yvt Date: Wed, 25 May 2022 23:33:51 +0900 Subject: [PATCH 052/127] Feature-gate `global_set_readonly` by `master` Fixes build failure when built without `feature = "master"`. The low-level function `gccjit_sys::gcc_jit_global_set_readonly` is only available with `feature = "master"`. --- src/lvalue.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lvalue.rs b/src/lvalue.rs index 0ddbf45..052cbee 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -140,6 +140,7 @@ impl<'ctx> LValue<'ctx> { } } + #[cfg(feature="master")] pub fn global_set_readonly(&self) { unsafe { gccjit_sys::gcc_jit_global_set_readonly(self.ptr); From a8997afb665dc467c1bdbddf04877143683f0cce Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 29 Jun 2022 21:03:10 -0400 Subject: [PATCH 053/127] Add support for function attributes --- gccjit_sys/src/lib.rs | 10 ++++++++++ src/function.rs | 14 ++++++++++++++ src/lib.rs | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 2d6fad6..efea2fb 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -260,6 +260,13 @@ pub enum gcc_jit_inline_mode GCC_JIT_INLINE_MODE_INLINE, } +#[cfg(feature="master")] +#[repr(C)] +pub enum gcc_jit_fn_attribute +{ + GCC_JIT_FN_ATTRIBUTE_TARGET, +} + #[link(name = "gccjit")] extern { // context operations @@ -598,4 +605,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_global_set_readonly(global: *mut gcc_jit_lvalue); + + #[cfg(feature="master")] + pub fn gcc_jit_function_add_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char); } diff --git a/src/function.rs b/src/function.rs index 3b90584..d07b07b 100644 --- a/src/function.rs +++ b/src/function.rs @@ -53,6 +53,12 @@ pub enum InlineMode { Inline, } +#[repr(C)] +#[derive(Clone, Copy, Debug)] +pub enum FnAttribute { + Target, +} + /// Function is gccjit's representation of a function. Functions are constructed /// by constructing basic blocks and connecting them together. Locals are declared /// at the function level. @@ -158,6 +164,14 @@ impl<'ctx> Function<'ctx> { lvalue::from_ptr(ptr) } } + + #[cfg(feature="master")] + pub fn add_attribute(&self, attribute: FnAttribute, value: &str) { + let cstr = CString::new(value).unwrap(); + unsafe { + gccjit_sys::gcc_jit_function_add_attribute(self.ptr, std::mem::transmute(attribute), cstr.as_ptr()); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_function) -> Function<'ctx> { diff --git a/src/lib.rs b/src/lib.rs index 7b7b3d8..8be70ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,5 +46,5 @@ pub use structs::Struct; pub use lvalue::{LValue, TlsModel, ToLValue}; pub use rvalue::{RValue, ToRValue}; pub use parameter::Parameter; -pub use function::{Function, FunctionType, InlineMode}; +pub use function::{FnAttribute, Function, FunctionType, InlineMode}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; From 1a60fe3918a5b3b0983c1ea09f4b9445001a6468 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 24 Jul 2022 18:07:12 -0400 Subject: [PATCH 054/127] Add support for variable attributes --- gccjit_sys/src/lib.rs | 11 ++++++++ src/function.rs | 40 +++++++++++++++++++++++----- src/lib.rs | 6 ++++- src/lvalue.rs | 62 +++++++++++++++++++++++++++++++++++++++++++ src/structs.rs | 7 ++++- 5 files changed, 117 insertions(+), 9 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index efea2fb..649651e 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -265,6 +265,14 @@ pub enum gcc_jit_inline_mode pub enum gcc_jit_fn_attribute { GCC_JIT_FN_ATTRIBUTE_TARGET, + GCC_JIT_FN_ATTRIBUTE_VISIBILITY, +} + +#[cfg(feature="master")] +#[repr(C)] +pub enum gcc_jit_variable_attribute +{ + GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY, } #[link(name = "gccjit")] @@ -608,4 +616,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_function_add_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char); + + #[cfg(feature="master")] + pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); } diff --git a/src/function.rs b/src/function.rs index d07b07b..fba1d14 100644 --- a/src/function.rs +++ b/src/function.rs @@ -9,6 +9,8 @@ use block; use context::Context; use location::Location; use location; +#[cfg(feature="master")] +use lvalue::{AttributeValue, Visibility}; use lvalue::LValue; use lvalue; use object::{ToObject, Object}; @@ -53,10 +55,28 @@ pub enum InlineMode { Inline, } -#[repr(C)] +#[cfg(feature="master")] #[derive(Clone, Copy, Debug)] -pub enum FnAttribute { - Target, +pub enum FnAttribute<'a> { + Target(&'a str), + Visibility(Visibility), +} + +#[cfg(feature="master")] +impl<'a> FnAttribute<'a> { + fn get_value(&self) -> AttributeValue { + match *self { + FnAttribute::Target(target) => AttributeValue::String(target), + FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), + } + } + + fn to_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { + match *self { + FnAttribute::Target(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_TARGET, + FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, + } + } } /// Function is gccjit's representation of a function. Functions are constructed @@ -166,10 +186,16 @@ impl<'ctx> Function<'ctx> { } #[cfg(feature="master")] - pub fn add_attribute(&self, attribute: FnAttribute, value: &str) { - let cstr = CString::new(value).unwrap(); - unsafe { - gccjit_sys::gcc_jit_function_add_attribute(self.ptr, std::mem::transmute(attribute), cstr.as_ptr()); + pub fn add_attribute<'a>(&self, attribute: FnAttribute<'a>) { + let value = attribute.get_value(); + match value { + AttributeValue::Int(_) => unimplemented!(), + AttributeValue::String(string) => { + let cstr = CString::new(string).unwrap(); + unsafe { + gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); + } + }, } } } diff --git a/src/lib.rs b/src/lib.rs index 8be70ec..1a2bce3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,8 +43,12 @@ pub use types::Type; pub use types::Typeable; pub use field::Field; pub use structs::Struct; +#[cfg(feature="master")] +pub use lvalue::{VarAttribute, Visibility}; pub use lvalue::{LValue, TlsModel, ToLValue}; pub use rvalue::{RValue, ToRValue}; pub use parameter::Parameter; -pub use function::{FnAttribute, Function, FunctionType, InlineMode}; +#[cfg(feature="master")] +pub use function::FnAttribute; +pub use function::{Function, FunctionType, InlineMode}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; diff --git a/src/lvalue.rs b/src/lvalue.rs index 052cbee..c5cd1fc 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -12,6 +12,54 @@ use field; use location::Location; use location; +#[cfg(feature="master")] +#[derive(Clone, Copy, Debug)] +pub enum Visibility { + Default, + Hidden, + Internal, + Protected, +} + +#[cfg(feature="master")] +impl Visibility { + pub fn as_str(&self) -> &'static str { + match *self { + Visibility::Default => "default", + Visibility::Hidden => "hidden", + Visibility::Internal => "internal", + Visibility::Protected => "protected", + } + } +} + +#[cfg(feature="master")] +pub enum AttributeValue<'a> { + Int(i32), + String(&'a str), +} + +#[cfg(feature="master")] +#[derive(Clone, Debug)] +pub enum VarAttribute { + Visibility(Visibility), +} + +#[cfg(feature="master")] +impl VarAttribute { + fn get_value(&self) -> AttributeValue { + match *self { + VarAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), + } + } + + fn to_sys(&self) -> gccjit_sys::gcc_jit_variable_attribute { + match *self { + VarAttribute::Visibility(_) => gccjit_sys::gcc_jit_variable_attribute::GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY, + } + } +} + #[derive(Clone, Copy, Debug)] pub enum TlsModel { GlobalDynamic, @@ -165,6 +213,20 @@ impl<'ctx> LValue<'ctx> { gccjit_sys::gcc_jit_lvalue_get_alignment(self.ptr) } } + + #[cfg(feature="master")] + pub fn add_attribute(&self, attribute: VarAttribute) { + let value = attribute.get_value(); + match value { + AttributeValue::Int(_) => unimplemented!(), + AttributeValue::String(string) => { + let cstr = CString::new(string).unwrap(); + unsafe { + gccjit_sys::gcc_jit_lvalue_add_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); + } + }, + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { diff --git a/src/structs.rs b/src/structs.rs index da1264f..f6c514d 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -70,7 +70,12 @@ impl<'ctx> Struct<'ctx> { pub fn get_field_count(&self) -> usize { unsafe { - gccjit_sys::gcc_jit_struct_get_field_count(self.ptr) as usize + let count = gccjit_sys::gcc_jit_struct_get_field_count(self.ptr) as usize; + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } + count } } } From f30cc2bd330f4fda3d625f305bdfd7e523e2d8f8 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 27 Aug 2022 15:30:32 -0400 Subject: [PATCH 055/127] Add used function attribute --- gccjit_sys/src/lib.rs | 6 +++++- src/function.rs | 10 +++++++++- src/lvalue.rs | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 649651e..21f45a6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -265,6 +265,7 @@ pub enum gcc_jit_inline_mode pub enum gcc_jit_fn_attribute { GCC_JIT_FN_ATTRIBUTE_TARGET, + GCC_JIT_FN_ATTRIBUTE_USED, GCC_JIT_FN_ATTRIBUTE_VISIBILITY, } @@ -615,7 +616,10 @@ extern { pub fn gcc_jit_global_set_readonly(global: *mut gcc_jit_lvalue); #[cfg(feature="master")] - pub fn gcc_jit_function_add_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char); + pub fn gcc_jit_function_add_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute); + + #[cfg(feature="master")] + pub fn gcc_jit_function_add_string_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char); #[cfg(feature="master")] pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); diff --git a/src/function.rs b/src/function.rs index fba1d14..3eba5e6 100644 --- a/src/function.rs +++ b/src/function.rs @@ -59,6 +59,7 @@ pub enum InlineMode { #[derive(Clone, Copy, Debug)] pub enum FnAttribute<'a> { Target(&'a str), + Used, Visibility(Visibility), } @@ -67,6 +68,7 @@ impl<'a> FnAttribute<'a> { fn get_value(&self) -> AttributeValue { match *self { FnAttribute::Target(target) => AttributeValue::String(target), + FnAttribute::Used => AttributeValue::None, FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), } } @@ -74,6 +76,7 @@ impl<'a> FnAttribute<'a> { fn to_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { match *self { FnAttribute::Target(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_TARGET, + FnAttribute::Used => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_USED, FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, } } @@ -190,10 +193,15 @@ impl<'ctx> Function<'ctx> { let value = attribute.get_value(); match value { AttributeValue::Int(_) => unimplemented!(), + AttributeValue::None => { + unsafe { + gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.to_sys()); + } + }, AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); unsafe { - gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); + gccjit_sys::gcc_jit_function_add_string_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); } }, } diff --git a/src/lvalue.rs b/src/lvalue.rs index c5cd1fc..1bd79bd 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -36,6 +36,7 @@ impl Visibility { #[cfg(feature="master")] pub enum AttributeValue<'a> { Int(i32), + None, String(&'a str), } @@ -219,6 +220,7 @@ impl<'ctx> LValue<'ctx> { let value = attribute.get_value(); match value { AttributeValue::Int(_) => unimplemented!(), + AttributeValue::None => unimplemented!(), AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); unsafe { From 1e6ecc67fe73ac995e511516eacf4fe3aec8974e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 9 Jan 2023 17:11:16 -0500 Subject: [PATCH 056/127] Add support for try/catch --- gccjit_sys/src/lib.rs | 12 +++++++++--- src/block.rs | 18 +++++++++++++++--- src/function.rs | 13 +++++++++---- src/types.rs | 10 ++++++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 21f45a6..49c6d80 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -570,9 +570,6 @@ extern { pub fn gcc_jit_lvalue_set_tls_model(lvalue: *mut gcc_jit_lvalue, model: gcc_jit_tls_model); pub fn gcc_jit_lvalue_set_link_section(lvalue: *mut gcc_jit_lvalue, name: *const c_char); - /*pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); - pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block);*/ - pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; //pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); @@ -623,4 +620,13 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); + + #[cfg(feature="master")] + pub fn gcc_jit_block_add_try_catch(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, catch_block: *mut gcc_jit_block); + + #[cfg(feature="master")] + pub fn gcc_jit_block_add_try_finally(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, finally_block: *mut gcc_jit_block); + + #[cfg(feature="master")] + pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); } diff --git a/src/block.rs b/src/block.rs index 78a5f49..1a17951 100644 --- a/src/block.rs +++ b/src/block.rs @@ -62,7 +62,7 @@ pub enum ComparisonOp { /// A basic block consists of a series of instructions terminated by a terminator /// instruction, which can be either a jump to one block, a conditional branch to /// two blocks (true/false branches), a return, or a void return. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Eq, Hash, PartialEq)] pub struct Block<'ctx> { marker: PhantomData<&'ctx Context<'ctx>>, ptr: *mut gccjit_sys::gcc_jit_block @@ -113,7 +113,19 @@ impl<'ctx> Block<'ctx> { } } - /*pub fn add_try_finally(&self, loc: Option>, try_block: Block<'ctx>, finally_block: Block<'ctx>) { + #[cfg(feature="master")] + pub fn add_try_catch(&self, loc: Option>, try_block: Block<'ctx>, catch_block: Block<'ctx>) { + let loc_ptr = match loc { + Some(loc) => unsafe { location::get_ptr(&loc) }, + None => ptr::null_mut() + }; + unsafe { + gccjit_sys::gcc_jit_block_add_try_catch(self.ptr, loc_ptr, try_block.ptr, catch_block.ptr); + } + } + + #[cfg(feature="master")] + pub fn add_try_finally(&self, loc: Option>, try_block: Block<'ctx>, finally_block: Block<'ctx>) { let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, None => ptr::null_mut() @@ -121,7 +133,7 @@ impl<'ctx> Block<'ctx> { unsafe { gccjit_sys::gcc_jit_block_add_try_finally(self.ptr, loc_ptr, try_block.ptr, finally_block.ptr); } - }*/ + } /// Assigns the value of an rvalue to an lvalue directly. Equivalent /// to = in C. diff --git a/src/function.rs b/src/function.rs index 3eba5e6..192c34c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -29,7 +29,7 @@ use types; /// is a function with external linkage, and always inline is a function that is /// always inlined wherever it is called and cannot be accessed outside of the jit. #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq)] pub enum FunctionType { /// Defines a function that is "exported" by the JIT and can be called from /// Rust. @@ -68,8 +68,8 @@ impl<'a> FnAttribute<'a> { fn get_value(&self) -> AttributeValue { match *self { FnAttribute::Target(target) => AttributeValue::String(target), - FnAttribute::Used => AttributeValue::None, FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), + FnAttribute::Used => AttributeValue::None, } } @@ -164,11 +164,12 @@ impl<'ctx> Function<'ctx> { } } - /*pub fn set_personality_function(&self, personality_func: Function<'ctx>) { + #[cfg(feature="master")] + pub fn set_personality_function(&self, personality_func: Function<'ctx>) { unsafe { gccjit_sys::gcc_jit_function_set_personality_function(self.ptr, personality_func.ptr); } - }*/ + } pub fn new_local>(&self, loc: Option>, @@ -184,6 +185,10 @@ impl<'ctx> Function<'ctx> { loc_ptr, types::get_ptr(&ty), cstr.as_ptr()); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{} ({:?})", error, self); + } lvalue::from_ptr(ptr) } } diff --git a/src/types.rs b/src/types.rs index 0fe9280..a40850f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -53,6 +53,16 @@ pub struct FunctionPtrType<'ctx> { ptr: *mut gccjit_sys::gcc_jit_function_type } +impl<'ctx> fmt::Debug for FunctionPtrType<'ctx> { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { + write!(fmt, "{:?} (", self.get_return_type())?; + for i in 0..self.get_param_count() { + write!(fmt, "{:?}, ", self.get_param_type(i))?; + } + write!(fmt, ")") + } +} + impl<'ctx> FunctionPtrType<'ctx> { unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_function_type) -> FunctionPtrType<'ctx> { FunctionPtrType { From 1bd270d0d130fe31807cfbe509ca095c082e5848 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 25 Feb 2023 11:49:17 -0500 Subject: [PATCH 057/127] Remove vector constructor and add bfloat16 --- gccjit_sys/src/lib.rs | 7 +++++-- src/context.rs | 19 ++----------------- src/function.rs | 9 ++++++++- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 49c6d80..26bb57e 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -116,6 +116,8 @@ pub enum gcc_jit_types { GCC_JIT_TYPE_INT32_T, GCC_JIT_TYPE_INT64_T, GCC_JIT_TYPE_INT128_T, + + GCC_JIT_TYPE_BFLOAT16, } #[repr(C)] @@ -264,6 +266,9 @@ pub enum gcc_jit_inline_mode #[repr(C)] pub enum gcc_jit_fn_attribute { + /*GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, + GCC_JIT_FN_ATTRIBUTE_INLINE, + GCC_JIT_FN_ATTRIBUTE_NOINLINE,*/ GCC_JIT_FN_ATTRIBUTE_TARGET, GCC_JIT_FN_ATTRIBUTE_USED, GCC_JIT_FN_ATTRIBUTE_VISIBILITY, @@ -596,8 +601,6 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_context_new_rvalue_vector_perm(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, elements1: *mut gcc_jit_rvalue, elements2: *mut gcc_jit_rvalue, mask: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; #[cfg(feature="master")] - pub fn gcc_jit_context_new_vector_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: size_t, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; - #[cfg(feature="master")] pub fn gcc_jit_context_new_vector_access(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, index: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; #[cfg(feature="master")] diff --git a/src/context.rs b/src/context.rs index 96fff74..267e8af 100644 --- a/src/context.rs +++ b/src/context.rs @@ -881,23 +881,6 @@ impl<'ctx> Context<'ctx> { } } - #[cfg(feature="master")] - pub fn new_vector_constructor<'a>(&'a self, loc: Option>, vector_type: types::Type<'a>, elements: &[RValue<'a>]) -> RValue<'a> { - unsafe { - let loc_ptr = match loc { - Some(loc) => location::get_ptr(&loc), - None => ptr::null_mut() - }; - - let ptr = gccjit_sys::gcc_jit_context_new_vector_constructor(self.ptr, loc_ptr, types::get_ptr(&vector_type), elements.len() as _, elements.as_ptr() as *mut *mut _); - #[cfg(debug_assertions)] - if let Ok(Some(error)) = self.get_last_error() { - panic!("{}", error); - } - rvalue::from_ptr(ptr) - } - } - #[cfg(feature="master")] pub fn new_vector_access<'a>(&'a self, loc: Option>, vector: RValue<'a>, index: RValue<'a>) -> LValue<'a> { unsafe { @@ -1298,6 +1281,7 @@ pub enum CType { UInt64t, UInt128t, ConstCharPtr, + BFloat16, } impl CType { @@ -1330,6 +1314,7 @@ impl CType { UInt64t => GCC_JIT_TYPE_UINT64_T, UInt128t => GCC_JIT_TYPE_UINT128_T, ConstCharPtr => GCC_JIT_TYPE_CONST_CHAR_PTR, + BFloat16 => GCC_JIT_TYPE_BFLOAT16, } } } diff --git a/src/function.rs b/src/function.rs index 192c34c..0fcf5c8 100644 --- a/src/function.rs +++ b/src/function.rs @@ -58,6 +58,9 @@ pub enum InlineMode { #[cfg(feature="master")] #[derive(Clone, Copy, Debug)] pub enum FnAttribute<'a> { + /*AlwaysInline, + Inline, + NoInline,*/ Target(&'a str), Used, Visibility(Visibility), @@ -69,12 +72,16 @@ impl<'a> FnAttribute<'a> { match *self { FnAttribute::Target(target) => AttributeValue::String(target), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), - FnAttribute::Used => AttributeValue::None, + /*FnAttribute::AlwaysInline | FnAttribute::Inline | FnAttribute::NoInline |*/ FnAttribute::Used => + AttributeValue::None, } } fn to_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { match *self { + /*FnAttribute::AlwaysInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, + FnAttribute::Inline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_INLINE, + FnAttribute::NoInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NOINLINE,*/ FnAttribute::Target(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_TARGET, FnAttribute::Used => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_USED, FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, From eefb8c662d61477f34b7c32d26bcda5f1ef08432 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 4 Mar 2023 00:45:19 -0500 Subject: [PATCH 058/127] Make new_array_type take u64 --- gccjit_sys/src/lib.rs | 4 ++-- src/context.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 26bb57e..3d7c894 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -2,7 +2,7 @@ extern crate libc; -use libc::{c_char, c_int, FILE, c_void, c_long, c_double, size_t, ssize_t}; +use libc::{c_char, c_int, FILE, c_void, c_long, c_double, c_ulong, size_t, ssize_t}; // opaque pointers pub enum gcc_jit_context {} @@ -344,7 +344,7 @@ extern { pub fn gcc_jit_context_new_array_type(ctx: *mut gcc_jit_context, loc: *mut gcc_jit_location, ty: *mut gcc_jit_type, - num_elements: c_int) -> *mut gcc_jit_type; + num_elements: c_ulong) -> *mut gcc_jit_type; // struct handling pub fn gcc_jit_context_new_field(ctx: *mut gcc_jit_context, loc: *mut gcc_jit_location, diff --git a/src/context.rs b/src/context.rs index 267e8af..bd38af9 100644 --- a/src/context.rs +++ b/src/context.rs @@ -3,7 +3,7 @@ use std::ops::Drop; use std::ffi::{CStr, CString}; use std::marker::PhantomData; use std::mem; -use std::os::raw::c_int; +use std::os::raw::{c_int, c_ulong}; use std::ptr; use std::str::Utf8Error; @@ -403,7 +403,7 @@ impl<'ctx> Context<'ctx> { pub fn new_array_type<'a>(&'a self, loc: Option>, ty: types::Type<'a>, - num_elements: i32) -> types::Type<'a> { + num_elements: u64) -> types::Type<'a> { let loc_ptr = match loc { Some(loc) => unsafe { location::get_ptr(&loc) }, None => ptr::null_mut() @@ -412,7 +412,7 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_array_type(self.ptr, loc_ptr, types::get_ptr(&ty), - num_elements); + num_elements as c_ulong); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); From fbb2400ebad7fc19cd96007b05681cda9475c6c3 Mon Sep 17 00:00:00 2001 From: J Pratt Date: Tue, 7 Mar 2023 20:38:22 +1100 Subject: [PATCH 059/127] Move a heading out of the preceding paragraph --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b6c50f..5d83c8d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ There are four examples right now living in the `examples/` directory: * `square_function` - A square function, as a simple example for code generation, * `factorial` - A factorial function, as a more complicated example involving recursion and conditional jumps. gcc removes all recursion at O3. * `hello_world` - An example that invokes a function written in Rust from JIT-compiled code. -* `brainfuck` - An ahead-of-time compiler for brainfuck. The speed is very impressive given how easy it was to setup with libgccjit. Some benchmarks, my compiler vs a naive interpreter I wrote in Haskell: +* `brainfuck` - An ahead-of-time compiler for brainfuck. The speed is very impressive given how easy it was to setup with libgccjit. + +## Some benchmarks, my compiler vs a naive interpreter I wrote in Haskell: ``` sierpinski_triangle, haskell: real 0m0.052s From 91bfc4b3bf583d7da3160d763b2e317873f42090 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 30 Mar 2023 17:25:18 +0200 Subject: [PATCH 060/127] Update libc version to 0.2 to support s390x --- gccjit_sys/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 71fc7c9..900cd53 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -12,4 +12,4 @@ repository = "https://github.com/swgillespie/gccjit.rs" master = [] [dependencies] -libc = "0.1.6" +libc = "0.2" From 98a29ddd64f662beb5d11810434fbeaad4a1856c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 1 Apr 2023 14:54:22 -0400 Subject: [PATCH 061/127] Add support for inline attribute --- gccjit_sys/src/lib.rs | 15 ++------------- src/block.rs | 4 ++++ src/function.rs | 25 +++++-------------------- src/lib.rs | 2 +- 4 files changed, 12 insertions(+), 34 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 3d7c894..54c8aa8 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -253,22 +253,13 @@ pub enum gcc_jit_comparison GCC_JIT_COMPARISON_GE } -#[repr(C)] -pub enum gcc_jit_inline_mode -{ - GCC_JIT_INLINE_MODE_DEFAULT, - GCC_JIT_INLINE_MODE_ALWAYS_INLINE, - GCC_JIT_INLINE_MODE_NO_INLINE, - GCC_JIT_INLINE_MODE_INLINE, -} - #[cfg(feature="master")] #[repr(C)] pub enum gcc_jit_fn_attribute { - /*GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, + GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, GCC_JIT_FN_ATTRIBUTE_INLINE, - GCC_JIT_FN_ATTRIBUTE_NOINLINE,*/ + GCC_JIT_FN_ATTRIBUTE_NOINLINE, GCC_JIT_FN_ATTRIBUTE_TARGET, GCC_JIT_FN_ATTRIBUTE_USED, GCC_JIT_FN_ATTRIBUTE_VISIBILITY, @@ -577,8 +568,6 @@ extern { pub fn gcc_jit_context_new_bitcast(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, rvalue: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; - //pub fn gcc_jit_function_set_inline_mode(func: *mut gcc_jit_function, inline_mode: gcc_jit_inline_mode); - pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; diff --git a/src/block.rs b/src/block.rs index 1a17951..75db545 100644 --- a/src/block.rs +++ b/src/block.rs @@ -239,6 +239,10 @@ impl<'ctx> Block<'ctx> { loc_ptr, target.ptr); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } /// Terminates a block by returning from the containing function, setting diff --git a/src/function.rs b/src/function.rs index 0fcf5c8..eae1ae6 100644 --- a/src/function.rs +++ b/src/function.rs @@ -46,21 +46,12 @@ pub enum FunctionType { AlwaysInline } -#[repr(C)] -#[derive(Clone, Copy, Debug)] -pub enum InlineMode { - Default, - AlwaysInline, - NoInline, - Inline, -} - #[cfg(feature="master")] #[derive(Clone, Copy, Debug)] pub enum FnAttribute<'a> { - /*AlwaysInline, + AlwaysInline, Inline, - NoInline,*/ + NoInline, Target(&'a str), Used, Visibility(Visibility), @@ -72,16 +63,16 @@ impl<'a> FnAttribute<'a> { match *self { FnAttribute::Target(target) => AttributeValue::String(target), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), - /*FnAttribute::AlwaysInline | FnAttribute::Inline | FnAttribute::NoInline |*/ FnAttribute::Used => + FnAttribute::AlwaysInline | FnAttribute::Inline | FnAttribute::NoInline | FnAttribute::Used => AttributeValue::None, } } fn to_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { match *self { - /*FnAttribute::AlwaysInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, + FnAttribute::AlwaysInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, FnAttribute::Inline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_INLINE, - FnAttribute::NoInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NOINLINE,*/ + FnAttribute::NoInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NOINLINE, FnAttribute::Target(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_TARGET, FnAttribute::Used => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_USED, FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, @@ -122,12 +113,6 @@ impl<'ctx> Function<'ctx> { } } - /*pub fn set_inline_mode(&self, inline_mode: InlineMode) { - unsafe { - gccjit_sys::gcc_jit_function_set_inline_mode(self.ptr, std::mem::transmute(inline_mode)); - } - }*/ - pub fn get_param_count(&self) -> usize { unsafe { gccjit_sys::gcc_jit_function_get_param_count(self.ptr) as usize diff --git a/src/lib.rs b/src/lib.rs index 1a2bce3..f936209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,5 +50,5 @@ pub use rvalue::{RValue, ToRValue}; pub use parameter::Parameter; #[cfg(feature="master")] pub use function::FnAttribute; -pub use function::{Function, FunctionType, InlineMode}; +pub use function::{Function, FunctionType}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; From d6e52626cfc6f487094a5d5ac66302baf3439984 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 16 Apr 2023 14:10:20 -0400 Subject: [PATCH 062/127] Add set_global_personality_function_name --- gccjit_sys/src/lib.rs | 3 +++ src/lib.rs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 54c8aa8..37ddb85 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -621,4 +621,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_function_set_personality_function(func: *mut gcc_jit_function, personality_func: *mut gcc_jit_function); + + #[cfg(feature="master")] + pub fn gcc_jit_set_global_personality_function_name(name: *const c_char); } diff --git a/src/lib.rs b/src/lib.rs index f936209..91361e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,3 +52,11 @@ pub use parameter::Parameter; pub use function::FnAttribute; pub use function::{Function, FunctionType}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; + +#[cfg(feature="master")] +pub fn set_global_personality_function_name(name: &'static [u8]) { + debug_assert!(name.ends_with(&[b'\0']), "Expecting a NUL-terminated C string"); + unsafe { + gccjit_sys::gcc_jit_set_global_personality_function_name(name.as_ptr() as *const _); + } +} From 4cbd3f1fe45da89d0598f6b68d77ed166d934177 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 30 Jun 2023 15:11:27 +0200 Subject: [PATCH 063/127] Add GCC_JIT_FN_ATTRIBUTE_COLD attribute --- gccjit_sys/src/lib.rs | 1 + src/function.rs | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 37ddb85..eb3aac2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -263,6 +263,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_TARGET, GCC_JIT_FN_ATTRIBUTE_USED, GCC_JIT_FN_ATTRIBUTE_VISIBILITY, + GCC_JIT_FN_ATTRIBUTE_COLD, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index eae1ae6..acca0a6 100644 --- a/src/function.rs +++ b/src/function.rs @@ -55,6 +55,7 @@ pub enum FnAttribute<'a> { Target(&'a str), Used, Visibility(Visibility), + Cold, } #[cfg(feature="master")] @@ -63,8 +64,11 @@ impl<'a> FnAttribute<'a> { match *self { FnAttribute::Target(target) => AttributeValue::String(target), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), - FnAttribute::AlwaysInline | FnAttribute::Inline | FnAttribute::NoInline | FnAttribute::Used => - AttributeValue::None, + FnAttribute::AlwaysInline + | FnAttribute::Inline + | FnAttribute::NoInline + | FnAttribute::Used + | FnAttribute::Cold => AttributeValue::None, } } @@ -76,6 +80,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::Target(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_TARGET, FnAttribute::Used => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_USED, FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, + FnAttribute::Cold => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_COLD, } } } From 57e957e746fc12ba2b59f84faea76d197a5fa782 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 4 Jul 2023 17:47:42 +0200 Subject: [PATCH 064/127] Add CI checks --- .github/workflows/ci.yml | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..ed69266 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,76 @@ +name: CI + +on: + - pull_request + +permissions: + contents: read + +env: + # Enable backtraces for easier debugging + RUST_BACKTRACE: 1 + +jobs: + build: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: clippy + + - name: Download artifact + uses: dawidd6/action-download-artifact@v2 + with: + workflow: main.yml + name: "libgccjit.so" + path: gcc-build + repo: antoyo/gcc + branch: "master" + event: push + search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts. + + - name: Setup path to libgccjit + run: | + echo $(readlink -f gcc-build) > gcc_path + # NOTE: the filename is still libgccjit.so even when the artifact name is different. + ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0 + + - name: Set env + run: | + echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV + echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV + echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV + + - name: Set RUST_COMPILER_RT_ROOT + run: echo "RUST_COMPILER_RT_ROOT="${{ env.workspace }}/llvm/compiler-rt >> $GITHUB_ENV + + - name: Cache cargo registry + uses: actions/cache@v3 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry2-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo index + uses: actions/cache@v3 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + + - name: Cache cargo target dir + uses: actions/cache@v3 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain') }} + + - name: Build + run: | + cargo build + cargo build --examples + + #- name: clippy + #run: cargo clippy --all-targets -- -D warnings From 46a1b61f753c63c0aa7de624ebbce2826738374e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 4 Jul 2023 20:46:22 +0200 Subject: [PATCH 065/127] Fix clippy lints --- src/asm.rs | 2 +- src/block.rs | 2 +- src/context.rs | 18 ++++++++++-------- src/field.rs | 2 +- src/function.rs | 10 +++++----- src/lib.rs | 2 ++ src/location.rs | 2 +- src/lvalue.rs | 13 +++++++------ src/object.rs | 2 +- src/parameter.rs | 2 +- src/rvalue.rs | 2 +- src/structs.rs | 4 ++-- src/types.rs | 6 +++--- 13 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/asm.rs b/src/asm.rs index 501c618..44ece02 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -68,7 +68,7 @@ impl<'ctx> ExtendedAsm<'ctx> { pub unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_extended_asm) -> Self { Self { marker: PhantomData, - ptr: ptr + ptr } } } diff --git a/src/block.rs b/src/block.rs index 75db545..b43c6a6 100644 --- a/src/block.rs +++ b/src/block.rs @@ -330,7 +330,7 @@ impl<'ctx> Block<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_block) -> Block<'ctx> { Block { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/context.rs b/src/context.rs index bd38af9..632abfb 100644 --- a/src/context.rs +++ b/src/context.rs @@ -446,7 +446,7 @@ impl<'ctx> Context<'ctx> { }; let num_fields = fields.len() as i32; let mut fields_ptrs : Vec<_> = fields.iter() - .map(|x| unsafe { field::get_ptr(&x) }) + .map(|x| unsafe { field::get_ptr(x) }) .collect(); unsafe { let cname = CString::new(name_ref).unwrap(); @@ -498,7 +498,7 @@ impl<'ctx> Context<'ctx> { }; let num_fields = fields.len() as i32; let mut fields_ptrs : Vec<_> = fields.iter() - .map(|x| unsafe { field::get_ptr(&x) }) + .map(|x| unsafe { field::get_ptr(x) }) .collect(); unsafe { let cname = CString::new(name_ref).unwrap(); @@ -530,7 +530,7 @@ impl<'ctx> Context<'ctx> { }; let num_types = param_types.len() as i32; let mut types_ptrs : Vec<_> = param_types.iter() - .map(|x| unsafe { types::get_ptr(&x) }) + .map(|x| unsafe { types::get_ptr(x) }) .collect(); unsafe { let ptr = gccjit_sys::gcc_jit_context_new_function_ptr_type(self.ptr, @@ -563,7 +563,7 @@ impl<'ctx> Context<'ctx> { }; let num_params = params.len() as i32; let mut params_ptrs : Vec<_> = params.iter() - .map(|x| unsafe { parameter::get_ptr(&x) }) + .map(|x| unsafe { parameter::get_ptr(x) }) .collect(); unsafe { let cstr = CString::new(name_ref).unwrap(); @@ -678,7 +678,7 @@ impl<'ctx> Context<'ctx> { }; let num_params = args.len() as i32; let mut params_ptrs : Vec<_> = args.iter() - .map(|x| unsafe { rvalue::get_ptr(&x) }) + .map(|x| unsafe { rvalue::get_ptr(x) }) .collect(); unsafe { let ptr = gccjit_sys::gcc_jit_context_new_call(self.ptr, @@ -978,6 +978,7 @@ impl<'ctx> Context<'ctx> { /// Creates an RValue for a raw pointer. This function /// requires that the lifetime of the pointer be greater /// than that of the jitted program. + #[allow(clippy::not_unsafe_ptr_arg_deref)] pub fn new_rvalue_from_ptr<'a>(&'a self, ty: types::Type<'a>, value: *mut ()) -> RValue<'a> { @@ -1172,7 +1173,7 @@ pub unsafe fn get_ptr<'ctx>(ctx: &'ctx Context<'ctx>) -> *mut gccjit_sys::gcc_ji pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_context) -> Context<'ctx> { Context { marker: PhantomData, - ptr: ptr + ptr } } @@ -1256,6 +1257,7 @@ mod tests { }*/ } +#[derive(Clone, Copy)] pub enum CType { Bool, Char, @@ -1285,11 +1287,11 @@ pub enum CType { } impl CType { - fn to_sys(&self) -> gccjit_sys::gcc_jit_types { + fn to_sys(self) -> gccjit_sys::gcc_jit_types { use gccjit_sys::gcc_jit_types::*; use self::CType::*; - match *self { + match self { Bool => GCC_JIT_TYPE_BOOL, Char => GCC_JIT_TYPE_CHAR, UChar => GCC_JIT_TYPE_UNSIGNED_CHAR, diff --git a/src/field.rs b/src/field.rs index 29b72e1..d7aaa9d 100644 --- a/src/field.rs +++ b/src/field.rs @@ -33,7 +33,7 @@ impl<'ctx> fmt::Debug for Field<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_field) -> Field<'ctx> { Field { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/function.rs b/src/function.rs index acca0a6..698f786 100644 --- a/src/function.rs +++ b/src/function.rs @@ -47,7 +47,7 @@ pub enum FunctionType { } #[cfg(feature="master")] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] pub enum FnAttribute<'a> { AlwaysInline, Inline, @@ -72,7 +72,7 @@ impl<'a> FnAttribute<'a> { } } - fn to_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { + fn as_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { match *self { FnAttribute::AlwaysInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, FnAttribute::Inline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_INLINE, @@ -197,13 +197,13 @@ impl<'ctx> Function<'ctx> { AttributeValue::Int(_) => unimplemented!(), AttributeValue::None => { unsafe { - gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.to_sys()); + gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.as_sys()); } }, AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); unsafe { - gccjit_sys::gcc_jit_function_add_string_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); + gccjit_sys::gcc_jit_function_add_string_attribute(self.ptr, attribute.as_sys(), cstr.as_ptr()); } }, } @@ -213,7 +213,7 @@ impl<'ctx> Function<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_function) -> Function<'ctx> { Function { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/lib.rs b/src/lib.rs index 91361e6..efded20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,8 @@ //! never outlive the Context object from which they came, a requirement //! to using libgccjit correctly. +#![allow(clippy::needless_lifetimes)] + extern crate gccjit_sys; mod asm; diff --git a/src/location.rs b/src/location.rs index 1cabc18..3a6c0f2 100644 --- a/src/location.rs +++ b/src/location.rs @@ -30,7 +30,7 @@ impl<'ctx> fmt::Debug for Location<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_location) -> Location<'ctx> { Location { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/lvalue.rs b/src/lvalue.rs index 1bd79bd..fc038da 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -35,13 +35,14 @@ impl Visibility { #[cfg(feature="master")] pub enum AttributeValue<'a> { + #[allow(dead_code)] Int(i32), None, String(&'a str), } #[cfg(feature="master")] -#[derive(Clone, Debug)] +#[derive(Clone, Copy, Debug)] pub enum VarAttribute { Visibility(Visibility), } @@ -54,8 +55,8 @@ impl VarAttribute { } } - fn to_sys(&self) -> gccjit_sys::gcc_jit_variable_attribute { - match *self { + fn to_sys(self) -> gccjit_sys::gcc_jit_variable_attribute { + match self { VarAttribute::Visibility(_) => gccjit_sys::gcc_jit_variable_attribute::GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY, } } @@ -71,10 +72,10 @@ pub enum TlsModel { } impl TlsModel { - fn to_sys(&self) -> gccjit_sys::gcc_jit_tls_model { + fn to_sys(self) -> gccjit_sys::gcc_jit_tls_model { use gccjit_sys::gcc_jit_tls_model::*; - match *self { + match self { TlsModel::GlobalDynamic => GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC, TlsModel::LocalDynamic => GCC_JIT_TLS_MODEL_LOCAL_DYNAMIC, TlsModel::InitialExec => GCC_JIT_TLS_MODEL_INITIAL_EXEC, @@ -234,7 +235,7 @@ impl<'ctx> LValue<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { LValue { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/object.rs b/src/object.rs index b922681..56f3e84 100644 --- a/src/object.rs +++ b/src/object.rs @@ -67,7 +67,7 @@ impl<'ctx> ToObject<'ctx> for Object<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_object) -> Object<'ctx> { Object { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/parameter.rs b/src/parameter.rs index 0c26074..b93132e 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -54,7 +54,7 @@ impl<'ctx> ToLValue<'ctx> for Parameter<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_param) -> Parameter<'ctx> { Parameter { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/rvalue.rs b/src/rvalue.rs index 54d4c8e..fead9d5 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -159,7 +159,7 @@ impl<'ctx> RValue<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_rvalue) -> RValue<'ctx> { RValue { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/structs.rs b/src/structs.rs index f6c514d..efb2efb 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -38,7 +38,7 @@ impl<'ctx> Struct<'ctx> { }; let num_fields = fields.len() as i32; let mut fields_ptrs : Vec<_> = fields.iter() - .map(|x| unsafe { field::get_ptr(&x) }) + .map(|x| unsafe { field::get_ptr(x) }) .collect(); unsafe { gccjit_sys::gcc_jit_struct_set_fields(self.ptr, @@ -97,6 +97,6 @@ impl<'ctx> fmt::Debug for Struct<'ctx> { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_struct) -> Struct<'ctx> { Struct { marker: PhantomData, - ptr: ptr + ptr } } diff --git a/src/types.rs b/src/types.rs index a40850f..4004461 100644 --- a/src/types.rs +++ b/src/types.rs @@ -30,7 +30,7 @@ impl<'ctx> VectorType<'ctx> { unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_vector_type) -> VectorType<'ctx> { VectorType { marker: PhantomData, - ptr: ptr + ptr } } @@ -67,7 +67,7 @@ impl<'ctx> FunctionPtrType<'ctx> { unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_function_type) -> FunctionPtrType<'ctx> { FunctionPtrType { marker: PhantomData, - ptr: ptr + ptr } } @@ -331,7 +331,7 @@ impl Typeable for *const T { pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_type) -> Type<'ctx> { Type { marker: PhantomData, - ptr: ptr + ptr } } From 220416c45bb32e4f8955dbab6631403d5e90514c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 4 Jul 2023 17:57:30 +0200 Subject: [PATCH 066/127] Add clippy check in CI --- .github/workflows/ci.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed69266..008c186 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,7 +70,10 @@ jobs: - name: Build run: | cargo build + cargo build --features master cargo build --examples - #- name: clippy - #run: cargo clippy --all-targets -- -D warnings + - name: clippy + run: | + cargo clippy --all-targets -- -D warnings + cargo clippy --all-targets --features master -- -D warnings From 90baf32418b75e497fa8169f320a2b289625be64 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 4 Jul 2023 19:49:17 +0200 Subject: [PATCH 067/127] Fix CI failure --- .github/workflows/ci.yml | 76 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 008c186..b1e9090 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,19 +13,18 @@ env: jobs: build: runs-on: ubuntu-22.04 - steps: - - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true components: clippy - - name: Download artifact - uses: dawidd6/action-download-artifact@v2 - with: + - name: Download artifact + uses: dawidd6/action-download-artifact@v2 + with: workflow: main.yml name: "libgccjit.so" path: gcc-build @@ -34,46 +33,43 @@ jobs: event: push search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts. - - name: Setup path to libgccjit - run: | + - name: Setup path to libgccjit + run: | echo $(readlink -f gcc-build) > gcc_path # NOTE: the filename is still libgccjit.so even when the artifact name is different. ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0 - - name: Set env - run: | - echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV - echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV - echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV - - - name: Set RUST_COMPILER_RT_ROOT - run: echo "RUST_COMPILER_RT_ROOT="${{ env.workspace }}/llvm/compiler-rt >> $GITHUB_ENV + - name: Set env + run: | + echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV + echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV + echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV - - name: Cache cargo registry - uses: actions/cache@v3 - with: - path: ~/.cargo/registry - key: ${{ runner.os }}-cargo-registry2-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo registry + uses: actions/cache@v3 + with: + path: ~/.cargo/registry + key: ${{ runner.os }}-cargo-registry2-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo index - uses: actions/cache@v3 - with: - path: ~/.cargo/git - key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} + - name: Cache cargo index + uses: actions/cache@v3 + with: + path: ~/.cargo/git + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo target dir - uses: actions/cache@v3 - with: - path: target - key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain') }} + - name: Cache cargo target dir + uses: actions/cache@v3 + with: + path: target + key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain') }} - - name: Build - run: | - cargo build - cargo build --features master - cargo build --examples + - name: Build + run: | + cargo build + cargo build --features master + cargo build --examples - - name: clippy - run: | - cargo clippy --all-targets -- -D warnings - cargo clippy --all-targets --features master -- -D warnings + - name: clippy + run: | + cargo clippy --all-targets -- -D warnings + cargo clippy --all-targets --features master -- -D warnings From bfe9a0a67c4ed139530c6a2b963df26de51029f7 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 4 Jul 2023 19:57:54 -0400 Subject: [PATCH 068/127] Add support for target info --- gccjit_sys/src/lib.rs | 16 +++++++++++++ src/context.rs | 9 ++++++++ src/lib.rs | 4 ++++ src/rvalue.rs | 5 +++- src/target_info.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/target_info.rs diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index eb3aac2..29927a2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -21,6 +21,7 @@ pub enum gcc_jit_case {} pub enum gcc_jit_function_type {} pub enum gcc_jit_vector_type {} pub enum gcc_jit_extended_asm {} +pub enum gcc_jit_target_info {} #[repr(C)] pub enum gcc_jit_tls_model { @@ -625,4 +626,19 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_set_global_personality_function_name(name: *const c_char); + + #[cfg(feature="master")] + pub fn gcc_jit_context_get_target_info(ctxt: *mut gcc_jit_context) -> *mut gcc_jit_target_info; + + #[cfg(feature="master")] + pub fn gcc_jit_target_info_release(info: *mut gcc_jit_target_info); + + #[cfg(feature="master")] + pub fn gcc_jit_target_info_cpu_supports(info: *mut gcc_jit_target_info, feature: *const c_char) -> c_int; + + #[cfg(feature="master")] + pub fn gcc_jit_target_info_arch(info: *mut gcc_jit_target_info) -> *const c_char; + + #[cfg(feature="master")] + pub fn gcc_jit_target_info_supports_128bit_int(info: *mut gcc_jit_target_info) -> c_int; } diff --git a/src/context.rs b/src/context.rs index 632abfb..96615e2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -21,6 +21,8 @@ use object::{self, Object, ToObject}; use parameter::{self, Parameter}; use rvalue::{self, RValue, ToRValue}; use structs::{self, Struct}; +#[cfg(feature="master")] +use target_info::{self, TargetInfo}; use Type; use types; @@ -269,6 +271,13 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + pub fn get_target_info(&self) -> TargetInfo { + unsafe { + target_info::from_ptr(gccjit_sys::gcc_jit_context_get_target_info(self.ptr)) + } + } + /// Creates a new child context from this context. The child context /// is a fully-featured context, but it has a lifetime that is strictly /// less than the lifetime that spawned it. diff --git a/src/lib.rs b/src/lib.rs index efded20..9e22570 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,8 @@ mod rvalue; mod parameter; mod function; mod block; +#[cfg(feature="master")] +mod target_info; pub use context::Context; pub use context::CType; @@ -54,6 +56,8 @@ pub use parameter::Parameter; pub use function::FnAttribute; pub use function::{Function, FunctionType}; pub use block::{Block, BinaryOp, UnaryOp, ComparisonOp}; +#[cfg(feature="master")] +pub use target_info::TargetInfo; #[cfg(feature="master")] pub fn set_global_personality_function_name(name: &'static [u8]) { diff --git a/src/rvalue.rs b/src/rvalue.rs index fead9d5..f3da005 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -132,6 +132,10 @@ impl<'ctx> RValue<'ctx> { let ptr = gccjit_sys::gcc_jit_rvalue_dereference_field(self.ptr, loc_ptr, field::get_ptr(&field)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } lvalue::from_ptr(ptr) } } @@ -166,4 +170,3 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_rvalue) -> RValue<'ct pub unsafe fn get_ptr<'ctx>(rvalue: &RValue<'ctx>) -> *mut gccjit_sys::gcc_jit_rvalue { rvalue.ptr } - diff --git a/src/target_info.rs b/src/target_info.rs new file mode 100644 index 0000000..3dea155 --- /dev/null +++ b/src/target_info.rs @@ -0,0 +1,53 @@ +use std::{ffi::CStr, fmt}; + +pub struct TargetInfo { + ptr: *mut gccjit_sys::gcc_jit_target_info, +} + +unsafe impl Send for TargetInfo {} +unsafe impl Sync for TargetInfo {} + +impl fmt::Debug for TargetInfo { + fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> { + "TargetInfo".fmt(fmt) + } +} + +impl TargetInfo { + pub fn cpu_supports(&self, feature: &[u8]) -> bool { + debug_assert!(feature.ends_with(&[b'\0']), "Expecting a NUL-terminated C string"); + unsafe { + gccjit_sys::gcc_jit_target_info_cpu_supports(self.ptr, feature.as_ptr() as *const _) != 0 + } + } + + pub fn arch(&self) -> Option<&'static CStr> { + unsafe { + let arch = gccjit_sys::gcc_jit_target_info_arch(self.ptr); + if arch.is_null() { + return None; + } + Some(CStr::from_ptr(arch)) + } + } + + pub fn supports_128bit_int(&self) -> bool { + unsafe { + gccjit_sys::gcc_jit_target_info_supports_128bit_int(self.ptr) != 0 + } + } +} + +impl Drop for TargetInfo { + fn drop(&mut self) { + unsafe { + gccjit_sys::gcc_jit_target_info_release(self.ptr); + } + } +} + +pub unsafe fn from_ptr(ptr: *mut gccjit_sys::gcc_jit_target_info) -> TargetInfo { + TargetInfo { + ptr, + } +} From 6c860c7cfe1e4bbec77d1f2b4b3252ecc4daea40 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 4 Jul 2023 21:10:12 -0400 Subject: [PATCH 069/127] Make TargetInfo::cpu_supports take a &str --- src/target_info.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/target_info.rs b/src/target_info.rs index 3dea155..1a43c74 100644 --- a/src/target_info.rs +++ b/src/target_info.rs @@ -1,4 +1,4 @@ -use std::{ffi::CStr, fmt}; +use std::{ffi::{CStr, CString}, fmt}; pub struct TargetInfo { ptr: *mut gccjit_sys::gcc_jit_target_info, @@ -14,10 +14,14 @@ impl fmt::Debug for TargetInfo { } impl TargetInfo { - pub fn cpu_supports(&self, feature: &[u8]) -> bool { - debug_assert!(feature.ends_with(&[b'\0']), "Expecting a NUL-terminated C string"); + pub fn cpu_supports(&self, feature: &str) -> bool { + let feature = + match CString::new(feature) { + Ok(feature) => feature, + Err(_) => return false, + }; unsafe { - gccjit_sys::gcc_jit_target_info_cpu_supports(self.ptr, feature.as_ptr() as *const _) != 0 + gccjit_sys::gcc_jit_target_info_cpu_supports(self.ptr, feature.as_ptr()) != 0 } } From 8e69cb6445327b0270effb7a367b77b92f634aaa Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 6 Jul 2023 16:53:46 +0200 Subject: [PATCH 070/127] Add support for GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE --- gccjit_sys/src/lib.rs | 1 + src/function.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index eb3aac2..0588c8a 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -264,6 +264,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_USED, GCC_JIT_FN_ATTRIBUTE_VISIBILITY, GCC_JIT_FN_ATTRIBUTE_COLD, + GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index 698f786..e03baf4 100644 --- a/src/function.rs +++ b/src/function.rs @@ -56,6 +56,7 @@ pub enum FnAttribute<'a> { Used, Visibility(Visibility), Cold, + ReturnsTwice, } #[cfg(feature="master")] @@ -68,7 +69,8 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Inline | FnAttribute::NoInline | FnAttribute::Used - | FnAttribute::Cold => AttributeValue::None, + | FnAttribute::Cold + | FnAttribute::ReturnsTwice => AttributeValue::None, } } @@ -81,6 +83,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::Used => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_USED, FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, FnAttribute::Cold => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_COLD, + FnAttribute::ReturnsTwice => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, } } } From fe8e19cb18e270dac38a89d41d76e9a680567600 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Jul 2023 21:41:52 +0200 Subject: [PATCH 071/127] Add support for GCC_JIT_FN_ATTRIBUTE_PURE --- gccjit_sys/src/lib.rs | 1 + src/function.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 0588c8a..fbd8f97 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -265,6 +265,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_VISIBILITY, GCC_JIT_FN_ATTRIBUTE_COLD, GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, + GCC_JIT_FN_ATTRIBUTE_PURE, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index e03baf4..7eb7a26 100644 --- a/src/function.rs +++ b/src/function.rs @@ -57,6 +57,7 @@ pub enum FnAttribute<'a> { Visibility(Visibility), Cold, ReturnsTwice, + Pure, } #[cfg(feature="master")] @@ -70,7 +71,8 @@ impl<'a> FnAttribute<'a> { | FnAttribute::NoInline | FnAttribute::Used | FnAttribute::Cold - | FnAttribute::ReturnsTwice => AttributeValue::None, + | FnAttribute::ReturnsTwice + | FnAttribute::Pure => AttributeValue::None, } } @@ -84,6 +86,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::Visibility(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_VISIBILITY, FnAttribute::Cold => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_COLD, FnAttribute::ReturnsTwice => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, + FnAttribute::Pure => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_PURE, } } } From 5f6a5d65227635d2f42fa637fd08d806281ed5e7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 21 Jul 2023 11:22:13 +0200 Subject: [PATCH 072/127] Add support for GCC_JIT_FN_ATTRIBUTE_CONST --- gccjit_sys/src/lib.rs | 1 + src/function.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index fbd8f97..4ddf498 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -266,6 +266,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_COLD, GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, GCC_JIT_FN_ATTRIBUTE_PURE, + GCC_JIT_FN_ATTRIBUTE_CONST, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index 7eb7a26..859bd2b 100644 --- a/src/function.rs +++ b/src/function.rs @@ -58,6 +58,7 @@ pub enum FnAttribute<'a> { Cold, ReturnsTwice, Pure, + Const, } #[cfg(feature="master")] @@ -72,7 +73,8 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Used | FnAttribute::Cold | FnAttribute::ReturnsTwice - | FnAttribute::Pure => AttributeValue::None, + | FnAttribute::Pure + | FnAttribute::Const => AttributeValue::None, } } @@ -87,6 +89,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::Cold => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_COLD, FnAttribute::ReturnsTwice => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, FnAttribute::Pure => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_PURE, + FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, } } } From 814eea1a0a098d08a113794225cad301622fd7b4 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 4 Aug 2023 07:54:14 -0400 Subject: [PATCH 073/127] Add support for alias and weak function attributes --- gccjit_sys/src/lib.rs | 2 ++ src/function.rs | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 0983707..624bb45 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -258,6 +258,7 @@ pub enum gcc_jit_comparison #[repr(C)] pub enum gcc_jit_fn_attribute { + GCC_JIT_FN_ATTRIBUTE_ALIAS, GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, GCC_JIT_FN_ATTRIBUTE_INLINE, GCC_JIT_FN_ATTRIBUTE_NOINLINE, @@ -268,6 +269,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, GCC_JIT_FN_ATTRIBUTE_PURE, GCC_JIT_FN_ATTRIBUTE_CONST, + GCC_JIT_FN_ATTRIBUTE_WEAK, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index 859bd2b..70cf88b 100644 --- a/src/function.rs +++ b/src/function.rs @@ -49,6 +49,7 @@ pub enum FunctionType { #[cfg(feature="master")] #[derive(Clone, Debug)] pub enum FnAttribute<'a> { + Alias(&'a str), AlwaysInline, Inline, NoInline, @@ -59,13 +60,14 @@ pub enum FnAttribute<'a> { ReturnsTwice, Pure, Const, + Weak, } #[cfg(feature="master")] impl<'a> FnAttribute<'a> { fn get_value(&self) -> AttributeValue { match *self { - FnAttribute::Target(target) => AttributeValue::String(target), + FnAttribute::Alias(value) | FnAttribute::Target(value) => AttributeValue::String(value), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), FnAttribute::AlwaysInline | FnAttribute::Inline @@ -74,12 +76,14 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Cold | FnAttribute::ReturnsTwice | FnAttribute::Pure - | FnAttribute::Const => AttributeValue::None, + | FnAttribute::Const + | FnAttribute::Weak => AttributeValue::None, } } fn as_sys(&self) -> gccjit_sys::gcc_jit_fn_attribute { match *self { + FnAttribute::Alias(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALIAS, FnAttribute::AlwaysInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE, FnAttribute::Inline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_INLINE, FnAttribute::NoInline => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NOINLINE, @@ -90,6 +94,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::ReturnsTwice => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_RETURNS_TWICE, FnAttribute::Pure => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_PURE, FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, + FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK, } } } From ac8bb23bcba520c56d15ff94c711792e28f14bec Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 12 Aug 2023 15:51:13 +0200 Subject: [PATCH 074/127] Add `gcc_jit_type_get_restrict` --- gccjit_sys/src/lib.rs | 2 ++ src/types.rs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 4ddf498..0f6c9d6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -336,6 +336,8 @@ extern { pub fn gcc_jit_type_get_pointer(ty: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_get_const(ty: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_get_volatile(ty: *mut gcc_jit_type) -> *mut gcc_jit_type; + #[cfg(feature="master")] + pub fn gcc_jit_type_get_restrict(ty: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_context_new_array_type(ctx: *mut gcc_jit_context, loc: *mut gcc_jit_location, ty: *mut gcc_jit_type, diff --git a/src/types.rs b/src/types.rs index 4004461..bc69089 100644 --- a/src/types.rs +++ b/src/types.rs @@ -137,6 +137,15 @@ impl<'ctx> Type<'ctx> { } } + /// Given a type T, creates a new type of restrict T, which + /// has the semantics of C's restrict. + #[cfg(feature="master")] + pub fn make_restrict(self) -> Type<'ctx> { + unsafe { + from_ptr(gccjit_sys::gcc_jit_type_get_restrict(self.ptr)) + } + } + pub fn get_aligned(self, alignment_in_bytes: u64) -> Type<'ctx> { unsafe { from_ptr(gccjit_sys::gcc_jit_type_get_aligned(self.ptr, alignment_in_bytes as _)) From 034a8c4f5711865a9066c13ad32cc6a89cfbc705 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 19 Sep 2023 22:12:21 -0400 Subject: [PATCH 075/127] Implement sizeof --- gccjit_sys/src/lib.rs | 3 +++ src/context.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index f8808f6..5d82778 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -648,4 +648,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_target_info_supports_128bit_int(info: *mut gcc_jit_target_info) -> c_int; + + #[cfg(feature="master")] + pub fn gcc_jit_context_new_sizeof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; } diff --git a/src/context.rs b/src/context.rs index 96615e2..abc8a17 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1032,6 +1032,20 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + /// Creates a new RValue from a sizeof(type). + pub fn new_sizeof<'a>(&'a self, ty: types::Type<'a>) -> RValue<'a> { + + unsafe { + let ptr = gccjit_sys::gcc_jit_context_new_sizeof(self.ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Dumps a small C file to the path that can be used to reproduce a series /// of API calls. You should only ever need to call this if you are debugging /// an issue in gccjit itself or this library. From 15cf0bf83c211fe393e74cbc4405d7582b6cf78d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 4 Sep 2023 20:28:49 +0200 Subject: [PATCH 076/127] Add support for IntArray and for NonNull function attribute --- gccjit_sys/src/lib.rs | 4 ++++ src/function.rs | 27 ++++++++++++++++++++++++++- src/lvalue.rs | 4 +++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 5d82778..9302600 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -270,6 +270,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_PURE, GCC_JIT_FN_ATTRIBUTE_CONST, GCC_JIT_FN_ATTRIBUTE_WEAK, + GCC_JIT_FN_ATTRIBUTE_NONNULL, } #[cfg(feature="master")] @@ -619,6 +620,9 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_function_add_string_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_char); + #[cfg(feature="master")] + pub fn gcc_jit_function_add_integer_array_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_int, length: size_t); + #[cfg(feature="master")] pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); diff --git a/src/function.rs b/src/function.rs index 70cf88b..c5fbdb1 100644 --- a/src/function.rs +++ b/src/function.rs @@ -61,6 +61,7 @@ pub enum FnAttribute<'a> { Pure, Const, Weak, + NonNull(Vec), } #[cfg(feature="master")] @@ -78,6 +79,7 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Pure | FnAttribute::Const | FnAttribute::Weak => AttributeValue::None, + FnAttribute::NonNull(ref value) => AttributeValue::IntArray(value), } } @@ -95,6 +97,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::Pure => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_PURE, FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK, + FnAttribute::NonNull(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NONNULL, } } } @@ -208,7 +211,29 @@ impl<'ctx> Function<'ctx> { pub fn add_attribute<'a>(&self, attribute: FnAttribute<'a>) { let value = attribute.get_value(); match value { - AttributeValue::Int(_) => unimplemented!(), + AttributeValue::Int(value) => { + // Basically the same as `IntArray` but for only one element. + let value = &[value]; + unsafe { + gccjit_sys::gcc_jit_function_add_integer_array_attribute( + self.ptr, + attribute.as_sys(), + value.as_ptr(), + value.len() as _, + ); + } + + } + AttributeValue::IntArray(value) => { + unsafe { + gccjit_sys::gcc_jit_function_add_integer_array_attribute( + self.ptr, + attribute.as_sys(), + value.as_ptr(), + value.len() as _, + ); + } + } AttributeValue::None => { unsafe { gccjit_sys::gcc_jit_function_add_attribute(self.ptr, attribute.as_sys()); diff --git a/src/lvalue.rs b/src/lvalue.rs index fc038da..87e63df 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -39,6 +39,7 @@ pub enum AttributeValue<'a> { Int(i32), None, String(&'a str), + IntArray(&'a [i32]), } #[cfg(feature="master")] @@ -51,7 +52,7 @@ pub enum VarAttribute { impl VarAttribute { fn get_value(&self) -> AttributeValue { match *self { - VarAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), + Self::Visibility(visibility) => AttributeValue::String(visibility.as_str()), } } @@ -221,6 +222,7 @@ impl<'ctx> LValue<'ctx> { let value = attribute.get_value(); match value { AttributeValue::Int(_) => unimplemented!(), + AttributeValue::IntArray(_) => unimplemented!(), AttributeValue::None => unimplemented!(), AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); From d8e59cb2c2fb947dcd9d0a763d50623d336ddebc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Oct 2023 00:18:27 +0200 Subject: [PATCH 077/127] Use `c_int` instead of `i32` --- gccjit_sys/src/lib.rs | 3 ++- src/function.rs | 3 ++- src/lvalue.rs | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 9302600..b639a96 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -1,6 +1,7 @@ #![allow(non_camel_case_types)] -extern crate libc; +// Re-export libc to use the same version in `gccjit-rs`. +pub extern crate libc; use libc::{c_char, c_int, FILE, c_void, c_long, c_double, c_ulong, size_t, ssize_t}; diff --git a/src/function.rs b/src/function.rs index c5fbdb1..e8fd703 100644 --- a/src/function.rs +++ b/src/function.rs @@ -3,6 +3,7 @@ use std::fmt; use std::ptr; use gccjit_sys; +use gccjit_sys::libc::c_int; use block::Block; use block; @@ -61,7 +62,7 @@ pub enum FnAttribute<'a> { Pure, Const, Weak, - NonNull(Vec), + NonNull(Vec), } #[cfg(feature="master")] diff --git a/src/lvalue.rs b/src/lvalue.rs index 87e63df..6f14ab8 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -12,6 +12,8 @@ use field; use location::Location; use location; +use gccjit_sys::libc::c_int; + #[cfg(feature="master")] #[derive(Clone, Copy, Debug)] pub enum Visibility { @@ -39,7 +41,7 @@ pub enum AttributeValue<'a> { Int(i32), None, String(&'a str), - IntArray(&'a [i32]), + IntArray(&'a [c_int]), } #[cfg(feature="master")] From 387d12b5945e9668c17268b6b97463d5d7ff6fbc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 11 Oct 2023 00:20:41 +0200 Subject: [PATCH 078/127] Ensure that all values in `FnAttribute::NonNull` are positive --- gccjit_sys/src/lib.rs | 3 +-- src/function.rs | 11 ++++++++--- src/lvalue.rs | 4 +--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index b639a96..9302600 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -1,7 +1,6 @@ #![allow(non_camel_case_types)] -// Re-export libc to use the same version in `gccjit-rs`. -pub extern crate libc; +extern crate libc; use libc::{c_char, c_int, FILE, c_void, c_long, c_double, c_ulong, size_t, ssize_t}; diff --git a/src/function.rs b/src/function.rs index e8fd703..87fd061 100644 --- a/src/function.rs +++ b/src/function.rs @@ -3,7 +3,6 @@ use std::fmt; use std::ptr; use gccjit_sys; -use gccjit_sys::libc::c_int; use block::Block; use block; @@ -62,7 +61,7 @@ pub enum FnAttribute<'a> { Pure, Const, Weak, - NonNull(Vec), + NonNull(Vec), } #[cfg(feature="master")] @@ -80,7 +79,13 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Pure | FnAttribute::Const | FnAttribute::Weak => AttributeValue::None, - FnAttribute::NonNull(ref value) => AttributeValue::IntArray(value), + FnAttribute::NonNull(ref value) => { + debug_assert!( + value.iter().all(|attr| *attr > 0), + "all values must be > 0 for non-null attribute", + ); + AttributeValue::IntArray(value) + } } } diff --git a/src/lvalue.rs b/src/lvalue.rs index 6f14ab8..b3f3107 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -12,8 +12,6 @@ use field; use location::Location; use location; -use gccjit_sys::libc::c_int; - #[cfg(feature="master")] #[derive(Clone, Copy, Debug)] pub enum Visibility { @@ -41,7 +39,7 @@ pub enum AttributeValue<'a> { Int(i32), None, String(&'a str), - IntArray(&'a [c_int]), + IntArray(&'a [std::ffi::c_int]), } #[cfg(feature="master")] From d4b71ef7acacbd0ba0618c91e7d626e13edfe1a3 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 27 Oct 2023 19:10:19 -0400 Subject: [PATCH 079/127] Add support for setting the output ident --- gccjit_sys/src/lib.rs | 3 +++ src/context.rs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 9302600..d2a4e22 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -655,4 +655,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_context_new_sizeof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; + + #[cfg(feature="master")] + pub fn gcc_jit_context_set_output_ident(ctxt: *mut gcc_jit_context, output_ident: *const c_char); } diff --git a/src/context.rs b/src/context.rs index abc8a17..c38a17d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -194,6 +194,14 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + pub fn set_output_ident(&self, ident: &str) { + let c_str = CString::new(ident).unwrap(); + unsafe { + gccjit_sys::gcc_jit_context_set_output_ident(self.ptr, c_str.as_ptr()); + } + } + pub fn set_debug_info(&self, value: bool) { unsafe { gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, From 8b5d8a7778dafcbee9db5c312d6204793537431b Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 27 Oct 2023 19:21:01 -0400 Subject: [PATCH 080/127] Add function to get the version of libgccjit --- gccjit_sys/src/lib.rs | 4 ++++ src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index d2a4e22..a292c68 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -658,4 +658,8 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_context_set_output_ident(ctxt: *mut gcc_jit_context, output_ident: *const c_char); + + pub fn gcc_jit_version_major() -> c_int; + pub fn gcc_jit_version_minor() -> c_int; + pub fn gcc_jit_version_patchlevel() -> c_int; } diff --git a/src/lib.rs b/src/lib.rs index 9e22570..b97f760 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,3 +66,22 @@ pub fn set_global_personality_function_name(name: &'static [u8]) { gccjit_sys::gcc_jit_set_global_personality_function_name(name.as_ptr() as *const _); } } + +#[derive(Debug)] +pub struct Version { + pub major: i32, + pub minor: i32, + pub patch: i32, +} + +impl Version { + pub fn get() -> Self { + unsafe { + Self { + major: gccjit_sys::gcc_jit_version_major(), + minor: gccjit_sys::gcc_jit_version_minor(), + patch: gccjit_sys::gcc_jit_version_patchlevel(), + } + } + } +} From 1305cdac090a04f7e8b999a89298e00fb379bd16 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 20 Jan 2024 20:10:16 -0500 Subject: [PATCH 081/127] Add new_temp --- gccjit_sys/src/lib.rs | 3 +++ src/function.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index a292c68..a0c070d 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -662,4 +662,7 @@ extern { pub fn gcc_jit_version_major() -> c_int; pub fn gcc_jit_version_minor() -> c_int; pub fn gcc_jit_version_patchlevel() -> c_int; + + #[cfg(feature="master")] + pub fn gcc_jit_function_new_temp(func: *mut gcc_jit_function, loc: *mut gcc_jit_location, ty: *mut gcc_jit_type) -> *mut gcc_jit_lvalue; } diff --git a/src/function.rs b/src/function.rs index 87fd061..5b7e1fb 100644 --- a/src/function.rs +++ b/src/function.rs @@ -213,6 +213,22 @@ impl<'ctx> Function<'ctx> { } } + #[cfg(feature="master")] + pub fn new_temp(&self, loc: Option>, ty: Type<'ctx>) -> LValue<'ctx> { + unsafe { + let loc_ptr = match loc { + Some(loc) => location::get_ptr(&loc), + None => ptr::null_mut() + }; + let ptr = gccjit_sys::gcc_jit_function_new_temp(self.ptr, loc_ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{} ({:?})", error, self); + } + lvalue::from_ptr(ptr) + } + } + #[cfg(feature="master")] pub fn add_attribute<'a>(&self, attribute: FnAttribute<'a>) { let value = attribute.get_value(); From c7f5b25b4f5eeb04946f7428dc41e0eaac1caddd Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 25 Jan 2024 21:15:31 -0400 Subject: [PATCH 082/127] Update for rebased gcc --- gccjit_sys/src/lib.rs | 2 +- src/lvalue.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index a0c070d..86b62dc 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -624,7 +624,7 @@ extern { pub fn gcc_jit_function_add_integer_array_attribute(func: *mut gcc_jit_function, attribute: gcc_jit_fn_attribute, value: *const c_int, length: size_t); #[cfg(feature="master")] - pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); + pub fn gcc_jit_lvalue_add_string_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute, value: *const c_char); #[cfg(feature="master")] pub fn gcc_jit_block_add_try_catch(block: *mut gcc_jit_block, loc: *mut gcc_jit_location, try_block: *mut gcc_jit_block, catch_block: *mut gcc_jit_block); diff --git a/src/lvalue.rs b/src/lvalue.rs index b3f3107..f886d0a 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -218,7 +218,7 @@ impl<'ctx> LValue<'ctx> { } #[cfg(feature="master")] - pub fn add_attribute(&self, attribute: VarAttribute) { + pub fn add_string_attribute(&self, attribute: VarAttribute) { let value = attribute.get_value(); match value { AttributeValue::Int(_) => unimplemented!(), @@ -227,7 +227,7 @@ impl<'ctx> LValue<'ctx> { AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); unsafe { - gccjit_sys::gcc_jit_lvalue_add_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); + gccjit_sys::gcc_jit_lvalue_add_string_attribute(self.ptr, attribute.to_sys(), cstr.as_ptr()); } }, } From 4b7aba76891e6436984f7f098fe92824d95194d5 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 15 Feb 2024 17:14:58 -0500 Subject: [PATCH 083/127] Add set_allow_special_chars_in_func_names --- gccjit_sys/src/lib.rs | 1 + src/block.rs | 4 ++++ src/context.rs | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 86b62dc..2982d45 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -54,6 +54,7 @@ pub enum gcc_jit_bool_option { GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, GCC_JIT_BOOL_OPTION_SELFCHECK_GC, GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, + GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, GCC_JIT_NUM_BOOL_OPTIONS } diff --git a/src/block.rs b/src/block.rs index b43c6a6..11f473d 100644 --- a/src/block.rs +++ b/src/block.rs @@ -293,6 +293,10 @@ impl<'ctx> Block<'ctx> { gccjit_sys::gcc_jit_block_end_with_switch(self.ptr, loc_ptr, rvalue::get_ptr(&expr), block::get_ptr(&default_block), cases.len() as c_int, cases.as_ptr() as *mut *mut _); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } pub fn add_extended_asm(&self, loc: Option>, asm_template: &str) -> ExtendedAsm<'ctx> { diff --git a/src/context.rs b/src/context.rs index c38a17d..7f44786 100644 --- a/src/context.rs +++ b/src/context.rs @@ -202,6 +202,13 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + pub fn set_allow_special_chars_in_func_names(&self, value: bool) { + unsafe { + gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, value as i32); + } + } + pub fn set_debug_info(&self, value: bool) { unsafe { gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, From bb4a5c0195acf79e3aa736cedf36846d2c05148f Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 20 Feb 2024 17:03:11 -0500 Subject: [PATCH 084/127] Fix some new warnings --- Cargo.toml | 2 +- src/block.rs | 5 ++++- src/context.rs | 3 --- src/field.rs | 2 -- src/function.rs | 2 -- src/location.rs | 1 - src/lvalue.rs | 1 - src/object.rs | 1 - src/parameter.rs | 1 - src/rvalue.rs | 1 - src/structs.rs | 2 -- src/types.rs | 2 -- 12 files changed, 5 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 63dd4ca..4eb0d4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "gccjit" version = "1.0.0" -authors = ["Sean Gillespie "] +authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] license = "GPL-3.0" diff --git a/src/block.rs b/src/block.rs index 11f473d..c651085 100644 --- a/src/block.rs +++ b/src/block.rs @@ -8,7 +8,6 @@ use std::os::raw::c_int; use asm::ExtendedAsm; use block; use context::{Case, Context}; -use gccjit_sys; use object::{self, ToObject, Object}; use function::{self, Function}; use location::{self, Location}; @@ -281,6 +280,10 @@ impl<'ctx> Block<'ctx> { gccjit_sys::gcc_jit_block_end_with_void_return(self.ptr, loc_ptr); } + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{}", error); + } } pub fn end_with_switch>(&self, loc: Option>, expr: T, default_block: Block<'ctx>, cases: &[Case<'ctx>]) { diff --git a/src/context.rs b/src/context.rs index 7f44786..ce8f078 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,5 +1,3 @@ -use std::default::Default; -use std::ops::Drop; use std::ffi::{CStr, CString}; use std::marker::PhantomData; use std::mem; @@ -7,7 +5,6 @@ use std::os::raw::{c_int, c_ulong}; use std::ptr; use std::str::Utf8Error; -use gccjit_sys; use gccjit_sys::gcc_jit_int_option::*; use gccjit_sys::gcc_jit_str_option::*; use gccjit_sys::gcc_jit_bool_option::*; diff --git a/src/field.rs b/src/field.rs index d7aaa9d..5ac2c16 100644 --- a/src/field.rs +++ b/src/field.rs @@ -1,5 +1,3 @@ -use gccjit_sys; - use std::marker::PhantomData; use std::fmt; diff --git a/src/function.rs b/src/function.rs index 5b7e1fb..83aee46 100644 --- a/src/function.rs +++ b/src/function.rs @@ -2,8 +2,6 @@ use std::marker::PhantomData; use std::fmt; use std::ptr; -use gccjit_sys; - use block::Block; use block; use context::Context; diff --git a/src/location.rs b/src/location.rs index 3a6c0f2..9677d7f 100644 --- a/src/location.rs +++ b/src/location.rs @@ -1,4 +1,3 @@ -use gccjit_sys; use std::marker::PhantomData; use std::fmt; use context::Context; diff --git a/src/lvalue.rs b/src/lvalue.rs index f886d0a..8749918 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -1,7 +1,6 @@ use std::{ffi::CString, marker::PhantomData}; use std::fmt; use std::ptr; -use gccjit_sys; use context::Context; use rvalue::{RValue, ToRValue}; use rvalue; diff --git a/src/object.rs b/src/object.rs index 56f3e84..5190157 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,4 +1,3 @@ -use gccjit_sys; use context::Context; use std::marker::PhantomData; use std::fmt; diff --git a/src/parameter.rs b/src/parameter.rs index b93132e..f5e1bf4 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -1,6 +1,5 @@ use std::marker::PhantomData; use std::fmt; -use gccjit_sys; use context::Context; use object::{ToObject, Object}; use object; diff --git a/src/rvalue.rs b/src/rvalue.rs index f3da005..6731233 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -3,7 +3,6 @@ use std::fmt; use std::ptr; use std::mem; use std::ops::{Add, Sub, Mul, Div, Rem, BitAnd, BitOr, BitXor, Shl, Shr}; -use gccjit_sys; use context::Context; use object::{ToObject, Object}; use object; diff --git a/src/structs.rs b/src/structs.rs index efb2efb..16981b3 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,5 +1,3 @@ -use gccjit_sys; - use std::marker::PhantomData; use std::fmt; use std::ptr; diff --git a/src/types.rs b/src/types.rs index bc69089..5039953 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,8 +1,6 @@ use std::marker::PhantomData; use std::fmt; -use gccjit_sys; - use context::Context; use context; use object; From 9a7fd173bf5ff8eda6096aca7d779a815156c32c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 23 Feb 2024 12:03:46 -0500 Subject: [PATCH 085/127] Fix CI --- .github/workflows/ci.yml | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b1e9090..dfc0521 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,27 +22,13 @@ jobs: override: true components: clippy - - name: Download artifact - uses: dawidd6/action-download-artifact@v2 - with: - workflow: main.yml - name: "libgccjit.so" - path: gcc-build - repo: antoyo/gcc - branch: "master" - event: push - search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts. - - - name: Setup path to libgccjit + - name: Download and install GCC fork run: | - echo $(readlink -f gcc-build) > gcc_path - # NOTE: the filename is still libgccjit.so even when the artifact name is different. - ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0 + curl -LO https://github.com/antoyo/gcc/releases/latest/download/gcc-13.deb + sudo dpkg --force-overwrite -i gcc-13.deb - name: Set env run: | - echo "LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV - echo "LD_LIBRARY_PATH=$(cat gcc_path)" >> $GITHUB_ENV echo "workspace="$GITHUB_WORKSPACE >> $GITHUB_ENV - name: Cache cargo registry @@ -69,6 +55,11 @@ jobs: cargo build --features master cargo build --examples + - name: Test + run: | + cd examples/factorial + cargo build + - name: clippy run: | cargo clippy --all-targets -- -D warnings From 08e404d8736eb282adc5654b4e959bb8d8911911 Mon Sep 17 00:00:00 2001 From: tempdragon <645703113@qq.com> Date: Sun, 18 Feb 2024 10:58:16 +0800 Subject: [PATCH 086/127] feat(rvalue): Add `set_location()` method to allow location updates --- gccjit_sys/src/lib.rs | 4 ++++ src/rvalue.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 2982d45..09aa8c2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -666,4 +666,8 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_function_new_temp(func: *mut gcc_jit_function, loc: *mut gcc_jit_location, ty: *mut gcc_jit_type) -> *mut gcc_jit_lvalue; + + #[cfg(feature="master")] + pub fn gcc_jit_rvalue_set_location(rvalue: *mut gcc_jit_rvalue, + loc: *mut gcc_jit_location); } diff --git a/src/rvalue.rs b/src/rvalue.rs index 6731233..558d7c2 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -101,6 +101,15 @@ impl<'ctx> RValue<'ctx> { } } + /// Sets the location of this RValue. + #[cfg(feature="master")] + pub unsafe fn set_location(&self, loc: Location) { + unsafe { + let loc_ptr = location::get_ptr(&loc); + gccjit_sys::gcc_jit_rvalue_set_location(self.ptr, loc_ptr); + } + } + /// Given an RValue x and a Field f, returns an RValue representing /// C's x.f. pub fn access_field(&self, From a4d72e5b7af45111ae089ce7ed7a9cddf9684c4b Mon Sep 17 00:00:00 2001 From: tempdragon <645703113@qq.com> Date: Thu, 22 Feb 2024 20:33:15 +0800 Subject: [PATCH 087/127] feat(Location): Add null ptr creator --- src/location.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/location.rs b/src/location.rs index 9677d7f..4b9476f 100644 --- a/src/location.rs +++ b/src/location.rs @@ -26,6 +26,15 @@ impl<'ctx> fmt::Debug for Location<'ctx> { } } +impl<'ctx> Location<'ctx> { + pub fn null<'a>() -> Self { + Location { + marker: std::marker::PhantomData, + ptr: unsafe { core::ptr::null_mut() }, + } + } +} + pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_location) -> Location<'ctx> { Location { marker: PhantomData, From a33fb5c9ca08b1a54ca04d5e17137993e5b55683 Mon Sep 17 00:00:00 2001 From: tempdragon <645703113@qq.com> Date: Wed, 28 Feb 2024 08:44:08 +0800 Subject: [PATCH 088/127] fix(location&context): Fix clippy errors --- src/context.rs | 1 - src/location.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/context.rs b/src/context.rs index ce8f078..61be21d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1215,7 +1215,6 @@ pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_context) -> Context<' #[cfg(test)] mod tests { use super::super::*; - use std::default::Default; use std::mem; #[test] diff --git a/src/location.rs b/src/location.rs index 4b9476f..1356062 100644 --- a/src/location.rs +++ b/src/location.rs @@ -27,10 +27,10 @@ impl<'ctx> fmt::Debug for Location<'ctx> { } impl<'ctx> Location<'ctx> { - pub fn null<'a>() -> Self { + pub fn null() -> Self { Location { marker: std::marker::PhantomData, - ptr: unsafe { core::ptr::null_mut() }, + ptr: core::ptr::null_mut(), } } } From 96fb948d2be8e6a67848c83772ccf3acf4a1d5fc Mon Sep 17 00:00:00 2001 From: tempdragon <645703113@qq.com> Date: Wed, 28 Feb 2024 08:55:04 +0800 Subject: [PATCH 089/127] fix(clippy): Remove unsafe marker in RValue::set_location --- src/rvalue.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rvalue.rs b/src/rvalue.rs index 558d7c2..6e15d22 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -103,7 +103,7 @@ impl<'ctx> RValue<'ctx> { /// Sets the location of this RValue. #[cfg(feature="master")] - pub unsafe fn set_location(&self, loc: Location) { + pub fn set_location(&self, loc: Location) { unsafe { let loc_ptr = location::get_ptr(&loc); gccjit_sys::gcc_jit_rvalue_set_location(self.ptr, loc_ptr); From 9f8f67edc006d543b17529a001803ffece48349e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 1 Mar 2024 12:19:58 -0500 Subject: [PATCH 090/127] Switch from set_allow_special_chars_in_func_names to set_special_chars_allowed_in_func_names --- gccjit_sys/src/lib.rs | 4 ++-- src/context.rs | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 09aa8c2..9c81070 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -35,7 +35,8 @@ pub enum gcc_jit_tls_model { #[repr(C)] pub enum gcc_jit_str_option { GCC_JIT_STR_OPTION_PROGNAME, - GCC_JIT_NUM_STR_OPTIONS + GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, + GCC_JIT_NUM_STR_OPTIONS, } #[repr(C)] @@ -54,7 +55,6 @@ pub enum gcc_jit_bool_option { GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING, GCC_JIT_BOOL_OPTION_SELFCHECK_GC, GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES, - GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, GCC_JIT_NUM_BOOL_OPTIONS } diff --git a/src/context.rs b/src/context.rs index 61be21d..b12e379 100644 --- a/src/context.rs +++ b/src/context.rs @@ -200,9 +200,10 @@ impl<'ctx> Context<'ctx> { } #[cfg(feature="master")] - pub fn set_allow_special_chars_in_func_names(&self, value: bool) { + pub fn set_special_chars_allowed_in_func_names(&self, value: &str) { + let c_str = CString::new(value).unwrap(); unsafe { - gccjit_sys::gcc_jit_context_set_bool_option(self.ptr, GCC_JIT_BOOL_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, value as i32); + gccjit_sys::gcc_jit_context_set_str_option(self.ptr, GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, c_str.as_ptr()); } } From 0d9271a2917c4157aa2f6777cfb3130b539959dd Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 11 Mar 2024 10:22:19 -0400 Subject: [PATCH 091/127] Update versions to publish --- Cargo.toml | 8 ++++---- gccjit_sys/Cargo.toml | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4eb0d4a..90de564 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,16 @@ [package] name = "gccjit" -version = "1.0.0" +version = "2.0.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] license = "GPL-3.0" -repository = "https://github.com/swgillespie/gccjit.rs" -documentation = "http://swgillespie.github.io/gccjit.rs/gccjit/" +repository = "https://github.com/antoyo/gccjit.rs" +documentation = "https://docs.rs/gccjit/latest/gccjit/" readme = "README.md" [features] master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.0.1", path = "gccjit_sys" } +gccjit_sys = { version = "0.1.0", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 900cd53..0ef6014 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "gccjit_sys" -version = "0.0.1" -authors = ["Sean Gillespie "] +version = "0.1.0" +authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." keywords = ["compiler", "jit", "gcc"] license = "GPL-3.0" -repository = "https://github.com/swgillespie/gccjit.rs" +repository = "https://github.com/antoyo/gccjit.rs" [features] master = [] From 55b63b4089a8f3046e7072f4b13f5873c6cf27f6 Mon Sep 17 00:00:00 2001 From: Mubarak Muhammad Aminu Date: Thu, 21 Mar 2024 17:52:17 +0100 Subject: [PATCH 092/127] add #[must_use] new_call function to show warning (#30) --- examples/brainfuck/src/main.rs | 1 + src/context.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/brainfuck/src/main.rs b/examples/brainfuck/src/main.rs index 6a47a13..c4c2b60 100644 --- a/examples/brainfuck/src/main.rs +++ b/examples/brainfuck/src/main.rs @@ -107,6 +107,7 @@ fn codegen<'a, 'ctx>(ops: &[Op], context: &'a gccjit::Context<'ctx>) -> bool { let mut current_block = brainf_main.new_block("entry_block"); // now we have to zero out the giant buffer we just allocated on the stack. let zero_access = context.new_array_access(None, array.to_rvalue(), context.new_rvalue_zero(int_ty)); + // A function call that is done for its side effects must be sent to add_eval. current_block.add_eval(None, context.new_call(None, memset, &[zero_access.get_address(None), context.new_rvalue_zero(int_ty), size])); let mut block_stack = vec![]; let mut blocks = 0; diff --git a/src/context.rs b/src/context.rs index b12e379..d028d8d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -690,6 +690,7 @@ impl<'ctx> Context<'ctx> { /// together with LValues and Parameters, for example), so in order to /// mix the types of the arguments it may be necessary to call to_rvalue() /// before calling this function. + #[must_use] pub fn new_call<'a>(&'a self, loc: Option>, func: Function<'a>, From 1970d2127d092c076544dd2ed4f05dfa53be2142 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 4 Apr 2024 19:20:17 -0400 Subject: [PATCH 093/127] Add new_alignof method --- gccjit_sys/src/lib.rs | 3 +++ src/context.rs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 9c81070..0111082 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -670,4 +670,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_rvalue_set_location(rvalue: *mut gcc_jit_rvalue, loc: *mut gcc_jit_location); + + #[cfg(feature="master")] + pub fn gcc_jit_context_new_alignof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; } diff --git a/src/context.rs b/src/context.rs index b12e379..61ee811 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1048,7 +1048,6 @@ impl<'ctx> Context<'ctx> { #[cfg(feature="master")] /// Creates a new RValue from a sizeof(type). pub fn new_sizeof<'a>(&'a self, ty: types::Type<'a>) -> RValue<'a> { - unsafe { let ptr = gccjit_sys::gcc_jit_context_new_sizeof(self.ptr, types::get_ptr(&ty)); #[cfg(debug_assertions)] @@ -1059,6 +1058,19 @@ impl<'ctx> Context<'ctx> { } } + #[cfg(feature="master")] + /// Creates a new RValue from a _Alignof(type). + pub fn new_alignof<'a>(&'a self, ty: types::Type<'a>) -> RValue<'a> { + unsafe { + let ptr = gccjit_sys::gcc_jit_context_new_alignof(self.ptr, types::get_ptr(&ty)); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.get_last_error() { + panic!("{}", error); + } + rvalue::from_ptr(ptr) + } + } + /// Dumps a small C file to the path that can be used to reproduce a series /// of API calls. You should only ever need to call this if you are debugging /// an issue in gccjit itself or this library. From 9c3df282d78a4aca0848d433d4d401af544857e4 Mon Sep 17 00:00:00 2001 From: Robert Zakrzewski Date: Tue, 9 Apr 2024 21:50:23 +0200 Subject: [PATCH 094/127] Add Float16, Float32, Float64 and Float128 --- gccjit_sys/src/lib.rs | 4 ++++ src/context.rs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 9c81070..e68142a 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -120,6 +120,10 @@ pub enum gcc_jit_types { GCC_JIT_TYPE_INT128_T, GCC_JIT_TYPE_BFLOAT16, + GCC_JIT_TYPE_FLOAT16, + GCC_JIT_TYPE_FLOAT32, + GCC_JIT_TYPE_FLOAT64, + GCC_JIT_TYPE_FLOAT128, } #[repr(C)] diff --git a/src/context.rs b/src/context.rs index d028d8d..af0277a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1320,6 +1320,10 @@ pub enum CType { UInt128t, ConstCharPtr, BFloat16, + Float16, + Float32, + Float64, + Float128, } impl CType { @@ -1353,6 +1357,10 @@ impl CType { UInt128t => GCC_JIT_TYPE_UINT128_T, ConstCharPtr => GCC_JIT_TYPE_CONST_CHAR_PTR, BFloat16 => GCC_JIT_TYPE_BFLOAT16, + Float16 => GCC_JIT_TYPE_FLOAT16, + Float32 => GCC_JIT_TYPE_FLOAT32, + Float64 => GCC_JIT_TYPE_FLOAT64, + Float128 => GCC_JIT_TYPE_FLOAT128, } } } From 82e65eb9c941cad48812afb3b63704b42baa2060 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 19 Apr 2024 20:55:43 -0400 Subject: [PATCH 095/127] Add debug check --- src/function.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/function.rs b/src/function.rs index 83aee46..3f01769 100644 --- a/src/function.rs +++ b/src/function.rs @@ -135,6 +135,10 @@ impl<'ctx> Function<'ctx> { pub fn get_param(&self, idx: i32) -> Parameter<'ctx> { unsafe { let ptr = gccjit_sys::gcc_jit_function_get_param(self.ptr, idx); + #[cfg(debug_assertions)] + if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { + panic!("{} ({:?})", error, self); + } parameter::from_ptr(ptr) } } From ed8045cb0f9ff70c799f2cac02f9bef0b4bc1a9f Mon Sep 17 00:00:00 2001 From: Robert Zakrzewski Date: Sat, 20 Apr 2024 20:47:49 +0200 Subject: [PATCH 096/127] Target info with function to check if target dependent type is supported Target with function to check if target dependent type is supported --- gccjit_sys/src/lib.rs | 3 +++ src/context.rs | 2 +- src/target_info.rs | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 121bbbb..c2db8d2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -658,6 +658,9 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_target_info_supports_128bit_int(info: *mut gcc_jit_target_info) -> c_int; + #[cfg(feature="master")] + pub fn gcc_jit_target_info_supports_target_dependent_type(info: *mut gcc_jit_target_info, ty: gcc_jit_types) -> c_int; + #[cfg(feature="master")] pub fn gcc_jit_context_new_sizeof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; diff --git a/src/context.rs b/src/context.rs index 3fb3bb5..3c0c8d5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1339,7 +1339,7 @@ pub enum CType { } impl CType { - fn to_sys(self) -> gccjit_sys::gcc_jit_types { + pub(crate) fn to_sys(self) -> gccjit_sys::gcc_jit_types { use gccjit_sys::gcc_jit_types::*; use self::CType::*; diff --git a/src/target_info.rs b/src/target_info.rs index 1a43c74..d29b807 100644 --- a/src/target_info.rs +++ b/src/target_info.rs @@ -1,3 +1,4 @@ +use context::CType; use std::{ffi::{CStr, CString}, fmt}; pub struct TargetInfo { @@ -40,6 +41,13 @@ impl TargetInfo { gccjit_sys::gcc_jit_target_info_supports_128bit_int(self.ptr) != 0 } } + + #[cfg(feature="master")] + pub fn supports_target_dependent_type(&self, c_type: CType) -> bool { + unsafe { + gccjit_sys::gcc_jit_target_info_supports_target_dependent_type(self.ptr, c_type.to_sys()) != 0 + } + } } impl Drop for TargetInfo { From 4f032695bc5d099216fa9186a40bb2e45ec49084 Mon Sep 17 00:00:00 2001 From: Robert Zakrzewski Date: Mon, 22 Apr 2024 22:40:37 +0200 Subject: [PATCH 097/127] add Type::is_floating_point() function Rename is_float to is_floating_point --- gccjit_sys/src/lib.rs | 1 + src/types.rs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index c2db8d2..d12570d 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -553,6 +553,7 @@ extern { pub fn gcc_jit_type_dyncast_array(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_integral(typ: *mut gcc_jit_type) -> c_int; + pub fn gcc_jit_type_is_floating_point(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_pointer(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_dyncast_function_ptr_type(typ: *mut gcc_jit_type) -> *mut gcc_jit_function_type; diff --git a/src/types.rs b/src/types.rs index 5039953..c0f1175 100644 --- a/src/types.rs +++ b/src/types.rs @@ -172,6 +172,12 @@ impl<'ctx> Type<'ctx> { } } + pub fn is_floating_point(self) -> bool { + unsafe { + gccjit_sys::gcc_jit_type_is_floating_point(self.ptr) != 0 + } + } + pub fn dyncast_vector(self) -> Option> { unsafe { let vector_type = gccjit_sys::gcc_jit_type_dyncast_vector(self.ptr); From 296672cbbe60f86a4cc2685cbd2f6e4dab01bf5d Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 24 Apr 2024 20:12:38 -0400 Subject: [PATCH 098/127] Add LValue::remove --- gccjit_sys/src/lib.rs | 3 +++ src/lvalue.rs | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 121bbbb..ed232cd 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -677,4 +677,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_context_new_alignof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; + + #[cfg(feature="master")] + pub fn gcc_jit_lvalue_remove(lvalue: *mut gcc_jit_lvalue); } diff --git a/src/lvalue.rs b/src/lvalue.rs index 8749918..ce54745 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -177,6 +177,13 @@ impl<'ctx> LValue<'ctx> { } } + #[cfg(feature="master")] + pub fn remove(&self) { + unsafe { + gccjit_sys::gcc_jit_lvalue_remove(self.ptr); + } + } + pub fn set_tls_model(&self, model: TlsModel) { unsafe { gccjit_sys::gcc_jit_lvalue_set_tls_model(self.ptr, model.to_sys()); From 7dfc41b1dcb673dab2b158c15d9526fdeb673f72 Mon Sep 17 00:00:00 2001 From: Robert Zakrzewski Date: Mon, 29 Apr 2024 21:58:58 +0200 Subject: [PATCH 099/127] Make is_floating_point() available for master only --- gccjit_sys/src/lib.rs | 1 + src/types.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 7166832..c3a8a40 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -553,6 +553,7 @@ extern { pub fn gcc_jit_type_dyncast_array(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_bool(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_is_integral(typ: *mut gcc_jit_type) -> c_int; + #[cfg(feature = "master")] pub fn gcc_jit_type_is_floating_point(typ: *mut gcc_jit_type) -> c_int; pub fn gcc_jit_type_unqualified(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; pub fn gcc_jit_type_is_pointer(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; diff --git a/src/types.rs b/src/types.rs index c0f1175..4fd0d69 100644 --- a/src/types.rs +++ b/src/types.rs @@ -172,6 +172,7 @@ impl<'ctx> Type<'ctx> { } } + #[cfg(feature = "master")] pub fn is_floating_point(self) -> bool { unsafe { gccjit_sys::gcc_jit_type_is_floating_point(self.ptr) != 0 From 328cb1b414f67dfa15162ba7a55ed01931f1b219 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Tue, 2 Jul 2024 09:52:31 -0400 Subject: [PATCH 100/127] Add gcc_jit_rvalue_set_type --- gccjit_sys/src/lib.rs | 3 +++ src/rvalue.rs | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index c3a8a40..0ea5d43 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -685,4 +685,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_lvalue_remove(lvalue: *mut gcc_jit_lvalue); + + #[cfg(feature="master")] + pub fn gcc_jit_rvalue_set_type(rvalue: *mut gcc_jit_rvalue, new_type: *mut gcc_jit_type); } diff --git a/src/rvalue.rs b/src/rvalue.rs index 6e15d22..3103696 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -110,6 +110,15 @@ impl<'ctx> RValue<'ctx> { } } + /// Change the type of this RValue. + #[cfg(feature="master")] + pub fn set_type(&self, typ: Type<'ctx>) { + unsafe { + let type_ptr = types::get_ptr(&typ); + gccjit_sys::gcc_jit_rvalue_set_type(self.ptr, type_ptr); + } + } + /// Given an RValue x and a Field f, returns an RValue representing /// C's x.f. pub fn access_field(&self, From 9ae0279ff9a83baa0d18b6eef69861ef3881ba84 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Fri, 5 Jul 2024 15:31:52 -0400 Subject: [PATCH 101/127] Update versions --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90de564..481e408 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.0.0" +version = "2.1.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,4 +13,4 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.1.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.2.0", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 0ef6014..a3a005d 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.1.0" +version = "0.2.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From 8b2d3e3a39e6f8af7da39c1cc4d9d1e89847e389 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 28 Jul 2024 09:57:22 -0400 Subject: [PATCH 102/127] Fix clippy warnings --- src/block.rs | 8 ++++---- src/context.rs | 52 +++++++++++++++++++++++++------------------------- src/rvalue.rs | 10 +++++----- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/block.rs b/src/block.rs index c651085..1502350 100644 --- a/src/block.rs +++ b/src/block.rs @@ -175,10 +175,10 @@ impl<'ctx> Block<'ctx> { }; unsafe { gccjit_sys::gcc_jit_block_add_assignment_op(self.ptr, - loc_ptr, - lvalue::get_ptr(&lvalue), - mem::transmute(op), - rvalue::get_ptr(&rvalue)); + loc_ptr, + lvalue::get_ptr(&lvalue), + mem::transmute::(op), + rvalue::get_ptr(&rvalue)); } } diff --git a/src/context.rs b/src/context.rs index 3c0c8d5..f240e79 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,4 +1,4 @@ -use std::ffi::{CStr, CString}; +use std::ffi::{c_void, CStr, CString}; use std::marker::PhantomData; use std::mem; use std::os::raw::{c_int, c_ulong}; @@ -279,8 +279,8 @@ impl<'ctx> Context<'ctx> { let file_ref = file.as_ref(); let cstr = CString::new(file_ref).unwrap(); gccjit_sys::gcc_jit_context_compile_to_file(self.ptr, - mem::transmute(kind), - cstr.as_ptr()); + mem::transmute::(kind), + cstr.as_ptr()); } } @@ -352,7 +352,7 @@ impl<'ctx> Context<'ctx> { let ptr = gccjit_sys::gcc_jit_context_new_global( self.ptr, loc_ptr, - mem::transmute(kind), + mem::transmute::(kind), types::get_ptr(&ty), cstr.as_ptr()); #[cfg(debug_assertions)] @@ -590,13 +590,13 @@ impl<'ctx> Context<'ctx> { unsafe { let cstr = CString::new(name_ref).unwrap(); let ptr = gccjit_sys::gcc_jit_context_new_function(self.ptr, - loc_ptr, - mem::transmute(kind), - types::get_ptr(&return_ty), - cstr.as_ptr(), - num_params, - params_ptrs.as_mut_ptr(), - is_variadic as i32); + loc_ptr, + mem::transmute::(kind), + types::get_ptr(&return_ty), + cstr.as_ptr(), + num_params, + params_ptrs.as_mut_ptr(), + is_variadic as i32); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); @@ -620,11 +620,11 @@ impl<'ctx> Context<'ctx> { }; unsafe { let ptr = gccjit_sys::gcc_jit_context_new_binary_op(self.ptr, - loc_ptr, - mem::transmute(op), - types::get_ptr(&ty), - rvalue::get_ptr(&left_rvalue), - rvalue::get_ptr(&right_rvalue)); + loc_ptr, + mem::transmute::(op), + types::get_ptr(&ty), + rvalue::get_ptr(&left_rvalue), + rvalue::get_ptr(&right_rvalue)); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); @@ -646,10 +646,10 @@ impl<'ctx> Context<'ctx> { }; unsafe { let ptr = gccjit_sys::gcc_jit_context_new_unary_op(self.ptr, - loc_ptr, - mem::transmute(op), - types::get_ptr(&ty), - rvalue::get_ptr(&rvalue)); + loc_ptr, + mem::transmute::(op), + types::get_ptr(&ty), + rvalue::get_ptr(&rvalue)); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); @@ -671,10 +671,10 @@ impl<'ctx> Context<'ctx> { }; unsafe { let ptr = gccjit_sys::gcc_jit_context_new_comparison(self.ptr, - loc_ptr, - mem::transmute(op), - rvalue::get_ptr(&left_rvalue), - rvalue::get_ptr(&right_rvalue)); + loc_ptr, + mem::transmute::(op), + rvalue::get_ptr(&left_rvalue), + rvalue::get_ptr(&right_rvalue)); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); @@ -1007,8 +1007,8 @@ impl<'ctx> Context<'ctx> { value: *mut ()) -> RValue<'a> { unsafe { let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_ptr(self.ptr, - types::get_ptr(&ty), - mem::transmute(value)); + types::get_ptr(&ty), + mem::transmute::<*mut (), *mut c_void>(value)); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.get_last_error() { panic!("{}", error); diff --git a/src/rvalue.rs b/src/rvalue.rs index 3103696..bb91c18 100644 --- a/src/rvalue.rs +++ b/src/rvalue.rs @@ -64,11 +64,11 @@ macro_rules! binary_operator_for { let ctx_ptr = gccjit_sys::gcc_jit_object_get_context(obj_ptr); let ty = rhs.get_type(); let ptr = gccjit_sys::gcc_jit_context_new_binary_op(ctx_ptr, - ptr::null_mut(), - mem::transmute($op), - types::get_ptr(&ty), - self.ptr, - rhs_rvalue.ptr); + ptr::null_mut(), + mem::transmute::($op), + types::get_ptr(&ty), + self.ptr, + rhs_rvalue.ptr); #[cfg(debug_assertions)] if let Ok(Some(error)) = self.to_object().get_context().get_last_error() { panic!("{}", error); From 8e4ee7809ada26ddfcb16e5fd9cc37fc3c1e597c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bennet=20Ble=C3=9Fmann?= Date: Sun, 28 Jul 2024 15:23:10 +0200 Subject: [PATCH 103/127] update repo url in Cargo.toml fixes rust-lang/gccjit.rs#40 --- Cargo.toml | 2 +- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 481e408..cabc206 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Sean Gillespie ", "Antoni Boucher ", "Antoni Boucher Date: Sun, 1 Sep 2024 11:15:07 -0400 Subject: [PATCH 104/127] Add support for the weak variable attribute --- gccjit_sys/src/lib.rs | 4 ++++ src/lvalue.rs | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 0ea5d43..d9eb8d8 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -283,6 +283,7 @@ pub enum gcc_jit_fn_attribute pub enum gcc_jit_variable_attribute { GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY, + GCC_JIT_VARIABLE_ATTRIBUTE_WEAK, } #[link(name = "gccjit")] @@ -688,4 +689,7 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_rvalue_set_type(rvalue: *mut gcc_jit_rvalue, new_type: *mut gcc_jit_type); + + #[cfg(feature="master")] + pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute); } diff --git a/src/lvalue.rs b/src/lvalue.rs index ce54745..3f1f4b0 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -45,6 +45,7 @@ pub enum AttributeValue<'a> { #[derive(Clone, Copy, Debug)] pub enum VarAttribute { Visibility(Visibility), + Weak, } #[cfg(feature="master")] @@ -52,12 +53,14 @@ impl VarAttribute { fn get_value(&self) -> AttributeValue { match *self { Self::Visibility(visibility) => AttributeValue::String(visibility.as_str()), + Self::Weak => AttributeValue::None, } } fn to_sys(self) -> gccjit_sys::gcc_jit_variable_attribute { match self { VarAttribute::Visibility(_) => gccjit_sys::gcc_jit_variable_attribute::GCC_JIT_VARIABLE_ATTRIBUTE_VISIBILITY, + VarAttribute::Weak => gccjit_sys::gcc_jit_variable_attribute::GCC_JIT_VARIABLE_ATTRIBUTE_WEAK, } } } @@ -224,12 +227,16 @@ impl<'ctx> LValue<'ctx> { } #[cfg(feature="master")] - pub fn add_string_attribute(&self, attribute: VarAttribute) { + pub fn add_attribute(&self, attribute: VarAttribute) { let value = attribute.get_value(); match value { AttributeValue::Int(_) => unimplemented!(), AttributeValue::IntArray(_) => unimplemented!(), - AttributeValue::None => unimplemented!(), + AttributeValue::None => { + unsafe { + gccjit_sys::gcc_jit_lvalue_add_attribute(self.ptr, attribute.to_sys()); + } + }, AttributeValue::String(string) => { let cstr = CString::new(string).unwrap(); unsafe { From 4f10555323250a5c3718840f15c2eb512667966b Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 26 Sep 2024 19:41:56 -0400 Subject: [PATCH 105/127] Update versions --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cabc206..73830ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.1.0" +version = "2.2.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,4 +13,4 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.2.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.3.0", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index e90773b..d6de7b1 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.2.0" +version = "0.3.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From 93a0afd9467715beb5f7d35943ee5f130671c9cf Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 16 Oct 2024 20:53:31 -0400 Subject: [PATCH 106/127] Remove supports_128bit_int --- gccjit_sys/src/lib.rs | 3 --- src/target_info.rs | 6 ------ 2 files changed, 9 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index d9eb8d8..ece285c 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -658,9 +658,6 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_target_info_arch(info: *mut gcc_jit_target_info) -> *const c_char; - #[cfg(feature="master")] - pub fn gcc_jit_target_info_supports_128bit_int(info: *mut gcc_jit_target_info) -> c_int; - #[cfg(feature="master")] pub fn gcc_jit_target_info_supports_target_dependent_type(info: *mut gcc_jit_target_info, ty: gcc_jit_types) -> c_int; diff --git a/src/target_info.rs b/src/target_info.rs index d29b807..9b98b74 100644 --- a/src/target_info.rs +++ b/src/target_info.rs @@ -36,12 +36,6 @@ impl TargetInfo { } } - pub fn supports_128bit_int(&self) -> bool { - unsafe { - gccjit_sys::gcc_jit_target_info_supports_128bit_int(self.ptr) != 0 - } - } - #[cfg(feature="master")] pub fn supports_target_dependent_type(&self, c_type: CType) -> bool { unsafe { From 8438b1ea5c4ff483bc9ef57ddd19dce437bed242 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 16 Oct 2024 20:55:49 -0400 Subject: [PATCH 107/127] Update versions --- Cargo.toml | 2 +- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73830ca..52f7622 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.2.0" +version = "2.3.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index d6de7b1..53c5171 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.3.0" +version = "0.4.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From 0468c5b29a12c0532c0af2082e2f7dcb70b64d69 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 17 Oct 2024 19:17:44 -0400 Subject: [PATCH 108/127] Update dependency version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 52f7622..e1bc674 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,4 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.3.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.4.0", path = "gccjit_sys" } From 05ee257ee7911530bf40fbf8faf5ab3ac0c0dfe2 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 21 Nov 2024 12:02:44 -0500 Subject: [PATCH 109/127] Remove unused APIs --- gccjit_sys/src/lib.rs | 6 ------ src/lvalue.rs | 7 ------- src/types.rs | 11 ----------- 3 files changed, 24 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index ece285c..cdb3072 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -613,9 +613,6 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_type_set_packed(typ: *mut gcc_jit_type); - #[cfg(feature="master")] - pub fn gcc_jit_type_is_const(typ: *mut gcc_jit_type) -> *mut gcc_jit_type; - #[cfg(feature="master")] pub fn gcc_jit_context_convert_vector(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, vector: *mut gcc_jit_rvalue, type_: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; @@ -681,9 +678,6 @@ extern { #[cfg(feature="master")] pub fn gcc_jit_context_new_alignof(ctxt: *mut gcc_jit_context, typ: *mut gcc_jit_type) -> *mut gcc_jit_rvalue; - #[cfg(feature="master")] - pub fn gcc_jit_lvalue_remove(lvalue: *mut gcc_jit_lvalue); - #[cfg(feature="master")] pub fn gcc_jit_rvalue_set_type(rvalue: *mut gcc_jit_rvalue, new_type: *mut gcc_jit_type); diff --git a/src/lvalue.rs b/src/lvalue.rs index 3f1f4b0..851b210 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -180,13 +180,6 @@ impl<'ctx> LValue<'ctx> { } } - #[cfg(feature="master")] - pub fn remove(&self) { - unsafe { - gccjit_sys::gcc_jit_lvalue_remove(self.ptr); - } - } - pub fn set_tls_model(&self, model: TlsModel) { unsafe { gccjit_sys::gcc_jit_lvalue_set_tls_model(self.ptr, model.to_sys()); diff --git a/src/types.rs b/src/types.rs index 4fd0d69..4119ad2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -233,17 +233,6 @@ impl<'ctx> Type<'ctx> { } } - #[cfg(feature="master")] - pub fn is_const(&self) -> Option> { - unsafe { - let value = gccjit_sys::gcc_jit_type_is_const(self.ptr); - if value.is_null() { - return None; - } - Some(from_ptr(value)) - } - } - pub fn is_compatible_with(&self, typ: Type<'ctx>) -> bool { unsafe { gccjit_sys::gcc_jit_compatible_types(self.ptr, typ.ptr) From 3f083c2025e2b19f330bc5dfc82ff329487a5e82 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 21 Nov 2024 12:03:22 -0500 Subject: [PATCH 110/127] Update versions --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e1bc674..b88aaf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.3.0" +version = "2.4.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,4 +13,4 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.4.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.5.0", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 53c5171..0398815 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.4.0" +version = "0.5.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From b4bb406170ac28580173f10bedfd51e319d79e23 Mon Sep 17 00:00:00 2001 From: "[ Taha. Dostifam ]" Date: Sun, 26 Jan 2025 21:54:59 +0330 Subject: [PATCH 111/127] rename param arr_length->num_values in gcc_jit_context_new_struct_constructor function --- gccjit_sys/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index cdb3072..723a7a6 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -588,7 +588,7 @@ extern { pub fn gcc_jit_lvalue_set_register_name(lvalue: *mut gcc_jit_lvalue, reg_name: *const c_char); - pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; + pub fn gcc_jit_context_new_struct_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, num_values: c_int, fields: *mut *mut gcc_jit_field, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_union_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, field: *mut gcc_jit_field, value: *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_context_new_array_constructor(ctxt: *mut gcc_jit_context, loc: *mut gcc_jit_location, typ: *mut gcc_jit_type, arr_length: c_int, values: *mut *mut gcc_jit_rvalue) -> *mut gcc_jit_rvalue; pub fn gcc_jit_global_set_initializer_rvalue(global: *mut gcc_jit_lvalue, init_value: *mut gcc_jit_rvalue) -> *mut gcc_jit_lvalue; From e4524145f0d227ad37adb8b2d42f2f067b6c99a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5B=20Taha=2E=20Dostifam=E2=80=8D=20=5D?= Date: Wed, 29 Jan 2025 13:49:00 +0330 Subject: [PATCH 112/127] Update ci.yml --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dfc0521..dc9f967 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,8 +24,8 @@ jobs: - name: Download and install GCC fork run: | - curl -LO https://github.com/antoyo/gcc/releases/latest/download/gcc-13.deb - sudo dpkg --force-overwrite -i gcc-13.deb + curl -LO https://github.com/antoyo/gcc/releases/latest/download/gcc-15.deb + sudo dpkg --force-overwrite -i gcc-15.deb - name: Set env run: | From 33635fd0c7f9e64718d7dbb6fa435075c56f1317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5B=20Taha=2E=20Dostifam=E2=80=8D=20=5D?= Date: Wed, 29 Jan 2025 18:30:17 +0330 Subject: [PATCH 113/127] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc9f967..3eb44b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ env: jobs: build: - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 From 4ba19647b9411669fdad7e583105a72f0310088c Mon Sep 17 00:00:00 2001 From: "[ Taha. Dostifam ]" Date: Wed, 29 Jan 2025 18:37:48 +0330 Subject: [PATCH 114/127] fix null_terminator byte slice problem at src/lib.rs --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b97f760..8c96db7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ pub use target_info::TargetInfo; #[cfg(feature="master")] pub fn set_global_personality_function_name(name: &'static [u8]) { - debug_assert!(name.ends_with(&[b'\0']), "Expecting a NUL-terminated C string"); + debug_assert!(name.ends_with(b"\0"), "Expecting a NUL-terminated C string"); unsafe { gccjit_sys::gcc_jit_set_global_personality_function_name(name.as_ptr() as *const _); } From 6d53a8ec7b3c31f97b425e3b53cd4415b485a5f5 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 12 Feb 2025 19:38:43 -0500 Subject: [PATCH 115/127] Add MsAbi and SysvAbi function attributes --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- gccjit_sys/src/lib.rs | 2 ++ src/function.rs | 8 +++++++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b88aaf0..525e727 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.4.0" +version = "2.5.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,4 +13,4 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.5.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.6.0", path = "gccjit_sys" } diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 0398815..d53ad98 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.5.0" +version = "0.6.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index cdb3072..70e4958 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -276,6 +276,8 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_CONST, GCC_JIT_FN_ATTRIBUTE_WEAK, GCC_JIT_FN_ATTRIBUTE_NONNULL, + GCC_JIT_FN_ATTRIBUTE_MS_ABI, + GCC_JIT_FN_ATTRIBUTE_SYSV_ABI, } #[cfg(feature="master")] diff --git a/src/function.rs b/src/function.rs index 3f01769..8943a7c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -60,6 +60,8 @@ pub enum FnAttribute<'a> { Const, Weak, NonNull(Vec), + MsAbi, + SysvAbi, } #[cfg(feature="master")] @@ -76,7 +78,9 @@ impl<'a> FnAttribute<'a> { | FnAttribute::ReturnsTwice | FnAttribute::Pure | FnAttribute::Const - | FnAttribute::Weak => AttributeValue::None, + | FnAttribute::Weak + | FnAttribute::MsAbi + | FnAttribute::SysvAbi => AttributeValue::None, FnAttribute::NonNull(ref value) => { debug_assert!( value.iter().all(|attr| *attr > 0), @@ -102,6 +106,8 @@ impl<'a> FnAttribute<'a> { FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK, FnAttribute::NonNull(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NONNULL, + FnAttribute::MsAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_MS_ABI, + FnAttribute::SysvAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_SYSV_ABI, } } } From 3136813b253c93b55481510ebe7989538bf6a0f2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Mar 2025 15:51:58 +0100 Subject: [PATCH 116/127] Build documentation on docs.rs with `master` feature enabled --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 525e727..7f478f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,6 @@ master = ["gccjit_sys/master"] [dependencies] gccjit_sys = { version = "0.6.0", path = "gccjit_sys" } + +[package.metadata.docs.rs] +features = ["master"] From 6d15ba78a6cb44408975849c05e6a924c13401c0 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 30 Apr 2025 10:58:48 -0400 Subject: [PATCH 117/127] Add more calling convention attributes --- gccjit_sys/src/lib.rs | 17 +++++++++++++--- src/context.rs | 2 +- src/function.rs | 47 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index b191c49..6f1a2b2 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -276,8 +276,19 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_CONST, GCC_JIT_FN_ATTRIBUTE_WEAK, GCC_JIT_FN_ATTRIBUTE_NONNULL, - GCC_JIT_FN_ATTRIBUTE_MS_ABI, - GCC_JIT_FN_ATTRIBUTE_SYSV_ABI, + GCC_JIT_FN_ATTRIBUTE_ARM_PCS, + GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT, + GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK, + GCC_JIT_FN_ATTRIBUTE_AVR_SIGNAL, + GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL, + GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT, + GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL, + GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL, + GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT, + GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI, + GCC_JIT_FN_ATTRIBUTE_X86_STDCALL, + GCC_JIT_FN_ATTRIBUTE_X86_SYSV_ABI, + GCC_JIT_FN_ATTRIBUTE_X86_THIS_CALL, } #[cfg(feature="master")] @@ -289,7 +300,7 @@ pub enum gcc_jit_variable_attribute } #[link(name = "gccjit")] -extern { +extern "C" { // context operations pub fn gcc_jit_context_acquire() -> *mut gcc_jit_context; pub fn gcc_jit_context_release(ctx: *mut gcc_jit_context); diff --git a/src/context.rs b/src/context.rs index f240e79..2619dc8 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1180,7 +1180,7 @@ impl<'ctx> Context<'ctx> { pub fn set_logfile>(&self, _logfile: S) { use std::os::raw::c_void; - extern { + extern "C" { static stderr: *mut c_void; } diff --git a/src/function.rs b/src/function.rs index 8943a7c..7982e93 100644 --- a/src/function.rs +++ b/src/function.rs @@ -60,15 +60,27 @@ pub enum FnAttribute<'a> { Const, Weak, NonNull(Vec), - MsAbi, - SysvAbi, + ArmPcs(&'a str), + AvrInterrupt, + AvrNoblock, + AvrSignal, + GcnAmdGpuHsaKernel, + Msp430Interrupt, + NvptxKernel, + X86FastCall, + X86Interrupt, + X86MsAbi, + X86Stdcall, + X86SysvAbi, + X86ThisCall, } #[cfg(feature="master")] impl<'a> FnAttribute<'a> { fn get_value(&self) -> AttributeValue { match *self { - FnAttribute::Alias(value) | FnAttribute::Target(value) => AttributeValue::String(value), + FnAttribute::Alias(value) | FnAttribute::ArmPcs(value) | FnAttribute::Target(value) => + AttributeValue::String(value), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), FnAttribute::AlwaysInline | FnAttribute::Inline @@ -79,8 +91,18 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Pure | FnAttribute::Const | FnAttribute::Weak - | FnAttribute::MsAbi - | FnAttribute::SysvAbi => AttributeValue::None, + | FnAttribute::AvrInterrupt + | FnAttribute::AvrNoblock + | FnAttribute::AvrSignal + | FnAttribute::GcnAmdGpuHsaKernel + | FnAttribute::Msp430Interrupt + | FnAttribute::NvptxKernel + | FnAttribute::X86FastCall + | FnAttribute::X86Interrupt + | FnAttribute::X86MsAbi + | FnAttribute::X86Stdcall + | FnAttribute::X86SysvAbi + | FnAttribute::X86ThisCall => AttributeValue::None, FnAttribute::NonNull(ref value) => { debug_assert!( value.iter().all(|attr| *attr > 0), @@ -106,8 +128,19 @@ impl<'a> FnAttribute<'a> { FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK, FnAttribute::NonNull(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NONNULL, - FnAttribute::MsAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_MS_ABI, - FnAttribute::SysvAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_SYSV_ABI, + FnAttribute::ArmPcs(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ARM_PCS, + FnAttribute::AvrInterrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT, + FnAttribute::AvrNoblock => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK, + FnAttribute::AvrSignal => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_AVR_SIGNAL, + FnAttribute::GcnAmdGpuHsaKernel => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL, + FnAttribute::Msp430Interrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT, + FnAttribute::NvptxKernel => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL, + FnAttribute::X86FastCall => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL, + FnAttribute::X86Interrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT, + FnAttribute::X86MsAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI, + FnAttribute::X86Stdcall => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_STDCALL, + FnAttribute::X86SysvAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_SYSV_ABI, + FnAttribute::X86ThisCall => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_THIS_CALL, } } } From fb8d06e3a4c6b2062163f3de68501d54d39ce09b Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 30 Apr 2025 11:02:42 -0400 Subject: [PATCH 118/127] Update versions --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f478f0..fe3027c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.5.0" +version = "2.6.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,7 +13,7 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.6.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.7.0", path = "gccjit_sys" } [package.metadata.docs.rs] features = ["master"] diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index d53ad98..a23ed39 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.6.0" +version = "0.7.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From 4c74acc746261ae2b38dd5feebfe965c657838b2 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 30 Apr 2025 13:08:59 -0400 Subject: [PATCH 119/127] Support even more calling convention attributes --- gccjit_sys/src/lib.rs | 3 +++ src/function.rs | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 6f1a2b2..2511155 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -276,6 +276,8 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_CONST, GCC_JIT_FN_ATTRIBUTE_WEAK, GCC_JIT_FN_ATTRIBUTE_NONNULL, + GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_CALL, + GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_ENTRY, GCC_JIT_FN_ATTRIBUTE_ARM_PCS, GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT, GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK, @@ -283,6 +285,7 @@ pub enum gcc_jit_fn_attribute GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL, GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT, GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL, + GCC_JIT_FN_ATTRIBUTE_RISCV_INTERRUPT, GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL, GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT, GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI, diff --git a/src/function.rs b/src/function.rs index 7982e93..1e3fb6b 100644 --- a/src/function.rs +++ b/src/function.rs @@ -60,6 +60,8 @@ pub enum FnAttribute<'a> { Const, Weak, NonNull(Vec), + ArmCmseNonsecureCall, + ArmCmseNonsecureEntry, ArmPcs(&'a str), AvrInterrupt, AvrNoblock, @@ -67,6 +69,7 @@ pub enum FnAttribute<'a> { GcnAmdGpuHsaKernel, Msp430Interrupt, NvptxKernel, + RiscvInterrupt(&'a str), X86FastCall, X86Interrupt, X86MsAbi, @@ -79,8 +82,8 @@ pub enum FnAttribute<'a> { impl<'a> FnAttribute<'a> { fn get_value(&self) -> AttributeValue { match *self { - FnAttribute::Alias(value) | FnAttribute::ArmPcs(value) | FnAttribute::Target(value) => - AttributeValue::String(value), + FnAttribute::Alias(value) | FnAttribute::ArmPcs(value)| FnAttribute::RiscvInterrupt(value) + | FnAttribute::Target(value) => AttributeValue::String(value), FnAttribute::Visibility(visibility) => AttributeValue::String(visibility.as_str()), FnAttribute::AlwaysInline | FnAttribute::Inline @@ -91,6 +94,8 @@ impl<'a> FnAttribute<'a> { | FnAttribute::Pure | FnAttribute::Const | FnAttribute::Weak + | FnAttribute::ArmCmseNonsecureCall + | FnAttribute::ArmCmseNonsecureEntry | FnAttribute::AvrInterrupt | FnAttribute::AvrNoblock | FnAttribute::AvrSignal @@ -128,6 +133,8 @@ impl<'a> FnAttribute<'a> { FnAttribute::Const => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_CONST, FnAttribute::Weak => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_WEAK, FnAttribute::NonNull(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NONNULL, + FnAttribute::ArmCmseNonsecureCall => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_CALL, + FnAttribute::ArmCmseNonsecureEntry => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ARM_CMSE_NONSECURE_ENTRY, FnAttribute::ArmPcs(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_ARM_PCS, FnAttribute::AvrInterrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_AVR_INTERRUPT, FnAttribute::AvrNoblock => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_AVR_NOBLOCK, @@ -135,6 +142,7 @@ impl<'a> FnAttribute<'a> { FnAttribute::GcnAmdGpuHsaKernel => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_GCN_AMDGPU_HSA_KERNEL, FnAttribute::Msp430Interrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_MSP430_INTERRUPT, FnAttribute::NvptxKernel => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_NVPTX_KERNEL, + FnAttribute::RiscvInterrupt(_) => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_RISCV_INTERRUPT, FnAttribute::X86FastCall => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_FAST_CALL, FnAttribute::X86Interrupt => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_INTERRUPT, FnAttribute::X86MsAbi => gccjit_sys::gcc_jit_fn_attribute::GCC_JIT_FN_ATTRIBUTE_X86_MS_ABI, From 3696123072f6438af5a1a2d65a0c3f7f92284715 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Wed, 30 Apr 2025 13:09:24 -0400 Subject: [PATCH 120/127] Update versions --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe3027c..2fd9f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.6.0" +version = "2.7.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,7 +13,7 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.7.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.8.0", path = "gccjit_sys" } [package.metadata.docs.rs] features = ["master"] diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index a23ed39..54d62eb 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.7.0" +version = "0.8.0" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From d158a57681af6e9d1b966b99abbe543dfd4601ae Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 18 Jul 2025 16:48:33 +0200 Subject: [PATCH 121/127] Add binding for new `gcc_jit_lvalue_get_name` function --- gccjit_sys/src/lib.rs | 3 +++ src/lvalue.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 2511155..3399b04 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -699,4 +699,7 @@ extern "C" { #[cfg(feature="master")] pub fn gcc_jit_lvalue_add_attribute(variable: *mut gcc_jit_lvalue, attribute: gcc_jit_variable_attribute); + + #[cfg(feature="master")] + pub fn gcc_jit_lvalue_get_name(lvalue: *mut gcc_jit_lvalue) -> *const c_char; } diff --git a/src/lvalue.rs b/src/lvalue.rs index 851b210..20810b1 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -1,6 +1,7 @@ use std::{ffi::CString, marker::PhantomData}; use std::fmt; use std::ptr; + use context::Context; use rvalue::{RValue, ToRValue}; use rvalue; @@ -238,6 +239,18 @@ impl<'ctx> LValue<'ctx> { }, } } + + #[cfg(feature = "master")] + pub fn get_name(&self) -> Option<&'ctx str> { + unsafe { + let str = gccjit_sys::gcc_jit_lvalue_get_name(self.ptr); + if str.is_null() { + None + } else { + Some(std::ffi::CStr::from_ptr(str).to_str().expect("invalid lvalue name")) + } + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From de3329f3fd806726f6f1a234fa6d9eb88e2f3c14 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 18 Jul 2025 16:52:43 +0200 Subject: [PATCH 122/127] Fix new clippy lints --- src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index 4119ad2..8dcd9f8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -212,7 +212,7 @@ impl<'ctx> Type<'ctx> { pub fn get_size(&self) -> u32 { unsafe { let size = gccjit_sys::gcc_jit_type_get_size(self.ptr); - assert_ne!(size, -1, "called get_size of unsupported type: {:?}", self); + assert_ne!(size, -1, "called get_size of unsupported type: {self:?}"); size as u32 } } From eb90e3880df780961d949d350d7a81f2124d10c7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 18 Jul 2025 17:04:55 +0200 Subject: [PATCH 123/127] Update gccjit_sys version to `0.8.1` --- gccjit_sys/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 54d62eb..0dfcbfe 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.8.0" +version = "0.8.1" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate." From c1307335b0ec5d6f772c745c275b6100f1771c24 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 18 Jul 2025 17:05:07 +0200 Subject: [PATCH 124/127] Update crate version to `2.8.0` --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2fd9f3b..5c2a4e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.7.0" +version = "2.8.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,7 +13,7 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.8.0", path = "gccjit_sys" } +gccjit_sys = { version = "0.8.1", path = "gccjit_sys" } [package.metadata.docs.rs] features = ["master"] From 58973977d0369ddeb6bc203d4311221d60106057 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 Aug 2025 16:58:04 +0200 Subject: [PATCH 125/127] Add new `LValue::set_name` API --- gccjit_sys/src/lib.rs | 3 +++ src/lvalue.rs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/gccjit_sys/src/lib.rs b/gccjit_sys/src/lib.rs index 3399b04..c75110d 100644 --- a/gccjit_sys/src/lib.rs +++ b/gccjit_sys/src/lib.rs @@ -702,4 +702,7 @@ extern "C" { #[cfg(feature="master")] pub fn gcc_jit_lvalue_get_name(lvalue: *mut gcc_jit_lvalue) -> *const c_char; + + #[cfg(feature="master")] + pub fn gcc_jit_lvalue_set_name(lvalue: *mut gcc_jit_lvalue, new_name: *const c_char); } diff --git a/src/lvalue.rs b/src/lvalue.rs index 20810b1..af28b79 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -251,6 +251,14 @@ impl<'ctx> LValue<'ctx> { } } } + + #[cfg(feature = "master")] + pub fn set_name(&self, new_name: &str) { + let new_name = CString::new(new_name).unwrap(); + unsafe { + gccjit_sys::gcc_jit_lvalue_set_name(self.ptr, new_name.as_ptr()); + } + } } pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_lvalue) -> LValue<'ctx> { From 906cc716cad20f1a8a7b7059dea0dea599b55468 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 Aug 2025 17:24:38 +0200 Subject: [PATCH 126/127] Fix new rust errors --- src/function.rs | 2 +- src/lvalue.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/function.rs b/src/function.rs index 1e3fb6b..f391f8d 100644 --- a/src/function.rs +++ b/src/function.rs @@ -80,7 +80,7 @@ pub enum FnAttribute<'a> { #[cfg(feature="master")] impl<'a> FnAttribute<'a> { - fn get_value(&self) -> AttributeValue { + fn get_value(&self) -> AttributeValue<'_> { match *self { FnAttribute::Alias(value) | FnAttribute::ArmPcs(value)| FnAttribute::RiscvInterrupt(value) | FnAttribute::Target(value) => AttributeValue::String(value), diff --git a/src/lvalue.rs b/src/lvalue.rs index af28b79..0ae54f3 100644 --- a/src/lvalue.rs +++ b/src/lvalue.rs @@ -51,7 +51,7 @@ pub enum VarAttribute { #[cfg(feature="master")] impl VarAttribute { - fn get_value(&self) -> AttributeValue { + fn get_value(&self) -> AttributeValue<'_> { match *self { Self::Visibility(visibility) => AttributeValue::String(visibility.as_str()), Self::Weak => AttributeValue::None, From fe4110aa650899391d474539226b979f2ecb7df0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 8 Aug 2025 17:46:38 +0200 Subject: [PATCH 127/127] Update `gccjit` version to `2.9.0` and update `gccjit-sys` version to `0.8.2` --- Cargo.toml | 4 ++-- gccjit_sys/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c2a4e6..4c8fe6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit" -version = "2.8.0" +version = "2.9.0" authors = ["Sean Gillespie ", "Antoni Boucher "] description = "Higher-level Rust bindings for libgccjit." keywords = ["compiler", "jit", "gcc"] @@ -13,7 +13,7 @@ readme = "README.md" master = ["gccjit_sys/master"] [dependencies] -gccjit_sys = { version = "0.8.1", path = "gccjit_sys" } +gccjit_sys = { version = "0.8.2", path = "gccjit_sys" } [package.metadata.docs.rs] features = ["master"] diff --git a/gccjit_sys/Cargo.toml b/gccjit_sys/Cargo.toml index 0dfcbfe..33d91e7 100644 --- a/gccjit_sys/Cargo.toml +++ b/gccjit_sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gccjit_sys" -version = "0.8.1" +version = "0.8.2" authors = ["Sean Gillespie ", "Antoni Boucher "] #links = "gccjit" description = "Raw bindings to libgccjit. Companion to the gccjit crate."