]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/builder.rs
Merge commit '0c89065b934397b62838fe3e4ef6f6352fc52daf' 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
52 type Funclet = ();
53
54 // TODO: 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: 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 fn_ty = self.cx.val_ty(func);
166         // Strip off pointers
167         /*while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
168             fn_ty = self.cx.element_type(fn_ty);
169         }*/
170
171         /*assert!(
172             self.cx.type_kind(fn_ty) == TypeKind::Function,
173             "builder::{} not passed a function, but {:?}",
174             typ,
175             fn_ty
176         );
177
178         let param_tys = self.cx.func_params_types(fn_ty);
179
180         let all_args_match = param_tys
181             .iter()
182             .zip(args.iter().map(|&v| self.val_ty(v)))
183             .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);*/
184
185         let mut all_args_match = true;
186         let mut param_types = vec![];
187         let param_count = func.get_param_count();
188         for (index, arg) in args.iter().enumerate().take(param_count) {
189             let param = func.get_param(index as i32);
190             let param = param.to_rvalue().get_type();
191             if param != arg.get_type() {
192                 all_args_match = false;
193             }
194             param_types.push(param);
195         }
196
197         if all_args_match {
198             return Cow::Borrowed(args);
199         }
200
201         let casted_args: Vec<_> = param_types
202             .into_iter()
203             .zip(args.iter())
204             .enumerate()
205             .map(|(_i, (expected_ty, &actual_val))| {
206                 let actual_ty = actual_val.get_type();
207                 if expected_ty != actual_ty {
208                     /*debug!(
209                         "type mismatch in function call of {:?}. \
210                             Expected {:?} for param {}, got {:?}; injecting bitcast",
211                         func, expected_ty, i, actual_ty
212                     );*/
213                     /*println!(
214                         "type mismatch in function call of {:?}. \
215                             Expected {:?} for param {}, got {:?}; injecting bitcast",
216                         func, expected_ty, i, actual_ty
217                     );*/
218                     self.bitcast(actual_val, expected_ty)
219                 }
220                 else {
221                     actual_val
222                 }
223             })
224             .collect();
225
226         Cow::Owned(casted_args)
227     }
228
229     fn check_ptr_call<'b>(&mut self, _typ: &str, func_ptr: RValue<'gcc>, args: &'b [RValue<'gcc>]) -> Cow<'b, [RValue<'gcc>]> {
230         //let mut fn_ty = self.cx.val_ty(func);
231         // Strip off pointers
232         /*while self.cx.type_kind(fn_ty) == TypeKind::Pointer {
233             fn_ty = self.cx.element_type(fn_ty);
234         }*/
235
236         /*assert!(
237             self.cx.type_kind(fn_ty) == TypeKind::Function,
238             "builder::{} not passed a function, but {:?}",
239             typ,
240             fn_ty
241         );
242
243         let param_tys = self.cx.func_params_types(fn_ty);
244
245         let all_args_match = param_tys
246             .iter()
247             .zip(args.iter().map(|&v| self.val_ty(v)))
248             .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);*/
249
250         let mut all_args_match = true;
251         let mut param_types = vec![];
252         let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
253         for (index, arg) in args.iter().enumerate().take(gcc_func.get_param_count()) {
254             let param = gcc_func.get_param_type(index);
255             if param != arg.get_type() {
256                 all_args_match = false;
257             }
258             param_types.push(param);
259         }
260
261         if all_args_match {
262             return Cow::Borrowed(args);
263         }
264
265         let casted_args: Vec<_> = param_types
266             .into_iter()
267             .zip(args.iter())
268             .enumerate()
269             .map(|(_i, (expected_ty, &actual_val))| {
270                 let actual_ty = actual_val.get_type();
271                 if expected_ty != actual_ty {
272                     /*debug!(
273                         "type mismatch in function call of {:?}. \
274                             Expected {:?} for param {}, got {:?}; injecting bitcast",
275                         func, expected_ty, i, actual_ty
276                     );*/
277                     /*println!(
278                         "type mismatch in function call of {:?}. \
279                             Expected {:?} for param {}, got {:?}; injecting bitcast",
280                         func, expected_ty, i, actual_ty
281                     );*/
282                     self.bitcast(actual_val, expected_ty)
283                 }
284                 else {
285                     actual_val
286                 }
287             })
288             .collect();
289
290         Cow::Owned(casted_args)
291     }
292
293     fn check_store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
294         let dest_ptr_ty = self.cx.val_ty(ptr).make_pointer(); // TODO: make sure make_pointer() is okay here.
295         let stored_ty = self.cx.val_ty(val);
296         let stored_ptr_ty = self.cx.type_ptr_to(stored_ty);
297
298         //assert_eq!(self.cx.type_kind(dest_ptr_ty), TypeKind::Pointer);
299
300         if dest_ptr_ty == stored_ptr_ty {
301             ptr
302         }
303         else {
304             /*debug!(
305                 "type mismatch in store. \
306                     Expected {:?}, got {:?}; inserting bitcast",
307                 dest_ptr_ty, stored_ptr_ty
308             );*/
309             /*println!(
310                 "type mismatch in store. \
311                     Expected {:?}, got {:?}; inserting bitcast",
312                 dest_ptr_ty, stored_ptr_ty
313             );*/
314             //ptr
315             self.bitcast(ptr, stored_ptr_ty)
316         }
317     }
318
319     pub fn current_func(&self) -> Function<'gcc> {
320         self.block.expect("block").get_function()
321     }
322
323     fn function_call(&mut self, func: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
324         //debug!("call {:?} with args ({:?})", func, args);
325
326         // TODO: remove when the API supports a different type for functions.
327         let func: Function<'gcc> = self.cx.rvalue_as_function(func);
328         let args = self.check_call("call", func, args);
329         //let bundle = funclet.map(|funclet| funclet.bundle());
330         //let bundle = bundle.as_ref().map(|b| &*b.raw);
331
332         // gccjit requires to use the result of functions, even when it's not used.
333         // That's why we assign the result to a local or call add_eval().
334         let return_type = func.get_return_type();
335         let current_block = self.current_block.borrow().expect("block");
336         let void_type = self.context.new_type::<()>();
337         let current_func = current_block.get_function();
338         if return_type != void_type {
339             unsafe { RETURN_VALUE_COUNT += 1 };
340             let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
341             current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
342             result.to_rvalue()
343         }
344         else {
345             current_block.add_eval(None, self.cx.context.new_call(None, func, &args));
346             // Return dummy value when not having return value.
347             self.context.new_rvalue_from_long(self.isize_type, 0)
348         }
349     }
350
351     fn function_ptr_call(&mut self, func_ptr: RValue<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
352         //debug!("func ptr call {:?} with args ({:?})", func, args);
353
354         let args = self.check_ptr_call("call", func_ptr, args);
355         //let bundle = funclet.map(|funclet| funclet.bundle());
356         //let bundle = bundle.as_ref().map(|b| &*b.raw);
357
358         // gccjit requires to use the result of functions, even when it's not used.
359         // That's why we assign the result to a local or call add_eval().
360         let gcc_func = func_ptr.get_type().is_function_ptr_type().expect("function ptr");
361         let mut return_type = gcc_func.get_return_type();
362         let current_block = self.current_block.borrow().expect("block");
363         let void_type = self.context.new_type::<()>();
364         let current_func = current_block.get_function();
365
366         // FIXME: As a temporary workaround for unsupported LLVM intrinsics.
367         if gcc_func.get_param_count() == 0 && format!("{:?}", func_ptr) == "__builtin_ia32_pmovmskb128" {
368             return_type = self.int_type;
369         }
370
371         if return_type != void_type {
372             unsafe { RETURN_VALUE_COUNT += 1 };
373             let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
374             current_block.add_assignment(None, result, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
375             result.to_rvalue()
376         }
377         else {
378             if gcc_func.get_param_count() == 0 {
379                 // FIXME: As a temporary workaround for unsupported LLVM intrinsics.
380                 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &[]));
381             }
382             else {
383                 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
384             }
385             // Return dummy value when not having return value.
386             let result = current_func.new_local(None, self.isize_type, "dummyValueThatShouldNeverBeUsed");
387             current_block.add_assignment(None, result, self.context.new_rvalue_from_long(self.isize_type, 0));
388             result.to_rvalue()
389         }
390     }
391
392     pub fn overflow_call(&mut self, func: Function<'gcc>, args: &[RValue<'gcc>], _funclet: Option<&Funclet>) -> RValue<'gcc> {
393         //debug!("overflow_call {:?} with args ({:?})", func, args);
394
395         //let bundle = funclet.map(|funclet| funclet.bundle());
396         //let bundle = bundle.as_ref().map(|b| &*b.raw);
397
398         // gccjit requires to use the result of functions, even when it's not used.
399         // That's why we assign the result to a local.
400         let return_type = self.context.new_type::<bool>();
401         let current_block = self.current_block.borrow().expect("block");
402         let current_func = current_block.get_function();
403         // TODO: return the new_call() directly? Since the overflow function has no side-effects.
404         unsafe { RETURN_VALUE_COUNT += 1 };
405         let result = current_func.new_local(None, return_type, &format!("returnValue{}", unsafe { RETURN_VALUE_COUNT }));
406         current_block.add_assignment(None, result, self.cx.context.new_call(None, func, &args));
407         result.to_rvalue()
408     }
409 }
410
411 impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
412     type CodegenCx = CodegenCx<'gcc, 'tcx>;
413 }
414
415 impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
416     fn tcx(&self) -> TyCtxt<'tcx> {
417         self.cx.tcx()
418     }
419 }
420
421 impl HasDataLayout for Builder<'_, '_, '_> {
422     fn data_layout(&self) -> &TargetDataLayout {
423         self.cx.data_layout()
424     }
425 }
426
427 impl<'tcx> LayoutOf for Builder<'_, '_, 'tcx> {
428     type Ty = Ty<'tcx>;
429     type TyAndLayout = TyAndLayout<'tcx>;
430
431     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
432         self.cx.layout_of(ty)
433     }
434 }
435
436 impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
437     type Target = CodegenCx<'gcc, 'tcx>;
438
439     fn deref(&self) -> &Self::Target {
440         self.cx
441     }
442 }
443
444 impl<'gcc, 'tcx> BackendTypes for Builder<'_, 'gcc, 'tcx> {
445     type Value = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Value;
446     type Function = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Function;
447     type BasicBlock = <CodegenCx<'gcc, 'tcx> as BackendTypes>::BasicBlock;
448     type Type = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Type;
449     type Funclet = <CodegenCx<'gcc, 'tcx> as BackendTypes>::Funclet;
450
451     type DIScope = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIScope;
452     type DILocation = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DILocation;
453     type DIVariable = <CodegenCx<'gcc, 'tcx> as BackendTypes>::DIVariable;
454 }
455
456 impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
457     fn build(cx: &'a CodegenCx<'gcc, 'tcx>, block: Block<'gcc>) -> Self {
458         let mut bx = Builder::with_cx(cx);
459         *cx.current_block.borrow_mut() = Some(block);
460         bx.block = Some(block);
461         bx
462     }
463
464     fn build_sibling_block(&mut self, name: &str) -> Self {
465         let block = self.append_sibling_block(name);
466         Self::build(self.cx, block)
467     }
468
469     fn llbb(&self) -> Block<'gcc> {
470         self.block.expect("block")
471     }
472
473     fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
474         let func = cx.rvalue_as_function(func);
475         func.new_block(name)
476     }
477
478     fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
479         let func = self.current_func();
480         func.new_block(name)
481     }
482
483     fn ret_void(&mut self) {
484         self.llbb().end_with_void_return(None)
485     }
486
487     fn ret(&mut self, value: RValue<'gcc>) {
488         let value =
489             if self.structs_as_pointer.borrow().contains(&value) {
490                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
491                 // CodegenCx.structs_as_pointer
492                 value.dereference(None).to_rvalue()
493             }
494             else {
495                 value
496             };
497         self.llbb().end_with_return(None, value);
498     }
499
500     fn br(&mut self, dest: Block<'gcc>) {
501         self.llbb().end_with_jump(None, dest)
502     }
503
504     fn cond_br(&mut self, cond: RValue<'gcc>, then_block: Block<'gcc>, else_block: Block<'gcc>) {
505         self.llbb().end_with_conditional(None, cond, then_block, else_block)
506     }
507
508     fn switch(&mut self, value: RValue<'gcc>, default_block: Block<'gcc>, cases: impl ExactSizeIterator<Item = (u128, Block<'gcc>)>) {
509         let mut gcc_cases = vec![];
510         let typ = self.val_ty(value);
511         for (on_val, dest) in cases {
512             let on_val = self.const_uint_big(typ, on_val);
513             gcc_cases.push(self.context.new_case(on_val, on_val, dest));
514         }
515         self.block.expect("block").end_with_switch(None, value, default_block, &gcc_cases);
516     }
517
518     fn invoke(&mut self, _typ: Type<'gcc>, _func: RValue<'gcc>, _args: &[RValue<'gcc>], then: Block<'gcc>, catch: Block<'gcc>, _funclet: Option<&Funclet>) -> RValue<'gcc> {
519         let condition = self.context.new_rvalue_from_int(self.bool_type, 0);
520         self.llbb().end_with_conditional(None, condition, then, catch);
521         self.context.new_rvalue_from_int(self.int_type, 0)
522
523         // TODO
524         /*debug!("invoke {:?} with args ({:?})", func, args);
525
526         let args = self.check_call("invoke", func, args);
527         let bundle = funclet.map(|funclet| funclet.bundle());
528         let bundle = bundle.as_ref().map(|b| &*b.raw);
529
530         unsafe {
531             llvm::LLVMRustBuildInvoke(
532                 self.llbuilder,
533                 func,
534                 args.as_ptr(),
535                 args.len() as c_uint,
536                 then,
537                 catch,
538                 bundle,
539                 UNNAMED,
540             )
541         }*/
542     }
543
544     fn unreachable(&mut self) {
545         let func = self.context.get_builtin_function("__builtin_unreachable");
546         let block = self.block.expect("block");
547         block.add_eval(None, self.context.new_call(None, func, &[]));
548         let return_type = block.get_function().get_return_type();
549         let void_type = self.context.new_type::<()>();
550         if return_type == void_type {
551             block.end_with_void_return(None)
552         }
553         else {
554             let return_value = self.current_func()
555                 .new_local(None, return_type, "unreachableReturn");
556             block.end_with_return(None, return_value)
557         }
558     }
559
560     fn add(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
561         // FIXME: this should not be required.
562         if format!("{:?}", a.get_type()) != format!("{:?}", b.get_type()) {
563             b = self.context.new_cast(None, b, a.get_type());
564         }
565         a + b
566     }
567
568     fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
569         a + b
570     }
571
572     fn sub(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
573         if a.get_type() != b.get_type() {
574             b = self.context.new_cast(None, b, a.get_type());
575         }
576         a - b
577     }
578
579     fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
580         a - b
581     }
582
583     fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
584         a * b
585     }
586
587     fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
588         a * b
589     }
590
591     fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
592         // TODO: convert the arguments to unsigned?
593         a / b
594     }
595
596     fn exactudiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
597         // TODO: convert the arguments to unsigned?
598         // TODO: poison if not exact.
599         a / b
600     }
601
602     fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
603         // TODO: convert the arguments to signed?
604         a / b
605     }
606
607     fn exactsdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
608         // TODO: posion if not exact.
609         // FIXME: rustc_codegen_ssa::mir::intrinsic uses different types for a and b but they
610         // should be the same.
611         let typ = a.get_type().to_signed(self);
612         let a = self.context.new_cast(None, a, typ);
613         let b = self.context.new_cast(None, b, typ);
614         a / b
615     }
616
617     fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
618         a / b
619     }
620
621     fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
622         a % b
623     }
624
625     fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
626         a % b
627     }
628
629     fn frem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
630         if a.get_type() == self.cx.float_type {
631             let fmodf = self.context.get_builtin_function("fmodf");
632             // FIXME: this seems to produce the wrong result.
633             return self.context.new_call(None, fmodf, &[a, b]);
634         }
635         assert_eq!(a.get_type(), self.cx.double_type);
636
637         let fmod = self.context.get_builtin_function("fmod");
638         return self.context.new_call(None, fmod, &[a, b]);
639     }
640
641     fn shl(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
642         // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
643         let a_type = a.get_type();
644         let b_type = b.get_type();
645         if a_type.is_unsigned(self) && b_type.is_signed(self) {
646             //println!("shl: {:?} -> {:?}", a, b_type);
647             let a = self.context.new_cast(None, a, b_type);
648             let result = a << b;
649             //println!("shl: {:?} -> {:?}", result, a_type);
650             self.context.new_cast(None, result, a_type)
651         }
652         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
653             //println!("shl: {:?} -> {:?}", b, a_type);
654             let b = self.context.new_cast(None, b, a_type);
655             a << b
656         }
657         else {
658             a << b
659         }
660     }
661
662     fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
663         // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
664         // TODO: cast to unsigned to do a logical shift if that does not work.
665         let a_type = a.get_type();
666         let b_type = b.get_type();
667         if a_type.is_unsigned(self) && b_type.is_signed(self) {
668             //println!("lshl: {:?} -> {:?}", a, b_type);
669             let a = self.context.new_cast(None, a, b_type);
670             let result = a >> b;
671             //println!("lshl: {:?} -> {:?}", result, a_type);
672             self.context.new_cast(None, result, a_type)
673         }
674         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
675             //println!("lshl: {:?} -> {:?}", b, a_type);
676             let b = self.context.new_cast(None, b, a_type);
677             a >> b
678         }
679         else {
680             a >> b
681         }
682     }
683
684     fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
685         // TODO: check whether behavior is an arithmetic shift for >> .
686         // FIXME: remove the casts when libgccjit can shift an unsigned number by an unsigned number.
687         let a_type = a.get_type();
688         let b_type = b.get_type();
689         if a_type.is_unsigned(self) && b_type.is_signed(self) {
690             //println!("ashl: {:?} -> {:?}", a, b_type);
691             let a = self.context.new_cast(None, a, b_type);
692             let result = a >> b;
693             //println!("ashl: {:?} -> {:?}", result, a_type);
694             self.context.new_cast(None, result, a_type)
695         }
696         else if a_type.is_signed(self) && b_type.is_unsigned(self) {
697             //println!("ashl: {:?} -> {:?}", b, a_type);
698             let b = self.context.new_cast(None, b, a_type);
699             a >> b
700         }
701         else {
702             a >> b
703         }
704     }
705
706     fn and(&mut self, a: RValue<'gcc>, mut b: RValue<'gcc>) -> RValue<'gcc> {
707         // FIXME: hack by putting the result in a variable to workaround this bug:
708         // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=95498
709         if a.get_type() != b.get_type() {
710             b = self.context.new_cast(None, b, a.get_type());
711         }
712         let res = self.current_func().new_local(None, b.get_type(), "andResult");
713         self.llbb().add_assignment(None, res, a & b);
714         res.to_rvalue()
715     }
716
717     fn or(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
718         // FIXME: hack by putting the result in a variable to workaround this bug:
719         // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=95498
720         let res = self.current_func().new_local(None, b.get_type(), "orResult");
721         self.llbb().add_assignment(None, res, a | b);
722         res.to_rvalue()
723     }
724
725     fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
726         a ^ b
727     }
728
729     fn neg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
730         // TODO: use new_unary_op()?
731         self.cx.context.new_rvalue_from_long(a.get_type(), 0) - a
732     }
733
734     fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
735         self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
736     }
737
738     fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
739         let operation =
740             if a.get_type().is_bool() {
741                 UnaryOp::LogicalNegate
742             }
743             else {
744                 UnaryOp::BitwiseNegate
745             };
746         self.cx.context.new_unary_op(None, operation, a.get_type(), a)
747     }
748
749     fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
750         a + b
751     }
752
753     fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
754         a + b
755     }
756
757     fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
758         a - b
759     }
760
761     fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
762         // TODO: should generate poison value?
763         a - b
764     }
765
766     fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
767         a * b
768     }
769
770     fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
771         a * b
772     }
773
774     fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
775         unimplemented!();
776         /*unsafe {
777             let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
778             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
779             instr
780         }*/
781     }
782
783     fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
784         unimplemented!();
785         /*unsafe {
786             let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
787             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
788             instr
789         }*/
790     }
791
792     fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
793         unimplemented!();
794         /*unsafe {
795             let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
796             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
797             instr
798         }*/
799     }
800
801     fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
802         unimplemented!();
803         /*unsafe {
804             let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
805             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
806             instr
807         }*/
808     }
809
810     fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
811         unimplemented!();
812         /*unsafe {
813             let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
814             llvm::LLVMRustSetHasUnsafeAlgebra(instr);
815             instr
816         }*/
817     }
818
819     fn checked_binop(&mut self, oop: OverflowOp, typ: Ty<'_>, lhs: Self::Value, rhs: Self::Value) -> (Self::Value, Self::Value) {
820         use rustc_middle::ty::{Int, IntTy::*, Uint, UintTy::*};
821
822         let new_kind =
823             match typ.kind() {
824                 Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
825                 Uint(t @ Usize) => Uint(t.normalize(self.tcx.sess.target.pointer_width)),
826                 t @ (Uint(_) | Int(_)) => t.clone(),
827                 _ => panic!("tried to get overflow intrinsic for op applied to non-int type"),
828             };
829
830         // TODO: remove duplication with intrinsic?
831         let name =
832             match oop {
833                 OverflowOp::Add =>
834                     match new_kind {
835                         Int(I8) => "__builtin_add_overflow",
836                         Int(I16) => "__builtin_add_overflow",
837                         Int(I32) => "__builtin_sadd_overflow",
838                         Int(I64) => "__builtin_saddll_overflow",
839                         Int(I128) => "__builtin_add_overflow",
840
841                         Uint(U8) => "__builtin_add_overflow",
842                         Uint(U16) => "__builtin_add_overflow",
843                         Uint(U32) => "__builtin_uadd_overflow",
844                         Uint(U64) => "__builtin_uaddll_overflow",
845                         Uint(U128) => "__builtin_add_overflow",
846
847                         _ => unreachable!(),
848                     },
849                 OverflowOp::Sub =>
850                     match new_kind {
851                         Int(I8) => "__builtin_sub_overflow",
852                         Int(I16) => "__builtin_sub_overflow",
853                         Int(I32) => "__builtin_ssub_overflow",
854                         Int(I64) => "__builtin_ssubll_overflow",
855                         Int(I128) => "__builtin_sub_overflow",
856
857                         Uint(U8) => "__builtin_sub_overflow",
858                         Uint(U16) => "__builtin_sub_overflow",
859                         Uint(U32) => "__builtin_usub_overflow",
860                         Uint(U64) => "__builtin_usubll_overflow",
861                         Uint(U128) => "__builtin_sub_overflow",
862
863                         _ => unreachable!(),
864                     },
865                 OverflowOp::Mul =>
866                     match new_kind {
867                         Int(I8) => "__builtin_mul_overflow",
868                         Int(I16) => "__builtin_mul_overflow",
869                         Int(I32) => "__builtin_smul_overflow",
870                         Int(I64) => "__builtin_smulll_overflow",
871                         Int(I128) => "__builtin_mul_overflow",
872
873                         Uint(U8) => "__builtin_mul_overflow",
874                         Uint(U16) => "__builtin_mul_overflow",
875                         Uint(U32) => "__builtin_umul_overflow",
876                         Uint(U64) => "__builtin_umulll_overflow",
877                         Uint(U128) => "__builtin_mul_overflow",
878
879                         _ => unreachable!(),
880                     },
881             };
882
883         let intrinsic = self.context.get_builtin_function(&name);
884         let res = self.current_func()
885             // TODO: is it correct to use rhs type instead of the parameter typ?
886             .new_local(None, rhs.get_type(), "binopResult")
887             .get_address(None);
888         let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
889         (res.dereference(None).to_rvalue(), overflow)
890     }
891
892     fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
893         // FIXME: this check that we don't call get_aligned() a second time on a time.
894         // Ideally, we shouldn't need to do this check.
895         let aligned_type =
896             if ty == self.cx.u128_type || ty == self.cx.i128_type {
897                 ty
898             }
899             else {
900                 ty.get_aligned(align.bytes())
901             };
902         // TODO: It might be better to return a LValue, but fixing the rustc API is non-trivial.
903         self.stack_var_count.set(self.stack_var_count.get() + 1);
904         self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
905     }
906
907     fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
908         unimplemented!();
909         /*unsafe {
910             let alloca = llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED);
911             llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
912             alloca
913         }*/
914     }
915
916     fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
917         unimplemented!();
918         /*unsafe {
919             let alloca = llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED);
920             llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
921             alloca
922         }*/
923     }
924
925     fn load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
926         // TODO: use ty.
927         let block = self.llbb();
928         let function = block.get_function();
929         // NOTE: instead of returning the dereference here, we have to assign it to a variable in
930         // the current basic block. Otherwise, it could be used in another basic block, causing a
931         // dereference after a drop, for instance.
932         // TODO: handle align.
933         let deref = ptr.dereference(None).to_rvalue();
934         let value_type = deref.get_type();
935         unsafe { RETURN_VALUE_COUNT += 1 };
936         let loaded_value = function.new_local(None, value_type, &format!("loadedValue{}", unsafe { RETURN_VALUE_COUNT }));
937         block.add_assignment(None, loaded_value, deref);
938         loaded_value.to_rvalue()
939     }
940
941     fn volatile_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>) -> RValue<'gcc> {
942         // TODO: use ty.
943         //println!("5: volatile load: {:?} to {:?}", ptr, ptr.get_type().make_volatile());
944         let ptr = self.context.new_cast(None, ptr, ptr.get_type().make_volatile());
945         //println!("6");
946         ptr.dereference(None).to_rvalue()
947     }
948
949     fn atomic_load(&mut self, _ty: Type<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) -> RValue<'gcc> {
950         // TODO: use ty.
951         // TODO: handle alignment.
952         let atomic_load = self.context.get_builtin_function(&format!("__atomic_load_{}", size.bytes()));
953         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
954
955         let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
956         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
957         self.context.new_call(None, atomic_load, &[ptr, ordering])
958     }
959
960     fn load_operand(&mut self, place: PlaceRef<'tcx, RValue<'gcc>>) -> OperandRef<'tcx, RValue<'gcc>> {
961         //debug!("PlaceRef::load: {:?}", place);
962
963         assert_eq!(place.llextra.is_some(), place.layout.is_unsized());
964
965         if place.layout.is_zst() {
966             return OperandRef::new_zst(self, place.layout);
967         }
968
969         fn scalar_load_metadata<'a, 'gcc, 'tcx>(bx: &mut Builder<'a, 'gcc, 'tcx>, load: RValue<'gcc>, scalar: &abi::Scalar) {
970             let vr = scalar.valid_range.clone();
971             match scalar.value {
972                 abi::Int(..) => {
973                     let range = scalar.valid_range_exclusive(bx);
974                     if range.start != range.end {
975                         bx.range_metadata(load, range);
976                     }
977                 }
978                 abi::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
979                     bx.nonnull_metadata(load);
980                 }
981                 _ => {}
982             }
983         }
984
985         let val =
986             if let Some(llextra) = place.llextra {
987                 OperandValue::Ref(place.llval, Some(llextra), place.align)
988             }
989             else if place.layout.is_gcc_immediate() {
990                 let const_llval = None;
991                 /*unsafe {
992                     if let Some(global) = llvm::LLVMIsAGlobalVariable(place.llval) {
993                         if llvm::LLVMIsGlobalConstant(global) == llvm::True {
994                             const_llval = llvm::LLVMGetInitializer(global);
995                         }
996                     }
997                 }*/
998                 let llval = const_llval.unwrap_or_else(|| {
999                     let load = self.load(place.llval.get_type(), place.llval, place.align);
1000                     if let abi::Abi::Scalar(ref scalar) = place.layout.abi {
1001                         scalar_load_metadata(self, load, scalar);
1002                     }
1003                     load
1004                 });
1005                 OperandValue::Immediate(self.to_immediate(llval, place.layout))
1006             }
1007             else if let abi::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
1008                 let b_offset = a.value.size(self).align_to(b.value.align(self).abi);
1009                 let pair_type = place.layout.gcc_type(self, false);
1010
1011                 let mut load = |i, scalar: &abi::Scalar, align| {
1012                     let llptr = self.struct_gep(pair_type, place.llval, i as u64);
1013                     let load = self.load(llptr.get_type(), llptr, align);
1014                     scalar_load_metadata(self, load, scalar);
1015                     if scalar.is_bool() { self.trunc(load, self.type_i1()) } else { load }
1016                 };
1017
1018                 OperandValue::Pair(
1019                     load(0, a, place.align),
1020                     load(1, b, place.align.restrict_for_offset(b_offset)),
1021                 )
1022             }
1023             else {
1024                 OperandValue::Ref(place.llval, None, place.align)
1025             };
1026
1027         OperandRef { val, layout: place.layout }
1028     }
1029
1030     fn write_operand_repeatedly(mut self, cg_elem: OperandRef<'tcx, RValue<'gcc>>, count: u64, dest: PlaceRef<'tcx, RValue<'gcc>>) -> Self {
1031         let zero = self.const_usize(0);
1032         let count = self.const_usize(count);
1033         let start = dest.project_index(&mut self, zero).llval;
1034         let end = dest.project_index(&mut self, count).llval;
1035
1036         let mut header_bx = self.build_sibling_block("repeat_loop_header");
1037         let mut body_bx = self.build_sibling_block("repeat_loop_body");
1038         let next_bx = self.build_sibling_block("repeat_loop_next");
1039
1040         let ptr_type = start.get_type();
1041         let current = self.llbb().get_function().new_local(None, ptr_type, "loop_var");
1042         let current_val = current.to_rvalue();
1043         self.assign(current, start);
1044
1045         self.br(header_bx.llbb());
1046
1047         let keep_going = header_bx.icmp(IntPredicate::IntNE, current_val, end);
1048         header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
1049
1050         let align = dest.align.restrict_for_offset(dest.layout.field(self.cx(), 0).size);
1051         cg_elem.val.store(&mut body_bx, PlaceRef::new_sized_aligned(current_val, cg_elem.layout, align));
1052
1053         let next = body_bx.inbounds_gep(self.backend_type(cg_elem.layout), current.to_rvalue(), &[self.const_usize(1)]);
1054         body_bx.llbb().add_assignment(None, current, next);
1055         body_bx.br(header_bx.llbb());
1056
1057         next_bx
1058     }
1059
1060     fn range_metadata(&mut self, _load: RValue<'gcc>, _range: Range<u128>) {
1061         // TODO
1062         /*if self.sess().target.target.arch == "amdgpu" {
1063             // amdgpu/LLVM does something weird and thinks a i64 value is
1064             // split into a v2i32, halving the bitwidth LLVM expects,
1065             // tripping an assertion. So, for now, just disable this
1066             // optimization.
1067             return;
1068         }
1069
1070         unsafe {
1071             let llty = self.cx.val_ty(load);
1072             let v = [
1073                 self.cx.const_uint_big(llty, range.start),
1074                 self.cx.const_uint_big(llty, range.end),
1075             ];
1076
1077             llvm::LLVMSetMetadata(
1078                 load,
1079                 llvm::MD_range as c_uint,
1080                 llvm::LLVMMDNodeInContext(self.cx.llcx, v.as_ptr(), v.len() as c_uint),
1081             );
1082         }*/
1083     }
1084
1085     fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
1086         // TODO
1087         /*unsafe {
1088             llvm::LLVMSetMetadata(
1089                 load,
1090                 llvm::MD_nonnull as c_uint,
1091                 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1092             );
1093         }*/
1094     }
1095
1096     fn store(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
1097         self.store_with_flags(val, ptr, align, MemFlags::empty())
1098     }
1099
1100     fn store_with_flags(&mut self, val: RValue<'gcc>, ptr: RValue<'gcc>, _align: Align, _flags: MemFlags) -> RValue<'gcc> {
1101         //debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
1102         let ptr = self.check_store(val, ptr);
1103         self.llbb().add_assignment(None, ptr.dereference(None), val);
1104         /*let align =
1105             if flags.contains(MemFlags::UNALIGNED) { 1 } else { align.bytes() as c_uint };
1106         llvm::LLVMSetAlignment(store, align);
1107         if flags.contains(MemFlags::VOLATILE) {
1108             llvm::LLVMSetVolatile(store, llvm::True);
1109         }
1110         if flags.contains(MemFlags::NONTEMPORAL) {
1111             // According to LLVM [1] building a nontemporal store must
1112             // *always* point to a metadata value of the integer 1.
1113             //
1114             // [1]: http://llvm.org/docs/LangRef.html#store-instruction
1115             let one = self.cx.const_i32(1);
1116             let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
1117             llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
1118         }*/
1119         // NOTE: dummy value here since it's never used. FIXME: API should not return a value here?
1120         self.cx.context.new_rvalue_zero(self.type_i32())
1121     }
1122
1123     fn atomic_store(&mut self, value: RValue<'gcc>, ptr: RValue<'gcc>, order: AtomicOrdering, size: Size) {
1124         // TODO: handle alignment.
1125         let atomic_store = self.context.get_builtin_function(&format!("__atomic_store_{}", size.bytes()));
1126         let ordering = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1127         let volatile_const_void_ptr_type = self.context.new_type::<*mut ()>().make_const().make_volatile();
1128         let ptr = self.context.new_cast(None, ptr, volatile_const_void_ptr_type);
1129
1130         // FIXME: fix libgccjit to allow comparing an integer type with an aligned integer type because
1131         // the following cast is required to avoid this error:
1132         // 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))))
1133         let int_type = atomic_store.get_param(1).to_rvalue().get_type();
1134         let value = self.context.new_cast(None, value, int_type);
1135         self.llbb()
1136             .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
1137     }
1138
1139     fn gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
1140         let mut result = ptr;
1141         for index in indices {
1142             result = self.context.new_array_access(None, result, *index).get_address(None).to_rvalue();
1143         }
1144         result
1145     }
1146
1147     fn inbounds_gep(&mut self, _typ: Type<'gcc>, ptr: RValue<'gcc>, indices: &[RValue<'gcc>]) -> RValue<'gcc> {
1148         // FIXME: would be safer if doing the same thing (loop) as gep.
1149         // TODO: specify inbounds somehow.
1150         match indices.len() {
1151             1 => {
1152                 self.context.new_array_access(None, ptr, indices[0]).get_address(None)
1153             },
1154             2 => {
1155                 let array = ptr.dereference(None); // TODO: assert that first index is 0?
1156                 self.context.new_array_access(None, array, indices[1]).get_address(None)
1157             },
1158             _ => unimplemented!(),
1159         }
1160     }
1161
1162     fn struct_gep(&mut self, value_type: Type<'gcc>, ptr: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1163         // FIXME: it would be better if the API only called this on struct, not on arrays.
1164         assert_eq!(idx as usize as u64, idx);
1165         let value = ptr.dereference(None).to_rvalue();
1166
1167         if value_type.is_array().is_some() {
1168             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1169             let element = self.context.new_array_access(None, value, index);
1170             element.get_address(None)
1171         }
1172         else if let Some(vector_type) = value_type.is_vector() {
1173             let array_type = vector_type.get_element_type().make_pointer();
1174             let array = self.bitcast(ptr, array_type);
1175             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1176             let element = self.context.new_array_access(None, array, index);
1177             element.get_address(None)
1178         }
1179         else if let Some(struct_type) = value_type.is_struct() {
1180             ptr.dereference_field(None, struct_type.get_field(idx as i32)).get_address(None)
1181         }
1182         else {
1183             panic!("Unexpected type {:?}", value_type);
1184         }
1185     }
1186
1187     /* Casts */
1188     fn trunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1189         // TODO: check that it indeed truncate the value.
1190         //println!("trunc: {:?} -> {:?}", value, dest_ty);
1191         self.context.new_cast(None, value, dest_ty)
1192     }
1193
1194     fn sext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1195         // TODO: check that it indeed sign extend the value.
1196         //println!("Sext {:?} to {:?}", value, dest_ty);
1197         //if let Some(vector_type) = value.get_type().is_vector() {
1198         if dest_ty.is_vector().is_some() {
1199             // TODO: nothing to do as it is only for LLVM?
1200             return value;
1201             /*let dest_type = self.context.new_vector_type(dest_ty, vector_type.get_num_units() as u64);
1202             println!("Casting {:?} to {:?}", value, dest_type);
1203             return self.context.new_cast(None, value, dest_type);*/
1204         }
1205         self.context.new_cast(None, value, dest_ty)
1206     }
1207
1208     fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1209         //println!("7: fptoui: {:?} to {:?}", value, dest_ty);
1210         let ret = self.context.new_cast(None, value, dest_ty);
1211         //println!("8");
1212         ret
1213         //unsafe { llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED) }
1214     }
1215
1216     fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1217         self.context.new_cast(None, value, dest_ty)
1218     }
1219
1220     fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1221         //println!("1: uitofp: {:?} -> {:?}", value, dest_ty);
1222         let ret = self.context.new_cast(None, value, dest_ty);
1223         //println!("2");
1224         ret
1225     }
1226
1227     fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1228         //println!("3: sitofp: {:?} -> {:?}", value, dest_ty);
1229         let ret = self.context.new_cast(None, value, dest_ty);
1230         //println!("4");
1231         ret
1232     }
1233
1234     fn fptrunc(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1235         // TODO: make sure it trancates.
1236         self.context.new_cast(None, value, dest_ty)
1237     }
1238
1239     fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1240         self.context.new_cast(None, value, dest_ty)
1241     }
1242
1243     fn ptrtoint(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1244         self.cx.ptrtoint(self.block.expect("block"), value, dest_ty)
1245     }
1246
1247     fn inttoptr(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1248         self.cx.inttoptr(self.block.expect("block"), value, dest_ty)
1249     }
1250
1251     fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1252         self.cx.const_bitcast(value, dest_ty)
1253     }
1254
1255     fn intcast(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>, _is_signed: bool) -> RValue<'gcc> {
1256         // NOTE: is_signed is for value, not dest_typ.
1257         //println!("intcast: {:?} ({:?}) -> {:?}", value, value.get_type(), dest_typ);
1258         self.cx.context.new_cast(None, value, dest_typ)
1259     }
1260
1261     fn pointercast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1262         //println!("pointercast: {:?} ({:?}) -> {:?}", value, value.get_type(), dest_ty);
1263         let val_type = value.get_type();
1264         match (type_is_pointer(val_type), type_is_pointer(dest_ty)) {
1265             (false, true) => {
1266                 // NOTE: Projecting a field of a pointer type will attemp a cast from a signed char to
1267                 // a pointer, which is not supported by gccjit.
1268                 return self.cx.context.new_cast(None, self.inttoptr(value, val_type.make_pointer()), dest_ty);
1269             },
1270             (false, false) => {
1271                 // When they are not pointers, we want a transmute (or reinterpret_cast).
1272                 //self.cx.context.new_cast(None, value, dest_ty)
1273                 self.bitcast(value, dest_ty)
1274             },
1275             (true, true) => self.cx.context.new_cast(None, value, dest_ty),
1276             (true, false) => unimplemented!(),
1277         }
1278     }
1279
1280     /* Comparisons */
1281     fn icmp(&mut self, op: IntPredicate, lhs: RValue<'gcc>, mut rhs: RValue<'gcc>) -> RValue<'gcc> {
1282         if lhs.get_type() != rhs.get_type() {
1283             // NOTE: hack because we try to cast a vector type to the same vector type.
1284             if format!("{:?}", lhs.get_type()) != format!("{:?}", rhs.get_type()) {
1285                 rhs = self.context.new_cast(None, rhs, lhs.get_type());
1286             }
1287         }
1288         self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
1289     }
1290
1291     fn fcmp(&mut self, op: RealPredicate, lhs: RValue<'gcc>, rhs: RValue<'gcc>) -> RValue<'gcc> {
1292         self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
1293     }
1294
1295     /* Miscellaneous instructions */
1296     fn memcpy(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
1297         if flags.contains(MemFlags::NONTEMPORAL) {
1298             // HACK(nox): This is inefficient but there is no nontemporal memcpy.
1299             let val = self.load(src.get_type(), src, src_align);
1300             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
1301             self.store_with_flags(val, ptr, dst_align, flags);
1302             return;
1303         }
1304         let size = self.intcast(size, self.type_size_t(), false);
1305         let _is_volatile = flags.contains(MemFlags::VOLATILE);
1306         let dst = self.pointercast(dst, self.type_i8p());
1307         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
1308         let memcpy = self.context.get_builtin_function("memcpy");
1309         let block = self.block.expect("block");
1310         // TODO: handle aligns and is_volatile.
1311         block.add_eval(None, self.context.new_call(None, memcpy, &[dst, src, size]));
1312     }
1313
1314     fn memmove(&mut self, dst: RValue<'gcc>, dst_align: Align, src: RValue<'gcc>, src_align: Align, size: RValue<'gcc>, flags: MemFlags) {
1315         if flags.contains(MemFlags::NONTEMPORAL) {
1316             // HACK(nox): This is inefficient but there is no nontemporal memmove.
1317             let val = self.load(src.get_type(), src, src_align);
1318             let ptr = self.pointercast(dst, self.type_ptr_to(self.val_ty(val)));
1319             self.store_with_flags(val, ptr, dst_align, flags);
1320             return;
1321         }
1322         let size = self.intcast(size, self.type_size_t(), false);
1323         let _is_volatile = flags.contains(MemFlags::VOLATILE);
1324         let dst = self.pointercast(dst, self.type_i8p());
1325         let src = self.pointercast(src, self.type_ptr_to(self.type_void()));
1326
1327         let memmove = self.context.get_builtin_function("memmove");
1328         let block = self.block.expect("block");
1329         // TODO: handle is_volatile.
1330         block.add_eval(None, self.context.new_call(None, memmove, &[dst, src, size]));
1331     }
1332
1333     fn memset(&mut self, ptr: RValue<'gcc>, fill_byte: RValue<'gcc>, size: RValue<'gcc>, _align: Align, flags: MemFlags) {
1334         let _is_volatile = flags.contains(MemFlags::VOLATILE);
1335         let ptr = self.pointercast(ptr, self.type_i8p());
1336         let memset = self.context.get_builtin_function("memset");
1337         let block = self.block.expect("block");
1338         // TODO: handle aligns and is_volatile.
1339         //println!("memset: {:?} -> {:?}", fill_byte, self.i32_type);
1340         let fill_byte = self.context.new_cast(None, fill_byte, self.i32_type);
1341         let size = self.intcast(size, self.type_size_t(), false);
1342         block.add_eval(None, self.context.new_call(None, memset, &[ptr, fill_byte, size]));
1343     }
1344
1345     fn select(&mut self, cond: RValue<'gcc>, then_val: RValue<'gcc>, mut else_val: RValue<'gcc>) -> RValue<'gcc> {
1346         let func = self.current_func();
1347         let variable = func.new_local(None, then_val.get_type(), "selectVar");
1348         let then_block = func.new_block("then");
1349         let else_block = func.new_block("else");
1350         let after_block = func.new_block("after");
1351         self.llbb().end_with_conditional(None, cond, then_block, else_block);
1352
1353         then_block.add_assignment(None, variable, then_val);
1354         then_block.end_with_jump(None, after_block);
1355
1356         if then_val.get_type() != else_val.get_type() {
1357             else_val = self.context.new_cast(None, else_val, then_val.get_type());
1358         }
1359         else_block.add_assignment(None, variable, else_val);
1360         else_block.end_with_jump(None, after_block);
1361
1362         // NOTE: since jumps were added in a place rustc does not expect, the current blocks in the
1363         // state need to be updated.
1364         self.block = Some(after_block);
1365         *self.cx.current_block.borrow_mut() = Some(after_block);
1366
1367         variable.to_rvalue()
1368     }
1369
1370     #[allow(dead_code)]
1371     fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
1372         unimplemented!();
1373         //unsafe { llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED) }
1374     }
1375
1376     fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
1377         unimplemented!();
1378         //unsafe { llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED) }
1379     }
1380
1381     fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
1382         unimplemented!();
1383         /*unsafe {
1384             let elt_ty = self.cx.val_ty(elt);
1385             let undef = llvm::LLVMGetUndef(self.type_vector(elt_ty, num_elts as u64));
1386             let vec = self.insert_element(undef, elt, self.cx.const_i32(0));
1387             let vec_i32_ty = self.type_vector(self.type_i32(), num_elts as u64);
1388             self.shuffle_vector(vec, undef, self.const_null(vec_i32_ty))
1389         }*/
1390     }
1391
1392     fn extract_value(&mut self, aggregate_value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1393         // FIXME: it would be better if the API only called this on struct, not on arrays.
1394         assert_eq!(idx as usize as u64, idx);
1395         let value_type = aggregate_value.get_type();
1396
1397         if value_type.is_array().is_some() {
1398             let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1399             let element = self.context.new_array_access(None, aggregate_value, index);
1400             element.get_address(None)
1401         }
1402         else if value_type.is_vector().is_some() {
1403             panic!();
1404         }
1405         else if let Some(pointer_type) = value_type.get_pointee() {
1406             if let Some(struct_type) = pointer_type.is_struct() {
1407                 // NOTE: hack to workaround a limitation of the rustc API: see comment on
1408                 // CodegenCx.structs_as_pointer
1409                 aggregate_value.dereference_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1410             }
1411             else {
1412                 panic!("Unexpected type {:?}", value_type);
1413             }
1414         }
1415         else if let Some(struct_type) = value_type.is_struct() {
1416             aggregate_value.access_field(None, struct_type.get_field(idx as i32)).to_rvalue()
1417         }
1418         else {
1419             panic!("Unexpected type {:?}", value_type);
1420         }
1421         /*assert_eq!(idx as c_uint as u64, idx);
1422         unsafe { llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED) }*/
1423     }
1424
1425     fn insert_value(&mut self, aggregate_value: RValue<'gcc>, value: RValue<'gcc>, idx: u64) -> RValue<'gcc> {
1426         // FIXME: it would be better if the API only called this on struct, not on arrays.
1427         assert_eq!(idx as usize as u64, idx);
1428         let value_type = aggregate_value.get_type();
1429
1430         let lvalue =
1431             if value_type.is_array().is_some() {
1432                 let index = self.context.new_rvalue_from_long(self.u64_type, i64::try_from(idx).expect("i64::try_from"));
1433                 self.context.new_array_access(None, aggregate_value, index)
1434             }
1435             else if value_type.is_vector().is_some() {
1436                 panic!();
1437             }
1438             else if let Some(pointer_type) = value_type.get_pointee() {
1439                 if let Some(struct_type) = pointer_type.is_struct() {
1440                     // NOTE: hack to workaround a limitation of the rustc API: see comment on
1441                     // CodegenCx.structs_as_pointer
1442                     aggregate_value.dereference_field(None, struct_type.get_field(idx as i32))
1443                 }
1444                 else {
1445                     panic!("Unexpected type {:?}", value_type);
1446                 }
1447             }
1448             else {
1449                 panic!("Unexpected type {:?}", value_type);
1450             };
1451         self.llbb().add_assignment(None, lvalue, value);
1452
1453         aggregate_value
1454     }
1455
1456     fn landing_pad(&mut self, _ty: Type<'gcc>, _pers_fn: RValue<'gcc>, _num_clauses: usize) -> RValue<'gcc> {
1457         let field1 = self.context.new_field(None, self.u8_type, "landing_pad_field_1");
1458         let field2 = self.context.new_field(None, self.i32_type, "landing_pad_field_1");
1459         let struct_type = self.context.new_struct_type(None, "landing_pad", &[field1, field2]);
1460         self.current_func().new_local(None, struct_type.as_type(), "landing_pad")
1461             .to_rvalue()
1462         // TODO
1463         /*unsafe {
1464             llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn, num_clauses as c_uint, UNNAMED)
1465         }*/
1466     }
1467
1468     fn set_cleanup(&mut self, _landing_pad: RValue<'gcc>) {
1469         // TODO
1470         /*unsafe {
1471             llvm::LLVMSetCleanup(landing_pad, llvm::True);
1472         }*/
1473     }
1474
1475     fn resume(&mut self, _exn: RValue<'gcc>) -> RValue<'gcc> {
1476         unimplemented!();
1477         //unsafe { llvm::LLVMBuildResume(self.llbuilder, exn) }
1478     }
1479
1480     fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
1481         unimplemented!();
1482         /*let name = const_cstr!("cleanuppad");
1483         let ret = unsafe {
1484             llvm::LLVMRustBuildCleanupPad(
1485                 self.llbuilder,
1486                 parent,
1487                 args.len() as c_uint,
1488                 args.as_ptr(),
1489                 name.as_ptr(),
1490             )
1491         };
1492         Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))*/
1493     }
1494
1495     fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) -> RValue<'gcc> {
1496         unimplemented!();
1497         /*let ret =
1498             unsafe { llvm::LLVMRustBuildCleanupRet(self.llbuilder, funclet.cleanuppad(), unwind) };
1499         ret.expect("LLVM does not have support for cleanupret")*/
1500     }
1501
1502     fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
1503         unimplemented!();
1504         /*let name = const_cstr!("catchpad");
1505         let ret = unsafe {
1506             llvm::LLVMRustBuildCatchPad(
1507                 self.llbuilder,
1508                 parent,
1509                 args.len() as c_uint,
1510                 args.as_ptr(),
1511                 name.as_ptr(),
1512             )
1513         };
1514         Funclet::new(ret.expect("LLVM does not have support for catchpad"))*/
1515     }
1516
1517     fn catch_switch(&mut self, _parent: Option<RValue<'gcc>>, _unwind: Option<Block<'gcc>>, _num_handlers: usize) -> RValue<'gcc> {
1518         unimplemented!();
1519         /*let name = const_cstr!("catchswitch");
1520         let ret = unsafe {
1521             llvm::LLVMRustBuildCatchSwitch(
1522                 self.llbuilder,
1523                 parent,
1524                 unwind,
1525                 num_handlers as c_uint,
1526                 name.as_ptr(),
1527             )
1528         };
1529         ret.expect("LLVM does not have support for catchswitch")*/
1530     }
1531
1532     fn add_handler(&mut self, _catch_switch: RValue<'gcc>, _handler: Block<'gcc>) {
1533         unimplemented!();
1534         /*unsafe {
1535             llvm::LLVMRustAddHandler(catch_switch, handler);
1536         }*/
1537     }
1538
1539     fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
1540         // TODO
1541         /*unsafe {
1542             llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1543         }*/
1544     }
1545
1546     // Atomic Operations
1547     fn atomic_cmpxchg(&mut self, dst: RValue<'gcc>, cmp: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering, failure_order: AtomicOrdering, weak: bool) -> RValue<'gcc> {
1548         let expected = self.current_func().new_local(None, cmp.get_type(), "expected");
1549         self.llbb().add_assignment(None, expected, cmp);
1550         let success = self.compare_exchange(dst, expected, src, order, failure_order, weak);
1551
1552         let pair_type = self.cx.type_struct(&[src.get_type(), self.bool_type], false);
1553         let result = self.current_func().new_local(None, pair_type, "atomic_cmpxchg_result");
1554         let align = Align::from_bits(64).expect("align"); // TODO: use good align.
1555
1556         let value_type = result.to_rvalue().get_type();
1557         if let Some(struct_type) = value_type.is_struct() {
1558             self.store(success, result.access_field(None, struct_type.get_field(1)).get_address(None), align);
1559             // NOTE: since success contains the call to the intrinsic, it must be stored before
1560             // expected so that we store expected after the call.
1561             self.store(expected.to_rvalue(), result.access_field(None, struct_type.get_field(0)).get_address(None), align);
1562         }
1563         // TODO: handle when value is not a struct.
1564
1565         result.to_rvalue()
1566     }
1567
1568     fn atomic_rmw(&mut self, op: AtomicRmwBinOp, dst: RValue<'gcc>, src: RValue<'gcc>, order: AtomicOrdering) -> RValue<'gcc> {
1569         let size = self.cx.int_width(src.get_type()) / 8;
1570         let name =
1571             match op {
1572                 AtomicRmwBinOp::AtomicXchg => format!("__atomic_exchange_{}", size),
1573                 AtomicRmwBinOp::AtomicAdd => format!("__atomic_fetch_add_{}", size),
1574                 AtomicRmwBinOp::AtomicSub => format!("__atomic_fetch_sub_{}", size),
1575                 AtomicRmwBinOp::AtomicAnd => format!("__atomic_fetch_and_{}", size),
1576                 AtomicRmwBinOp::AtomicNand => format!("__atomic_fetch_nand_{}", size),
1577                 AtomicRmwBinOp::AtomicOr => format!("__atomic_fetch_or_{}", size),
1578                 AtomicRmwBinOp::AtomicXor => format!("__atomic_fetch_xor_{}", size),
1579                 AtomicRmwBinOp::AtomicMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1580                 AtomicRmwBinOp::AtomicMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1581                 AtomicRmwBinOp::AtomicUMax => return self.atomic_extremum(ExtremumOperation::Max, dst, src, order),
1582                 AtomicRmwBinOp::AtomicUMin => return self.atomic_extremum(ExtremumOperation::Min, dst, src, order),
1583             };
1584
1585
1586         let atomic_function = self.context.get_builtin_function(name);
1587         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1588
1589         let void_ptr_type = self.context.new_type::<*mut ()>();
1590         let volatile_void_ptr_type = void_ptr_type.make_volatile();
1591         let dst = self.context.new_cast(None, dst, volatile_void_ptr_type);
1592         // NOTE: not sure why, but we have the wrong type here.
1593         let new_src_type = atomic_function.get_param(1).to_rvalue().get_type();
1594         let src = self.context.new_cast(None, src, new_src_type);
1595         let res = self.context.new_call(None, atomic_function, &[dst, src, order]);
1596         self.context.new_cast(None, res, src.get_type())
1597     }
1598
1599     fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
1600         let name =
1601             match scope {
1602                 SynchronizationScope::SingleThread => "__atomic_signal_fence",
1603                 SynchronizationScope::CrossThread => "__atomic_thread_fence",
1604             };
1605         let thread_fence = self.context.get_builtin_function(name);
1606         let order = self.context.new_rvalue_from_int(self.i32_type, order.to_gcc());
1607         self.llbb().add_eval(None, self.context.new_call(None, thread_fence, &[order]));
1608     }
1609
1610     fn set_invariant_load(&mut self, load: RValue<'gcc>) {
1611         // NOTE: Hack to consider vtable function pointer as non-global-variable function pointer.
1612         self.normal_function_addresses.borrow_mut().insert(load);
1613         // TODO
1614         /*unsafe {
1615             llvm::LLVMSetMetadata(
1616                 load,
1617                 llvm::MD_invariant_load as c_uint,
1618                 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0),
1619             );
1620         }*/
1621     }
1622
1623     fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1624         // TODO
1625         //self.call_lifetime_intrinsic("llvm.lifetime.start.p0i8", ptr, size);
1626     }
1627
1628     fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1629         // TODO
1630         //self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
1631     }
1632
1633     fn call(&mut self, _typ: Type<'gcc>, func: RValue<'gcc>, args: &[RValue<'gcc>], funclet: Option<&Funclet>) -> RValue<'gcc> {
1634         // FIXME: remove when having a proper API.
1635         let gcc_func = unsafe { std::mem::transmute(func) };
1636         if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1637             self.function_call(func, args, funclet)
1638         }
1639         else {
1640             // If it's a not function that was defined, it's a function pointer.
1641             self.function_ptr_call(func, args, funclet)
1642         }
1643     }
1644
1645     fn zext(&mut self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
1646         // FIXME: this does not zero-extend.
1647         if value.get_type().is_bool() && dest_typ.is_i8(&self.cx) {
1648             // FIXME: hack because base::from_immediate converts i1 to i8.
1649             // Fix the code in codegen_ssa::base::from_immediate.
1650             return value;
1651         }
1652         //println!("zext: {:?} -> {:?}", value, dest_typ);
1653         self.context.new_cast(None, value, dest_typ)
1654     }
1655
1656     fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
1657         self.cx
1658     }
1659
1660     fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1661         unimplemented!();
1662         //llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1663     }
1664
1665     fn set_span(&mut self, _span: Span) {}
1666
1667     fn from_immediate(&mut self, val: Self::Value) -> Self::Value {
1668         if self.cx().val_ty(val) == self.cx().type_i1() {
1669             self.zext(val, self.cx().type_i8())
1670         }
1671         else {
1672             val
1673         }
1674     }
1675
1676     fn to_immediate_scalar(&mut self, val: Self::Value, scalar: &abi::Scalar) -> Self::Value {
1677         if scalar.is_bool() {
1678             return self.trunc(val, self.cx().type_i1());
1679         }
1680         val
1681     }
1682
1683     fn fptoui_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1684         None
1685     }
1686
1687     fn fptosi_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1688         None
1689     }
1690
1691     fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
1692         unimplemented!();
1693         /*debug!(
1694             "instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1695             fn_name, hash, num_counters, index
1696         );
1697
1698         let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1699         let args = &[fn_name, hash, num_counters, index];
1700         let args = self.check_call("call", llfn, args);
1701
1702         unsafe {
1703             let _ = llvm::LLVMRustBuildCall(
1704                 self.llbuilder,
1705                 llfn,
1706                 args.as_ptr() as *const &llvm::Value,
1707                 args.len() as c_uint,
1708                 None,
1709             );
1710         }*/
1711     }
1712 }
1713
1714 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
1715     pub fn shuffle_vector(&mut self, v1: RValue<'gcc>, v2: RValue<'gcc>, mask: RValue<'gcc>) -> RValue<'gcc> {
1716         let return_type = v1.get_type();
1717         let params = [
1718             self.context.new_parameter(None, return_type, "v1"),
1719             self.context.new_parameter(None, return_type, "v2"),
1720             self.context.new_parameter(None, mask.get_type(), "mask"),
1721         ];
1722         let shuffle = self.context.new_function(None, FunctionType::Extern, return_type, &params, "_mm_shuffle_epi8", false);
1723         self.context.new_call(None, shuffle, &[v1, v2, mask])
1724     }
1725 }
1726
1727 impl<'a, 'gcc, 'tcx> StaticBuilderMethods for Builder<'a, 'gcc, 'tcx> {
1728     fn get_static(&mut self, def_id: DefId) -> RValue<'gcc> {
1729         // Forward to the `get_static` method of `CodegenCx`
1730         self.cx().get_static(def_id)
1731     }
1732 }
1733
1734 impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
1735     fn param_env(&self) -> ParamEnv<'tcx> {
1736         self.cx.param_env()
1737     }
1738 }
1739
1740 impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
1741     fn target_spec(&self) -> &Target {
1742         &self.cx.target_spec()
1743     }
1744 }
1745
1746 trait ToGccComp {
1747     fn to_gcc_comparison(&self) -> ComparisonOp;
1748 }
1749
1750 impl ToGccComp for IntPredicate {
1751     fn to_gcc_comparison(&self) -> ComparisonOp {
1752         match *self {
1753             IntPredicate::IntEQ => ComparisonOp::Equals,
1754             IntPredicate::IntNE => ComparisonOp::NotEquals,
1755             IntPredicate::IntUGT => ComparisonOp::GreaterThan,
1756             IntPredicate::IntUGE => ComparisonOp::GreaterThanEquals,
1757             IntPredicate::IntULT => ComparisonOp::LessThan,
1758             IntPredicate::IntULE => ComparisonOp::LessThanEquals,
1759             IntPredicate::IntSGT => ComparisonOp::GreaterThan,
1760             IntPredicate::IntSGE => ComparisonOp::GreaterThanEquals,
1761             IntPredicate::IntSLT => ComparisonOp::LessThan,
1762             IntPredicate::IntSLE => ComparisonOp::LessThanEquals,
1763         }
1764     }
1765 }
1766
1767 impl ToGccComp for RealPredicate {
1768     fn to_gcc_comparison(&self) -> ComparisonOp {
1769         // TODO: check that ordered vs non-ordered is respected.
1770         match *self {
1771             RealPredicate::RealPredicateFalse => unreachable!(),
1772             RealPredicate::RealOEQ => ComparisonOp::Equals,
1773             RealPredicate::RealOGT => ComparisonOp::GreaterThan,
1774             RealPredicate::RealOGE => ComparisonOp::GreaterThanEquals,
1775             RealPredicate::RealOLT => ComparisonOp::LessThan,
1776             RealPredicate::RealOLE => ComparisonOp::LessThanEquals,
1777             RealPredicate::RealONE => ComparisonOp::NotEquals,
1778             RealPredicate::RealORD => unreachable!(),
1779             RealPredicate::RealUNO => unreachable!(),
1780             RealPredicate::RealUEQ => ComparisonOp::Equals,
1781             RealPredicate::RealUGT => ComparisonOp::GreaterThan,
1782             RealPredicate::RealUGE => ComparisonOp::GreaterThan,
1783             RealPredicate::RealULT => ComparisonOp::LessThan,
1784             RealPredicate::RealULE => ComparisonOp::LessThan,
1785             RealPredicate::RealUNE => ComparisonOp::NotEquals,
1786             RealPredicate::RealPredicateTrue => unreachable!(),
1787         }
1788     }
1789 }
1790
1791 #[repr(C)]
1792 #[allow(non_camel_case_types)]
1793 enum MemOrdering {
1794     __ATOMIC_RELAXED,
1795     __ATOMIC_CONSUME,
1796     __ATOMIC_ACQUIRE,
1797     __ATOMIC_RELEASE,
1798     __ATOMIC_ACQ_REL,
1799     __ATOMIC_SEQ_CST,
1800 }
1801
1802 trait ToGccOrdering {
1803     fn to_gcc(self) -> i32;
1804 }
1805
1806 impl ToGccOrdering for AtomicOrdering {
1807     fn to_gcc(self) -> i32 {
1808         use MemOrdering::*;
1809
1810         let ordering =
1811             match self {
1812                 AtomicOrdering::NotAtomic => __ATOMIC_RELAXED, // TODO: check if that's the same.
1813                 AtomicOrdering::Unordered => __ATOMIC_RELAXED,
1814                 AtomicOrdering::Monotonic => __ATOMIC_RELAXED, // TODO: check if that's the same.
1815                 AtomicOrdering::Acquire => __ATOMIC_ACQUIRE,
1816                 AtomicOrdering::Release => __ATOMIC_RELEASE,
1817                 AtomicOrdering::AcquireRelease => __ATOMIC_ACQ_REL,
1818                 AtomicOrdering::SequentiallyConsistent => __ATOMIC_SEQ_CST,
1819             };
1820         ordering as i32
1821     }
1822 }