blob: ca98ae6319b7f65bd1821eec9bc931a90fb8b4eb [file]
pub mod llvm;
mod simd;
#[cfg(feature = "master")]
use std::iter;
use gccjit::{CType, ComparisonOp, Function, FunctionType, RValue, ToRValue, Type, UnaryOp};
use rustc_abi::{Align, BackendRepr, HasDataLayout};
use rustc_codegen_ssa::base::wants_msvc_seh;
use rustc_codegen_ssa::common::IntPredicate;
use rustc_codegen_ssa::diagnostics::InvalidMonomorphization;
use rustc_codegen_ssa::mir::IntrinsicResult;
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
#[cfg(feature = "master")]
use rustc_codegen_ssa::traits::MiscCodegenMethods;
use rustc_codegen_ssa::traits::{
ArgAbiBuilderMethods, BaseTypeCodegenMethods, BuilderMethods, ConstCodegenMethods,
IntrinsicCallBuilderMethods, LayoutTypeCodegenMethods, ReturnSlot,
};
use rustc_codegen_ssa::{MemFlags, RetagInfo};
use rustc_data_structures::fx::FxHashSet;
#[cfg(feature = "master")]
use rustc_middle::ty::layout::FnAbiOf;
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, Instance, Ty};
use rustc_session::config::OptLevel;
use rustc_span::{Span, Symbol, bug, span_bug, sym};
use rustc_target::callconv::{ArgAbi, PassMode};
use crate::abi::GccType;
use crate::builder::Builder;
use crate::common::{SignType, TypeReflection};
use crate::context::CodegenCx;
use crate::intrinsic::simd::generic_simd_intrinsic;
use crate::type_of::LayoutGccExt;
// GCC doesn't have the intrinsic we want so we use the compiler-builtins one
fn binop_libcall<'gcc, 'tcx>(
cx: &CodegenCx<'gcc, 'tcx>,
typ: Type<'gcc>,
name: &str,
) -> Function<'gcc> {
cx.context.new_function(
None,
FunctionType::Extern,
typ,
&[cx.context.new_parameter(None, typ, "a"), cx.context.new_parameter(None, typ, "b")],
name,
false,
)
}
fn get_simple_function_f128<'gcc, 'tcx>(
span: Span,
cx: &CodegenCx<'gcc, 'tcx>,
name: Symbol,
) -> Function<'gcc> {
let f128_type = cx.type_f128();
let (func_name, args): (&str, &[gccjit::Type<'_>]) = match name {
sym::copysign => ("copysignf128", &[f128_type, f128_type]),
sym::ceil => ("ceilf128", &[f128_type]),
sym::cos => ("cosf128", &[f128_type]),
sym::fabs => ("fabsf128", &[f128_type]),
sym::exp => ("expf128", &[f128_type]),
sym::exp2 => ("exp2f128", &[f128_type]),
sym::floor => ("floorf128", &[f128_type]),
sym::log => ("logf128", &[f128_type]),
sym::log2 => ("log2f128", &[f128_type]),
sym::log10 => ("log10f128", &[f128_type]),
sym::trunc => ("truncf128", &[f128_type]),
sym::round => ("roundf128", &[f128_type]),
sym::round_ties_even => ("roundevenf128", &[f128_type]),
sym::sin => ("sinf128", &[f128_type]),
sym::sqrt => ("sqrtf128", &[f128_type]),
sym::powf => ("powf128", &[f128_type, f128_type]),
sym::fma => ("fmaf128", &[f128_type, f128_type, f128_type]),
sym::minimum => ("fminimumf128", &[f128_type, f128_type]),
sym::maximum => ("fmaximumf128", &[f128_type, f128_type]),
_ => span_bug!(span, "used get_simple_function_f128 for unsupported f128 intrinsic"),
};
let args: Vec<_> = args
.iter()
.enumerate()
.map(|(index, typ)| cx.context.new_parameter(None, *typ, format!("param{}", index)))
.collect();
cx.context.new_function(None, FunctionType::Extern, f128_type, &args, func_name, false)
}
fn f16_builtin<'gcc, 'tcx>(
cx: &CodegenCx<'gcc, 'tcx>,
name: Symbol,
args: &[OperandRef<'tcx, RValue<'gcc>>],
) -> RValue<'gcc> {
let f32_type = cx.type_f32();
let builtin_name = match name {
sym::ceil => "__builtin_ceilf",
sym::copysign => "__builtin_copysignf",
sym::cos => "cosf",
sym::exp => "expf",
sym::exp2 => "exp2f",
sym::fabs => "fabsf",
sym::floor => "__builtin_floorf",
sym::log => "logf",
sym::log2 => "log2f",
sym::log10 => "log10f",
sym::powf => "__builtin_powf",
sym::round => "__builtin_roundf",
sym::round_ties_even => "__builtin_rintf",
sym::sin => "sinf",
sym::sqrt => "__builtin_sqrtf",
sym::trunc => "__builtin_truncf",
_ => unreachable!(),
};
let func = cx.context.get_builtin_function(builtin_name);
let args: Vec<_> =
args.iter().map(|arg| cx.context.new_cast(None, arg.immediate(), f32_type)).collect();
let result = cx.context.new_call(None, func, &args);
cx.context.new_cast(None, result, cx.type_f16())
}
impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
fn codegen_intrinsic_call(
&mut self,
instance: Instance<'tcx>,
args: &[OperandRef<'tcx, RValue<'gcc>>],
result_layout: ty::layout::TyAndLayout<'tcx>,
result_place: Option<PlaceValue<RValue<'gcc>>>,
span: Span,
) -> IntrinsicResult<'tcx, RValue<'gcc>> {
let tcx = self.tcx;
let result = PlaceRef { val: result_place.unwrap(), layout: result_layout };
let name = tcx.item_name(instance.def_id());
let name_str = name.as_str();
let fn_args = instance.args;
let value = match name {
sym::is_val_statically_known => {
let a = args[0].immediate();
let builtin = self.context.get_builtin_function("__builtin_constant_p");
let res = self.context.new_call(None, builtin, &[a]);
self.icmp(IntPredicate::IntEQ, res, self.const_i32(0))
}
sym::catch_unwind => {
try_intrinsic(
self,
args[0].immediate(),
args[1].immediate(),
args[2].immediate(),
result,
);
return IntrinsicResult::WroteIntoPlace;
}
sym::breakpoint | sym::resumable_breakpoint => {
unimplemented!();
}
sym::va_arg => {
let va_list = args[0].immediate();
let gcc_type = self.immediate_backend_type(result.layout);
self.va_arg(va_list, gcc_type)
}
sym::volatile_load | sym::unaligned_volatile_load => {
let ptr = args[0].immediate();
let abi_align = result_layout.align;
let ptr_align = if name == sym::volatile_load { abi_align } else { Align::ONE };
let load = self.volatile_load(result.layout.gcc_type(self), ptr, ptr_align);
if let BackendRepr::Scalar(scalar) = result.layout.backend_repr {
self.to_immediate_scalar(load, scalar)
} else {
load
}
}
sym::prefetch_read_data
| sym::prefetch_write_data
| sym::prefetch_read_instruction
| sym::prefetch_write_instruction => {
unimplemented!();
}
sym::ctlz
| sym::ctlz_nonzero
| sym::cttz
| sym::cttz_nonzero
| sym::ctpop
| sym::bswap
| sym::bitreverse
| sym::rotate_left
| sym::rotate_right
| sym::saturating_add
| sym::saturating_sub => {
match int_type_width_signed(args[0].layout.ty, self) {
Some((width, signed)) => match name {
sym::ctlz => self.count_leading_zeroes(width, args[0].immediate()),
sym::ctlz_nonzero => {
self.count_leading_zeroes_nonzero(width, args[0].immediate())
}
sym::cttz => self.count_trailing_zeroes(width, args[0].immediate()),
sym::cttz_nonzero => {
self.count_trailing_zeroes_nonzero(width, args[0].immediate())
}
sym::ctpop => self.ctpop(args[0].immediate()),
sym::bswap => {
if width == 8 {
args[0].immediate() // byte swap a u8/i8 is just a no-op
} else {
self.gcc_bswap(args[0].immediate(), width)
}
}
sym::bitreverse => self.bit_reverse(width, args[0].immediate()),
sym::rotate_left | sym::rotate_right => {
// Using optimized branchless algorithm from:
// https://blog.regehr.org/archives/1063
// This implementation uses the pattern (x<<n) | (x>>(-n&(width-1)))
// which generates efficient code for other platforms.
let is_left = name == sym::rotate_left;
let val = args[0].immediate();
let raw_shift = args[1].immediate();
if is_left {
self.rotate_left(val, raw_shift, width)
} else {
self.rotate_right(val, raw_shift, width)
}
}
sym::saturating_add => self.saturating_add(
args[0].immediate(),
args[1].immediate(),
signed,
width,
),
sym::saturating_sub => self.saturating_sub(
args[0].immediate(),
args[1].immediate(),
signed,
width,
),
_ => bug!(),
},
None => {
let err = tcx.dcx().emit_err(InvalidMonomorphization::BasicIntegerType {
span,
name,
ty: args[0].layout.ty,
});
return IntrinsicResult::Err(err);
}
}
}
sym::copysign
| sym::fabs
| sym::minimum
| sym::maximum
| sym::sqrt
| sym::powf
| sym::fma
| sym::fmuladd
| sym::floor
| sym::ceil
| sym::trunc
| sym::round
| sym::round_ties_even
| sym::exp
| sym::exp2
| sym::log
| sym::log10
| sym::log2
| sym::sin
| sym::cos
| sym::powi => 'float_op: {
let ty = args[0].layout.ty;
let ty::Float(float_ty) = *ty.kind() else {
span_bug!(span, "expected float type for {:?} intrinsic: {:?}", name, ty);
};
use ty::FloatTy::*;
let func = match (name, float_ty) {
(sym::copysign, F32) => self.context.get_builtin_function("copysignf"),
(sym::copysign, F64) => self.context.get_builtin_function("copysign"),
(sym::fabs, F32) => self.context.get_builtin_function("fabsf"),
(sym::fabs, F64) => self.context.get_builtin_function("fabs"),
// FIXME(antoyo): We can probably remove these and use the fallback intrinsic implementation.
(sym::minimum, F32) => binop_libcall(self, self.type_f32(), "fminimumf"),
(sym::minimum, F64) => binop_libcall(self, self.type_f64(), "fminimum"),
(sym::maximum, F32) => binop_libcall(self, self.type_f32(), "fmaximumf"),
(sym::maximum, F64) => binop_libcall(self, self.type_f64(), "fmaximum"),
// `f16` has no builtin for these, use the intrinsic fallback bodies instead.
(sym::minimum | sym::maximum, F16) => {
let fallback = Instance::new_raw(instance.def_id(), instance.args);
return IntrinsicResult::Fallback(fallback);
}
(sym::sqrt, F32) => self.context.get_builtin_function("sqrtf"),
(sym::sqrt, F64) => self.context.get_builtin_function("sqrt"),
(sym::powf, F32) => self.context.get_builtin_function("powf"),
(sym::powf, F64) => self.context.get_builtin_function("pow"),
(sym::fma, F32) => self.context.get_builtin_function("fmaf"),
(sym::fma, F64) => self.context.get_builtin_function("fma"),
// FIXME: calling `fma` from libc without FMA target feature uses expensive
// software emulation.
// FIXME: use gcc intrinsics analogous to llvm.fmuladd.f32/f64.
(sym::fmuladd, F32) => self.context.get_builtin_function("fmaf"),
(sym::fmuladd, F64) => self.context.get_builtin_function("fma"),
// These have no `f16` builtin and no `fmuladdf128`; use the fallback bodies.
(sym::fma | sym::fmuladd, F16) | (sym::fmuladd, F128) => {
let fallback = Instance::new_raw(instance.def_id(), instance.args);
return IntrinsicResult::Fallback(fallback);
}
(sym::powi, F32) => self.context.get_builtin_function("__builtin_powif"),
(sym::powi, F64) => self.context.get_builtin_function("__builtin_powi"),
// No GCC builtin; the intrinsic's fallback uses `compiler-builtins`.
(sym::powi, F128) => {
let fallback = Instance::new_raw(instance.def_id(), instance.args);
return IntrinsicResult::Fallback(fallback);
}
// `f16` can't go through `f16_builtin` due to the integer argument.
(sym::powi, F16) => {
let func = self.cx.context.get_builtin_function("__builtin_powif");
let arg0 =
self.cx.context.new_cast(None, args[0].immediate(), self.cx.type_f32());
let args = [arg0, args[1].immediate()];
let result = self.cx.context.new_call(None, func, &args);
break 'float_op self.cx.context.new_cast(None, result, self.cx.type_f16());
}
(sym::floor, F32) => self.context.get_builtin_function("floorf"),
(sym::floor, F64) => self.context.get_builtin_function("floor"),
(sym::ceil, F32) => self.context.get_builtin_function("ceilf"),
(sym::ceil, F64) => self.context.get_builtin_function("ceil"),
(sym::trunc, F32) => self.context.get_builtin_function("truncf"),
(sym::trunc, F64) => self.context.get_builtin_function("trunc"),
(sym::round, F32) => self.context.get_builtin_function("roundf"),
(sym::round, F64) => self.context.get_builtin_function("round"),
// We match the LLVM backend and lower this to `rint`.
(sym::round_ties_even, F32) => self.context.get_builtin_function("rintf"),
(sym::round_ties_even, F64) => self.context.get_builtin_function("rint"),
(sym::exp, F32) => self.context.get_builtin_function("expf"),
(sym::exp, F64) => self.context.get_builtin_function("exp"),
(sym::exp2, F32) => self.context.get_builtin_function("exp2f"),
(sym::exp2, F64) => self.context.get_builtin_function("exp2"),
(sym::log, F32) => self.context.get_builtin_function("logf"),
(sym::log, F64) => self.context.get_builtin_function("log"),
(sym::log10, F32) => self.context.get_builtin_function("log10f"),
(sym::log10, F64) => self.context.get_builtin_function("log10"),
(sym::log2, F32) => self.context.get_builtin_function("log2f"),
(sym::log2, F64) => self.context.get_builtin_function("log2"),
(sym::sin, F32) => self.context.get_builtin_function("sinf"),
(sym::sin, F64) => self.context.get_builtin_function("sin"),
(sym::cos, F32) => self.context.get_builtin_function("cosf"),
(sym::cos, F64) => self.context.get_builtin_function("cos"),
(_, F32 | F64) => unreachable!(),
(_, F16) => break 'float_op f16_builtin(self, name, args),
(_, F128) => {
if !self.cx.supports_f128_type {
// Fall back to default body
let fallback = Instance::new_raw(instance.def_id(), instance.args);
return IntrinsicResult::Fallback(fallback);
}
get_simple_function_f128(span, self, name)
}
};
self.cx.context.new_call(
self.location,
func,
&args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
)
}
sym::raw_eq => {
use rustc_abi::BackendRepr::*;
let tp_ty = fn_args.type_at(0);
let layout = self.layout_of(tp_ty).layout;
let _use_integer_compare = match layout.backend_repr() {
Scalar(_) | ScalarPair { a: _, b: _, b_offset: _ } => true,
SimdVector { .. } | SimdScalableVector { .. } => false,
Memory { .. } => {
// For rusty ABIs, small aggregates are actually passed
// as `RegKind::Integer` (see `FnAbi::adjust_for_abi`),
// so we re-use that same threshold here.
layout.size() <= self.data_layout().pointer_size() * 2
}
};
let a = args[0].immediate();
let b = args[1].immediate();
if layout.size().bytes() == 0 {
self.const_bool(true)
}
/*else if use_integer_compare {
let integer_ty = self.type_ix(layout.size.bits()); // FIXME(antoyo): LLVM creates an integer of 96 bits for [i32; 3], but gcc doesn't support this, so it creates an integer of 128 bits.
let ptr_ty = self.type_ptr_to(integer_ty);
let a_ptr = self.bitcast(a, ptr_ty);
let a_val = self.load(integer_ty, a_ptr, layout.align.abi);
let b_ptr = self.bitcast(b, ptr_ty);
let b_val = self.load(integer_ty, b_ptr, layout.align.abi);
self.icmp(IntPredicate::IntEQ, a_val, b_val)
}*/
else {
let void_ptr_type = self.context.new_type::<*const ()>();
let a_ptr = self.bitcast(a, void_ptr_type);
let b_ptr = self.bitcast(b, void_ptr_type);
let n = self.context.new_cast(
None,
self.const_usize(layout.size().bytes()),
self.sizet_type,
);
let builtin = self.context.get_builtin_function("memcmp");
let cmp = self.context.new_call(None, builtin, &[a_ptr, b_ptr, n]);
self.icmp(IntPredicate::IntEQ, cmp, self.const_i32(0))
}
}
sym::compare_bytes => {
let a = args[0].immediate();
let b = args[1].immediate();
let n = args[2].immediate();
let void_ptr_type = self.context.new_type::<*const ()>();
let a_ptr = self.bitcast(a, void_ptr_type);
let b_ptr = self.bitcast(b, void_ptr_type);
// Here we assume that the `memcmp` provided by the target is a NOP for size 0.
let builtin = self.context.get_builtin_function("memcmp");
let cmp = self.context.new_call(None, builtin, &[a_ptr, b_ptr, n]);
self.sext(cmp, self.type_ix(32))
}
sym::black_box => {
args[0].val.store(self, result);
let block = self.llbb();
let extended_asm = block.add_extended_asm(None, "");
extended_asm.add_input_operand(None, "r", result.val.llval);
extended_asm.add_clobber("memory");
extended_asm.set_volatile_flag(true);
// We have copied the value to `result` already.
return IntrinsicResult::WroteIntoPlace;
}
sym::ptr_mask => {
let usize_type = self.context.new_type::<usize>();
let void_ptr_type = self.context.new_type::<*const ()>();
let ptr = args[0].immediate();
let mask = args[1].immediate();
let addr = self.bitcast(ptr, usize_type);
let masked = self.and(addr, mask);
self.bitcast(masked, void_ptr_type)
}
_ if name_str.starts_with("simd_") => {
match generic_simd_intrinsic(
self,
name,
args,
result.layout.ty,
result.layout.gcc_type(self),
span,
) {
Ok(value) => value,
Err(err) => return IntrinsicResult::Err(err),
}
}
// Fall back to default body
_ => {
let fallback = Instance::new_raw(instance.def_id(), instance.args);
return IntrinsicResult::Fallback(fallback);
}
};
if result.layout.ty.is_bool() {
let val = self.from_immediate(value);
self.store_to_place(val, result.val);
} else if !result.layout.ty.is_unit() {
self.store_to_place(value, result.val);
}
IntrinsicResult::WroteIntoPlace
}
fn codegen_llvm_intrinsic_call(
&mut self,
instance: ty::Instance<'tcx>,
args: &[OperandRef<'tcx, Self::Value>],
is_cleanup: bool,
) -> Self::Value {
let func = if let Some(&func) = self.intrinsic_instances.borrow().get(&instance) {
func
} else {
let sym = self.tcx.symbol_name(instance).name;
let func = if let Some(func) = self.intrinsics.borrow().get(sym) {
*func
} else {
self.linkage.set(FunctionType::Extern);
let func = match sym {
"llvm.fma.f16" => {
// fma is not a target builtin, but a normal builtin, so we handle it differently
// here.
self.context.get_builtin_function("fma")
}
_ => llvm::intrinsic(sym, self),
};
self.intrinsics.borrow_mut().insert(sym.to_string(), func);
self.on_stack_function_params.borrow_mut().insert(func, FxHashSet::default());
crate::attributes::from_fn_attrs(self, func, instance, None);
func
};
self.intrinsic_instances.borrow_mut().insert(instance, func);
func
};
let fn_ptr = func.get_address(None);
let fn_ty = fn_ptr.get_type();
let call_args = args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>();
// FIXME directly use the llvm intrinsic adjustment functions here
let llret =
self.call(fn_ty, None, None, fn_ptr, ReturnSlot::Direct, &call_args, None, None);
if is_cleanup {
self.apply_attrs_to_cleanup_callsite(llret);
}
llret
}
fn abort_immediate(&mut self) {
let func = self.context.get_builtin_function("__builtin_trap");
self.block.add_eval(self.location, self.context.new_call(self.location, func, &[]));
}
fn assume(&mut self, value: Self::Value) {
// libgccjit currently has no direct equivalent of LLVM's `llvm.assume`,
// so use the idiom `if (!cond) __builtin_unreachable()`.
// FIXME: this should use IFN_ASSUME when we have internal functions in
// libgccjit.
if self.sess().opts.optimize == OptLevel::No {
return;
}
let then_block = self.append_sibling_block("assume_holds");
let unreachable_block = self.append_sibling_block("assume_violated");
self.block.end_with_conditional(self.location, value, then_block, unreachable_block);
self.switch_to_block(unreachable_block);
self.unreachable();
self.switch_to_block(then_block);
}
fn expect(&mut self, cond: Self::Value, _expected: bool) -> Self::Value {
// FIXME(antoyo)
cond
}
fn ctpop(&mut self, val: Self::Value) -> Self::Value {
self.pop_count(val)
}
fn type_checked_load(
&mut self,
_vtable: Self::Value,
_vtable_byte_offset: u64,
_typeid: &[u8],
) -> Self::Value {
// Unsupported.
self.context.new_rvalue_from_int(self.int_type, 0)
}
fn va_start(&mut self, va_list: RValue<'gcc>) {
let func = self.context.get_builtin_function("__builtin_va_start");
let va_list_type = self.context.new_c_type(CType::VaList);
let va_list = self.context.new_cast(self.location, va_list, va_list_type.make_pointer());
// Pre-C23 requires that the last "normal" argument was passed to va_start.
// Just pass 0, this appears to be handled correctly.
let last_normal_arg = self.context.new_rvalue_from_int(self.int_type, 0);
let call = self.context.new_call(self.location, func, &[va_list, last_normal_arg]);
self.block.add_eval(self.location, call);
}
fn retag_reg(&mut self, _ptr: Self::Value, _info: &RetagInfo<Self::Value>) -> Self::Value {
unimplemented!()
}
fn retag_mem(&mut self, _ptr: Self::Value, _info: &RetagInfo<Self::Value>) {
unimplemented!()
}
}
impl<'a, 'gcc, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
fn store_fn_arg(
&mut self,
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
idx: &mut usize,
dst: PlaceRef<'tcx, Self::Value>,
) {
arg_abi.store_fn_arg(self, idx, dst)
}
fn store_arg(
&mut self,
arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
val: RValue<'gcc>,
dst: PlaceRef<'tcx, RValue<'gcc>>,
) {
arg_abi.store(self, val, dst)
}
}
pub trait ArgAbiExt<'gcc, 'tcx> {
fn store(
&self,
bx: &mut Builder<'_, 'gcc, 'tcx>,
val: RValue<'gcc>,
dst: PlaceRef<'tcx, RValue<'gcc>>,
);
fn store_fn_arg(
&self,
bx: &mut Builder<'_, 'gcc, 'tcx>,
idx: &mut usize,
dst: PlaceRef<'tcx, RValue<'gcc>>,
);
}
impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
/// Stores a direct/indirect value described by this ArgAbi into a
/// place for the original Rust type of this argument/return.
/// Can be used for both storing formal arguments into Rust variables
/// or results of call/invoke instructions into their destinations.
fn store(
&self,
bx: &mut Builder<'_, 'gcc, 'tcx>,
val: RValue<'gcc>,
dst: PlaceRef<'tcx, RValue<'gcc>>,
) {
if self.is_ignore() {
return;
}
if self.is_sized_indirect() {
OperandValue::Ref(PlaceValue::new_sized(val, self.layout.align)).store(bx, dst)
} else if self.is_unsized_indirect() {
bug!("unsized `ArgAbi` cannot be stored");
} else if let PassMode::Cast { ref cast, .. } = self.mode {
// FIXME(eddyb): Figure out when the simpler Store is safe, clang
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
let can_store_through_cast_ptr = false;
if can_store_through_cast_ptr {
let cast_ptr_llty = bx.type_ptr_to(cast.gcc_type(bx));
let cast_dst = bx.pointercast(dst.val.llval, cast_ptr_llty);
bx.store(val, cast_dst, self.layout.align);
} else {
// The actual return type is a struct, but the ABI
// adaptation code has cast it into some scalar type. The
// code that follows is the only reliable way I have
// found to do a transform like i64 -> {i32,i32}.
// Basically we dump the data onto the stack then memcpy it.
//
// Other approaches I tried:
// - Casting rust ret pointer to the foreign type and using Store
// is (a) unsafe if size of foreign type > size of rust type and
// (b) runs afoul of strict aliasing rules, yielding invalid
// assembly under -O (specifically, the store gets removed).
// - Truncating foreign type to correct integral type and then
// bitcasting to the struct type yields invalid cast errors.
// We instead thus allocate some scratch space...
let scratch_size = cast.size(bx);
let scratch_align = cast.align(bx);
let scratch = bx.alloca(scratch_size, scratch_align);
bx.lifetime_start(scratch, scratch_size);
// ... where we first store the value...
rustc_codegen_ssa::mir::store_cast(bx, cast, val, scratch, scratch_align);
// ... and then memcpy it to the intended destination.
bx.memcpy(
dst.val.llval,
self.layout.align,
scratch,
scratch_align,
bx.const_usize(self.layout.size.bytes()),
MemFlags::empty(),
None,
);
bx.lifetime_end(scratch, scratch_size);
}
} else {
OperandValue::Immediate(val).store(bx, dst);
}
}
fn store_fn_arg<'a>(
&self,
bx: &mut Builder<'a, 'gcc, 'tcx>,
idx: &mut usize,
dst: PlaceRef<'tcx, RValue<'gcc>>,
) {
let func = bx.current_func();
// The return pointer counted by cg_ssa is a hidden parameter in GCC.
let hidden_params = bx.functions_with_indirect_return.borrow().contains_key(&func) as usize;
let mut next = || {
let val = func.get_param((*idx - hidden_params) as i32);
*idx += 1;
val.to_rvalue()
};
match self.mode {
PassMode::Ignore => {}
PassMode::Pair(..) => {
OperandValue::Pair(next(), next()).store(bx, dst);
}
PassMode::IndirectUnsized { .. } => {
bug!("unsized `ArgAbi` cannot be stored");
}
PassMode::Direct(_) | PassMode::Indirect { .. } | PassMode::Cast { .. } => {
let next_arg = next();
self.store(bx, next_arg, dst);
}
}
}
}
fn int_type_width_signed<'gcc, 'tcx>(
ty: Ty<'tcx>,
cx: &CodegenCx<'gcc, 'tcx>,
) -> Option<(u64, bool)> {
match *ty.kind() {
ty::Int(t) => Some((
match t {
rustc_middle::ty::IntTy::Isize => u64::from(cx.tcx.sess.target.pointer_width),
rustc_middle::ty::IntTy::I8 => 8,
rustc_middle::ty::IntTy::I16 => 16,
rustc_middle::ty::IntTy::I32 => 32,
rustc_middle::ty::IntTy::I64 => 64,
rustc_middle::ty::IntTy::I128 => 128,
},
true,
)),
ty::Uint(t) => Some((
match t {
rustc_middle::ty::UintTy::Usize => u64::from(cx.tcx.sess.target.pointer_width),
rustc_middle::ty::UintTy::U8 => 8,
rustc_middle::ty::UintTy::U16 => 16,
rustc_middle::ty::UintTy::U32 => 32,
rustc_middle::ty::UintTy::U64 => 64,
rustc_middle::ty::UintTy::U128 => 128,
},
false,
)),
_ => None,
}
}
impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
fn bit_reverse(&mut self, width: u64, value: RValue<'gcc>) -> RValue<'gcc> {
let result_type = value.get_type();
let typ = result_type.to_unsigned(self.cx);
let value =
if result_type.is_signed(self.cx) { self.gcc_int_cast(value, typ) } else { value };
let context = &self.cx.context;
let result = match width {
8 | 16 | 32 | 64 => {
let mask = ((1u128 << width) - 1) as u64;
let (m0, m1, m2) = if width > 16 {
(
context.new_rvalue_from_long(typ, (0x5555555555555555u64 & mask) as i64),
context.new_rvalue_from_long(typ, (0x3333333333333333u64 & mask) as i64),
context.new_rvalue_from_long(typ, (0x0f0f0f0f0f0f0f0fu64 & mask) as i64),
)
} else {
(
context.new_rvalue_from_int(typ, (0x5555u64 & mask) as i32),
context.new_rvalue_from_int(typ, (0x3333u64 & mask) as i32),
context.new_rvalue_from_int(typ, (0x0f0fu64 & mask) as i32),
)
};
let one = context.new_rvalue_from_int(typ, 1);
let two = context.new_rvalue_from_int(typ, 2);
let four = context.new_rvalue_from_int(typ, 4);
// First step.
let left = self.lshr(value, one);
let left = self.and(left, m0);
let right = self.and(value, m0);
let right = self.shl(right, one);
let step1 = self.or(left, right);
// Second step.
let left = self.lshr(step1, two);
let left = self.and(left, m1);
let right = self.and(step1, m1);
let right = self.shl(right, two);
let step2 = self.or(left, right);
// Third step.
let left = self.lshr(step2, four);
let left = self.and(left, m2);
let right = self.and(step2, m2);
let right = self.shl(right, four);
let step3 = self.or(left, right);
// Fourth step.
if width == 8 { step3 } else { self.gcc_bswap(step3, width) }
}
128 => {
// FIXME(antoyo): find a more efficient implementation?
let sixty_four = self.gcc_int(typ, 64);
let right_shift = self.gcc_lshr(value, sixty_four);
let high = self.gcc_int_cast(right_shift, self.u64_type);
let low = self.gcc_int_cast(value, self.u64_type);
let reversed_high = self.bit_reverse(64, high);
let reversed_low = self.bit_reverse(64, low);
let new_low = self.gcc_int_cast(reversed_high, typ);
let new_high = self.shl(self.gcc_int_cast(reversed_low, typ), sixty_four);
self.gcc_or(new_low, new_high, self.location)
}
_ => {
panic!("cannot bit reverse with width = {}", width);
}
};
self.gcc_int_cast(result, result_type)
}
fn count_zeroes(&mut self, width: u64, arg: RValue<'gcc>, count_leading: bool) -> RValue<'gcc> {
// if arg is 0, early return 0, else call count_leading_zeroes_nonzero or count_trailing_zeroes_nonzero
let func = self.current_func();
let then_block = func.new_block("then");
let else_block = func.new_block("else");
let after_block = func.new_block("after");
let result = self.new_temp(func, None, self.u32_type);
let zero = self.cx.gcc_zero(arg.get_type());
let cond = self.gcc_icmp(IntPredicate::IntEQ, arg, zero);
self.llbb().end_with_conditional(None, cond, then_block, else_block);
let zero_result = self.cx.gcc_uint(self.u32_type, width);
then_block.add_assignment(None, result, zero_result);
then_block.end_with_jump(None, after_block);
// NOTE: since jumps were added in a place count_xxxxing_zeroes_nonzero() does not expect,
// the current block in the state need to be updated.
self.switch_to_block(else_block);
let zeros = if count_leading {
self.count_leading_zeroes_nonzero(width, arg)
} else {
self.count_trailing_zeroes_nonzero(width, arg)
};
self.llbb().add_assignment(None, result, zeros);
self.llbb().end_with_jump(None, after_block);
// NOTE: since jumps were added in a place rustc does not
// expect, the current block in the state need to be updated.
self.switch_to_block(after_block);
result.to_rvalue()
}
fn count_zeroes_nonzero(
&mut self,
width: u64,
arg: RValue<'gcc>,
count_leading: bool,
) -> RValue<'gcc> {
// Pre-condition: arg is guaranteed to not be 0 by caller
fn use_builtin_function<'a, 'gcc, 'tcx>(
builder: &mut Builder<'a, 'gcc, 'tcx>,
builtin: &str,
arg: RValue<'gcc>,
arg_type: gccjit::Type<'gcc>,
expected_type: gccjit::Type<'gcc>,
) -> RValue<'gcc> {
let arg = if arg_type != expected_type {
builder.context.new_cast(builder.location, arg, expected_type)
} else {
arg
};
let builtin = builder.context.get_builtin_function(builtin);
let res = builder.context.new_call(builder.location, builtin, &[arg]);
builder.context.new_cast(builder.location, res, builder.u32_type)
}
// FIXME(antoyo): use width?
let result_type = self.u32_type;
let mut arg_type = arg.get_type();
let arg = if arg_type.is_signed(self.cx) {
arg_type = arg_type.to_unsigned(self.cx);
self.gcc_int_cast(arg, arg_type)
} else {
arg
};
// FIXME(antoyo): write a new function Type::is_compatible_with(&Type) and use it here
// instead of using is_uint().
if arg_type.is_uchar(self.cx) || arg_type.is_ushort(self.cx) || arg_type.is_uint(self.cx) {
let builtin = if count_leading { "__builtin_clz" } else { "__builtin_ctz" };
use_builtin_function(self, builtin, arg, arg_type, self.cx.uint_type)
} else if arg_type.is_ulong(self.cx) {
let builtin = if count_leading { "__builtin_clzl" } else { "__builtin_ctzl" };
use_builtin_function(self, builtin, arg, arg_type, self.cx.uint_type)
} else if arg_type.is_ulonglong(self.cx) {
let builtin = if count_leading { "__builtin_clzll" } else { "__builtin_ctzll" };
use_builtin_function(self, builtin, arg, arg_type, self.cx.uint_type)
} else if width == 128 {
// arg is guaranteed to not be 0, so either its 64 high or 64 low bits are not 0
// __buildin_clzll is UB when called with 0, so call it on the 64 high bits if they are not 0,
// else call it on the 64 low bits and add 64. In the else case, 64 low bits can't be 0
// because arg is not 0.
// __buildin_ctzll is UB when called with 0, so call it on the 64 low bits if they are not 0,
// else call it on the 64 high bits and add 64. In the else case, 64 high bits can't be 0
// because arg is not 0.
let result = self.new_temp(self.current_func(), None, result_type);
let cz_then_block = self.current_func().new_block("cz_then");
let cz_else_block = self.current_func().new_block("cz_else");
let cz_after_block = self.current_func().new_block("cz_after");
let low = self.gcc_int_cast(arg, self.u64_type);
let sixty_four = self.const_uint(arg_type, 64);
let shift = self.lshr(arg, sixty_four);
let high = self.gcc_int_cast(shift, self.u64_type);
let (first, second, builtin) = if count_leading {
(low, high, self.context.get_builtin_function("__builtin_clzll"))
} else {
(high, low, self.context.get_builtin_function("__builtin_ctzll"))
};
let zero_64 = self.const_uint(self.u64_type, 0);
let cond = self.gcc_icmp(IntPredicate::IntNE, second, zero_64);
self.llbb().end_with_conditional(self.location, cond, cz_then_block, cz_else_block);
self.switch_to_block(cz_then_block);
let result_128 =
self.gcc_int_cast(self.context.new_call(None, builtin, &[second]), result_type);
cz_then_block.add_assignment(self.location, result, result_128);
cz_then_block.end_with_jump(self.location, cz_after_block);
self.switch_to_block(cz_else_block);
let count_more_zeroes =
self.gcc_int_cast(self.context.new_call(None, builtin, &[first]), result_type);
let sixty_four_result_type = self.const_uint(result_type, 64);
let count_result_type = self.add(count_more_zeroes, sixty_four_result_type);
cz_else_block.add_assignment(self.location, result, count_result_type);
cz_else_block.end_with_jump(self.location, cz_after_block);
self.switch_to_block(cz_after_block);
result.to_rvalue()
} else {
let byte_diff = self.ulonglong_type.get_size() as i64 - arg_type.get_size() as i64;
let diff = self.context.new_rvalue_from_long(self.int_type, byte_diff * 8);
let ull_arg = self.context.new_cast(self.location, arg, self.ulonglong_type);
let res = if count_leading {
let count_leading_zeroes = self.context.get_builtin_function("__builtin_clzll");
self.context.new_call(self.location, count_leading_zeroes, &[ull_arg]) - diff
} else {
let count_trailing_zeroes = self.context.get_builtin_function("__builtin_ctzll");
let mask = self.context.new_rvalue_from_long(arg_type, -1); // To get the value with all bits set.
let masked = mask
& self.context.new_unary_op(
self.location,
UnaryOp::BitwiseNegate,
arg_type,
arg,
);
let cond =
self.context.new_comparison(self.location, ComparisonOp::Equals, masked, mask);
let diff = diff * self.context.new_cast(self.location, cond, self.int_type);
self.context.new_call(self.location, count_trailing_zeroes, &[ull_arg]) - diff
};
self.context.new_cast(self.location, res, result_type)
}
}
fn count_leading_zeroes(&mut self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
self.count_zeroes(width, arg, true)
}
fn count_leading_zeroes_nonzero(&mut self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
// Pre-condition: arg is guaranteed to not be 0 by caller, else count_leading_zeros should be used
self.count_zeroes_nonzero(width, arg, true)
}
fn count_trailing_zeroes(&mut self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
self.count_zeroes(width, arg, false)
}
fn count_trailing_zeroes_nonzero(&mut self, width: u64, arg: RValue<'gcc>) -> RValue<'gcc> {
// Pre-condition: arg is guaranteed to not be 0 by caller, else count_trailing_zeros should be used
self.count_zeroes_nonzero(width, arg, false)
}
fn pop_count(&mut self, value: RValue<'gcc>) -> RValue<'gcc> {
// FIXME(antoyo): use the optimized version with fewer operations.
let result_type = self.u32_type;
let arg_type = value.get_type();
let value_type = arg_type.to_unsigned(self.cx);
let value =
if arg_type.is_signed(self.cx) { self.gcc_int_cast(value, value_type) } else { value };
// only break apart 128-bit ints if they're not natively supported
// FIXME(antoyo): remove this if/when native 128-bit integers land in libgccjit
if value_type.is_u128(self.cx) && !self.cx.supports_128bit_integers {
let sixty_four = self.gcc_int(value_type, 64);
let right_shift = self.gcc_lshr(value, sixty_four);
let high = self.gcc_int_cast(right_shift, self.cx.ulonglong_type);
let high = self.pop_count(high);
let low = self.gcc_int_cast(value, self.cx.ulonglong_type);
let low = self.pop_count(low);
let res = high + low;
return self.gcc_int_cast(res, result_type);
}
// Use Wenger's algorithm for population count, gcc's seems to play better with it
// for (int counter = 0; value != 0; counter++) {
// value &= value - 1;
// }
let func = self.current_func();
let loop_head = func.new_block("head");
let loop_body = func.new_block("body");
let loop_tail = func.new_block("tail");
let counter_type = self.int_type;
let counter = self.new_temp(self.current_func(), None, counter_type);
let val = self.new_temp(self.current_func(), None, value_type);
let zero = self.gcc_zero(counter_type);
self.llbb().add_assignment(self.location, counter, zero);
self.llbb().add_assignment(self.location, val, value);
self.br(loop_head);
// check if value isn't zero
self.switch_to_block(loop_head);
let zero = self.gcc_zero(value_type);
let cond = self.gcc_icmp(IntPredicate::IntNE, val.to_rvalue(), zero);
self.cond_br(cond, loop_body, loop_tail);
// val &= val - 1;
self.switch_to_block(loop_body);
let one = self.gcc_int(value_type, 1);
let sub = self.gcc_sub(val.to_rvalue(), one);
let op = self.gcc_and(val.to_rvalue(), sub);
loop_body.add_assignment(self.location, val, op);
// counter += 1
let one = self.gcc_int(counter_type, 1);
let op = self.gcc_add(counter.to_rvalue(), one);
loop_body.add_assignment(self.location, counter, op);
self.br(loop_head);
// end of loop
self.switch_to_block(loop_tail);
self.gcc_int_cast(counter.to_rvalue(), result_type)
}
// Algorithm from: https://blog.regehr.org/archives/1063
fn rotate_left(
&mut self,
value: RValue<'gcc>,
shift: RValue<'gcc>,
width: u64,
) -> RValue<'gcc> {
let max = self.const_uint(shift.get_type(), width);
let shift = self.urem(shift, max);
let lhs = self.shl(value, shift);
let result_neg = self.neg(shift);
let result_and = self.and(result_neg, self.const_uint(shift.get_type(), width - 1));
let rhs = self.lshr(value, result_and);
self.or(lhs, rhs)
}
// Algorithm from: https://blog.regehr.org/archives/1063
fn rotate_right(
&mut self,
value: RValue<'gcc>,
shift: RValue<'gcc>,
width: u64,
) -> RValue<'gcc> {
let max = self.const_uint(shift.get_type(), width);
let shift = self.urem(shift, max);
let lhs = self.lshr(value, shift);
let result_neg = self.neg(shift);
let result_and = self.and(result_neg, self.const_uint(shift.get_type(), width - 1));
let rhs = self.shl(value, result_and);
self.or(lhs, rhs)
}
fn saturating_add(
&mut self,
lhs: RValue<'gcc>,
rhs: RValue<'gcc>,
signed: bool,
width: u64,
) -> RValue<'gcc> {
let result_type = lhs.get_type();
if signed {
// Based on algorithm from: https://stackoverflow.com/a/56531252/389119
let func = self.current_func();
let res = func.new_local(self.location, result_type, "saturating_sum");
let supports_native_type = self.is_native_int_type(result_type);
let overflow = if supports_native_type {
let func_name = "__builtin_add_overflow";
let overflow_func = self.context.get_builtin_function(func_name);
self.overflow_call(overflow_func, &[lhs, rhs, res.get_address(self.location)], None)
} else {
let func_name = match width {
128 => "__rust_i128_addo",
_ => unreachable!(),
};
let (int_result, overflow) =
self.operation_with_overflow(func_name, lhs, rhs, width);
self.llbb().add_assignment(self.location, res, int_result);
overflow
};
let then_block = func.new_block("then");
let after_block = func.new_block("after");
// Return `result_type`'s maximum or minimum value on overflow
// NOTE: convert the type to unsigned to have an unsigned shift.
let unsigned_type = result_type.to_unsigned(self.cx);
let shifted = self.gcc_lshr(
self.gcc_int_cast(lhs, unsigned_type),
self.gcc_int(unsigned_type, width as i64 - 1),
);
let uint_max = self.gcc_not(self.gcc_int(unsigned_type, 0));
let int_max = self.gcc_lshr(uint_max, self.gcc_int(unsigned_type, 1));
then_block.add_assignment(
self.location,
res,
self.gcc_int_cast(self.gcc_add(shifted, int_max), result_type),
);
then_block.end_with_jump(self.location, after_block);
self.llbb().end_with_conditional(self.location, overflow, then_block, after_block);
// NOTE: since jumps were added in a place rustc does not
// expect, the current block in the state need to be updated.
self.switch_to_block(after_block);
res.to_rvalue()
} else {
// Algorithm from: http://locklessinc.com/articles/sat_arithmetic/
let res = self.gcc_add(lhs, rhs);
let cond = self.gcc_icmp(IntPredicate::IntULT, res, lhs);
let value = self.gcc_neg(self.gcc_int_cast(cond, result_type));
self.gcc_or(res, value, self.location)
}
}
// Algorithm from: https://locklessinc.com/articles/sat_arithmetic/
fn saturating_sub(
&mut self,
lhs: RValue<'gcc>,
rhs: RValue<'gcc>,
signed: bool,
width: u64,
) -> RValue<'gcc> {
let result_type = lhs.get_type();
if signed {
// Based on algorithm from: https://stackoverflow.com/a/56531252/389119
let func = self.current_func();
let res = func.new_local(self.location, result_type, "saturating_diff");
let supports_native_type = self.is_native_int_type(result_type);
let overflow = if supports_native_type {
let func_name = "__builtin_sub_overflow";
let overflow_func = self.context.get_builtin_function(func_name);
self.overflow_call(overflow_func, &[lhs, rhs, res.get_address(self.location)], None)
} else {
let func_name = match width {
128 => "__rust_i128_subo",
_ => unreachable!(),
};
let (int_result, overflow) =
self.operation_with_overflow(func_name, lhs, rhs, width);
self.llbb().add_assignment(self.location, res, int_result);
overflow
};
let then_block = func.new_block("then");
let after_block = func.new_block("after");
// Return `result_type`'s maximum or minimum value on overflow
// NOTE: convert the type to unsigned to have an unsigned shift.
let unsigned_type = result_type.to_unsigned(self.cx);
let shifted = self.gcc_lshr(
self.gcc_int_cast(lhs, unsigned_type),
self.gcc_int(unsigned_type, width as i64 - 1),
);
let uint_max = self.gcc_not(self.gcc_int(unsigned_type, 0));
let int_max = self.gcc_lshr(uint_max, self.gcc_int(unsigned_type, 1));
then_block.add_assignment(
self.location,
res,
self.gcc_int_cast(self.gcc_add(shifted, int_max), result_type),
);
then_block.end_with_jump(self.location, after_block);
self.llbb().end_with_conditional(self.location, overflow, then_block, after_block);
// NOTE: since jumps were added in a place rustc does not
// expect, the current block in the state need to be updated.
self.switch_to_block(after_block);
res.to_rvalue()
} else {
let res = self.gcc_sub(lhs, rhs);
let comparison = self.gcc_icmp(IntPredicate::IntULE, res, lhs);
let value = self.gcc_neg(self.gcc_int_cast(comparison, result_type));
self.gcc_and(res, value)
}
}
}
fn try_intrinsic<'a, 'b, 'gcc, 'tcx>(
bx: &'b mut Builder<'a, 'gcc, 'tcx>,
try_func: RValue<'gcc>,
data: RValue<'gcc>,
_catch_func: RValue<'gcc>,
dest: PlaceRef<'tcx, RValue<'gcc>>,
) {
if !bx.sess().panic_strategy().unwinds() {
let param_type = bx.u8_type.make_pointer();
let fn_type =
bx.context.new_function_pointer_type(None, bx.type_void(), &[param_type], false);
bx.call(fn_type, None, None, try_func, ReturnSlot::Direct, &[data], None, None);
// Return 0 unconditionally from the intrinsic call;
// we can never unwind.
OperandValue::Immediate(bx.const_bool(false)).store(bx, dest);
} else {
if wants_msvc_seh(&bx.sess().target) {
unimplemented!();
}
#[cfg(feature = "master")]
codegen_gnu_try(bx, try_func, data, _catch_func, dest);
#[cfg(not(feature = "master"))]
unimplemented!();
}
}
// Definition of the standard `try` function for Rust using the GNU-like model
// of exceptions (e.g., the normal semantics of LLVM's `landingpad` and `invoke`
// instructions).
//
// This codegen is a little surprising because we always call a shim
// function instead of inlining the call to `invoke` manually here. This is done
// because in LLVM we're only allowed to have one personality per function
// definition. The call to the `try` intrinsic is being inlined into the
// function calling it, and that function may already have other personality
// functions in play. By calling a shim we're guaranteed that our shim will have
// the right personality function.
#[cfg(feature = "master")]
fn codegen_gnu_try<'gcc, 'tcx>(
bx: &mut Builder<'_, 'gcc, 'tcx>,
try_func: RValue<'gcc>,
data: RValue<'gcc>,
catch_func: RValue<'gcc>,
dest: PlaceRef<'tcx, RValue<'gcc>>,
) {
let cx: &CodegenCx<'gcc, '_> = bx.cx;
let func = get_rust_try_fn(cx, &mut |mut bx| {
// Codegens the shims described above:
//
// bx:
// invoke %try_func(%data) normal %normal unwind %catch
//
// normal:
// ret 0
//
// catch:
// (%ptr, _) = landingpad
// call %catch_func(%data, %ptr)
// ret 1
let then = bx.append_sibling_block("then");
let catch = bx.append_sibling_block("catch");
let func = bx.current_func();
let try_func = func.get_param(0).to_rvalue();
let data = func.get_param(1).to_rvalue();
let catch_func = func.get_param(2).to_rvalue();
let try_func_ty = bx.type_func(&[bx.type_i8p()], bx.type_void());
let current_block = bx.block;
bx.switch_to_block(then);
bx.ret(bx.const_bool(false));
// Type indicator for the exception being thrown.
//
// The value is a pointer to the exception object
// being thrown.
bx.switch_to_block(catch);
bx.set_personality_fn(bx.eh_personality());
let eh_pointer_builtin = bx.cx.context.get_target_builtin_function("__builtin_eh_pointer");
let zero = bx.cx.context.new_rvalue_zero(bx.int_type);
let ptr = bx.cx.context.new_call(None, eh_pointer_builtin, &[zero]);
let catch_ty = bx.type_func(&[bx.type_i8p(), bx.type_i8p()], bx.type_void());
bx.call(catch_ty, None, None, catch_func, ReturnSlot::Direct, &[data, ptr], None, None);
bx.ret(bx.const_bool(true));
// NOTE: the blocks must be filled before adding the try/catch, otherwise gcc will not
// generate a try/catch.
// FIXME(antoyo): add a check in the libgccjit API to prevent this.
bx.switch_to_block(current_block);
bx.invoke(
try_func_ty,
None,
None,
try_func,
ReturnSlot::Direct,
&[data],
then,
catch,
None,
None,
);
});
let func = func.get_address(None);
// Note that no invoke is used here because by definition this function
// can't panic (that's what it's catching).
let ret = bx.call(
func.get_type(),
None,
None,
func,
ReturnSlot::Direct,
&[try_func, data, catch_func],
None,
None,
);
OperandValue::Immediate(ret).store(bx, dest);
}
// Helper function used to get a handle to the `__rust_try` function used to
// catch exceptions.
//
// This function is only generated once and is then cached.
#[cfg(feature = "master")]
fn get_rust_try_fn<'a, 'gcc, 'tcx>(
cx: &'a CodegenCx<'gcc, 'tcx>,
codegen: &mut dyn FnMut(Builder<'a, 'gcc, 'tcx>),
) -> Function<'gcc> {
if let Some(llfn) = cx.rust_try_fn.get() {
return llfn;
}
// Define the type up front for the signature of the rust_try function.
let tcx = cx.tcx;
let i8p = Ty::new_mut_ptr(tcx, tcx.types.i8);
// `unsafe fn(*mut Data) -> ()`
let try_fn_ty = Ty::new_fn_ptr(
tcx,
ty::Binder::dummy(tcx.mk_fn_sig_rust_abi(
iter::once(i8p),
tcx.types.unit,
rustc_hir::Safety::Unsafe,
)),
);
// `unsafe fn(*mut Data, *mut i8) -> ()`
let catch_fn_ty = Ty::new_fn_ptr(
tcx,
ty::Binder::dummy(tcx.mk_fn_sig_rust_abi(
[i8p, i8p].iter().cloned(),
tcx.types.unit,
rustc_hir::Safety::Unsafe,
)),
);
// `unsafe fn(unsafe fn(*mut Data) -> (), *mut Data, unsafe fn(*mut Data, *mut i8) -> ()) -> bool`
let rust_fn_sig = ty::Binder::dummy(cx.tcx.mk_fn_sig_rust_abi(
[try_fn_ty, i8p, catch_fn_ty],
tcx.types.bool,
rustc_hir::Safety::Unsafe,
));
let rust_try = gen_fn(cx, "__rust_try", rust_fn_sig, codegen);
cx.rust_try_fn.set(Some(rust_try));
rust_try
}
// Helper function to give a Block to a closure to codegen a shim function.
// This is currently primarily used for the `try` intrinsic functions above.
#[cfg(feature = "master")]
fn gen_fn<'a, 'gcc, 'tcx>(
cx: &'a CodegenCx<'gcc, 'tcx>,
name: &str,
rust_fn_sig: ty::PolyFnSig<'tcx>,
codegen: &mut dyn FnMut(Builder<'a, 'gcc, 'tcx>),
) -> Function<'gcc> {
let fn_abi = cx.fn_abi_of_fn_ptr(rust_fn_sig, ty::List::empty());
// FIXME(eddyb) find a nicer way to do this.
cx.linkage.set(FunctionType::Internal);
let func = cx.declare_fn(name, fn_abi);
cx.set_frame_pointer_type(func);
cx.apply_target_cpu_attr(func);
let block = Builder::append_block(cx, func, "entry-block");
let bx = Builder::build(cx, block);
codegen(bx);
func
}