3 use std::convert::TryFrom;
4 use std::ops::{Deref, Range};
6 use gccjit::FunctionType;
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::{
33 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
34 use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout};
36 use rustc_span::def_id::DefId;
37 use rustc_target::abi::{
45 use rustc_target::spec::{HasTargetSpec, Target};
47 use crate::common::{SignType, TypeReflection, type_is_pointer};
48 use crate::context::CodegenCx;
49 use crate::type_of::LayoutGccExt;
54 // TODO(antoyo): remove this variable.
55 static mut RETURN_VALUE_COUNT: usize = 0;
57 enum ExtremumOperation {
63 fn clone(&self) -> Self;
66 impl EnumClone for AtomicOrdering {
67 fn clone(&self) -> 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,
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>,
86 impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
87 fn with_cx(cx: &'a CodegenCx<'gcc, 'tcx>) -> Self {
91 stack_var_count: Cell::new(0),
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;
98 let func = self.current_func();
102 // TODO(antoyo): does this make sense?
103 AtomicOrdering::AcquireRelease | AtomicOrdering::Release => AtomicOrdering::Acquire,
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());
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);
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);
121 let comparison_operator =
123 ExtremumOperation::Max => ComparisonOp::LessThan,
124 ExtremumOperation::Min => ComparisonOp::GreaterThan,
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);
132 while_block.end_with_conditional(None, cond, while_block, after_block);
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);
139 return_value.to_rvalue()
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);
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);
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])
160 pub fn assign(&self, lvalue: LValue<'gcc>, value: RValue<'gcc>) {
161 self.llbb().add_assignment(None, lvalue, value);
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;
174 param_types.push(param);
178 return Cow::Borrowed(args);
181 let casted_args: Vec<_> = param_types
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)
196 Cow::Owned(casted_args)
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;
208 param_types.push(param);
212 return Cow::Borrowed(args);
215 let casted_args: Vec<_> = param_types
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)
230 Cow::Owned(casted_args)
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);
238 if dest_ptr_ty == stored_ptr_ty {
242 self.bitcast(ptr, stored_ptr_ty)
246 pub fn current_func(&self) -> Function<'gcc> {
247 self.block.expect("block").get_function()
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);
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));
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)
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);
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();
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;
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));
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, &[]));
302 current_block.add_eval(None, self.cx.context.new_call_through_ptr(None, func_ptr, &args));
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));
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));
325 impl<'gcc, 'tcx> HasCodegen<'tcx> for Builder<'_, 'gcc, 'tcx> {
326 type CodegenCx = CodegenCx<'gcc, 'tcx>;
329 impl<'tcx> HasTyCtxt<'tcx> for Builder<'_, '_, 'tcx> {
330 fn tcx(&self) -> TyCtxt<'tcx> {
335 impl HasDataLayout for Builder<'_, '_, '_> {
336 fn data_layout(&self) -> &TargetDataLayout {
337 self.cx.data_layout()
341 impl<'tcx> LayoutOf for Builder<'_, '_, 'tcx> {
343 type TyAndLayout = TyAndLayout<'tcx>;
345 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
346 self.cx.layout_of(ty)
350 impl<'gcc, 'tcx> Deref for Builder<'_, 'gcc, 'tcx> {
351 type Target = CodegenCx<'gcc, 'tcx>;
353 fn deref(&self) -> &Self::Target {
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;
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;
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);
378 fn build_sibling_block(&mut self, name: &str) -> Self {
379 let block = self.append_sibling_block(name);
380 Self::build(self.cx, block)
383 fn llbb(&self) -> Block<'gcc> {
384 self.block.expect("block")
387 fn append_block(cx: &'a CodegenCx<'gcc, 'tcx>, func: RValue<'gcc>, name: &str) -> Block<'gcc> {
388 let func = cx.rvalue_as_function(func);
392 fn append_sibling_block(&mut self, name: &str) -> Block<'gcc> {
393 let func = self.current_func();
397 fn ret_void(&mut self) {
398 self.llbb().end_with_void_return(None)
401 fn ret(&mut self, value: RValue<'gcc>) {
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()
411 self.llbb().end_with_return(None, value);
414 fn br(&mut self, dest: Block<'gcc>) {
415 self.llbb().end_with_jump(None, dest)
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)
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));
429 self.block.expect("block").end_with_switch(None, value, default_block, &gcc_cases);
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)
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)
450 let return_value = self.current_func()
451 .new_local(None, return_type, "unreachableReturn");
452 block.end_with_return(None, return_value)
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());
464 fn fadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
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());
475 fn fsub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
479 fn mul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
483 fn fmul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
487 fn udiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
488 // TODO(antoyo): convert the arguments to unsigned?
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.
498 fn sdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
499 // TODO(antoyo): convert the arguments to signed?
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);
513 fn fdiv(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
517 fn urem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
521 fn srem(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
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]);
531 assert_eq!(a.get_type(), self.cx.double_type);
533 let fmod = self.context.get_builtin_function("fmod");
534 return self.context.new_call(None, fmod, &[a, b]);
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);
544 self.context.new_cast(None, result, a_type)
546 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
547 let b = self.context.new_cast(None, b, a_type);
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);
563 self.context.new_cast(None, result, a_type)
565 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
566 let b = self.context.new_cast(None, b, a_type);
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);
582 self.context.new_cast(None, result, a_type)
584 else if a_type.is_signed(self) && b_type.is_unsigned(self) {
585 let b = self.context.new_cast(None, b, a_type);
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());
599 let res = self.current_func().new_local(None, b.get_type(), "andResult");
600 self.llbb().add_assignment(None, res, a & b);
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);
612 fn xor(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
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
621 fn fneg(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
622 self.cx.context.new_unary_op(None, UnaryOp::Minus, a.get_type(), a)
625 fn not(&mut self, a: RValue<'gcc>) -> RValue<'gcc> {
627 if a.get_type().is_bool() {
628 UnaryOp::LogicalNegate
631 UnaryOp::BitwiseNegate
633 self.cx.context.new_unary_op(None, operation, a.get_type(), a)
636 fn unchecked_sadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
640 fn unchecked_uadd(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
644 fn unchecked_ssub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
648 fn unchecked_usub(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
649 // TODO(antoyo): should generate poison value?
653 fn unchecked_smul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
657 fn unchecked_umul(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
661 fn fadd_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
665 fn fsub_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
669 fn fmul_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
673 fn fdiv_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
677 fn frem_fast(&mut self, _lhs: RValue<'gcc>, _rhs: RValue<'gcc>) -> RValue<'gcc> {
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::*};
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"),
692 // TODO(antoyo): remove duplication with intrinsic?
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",
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",
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",
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",
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",
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",
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")
750 let overflow = self.overflow_call(intrinsic, &[lhs, rhs, res], None);
751 (res.dereference(None).to_rvalue(), overflow)
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.
758 if ty == self.cx.u128_type || ty == self.cx.i128_type {
762 ty.get_aligned(align.bytes())
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)
769 fn dynamic_alloca(&mut self, _ty: Type<'gcc>, _align: Align) -> RValue<'gcc> {
773 fn array_alloca(&mut self, _ty: Type<'gcc>, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
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()
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()
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());
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])
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());
813 if place.layout.is_zst() {
814 return OperandRef::new_zst(self, place.layout);
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();
821 let range = scalar.valid_range_exclusive(bx);
822 if range.start != range.end {
823 bx.range_metadata(load, range);
826 abi::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
827 bx.nonnull_metadata(load);
834 if let Some(llextra) = place.llextra {
835 OperandValue::Ref(place.llval, Some(llextra), place.align)
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);
842 OperandValue::Immediate(self.to_immediate(load, place.layout))
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);
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 }
856 load(0, a, place.align),
857 load(1, b, place.align.restrict_for_offset(b_offset)),
861 OperandValue::Ref(place.llval, None, place.align)
864 OperandRef { val, layout: place.layout }
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;
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");
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);
882 self.br(header_bx.llbb());
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());
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));
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());
897 fn range_metadata(&mut self, _load: RValue<'gcc>, _range: Range<u128>) {
901 fn nonnull_metadata(&mut self, _load: RValue<'gcc>) {
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())
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())
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);
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);
930 .add_eval(None, self.context.new_call(None, atomic_store, &[ptr, value, ordering]));
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();
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() {
946 self.context.new_array_access(None, ptr, indices[0]).get_address(None)
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)
952 _ => unimplemented!(),
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();
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)
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)
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)
977 panic!("Unexpected type {:?}", value_type);
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)
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?
993 self.context.new_cast(None, value, dest_ty)
996 fn fptoui(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
997 self.context.new_cast(None, value, dest_ty)
1000 fn fptosi(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1001 self.context.new_cast(None, value, dest_ty)
1004 fn uitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1005 self.context.new_cast(None, value, dest_ty)
1008 fn sitofp(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1009 self.context.new_cast(None, value, dest_ty)
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)
1017 fn fpext(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1018 self.context.new_cast(None, value, dest_ty)
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)
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)
1029 fn bitcast(&mut self, value: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
1030 self.cx.const_bitcast(value, dest_ty)
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)
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)) {
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);
1047 // When they are not pointers, we want a transmute (or reinterpret_cast).
1048 self.bitcast(value, dest_ty)
1050 (true, true) => self.cx.context.new_cast(None, value, dest_ty),
1051 (true, false) => unimplemented!(),
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());
1063 self.context.new_comparison(None, op.to_gcc_comparison(), lhs, rhs)
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)
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);
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]));
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);
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()));
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]));
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]));
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);
1127 then_block.add_assignment(None, variable, then_val);
1128 then_block.end_with_jump(None, after_block);
1130 if then_val.get_type() != else_val.get_type() {
1131 else_val = self.context.new_cast(None, else_val, then_val.get_type());
1133 else_block.add_assignment(None, variable, else_val);
1134 else_block.end_with_jump(None, after_block);
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);
1141 variable.to_rvalue()
1145 fn va_arg(&mut self, _list: RValue<'gcc>, _ty: Type<'gcc>) -> RValue<'gcc> {
1149 fn extract_element(&mut self, _vec: RValue<'gcc>, _idx: RValue<'gcc>) -> RValue<'gcc> {
1153 fn vector_splat(&mut self, _num_elts: usize, _elt: RValue<'gcc>) -> RValue<'gcc> {
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();
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)
1167 else if value_type.is_vector().is_some() {
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()
1177 panic!("Unexpected type {:?}", value_type);
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()
1184 panic!("Unexpected type {:?}", value_type);
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();
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)
1198 else if value_type.is_vector().is_some() {
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))
1208 panic!("Unexpected type {:?}", value_type);
1212 panic!("Unexpected type {:?}", value_type);
1214 self.llbb().add_assignment(None, lvalue, value);
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")
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.
1230 fn set_cleanup(&mut self, _landing_pad: RValue<'gcc>) {
1234 fn resume(&mut self, _exn: RValue<'gcc>) -> RValue<'gcc> {
1238 fn cleanup_pad(&mut self, _parent: Option<RValue<'gcc>>, _args: &[RValue<'gcc>]) -> Funclet {
1242 fn cleanup_ret(&mut self, _funclet: &Funclet, _unwind: Option<Block<'gcc>>) -> RValue<'gcc> {
1246 fn catch_pad(&mut self, _parent: RValue<'gcc>, _args: &[RValue<'gcc>]) -> Funclet {
1250 fn catch_switch(&mut self, _parent: Option<RValue<'gcc>>, _unwind: Option<Block<'gcc>>, _num_handlers: usize) -> RValue<'gcc> {
1254 fn add_handler(&mut self, _catch_switch: RValue<'gcc>, _handler: Block<'gcc>) {
1258 fn set_personality_fn(&mut self, _personality: RValue<'gcc>) {
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);
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.
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);
1279 // TODO(antoyo): handle when value is not a struct.
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;
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),
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());
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())
1315 fn atomic_fence(&mut self, order: AtomicOrdering, scope: SynchronizationScope) {
1318 SynchronizationScope::SingleThread => "__atomic_signal_fence",
1319 SynchronizationScope::CrossThread => "__atomic_thread_fence",
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]));
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);
1332 fn lifetime_start(&mut self, _ptr: RValue<'gcc>, _size: Size) {
1336 fn lifetime_end(&mut self, _ptr: RValue<'gcc>, _size: Size) {
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)
1347 // If it's a not function that was defined, it's a function pointer.
1348 self.function_ptr_call(func, args, funclet)
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.
1359 self.context.new_cast(None, value, dest_typ)
1362 fn cx(&self) -> &CodegenCx<'gcc, 'tcx> {
1366 fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1370 fn set_span(&mut self, _span: Span) {}
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())
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());
1388 fn fptoui_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1392 fn fptosi_sat(&mut self, _val: RValue<'gcc>, _dest_ty: Type<'gcc>) -> Option<RValue<'gcc>> {
1396 fn instrprof_increment(&mut self, _fn_name: RValue<'gcc>, _hash: RValue<'gcc>, _num_counters: RValue<'gcc>, _index: RValue<'gcc>) {
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();
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"),
1409 let shuffle = self.context.new_function(None, FunctionType::Extern, return_type, ¶ms, "_mm_shuffle_epi8", false);
1410 self.context.new_call(None, shuffle, &[v1, v2, mask])
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)
1421 impl<'tcx> HasParamEnv<'tcx> for Builder<'_, '_, 'tcx> {
1422 fn param_env(&self) -> ParamEnv<'tcx> {
1427 impl<'tcx> HasTargetSpec for Builder<'_, '_, 'tcx> {
1428 fn target_spec(&self) -> &Target {
1429 &self.cx.target_spec()
1434 fn to_gcc_comparison(&self) -> ComparisonOp;
1437 impl ToGccComp for IntPredicate {
1438 fn to_gcc_comparison(&self) -> ComparisonOp {
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,
1454 impl ToGccComp for RealPredicate {
1455 fn to_gcc_comparison(&self) -> ComparisonOp {
1456 // TODO(antoyo): check that ordered vs non-ordered is respected.
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!(),
1479 #[allow(non_camel_case_types)]
1489 trait ToGccOrdering {
1490 fn to_gcc(self) -> i32;
1493 impl ToGccOrdering for AtomicOrdering {
1494 fn to_gcc(self) -> i32 {
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,