gccjit 6.1.0

Higher-level Rust bindings for libgccjit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::ffi::CString;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::os::raw::c_int;
use std::ptr::{self, NonNull};

use asm::ExtendedAsm;
use block;
use context::{Case, Context};
use function::{self, Function};
use location::{self, Location};
use lvalue::{self, ToLValue};
use object::{self, Object, ToObject};
#[cfg(feature = "master")]
use region::{self, Region};
use rvalue::{self, ToRValue};

use crate::{with_lib, with_lib_handle, with_lib_without_error_check};

/// BinaryOp is a enum representing the various binary operations
/// that gccjit knows how to codegen.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum BinaryOp {
    Plus,
    Minus,
    Mult,
    Divide,
    Modulo,
    BitwiseAnd,
    BitwiseXor,
    BitwiseOr,
    LogicalAnd,
    LogicalOr,
    LShift,
    RShift,
}

/// 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,
    LogicalNegate,
    Abs,
}

/// ComparisonOp is an enum representing the various comparisons that
/// gccjit is capable of doing.
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum ComparisonOp {
    Equals,
    NotEquals,
    LessThan,
    LessThanEquals,
    GreaterThan,
    GreaterThanEquals,
}

/// Block represents a basic block in gccjit. Blocks are created by functions.
/// 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, Eq, Hash, PartialEq)]
pub struct Block<'ctx> {
    marker: PhantomData<&'ctx Context<'ctx>>,
    ptr: NonNull<gccjit_sys::gcc_jit_block>,
}

impl<'ctx> ToObject<'ctx> for Block<'ctx> {
    fn to_object(&self) -> Object<'ctx> {
        with_lib_without_error_check(|lib| unsafe {
            let ptr = lib.gcc_jit_block_as_object(get_ptr(self));
            object::from_ptr(ptr).expect("Failed to get Object from Block")
        })
    }
}

impl<'ctx> crate::ContextGetter<'ctx> for Block<'ctx> {
    fn context(&self) -> crate::ContextRef<'ctx> {
        self.to_object().context()
    }
}

impl<'ctx> fmt::Debug for Block<'ctx> {
    fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> {
        let obj = self.to_object();
        obj.fmt(fmt)
    }
}

