gpt4 book ai didi

rust - 名为 "transmute"的 Rust 内在函数实际上在哪里实现?

转载 作者:行者123 更新时间:2023-11-29 08:00:06 29 4
gpt4 key购买 nike

我正在尝试寻找 Rust 内在函数的实现,尤其是接受一个参数的“transumte”内在函数。

我在 cast.rs 中看到了以下代码,但如您所见,它只是委托(delegate)给一些其他的 transmute 实现。

#[inline]
pub unsafe fn transmute<L, G>(thing: L) -> G {
intrinsics::transmute(thing)
}

内在函数的实际实现在哪里,尤其是 transmute 内在函数?

最佳答案

cast::transmute 委托(delegate)给 intrinsics::transmute。有一个名为 intrinsics 的模块(现在在 libcore 中),它包含 extern 绑定(bind)到几个函数,包括 transmute。正如您从模块文档中看到的那样,据说内部函数的实现位于 librustc/middle/trans/foreign.rs 中。

但是,据我所知,内在函数的实际实现存在于 librustc/middle/trans/intrinsic.rs 中。您可以搜索 transmute,您会在一个非常大的 match 语句中找到一个 ARM ,如下所示:

    "transmute" => {
let (in_type, out_type) = (*substs.substs.tps.get(0),
*substs.substs.tps.get(1));
let llintype = type_of::type_of(ccx, in_type);
let llouttype = type_of::type_of(ccx, out_type);

let in_type_size = machine::llbitsize_of_real(ccx, llintype);
let out_type_size = machine::llbitsize_of_real(ccx, llouttype);
if in_type_size != out_type_size {
let sp = match ccx.tcx.map.get(ref_id.unwrap()) {
ast_map::NodeExpr(e) => e.span,
_ => fail!("transmute has non-expr arg"),
};
ccx.sess().span_fatal(sp,
format!("transmute called on types with different sizes: \
{intype} ({insize, plural, =1{# bit} other{# bits}}) to \
{outtype} ({outsize, plural, =1{# bit} other{# bits}})",
intype = ty_to_str(ccx.tcx(), in_type),
insize = in_type_size as uint,
outtype = ty_to_str(ccx.tcx(), out_type),
outsize = out_type_size as uint));
}

if !return_type_is_void(ccx, out_type) {
let llsrcval = get_param(decl, first_real_arg);
if type_is_immediate(ccx, in_type) {
match fcx.llretptr.get() {
Some(llretptr) => {
Store(bcx, llsrcval, PointerCast(bcx, llretptr, llintype.ptr_to()));
RetVoid(bcx);
}
None => match (llintype.kind(), llouttype.kind()) {
(Pointer, other) | (other, Pointer) if other != Pointer => {
let tmp = Alloca(bcx, llouttype, "");
Store(bcx, llsrcval, PointerCast(bcx, tmp, llintype.ptr_to()));
Ret(bcx, Load(bcx, tmp));
}
(Array, _) | (_, Array) | (Struct, _) | (_, Struct) => {
let tmp = Alloca(bcx, llouttype, "");
Store(bcx, llsrcval, PointerCast(bcx, tmp, llintype.ptr_to()));
Ret(bcx, Load(bcx, tmp));
}
_ => {
let llbitcast = BitCast(bcx, llsrcval, llouttype);
Ret(bcx, llbitcast)
}
}
}
} else if type_is_immediate(ccx, out_type) {
let llsrcptr = PointerCast(bcx, llsrcval, llouttype.ptr_to());
let ll_load = Load(bcx, llsrcptr);
Ret(bcx, ll_load);
} else {
// NB: Do not use a Load and Store here. This causes massive
// code bloat when `transmute` is used on large structural
// types.
let lldestptr = fcx.llretptr.get().unwrap();
let lldestptr = PointerCast(bcx, lldestptr, Type::i8p(ccx));
let llsrcptr = PointerCast(bcx, llsrcval, Type::i8p(ccx));

let llsize = llsize_of(ccx, llintype);
call_memcpy(bcx, lldestptr, llsrcptr, llsize, 1);
RetVoid(bcx);
};
} else {
RetVoid(bcx);
}
}

这似乎是生成代码的代码,该代码将被插入而不是调用 transmute。我不是编译器专家,所以如果我错了,请有人纠正我。

关于rust - 名为 "transmute"的 Rust 内在函数实际上在哪里实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582931/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com