aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-04-28 15:48:42 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-04-28 16:01:56 +0100
commite09a57632038dc965977e45beed591bf7056d20b (patch)
treecaeae0bf7d1e6c9e8139c31a09875ed40d2603c7 /gcc/rust/backend
parentca7598514f5841cb08aef22a801d8a9f3f321b04 (diff)
downloadgcc-e09a57632038dc965977e45beed591bf7056d20b.zip
gcc-e09a57632038dc965977e45beed591bf7056d20b.tar.gz
gcc-e09a57632038dc965977e45beed591bf7056d20b.tar.bz2
Add support for transmute intrinsic
This adds in the transmute intrisic by utilizing the convert_expression framework we have in the backend class. Fixes #1130 Addresses #658
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-intrinsic.cc115
1 files changed, 113 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 &param_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