impl<'ctx> Block<'ctx> {
    #[track_caller]
    pub fn get_function(&self) -> Function<'ctx> {
        with_lib_handle(self, |lib| unsafe {
            let ptr = lib.gcc_jit_block_get_function(get_ptr(self));
            function::from_ptr(ptr)
        })
    }

    #[cfg(feature = "master")]
    #[track_caller]
    pub fn get_successors(&self) -> Vec<Block<'ctx>> {
        with_lib_handle(self, |lib| unsafe {
            let count = lib.gcc_jit_block_get_successor_count(get_ptr(self));
            (0..count)
                .map(|index| from_ptr(lib.gcc_jit_block_get_successor(get_ptr(self), index)))
                .collect::<Option<Vec<_>>>()
        })
    }

    /// Evaluates the rvalue parameter and discards its result. Equivalent
    /// to (void)<expr> in C.
    pub fn add_eval<T: ToRValue<'ctx>>(&self, loc: Option<Location<'ctx>>, value: T) {
        let rvalue = value.to_rvalue();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_eval(get_ptr(self), loc_ptr, rvalue::get_ptr(&rvalue));
        });
    }

    #[cfg(feature = "master")]
    pub fn add_try_catch(
        &self,
        loc: Option<Location<'ctx>>,
        try_region: Region<'ctx>,
        catch_region: Region<'ctx>,
    ) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_try_catch(
                get_ptr(self),
                loc_ptr,
                region::get_ptr(&try_region),
                region::get_ptr(&catch_region),
            );
        });
    }

    #[cfg(feature = "master")]
    pub fn add_try_finally(
        &self,
        loc: Option<Location<'ctx>>,
        try_region: Region<'ctx>,
        finally_region: Region<'ctx>,
    ) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_try_finally(
                get_ptr(self),
                loc_ptr,
                region::get_ptr(&try_region),
                region::get_ptr(&finally_region),
            );
        });
    }

    #[cfg(feature = "master")]
    pub fn add_cleanup(
        &self,
        loc: Option<Location<'ctx>>,
        try_region: Region<'ctx>,
        cleanup_region: Region<'ctx>,
    ) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_cleanup(
                get_ptr(self),
                loc_ptr,
                region::get_ptr(&try_region),
                region::get_ptr(&cleanup_region),
            );
        });
    }

    /// Assigns the value of an rvalue to an lvalue directly. Equivalent
    /// to <lvalue> = <rvalue> in C.
    pub fn add_assignment<L: ToLValue<'ctx>, R: ToRValue<'ctx>>(
        &self,
        loc: Option<Location<'ctx>>,
        assign_target: L,
        value: R,
    ) {
        let lvalue = assign_target.to_lvalue();
        let rvalue = value.to_rvalue();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_assignment(
                get_ptr(self),
                loc_ptr,
                lvalue::get_ptr(&lvalue),
                rvalue::get_ptr(&rvalue),
            );
        });
    }

    /// Performs a binary operation on an LValue and an RValue, assigning
    /// the result of the binary operation to the LValue upon completion.
    /// Equivalent to the *=, +=, -=, etc. operator family in C.
    pub fn add_assignment_op<L: ToLValue<'ctx>, R: ToRValue<'ctx>>(
        &self,
        loc: Option<Location<'ctx>>,
        assign_target: L,
        op: BinaryOp,
        value: R,
    ) {
        let lvalue = assign_target.to_lvalue();
        let rvalue = value.to_rvalue();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_add_assignment_op(
                get_ptr(self),
                loc_ptr,
                lvalue::get_ptr(&lvalue),
                mem::transmute::<BinaryOp, gccjit_sys::gcc_jit_binary_op>(op),
                rvalue::get_ptr(&rvalue),
            );
        });
    }

    /// Adds a comment to a block. It's unclear from the documentation what
    /// this actually means.
    pub fn add_comment<S: AsRef<str>>(&self, loc: Option<Location<'ctx>>, message: S) {
        let message_ref = message.as_ref();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            let cstr = CString::new(message_ref).unwrap();
            lib.gcc_jit_block_add_comment(get_ptr(self), loc_ptr, cstr.as_ptr());
        });
    }

    /// Terminates a block by branching to one of two blocks, depending
    /// on the value of a conditional RValue.
    pub fn end_with_conditional<T: ToRValue<'ctx>>(
        &self,
        loc: Option<Location<'ctx>>,
        cond: T,
        on_true: Block<'ctx>,
        on_false: Block<'ctx>,
    ) {
        let cond_rvalue = cond.to_rvalue();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_conditional(
                get_ptr(self),
                loc_ptr,
                rvalue::get_ptr(&cond_rvalue),
                get_ptr(&on_true),
                get_ptr(&on_false),
            );
        });
    }

    /// Terminates a block by unconditionally jumping to another block.
    pub fn end_with_jump(&self, loc: Option<Location<'ctx>>, target: Block<'ctx>) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_jump(get_ptr(self), loc_ptr, get_ptr(&target));
        });
    }

    /// Terminates a block by returning from the containing function, setting
    /// the rvalue to be the return value of the function. This is equivalent
    /// to C's "return <expr>". This function can only be used to terminate
    /// a block within a function whose return type is not void.
    pub fn end_with_return<T: ToRValue<'ctx>>(&self, loc: Option<Location<'ctx>>, ret: T) {
        let ret_rvalue = ret.to_rvalue();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_return(get_ptr(self), loc_ptr, rvalue::get_ptr(&ret_rvalue));
        });
    }

    /// Terminates a block by returning from the containing function, returning
    /// no value. This is equivalent to C's bare "return" with no expression.
    /// This function can only be used to terminate a block within a function
    /// that returns void.
    pub fn end_with_void_return(&self, loc: Option<Location<'ctx>>) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_void_return(get_ptr(self), loc_ptr);
        });
    }

    pub fn end_with_switch<T: ToRValue<'ctx>>(
        &self,
        loc: Option<Location<'ctx>>,
        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) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_switch(
                get_ptr(self),
                loc_ptr,
                rvalue::get_ptr(&expr),
                block::get_ptr(&default_block),
                cases.len() as c_int,
                cases.as_ptr() as *mut *mut _,
            );
        });
    }

    #[cfg(feature = "master")]
    pub fn end_with_fallthrough(&self, loc: Option<Location<'ctx>>) {
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib(self, |lib| unsafe {
            lib.gcc_jit_block_end_with_fallthrough(get_ptr(self), loc_ptr);
        });
    }

    #[track_caller]
    pub fn add_extended_asm(
        &self,
        loc: Option<Location<'ctx>>,
        asm_template: &str,
    ) -> ExtendedAsm<'ctx> {
        let asm_template = CString::new(asm_template).unwrap();
        let loc_ptr = match loc {
            Some(loc) => unsafe { location::get_ptr(&loc) },
            None => ptr::null_mut(),
        };
        with_lib_handle(self, |lib| unsafe {
            ExtendedAsm::from_ptr(lib.gcc_jit_block_add_extended_asm(
                get_ptr(self),
                loc_ptr,
                asm_template.as_ptr(),
            ))
        })
    }

    #[track_caller]
    pub fn end_with_extended_asm_goto(
        &self,
        loc: Option<Location<'ctx>>,
        asm_template: &str,
        goto_blocks: &[Block<'ctx>],
        fallthrough_block: Option<Block<'ctx>>,
    ) -> ExtendedAsm<'ctx> {
        let asm_template = CString::new(asm_template).unwrap();
        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(),
        };
        with_lib_handle(self, |lib| unsafe {
            ExtendedAsm::from_ptr(lib.gcc_jit_block_end_with_extended_asm_goto(
                get_ptr(self),
                loc_ptr,
                asm_template.as_ptr(),
                goto_blocks.len() as c_int,
                goto_blocks.as_ptr() as *mut _,
                fallthrough_block_ptr,
            ))
        })
    }
}

#[cfg(feature = "master")]
#[track_caller]
pub fn clone_blocks<'ctx>(blocks: &[Block<'ctx>]) -> Vec<Block<'ctx>> {
    if blocks.is_empty() {
        return Vec::new();
    }
    with_lib_handle(&blocks[0], |lib| {
        let mut src: Vec<_> = blocks
            .iter()
            .map(|block| unsafe { get_ptr(block) })
            .collect();
        let mut dst: Vec<*mut gccjit_sys::gcc_jit_block> = vec![ptr::null_mut(); blocks.len()];
        unsafe {
            lib.gcc_jit_blocks_clone(src.len() as c_int, src.as_mut_ptr(), dst.as_mut_ptr());
            dst.into_iter()
                .map(|ptr| from_ptr(ptr))
                .collect::<Option<Vec<_>>>()
        }
    })
}

pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_block) -> Option<Block<'ctx>> {
    Some(Block {
        marker: PhantomData,
        ptr: NonNull::new(ptr)?,
    })
}

pub unsafe fn get_ptr<'ctx>(block: &Block<'ctx>) -> *mut gccjit_sys::gcc_jit_block {
    block.ptr.as_ptr()
}