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