]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/builder.rs
Merge commit 'c4416f20dcaec5d93077f72470e83e150fb923b1' into sync-rustfmt
[rust.git] / compiler / rustc_codegen_gcc / src / builder.rs
1 use std::borrow::Cow;
2 use std::cell::Cell;
3 use std::convert::TryFrom;
4 use std::ops::Deref;
5
6 use gccjit::{
7     BinaryOp,
8     Block,
9     ComparisonOp,
10     Context,
11     Function,
12     LValue,
13     RValue,
14     ToRValue,
15     Type,
16     UnaryOp,
17 };
18 use rustc_codegen_ssa::MemFlags;
19 use rustc_codegen_ssa::common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
20 use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
21 use rustc_codegen_ssa::mir::place::PlaceRef;
22 use rustc_codegen_ssa::traits::{
23     BackendTypes,
24     BaseTypeMethods,
25     BuilderMethods,
26     ConstMethods,
27     DerivedTypeMethods,
28     LayoutTypeMethods,
29     HasCodegen,
30     OverflowOp,
31     StaticBuilderMethods,
32 };
33 use rustc_data_structures::stable_set::FxHashSet;
34 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
35 use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout};
36 use rustc_span::Span;
37 use rustc_span::def_id::DefId;
38 use rustc_target::abi::{
39     self,
40     call::FnAbi,
41     Align,
42     HasDataLayout,
43     Size,
44     TargetDataLayout,
45     WrappingRange,
46 };
47 use rustc_target::spec::{HasTargetSpec, Target};
48
49 use crate::common::{SignType, TypeReflection, type_is_pointer};
50 use crate::context::CodegenCx;
51 use crate::intrinsic::llvm;
52 use crate::type_of::LayoutGccExt;
53
54 // TODO(antoyo)
55 type Funclet = ();
56
57 // TODO(antoyo): remove this variable.
58 static mut RETURN_VALUE_COUNT: usize = 0;
59
60 enum ExtremumOperation {
61     Max,
62     Min,
63 }
64
65 pub struct Builder<'a: 'gcc, 'gcc, 'tcx> {
66     pub cx: &'a CodegenCx<'gcc, 'tcx>,
67     pub block: Block<'gcc>,
68     stack_var_count: Cell<usize>,
69 }
70
71 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
72     fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
73         Builder {
74             cx,
75             block,
76             stack_var_count: Cell::new(0),
77         }
78     }
79
80     fn atomic_extremum(&mut self, operation: ExtremumOperation, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
81         let size = src.get_type().get_size();
82
83         let func = self.current_func();
84
85         let load_ordering =
86             match order {
87                 // TODO(antoyo): does this make sense?
88                 AtomicOrdering::AcquireRelease | AtomicOrdering::Release => AtomicOrdering::Acquire,
89                 _ => order,
90             };
91         let previous_value = self.atomic_load(dst.get_type(), dst, load_ordering, Size::from_bytes(size));
92         let previous_var = func.new_local(None, previous_value.get_type(), "previous_value");
93         let return_value = func.new_local(None, previous_value.get_type(), "return_value");
94         self.llbb().add_assignment(None, previous_var, previous_value);
95         self.llbb().add_assignment(None, return_value, previous_var.to_rvalue());
96
97         let while_block = func.new_block("while");
98         let after_block = func.new_block("after_while");
99         self.llbb().end_with_jump(None, while_block);
100
101         // NOTE: since jumps were added and compare_exchange doesn't expect this, the current block in the
102         // state need to be updated.
103         self.switch_to_block(while_block);
104
105         let comparison_operator =
106             match operation {
107                 ExtremumOperation::Max => ComparisonOp::LessThan,
108                 ExtremumOperation::Min => ComparisonOp::GreaterThan,
109             };
110
111         let cond1 = self.context.new_comparison(None, comparison_operator, previous_var.to_rvalue(), self.context.new_cast(None, src, previous_value.get_type()));
112         let compare_exchange = self.compare_exchange(dst, previous_var, src, order, load_ordering, false);
113         let cond2 = self.cx.context.new_unary_op(None, UnaryOp::LogicalNegate, compare_exchange.get_type(), compare_exchange);
114         let cond = self.cx.context.new_binary_op(None, BinaryOp::LogicalAnd, self.cx.bool_type, cond1, cond2);
115
116         while_block.end_with_conditional(None, cond, while_block, after_block);
117
118         // NOTE: since jumps were added in a place rustc does not expect, the current block in the
119         // state need to be updated.
120         self.switch_to_block(after_block);
121
122         return_value.to_rvalue()
123     }
124
125     fn compare_exchange(&self, dst: RValue<'gcc>, cmp: LValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
126         let size = src.get_type().get_size();
127         let compare_exchange = self.context.get_builtin_function(&format!("__atomic_compare_exchange_{}", size));
128         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
129         let failure_order = self.context.new_rvalue_from_int(self.i32_type, failure_order.to_gcc());
130         let weak = self.context.new_rvalue_from_int(self.bool_type, weak as i32);
131
132         let void_ptr_type = self.context.new_type::<*mut ()>();
133         let volatile_void_ptr_type = void_ptr_type.make_volatile();
134         let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
135         let expected = self.context.new_cast(None, cmp.get_address(None), void_ptr_type);
136
137         // NOTE: not sure why, but we have the wrong type here.
138         let int_type = compare_exchange.get_param(2).to_rvalue().get_type();
139         let src = self.context.new_cast(None, src, int_type);
140         self.context.new_call(None, compare_exchange, &[dst, expected, src, weak, order, failure_order])
141     }
142
143     pub fn assign(&self, lvalue: LValue<'gcc>, value: RValue<'gcc>) {
144         self.llbb().add_assignment(None, lvalue, value);
145     }
146
147     fn check_call<'b>(&mut self, _typ: &str, func: Function<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
148         let mut all_args_match = true;
149         let mut param_types = vec![];
150         let param_count = func.get_param_count();
151         for (index, arg) in args.iter().enumerate().take(param_count) {
152             let param = func.get_param(index as i32);
153             let param = param.to_rvalue().get_type();
154             if param != arg.get_type() {
155                 all_args_match = false;
156             }
157             param_types.push(param);
158         }
159
160         if all_args_match {
161             return Cow::Borrowed(args);
162         }
163
164         let casted_args: Vec<_> = param_types
165             .into_iter()
166             .zip(args.iter())
167             .enumerate()
168             .map(|(_i, (expected_ty, &actual_val))| {
169                 let actual_ty = actual_val.get_type();
170                 if expected_ty != actual_ty {
171                     self.bitcast(actual_val, expected_ty)
172                 }
173                 else {
174                     actual_val
175                 }
176             })
177             .collect();
178
179         Cow::Owned(casted_args)
180     }
181
182     fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
183         let mut all_args_match = true;
184         let mut param_types = vec![];
185         let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
186         for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
187             let param = gcc_func.get_param_type(index);
188             if param != arg.get_type() {
189                 all_args_match = false;
190             }
191             param_types.push(param);
192         }
193
194         let mut on_stack_param_indices = FxHashSet::default();
195         if let Some(indices) = self.on_stack_params.borrow().get(&gcc_func) {
196             on_stack_param_indices = indices.clone();
197         }
198
199         if all_args_match {
200             return Cow::Borrowed(args);
201         }
202
203         let func_name = format!("{:?}", func_ptr);
204
205         let casted_args: Vec<_> = param_types
206             .into_iter()
207             .zip(args.iter())
208             .enumerate()
209             .map(|(index, (expected_ty, &actual_val))| {
210                 if llvm::ignore_arg_cast(&func_name, index, args.len()) {
211                     return actual_val;
212                 }
213
214                 let actual_ty = actual_val.get_type();
215                 if expected_ty != actual_ty {
216                     if !actual_ty.is_vector() && !expected_ty.is_vector() && actual_ty.is_integral() && expected_ty.is_integral() && actual_ty.get_size() != expected_ty.get_size() {
217                         self.context.new_cast(None, actual_val, expected_ty)
218                     }
219                     else if on_stack_param_indices.contains(&index) {
220                         actual_val.dereference(None).to_rvalue()
221                     }
222                     else {
223                         assert!(!((actual_ty.is_vector() && !expected_ty.is_vector()) || (!actual_ty.is_vector() && expected_ty.is_vector())), "{:?} ({}) -> {:?} ({}), index: {:?}[{}]", actual_ty, actual_ty.is_vector(), expected_ty, expected_ty.is_vector(), func_ptr, index);
224                         // TODO(antoyo): perhaps use __builtin_convertvector for vector casting.
225                         self.bitcast(actual_val, expected_ty)
226                     }
227                 }
228                 else {
229                     actual_val
230                 }
231             })
232             .collect();
233
234         Cow::Owned(casted_args)
235     }
236
237     fn check_store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
238         let dest_ptr_ty = self.cx.val_ty(ptr).make_pointer(); // TODO(antoyo): make sure make_pointer() is okay here.
239         let stored_ty = self.cx.val_ty(val);
240         let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
241
242         if dest_ptr_ty == stored_ptr_ty {
243             ptr
244         }
245         else {
246             self.bitcast(ptr, stored_ptr_ty)
247         }
248     }
249
250     pub fn current_func(&self) -> Function<'gcc> {
251         self.block.get_function()
252     }
253
254     fn function_call(&mut self, func: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
255         // TODO(antoyo): remove when the API supports a different type for functions.
256         let func: Function<'gcc> = self.cx.rvalue_as_function(func);
257         let args = self.check_call("call", func, args);
258
259         // gccjit requires to use the result of functions, even when it's not used.
260         // That's why we assign the result to a local or call add_eval().
261         let return_type = func.get_return_type();
262         let void_type = self.context.new_type::<()>();
263         let current_func = self.block.get_function();
264         if return_type != void_type {
265             unsafe { RETURN_VALUE_COUNT += 1 };
266             let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
267             self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
268             result.to_rvalue()
269         }
270         else {
271             self.block.add_eval(None, self.cx.context.new_call(None, func, &args));
272             // Return dummy value when not having return value.
273             self.context.new_rvalue_from_long(self.isize_type, 0)
274         }
275     }
276
277     fn function_ptr_call(&mut self, func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
278         let args = self.check_ptr_call("call", func_ptr, args);
279
280         // gccjit requires to use the result of functions, even when it's not used.
281         // That's why we assign the result to a local or call add_eval().
282         let gcc_func = func_ptr.get_type().dyncast_function_ptr_type().expect("function ptr");
283         let return_type = gcc_func.get_return_type();
284         let void_type = self.context.new_type::<()>();
285         let current_func = self.block.get_function();
286
287         if return_type != void_type {
288             unsafe { RETURN_VALUE_COUNT += 1 };
289             let result = current_func.new_local(None, return_type, &format!("ptrReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
290             let func_name = format!("{:?}", func_ptr);
291             let args = llvm::adjust_intrinsic_arguments(&self, gcc_func, args, &func_name);
292             self.block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
293             result.to_rvalue()
294         }
295         else {
296             #[cfg(not(feature="master"))]
297             if gcc_func.get_param_count() == 0 {
298                 // FIXME(antoyo): As a temporary workaround for unsupported LLVM intrinsics.
299                 self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
300             }
301             else {
302                 self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
303             }
304             #[cfg(feature="master")]
305             self.block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
306             // Return dummy value when not having return value.
307             let result = current_func.new_local(None, self.isize_type, "dummyValueThatShouldNeverBeUsed");
308             self.block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
309             result.to_rvalue()
310         }
311     }
312
313     pub fn overflow_call(&self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
314         // gccjit requires to use the result of functions, even when it's not used.
315         // That's why we assign the result to a local.
316         let return_type = self.context.new_type::<bool>();
317         let current_func = self.block.get_function();
318         // TODO(antoyo): return the new_call() directly? Since the overflow function has no side-effects.
319         unsafe { RETURN_VALUE_COUNT += 1 };
320         let result = current_func.new_local(None, return_type, &format!("overflowReturnValue{}", unsafe { RETURN_VALUE_COUNT }));
321         self.block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
322         result.to_rvalue()
323     }
324 }
325
326 impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
327     type CodegenCx = CodegenCx<'gcc, 'tcx>;
328 }
329
330 impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
331     fn tcx(&self) -> TyCtxt<'tcx> {
332         self.cx.tcx()
333     }
334 }
335
336 impl HasDataLayout for Builder<'_, '_, '_> {
337     fn data_layout(&self) -> &TargetDataLayout {
338         self.cx.data_layout()
339     }
340 }
341
342 impl<'tcx> LayoutOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
343     type LayoutOfResult = TyAndLayout<'tcx>;
344
345     #[inline]
346     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
347         self.cx.handle_layout_err(err, span, ty)
348     }
349 }
350
351 impl<'tcx> FnAbiOfHelpers<'tcx> for Builder<'_, '_, 'tcx> {
352     type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
353
354     #[inline]
355     fn handle_fn_abi_err(
356         &self,
357         err: FnAbiError<'tcx>,
358         span: Span,
359         fn_abi_request: FnAbiRequest<'tcx>,
360     ) -> ! {
361         self.cx.handle_fn_abi_err(err, span, fn_abi_request)
362     }
363 }
364
365 impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
366     type Target = CodegenCx<'gcc, 'tcx>;
367
368     fn deref(&self) -> &Self::Target {
369         self.cx
370     }
371 }
372
373 impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
374     type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
375     type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
376     type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
377     type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
378     type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
379
380     type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
381     type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
382     type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
383 }
384
385 impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
386     fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
387         Builder::with_cx(cx, block)
388     }
389
390     fn llbb(&self) -> Block<'gcc> {
391         self.block
392     }
393
394     fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
395         let func = cx.rvalue_as_function(func);
396         func.new_block(name)
397     }
398
399     fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
400         let func = self.current_func();
401         func.new_block(name)
402     }
403
404     fn switch_to_block(&mut self, block: Self::BasicBlock) {
405         self.block = block;
406     }
407
408     fn ret_void(&mut self) {
409         self.llbb().end_with_void_return(None)
410     }
411
412     fn ret(&mut self, value: RValue<'gcc>) {
413         let value =
414             if self.structs_as_pointer.borrow().contains(&value) {
415                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
416                 // CodegenCx.structs_as_pointer
417                 value.dereference(None).to_rvalue()
418             }
419             else {
420                 value
421             };
422         self.llbb().end_with_return(None, value);
423     }
424
425     fn br(&mut self, dest: Block<'gcc>) {
426         self.llbb().end_with_jump(None, dest)
427     }
428
429     fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
430         self.llbb().end_with_conditional(None, cond, then_block, else_block)
431     }
432
433     fn switch(&mut self, value: RValue<'gcc>, default_block: Block<'gcc>, cases: impl ExactSizeIterator<Item = (u128, Block<'gcc>)>) {
434         let mut gcc_cases = vec![];
435         let typ = self.val_ty(value);
436         for (on_val, dest) in cases {
437             let on_val = self.const_uint_big(typ, on_val);
438             gcc_cases.push(self.context.new_case(on_val, on_val, dest));
439         }
440         self.block.end_with_switch(None, value, default_block, &gcc_cases);
441     }
442
443     fn invoke(&mut self, typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
444         // TODO(bjorn3): Properly implement unwinding.
445         let call_site = self.call(typ, func, args, None);
446         let condition = self.context.new_rvalue_from_int(self.bool_type, 1);
447         self.llbb().end_with_conditional(None, condition, then, catch);
448         call_site
449     }
450
451     fn unreachable(&mut self) {
452         let func = self.context.get_builtin_function("__builtin_unreachable");
453         self.block.add_eval(None, self.context.new_call(None, func, &[]));
454         let return_type = self.block.get_function().get_return_type();
455         let void_type = self.context.new_type::<()>();
456         if return_type == void_type {
457             self.block.end_with_void_return(None)
458         }
459         else {
460             let return_value = self.current_func()
461                 .new_local(None, return_type, "unreachableReturn");
462             self.block.end_with_return(None, return_value)
463         }
464     }
465
466     fn add(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
467         self.gcc_add(a, b)
468     }
469
470     fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
471         a + b
472     }
473
474     fn sub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
475         self.gcc_sub(a, b)
476     }
477
478     fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
479         a - b
480     }
481
482     fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
483         self.gcc_mul(a, b)
484     }
485
486     fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
487         a * b
488     }
489
490     fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
491         self.gcc_udiv(a, b)
492     }
493
494     fn exactudiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
495         // TODO(antoyo): poison if not exact.
496         let a_type = a.get_type().to_unsigned(self);
497         let a = self.gcc_int_cast(a, a_type);
498         let b_type = b.get_type().to_unsigned(self);
499         let b = self.gcc_int_cast(b, b_type);
500         a / b
501     }
502
503     fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
504         self.gcc_sdiv(a, b)
505     }
506
507     fn exactsdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
508         // TODO(antoyo): poison if not exact.
509         // FIXME(antoyo): rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
510         // should be the same.
511         let typ = a.get_type().to_signed(self);
512         let b = self.context.new_cast(None, b, typ);
513         a / b
514     }
515
516     fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
517         a / b
518     }
519
520     fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
521         self.gcc_urem(a, b)
522     }
523
524     fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
525         self.gcc_srem(a, b)
526     }
527
528     fn frem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
529         if a.get_type().is_compatible_with(self.cx.float_type) {
530             let fmodf = self.context.get_builtin_function("fmodf");
531             // FIXME(antoyo): this seems to produce the wrong result.
532             return self.context.new_call(None, fmodf, &[a, b]);
533         }
534         assert_eq!(a.get_type().unqualified(), self.cx.double_type);
535
536         let fmod = self.context.get_builtin_function("fmod");
537         return self.context.new_call(None, fmod, &[a, b]);
538     }
539
540     fn shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
541         self.gcc_shl(a, b)
542     }
543
544     fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
545         self.gcc_lshr(a, b)
546     }
547
548     fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
549         // TODO(antoyo): check whether behavior is an arithmetic shift for >> .
550         // It seems to be if the value is signed.
551         self.gcc_lshr(a, b)
552     }
553
554     fn and(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
555         self.gcc_and(a, b)
556     }
557
558     fn or(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
559         self.cx.gcc_or(a, b)
560     }
561
562     fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
563         self.gcc_xor(a, b)
564     }
565
566     fn neg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
567         self.gcc_neg(a)
568     }
569
570     fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
571         self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
572     }
573
574     fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
575         self.gcc_not(a)
576     }
577
578     fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
579         a + b
580     }
581
582     fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
583         self.gcc_add(a, b)
584     }
585
586     fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
587         a - b
588     }
589
590     fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
591         // TODO(antoyo): should generate poison value?
592         self.gcc_sub(a, b)
593     }
594
595     fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
596         a * b
597     }
598
599     fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
600         a * b
601     }
602
603     fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
604         unimplemented!();
605     }
606
607     fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
608         unimplemented!();
609     }
610
611     fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
612         unimplemented!();
613     }
614
615     fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
616         unimplemented!();
617     }
618
619     fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
620         unimplemented!();
621     }
622
623     fn checked_binop(&mut self, oop: OverflowOp, typ: Ty<'_>, lhs: Self::Value, rhs: Self::Value) -> (Self::Value, Self::Value) {
624         self.gcc_checked_binop(oop, typ, lhs, rhs)
625     }
626
627     fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
628         // FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
629         // Ideally, we shouldn't need to do this check.
630         let aligned_type =
631             if ty == self.cx.u128_type || ty == self.cx.i128_type {
632                 ty
633             }
634             else {
635                 ty.get_aligned(align.bytes())
636             };
637         // TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
638         self.stack_var_count.set(self.stack_var_count.get() + 1);
639         self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
640     }
641
642     fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
643         unimplemented!();
644     }
645
646     fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
647         unimplemented!();
648     }
649
650     fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
651         let block = self.llbb();
652         let function = block.get_function();
653         // NOTE: instead of returning the dereference here, we have to assign it to a variable in
654         // the current basic block. Otherwise, it could be used in another basic block, causing a
655         // dereference after a drop, for instance.
656         // TODO(antoyo): handle align of the load instruction.
657         let ptr = self.context.new_cast(None, ptr, pointee_ty.make_pointer());
658         let deref = ptr.dereference(None).to_rvalue();
659         unsafe { RETURN_VALUE_COUNT += 1 };
660         let loaded_value = function.new_local(None, pointee_ty, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
661         block.add_assignment(None, loaded_value, deref);
662         loaded_value.to_rvalue()
663     }
664
665     fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
666         // TODO(antoyo): use ty.
667         let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
668         ptr.dereference(None).to_rvalue()
669     }
670
671     fn atomic_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) -> RValue<'gcc> {
672         // TODO(antoyo): use ty.
673         // TODO(antoyo): handle alignment.
674         let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
675         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
676
677         let volatile_const_void_ptr_type = self.context.new_type::<()>()
678             .make_const()
679             .make_volatile()
680             .make_pointer();
681         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
682         self.context.new_call(None, atomic_load, &[ptr, ordering])
683     }
684
685     fn load_operand(&mut self, place: PlaceRef<'tcx, RValue<'gcc>>) -> OperandRef<'tcx, RValue<'gcc>> {
686         assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
687
688         if place.layout.is_zst() {
689             return OperandRef::new_zst(self, place.layout);
690         }
691
692         fn scalar_load_metadata<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, load: RValue<'gcc>, scalar: &abi::Scalar) {
693             let vr = scalar.valid_range(bx);
694             match scalar.primitive() {
695                 abi::Int(..) => {
696                     if !scalar.is_always_valid(bx) {
697                         bx.range_metadata(load, vr);
698                     }
699                 }
700                 abi::Pointer if vr.start < vr.end && !vr.contains(0) => {
701                     bx.nonnull_metadata(load);
702                 }
703                 _ => {}
704             }
705         }
706
707         let val =
708             if let Some(llextra) = place.llextra {
709                 OperandValue::Ref(place.llval, Some(llextra), place.align)
710             }
711             else if place.layout.is_gcc_immediate() {
712                 let load = self.load(
713                     place.layout.gcc_type(self, false),
714                     place.llval,
715                     place.align,
716                 );
717                 if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
718                     scalar_load_metadata(self, load, scalar);
719                 }
720                 OperandValue::Immediate(self.to_immediate(load, place.layout))
721             }
722             else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
723                 let b_offset = a.size(self).align_to(b.align(self).abi);
724                 let pair_type = place.layout.gcc_type(self, false);
725
726                 let mut load = |i, scalar: &abi::Scalar, align| {
727                     let llptr = self.struct_gep(pair_type, place.llval, i as u64);
728                     let llty = place.layout.scalar_pair_element_gcc_type(self, i, false);
729                     let load = self.load(llty, llptr, align);
730                     scalar_load_metadata(self, load, scalar);
731                     if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
732                 };
733
734                 OperandValue::Pair(
735                     load(0, a, place.align),
736                     load(1, b, place.align.restrict_for_offset(b_offset)),
737                 )
738             }
739             else {
740                 OperandValue::Ref(place.llval, None, place.align)
741             };
742
743         OperandRef { val, layout: place.layout }
744     }
745
746     fn write_operand_repeatedly(mut self, cg_elem: OperandRef<'tcx, RValue<'gcc>>, count: u64, dest: PlaceRef<'tcx, RValue<'gcc>>) -> Self {
747         let zero = self.const_usize(0);
748         let count = self.const_usize(count);
749         let start = dest.project_index(&mut self, zero).llval;
750         let end = dest.project_index(&mut self, count).llval;
751
752         let header_bb = self.append_sibling_block("repeat_loop_header");
753         let body_bb = self.append_sibling_block("repeat_loop_body");
754         let next_bb = self.append_sibling_block("repeat_loop_next");
755
756         let ptr_type = start.get_type();
757         let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
758         let current_val = current.to_rvalue();
759         self.assign(current, start);
760
761         self.br(header_bb);
762
763         self.switch_to_block(header_bb);
764         let keep_going = self.icmp(IntPredicate::IntNE, current_val, end);
765         self.cond_br(keep_going, body_bb, next_bb);
766
767         self.switch_to_block(body_bb);
768         let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
769         cg_elem.val.store(&mut self, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
770
771         let next = self.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
772         self.llbb().add_assignment(None, current, next);
773         self.br(header_bb);
774
775         self.switch_to_block(next_bb);
776         self
777     }
778
779     fn range_metadata(&mut self, _load: RValue<'gcc>, _range: WrappingRange) {
780         // TODO(antoyo)
781     }
782
783     fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
784         // TODO(antoyo)
785     }
786
787     fn type_metadata(&mut self, _function: RValue<'gcc>, _typeid: String) {
788         // Unsupported.
789     }
790
791     fn typeid_metadata(&mut self, _typeid: String) -> RValue<'gcc> {
792         // Unsupported.
793         self.context.new_rvalue_from_int(self.int_type, 0)
794     }
795
796
797     fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
798         self.store_with_flags(val, ptr, align, MemFlags::empty())
799     }
800
801     fn store_with_flags(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align, _flags: MemFlags) -> RValue<'gcc> {
802         let ptr = self.check_store(val, ptr);
803         let destination = ptr.dereference(None);
804         // NOTE: libgccjit does not support specifying the alignment on the assignment, so we cast
805         // to type so it gets the proper alignment.
806         let destination_type = destination.to_rvalue().get_type().unqualified();
807         let aligned_type = destination_type.get_aligned(align.bytes()).make_pointer();
808         let aligned_destination = self.cx.context.new_bitcast(None, ptr, aligned_type);
809         let aligned_destination = aligned_destination.dereference(None);
810         self.llbb().add_assignment(None, aligned_destination, val);
811         // TODO(antoyo): handle align and flags.
812         // NOTE: dummy value here since it's never used. FIXME(antoyo): API should not return a value here?
813         self.cx.context.new_rvalue_zero(self.type_i32())
814     }
815
816     fn atomic_store(&mut self, value: RValue<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) {
817         // TODO(antoyo): handle alignment.
818         let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
819         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
820         let volatile_const_void_ptr_type = self.context.new_type::<()>()
821             .make_volatile()
822             .make_pointer();
823         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
824
825         // FIXME(antoyo): fix libgccjit to allow comparing an integer type with an aligned integer type because
826         // the following cast is required to avoid this error:
827         // gcc_jit_context_new_call: mismatching types for argument 2 of function "__atomic_store_4": assignment to param arg1 (type: int) from loadedValue3577 (type: unsigned int  __attribute__((aligned(4))))
828         let int_type = atomic_store.get_param(1).to_rvalue().get_type();
829         let value = self.context.new_cast(None, value, int_type);
830         self.llbb()
831             .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
832     }
833
834     fn gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
835         let mut result = ptr;
836         for index in indices {
837             result = self.context.new_array_access(None, result, *index).get_address(None).to_rvalue();
838         }
839         result
840     }
841
842     fn inbounds_gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
843         // FIXME(antoyo): would be safer if doing the same thing (loop) as gep.
844         // TODO(antoyo): specify inbounds somehow.
845         match indices.len() {
846             1 => {
847                 self.context.new_array_access(None, ptr, indices[0]).get_address(None)
848             },
849             2 => {
850                 let array = ptr.dereference(None); // TODO(antoyo): assert that first index is 0?
851                 self.context.new_array_access(None, array, indices[1]).get_address(None)
852             },
853             _ => unimplemented!(),
854         }
855     }
856
857     fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
858         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
859         assert_eq!(idx as usize as u64, idx);
860         let value = ptr.dereference(None).to_rvalue();
861
862         if value_type.dyncast_array().is_some() {
863             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
864             let element = self.context.new_array_access(None, value, index);
865             element.get_address(None)
866         }
867         else if let Some(vector_type) = value_type.dyncast_vector() {
868             let array_type = vector_type.get_element_type().make_pointer();
869             let array = self.bitcast(ptr, array_type);
870             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
871             let element = self.context.new_array_access(None, array, index);
872             element.get_address(None)
873         }
874         else if let Some(struct_type) = value_type.is_struct() {
875             ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
876         }
877         else {
878             panic!("Unexpected type {:?}", value_type);
879         }
880     }
881
882     /* Casts */
883     fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
884         // TODO(antoyo): check that it indeed truncate the value.
885         self.gcc_int_cast(value, dest_ty)
886     }
887
888     fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
889         // TODO(antoyo): check that it indeed sign extend the value.
890         if dest_ty.dyncast_vector().is_some() {
891             // TODO(antoyo): nothing to do as it is only for LLVM?
892             return value;
893         }
894         self.context.new_cast(None, value, dest_ty)
895     }
896
897     fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
898         self.gcc_float_to_uint_cast(value, dest_ty)
899     }
900
901     fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
902         self.gcc_float_to_int_cast(value, dest_ty)
903     }
904
905     fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
906         self.gcc_uint_to_float_cast(value, dest_ty)
907     }
908
909     fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
910         self.gcc_int_to_float_cast(value, dest_ty)
911     }
912
913     fn fptrunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
914         // TODO(antoyo): make sure it truncates.
915         self.context.new_cast(None, value, dest_ty)
916     }
917
918     fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
919         self.context.new_cast(None, value, dest_ty)
920     }
921
922     fn ptrtoint(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
923         let usize_value = self.cx.const_bitcast(value, self.cx.type_isize());
924         self.intcast(usize_value, dest_ty, false)
925     }
926
927     fn inttoptr(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
928         let usize_value = self.intcast(value, self.cx.type_isize(), false);
929         self.cx.const_bitcast(usize_value, dest_ty)
930     }
931
932     fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
933         self.cx.const_bitcast(value, dest_ty)
934     }
935
936     fn intcast(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>, _is_signed: bool) -> RValue<'gcc> {
937         // NOTE: is_signed is for value, not dest_typ.
938         self.gcc_int_cast(value, dest_typ)
939     }
940
941     fn pointercast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
942         let val_type = value.get_type();
943         match (type_is_pointer(val_type), type_is_pointer(dest_ty)) {
944             (false, true) => {
945                 // NOTE: Projecting a field of a pointer type will attempt a cast from a signed char to
946                 // a pointer, which is not supported by gccjit.
947                 return self.cx.context.new_cast(None, self.inttoptr(value, val_type.make_pointer()), dest_ty);
948             },
949             (false, false) => {
950                 // When they are not pointers, we want a transmute (or reinterpret_cast).
951                 self.bitcast(value, dest_ty)
952             },
953             (true, true) => self.cx.context.new_cast(None, value, dest_ty),
954             (true, false) => unimplemented!(),
955         }
956     }
957
958     /* Comparisons */
959     fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
960         self.gcc_icmp(op, lhs, rhs)
961     }
962
963     fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
964         self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
965     }
966
967     /* Miscellaneous instructions */
968     fn memcpy(&mut self, dst: RValue<'gcc>, _dst_align: Align, src: RValue<'gcc>, _src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
969         assert!(!flags.contains(MemFlags::NONTEMPORAL), "non-temporal memcpy not supported");
970         let size = self.intcast(size, self.type_size_t(), false);
971         let _is_volatile = flags.contains(MemFlags::VOLATILE);
972         let dst = self.pointercast(dst, self.type_i8p());
973         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
974         let memcpy = self.context.get_builtin_function("memcpy");
975         // TODO(antoyo): handle aligns and is_volatile.
976         self.block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
977     }
978
979     fn memmove(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
980         if flags.contains(MemFlags::NONTEMPORAL) {
981             // HACK(nox): This is inefficient but there is no nontemporal memmove.
982             let val = self.load(src.get_type().get_pointee().expect("get_pointee"), src, src_align);
983             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
984             self.store_with_flags(val, ptr, dst_align, flags);
985             return;
986         }
987         let size = self.intcast(size, self.type_size_t(), false);
988         let _is_volatile = flags.contains(MemFlags::VOLATILE);
989         let dst = self.pointercast(dst, self.type_i8p());
990         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
991
992         let memmove = self.context.get_builtin_function("memmove");
993         // TODO(antoyo): handle is_volatile.
994         self.block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
995     }
996
997     fn memset(&mut self, ptr: RValue<'gcc>, fill_byte: RValue<'gcc>, size: RValue<'gcc>, _align: Align, flags: MemFlags) {
998         let _is_volatile = flags.contains(MemFlags::VOLATILE);
999         let ptr = self.pointercast(ptr, self.type_i8p());
1000         let memset = self.context.get_builtin_function("memset");
1001         // TODO(antoyo): handle align and is_volatile.
1002         let fill_byte = self.context.new_cast(None, fill_byte, self.i32_type);
1003         let size = self.intcast(size, self.type_size_t(), false);
1004         self.block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
1005     }
1006
1007     fn select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, mut else_val: RValue<'gcc>) -> RValue<'gcc> {
1008         let func = self.current_func();
1009         let variable = func.new_local(None, then_val.get_type(), "selectVar");
1010         let then_block = func.new_block("then");
1011         let else_block = func.new_block("else");
1012         let after_block = func.new_block("after");
1013         self.llbb().end_with_conditional(None, cond, then_block, else_block);
1014
1015         then_block.add_assignment(None, variable, then_val);
1016         then_block.end_with_jump(None, after_block);
1017
1018         if !then_val.get_type().is_compatible_with(else_val.get_type()) {
1019             else_val = self.context.new_cast(None, else_val, then_val.get_type());
1020         }
1021         else_block.add_assignment(None, variable, else_val);
1022         else_block.end_with_jump(None, after_block);
1023
1024         // NOTE: since jumps were added in a place rustc does not expect, the current block in the
1025         // state need to be updated.
1026         self.switch_to_block(after_block);
1027
1028         variable.to_rvalue()
1029     }
1030
1031     #[allow(dead_code)]
1032     fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
1033         unimplemented!();
1034     }
1035
1036     fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
1037         unimplemented!();
1038     }
1039
1040     fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
1041         unimplemented!();
1042     }
1043
1044     fn extract_value(&mut self, aggregate_value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1045         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
1046         assert_eq!(idx as usize as u64, idx);
1047         let value_type = aggregate_value.get_type();
1048
1049         if value_type.dyncast_array().is_some() {
1050             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1051             let element = self.context.new_array_access(None, aggregate_value, index);
1052             element.get_address(None)
1053         }
1054         else if value_type.dyncast_vector().is_some() {
1055             panic!();
1056         }
1057         else if let Some(pointer_type) = value_type.get_pointee() {
1058             if let Some(struct_type) = pointer_type.is_struct() {
1059                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
1060                 // CodegenCx.structs_as_pointer
1061                 aggregate_value.dereference_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1062             }
1063             else {
1064                 panic!("Unexpected type {:?}", value_type);
1065             }
1066         }
1067         else if let Some(struct_type) = value_type.is_struct() {
1068             aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1069         }
1070         else {
1071             panic!("Unexpected type {:?}", value_type);
1072         }
1073     }
1074
1075     fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1076         // FIXME(antoyo): it would be better if the API only called this on struct, not on arrays.
1077         assert_eq!(idx as usize as u64, idx);
1078         let value_type = aggregate_value.get_type();
1079
1080         let lvalue =
1081             if value_type.dyncast_array().is_some() {
1082                 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1083                 self.context.new_array_access(None, aggregate_value, index)
1084             }
1085             else if value_type.dyncast_vector().is_some() {
1086                 panic!();
1087             }
1088             else if let Some(pointer_type) = value_type.get_pointee() {
1089                 if let Some(struct_type) = pointer_type.is_struct() {
1090                     // NOTE: hack to workaround a limitation of the rustc API: see comment on
1091                     // CodegenCx.structs_as_pointer
1092                     aggregate_value.dereference_field(None, struct_type.get_field(idx as i32))
1093                 }
1094                 else {
1095                     panic!("Unexpected type {:?}", value_type);
1096                 }
1097             }
1098             else {
1099                 panic!("Unexpected type {:?}", value_type);
1100             };
1101
1102         let lvalue_type = lvalue.to_rvalue().get_type();
1103         let value =
1104             // NOTE: sometimes, rustc will create a value with the wrong type.
1105             if lvalue_type != value.get_type() {
1106                 self.context.new_cast(None, value, lvalue_type)
1107             }
1108             else {
1109                 value
1110             };
1111
1112         self.llbb().add_assignment(None, lvalue, value);
1113
1114         aggregate_value
1115     }
1116
1117     fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1118         // TODO(antoyo)
1119     }
1120
1121     fn cleanup_landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>) -> RValue<'gcc> {
1122         let field1 = self.context.new_field(None, self.u8_type.make_pointer(), "landing_pad_field_1");
1123         let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
1124         let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1125         self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
1126             .to_rvalue()
1127         // TODO(antoyo): Properly implement unwinding.
1128         // the above is just to make the compilation work as it seems
1129         // rustc_codegen_ssa now calls the unwinding builder methods even on panic=abort.
1130     }
1131
1132     fn resume(&mut self, _exn: RValue<'gcc>) {
1133         // TODO(bjorn3): Properly implement unwinding.
1134         self.unreachable();
1135     }
1136
1137     fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
1138         unimplemented!();
1139     }
1140
1141     fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) {
1142         unimplemented!();
1143     }
1144
1145     fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
1146         unimplemented!();
1147     }
1148
1149     fn catch_switch(
1150         &mut self,
1151         _parent: Option<RValue<'gcc>>,
1152         _unwind: Option<Block<'gcc>>,
1153         _handlers: &[Block<'gcc>],
1154     ) -> RValue<'gcc> {
1155         unimplemented!();
1156     }
1157
1158     // Atomic Operations
1159     fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
1160         let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
1161         self.llbb().add_assignment(None, expected, cmp);
1162         let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);
1163
1164         let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
1165         let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
1166         let align = Align::from_bits(64).expect("align"); // TODO(antoyo): use good align.
1167
1168         let value_type = result.to_rvalue().get_type();
1169         if let Some(struct_type) = value_type.is_struct() {
1170             self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
1171             // NOTE: since success contains the call to the intrinsic, it must be stored before
1172             // expected so that we store expected after the call.
1173             self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
1174         }
1175         // TODO(antoyo): handle when value is not a struct.
1176
1177         result.to_rvalue()
1178     }
1179
1180     fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
1181         let size = src.get_type().get_size();
1182         let name =
1183             match op {
1184                 AtomicRmwBinOp::AtomicXchg => format!("__atomic_exchange_{}", size),
1185                 AtomicRmwBinOp::AtomicAdd => format!("__atomic_fetch_add_{}", size),
1186                 AtomicRmwBinOp::AtomicSub => format!("__atomic_fetch_sub_{}", size),
1187                 AtomicRmwBinOp::AtomicAnd => format!("__atomic_fetch_and_{}", size),
1188                 AtomicRmwBinOp::AtomicNand => format!("__atomic_fetch_nand_{}", size),
1189                 AtomicRmwBinOp::AtomicOr => format!("__atomic_fetch_or_{}", size),
1190                 AtomicRmwBinOp::AtomicXor => format!("__atomic_fetch_xor_{}", size),
1191                 AtomicRmwBinOp::AtomicMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1192                 AtomicRmwBinOp::AtomicMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1193                 AtomicRmwBinOp::AtomicUMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1194                 AtomicRmwBinOp::AtomicUMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1195             };
1196
1197
1198         let atomic_function = self.context.get_builtin_function(name);
1199         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1200
1201         let void_ptr_type = self.context.new_type::<*mut ()>();
1202         let volatile_void_ptr_type = void_ptr_type.make_volatile();
1203         let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
1204         // FIXME(antoyo): not sure why, but we have the wrong type here.
1205         let new_src_type = atomic_function.get_param(1).to_rvalue().get_type();
1206         let src = self.context.new_cast(None, src, new_src_type);
1207         let res = self.context.new_call(None, atomic_function, &[dst, src, order]);
1208         self.context.new_cast(None, res, src.get_type())
1209     }
1210
1211     fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
1212         let name =
1213             match scope {
1214                 SynchronizationScope::SingleThread => "__atomic_signal_fence",
1215                 SynchronizationScope::CrossThread => "__atomic_thread_fence",
1216             };
1217         let thread_fence = self.context.get_builtin_function(name);
1218         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1219         self.llbb().add_eval(None, self.context.new_call(None, thread_fence, &[order]));
1220     }
1221
1222     fn set_invariant_load(&mut self, load: RValue<'gcc>) {
1223         // NOTE: Hack to consider vtable function pointer as non-global-variable function pointer.
1224         self.normal_function_addresses.borrow_mut().insert(load);
1225         // TODO(antoyo)
1226     }
1227
1228     fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1229         // TODO(antoyo)
1230     }
1231
1232     fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1233         // TODO(antoyo)
1234     }
1235
1236     fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
1237         // FIXME(antoyo): remove when having a proper API.
1238         let gcc_func = unsafe { std::mem::transmute(func) };
1239         if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1240             self.function_call(func, args, funclet)
1241         }
1242         else {
1243             // If it's a not function that was defined, it's a function pointer.
1244             self.function_ptr_call(func, args, funclet)
1245         }
1246     }
1247
1248     fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
1249         // FIXME(antoyo): this does not zero-extend.
1250         if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
1251             // FIXME(antoyo): hack because base::from_immediate converts i1 to i8.
1252             // Fix the code in codegen_ssa::base::from_immediate.
1253             return value;
1254         }
1255         self.gcc_int_cast(value, dest_typ)
1256     }
1257
1258     fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
1259         self.cx
1260     }
1261
1262     fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1263         // FIXME(bjorn3): implement
1264     }
1265
1266     fn set_span(&mut self, _span: Span) {}
1267
1268     fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
1269         if self.cx().val_ty(val) == self.cx().type_i1() {
1270             self.zext(val, self.cx().type_i8())
1271         }
1272         else {
1273             val
1274         }
1275     }
1276
1277     fn to_immediate_scalar(&mut self, val: Self::Value, scalar: abi::Scalar) -> Self::Value {
1278         if scalar.is_bool() {
1279             return self.trunc(val, self.cx().type_i1());
1280         }
1281         val
1282     }
1283
1284     fn fptoui_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1285         None
1286     }
1287
1288     fn fptosi_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1289         None
1290     }
1291
1292     fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
1293         unimplemented!();
1294     }
1295 }
1296
1297 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
1298     #[cfg(feature="master")]
1299     pub fn shuffle_vector(&mut self, v1: RValue<'gcc>, v2: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
1300         let struct_type = mask.get_type().is_struct().expect("mask of struct type");
1301
1302         // TODO(antoyo): use a recursive unqualified() here.
1303         let vector_type = v1.get_type().unqualified().dyncast_vector().expect("vector type");
1304         let element_type = vector_type.get_element_type();
1305         let vec_num_units = vector_type.get_num_units();
1306
1307         let mask_num_units = struct_type.get_field_count();
1308         let mut vector_elements = vec![];
1309         let mask_element_type =
1310             if element_type.is_integral() {
1311                 element_type
1312             }
1313             else {
1314                 #[cfg(feature="master")]
1315                 {
1316                     self.cx.type_ix(element_type.get_size() as u64 * 8)
1317                 }
1318                 #[cfg(not(feature="master"))]
1319                 self.int_type
1320             };
1321         for i in 0..mask_num_units {
1322             let field = struct_type.get_field(i as i32);
1323             vector_elements.push(self.context.new_cast(None, mask.access_field(None, field).to_rvalue(), mask_element_type));
1324         }
1325
1326         // NOTE: the mask needs to be the same length as the input vectors, so add the missing
1327         // elements in the mask if needed.
1328         for _ in mask_num_units..vec_num_units {
1329             vector_elements.push(self.context.new_rvalue_zero(mask_element_type));
1330         }
1331
1332         let array_type = self.context.new_array_type(None, element_type, vec_num_units as i32);
1333         let result_type = self.context.new_vector_type(element_type, mask_num_units as u64);
1334         let (v1, v2) =
1335             if vec_num_units < mask_num_units {
1336                 // NOTE: the mask needs to be the same length as the input vectors, so join the 2
1337                 // vectors and create a dummy second vector.
1338                 // TODO(antoyo): switch to using new_vector_access.
1339                 let array = self.context.new_bitcast(None, v1, array_type);
1340                 let mut elements = vec![];
1341                 for i in 0..vec_num_units {
1342                     elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1343                 }
1344                 // TODO(antoyo): switch to using new_vector_access.
1345                 let array = self.context.new_bitcast(None, v2, array_type);
1346                 for i in 0..(mask_num_units - vec_num_units) {
1347                     elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1348                 }
1349                 let v1 = self.context.new_rvalue_from_vector(None, result_type, &elements);
1350                 let zero = self.context.new_rvalue_zero(element_type);
1351                 let v2 = self.context.new_rvalue_from_vector(None, result_type, &vec![zero; mask_num_units]);
1352                 (v1, v2)
1353             }
1354             else {
1355                 (v1, v2)
1356             };
1357
1358         let new_mask_num_units = std::cmp::max(mask_num_units, vec_num_units);
1359         let mask_type = self.context.new_vector_type(mask_element_type, new_mask_num_units as u64);
1360         let mask = self.context.new_rvalue_from_vector(None, mask_type, &vector_elements);
1361         let result = self.context.new_rvalue_vector_perm(None, v1, v2, mask);
1362
1363         if vec_num_units != mask_num_units {
1364             // NOTE: if padding was added, only select the number of elements of the masks to
1365             // remove that padding in the result.
1366             let mut elements = vec![];
1367             // TODO(antoyo): switch to using new_vector_access.
1368             let array = self.context.new_bitcast(None, result, array_type);
1369             for i in 0..mask_num_units {
1370                 elements.push(self.context.new_array_access(None, array, self.context.new_rvalue_from_int(self.int_type, i as i32)).to_rvalue());
1371             }
1372             self.context.new_rvalue_from_vector(None, result_type, &elements)
1373         }
1374         else {
1375             result
1376         }
1377     }
1378
1379     #[cfg(not(feature="master"))]
1380     pub fn shuffle_vector(&mut self, _v1: RValue<'gcc>, _v2: RValue<'gcc>, _mask: RValue<'gcc>) -> RValue<'gcc> {
1381         unimplemented!();
1382     }
1383
1384     #[cfg(feature="master")]
1385     pub fn vector_reduce<F>(&mut self, src: RValue<'gcc>, op: F) -> RValue<'gcc>
1386     where F: Fn(RValue<'gcc>, RValue<'gcc>, &'gcc Context<'gcc>) -> RValue<'gcc>
1387     {
1388         let vector_type = src.get_type().unqualified().dyncast_vector().expect("vector type");
1389         let element_count = vector_type.get_num_units();
1390         let mut vector_elements = vec![];
1391         for i in 0..element_count {
1392             vector_elements.push(i);
1393         }
1394         let mask_type = self.context.new_vector_type(self.int_type, element_count as u64);
1395         let mut shift = 1;
1396         let mut res = src;
1397         while shift < element_count {
1398             let vector_elements: Vec<_> =
1399                 vector_elements.iter()
1400                     .map(|i| self.context.new_rvalue_from_int(self.int_type, ((i + shift) % element_count) as i32))
1401                     .collect();
1402             let mask = self.context.new_rvalue_from_vector(None, mask_type, &vector_elements);
1403             let shifted = self.context.new_rvalue_vector_perm(None, res, res, mask);
1404             shift *= 2;
1405             res = op(res, shifted, &self.context);
1406         }
1407         self.context.new_vector_access(None, res, self.context.new_rvalue_zero(self.int_type))
1408             .to_rvalue()
1409     }
1410
1411     #[cfg(not(feature="master"))]
1412     pub fn vector_reduce<F>(&mut self, src: RValue<'gcc>, op: F) -> RValue<'gcc>
1413     where F: Fn(RValue<'gcc>, RValue<'gcc>, &'gcc Context<'gcc>) -> RValue<'gcc>
1414     {
1415         unimplemented!();
1416     }
1417
1418     pub fn vector_reduce_op(&mut self, src: RValue<'gcc>, op: BinaryOp) -> RValue<'gcc> {
1419         self.vector_reduce(src, |a, b, context| context.new_binary_op(None, op, a.get_type(), a, b))
1420     }
1421
1422     pub fn vector_reduce_fadd_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1423         unimplemented!();
1424     }
1425
1426     pub fn vector_reduce_fmul_fast(&mut self, _acc: RValue<'gcc>, _src: RValue<'gcc>) -> RValue<'gcc> {
1427         unimplemented!();
1428     }
1429
1430     // Inspired by Hacker's Delight min implementation.
1431     pub fn vector_reduce_min(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
1432         self.vector_reduce(src, |a, b, context| {
1433             let differences_or_zeros = difference_or_zero(a, b, context);
1434             context.new_binary_op(None, BinaryOp::Minus, a.get_type(), a, differences_or_zeros)
1435         })
1436     }
1437
1438     // Inspired by Hacker's Delight max implementation.
1439     pub fn vector_reduce_max(&mut self, src: RValue<'gcc>) -> RValue<'gcc> {
1440         self.vector_reduce(src, |a, b, context| {
1441             let differences_or_zeros = difference_or_zero(a, b, context);
1442             context.new_binary_op(None, BinaryOp::Plus, b.get_type(), b, differences_or_zeros)
1443         })
1444     }
1445
1446     pub fn vector_select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, else_val: RValue<'gcc>) -> RValue<'gcc> {
1447         // cond is a vector of integers, not of bools.
1448         let cond_type = cond.get_type();
1449         let vector_type = cond_type.unqualified().dyncast_vector().expect("vector type");
1450         let num_units = vector_type.get_num_units();
1451         let element_type = vector_type.get_element_type();
1452         let zeros = vec![self.context.new_rvalue_zero(element_type); num_units];
1453         let zeros = self.context.new_rvalue_from_vector(None, cond_type, &zeros);
1454
1455         let masks = self.context.new_comparison(None, ComparisonOp::NotEquals, cond, zeros);
1456         let then_vals = masks & then_val;
1457
1458         let ones = vec![self.context.new_rvalue_one(element_type); num_units];
1459         let ones = self.context.new_rvalue_from_vector(None, cond_type, &ones);
1460         let inverted_masks = masks + ones;
1461         // NOTE: sometimes, the type of else_val can be different than the type of then_val in
1462         // libgccjit (vector of int vs vector of int32_t), but they should be the same for the AND
1463         // operation to work.
1464         let else_val = self.context.new_bitcast(None, else_val, then_val.get_type());
1465         let else_vals = inverted_masks & else_val;
1466
1467         then_vals | else_vals
1468     }
1469 }
1470
1471 fn difference_or_zero<'gcc>(a: RValue<'gcc>, b: RValue<'gcc>, context: &'gcc Context<'gcc>) -> RValue<'gcc> {
1472     let difference = a - b;
1473     let masks = context.new_comparison(None, ComparisonOp::GreaterThanEquals, b, a);
1474     difference & masks
1475 }
1476
1477 impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
1478     fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
1479         // Forward to the `get_static` method of `CodegenCx`
1480         self.cx().get_static(def_id).get_address(None)
1481     }
1482 }
1483
1484 impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
1485     fn param_env(&self) -> ParamEnv<'tcx> {
1486         self.cx.param_env()
1487     }
1488 }
1489
1490 impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
1491     fn target_spec(&self) -> &Target {
1492         &self.cx.target_spec()
1493     }
1494 }
1495
1496 pub trait ToGccComp {
1497     fn to_gcc_comparison(&self) -> ComparisonOp;
1498 }
1499
1500 impl ToGccComp for IntPredicate {
1501     fn to_gcc_comparison(&self) -> ComparisonOp {
1502         match *self {
1503             IntPredicate::IntEQ => ComparisonOp::Equals,
1504             IntPredicate::IntNE => ComparisonOp::NotEquals,
1505             IntPredicate::IntUGT => ComparisonOp::GreaterThan,
1506             IntPredicate::IntUGE => ComparisonOp::GreaterThanEquals,
1507             IntPredicate::IntULT => ComparisonOp::LessThan,
1508             IntPredicate::IntULE => ComparisonOp::LessThanEquals,
1509             IntPredicate::IntSGT => ComparisonOp::GreaterThan,
1510             IntPredicate::IntSGE => ComparisonOp::GreaterThanEquals,
1511             IntPredicate::IntSLT => ComparisonOp::LessThan,
1512             IntPredicate::IntSLE => ComparisonOp::LessThanEquals,
1513         }
1514     }
1515 }
1516
1517 impl ToGccComp for RealPredicate {
1518     fn to_gcc_comparison(&self) -> ComparisonOp {
1519         // TODO(antoyo): check that ordered vs non-ordered is respected.
1520         match *self {
1521             RealPredicate::RealPredicateFalse => unreachable!(),
1522             RealPredicate::RealOEQ => ComparisonOp::Equals,
1523             RealPredicate::RealOGT => ComparisonOp::GreaterThan,
1524             RealPredicate::RealOGE => ComparisonOp::GreaterThanEquals,
1525             RealPredicate::RealOLT => ComparisonOp::LessThan,
1526             RealPredicate::RealOLE => ComparisonOp::LessThanEquals,
1527             RealPredicate::RealONE => ComparisonOp::NotEquals,
1528             RealPredicate::RealORD => unreachable!(),
1529             RealPredicate::RealUNO => unreachable!(),
1530             RealPredicate::RealUEQ => ComparisonOp::Equals,
1531             RealPredicate::RealUGT => ComparisonOp::GreaterThan,
1532             RealPredicate::RealUGE => ComparisonOp::GreaterThan,
1533             RealPredicate::RealULT => ComparisonOp::LessThan,
1534             RealPredicate::RealULE => ComparisonOp::LessThan,
1535             RealPredicate::RealUNE => ComparisonOp::NotEquals,
1536             RealPredicate::RealPredicateTrue => unreachable!(),
1537         }
1538     }
1539 }
1540
1541 #[repr(C)]
1542 #[allow(non_camel_case_types)]
1543 enum MemOrdering {
1544     __ATOMIC_RELAXED,
1545     __ATOMIC_CONSUME,
1546     __ATOMIC_ACQUIRE,
1547     __ATOMIC_RELEASE,
1548     __ATOMIC_ACQ_REL,
1549     __ATOMIC_SEQ_CST,
1550 }
1551
1552 trait ToGccOrdering {
1553     fn to_gcc(self) -> i32;
1554 }
1555
1556 impl ToGccOrdering for AtomicOrdering {
1557     fn to_gcc(self) -> i32 {
1558         use MemOrdering::*;
1559
1560         let ordering =
1561             match self {
1562                 AtomicOrdering::Unordered => __ATOMIC_RELAXED,
1563                 AtomicOrdering::Relaxed => __ATOMIC_RELAXED, // TODO(antoyo): check if that's the same.
1564                 AtomicOrdering::Acquire => __ATOMIC_ACQUIRE,
1565                 AtomicOrdering::Release => __ATOMIC_RELEASE,
1566                 AtomicOrdering::AcquireRelease => __ATOMIC_ACQ_REL,
1567                 AtomicOrdering::SequentiallyConsistent => __ATOMIC_SEQ_CST,
1568             };
1569         ordering as i32
1570     }
1571 }