forked from swgillespie/gccjit.rs
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontext.rs
1378 lines (1279 loc) · 52.9 KB
/
context.rs
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::ffi::{c_void, CStr, CString};
use std::marker::PhantomData;
use std::mem;
use std::os::raw::{c_int, c_ulong};
use std::ptr;
use std::str::Utf8Error;
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};
#[cfg(feature="master")]
use target_info::{self, TargetInfo};
use Type;
use types;
#[repr(C)]
#[derive(Debug)]
pub enum GlobalKind {
Exported,
Internal,
Imported,
}
/// 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,
/// Optimizies for both speed and code size, but doesn't apply
/// any optimizations that take extended periods of time.
Limited,
/// Performs all optimizations that do not involve a tradeoff
/// of code size for speed.
Standard,
/// Performs all optimizations at the Standard level, as well
/// as function inlining, loop vectorization, some loop unrolling,
/// and various other optimizations.
Aggressive
}
/// This enum indicates to gccjit the format of the output
/// code that is written out by compile_to_file.
#[repr(C)]
pub enum OutputKind {
/// Outputs an assembly file (.S)
Assembler,
/// Outputs an object file (.o)
ObjectFile,
/// Outputs a dynamic library (.so)
DynamicLibrary,
/// Outputs an executable
Executable
}
/// Represents a successful compilation of a context. This type
/// provides the means to access compiled functions and globals.
/// JIT compiled functions are exposted to Rust as an extern "C" function
/// pointer.
pub struct CompileResult {
ptr: *mut gccjit_sys::gcc_jit_result
}
impl CompileResult {
/// Gets a function pointer to a JIT compiled function. If the function
/// does not exist (wasn't compiled by the Context that produced this
/// CompileResult), this function returns a null pointer.
///
/// It is the caller's responsibility to ensure that this pointer is not used
/// past the lifetime of the CompileResult object. Second, it is
/// the caller's responsibility to check whether or not the pointer
/// is null. It is also expected that the caller of this function
/// will transmute this pointer to a function pointer type.
pub fn get_function<S: AsRef<str>>(&self, name: S) -> *mut () {
let c_str = CString::new(name.as_ref()).unwrap();
unsafe {
let func = gccjit_sys::gcc_jit_result_get_code(self.ptr,
c_str.as_ptr());
mem::transmute(func)
}
}
/// Gets a pointer to a global variable that lives on the JIT heap.
///
/// It is the caller's responsibility
/// to ensure that the pointer is not used past the lifetime of the
/// CompileResult object. It is also the caller's responsibility to
/// check whether or not the returned pointer is null.
pub fn get_global<S: AsRef<str>>(&self, name: S) -> *mut () {
let c_str = CString::new(name.as_ref()).unwrap();
unsafe {
let ptr = gccjit_sys::gcc_jit_result_get_global(self.ptr, c_str.as_ptr());
mem::transmute(ptr)
}
}
}
impl Drop for CompileResult {
fn drop(&mut self) {
unsafe {
gccjit_sys::gcc_jit_result_release(self.ptr);
}
}
}
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
/// structures, and as such anything made from this context
/// must have a lifetime strictly less than this object.
///
/// 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
}
impl Default for Context<'static> {
fn default() -> Context<'static> {
unsafe {
Context {
marker: PhantomData,
ptr: gccjit_sys::gcc_jit_context_acquire(),
}
}
}
}
impl<'ctx> Context<'ctx> {
/// Sets the program name reported by the JIT.
pub fn set_program_name<S: AsRef<str>>(&self, name: S) {
let name_ref = name.as_ref();
let c_str = CString::new(name_ref).unwrap();
unsafe {
gccjit_sys::gcc_jit_context_set_str_option(self.ptr,
GCC_JIT_STR_OPTION_PROGNAME,
c_str.as_ptr());
}
}
pub fn add_command_line_option<S: AsRef<str>>(&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<S: AsRef<str>>(&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.
pub fn set_optimization_level(&self, level: OptimizationLevel) {
unsafe {
gccjit_sys::gcc_jit_context_set_int_option(self.ptr,
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
level as i32);
}
}
#[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());
}
}
#[cfg(feature="master")]
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_str_option(self.ptr, GCC_JIT_STR_OPTION_SPECIAL_CHARS_IN_FUNC_NAMES, c_str.as_ptr());
}
}
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_print_errors_to_stderr(&self, enabled: bool) {
unsafe {
gccjit_sys::gcc_jit_context_set_bool_print_errors_to_stderr(self.ptr, enabled as c_int);
}
}
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) {
unsafe {
gccjit_sys::gcc_jit_context_set_bool_option(self.ptr,
GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
value as i32);
}
}
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.
pub fn compile(&self) -> CompileResult {
unsafe {
CompileResult {
ptr: gccjit_sys::gcc_jit_context_compile(self.ptr)
}
}
}
/// 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<S: AsRef<str>>(&self, kind: OutputKind, file: S) {
unsafe {
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::<OutputKind, gccjit_sys::gcc_jit_output_kind>(kind),
cstr.as_ptr());
}
}
#[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.
pub fn new_child_context<'b>(&'b self) -> Context<'b> {
unsafe {
Context {
marker: PhantomData,
ptr: gccjit_sys::gcc_jit_context_new_child_context(self.ptr)
}
}
}
pub fn new_case<S: ToRValue<'ctx>, 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();
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
/// program. The filename, line, and col are used by gdb to "show" your
/// source when in a debugger.
pub fn new_location<'a, S: AsRef<str>>(&'a self,
filename: S,
line: i32,
col: i32) -> Location<'a> {
unsafe {
let filename_ref = filename.as_ref();
let cstr = CString::new(filename_ref).unwrap();
let ptr = gccjit_sys::gcc_jit_context_new_location(self.ptr,
cstr.as_ptr(),
line,
col);
#[cfg(debug_assertions)]
if let Ok(Some(error)) = self.get_last_error() {
panic!("{}", error);
}
location::from_ptr(ptr)
}
}
pub fn new_global<'a, S: AsRef<str>>(&self, loc: Option<Location<'a>>, 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::<GlobalKind, gccjit_sys::gcc_jit_global_kind>(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)
}
}
/// 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
/// to provide their own types that implement Typeable for ease of type
/// creation.
pub fn new_type<'a, T: types::Typeable>(&'a self) -> types::Type<'a> {
<T as types::Typeable>::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());
#[cfg(debug_assertions)]
if let Ok(Some(error)) = self.get_last_error() {
panic!("{}", error);
}
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);
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)
}
}
/// 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<str>>(&'a self,
loc: Option<Location<'a>>,
ty: types::Type<'a>,
name: S) -> Field<'a> {
let name_ref = name.as_ref();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
unsafe {
let cstr = CString::new(name_ref).unwrap();
let ptr = gccjit_sys::gcc_jit_context_new_field(self.ptr,
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)
}
}
/// Constructs a new array type with a given base element type and a
/// size.
pub fn new_array_type<'a>(&'a self,
loc: Option<Location<'a>>,
ty: 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()
};
unsafe {
let ptr = gccjit_sys::gcc_jit_context_new_array_type(self.ptr,
loc_ptr,
types::get_ptr(&ty),
num_elements as c_ulong);
#[cfg(debug_assertions)]
if let Ok(Some(error)) = self.get_last_error() {
panic!("{}", error);
}
types::from_ptr(ptr)
}
}
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)
}
}
/// 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.
pub fn new_struct_type<'a, S: AsRef<str>>(&'a self,
loc: Option<Location<'a>>,
name: S,
fields: &[Field<'a>]) -> Struct<'a> {
let name_ref = name.as_ref();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_fields = fields.len() as i32;
let mut fields_ptrs : Vec<_> = fields.iter()
.map(|x| unsafe { field::get_ptr(x) })
.collect();
unsafe {
let cname = CString::new(name_ref).unwrap();
let ptr = gccjit_sys::gcc_jit_context_new_struct_type(self.ptr,
loc_ptr,
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)
}
}
/// 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<str>>(&'a self,
loc: Option<Location<'a>>,
name: S) -> Struct<'a> {
let name_ref = name.as_ref();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
unsafe {
let cstr = CString::new(name_ref).unwrap();
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)
}
}
/// Creates a new union type from a set of fields.
pub fn new_union_type<'a, S: AsRef<str>>(&'a self,
loc: Option<Location<'a>>,
name: S,
fields: &[Field<'a>]) -> types::Type<'a> {
let name_ref = name.as_ref();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_fields = fields.len() as i32;
let mut fields_ptrs : Vec<_> = fields.iter()
.map(|x| unsafe { field::get_ptr(x) })
.collect();
unsafe {
let cname = CString::new(name_ref).unwrap();
let ptr = gccjit_sys::gcc_jit_context_new_union_type(self.ptr,
loc_ptr,
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)
}
}
/// 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
/// the varargs calling convention.
pub fn new_function_pointer_type<'a>(&'a self,
loc: Option<Location<'a>>,
return_type: types::Type<'a>,
param_types: &[types::Type<'a>],
is_variadic: bool) -> types::Type<'a> {
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_types = param_types.len() as i32;
let mut types_ptrs : Vec<_> = param_types.iter()
.map(|x| unsafe { types::get_ptr(x) })
.collect();
unsafe {
let ptr = gccjit_sys::gcc_jit_context_new_function_ptr_type(self.ptr,
loc_ptr,
types::get_ptr(&return_type),
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)
}
}
/// Creates a new function with the given function kind, return type, parameters, name,
/// and whether or not the function is variadic.
pub fn new_function<'a, S: AsRef<str>>(&'a self,
loc: Option<Location<'a>>,
kind: FunctionType,
return_ty: types::Type<'a>,
params: &[Parameter<'a>],
name: S,
is_variadic: bool) -> Function<'a> {
let name_ref = name.as_ref();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_params = params.len() as i32;
let mut params_ptrs : Vec<_> = params.iter()
.map(|x| unsafe { parameter::get_ptr(x) })
.collect();
unsafe {
let cstr = CString::new(name_ref).unwrap();
let ptr = gccjit_sys::gcc_jit_context_new_function(self.ptr,
loc_ptr,
mem::transmute::<FunctionType, gccjit_sys::gcc_jit_function_kind>(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);
}
function::from_ptr(ptr)
}
}
/// Creates a new binary operation between two RValues and produces a new RValue.
pub fn new_binary_op<'a, L: ToRValue<'a>, R: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
op: BinaryOp,
ty: types::Type<'a>,
left: L,
right: R) -> RValue<'a> {
let left_rvalue = left.to_rvalue();
let right_rvalue = right.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_binary_op(self.ptr,
loc_ptr,
mem::transmute::<BinaryOp, gccjit_sys::gcc_jit_binary_op>(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);
}
rvalue::from_ptr(ptr)
}
}
/// Creates a unary operation on one RValue and produces a result RValue.
pub fn new_unary_op<'a, T: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
op: UnaryOp,
ty: types::Type<'a>,
target: T) -> RValue<'a> {
let rvalue = target.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_unary_op(self.ptr,
loc_ptr,
mem::transmute::<UnaryOp, gccjit_sys::gcc_jit_unary_op>(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)
}
}
pub fn new_comparison<'a, L: ToRValue<'a>, R: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
op: ComparisonOp,
left: L,
right: R) -> RValue<'a> {
let left_rvalue = left.to_rvalue();
let right_rvalue = right.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_comparison(self.ptr,
loc_ptr,
mem::transmute::<ComparisonOp, gccjit_sys::gcc_jit_comparison>(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)
}
}
/// Creates a function call to a function object with a given number of parameters.
/// The RValue that is returned is the result of the function call.
/// Note that due to the way that Rust's generics work, it is currently
/// not possible to be generic over different types of arguments (RValues
/// 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<Location<'a>>,
func: Function<'a>,
args: &[RValue<'a>]) -> RValue<'a> {
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_params = args.len() as i32;
let mut params_ptrs : Vec<_> = args.iter()
.map(|x| unsafe { rvalue::get_ptr(x) })
.collect();
unsafe {
let ptr = gccjit_sys::gcc_jit_context_new_call(self.ptr,
loc_ptr,
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)
}
}
/// Creates an indirect function call that dereferences a function pointer and
/// attempts to invoke it with the given arguments. The RValue that is returned
/// is the result of the function call.
pub fn new_call_through_ptr<'a, F: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
fun_ptr: F,
args: &[RValue<'a>]) -> RValue<'a> {
let fun_ptr_rvalue = fun_ptr.to_rvalue();
let loc_ptr = match loc {
Some(loc) => unsafe { location::get_ptr(&loc) },
None => ptr::null_mut()
};
let num_params = args.len() as i32;
let mut params_ptrs : Vec<_> = args.iter()
.map(|x| x.to_rvalue())
.map(|x| unsafe { rvalue::get_ptr(&x) })
.collect();
unsafe {
let ptr = gccjit_sys::gcc_jit_context_new_call_through_ptr(self.ptr,
loc_ptr,
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)
}
}
/// Cast an RValue to a specific type. I don't know what happens when the cast fails yet.
pub fn new_cast<'a, T: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
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_cast(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)
}
}
/// Bitcast an RValue to a specific type.
pub fn new_bitcast<'a, T: ToRValue<'a>>(&'a self,
loc: Option<Location<'a>>,
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,
loc: Option<Location<'a>>,
array_ptr: A,
index: I) -> LValue<'a> {
let array_rvalue = array_ptr.to_rvalue();
let idx_rvalue = index.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_array_access(self.ptr,
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)
}
}
/// Creates a new RValue from a given long value.
pub fn new_rvalue_from_long<'a>(&'a self,
ty: types::Type<'a>,
value: i64) -> RValue<'a> {
unsafe {
let ptr = gccjit_sys::gcc_jit_context_new_rvalue_from_long(self.ptr,
types::get_ptr(&ty),
value as _);
#[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<Location<'a>>, 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 _);
#[cfg(debug_assertions)]
if let Ok(Some(error)) = self.get_last_error() {
panic!("{}", error);
}
rvalue::from_ptr(ptr)
}
}
#[cfg(feature="master")]
pub fn new_rvalue_vector_perm<'a>(&'a self, loc: Option<Location<'a>>, 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<Location<'a>>, 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 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);
}
rvalue::from_ptr(ptr)
}
}
pub fn new_array_constructor<'a>(&'a self, loc: Option<Location<'a>>, 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_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);
}
rvalue::from_ptr(ptr)
}
}
#[cfg(feature="master")]
pub fn new_vector_access<'a>(&'a self, loc: Option<Location<'a>>, 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)
}
}
#[cfg(feature="master")]
pub fn convert_vector<'a>(&'a self, loc: Option<Location<'a>>, 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>,
value: i32) -> RValue<'a> {
unsafe {
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)
}
}
/// Creates a new RValue from a given double value.
pub fn new_rvalue_from_double<'a>(&'a self,
ty: types::Type<'a>,
value: f64) -> RValue<'a> {
unsafe {
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)
}
}
/// Creates a zero element for a given type.
pub fn new_rvalue_zero<'a>(&'a self,
ty: types::Type<'a>) -> RValue<'a> {
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)
}
}
/// Creates a one element for a given type.
pub fn new_rvalue_one<'a>(&'a self,
ty: types::Type<'a>) -> RValue<'a> {
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)
}
}