Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_COMPILE_EXTERN_ITEM
20 : #define RUST_COMPILE_EXTERN_ITEM
21 :
22 : #include "rust-compile-base.h"
23 : #include "rust-compile-intrinsic.h"
24 : #include "rust-compile-type.h"
25 : #include "rust-diagnostics.h"
26 : #include "rust-hir-full-decls.h"
27 : #include "rust-attributes.h"
28 : #include "rust-attribute-values.h"
29 : #include "rust-builtins.h"
30 : #include "rust-compile-fnparam.h"
31 : #include "fold-const.h"
32 :
33 : namespace Rust {
34 : namespace Compile {
35 :
36 3260 : class CompileExternItem : public HIRCompileBase,
37 : public HIR::HIRExternalItemVisitor
38 : {
39 : public:
40 3260 : static tree compile (HIR::ExternalItem *item, Context *ctx,
41 : TyTy::BaseType *concrete = nullptr,
42 : location_t ref_locus = UNDEF_LOCATION)
43 : {
44 3260 : CompileExternItem compiler (ctx, concrete, ref_locus);
45 3260 : item->accept_vis (compiler);
46 3260 : return compiler.reference;
47 3260 : }
48 :
49 0 : void visit (HIR::ExternalStaticItem &item) override
50 : {
51 : // check if its already been compiled
52 0 : Bvariable *lookup = Bvariable::error_variable ();
53 0 : if (ctx->lookup_var_decl (item.get_mappings ().get_hirid (), &lookup))
54 : {
55 0 : reference = Backend::var_expression (lookup, ref_locus);
56 0 : return;
57 : }
58 :
59 0 : TyTy::BaseType *resolved_type = nullptr;
60 0 : bool ok = ctx->get_tyctx ()->lookup_type (item.get_mappings ().get_hirid (),
61 : &resolved_type);
62 0 : rust_assert (ok);
63 :
64 0 : std::string name = item.get_item_name ().as_string ();
65 0 : GGC::Ident asm_name = get_link_name (item);
66 :
67 0 : tree type = TyTyResolveCompile::compile (ctx, resolved_type);
68 0 : bool is_external = true;
69 0 : bool is_hidden = false;
70 0 : bool in_unique_section = false;
71 :
72 0 : Bvariable *static_global
73 0 : = Backend::global_variable (name, asm_name, type, is_external, is_hidden,
74 : in_unique_section, item.get_locus ());
75 0 : ctx->insert_var_decl (item.get_mappings ().get_hirid (), static_global);
76 0 : ctx->push_var (static_global);
77 :
78 0 : reference = Backend::var_expression (static_global, ref_locus);
79 0 : }
80 :
81 3258 : void visit (HIR::ExternalFunctionItem &function) override
82 : {
83 3258 : TyTy::BaseType *fntype_tyty;
84 3258 : if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
85 : &fntype_tyty))
86 : {
87 0 : rust_fatal_error (function.get_locus (),
88 : "failed to lookup function type");
89 2080 : return;
90 : }
91 :
92 3258 : rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
93 3258 : TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
94 3258 : if (fntype->has_substitutions_defined ())
95 : {
96 : // we cant do anything for this only when it is used and a concrete type
97 : // is given
98 1563 : if (concrete == nullptr)
99 : return;
100 : else
101 : {
102 0 : rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
103 0 : fntype = static_cast<TyTy::FnType *> (concrete);
104 : }
105 : }
106 :
107 : // items can be forward compiled which means we may not need to invoke this
108 : // code. We might also have already compiled this generic function as well.
109 1695 : tree lookup = NULL_TREE;
110 1695 : if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup,
111 : fntype->get_id (), fntype))
112 : {
113 45 : reference = address_expression (lookup, ref_locus);
114 45 : return;
115 : }
116 :
117 1650 : if (fntype->has_substitutions_defined ())
118 : // override the HIR lookups for the substitutions in this context
119 0 : fntype->override_context ();
120 :
121 1650 : if (fntype->get_abi () == ABI::INTRINSIC)
122 : {
123 467 : Intrinsics compile (ctx);
124 467 : tree fndecl = compile.compile (fntype, ref_locus);
125 467 : ctx->insert_function_decl (fntype, fndecl);
126 467 : return;
127 : }
128 :
129 1183 : tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
130 2366 : std::string ir_symbol_name = function.get_item_name ().as_string ();
131 1183 : GGC::Ident asm_name = get_link_name (function);
132 :
133 1183 : if (fntype->get_abi () == ABI::UNADJUSTED)
134 : {
135 10 : auto res = BuiltinsContext::get ().map_llvm_to_gcc_builtin (
136 5 : asm_name.as_string ());
137 :
138 5 : if (!res)
139 : {
140 0 : res.error ().emit (asm_name, function.get_locus ());
141 0 : reference = error_mark_node;
142 0 : return;
143 : }
144 :
145 5 : tree resolved = res->first;
146 5 : LlvmBuiltinAdapter adapter = res->second;
147 :
148 5 : tree adapter_tree = error_mark_node;
149 :
150 5 : switch (adapter)
151 : {
152 2 : case LlvmBuiltinAdapter::OUTPUT_POINTER_VALUE_STATUS:
153 2 : adapter_tree = compile_x86_output_pointer_adapter (
154 : ctx, fntype, resolved, OutputTupleOrder::VALUE_STATUS,
155 : function.get_locus ());
156 2 : break;
157 2 : case LlvmBuiltinAdapter::OUTPUT_POINTER_STATUS_VALUE:
158 2 : adapter_tree = compile_x86_output_pointer_adapter (
159 : ctx, fntype, resolved, OutputTupleOrder::STATUS_VALUE,
160 : function.get_locus ());
161 2 : break;
162 1 : case LlvmBuiltinAdapter::FORWARD_ARGUMENTS:
163 1 : adapter_tree
164 1 : = compile_x86_forwarding_adapter (ctx, fntype, resolved,
165 : function.get_locus ());
166 1 : break;
167 : }
168 :
169 5 : if (adapter_tree == error_mark_node)
170 : {
171 0 : rust_error_at (function.get_locus (),
172 : "Invalid signature for LLVM intrinsic %qs",
173 : asm_name.c_str ());
174 0 : reference = error_mark_node;
175 0 : return;
176 : }
177 :
178 5 : ctx->insert_function_decl (fntype, adapter_tree);
179 5 : reference = address_expression (adapter_tree, ref_locus);
180 5 : return;
181 : }
182 :
183 1178 : const unsigned int flags = Backend::function_is_declaration;
184 1178 : tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name,
185 : flags, function.get_locus ());
186 1178 : TREE_PUBLIC (fndecl) = 1;
187 1178 : setup_abi_options (fndecl, fntype->get_abi ());
188 :
189 1178 : ctx->insert_function_decl (fntype, fndecl);
190 :
191 1178 : reference = address_expression (fndecl, ref_locus);
192 1183 : }
193 :
194 2 : void visit (HIR::ExternalTypeItem &type) override
195 : {
196 2 : TyTy::BaseType *lookup = nullptr;
197 2 : if (!ctx->get_tyctx ()->lookup_type (type.get_mappings ().get_hirid (),
198 : &lookup))
199 : {
200 0 : rust_error_at (type.get_locus (), "failed to resolve type");
201 0 : return;
202 : }
203 :
204 2 : TyTyResolveCompile::compile (ctx, lookup);
205 : }
206 :
207 : private:
208 : enum class OutputTupleOrder
209 : {
210 : VALUE_STATUS,
211 : STATUS_VALUE,
212 : };
213 :
214 3260 : CompileExternItem (Context *ctx, TyTy::BaseType *concrete,
215 : location_t ref_locus)
216 3260 : : HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node),
217 3260 : ref_locus (ref_locus)
218 : {}
219 :
220 1183 : template <typename T> static GGC::Ident get_link_name (T &obj)
221 : {
222 1183 : AST::Attribute *use_attr = nullptr;
223 :
224 1195 : for (auto &attr : obj.get_outer_attrs ())
225 : {
226 12 : if (attr.get_path ().as_string () == Values::Attributes::LINK_NAME)
227 : {
228 : // later attributes override earlier ones
229 : // TODO: add warning -- should duplicate
230 : // attributes be folded elsewhere?
231 12 : use_attr = &attr;
232 : }
233 : }
234 :
235 1183 : if (use_attr)
236 : {
237 12 : auto link_name
238 : = Analysis::Attributes::extract_string_literal (*use_attr);
239 :
240 12 : if (!link_name.has_value ())
241 0 : rust_error_at (use_attr->get_locus (),
242 : "malformed %<link_name%> attribute input");
243 : else
244 12 : return *link_name;
245 12 : }
246 :
247 1171 : return obj.get_item_name ();
248 : }
249 :
250 : /**
251 : * Compiles a wrapper that wraps around GCC built-ins for compatibility
252 : * with LLVM built-ins function signatures returning a tuple with value +
253 : * status.
254 : *
255 : * @param ctx
256 : * @param fntype the LLVM built-in function type
257 : * @param gcc_builtin the GCC built-in function
258 : * @param order whether the LLVM built-in returns (value, status) or (status,
259 : * value)
260 : * @param locus
261 : * @return tree the resultant wrapper function
262 : */
263 4 : static tree compile_x86_output_pointer_adapter (Context *ctx,
264 : TyTy::FnType *fntype,
265 : tree gcc_builtin,
266 : OutputTupleOrder order,
267 : location_t locus)
268 : {
269 : // expect to be a 2-field tuple return type
270 4 : TyTy::TupleType *tuple
271 4 : = fntype->get_return_type ()->try_as<TyTy::TupleType> ();
272 :
273 4 : if (tuple == nullptr || tuple->num_fields () != 2)
274 : // TODO probably add error here
275 0 : return error_mark_node;
276 :
277 4 : tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
278 :
279 4 : const auto &path = fntype->get_ident ().path;
280 4 : std::string ir_name = path.get () + fntype->subst_as_string ();
281 4 : std::string asm_name = ctx->mangle_item (fntype, path);
282 :
283 : // start building the wrapper function
284 4 : tree fndecl
285 4 : = Backend::function (compiled_fn_type, ir_name, asm_name, 0, locus);
286 :
287 4 : TREE_PUBLIC (fndecl) = 0;
288 4 : DECL_ARTIFICIAL (fndecl) = 1;
289 4 : DECL_EXTERNAL (fndecl) = 0;
290 4 : DECL_DECLARED_INLINE_P (fndecl) = 1;
291 :
292 : // compile params for the rust wrapper
293 4 : std::vector<Bvariable *> param_vars;
294 4 : param_vars.reserve (fntype->get_params ().size ());
295 10 : for (auto ¶m : fntype->get_params ())
296 : {
297 6 : auto &pattern = param.get_pattern ();
298 6 : tree type = TyTyResolveCompile::compile (ctx, param.get_type ());
299 6 : Bvariable *variable
300 6 : = CompileFnParam::compile (ctx, fndecl, pattern, type,
301 6 : pattern.get_locus ());
302 6 : param_vars.emplace_back (variable);
303 : }
304 :
305 4 : if (!Backend::function_set_parameters (fndecl, param_vars))
306 0 : return error_mark_node;
307 :
308 : // forward the rust params, convert each into the corresponding gcc
309 : // built-in param type
310 4 : std::vector<tree> call_arguments;
311 : // +1 here because gcc built-in expects output pointer as param
312 4 : call_arguments.reserve (param_vars.size () + 1);
313 4 : tree gcc_argument_types = TYPE_ARG_TYPES (TREE_TYPE (gcc_builtin));
314 10 : for (Bvariable *param : param_vars)
315 : {
316 6 : tree argument = param->get_tree (locus);
317 6 : tree expected_type = TREE_VALUE (gcc_argument_types);
318 6 : argument = Backend::convert_expression (expected_type, argument, locus);
319 6 : call_arguments.emplace_back (argument);
320 6 : gcc_argument_types = TREE_CHAIN (gcc_argument_types);
321 : }
322 :
323 : // the order of llvm built-ins' tuple return types' fields is not
324 : // consistent, some are (value, status) and others are (status, value), so
325 : // we need to reorder it...
326 4 : tree tuple_type = TREE_TYPE (DECL_RESULT (fndecl));
327 4 : tree first_field = TYPE_FIELDS (tuple_type);
328 4 : tree second_field = DECL_CHAIN (first_field);
329 :
330 4 : tree value_field
331 4 : = order == OutputTupleOrder::VALUE_STATUS ? first_field : second_field;
332 2 : tree status_field
333 : = order == OutputTupleOrder::VALUE_STATUS ? second_field : first_field;
334 :
335 4 : tree value_type = TREE_TYPE (value_field);
336 4 : tree status_type = TREE_TYPE (status_field);
337 :
338 4 : tree temporary_stmt = NULL_TREE;
339 4 : Bvariable *value
340 4 : = Backend::temporary_variable (fndecl, NULL_TREE, value_type, NULL_TREE,
341 : true, locus, &temporary_stmt);
342 4 : Bvariable *status
343 4 : = Backend::temporary_variable (fndecl, NULL_TREE, status_type, NULL_TREE,
344 : false, locus, &temporary_stmt);
345 :
346 : // compile the final gcc built-in param which is the output value pointer,
347 : // which llvm returns as part of the tuple return type
348 4 : tree gcc_pointer_type = TREE_VALUE (gcc_argument_types);
349 4 : tree block
350 4 : = Backend::block (fndecl, NULL_TREE, {value, status}, locus, locus);
351 4 : ctx->push_block (block);
352 :
353 4 : tree value_decl = value->get_tree (locus);
354 4 : tree status_decl = status->get_tree (locus);
355 :
356 4 : tree value_address = build_fold_addr_expr_loc (locus, value_decl);
357 4 : value_address
358 4 : = Backend::convert_expression (gcc_pointer_type, value_address, locus);
359 4 : call_arguments.emplace_back (value_address);
360 :
361 : // call the gcc built-in with the params that were built and assign the
362 : // returned val to the status temp var
363 4 : tree builtin_call
364 4 : = build_call_expr_loc_array (locus, gcc_builtin,
365 4 : static_cast<int> (call_arguments.size ()),
366 : call_arguments.data ());
367 4 : builtin_call
368 4 : = Backend::convert_expression (status_type, builtin_call, locus);
369 4 : ctx->add_statement (
370 : Backend::assignment_statement (status_decl, builtin_call, locus));
371 :
372 : // finally compile and add the return statement
373 4 : std::vector<tree> tuple_values;
374 4 : if (order == OutputTupleOrder::VALUE_STATUS)
375 2 : tuple_values = {value_decl, status_decl};
376 : else
377 2 : tuple_values = {status_decl, value_decl};
378 :
379 4 : tree tuple_result
380 4 : = Backend::constructor_expression (tuple_type, false, tuple_values, -1,
381 : locus);
382 4 : ctx->add_statement (
383 : Backend::return_statement (fndecl, tuple_result, locus));
384 :
385 4 : tree body = ctx->pop_block ();
386 4 : DECL_SAVED_TREE (fndecl) = body;
387 :
388 4 : ctx->push_function (fndecl);
389 4 : return fndecl;
390 8 : }
391 :
392 : /**
393 : * Compiles a 1-to-1 wrapper for LLVM built-ins that wraps around GCC
394 : * built-ins.
395 : *
396 : * @param ctx
397 : * @param fntype the LLVM built-in function type
398 : * @param gcc_builtin the GCC built-in function
399 : * @param locus
400 : * @return tree the resultant wrapper function
401 : */
402 1 : static tree compile_x86_forwarding_adapter (Context *ctx,
403 : TyTy::FnType *fntype,
404 : tree gcc_builtin,
405 : location_t locus)
406 : {
407 1 : tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
408 :
409 1 : const auto &path = fntype->get_ident ().path;
410 1 : std::string ir_name = path.get () + fntype->subst_as_string ();
411 1 : std::string asm_name = ctx->mangle_item (fntype, path);
412 :
413 : // start building the wrapper function
414 1 : tree fndecl
415 1 : = Backend::function (compiled_fn_type, ir_name, asm_name, 0, locus);
416 :
417 1 : TREE_PUBLIC (fndecl) = 0;
418 1 : DECL_ARTIFICIAL (fndecl) = 1;
419 1 : DECL_EXTERNAL (fndecl) = 0;
420 1 : DECL_DECLARED_INLINE_P (fndecl) = 1;
421 :
422 : // compile params for the rust wrapper
423 1 : std::vector<Bvariable *> param_vars;
424 1 : param_vars.reserve (fntype->get_params ().size ());
425 5 : for (auto ¶m : fntype->get_params ())
426 : {
427 4 : auto &pattern = param.get_pattern ();
428 4 : tree type = TyTyResolveCompile::compile (ctx, param.get_type ());
429 4 : Bvariable *variable
430 4 : = CompileFnParam::compile (ctx, fndecl, pattern, type,
431 4 : pattern.get_locus ());
432 4 : param_vars.emplace_back (variable);
433 : }
434 :
435 1 : if (!Backend::function_set_parameters (fndecl, param_vars))
436 0 : return error_mark_node;
437 :
438 : // forward the rust params, convert each into the corresponding gcc
439 : // built-in param type
440 1 : std::vector<tree> call_arguments;
441 1 : call_arguments.reserve (param_vars.size ());
442 1 : tree gcc_argument_types = TYPE_ARG_TYPES (TREE_TYPE (gcc_builtin));
443 5 : for (Bvariable *param : param_vars)
444 : {
445 4 : tree argument = param->get_tree (locus);
446 4 : tree expected_type = TREE_VALUE (gcc_argument_types);
447 4 : argument = Backend::convert_expression (expected_type, argument, locus);
448 4 : call_arguments.emplace_back (argument);
449 4 : gcc_argument_types = TREE_CHAIN (gcc_argument_types);
450 : }
451 :
452 1 : tree builtin_call
453 1 : = build_call_expr_loc_array (locus, gcc_builtin,
454 1 : static_cast<int> (call_arguments.size ()),
455 : call_arguments.data ());
456 1 : tree wrapper_ret_type = TREE_TYPE (DECL_RESULT (fndecl));
457 :
458 1 : builtin_call
459 1 : = Backend::convert_expression (wrapper_ret_type, builtin_call, locus);
460 1 : tree block = Backend::block (fndecl, NULL_TREE, {}, locus, locus);
461 1 : ctx->push_block (block);
462 1 : ctx->add_statement (
463 : Backend::return_statement (fndecl, builtin_call, locus));
464 1 : tree body = ctx->pop_block ();
465 1 : DECL_SAVED_TREE (fndecl) = body;
466 :
467 1 : ctx->push_function (fndecl);
468 1 : return fndecl;
469 2 : }
470 :
471 : TyTy::BaseType *concrete;
472 : tree reference;
473 : location_t ref_locus;
474 : };
475 :
476 : } // namespace Compile
477 : } // namespace Rust
478 :
479 : #endif // RUST_COMPILE_EXTERN_ITEM
|