diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-intrinsic.cc | 115 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty-coercion.h | 24 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/issue-1130.rs | 47 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/torture/transmute1.rs | 11 |
4 files changed, 195 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 5fde694..2436aaa 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -174,11 +174,14 @@ static tree offset_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype); static tree sizeof_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype); +static tree +transmute_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype); static const std::map<std::string, std::function<tree (Context *, TyTy::BaseType *)>> generic_intrinsics = {{"offset", &offset_intrinsic_handler}, - {"size_of", &sizeof_intrinsic_handler}}; + {"size_of", &sizeof_intrinsic_handler}, + {"transmute", &transmute_intrinsic_handler}}; Intrinsics::Intrinsics (Context *ctx) : ctx (ctx) {} @@ -340,7 +343,7 @@ sizeof_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype_tyty) fntype->override_context (); } - // offset intrinsic has two params dst pointer and offset isize + // size_of has _zero_ parameters its parameter is the generic one if (fntype->get_params ().size () != 0) { rust_error_at (fntype->get_ident ().locus, @@ -396,5 +399,113 @@ sizeof_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype_tyty) return fndecl; } +static tree +transmute_intrinsic_handler (Context *ctx, TyTy::BaseType *fntype_tyty) +{ + rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF); + TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty); + const Resolver::CanonicalPath &canonical_path = fntype->get_ident ().path; + + // items can be forward compiled which means we may not need to invoke this + // code. We might also have already compiled this generic function as well. + tree lookup = NULL_TREE; + if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup, + fntype->get_id (), fntype)) + { + // has this been added to the list then it must be finished + if (ctx->function_completed (lookup)) + { + tree dummy = NULL_TREE; + if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy)) + { + ctx->insert_function_decl (fntype, lookup); + } + return lookup; + } + } + + if (fntype->has_subsititions_defined ()) + { + // override the Hir Lookups for the substituions in this context + fntype->override_context (); + } + + // transmute intrinsic has one parameter + if (fntype->get_params ().size () != 1) + { + rust_error_at (fntype->get_ident ().locus, + "invalid number of parameters for transmute intrinsic"); + return error_mark_node; + } + + // build the intrinsic function + tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); + std::string ir_symbol_name + = canonical_path.get () + fntype->subst_as_string (); + std::string asm_name = ctx->mangle_item (fntype, canonical_path); + + unsigned int flags = 0; + tree fndecl + = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name, asm_name, + flags, fntype->get_ident ().locus); + TREE_PUBLIC (fndecl) = 0; + TREE_READONLY (fndecl) = 1; + DECL_ARTIFICIAL (fndecl) = 1; + DECL_EXTERNAL (fndecl) = 0; + DECL_DECLARED_INLINE_P (fndecl) = 1; + + // setup the params + std::vector<Bvariable *> param_vars; + for (auto &parm : fntype->get_params ()) + { + auto &referenced_param = parm.first; + auto ¶m_tyty = parm.second; + auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); + + Location param_locus = referenced_param->get_locus (); + Bvariable *compiled_param_var + = CompileFnParam::compile (ctx, fndecl, referenced_param, + compiled_param_type, param_locus); + + param_vars.push_back (compiled_param_var); + } + + rust_assert (param_vars.size () == 1); + if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars)) + return error_mark_node; + + // param to convert + Bvariable *convert_me_param = param_vars.at (0); + tree convert_me_expr + = ctx->get_backend ()->var_expression (convert_me_param, Location ()); + + tree enclosing_scope = NULL_TREE; + Location start_location = Location (); + Location end_location = Location (); + + tree code_block = ctx->get_backend ()->block (fndecl, enclosing_scope, {}, + start_location, end_location); + ctx->push_block (code_block); + + // BUILTIN transmute FN BODY BEGIN + tree result_type_tree = TREE_TYPE (DECL_RESULT (fndecl)); + tree result_expr + = ctx->get_backend ()->convert_expression (result_type_tree, + convert_me_expr, Location ()); + auto return_statement + = ctx->get_backend ()->return_statement (fndecl, {result_expr}, + Location ()); + ctx->add_statement (return_statement); + // BUILTIN transmute FN BODY END + + tree bind_tree = ctx->pop_block (); + + gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); + DECL_SAVED_TREE (fndecl) = bind_tree; + ctx->push_function (fndecl); + + return fndecl; +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/typecheck/rust-tyty-coercion.h b/gcc/rust/typecheck/rust-tyty-coercion.h index e13c7f8..c862de5 100644 --- a/gcc/rust/typecheck/rust-tyty-coercion.h +++ b/gcc/rust/typecheck/rust-tyty-coercion.h @@ -856,6 +856,18 @@ public: TyVar (base_resolved->get_ref ())); } + void visit (InferType &type) override + { + if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL) + { + BaseCoercionRules::visit (type); + return; + } + + resolved = base->clone (); + resolved->set_ref (type.get_ref ()); + } + private: BaseType *get_base () override { return base; } @@ -902,6 +914,18 @@ public: TyVar (base_resolved->get_ref ())); } + void visit (InferType &type) override + { + if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL) + { + BaseCoercionRules::visit (type); + return; + } + + resolved = base->clone (); + resolved->set_ref (type.get_ref ()); + } + private: BaseType *get_base () override { return base; } diff --git a/gcc/testsuite/rust/compile/issue-1130.rs b/gcc/testsuite/rust/compile/issue-1130.rs new file mode 100644 index 0000000..92200c7 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-1130.rs @@ -0,0 +1,47 @@ +// { dg-additional-options "-w" } +mod mem { + extern "rust-intrinsic" { + fn size_of<T>() -> usize; + fn transmute<U, V>(_: U) -> V; + } +} + +impl u16 { + fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { + unsafe { mem::transmute(self) } + } +} + +pub trait Hasher { + fn finish(&self) -> u64; + + fn write(&mut self, bytes: &[u8]); + + fn write_u8(&mut self, i: u8) { + self.write(&[i]) + } + + fn write_i8(&mut self, i: i8) { + self.write_u8(i as u8) + } + + fn write_u16(&mut self, i: u16) { + self.write(&i.to_ne_bytes()) + } + + fn write_i16(&mut self, i: i16) { + self.write_u16(i as u16) + } +} + +pub struct SipHasher; + +impl Hasher for SipHasher { + #[inline] + fn write(&mut self, msg: &[u8]) {} + + #[inline] + fn finish(&self) -> u64 { + 0 + } +} diff --git a/gcc/testsuite/rust/compile/torture/transmute1.rs b/gcc/testsuite/rust/compile/torture/transmute1.rs new file mode 100644 index 0000000..333fffe --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/transmute1.rs @@ -0,0 +1,11 @@ +mod mem { + extern "rust-intrinsic" { + fn size_of<T>() -> usize; + fn transmute<U, V>(_: U) -> V; + } +} + +fn main() { + let a = 123; + let _b: [u8; mem::size_of::<i32>()] = mem::transmute(a); +} |