1 // gogo-tree.cc -- convert Go frontend Gogo IR to gcc trees.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
19 #include "tree-iterator.h"
21 #include "langhooks.h"
24 #include "diagnostic.h"
27 #ifndef ENABLE_BUILD_WITH_CXX
33 #include "expressions.h"
34 #include "statements.h"
37 // Whether we have seen any errors.
42 return errorcount
!= 0 || sorrycount
!= 0;
48 get_identifier_from_string(const std::string
& str
)
50 return get_identifier_with_length(str
.data(), str
.length());
55 static std::map
<std::string
, tree
> builtin_functions
;
57 // Define a builtin function. BCODE is the builtin function code
58 // defined by builtins.def. NAME is the name of the builtin function.
59 // LIBNAME is the name of the corresponding library function, and is
60 // NULL if there isn't one. FNTYPE is the type of the function.
61 // CONST_P is true if the function has the const attribute.
64 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
65 tree fntype
, bool const_p
)
67 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
70 TREE_READONLY(decl
) = 1;
71 built_in_decls
[bcode
] = decl
;
72 implicit_built_in_decls
[bcode
] = decl
;
73 builtin_functions
[name
] = decl
;
76 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
79 TREE_READONLY(decl
) = 1;
80 builtin_functions
[libname
] = decl
;
84 // Create trees for implicit builtin functions.
87 Gogo::define_builtin_function_trees()
89 /* We need to define the fetch_and_add functions, since we use them
91 tree t
= go_type_for_size(BITS_PER_UNIT
, 1);
92 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
93 define_builtin(BUILT_IN_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1", NULL
,
94 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
96 t
= go_type_for_size(BITS_PER_UNIT
* 2, 1);
97 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
98 define_builtin (BUILT_IN_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2", NULL
,
99 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
101 t
= go_type_for_size(BITS_PER_UNIT
* 4, 1);
102 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
103 define_builtin(BUILT_IN_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4", NULL
,
104 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
106 t
= go_type_for_size(BITS_PER_UNIT
* 8, 1);
107 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
108 define_builtin(BUILT_IN_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8", NULL
,
109 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
111 // We use __builtin_expect for magic import functions.
112 define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
113 build_function_type_list(long_integer_type_node
,
114 long_integer_type_node
,
115 long_integer_type_node
,
119 // We use __builtin_memmove for the predeclared copy function.
120 define_builtin(BUILT_IN_MEMMOVE
, "__builtin_memmove", "memmove",
121 build_function_type_list(ptr_type_node
,
128 // We provide sqrt for the math library.
129 define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
130 build_function_type_list(double_type_node
,
134 define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
135 build_function_type_list(long_double_type_node
,
136 long_double_type_node
,
140 // We use __builtin_return_address in the thunk we build for
141 // functions which call recover.
142 define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address", NULL
,
143 build_function_type_list(ptr_type_node
,
148 // The compiler uses __builtin_trap for some exception handling
150 define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
151 build_function_type(void_type_node
, void_list_node
),
155 // Get the name to use for the import control function. If there is a
156 // global function or variable, then we know that that name must be
157 // unique in the link, and we use it as the basis for our name.
160 Gogo::get_init_fn_name()
162 if (this->init_fn_name_
.empty())
164 gcc_assert(this->package_
!= NULL
);
165 if (this->package_name() == "main")
167 // Use a name which the runtime knows.
168 this->init_fn_name_
= "__go_init_main";
172 std::string s
= this->unique_prefix();
174 s
.append(this->package_name());
175 s
.append("..import");
176 this->init_fn_name_
= s
;
180 return this->init_fn_name_
;
183 // Add statements to INIT_STMT_LIST which run the initialization
184 // functions for imported packages. This is only used for the "main"
188 Gogo::init_imports(tree
* init_stmt_list
)
190 gcc_assert(this->package_name() == "main");
192 if (this->imported_init_fns_
.empty())
195 tree fntype
= build_function_type(void_type_node
, void_list_node
);
197 // We must call them in increasing priority order.
198 std::vector
<Import_init
> v
;
199 for (std::set
<Import_init
>::const_iterator p
=
200 this->imported_init_fns_
.begin();
201 p
!= this->imported_init_fns_
.end();
204 std::sort(v
.begin(), v
.end());
206 for (std::vector
<Import_init
>::const_iterator p
= v
.begin();
210 std::string user_name
= p
->package_name() + ".init";
211 tree decl
= build_decl(UNKNOWN_LOCATION
, FUNCTION_DECL
,
212 get_identifier_from_string(user_name
),
214 const std::string
& init_name(p
->init_name());
215 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(init_name
));
216 TREE_PUBLIC(decl
) = 1;
217 DECL_EXTERNAL(decl
) = 1;
218 append_to_statement_list(build_call_expr(decl
, 0), init_stmt_list
);
222 // Register global variables with the garbage collector. We need to
223 // register all variables which can hold a pointer value. They become
224 // roots during the mark phase. We build a struct that is easy to
225 // hook into a list of roots.
227 // struct __go_gc_root_list
229 // struct __go_gc_root_list* __next;
230 // struct __go_gc_root
237 // The last entry in the roots array has a NULL decl field.
240 Gogo::register_gc_vars(const std::vector
<Named_object
*>& var_gc
,
241 tree
* init_stmt_list
)
246 size_t count
= var_gc
.size();
248 tree root_type
= Gogo::builtin_struct(NULL
, "__go_gc_root", NULL_TREE
, 2,
254 tree index_type
= build_index_type(size_int(count
));
255 tree array_type
= build_array_type(root_type
, index_type
);
257 tree root_list_type
= make_node(RECORD_TYPE
);
258 root_list_type
= Gogo::builtin_struct(NULL
, "__go_gc_root_list",
261 build_pointer_type(root_list_type
),
265 // Build an initialier for the __roots array.
267 VEC(constructor_elt
,gc
)* roots_init
= VEC_alloc(constructor_elt
, gc
,
271 for (std::vector
<Named_object
*>::const_iterator p
= var_gc
.begin();
275 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
277 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
278 tree field
= TYPE_FIELDS(root_type
);
280 tree decl
= (*p
)->get_tree(this, NULL
);
281 gcc_assert(TREE_CODE(decl
) == VAR_DECL
);
282 elt
->value
= build_fold_addr_expr(decl
);
284 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
285 field
= DECL_CHAIN(field
);
287 elt
->value
= DECL_SIZE_UNIT(decl
);
289 elt
= VEC_quick_push(constructor_elt
, roots_init
, NULL
);
290 elt
->index
= size_int(i
);
291 elt
->value
= build_constructor(root_type
, init
);
294 // The list ends with a NULL entry.
296 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
298 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
299 tree field
= TYPE_FIELDS(root_type
);
301 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
303 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
304 field
= DECL_CHAIN(field
);
306 elt
->value
= size_zero_node
;
308 elt
= VEC_quick_push(constructor_elt
, roots_init
, NULL
);
309 elt
->index
= size_int(i
);
310 elt
->value
= build_constructor(root_type
, init
);
312 // Build a constructor for the struct.
314 VEC(constructor_elt
,gc
*) root_list_init
= VEC_alloc(constructor_elt
, gc
, 2);
316 elt
= VEC_quick_push(constructor_elt
, root_list_init
, NULL
);
317 field
= TYPE_FIELDS(root_list_type
);
319 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
321 elt
= VEC_quick_push(constructor_elt
, root_list_init
, NULL
);
322 field
= DECL_CHAIN(field
);
324 elt
->value
= build_constructor(array_type
, roots_init
);
326 // Build a decl to register.
328 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
329 create_tmp_var_name("gc"), root_list_type
);
330 DECL_EXTERNAL(decl
) = 0;
331 TREE_PUBLIC(decl
) = 0;
332 TREE_STATIC(decl
) = 1;
333 DECL_ARTIFICIAL(decl
) = 1;
334 DECL_INITIAL(decl
) = build_constructor(root_list_type
, root_list_init
);
335 rest_of_decl_compilation(decl
, 1, 0);
337 static tree register_gc_fndecl
;
338 tree call
= Gogo::call_builtin(®ister_gc_fndecl
, BUILTINS_LOCATION
,
339 "__go_register_gc_roots",
342 build_pointer_type(root_list_type
),
343 build_fold_addr_expr(decl
));
344 append_to_statement_list(call
, init_stmt_list
);
347 // Build the decl for the initialization function.
350 Gogo::initialization_function_decl()
352 // The tedious details of building your own function. There doesn't
353 // seem to be a helper function for this.
354 std::string name
= this->package_name() + ".init";
355 tree fndecl
= build_decl(BUILTINS_LOCATION
, FUNCTION_DECL
,
356 get_identifier_from_string(name
),
357 build_function_type(void_type_node
,
359 const std::string
& asm_name(this->get_init_fn_name());
360 SET_DECL_ASSEMBLER_NAME(fndecl
, get_identifier_from_string(asm_name
));
362 tree resdecl
= build_decl(BUILTINS_LOCATION
, RESULT_DECL
, NULL_TREE
,
364 DECL_ARTIFICIAL(resdecl
) = 1;
365 DECL_CONTEXT(resdecl
) = fndecl
;
366 DECL_RESULT(fndecl
) = resdecl
;
368 TREE_STATIC(fndecl
) = 1;
369 TREE_USED(fndecl
) = 1;
370 DECL_ARTIFICIAL(fndecl
) = 1;
371 TREE_PUBLIC(fndecl
) = 1;
373 DECL_INITIAL(fndecl
) = make_node(BLOCK
);
374 TREE_USED(DECL_INITIAL(fndecl
)) = 1;
379 // Create the magic initialization function. INIT_STMT_LIST is the
380 // code that it needs to run.
383 Gogo::write_initialization_function(tree fndecl
, tree init_stmt_list
)
385 // Make sure that we thought we needed an initialization function,
386 // as otherwise we will not have reported it in the export data.
387 gcc_assert(this->package_name() == "main" || this->need_init_fn_
);
389 if (fndecl
== NULL_TREE
)
390 fndecl
= this->initialization_function_decl();
392 DECL_SAVED_TREE(fndecl
) = init_stmt_list
;
394 current_function_decl
= fndecl
;
395 if (DECL_STRUCT_FUNCTION(fndecl
) == NULL
)
396 push_struct_function(fndecl
);
398 push_cfun(DECL_STRUCT_FUNCTION(fndecl
));
399 cfun
->function_end_locus
= BUILTINS_LOCATION
;
401 gimplify_function_tree(fndecl
);
403 cgraph_add_new_function(fndecl
, false);
404 cgraph_mark_needed_node(cgraph_node(fndecl
));
406 current_function_decl
= NULL_TREE
;
410 // Search for references to VAR in any statements or called functions.
412 class Find_var
: public Traverse
415 // A hash table we use to avoid looping. The index is the name of a
416 // named object. We only look through objects defined in this
418 typedef Unordered_set(std::string
) Seen_objects
;
420 Find_var(Named_object
* var
, Seen_objects
* seen_objects
)
421 : Traverse(traverse_expressions
),
422 var_(var
), seen_objects_(seen_objects
), found_(false)
425 // Whether the variable was found.
428 { return this->found_
; }
431 expression(Expression
**);
434 // The variable we are looking for.
436 // Names of objects we have already seen.
437 Seen_objects
* seen_objects_
;
438 // True if the variable was found.
442 // See if EXPR refers to VAR, looking through function calls and
443 // variable initializations.
446 Find_var::expression(Expression
** pexpr
)
448 Expression
* e
= *pexpr
;
450 Var_expression
* ve
= e
->var_expression();
453 Named_object
* v
= ve
->named_object();
457 return TRAVERSE_EXIT
;
460 if (v
->is_variable() && v
->package() == NULL
)
462 Expression
* init
= v
->var_value()->init();
465 std::pair
<Seen_objects::iterator
, bool> ins
=
466 this->seen_objects_
->insert(v
->name());
469 // This is the first time we have seen this name.
470 if (Expression::traverse(&init
, this) == TRAVERSE_EXIT
)
471 return TRAVERSE_EXIT
;
477 // We traverse the code of any function we see. Note that this
478 // means that we will traverse the code of a function whose address
479 // is taken even if it is not called.
480 Func_expression
* fe
= e
->func_expression();
483 const Named_object
* f
= fe
->named_object();
484 if (f
->is_function() && f
->package() == NULL
)
486 std::pair
<Seen_objects::iterator
, bool> ins
=
487 this->seen_objects_
->insert(f
->name());
490 // This is the first time we have seen this name.
491 if (f
->func_value()->block()->traverse(this) == TRAVERSE_EXIT
)
492 return TRAVERSE_EXIT
;
497 return TRAVERSE_CONTINUE
;
500 // Return true if EXPR refers to VAR.
503 expression_requires(Expression
* expr
, Block
* preinit
, Named_object
* var
)
505 Find_var::Seen_objects seen_objects
;
506 Find_var
find_var(var
, &seen_objects
);
508 Expression::traverse(&expr
, &find_var
);
510 preinit
->traverse(&find_var
);
512 return find_var
.found();
515 // Sort variable initializations. If the initialization expression
516 // for variable A refers directly or indirectly to the initialization
517 // expression for variable B, then we must initialize B before A.
523 : var_(NULL
), init_(NULL_TREE
), waiting_(0)
526 Var_init(Named_object
* var
, tree init
)
527 : var_(var
), init_(init
), waiting_(0)
530 // Return the variable.
533 { return this->var_
; }
535 // Return the initialization expression.
538 { return this->init_
; }
540 // Return the number of variables waiting for this one to be
544 { return this->waiting_
; }
546 // Increment the number waiting.
549 { ++this->waiting_
; }
552 // The variable being initialized.
554 // The initialization expression to run.
556 // The number of variables which are waiting for this one.
560 typedef std::list
<Var_init
> Var_inits
;
562 // Sort the variable initializations. The rule we follow is that we
563 // emit them in the order they appear in the array, except that if the
564 // initialization expression for a variable V1 depends upon another
565 // variable V2 then we initialize V1 after V2.
568 sort_var_inits(Var_inits
* var_inits
)
571 while (!var_inits
->empty())
573 Var_inits::iterator p1
= var_inits
->begin();
574 Named_object
* var
= p1
->var();
575 Expression
* init
= var
->var_value()->init();
576 Block
* preinit
= var
->var_value()->preinit();
578 // Start walking through the list to see which variables VAR
579 // needs to wait for. We can skip P1->WAITING variables--that
580 // is the number we've already checked.
581 Var_inits::iterator p2
= p1
;
583 for (size_t i
= p1
->waiting(); i
> 0; --i
)
586 for (; p2
!= var_inits
->end(); ++p2
)
588 if (expression_requires(init
, preinit
, p2
->var()))
591 if (expression_requires(p2
->var()->var_value()->init(),
592 p2
->var()->var_value()->preinit(),
595 error_at(var
->location(),
596 ("initialization expressions for %qs and "
597 "%qs depend upon each other"),
598 var
->message_name().c_str(),
599 p2
->var()->message_name().c_str());
600 inform(p2
->var()->location(), "%qs defined here",
601 p2
->var()->message_name().c_str());
602 p2
= var_inits
->end();
606 // We can't emit P1 until P2 is emitted. Move P1.
607 // Note that the WAITING loop always executes at
608 // least once, which is what we want.
609 p2
->increment_waiting();
610 Var_inits::iterator p3
= p2
;
611 for (size_t i
= p2
->waiting(); i
> 0; --i
)
613 var_inits
->splice(p3
, *var_inits
, p1
);
619 if (p2
== var_inits
->end())
621 // VAR does not depends upon any other initialization expressions.
623 // Check for a loop of VAR on itself. We only do this if
624 // INIT is not NULL; when INIT is NULL, it means that
625 // PREINIT sets VAR, which we will interpret as a loop.
626 if (init
!= NULL
&& expression_requires(init
, preinit
, var
))
627 error_at(var
->location(),
628 "initialization expression for %qs depends upon itself",
629 var
->message_name().c_str());
630 ready
.splice(ready
.end(), *var_inits
, p1
);
634 // Now READY is the list in the desired initialization order.
635 var_inits
->swap(ready
);
638 // Write out the global definitions.
641 Gogo::write_globals()
643 Bindings
* bindings
= this->current_bindings();
644 size_t count
= bindings
->size_definitions();
646 tree
* vec
= new tree
[count
];
648 tree init_fndecl
= NULL_TREE
;
649 tree init_stmt_list
= NULL_TREE
;
651 if (this->package_name() == "main")
652 this->init_imports(&init_stmt_list
);
654 // A list of variable initializations.
657 // A list of variables which need to be registered with the garbage
659 std::vector
<Named_object
*> var_gc
;
660 var_gc
.reserve(count
);
662 tree var_init_stmt_list
= NULL_TREE
;
664 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
665 p
!= bindings
->end_definitions();
668 Named_object
* no
= *p
;
670 gcc_assert(!no
->is_type_declaration() && !no
->is_function_declaration());
671 // There is nothing to do for a package.
672 if (no
->is_package())
679 // There is nothing to do for an object which was imported from
680 // a different package into the global scope.
681 if (no
->package() != NULL
)
688 // There is nothing useful we can output for constants which
689 // have ideal or non-integeral type.
692 Type
* type
= no
->const_value()->type();
694 type
= no
->const_value()->expr()->type();
695 if (type
->is_abstract() || type
->integer_type() == NULL
)
703 vec
[i
] = no
->get_tree(this, NULL
);
705 if (vec
[i
] == error_mark_node
)
707 gcc_assert(saw_errors());
713 // If a variable is initialized to a non-constant value, do the
714 // initialization in an initialization function.
715 if (TREE_CODE(vec
[i
]) == VAR_DECL
)
717 gcc_assert(no
->is_variable());
719 // Check for a sink variable, which may be used to run
720 // an initializer purely for its side effects.
721 bool is_sink
= no
->name()[0] == '_' && no
->name()[1] == '.';
723 tree var_init_tree
= NULL_TREE
;
724 if (!no
->var_value()->has_pre_init())
726 tree init
= no
->var_value()->get_init_tree(this, NULL
);
727 if (init
== error_mark_node
)
728 gcc_assert(saw_errors());
729 else if (init
== NULL_TREE
)
731 else if (TREE_CONSTANT(init
))
732 DECL_INITIAL(vec
[i
]) = init
;
734 var_init_tree
= init
;
736 var_init_tree
= fold_build2_loc(no
->location(), MODIFY_EXPR
,
737 void_type_node
, vec
[i
], init
);
741 // We are going to create temporary variables which
742 // means that we need an fndecl.
743 if (init_fndecl
== NULL_TREE
)
744 init_fndecl
= this->initialization_function_decl();
745 current_function_decl
= init_fndecl
;
746 if (DECL_STRUCT_FUNCTION(init_fndecl
) == NULL
)
747 push_struct_function(init_fndecl
);
749 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl
));
751 tree var_decl
= is_sink
? NULL_TREE
: vec
[i
];
752 var_init_tree
= no
->var_value()->get_init_block(this, NULL
,
755 current_function_decl
= NULL_TREE
;
759 if (var_init_tree
!= NULL_TREE
)
761 if (no
->var_value()->init() == NULL
762 && !no
->var_value()->has_pre_init())
763 append_to_statement_list(var_init_tree
, &var_init_stmt_list
);
765 var_inits
.push_back(Var_init(no
, var_init_tree
));
768 if (!is_sink
&& no
->var_value()->type()->has_pointer())
769 var_gc
.push_back(no
);
773 // Register global variables with the garbage collector.
774 this->register_gc_vars(var_gc
, &init_stmt_list
);
776 // Simple variable initializations, after all variables are
778 append_to_statement_list(var_init_stmt_list
, &init_stmt_list
);
780 // Complex variable initializations, first sorting them into a
782 if (!var_inits
.empty())
784 sort_var_inits(&var_inits
);
785 for (Var_inits::const_iterator p
= var_inits
.begin();
786 p
!= var_inits
.end();
788 append_to_statement_list(p
->init(), &init_stmt_list
);
791 // After all the variables are initialized, call the "init"
792 // functions if there are any.
793 for (std::vector
<Named_object
*>::const_iterator p
=
794 this->init_functions_
.begin();
795 p
!= this->init_functions_
.end();
798 tree decl
= (*p
)->get_tree(this, NULL
);
799 tree call
= build_call_expr(decl
, 0);
800 append_to_statement_list(call
, &init_stmt_list
);
803 // Set up a magic function to do all the initialization actions.
804 // This will be called if this package is imported.
805 if (init_stmt_list
!= NULL_TREE
806 || this->need_init_fn_
807 || this->package_name() == "main")
808 this->write_initialization_function(init_fndecl
, init_stmt_list
);
810 // Pass everything back to the middle-end.
812 if (this->imported_unsafe_
)
814 // Importing the "unsafe" package automatically disables TBAA.
815 flag_strict_aliasing
= false;
817 // This is a real hack. init_varasm_once has already grabbed an
818 // alias set, which we don't want when we aren't going strict
819 // aliasing. We reinitialize to make it do it again. FIXME.
823 wrapup_global_declarations(vec
, count
);
825 cgraph_finalize_compilation_unit();
827 check_global_declarations(vec
, count
);
828 emit_debug_global_declarations(vec
, count
);
833 // Get a tree for the identifier for a named object.
836 Named_object::get_id(Gogo
* gogo
)
838 std::string decl_name
;
839 if (this->is_function_declaration()
840 && !this->func_declaration_value()->asm_name().empty())
841 decl_name
= this->func_declaration_value()->asm_name();
842 else if ((this->is_variable() && !this->var_value()->is_global())
844 && this->type_value()->location() == BUILTINS_LOCATION
))
846 // We don't need the package name for local variables or builtin
848 decl_name
= Gogo::unpack_hidden_name(this->name_
);
850 else if (this->is_function()
851 && !this->func_value()->is_method()
852 && this->package_
== NULL
853 && Gogo::unpack_hidden_name(this->name_
) == "init")
855 // A single package can have multiple "init" functions, which
856 // means that we need to give them different names.
857 static int init_index
;
859 snprintf(buf
, sizeof buf
, "%d", init_index
);
861 decl_name
= gogo
->package_name() + ".init." + buf
;
865 std::string package_name
;
866 if (this->package_
== NULL
)
867 package_name
= gogo
->package_name();
869 package_name
= this->package_
->name();
871 decl_name
= package_name
+ '.' + Gogo::unpack_hidden_name(this->name_
);
873 Function_type
* fntype
;
874 if (this->is_function())
875 fntype
= this->func_value()->type();
876 else if (this->is_function_declaration())
877 fntype
= this->func_declaration_value()->type();
880 if (fntype
!= NULL
&& fntype
->is_method())
882 decl_name
.push_back('.');
883 decl_name
.append(fntype
->receiver()->type()->mangled_name(gogo
));
888 const Named_object
* in_function
= this->type_value()->in_function();
889 if (in_function
!= NULL
)
890 decl_name
+= '$' + in_function
->name();
892 return get_identifier_from_string(decl_name
);
895 // Get a tree for a named object.
898 Named_object::get_tree(Gogo
* gogo
, Named_object
* function
)
900 if (this->tree_
!= NULL_TREE
)
902 // If this is a variable whose address is taken, we must rebuild
903 // the INDIRECT_REF each time to avoid invalid sharing.
904 tree ret
= this->tree_
;
905 if (((this->classification_
== NAMED_OBJECT_VAR
906 && this->var_value()->is_in_heap())
907 || (this->classification_
== NAMED_OBJECT_RESULT_VAR
908 && this->result_var_value()->is_in_heap()))
909 && ret
!= error_mark_node
)
911 gcc_assert(TREE_CODE(ret
) == INDIRECT_REF
);
912 ret
= build_fold_indirect_ref(TREE_OPERAND(ret
, 0));
913 TREE_THIS_NOTRAP(ret
) = 1;
919 if (this->classification_
== NAMED_OBJECT_TYPE
)
922 name
= this->get_id(gogo
);
924 switch (this->classification_
)
926 case NAMED_OBJECT_CONST
:
928 Named_constant
* named_constant
= this->u_
.const_value
;
929 Translate_context
subcontext(gogo
, function
, NULL
, NULL_TREE
);
930 tree expr_tree
= named_constant
->expr()->get_tree(&subcontext
);
931 if (expr_tree
== error_mark_node
)
932 decl
= error_mark_node
;
935 Type
* type
= named_constant
->type();
936 if (type
!= NULL
&& !type
->is_abstract())
937 expr_tree
= fold_convert(type
->get_tree(gogo
), expr_tree
);
938 if (expr_tree
== error_mark_node
)
939 decl
= error_mark_node
;
940 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
942 decl
= build_decl(named_constant
->location(), CONST_DECL
,
943 name
, TREE_TYPE(expr_tree
));
944 DECL_INITIAL(decl
) = expr_tree
;
945 TREE_CONSTANT(decl
) = 1;
946 TREE_READONLY(decl
) = 1;
950 // A CONST_DECL is only for an enum constant, so we
951 // shouldn't use for non-integral types. Instead we
952 // just return the constant itself, rather than a
960 case NAMED_OBJECT_TYPE
:
962 Named_type
* named_type
= this->u_
.type_value
;
963 tree type_tree
= named_type
->get_tree(gogo
);
964 if (type_tree
== error_mark_node
)
965 decl
= error_mark_node
;
968 decl
= TYPE_NAME(type_tree
);
969 gcc_assert(decl
!= NULL_TREE
);
971 // We need to produce a type descriptor for every named
972 // type, and for a pointer to every named type, since
973 // other files or packages might refer to them. We need
974 // to do this even for hidden types, because they might
975 // still be returned by some function. Simply calling the
976 // type_descriptor method is enough to create the type
977 // descriptor, even though we don't do anything with it.
978 if (this->package_
== NULL
)
980 named_type
->type_descriptor_pointer(gogo
);
981 Type
* pn
= Type::make_pointer_type(named_type
);
982 pn
->type_descriptor_pointer(gogo
);
988 case NAMED_OBJECT_TYPE_DECLARATION
:
989 error("reference to undefined type %qs",
990 this->message_name().c_str());
991 return error_mark_node
;
993 case NAMED_OBJECT_VAR
:
995 Variable
* var
= this->u_
.var_value
;
996 Type
* type
= var
->type();
997 if (type
->is_error_type()
998 || (type
->is_undefined()
999 && (!var
->is_global() || this->package() == NULL
)))
1001 // Force the error for an undefined type, just in case.
1003 decl
= error_mark_node
;
1007 tree var_type
= type
->get_tree(gogo
);
1008 bool is_parameter
= var
->is_parameter();
1009 if (var
->is_receiver() && type
->points_to() == NULL
)
1010 is_parameter
= false;
1011 if (var
->is_in_heap())
1013 is_parameter
= false;
1014 var_type
= build_pointer_type(var_type
);
1016 decl
= build_decl(var
->location(),
1017 is_parameter
? PARM_DECL
: VAR_DECL
,
1019 if (!var
->is_global())
1021 tree fnid
= function
->get_id(gogo
);
1022 tree fndecl
= function
->func_value()->get_or_make_decl(gogo
,
1025 DECL_CONTEXT(decl
) = fndecl
;
1028 DECL_ARG_TYPE(decl
) = TREE_TYPE(decl
);
1030 if (var
->is_global())
1032 const Package
* package
= this->package();
1033 if (package
== NULL
)
1034 TREE_STATIC(decl
) = 1;
1036 DECL_EXTERNAL(decl
) = 1;
1037 if (!Gogo::is_hidden_name(this->name_
))
1039 TREE_PUBLIC(decl
) = 1;
1040 std::string asm_name
= (package
== NULL
1041 ? gogo
->unique_prefix()
1042 : package
->unique_prefix());
1043 asm_name
.append(1, '.');
1044 asm_name
.append(IDENTIFIER_POINTER(name
),
1045 IDENTIFIER_LENGTH(name
));
1046 tree asm_id
= get_identifier_from_string(asm_name
);
1047 SET_DECL_ASSEMBLER_NAME(decl
, asm_id
);
1051 // FIXME: We should only set this for variables which are
1052 // actually used somewhere.
1053 TREE_USED(decl
) = 1;
1058 case NAMED_OBJECT_RESULT_VAR
:
1060 Result_variable
* result
= this->u_
.result_var_value
;
1061 Type
* type
= result
->type();
1062 if (type
->is_error_type() || type
->is_undefined())
1066 decl
= error_mark_node
;
1070 gcc_assert(result
->function() == function
->func_value());
1071 source_location loc
= function
->location();
1072 tree result_type
= type
->get_tree(gogo
);
1074 if (!result
->is_in_heap())
1075 init
= type
->get_init_tree(gogo
, false);
1078 tree space
= gogo
->allocate_memory(type
,
1079 TYPE_SIZE_UNIT(result_type
),
1081 result_type
= build_pointer_type(result_type
);
1082 tree subinit
= type
->get_init_tree(gogo
, true);
1083 if (subinit
== NULL_TREE
)
1084 init
= fold_convert_loc(loc
, result_type
, space
);
1087 space
= save_expr(space
);
1088 space
= fold_convert_loc(loc
, result_type
, space
);
1089 tree spaceref
= build_fold_indirect_ref_loc(loc
, space
);
1090 TREE_THIS_NOTRAP(spaceref
) = 1;
1091 tree set
= fold_build2_loc(loc
, MODIFY_EXPR
, void_type_node
,
1093 init
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(space
),
1097 decl
= build_decl(loc
, VAR_DECL
, name
, result_type
);
1098 tree fnid
= function
->get_id(gogo
);
1099 tree fndecl
= function
->func_value()->get_or_make_decl(gogo
,
1102 DECL_CONTEXT(decl
) = fndecl
;
1103 DECL_INITIAL(decl
) = init
;
1104 TREE_USED(decl
) = 1;
1109 case NAMED_OBJECT_SINK
:
1112 case NAMED_OBJECT_FUNC
:
1114 Function
* func
= this->u_
.func_value
;
1115 decl
= func
->get_or_make_decl(gogo
, this, name
);
1116 if (decl
!= error_mark_node
)
1118 if (func
->block() != NULL
)
1120 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
1121 push_struct_function(decl
);
1123 push_cfun(DECL_STRUCT_FUNCTION(decl
));
1125 cfun
->function_end_locus
= func
->block()->end_location();
1127 current_function_decl
= decl
;
1129 func
->build_tree(gogo
, this);
1131 gimplify_function_tree(decl
);
1133 cgraph_finalize_function(decl
, true);
1135 current_function_decl
= NULL_TREE
;
1146 if (TREE_TYPE(decl
) == error_mark_node
)
1147 decl
= error_mark_node
;
1151 // If this is a local variable whose address is taken, then we
1152 // actually store it in the heap. For uses of the variable we need
1153 // to return a reference to that heap location.
1154 if (((this->classification_
== NAMED_OBJECT_VAR
1155 && this->var_value()->is_in_heap())
1156 || (this->classification_
== NAMED_OBJECT_RESULT_VAR
1157 && this->result_var_value()->is_in_heap()))
1158 && ret
!= error_mark_node
)
1160 gcc_assert(POINTER_TYPE_P(TREE_TYPE(ret
)));
1161 ret
= build_fold_indirect_ref(ret
);
1162 TREE_THIS_NOTRAP(ret
) = 1;
1167 if (ret
!= error_mark_node
)
1168 go_preserve_from_gc(ret
);
1173 // Get the initial value of a variable as a tree. This does not
1174 // consider whether the variable is in the heap--it returns the
1175 // initial value as though it were always stored in the stack.
1178 Variable::get_init_tree(Gogo
* gogo
, Named_object
* function
)
1180 gcc_assert(this->preinit_
== NULL
);
1181 if (this->init_
== NULL
)
1183 gcc_assert(!this->is_parameter_
);
1184 return this->type_
->get_init_tree(gogo
, this->is_global_
);
1188 Translate_context
context(gogo
, function
, NULL
, NULL_TREE
);
1189 tree rhs_tree
= this->init_
->get_tree(&context
);
1190 return Expression::convert_for_assignment(&context
, this->type(),
1191 this->init_
->type(),
1192 rhs_tree
, this->location());
1196 // Get the initial value of a variable when a block is required.
1197 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1200 Variable::get_init_block(Gogo
* gogo
, Named_object
* function
, tree var_decl
)
1202 gcc_assert(this->preinit_
!= NULL
);
1204 // We want to add the variable assignment to the end of the preinit
1205 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1206 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1207 // regular statements.
1209 Translate_context
context(gogo
, function
, NULL
, NULL_TREE
);
1210 tree block_tree
= this->preinit_
->get_tree(&context
);
1211 gcc_assert(TREE_CODE(block_tree
) == BIND_EXPR
);
1212 tree statements
= BIND_EXPR_BODY(block_tree
);
1213 while (TREE_CODE(statements
) == TRY_FINALLY_EXPR
1214 || TREE_CODE(statements
) == TRY_CATCH_EXPR
)
1215 statements
= TREE_OPERAND(statements
, 0);
1217 // It's possible to have pre-init statements without an initializer
1218 // if the pre-init statements set the variable.
1219 if (this->init_
!= NULL
)
1221 tree rhs_tree
= this->init_
->get_tree(&context
);
1222 if (var_decl
== NULL_TREE
)
1223 append_to_statement_list(rhs_tree
, &statements
);
1226 tree val
= Expression::convert_for_assignment(&context
, this->type(),
1227 this->init_
->type(),
1230 tree set
= fold_build2_loc(this->location(), MODIFY_EXPR
,
1231 void_type_node
, var_decl
, val
);
1232 append_to_statement_list(set
, &statements
);
1239 // Get a tree for a function decl.
1242 Function::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1244 if (this->fndecl_
== NULL_TREE
)
1246 tree functype
= this->type_
->get_tree(gogo
);
1247 if (functype
== error_mark_node
)
1248 this->fndecl_
= error_mark_node
;
1251 // The type of a function comes back as a pointer, but we
1252 // want the real function type for a function declaration.
1253 gcc_assert(POINTER_TYPE_P(functype
));
1254 functype
= TREE_TYPE(functype
);
1255 tree decl
= build_decl(this->location(), FUNCTION_DECL
, id
, functype
);
1257 this->fndecl_
= decl
;
1259 gcc_assert(no
->package() == NULL
);
1260 if (this->enclosing_
!= NULL
|| Gogo::is_thunk(no
))
1262 else if (Gogo::unpack_hidden_name(no
->name()) == "init"
1263 && !this->type_
->is_method())
1265 else if (Gogo::unpack_hidden_name(no
->name()) == "main"
1266 && gogo
->package_name() == "main")
1267 TREE_PUBLIC(decl
) = 1;
1268 // Methods have to be public even if they are hidden because
1269 // they can be pulled into type descriptors when using
1270 // anonymous fields.
1271 else if (!Gogo::is_hidden_name(no
->name())
1272 || this->type_
->is_method())
1274 TREE_PUBLIC(decl
) = 1;
1275 std::string asm_name
= gogo
->unique_prefix();
1276 asm_name
.append(1, '.');
1277 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1278 SET_DECL_ASSEMBLER_NAME(decl
,
1279 get_identifier_from_string(asm_name
));
1282 // Why do we have to do this in the frontend?
1283 tree restype
= TREE_TYPE(functype
);
1284 tree resdecl
= build_decl(this->location(), RESULT_DECL
, NULL_TREE
,
1286 DECL_ARTIFICIAL(resdecl
) = 1;
1287 DECL_IGNORED_P(resdecl
) = 1;
1288 DECL_CONTEXT(resdecl
) = decl
;
1289 DECL_RESULT(decl
) = resdecl
;
1291 if (this->enclosing_
!= NULL
)
1292 DECL_STATIC_CHAIN(decl
) = 1;
1294 // If a function calls the predeclared recover function, we
1295 // can't inline it, because recover behaves differently in a
1296 // function passed directly to defer.
1297 if (this->calls_recover_
&& !this->is_recover_thunk_
)
1298 DECL_UNINLINABLE(decl
) = 1;
1300 // If this is a thunk created to call a function which calls
1301 // the predeclared recover function, we need to disable
1302 // stack splitting for the thunk.
1303 if (this->is_recover_thunk_
)
1305 tree attr
= get_identifier("__no_split_stack__");
1306 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1309 go_preserve_from_gc(decl
);
1311 if (this->closure_var_
!= NULL
)
1313 push_struct_function(decl
);
1315 tree closure_decl
= this->closure_var_
->get_tree(gogo
, no
);
1317 DECL_ARTIFICIAL(closure_decl
) = 1;
1318 DECL_IGNORED_P(closure_decl
) = 1;
1319 TREE_USED(closure_decl
) = 1;
1320 DECL_ARG_TYPE(closure_decl
) = TREE_TYPE(closure_decl
);
1321 TREE_READONLY(closure_decl
) = 1;
1323 DECL_STRUCT_FUNCTION(decl
)->static_chain_decl
= closure_decl
;
1328 return this->fndecl_
;
1331 // Get a tree for a function declaration.
1334 Function_declaration::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1336 if (this->fndecl_
== NULL_TREE
)
1338 // Let Go code use an asm declaration to pick up a builtin
1340 if (!this->asm_name_
.empty())
1342 std::map
<std::string
, tree
>::const_iterator p
=
1343 builtin_functions
.find(this->asm_name_
);
1344 if (p
!= builtin_functions
.end())
1346 this->fndecl_
= p
->second
;
1347 return this->fndecl_
;
1351 tree functype
= this->fntype_
->get_tree(gogo
);
1353 if (functype
== error_mark_node
)
1354 decl
= error_mark_node
;
1357 // The type of a function comes back as a pointer, but we
1358 // want the real function type for a function declaration.
1359 gcc_assert(POINTER_TYPE_P(functype
));
1360 functype
= TREE_TYPE(functype
);
1361 decl
= build_decl(this->location(), FUNCTION_DECL
, id
, functype
);
1362 TREE_PUBLIC(decl
) = 1;
1363 DECL_EXTERNAL(decl
) = 1;
1365 if (this->asm_name_
.empty())
1367 std::string asm_name
= (no
->package() == NULL
1368 ? gogo
->unique_prefix()
1369 : no
->package()->unique_prefix());
1370 asm_name
.append(1, '.');
1371 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1372 SET_DECL_ASSEMBLER_NAME(decl
,
1373 get_identifier_from_string(asm_name
));
1376 this->fndecl_
= decl
;
1377 go_preserve_from_gc(decl
);
1379 return this->fndecl_
;
1382 // We always pass the receiver to a method as a pointer. If the
1383 // receiver is actually declared as a non-pointer type, then we copy
1384 // the value into a local variable, so that it has the right type. In
1385 // this function we create the real PARM_DECL to use, and set
1386 // DEC_INITIAL of the var_decl to be the value passed in.
1389 Function::make_receiver_parm_decl(Gogo
* gogo
, Named_object
* no
, tree var_decl
)
1391 if (var_decl
== error_mark_node
)
1392 return error_mark_node
;
1393 // If the function takes the address of a receiver which is passed
1394 // by value, then we will have an INDIRECT_REF here. We need to get
1395 // the real variable.
1396 bool is_in_heap
= no
->var_value()->is_in_heap();
1398 if (TREE_CODE(var_decl
) != INDIRECT_REF
)
1400 gcc_assert(!is_in_heap
);
1401 val_type
= TREE_TYPE(var_decl
);
1405 gcc_assert(is_in_heap
);
1406 var_decl
= TREE_OPERAND(var_decl
, 0);
1407 if (var_decl
== error_mark_node
)
1408 return error_mark_node
;
1409 gcc_assert(POINTER_TYPE_P(TREE_TYPE(var_decl
)));
1410 val_type
= TREE_TYPE(TREE_TYPE(var_decl
));
1412 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1413 source_location loc
= DECL_SOURCE_LOCATION(var_decl
);
1414 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1416 tree id
= get_identifier_from_string(name
);
1417 tree parm_decl
= build_decl(loc
, PARM_DECL
, id
, build_pointer_type(val_type
));
1418 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1419 DECL_ARG_TYPE(parm_decl
) = TREE_TYPE(parm_decl
);
1421 gcc_assert(DECL_INITIAL(var_decl
) == NULL_TREE
);
1422 // The receiver might be passed as a null pointer.
1423 tree check
= fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, parm_decl
,
1424 fold_convert_loc(loc
, TREE_TYPE(parm_decl
),
1425 null_pointer_node
));
1426 tree ind
= build_fold_indirect_ref_loc(loc
, parm_decl
);
1427 TREE_THIS_NOTRAP(ind
) = 1;
1428 tree zero_init
= no
->var_value()->type()->get_init_tree(gogo
, false);
1429 tree init
= fold_build3_loc(loc
, COND_EXPR
, TREE_TYPE(ind
),
1430 check
, ind
, zero_init
);
1434 tree size
= TYPE_SIZE_UNIT(val_type
);
1435 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
,
1437 space
= save_expr(space
);
1438 space
= fold_convert(build_pointer_type(val_type
), space
);
1439 tree spaceref
= build_fold_indirect_ref_loc(no
->location(), space
);
1440 TREE_THIS_NOTRAP(spaceref
) = 1;
1441 tree check
= fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
,
1443 fold_convert_loc(loc
, TREE_TYPE(parm_decl
),
1444 null_pointer_node
));
1445 tree parmref
= build_fold_indirect_ref_loc(no
->location(), parm_decl
);
1446 TREE_THIS_NOTRAP(parmref
) = 1;
1447 tree set
= fold_build2_loc(loc
, MODIFY_EXPR
, void_type_node
,
1449 init
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(space
),
1450 build3(COND_EXPR
, void_type_node
,
1451 check
, set
, NULL_TREE
),
1455 DECL_INITIAL(var_decl
) = init
;
1460 // If we take the address of a parameter, then we need to copy it into
1461 // the heap. We will access it as a local variable via an
1465 Function::copy_parm_to_heap(Gogo
* gogo
, Named_object
* no
, tree ref
)
1467 if (ref
== error_mark_node
)
1468 return error_mark_node
;
1470 gcc_assert(TREE_CODE(ref
) == INDIRECT_REF
);
1472 tree var_decl
= TREE_OPERAND(ref
, 0);
1473 if (var_decl
== error_mark_node
)
1474 return error_mark_node
;
1475 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1476 source_location loc
= DECL_SOURCE_LOCATION(var_decl
);
1478 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1480 tree id
= get_identifier_from_string(name
);
1482 tree type
= TREE_TYPE(var_decl
);
1483 gcc_assert(POINTER_TYPE_P(type
));
1484 type
= TREE_TYPE(type
);
1486 tree parm_decl
= build_decl(loc
, PARM_DECL
, id
, type
);
1487 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1488 DECL_ARG_TYPE(parm_decl
) = type
;
1490 tree size
= TYPE_SIZE_UNIT(type
);
1491 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
, loc
);
1492 space
= save_expr(space
);
1493 space
= fold_convert(TREE_TYPE(var_decl
), space
);
1494 tree spaceref
= build_fold_indirect_ref_loc(loc
, space
);
1495 TREE_THIS_NOTRAP(spaceref
) = 1;
1496 tree init
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
1497 build2(MODIFY_EXPR
, void_type_node
, spaceref
, parm_decl
),
1499 DECL_INITIAL(var_decl
) = init
;
1504 // Get a tree for function code.
1507 Function::build_tree(Gogo
* gogo
, Named_object
* named_function
)
1509 tree fndecl
= this->fndecl_
;
1510 gcc_assert(fndecl
!= NULL_TREE
);
1512 tree params
= NULL_TREE
;
1515 tree declare_vars
= NULL_TREE
;
1516 for (Bindings::const_definitions_iterator p
=
1517 this->block_
->bindings()->begin_definitions();
1518 p
!= this->block_
->bindings()->end_definitions();
1521 if ((*p
)->is_variable() && (*p
)->var_value()->is_parameter())
1523 *pp
= (*p
)->get_tree(gogo
, named_function
);
1525 // We always pass the receiver to a method as a pointer. If
1526 // the receiver is declared as a non-pointer type, then we
1527 // copy the value into a local variable.
1528 if ((*p
)->var_value()->is_receiver()
1529 && (*p
)->var_value()->type()->points_to() == NULL
)
1531 tree parm_decl
= this->make_receiver_parm_decl(gogo
, *p
, *pp
);
1533 if (TREE_CODE(var
) == INDIRECT_REF
)
1534 var
= TREE_OPERAND(var
, 0);
1535 if (var
!= error_mark_node
)
1537 gcc_assert(TREE_CODE(var
) == VAR_DECL
);
1538 DECL_CHAIN(var
) = declare_vars
;
1543 else if ((*p
)->var_value()->is_in_heap())
1545 // If we take the address of a parameter, then we need
1546 // to copy it into the heap.
1547 tree parm_decl
= this->copy_parm_to_heap(gogo
, *p
, *pp
);
1548 if (*pp
!= error_mark_node
)
1550 gcc_assert(TREE_CODE(*pp
) == INDIRECT_REF
);
1551 tree var_decl
= TREE_OPERAND(*pp
, 0);
1552 if (var_decl
!= error_mark_node
)
1554 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1555 DECL_CHAIN(var_decl
) = declare_vars
;
1556 declare_vars
= var_decl
;
1562 if (*pp
!= error_mark_node
)
1564 gcc_assert(TREE_CODE(*pp
) == PARM_DECL
);
1565 pp
= &DECL_CHAIN(*pp
);
1568 else if ((*p
)->is_result_variable())
1570 tree var_decl
= (*p
)->get_tree(gogo
, named_function
);
1571 if (var_decl
!= error_mark_node
1572 && (*p
)->result_var_value()->is_in_heap())
1574 gcc_assert(TREE_CODE(var_decl
) == INDIRECT_REF
);
1575 var_decl
= TREE_OPERAND(var_decl
, 0);
1577 if (var_decl
!= error_mark_node
)
1579 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1580 DECL_CHAIN(var_decl
) = declare_vars
;
1581 declare_vars
= var_decl
;
1587 DECL_ARGUMENTS(fndecl
) = params
;
1589 if (this->block_
!= NULL
)
1591 gcc_assert(DECL_INITIAL(fndecl
) == NULL_TREE
);
1593 // Declare variables if necessary.
1594 tree bind
= NULL_TREE
;
1595 if (declare_vars
!= NULL_TREE
)
1597 tree block
= make_node(BLOCK
);
1598 BLOCK_SUPERCONTEXT(block
) = fndecl
;
1599 DECL_INITIAL(fndecl
) = block
;
1600 BLOCK_VARS(block
) = declare_vars
;
1601 TREE_USED(block
) = 1;
1602 bind
= build3(BIND_EXPR
, void_type_node
, BLOCK_VARS(block
),
1604 TREE_SIDE_EFFECTS(bind
) = 1;
1607 // Build the trees for all the statements in the function.
1608 Translate_context
context(gogo
, named_function
, NULL
, NULL_TREE
);
1609 tree code
= this->block_
->get_tree(&context
);
1611 tree init
= NULL_TREE
;
1612 tree except
= NULL_TREE
;
1613 tree fini
= NULL_TREE
;
1615 // Initialize variables if necessary.
1616 for (tree v
= declare_vars
; v
!= NULL_TREE
; v
= DECL_CHAIN(v
))
1618 tree dv
= build1(DECL_EXPR
, void_type_node
, v
);
1619 SET_EXPR_LOCATION(dv
, DECL_SOURCE_LOCATION(v
));
1620 append_to_statement_list(dv
, &init
);
1623 // If we have a defer stack, initialize it at the start of a
1625 if (this->defer_stack_
!= NULL_TREE
)
1627 tree defer_init
= build1(DECL_EXPR
, void_type_node
,
1628 this->defer_stack_
);
1629 SET_EXPR_LOCATION(defer_init
, this->block_
->start_location());
1630 append_to_statement_list(defer_init
, &init
);
1632 // Clean up the defer stack when we leave the function.
1633 this->build_defer_wrapper(gogo
, named_function
, &except
, &fini
);
1636 if (code
!= NULL_TREE
&& code
!= error_mark_node
)
1638 if (init
!= NULL_TREE
)
1639 code
= build2(COMPOUND_EXPR
, void_type_node
, init
, code
);
1640 if (except
!= NULL_TREE
)
1641 code
= build2(TRY_CATCH_EXPR
, void_type_node
, code
,
1642 build2(CATCH_EXPR
, void_type_node
, NULL
, except
));
1643 if (fini
!= NULL_TREE
)
1644 code
= build2(TRY_FINALLY_EXPR
, void_type_node
, code
, fini
);
1647 // Stick the code into the block we built for the receiver, if
1649 if (bind
!= NULL_TREE
&& code
!= NULL_TREE
&& code
!= error_mark_node
)
1651 BIND_EXPR_BODY(bind
) = code
;
1655 DECL_SAVED_TREE(fndecl
) = code
;
1659 // Build the wrappers around function code needed if the function has
1660 // any defer statements. This sets *EXCEPT to an exception handler
1661 // and *FINI to a finally handler.
1664 Function::build_defer_wrapper(Gogo
* gogo
, Named_object
* named_function
,
1665 tree
*except
, tree
*fini
)
1667 source_location end_loc
= this->block_
->end_location();
1669 // Add an exception handler. This is used if a panic occurs. Its
1670 // purpose is to stop the stack unwinding if a deferred function
1671 // calls recover. There are more details in
1672 // libgo/runtime/go-unwind.c.
1673 tree stmt_list
= NULL_TREE
;
1674 static tree check_fndecl
;
1675 tree call
= Gogo::call_builtin(&check_fndecl
,
1681 this->defer_stack(end_loc
));
1682 append_to_statement_list(call
, &stmt_list
);
1684 tree retval
= this->return_value(gogo
, named_function
, end_loc
, &stmt_list
);
1686 if (retval
== NULL_TREE
)
1689 set
= fold_build2_loc(end_loc
, MODIFY_EXPR
, void_type_node
,
1690 DECL_RESULT(this->fndecl_
), retval
);
1691 tree ret_stmt
= fold_build1_loc(end_loc
, RETURN_EXPR
, void_type_node
, set
);
1692 append_to_statement_list(ret_stmt
, &stmt_list
);
1694 gcc_assert(*except
== NULL_TREE
);
1695 *except
= stmt_list
;
1697 // Add some finally code to run the defer functions. This is used
1698 // both in the normal case, when no panic occurs, and also if a
1699 // panic occurs to run any further defer functions. Of course, it
1700 // is possible for a defer function to call panic which should be
1701 // caught by another defer function. To handle that we use a loop.
1703 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1704 // if (return values are named) return named_vals;
1708 tree label
= create_artificial_label(end_loc
);
1709 tree define_label
= fold_build1_loc(end_loc
, LABEL_EXPR
, void_type_node
,
1711 append_to_statement_list(define_label
, &stmt_list
);
1713 static tree undefer_fndecl
;
1714 tree undefer
= Gogo::call_builtin(&undefer_fndecl
,
1720 this->defer_stack(end_loc
));
1721 TREE_NOTHROW(undefer_fndecl
) = 0;
1723 tree defer
= Gogo::call_builtin(&check_fndecl
,
1729 this->defer_stack(end_loc
));
1730 tree jump
= fold_build1_loc(end_loc
, GOTO_EXPR
, void_type_node
, label
);
1731 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer
, jump
);
1732 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
1733 tree try_catch
= build2(TRY_CATCH_EXPR
, void_type_node
, undefer
, catch_body
);
1735 append_to_statement_list(try_catch
, &stmt_list
);
1737 if (this->type_
->results() != NULL
1738 && !this->type_
->results()->empty()
1739 && !this->type_
->results()->front().name().empty())
1741 // If the result variables are named, we need to return them
1742 // again, because they might have been changed by a defer
1744 retval
= this->return_value(gogo
, named_function
, end_loc
,
1746 set
= fold_build2_loc(end_loc
, MODIFY_EXPR
, void_type_node
,
1747 DECL_RESULT(this->fndecl_
), retval
);
1748 ret_stmt
= fold_build1_loc(end_loc
, RETURN_EXPR
, void_type_node
, set
);
1749 append_to_statement_list(ret_stmt
, &stmt_list
);
1752 gcc_assert(*fini
== NULL_TREE
);
1756 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1757 // also add statements to STMT_LIST, which need to be executed before
1758 // the assignment. This is used for a return statement with no
1762 Function::return_value(Gogo
* gogo
, Named_object
* named_function
,
1763 source_location location
, tree
* stmt_list
) const
1765 const Typed_identifier_list
* results
= this->type_
->results();
1766 if (results
== NULL
|| results
->empty())
1769 // In the case of an exception handler created for functions with
1770 // defer statements, the result variables may be unnamed.
1771 bool is_named
= !results
->front().name().empty();
1773 gcc_assert(this->named_results_
!= NULL
1774 && this->named_results_
->size() == results
->size());
1777 if (results
->size() == 1)
1780 return this->named_results_
->front()->get_tree(gogo
, named_function
);
1782 return results
->front().type()->get_init_tree(gogo
, false);
1786 tree rettype
= TREE_TYPE(DECL_RESULT(this->fndecl_
));
1787 retval
= create_tmp_var(rettype
, "RESULT");
1788 tree field
= TYPE_FIELDS(rettype
);
1790 for (Typed_identifier_list::const_iterator pr
= results
->begin();
1791 pr
!= results
->end();
1792 ++pr
, ++index
, field
= DECL_CHAIN(field
))
1794 gcc_assert(field
!= NULL
);
1797 val
= (*this->named_results_
)[index
]->get_tree(gogo
,
1800 val
= pr
->type()->get_init_tree(gogo
, false);
1801 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
1802 build3(COMPONENT_REF
, TREE_TYPE(field
),
1803 retval
, field
, NULL_TREE
),
1805 append_to_statement_list(set
, stmt_list
);
1811 // Get the tree for the variable holding the defer stack for this
1812 // function. At least at present, the value of this variable is not
1813 // used. However, a pointer to this variable is used as a marker for
1814 // the functions on the defer stack associated with this function.
1815 // Doing things this way permits inlining a function which uses defer.
1818 Function::defer_stack(source_location location
)
1820 if (this->defer_stack_
== NULL_TREE
)
1822 tree var
= create_tmp_var(ptr_type_node
, "DEFER");
1823 DECL_INITIAL(var
) = null_pointer_node
;
1824 DECL_SOURCE_LOCATION(var
) = location
;
1825 TREE_ADDRESSABLE(var
) = 1;
1826 this->defer_stack_
= var
;
1828 return fold_convert_loc(location
, ptr_type_node
,
1829 build_fold_addr_expr_loc(location
,
1830 this->defer_stack_
));
1833 // Get a tree for the statements in a block.
1836 Block::get_tree(Translate_context
* context
)
1838 Gogo
* gogo
= context
->gogo();
1840 tree block
= make_node(BLOCK
);
1842 // Put the new block into the block tree.
1844 if (context
->block() == NULL
)
1847 if (context
->function() != NULL
)
1848 fndecl
= context
->function()->func_value()->get_decl();
1850 fndecl
= current_function_decl
;
1851 gcc_assert(fndecl
!= NULL_TREE
);
1853 // We may have already created a block for the receiver.
1854 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1856 BLOCK_SUPERCONTEXT(block
) = fndecl
;
1857 DECL_INITIAL(fndecl
) = block
;
1861 tree superblock_tree
= DECL_INITIAL(fndecl
);
1862 BLOCK_SUPERCONTEXT(block
) = superblock_tree
;
1863 gcc_assert(BLOCK_CHAIN(block
) == NULL_TREE
);
1864 BLOCK_CHAIN(block
) = block
;
1869 tree superblock_tree
= context
->block_tree();
1870 BLOCK_SUPERCONTEXT(block
) = superblock_tree
;
1872 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1874 pp
= &BLOCK_CHAIN(*pp
))
1879 // Expand local variables in the block.
1881 tree
* pp
= &BLOCK_VARS(block
);
1882 for (Bindings::const_definitions_iterator pv
=
1883 this->bindings_
->begin_definitions();
1884 pv
!= this->bindings_
->end_definitions();
1887 if ((!(*pv
)->is_variable() || !(*pv
)->var_value()->is_parameter())
1888 && !(*pv
)->is_result_variable()
1889 && !(*pv
)->is_const())
1891 tree var
= (*pv
)->get_tree(gogo
, context
->function());
1892 if (var
!= error_mark_node
&& TREE_TYPE(var
) != error_mark_node
)
1894 if ((*pv
)->is_variable() && (*pv
)->var_value()->is_in_heap())
1896 gcc_assert(TREE_CODE(var
) == INDIRECT_REF
);
1897 var
= TREE_OPERAND(var
, 0);
1898 gcc_assert(TREE_CODE(var
) == VAR_DECL
);
1901 pp
= &DECL_CHAIN(*pp
);
1907 Translate_context
subcontext(context
->gogo(), context
->function(),
1910 tree statements
= NULL_TREE
;
1912 // Expand the statements.
1914 for (std::vector
<Statement
*>::const_iterator p
= this->statements_
.begin();
1915 p
!= this->statements_
.end();
1918 tree statement
= (*p
)->get_tree(&subcontext
);
1919 if (statement
!= error_mark_node
)
1920 append_to_statement_list(statement
, &statements
);
1923 TREE_USED(block
) = 1;
1925 tree bind
= build3(BIND_EXPR
, void_type_node
, BLOCK_VARS(block
), statements
,
1927 TREE_SIDE_EFFECTS(bind
) = 1;
1932 // Get the LABEL_DECL for a label.
1937 if (this->decl_
== NULL
)
1939 tree id
= get_identifier_from_string(this->name_
);
1940 this->decl_
= build_decl(this->location_
, LABEL_DECL
, id
, void_type_node
);
1941 DECL_CONTEXT(this->decl_
) = current_function_decl
;
1946 // Return an expression for the address of this label.
1949 Label::get_addr(source_location location
)
1951 tree decl
= this->get_decl();
1952 TREE_USED(decl
) = 1;
1953 TREE_ADDRESSABLE(decl
) = 1;
1954 return fold_convert_loc(location
, ptr_type_node
,
1955 build_fold_addr_expr_loc(location
, decl
));
1958 // Get the LABEL_DECL for an unnamed label.
1961 Unnamed_label::get_decl()
1963 if (this->decl_
== NULL
)
1964 this->decl_
= create_artificial_label(this->location_
);
1968 // Get the LABEL_EXPR for an unnamed label.
1971 Unnamed_label::get_definition()
1973 tree t
= build1(LABEL_EXPR
, void_type_node
, this->get_decl());
1974 SET_EXPR_LOCATION(t
, this->location_
);
1978 // Return a goto to this label.
1981 Unnamed_label::get_goto(source_location location
)
1983 tree t
= build1(GOTO_EXPR
, void_type_node
, this->get_decl());
1984 SET_EXPR_LOCATION(t
, location
);
1988 // Return the integer type to use for a size.
1992 go_type_for_size(unsigned int bits
, int unsignedp
)
1998 name
= unsignedp
? "uint8" : "int8";
2001 name
= unsignedp
? "uint16" : "int16";
2004 name
= unsignedp
? "uint32" : "int32";
2007 name
= unsignedp
? "uint64" : "int64";
2010 if (bits
== POINTER_SIZE
&& unsignedp
)
2015 Type
* type
= Type::lookup_integer_type(name
);
2016 return type
->get_tree(go_get_gogo());
2019 // Return the type to use for a mode.
2023 go_type_for_mode(enum machine_mode mode
, int unsignedp
)
2025 // FIXME: This static_cast should be in machmode.h.
2026 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
2028 return go_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
2029 else if (mc
== MODE_FLOAT
)
2032 switch (GET_MODE_BITSIZE (mode
))
2035 type
= Type::lookup_float_type("float32");
2038 type
= Type::lookup_float_type("float64");
2041 // We have to check for long double in order to support
2042 // i386 excess precision.
2043 if (mode
== TYPE_MODE(long_double_type_node
))
2044 return long_double_type_node
;
2047 return type
->float_type()->type_tree();
2049 else if (mc
== MODE_COMPLEX_FLOAT
)
2052 switch (GET_MODE_BITSIZE (mode
))
2055 type
= Type::lookup_complex_type("complex64");
2058 type
= Type::lookup_complex_type("complex128");
2061 // We have to check for long double in order to support
2062 // i386 excess precision.
2063 if (mode
== TYPE_MODE(complex_long_double_type_node
))
2064 return complex_long_double_type_node
;
2067 return type
->complex_type()->type_tree();
2073 // Return a tree which allocates SIZE bytes which will holds value of
2077 Gogo::allocate_memory(Type
* type
, tree size
, source_location location
)
2079 // If the package imports unsafe, then it may play games with
2080 // pointers that look like integers.
2081 if (this->imported_unsafe_
|| type
->has_pointer())
2083 static tree new_fndecl
;
2084 return Gogo::call_builtin(&new_fndecl
,
2094 static tree new_nopointers_fndecl
;
2095 return Gogo::call_builtin(&new_nopointers_fndecl
,
2097 "__go_new_nopointers",
2105 // Build a builtin struct with a list of fields. The name is
2106 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
2107 // node; this exists so that the struct can have fields which point to
2108 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
2109 // are NFIELDS fields. Each field is a name (a const char*) followed
2110 // by a type (a tree).
2113 Gogo::builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
2116 if (ptype
!= NULL
&& *ptype
!= NULL_TREE
)
2120 va_start(ap
, nfields
);
2122 tree fields
= NULL_TREE
;
2123 for (int i
= 0; i
< nfields
; ++i
)
2125 const char* field_name
= va_arg(ap
, const char*);
2126 tree type
= va_arg(ap
, tree
);
2127 if (type
== error_mark_node
)
2130 *ptype
= error_mark_node
;
2131 return error_mark_node
;
2133 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
,
2134 get_identifier(field_name
), type
);
2135 DECL_CHAIN(field
) = fields
;
2141 if (struct_type
== NULL_TREE
)
2142 struct_type
= make_node(RECORD_TYPE
);
2143 finish_builtin_struct(struct_type
, struct_name
, fields
, NULL_TREE
);
2147 go_preserve_from_gc(struct_type
);
2148 *ptype
= struct_type
;
2154 // Return a type to use for pointer to const char for a string.
2157 Gogo::const_char_pointer_type_tree()
2160 if (type
== NULL_TREE
)
2162 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
2164 type
= build_pointer_type(const_char_type
);
2165 go_preserve_from_gc(type
);
2170 // Return a tree for a string constant.
2173 Gogo::string_constant_tree(const std::string
& val
)
2175 tree index_type
= build_index_type(size_int(val
.length()));
2176 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
2178 tree string_type
= build_array_type(const_char_type
, index_type
);
2179 string_type
= build_variant_type_copy(string_type
);
2180 TYPE_STRING_FLAG(string_type
) = 1;
2181 tree string_val
= build_string(val
.length(), val
.data());
2182 TREE_TYPE(string_val
) = string_type
;
2186 // Return a tree for a Go string constant.
2189 Gogo::go_string_constant_tree(const std::string
& val
)
2191 tree string_type
= Type::make_string_type()->get_tree(this);
2193 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2195 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2196 tree field
= TYPE_FIELDS(string_type
);
2197 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__data") == 0);
2199 tree str
= Gogo::string_constant_tree(val
);
2200 elt
->value
= fold_convert(TREE_TYPE(field
),
2201 build_fold_addr_expr(str
));
2203 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2204 field
= DECL_CHAIN(field
);
2205 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__length") == 0);
2207 elt
->value
= build_int_cst_type(TREE_TYPE(field
), val
.length());
2209 tree constructor
= build_constructor(string_type
, init
);
2210 TREE_READONLY(constructor
) = 1;
2211 TREE_CONSTANT(constructor
) = 1;
2216 // Return a tree for a pointer to a Go string constant. This is only
2217 // used for type descriptors, so we return a pointer to a constant
2221 Gogo::ptr_go_string_constant_tree(const std::string
& val
)
2223 tree pval
= this->go_string_constant_tree(val
);
2225 tree decl
= build_decl(UNKNOWN_LOCATION
, VAR_DECL
,
2226 create_tmp_var_name("SP"), TREE_TYPE(pval
));
2227 DECL_EXTERNAL(decl
) = 0;
2228 TREE_PUBLIC(decl
) = 0;
2229 TREE_USED(decl
) = 1;
2230 TREE_READONLY(decl
) = 1;
2231 TREE_CONSTANT(decl
) = 1;
2232 TREE_STATIC(decl
) = 1;
2233 DECL_ARTIFICIAL(decl
) = 1;
2234 DECL_INITIAL(decl
) = pval
;
2235 rest_of_decl_compilation(decl
, 1, 0);
2237 return build_fold_addr_expr(decl
);
2240 // Build the type of the struct that holds a slice for the given
2244 Gogo::slice_type_tree(tree element_type_tree
)
2246 // We use int for the count and capacity fields in a slice header.
2247 // This matches 6g. The language definition guarantees that we
2248 // can't allocate space of a size which does not fit in int
2249 // anyhow. FIXME: integer_type_node is the the C type "int" but is
2250 // not necessarily the Go type "int". They will differ when the C
2251 // type "int" has fewer than 32 bits.
2252 return Gogo::builtin_struct(NULL
, "__go_slice", NULL_TREE
, 3,
2254 build_pointer_type(element_type_tree
),
2261 // Given the tree for a slice type, return the tree for the type of
2262 // the elements of the slice.
2265 Gogo::slice_element_type_tree(tree slice_type_tree
)
2267 gcc_assert(TREE_CODE(slice_type_tree
) == RECORD_TYPE
2268 && POINTER_TYPE_P(TREE_TYPE(TYPE_FIELDS(slice_type_tree
))));
2269 return TREE_TYPE(TREE_TYPE(TYPE_FIELDS(slice_type_tree
)));
2272 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2273 // the slice. VALUES is the value pointer and COUNT is the number of
2274 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2275 // the capacity and the count are the same.
2278 Gogo::slice_constructor(tree slice_type_tree
, tree values
, tree count
,
2281 gcc_assert(TREE_CODE(slice_type_tree
) == RECORD_TYPE
);
2283 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
2285 tree field
= TYPE_FIELDS(slice_type_tree
);
2286 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
2287 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2289 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field
))
2290 == TYPE_MAIN_VARIANT(TREE_TYPE(values
)));
2291 elt
->value
= values
;
2293 count
= fold_convert(sizetype
, count
);
2294 if (capacity
== NULL_TREE
)
2296 count
= save_expr(count
);
2300 field
= DECL_CHAIN(field
);
2301 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
2302 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2304 elt
->value
= fold_convert(TREE_TYPE(field
), count
);
2306 field
= DECL_CHAIN(field
);
2307 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
2308 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2310 elt
->value
= fold_convert(TREE_TYPE(field
), capacity
);
2312 return build_constructor(slice_type_tree
, init
);
2315 // Build a constructor for an empty slice.
2318 Gogo::empty_slice_constructor(tree slice_type_tree
)
2320 tree element_field
= TYPE_FIELDS(slice_type_tree
);
2321 tree ret
= Gogo::slice_constructor(slice_type_tree
,
2322 fold_convert(TREE_TYPE(element_field
),
2326 TREE_CONSTANT(ret
) = 1;
2330 // Build a map descriptor for a map of type MAPTYPE.
2333 Gogo::map_descriptor(Map_type
* maptype
)
2335 if (this->map_descriptors_
== NULL
)
2336 this->map_descriptors_
= new Map_descriptors(10);
2338 std::pair
<const Map_type
*, tree
> val(maptype
, NULL
);
2339 std::pair
<Map_descriptors::iterator
, bool> ins
=
2340 this->map_descriptors_
->insert(val
);
2341 Map_descriptors::iterator p
= ins
.first
;
2344 gcc_assert(p
->second
!= NULL_TREE
&& DECL_P(p
->second
));
2345 return build_fold_addr_expr(p
->second
);
2348 Type
* keytype
= maptype
->key_type();
2349 Type
* valtype
= maptype
->val_type();
2351 std::string mangled_name
= ("__go_map_" + maptype
->mangled_name(this));
2353 tree id
= get_identifier_from_string(mangled_name
);
2355 // Get the type of the map descriptor. This is __go_map_descriptor
2358 tree struct_type
= this->map_descriptor_type();
2360 // The map entry type is a struct with three fields. This struct is
2361 // specific to MAPTYPE. Build it.
2363 tree map_entry_type
= make_node(RECORD_TYPE
);
2365 map_entry_type
= Gogo::builtin_struct(NULL
, "__map", map_entry_type
, 3,
2367 build_pointer_type(map_entry_type
),
2369 keytype
->get_tree(this),
2371 valtype
->get_tree(this));
2372 if (map_entry_type
== error_mark_node
)
2373 return error_mark_node
;
2375 tree map_entry_key_field
= DECL_CHAIN(TYPE_FIELDS(map_entry_type
));
2376 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field
)),
2379 tree map_entry_val_field
= DECL_CHAIN(map_entry_key_field
);
2380 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field
)),
2383 // Initialize the entries.
2385 tree map_descriptor_field
= TYPE_FIELDS(struct_type
);
2386 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field
)),
2387 "__map_descriptor") == 0);
2388 tree entry_size_field
= DECL_CHAIN(map_descriptor_field
);
2389 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field
)),
2390 "__entry_size") == 0);
2391 tree key_offset_field
= DECL_CHAIN(entry_size_field
);
2392 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field
)),
2393 "__key_offset") == 0);
2394 tree val_offset_field
= DECL_CHAIN(key_offset_field
);
2395 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field
)),
2396 "__val_offset") == 0);
2398 VEC(constructor_elt
, gc
)* descriptor
= VEC_alloc(constructor_elt
, gc
, 6);
2400 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2401 elt
->index
= map_descriptor_field
;
2402 elt
->value
= maptype
->type_descriptor_pointer(this);
2404 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2405 elt
->index
= entry_size_field
;
2406 elt
->value
= TYPE_SIZE_UNIT(map_entry_type
);
2408 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2409 elt
->index
= key_offset_field
;
2410 elt
->value
= byte_position(map_entry_key_field
);
2412 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2413 elt
->index
= val_offset_field
;
2414 elt
->value
= byte_position(map_entry_val_field
);
2416 tree constructor
= build_constructor(struct_type
, descriptor
);
2418 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, struct_type
);
2419 TREE_STATIC(decl
) = 1;
2420 TREE_USED(decl
) = 1;
2421 TREE_READONLY(decl
) = 1;
2422 TREE_CONSTANT(decl
) = 1;
2423 DECL_INITIAL(decl
) = constructor
;
2424 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2425 resolve_unique_section(decl
, 1, 0);
2427 rest_of_decl_compilation(decl
, 1, 0);
2429 go_preserve_from_gc(decl
);
2432 return build_fold_addr_expr(decl
);
2435 // Return a tree for the type of a map descriptor. This is struct
2436 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
2440 Gogo::map_descriptor_type()
2442 static tree struct_type
;
2443 tree dtype
= Type::make_type_descriptor_type()->get_tree(this);
2444 dtype
= build_qualified_type(dtype
, TYPE_QUAL_CONST
);
2445 return Gogo::builtin_struct(&struct_type
, "__go_map_descriptor", NULL_TREE
,
2448 build_pointer_type(dtype
),
2457 // Return the name to use for a type descriptor decl for TYPE. This
2458 // is used when TYPE does not have a name.
2461 Gogo::unnamed_type_descriptor_decl_name(const Type
* type
)
2463 return "__go_td_" + type
->mangled_name(this);
2466 // Return the name to use for a type descriptor decl for a type named
2467 // NAME, defined in the function IN_FUNCTION. IN_FUNCTION will
2468 // normally be NULL.
2471 Gogo::type_descriptor_decl_name(const Named_object
* no
,
2472 const Named_object
* in_function
)
2474 std::string ret
= "__go_tdn_";
2475 if (no
->type_value()->is_builtin())
2476 gcc_assert(in_function
== NULL
);
2479 const std::string
& unique_prefix(no
->package() == NULL
2480 ? this->unique_prefix()
2481 : no
->package()->unique_prefix());
2482 const std::string
& package_name(no
->package() == NULL
2483 ? this->package_name()
2484 : no
->package()->name());
2485 ret
.append(unique_prefix
);
2487 ret
.append(package_name
);
2489 if (in_function
!= NULL
)
2491 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
2495 ret
.append(no
->name());
2499 // Where a type descriptor decl should be defined.
2501 Gogo::Type_descriptor_location
2502 Gogo::type_descriptor_location(const Type
* type
)
2504 const Named_type
* name
= type
->named_type();
2507 if (name
->named_object()->package() != NULL
)
2509 // This is a named type defined in a different package. The
2510 // descriptor should be defined in that package.
2511 return TYPE_DESCRIPTOR_UNDEFINED
;
2513 else if (name
->is_builtin())
2515 // We create the descriptor for a builtin type whenever we
2517 return TYPE_DESCRIPTOR_COMMON
;
2521 // This is a named type defined in this package. The
2522 // descriptor should be defined here.
2523 return TYPE_DESCRIPTOR_DEFINED
;
2528 if (type
->points_to() != NULL
2529 && type
->points_to()->named_type() != NULL
2530 && type
->points_to()->named_type()->named_object()->package() != NULL
)
2532 // This is an unnamed pointer to a named type defined in a
2533 // different package. The descriptor should be defined in
2535 return TYPE_DESCRIPTOR_UNDEFINED
;
2539 // This is an unnamed type. The descriptor could be defined
2540 // in any package where it is needed, and the linker will
2541 // pick one descriptor to keep.
2542 return TYPE_DESCRIPTOR_COMMON
;
2547 // Build a type descriptor decl for TYPE. INITIALIZER is a struct
2548 // composite literal which initializers the type descriptor.
2551 Gogo::build_type_descriptor_decl(const Type
* type
, Expression
* initializer
,
2554 const Named_type
* name
= type
->named_type();
2556 // We can have multiple instances of unnamed types, but we only want
2557 // to emit the type descriptor once. We use a hash table to handle
2558 // this. This is not necessary for named types, as they are unique,
2559 // and we store the type descriptor decl in the type itself.
2563 if (this->type_descriptor_decls_
== NULL
)
2564 this->type_descriptor_decls_
= new Type_descriptor_decls(10);
2566 std::pair
<Type_descriptor_decls::iterator
, bool> ins
=
2567 this->type_descriptor_decls_
->insert(std::make_pair(type
, NULL_TREE
));
2570 // We've already built a type descriptor for this type.
2571 *pdecl
= ins
.first
->second
;
2574 phash
= &ins
.first
->second
;
2577 std::string decl_name
;
2579 decl_name
= this->unnamed_type_descriptor_decl_name(type
);
2581 decl_name
= this->type_descriptor_decl_name(name
->named_object(),
2582 name
->in_function());
2583 tree id
= get_identifier_from_string(decl_name
);
2584 tree descriptor_type_tree
= initializer
->type()->get_tree(this);
2585 if (descriptor_type_tree
== error_mark_node
)
2587 *pdecl
= error_mark_node
;
2590 tree decl
= build_decl(name
== NULL
? BUILTINS_LOCATION
: name
->location(),
2592 build_qualified_type(descriptor_type_tree
,
2594 TREE_READONLY(decl
) = 1;
2595 TREE_CONSTANT(decl
) = 1;
2596 DECL_ARTIFICIAL(decl
) = 1;
2598 go_preserve_from_gc(decl
);
2602 // We store the new DECL now because we may need to refer to it when
2603 // expanding INITIALIZER.
2606 // If appropriate, just refer to the exported type identifier.
2607 Gogo::Type_descriptor_location type_descriptor_location
=
2608 this->type_descriptor_location(type
);
2609 if (type_descriptor_location
== TYPE_DESCRIPTOR_UNDEFINED
)
2611 TREE_PUBLIC(decl
) = 1;
2612 DECL_EXTERNAL(decl
) = 1;
2616 TREE_STATIC(decl
) = 1;
2617 TREE_USED(decl
) = 1;
2619 Translate_context
context(this, NULL
, NULL
, NULL
);
2620 context
.set_is_const();
2621 tree constructor
= initializer
->get_tree(&context
);
2623 if (constructor
== error_mark_node
)
2624 gcc_assert(saw_errors());
2626 DECL_INITIAL(decl
) = constructor
;
2628 if (type_descriptor_location
== TYPE_DESCRIPTOR_COMMON
)
2630 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2631 resolve_unique_section(decl
, 1, 0);
2635 #ifdef OBJECT_FORMAT_ELF
2636 // Give the decl protected visibility. This avoids out-of-range
2637 // references with shared libraries with the x86_64 small model
2638 // when the type descriptor gets a COPY reloc into the main
2639 // executable. There is no need to have unique pointers to type
2640 // descriptors, as the runtime code compares reflection strings
2642 DECL_VISIBILITY(decl
) = VISIBILITY_PROTECTED
;
2643 DECL_VISIBILITY_SPECIFIED(decl
) = 1;
2646 TREE_PUBLIC(decl
) = 1;
2649 rest_of_decl_compilation(decl
, 1, 0);
2652 // Build an interface method table for a type: a list of function
2653 // pointers, one for each interface method. This is used for
2657 Gogo::interface_method_table_for_type(const Interface_type
* interface
,
2661 const Typed_identifier_list
* interface_methods
= interface
->methods();
2662 gcc_assert(!interface_methods
->empty());
2664 std::string mangled_name
= ((is_pointer
? "__go_pimt__" : "__go_imt_")
2665 + interface
->mangled_name(this)
2667 + type
->mangled_name(this));
2669 tree id
= get_identifier_from_string(mangled_name
);
2671 // See whether this interface has any hidden methods.
2672 bool has_hidden_methods
= false;
2673 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2674 p
!= interface_methods
->end();
2677 if (Gogo::is_hidden_name(p
->name()))
2679 has_hidden_methods
= true;
2684 // We already know that the named type is convertible to the
2685 // interface. If the interface has hidden methods, and the named
2686 // type is defined in a different package, then the interface
2687 // conversion table will be defined by that other package.
2688 if (has_hidden_methods
&& type
->named_object()->package() != NULL
)
2690 tree array_type
= build_array_type(const_ptr_type_node
, NULL
);
2691 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2692 TREE_READONLY(decl
) = 1;
2693 TREE_CONSTANT(decl
) = 1;
2694 TREE_PUBLIC(decl
) = 1;
2695 DECL_EXTERNAL(decl
) = 1;
2696 go_preserve_from_gc(decl
);
2700 size_t count
= interface_methods
->size();
2701 VEC(constructor_elt
, gc
)* pointers
= VEC_alloc(constructor_elt
, gc
,
2704 // The first element is the type descriptor.
2705 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2706 elt
->index
= size_zero_node
;
2711 td_type
= Type::make_pointer_type(type
);
2712 elt
->value
= fold_convert(const_ptr_type_node
,
2713 td_type
->type_descriptor_pointer(this));
2716 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2717 p
!= interface_methods
->end();
2721 Method
* m
= type
->method_function(p
->name(), &is_ambiguous
);
2722 gcc_assert(m
!= NULL
);
2724 Named_object
* no
= m
->named_object();
2726 tree fnid
= no
->get_id(this);
2729 if (no
->is_function())
2730 fndecl
= no
->func_value()->get_or_make_decl(this, no
, fnid
);
2731 else if (no
->is_function_declaration())
2732 fndecl
= no
->func_declaration_value()->get_or_make_decl(this, no
,
2736 fndecl
= build_fold_addr_expr(fndecl
);
2738 elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2739 elt
->index
= size_int(i
);
2740 elt
->value
= fold_convert(const_ptr_type_node
, fndecl
);
2742 gcc_assert(i
== count
+ 1);
2744 tree array_type
= build_array_type(const_ptr_type_node
,
2745 build_index_type(size_int(count
)));
2746 tree constructor
= build_constructor(array_type
, pointers
);
2748 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2749 TREE_STATIC(decl
) = 1;
2750 TREE_USED(decl
) = 1;
2751 TREE_READONLY(decl
) = 1;
2752 TREE_CONSTANT(decl
) = 1;
2753 DECL_INITIAL(decl
) = constructor
;
2755 // If the interface type has hidden methods, then this is the only
2756 // definition of the table. Otherwise it is a comdat table which
2757 // may be defined in multiple packages.
2758 if (has_hidden_methods
)
2760 #ifdef OBJECT_FORMAT_ELF
2761 // Give the decl protected visibility. This avoids out-of-range
2762 // references with shared libraries with the x86_64 small model
2763 // when the table gets a COPY reloc into the main executable.
2764 DECL_VISIBILITY(decl
) = VISIBILITY_PROTECTED
;
2765 DECL_VISIBILITY_SPECIFIED(decl
) = 1;
2768 TREE_PUBLIC(decl
) = 1;
2772 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2773 resolve_unique_section(decl
, 1, 0);
2776 rest_of_decl_compilation(decl
, 1, 0);
2778 go_preserve_from_gc(decl
);
2783 // Mark a function as a builtin library function.
2786 Gogo::mark_fndecl_as_builtin_library(tree fndecl
)
2788 DECL_EXTERNAL(fndecl
) = 1;
2789 TREE_PUBLIC(fndecl
) = 1;
2790 DECL_ARTIFICIAL(fndecl
) = 1;
2791 TREE_NOTHROW(fndecl
) = 1;
2792 DECL_VISIBILITY(fndecl
) = VISIBILITY_DEFAULT
;
2793 DECL_VISIBILITY_SPECIFIED(fndecl
) = 1;
2796 // Build a call to a builtin function.
2799 Gogo::call_builtin(tree
* pdecl
, source_location location
, const char* name
,
2800 int nargs
, tree rettype
, ...)
2802 if (rettype
== error_mark_node
)
2803 return error_mark_node
;
2805 tree
* types
= new tree
[nargs
];
2806 tree
* args
= new tree
[nargs
];
2809 va_start(ap
, rettype
);
2810 for (int i
= 0; i
< nargs
; ++i
)
2812 types
[i
] = va_arg(ap
, tree
);
2813 args
[i
] = va_arg(ap
, tree
);
2814 if (types
[i
] == error_mark_node
|| args
[i
] == error_mark_node
)
2815 return error_mark_node
;
2819 if (*pdecl
== NULL_TREE
)
2821 tree fnid
= get_identifier(name
);
2823 tree argtypes
= NULL_TREE
;
2824 tree
* pp
= &argtypes
;
2825 for (int i
= 0; i
< nargs
; ++i
)
2827 *pp
= tree_cons(NULL_TREE
, types
[i
], NULL_TREE
);
2828 pp
= &TREE_CHAIN(*pp
);
2830 *pp
= void_list_node
;
2832 tree fntype
= build_function_type(rettype
, argtypes
);
2834 *pdecl
= build_decl(BUILTINS_LOCATION
, FUNCTION_DECL
, fnid
, fntype
);
2835 Gogo::mark_fndecl_as_builtin_library(*pdecl
);
2836 go_preserve_from_gc(*pdecl
);
2839 tree fnptr
= build_fold_addr_expr(*pdecl
);
2840 if (CAN_HAVE_LOCATION_P(fnptr
))
2841 SET_EXPR_LOCATION(fnptr
, location
);
2843 tree ret
= build_call_array(rettype
, fnptr
, nargs
, args
);
2844 SET_EXPR_LOCATION(ret
, location
);
2852 // Build a call to the runtime error function.
2855 Gogo::runtime_error(int code
, source_location location
)
2857 static tree runtime_error_fndecl
;
2858 tree ret
= Gogo::call_builtin(&runtime_error_fndecl
,
2860 "__go_runtime_error",
2864 build_int_cst(integer_type_node
, code
));
2865 // The runtime error function panics and does not return.
2866 TREE_NOTHROW(runtime_error_fndecl
) = 0;
2867 TREE_THIS_VOLATILE(runtime_error_fndecl
) = 1;
2871 // Send VAL on CHANNEL. If BLOCKING is true, the resulting tree has a
2872 // void type. If BLOCKING is false, the resulting tree has a boolean
2873 // type, and it will evaluate as true if the value was sent. If
2874 // FOR_SELECT is true, this is being done because it was chosen in a
2875 // select statement.
2878 Gogo::send_on_channel(tree channel
, tree val
, bool blocking
, bool for_select
,
2879 source_location location
)
2881 if (int_size_in_bytes(TREE_TYPE(val
)) <= 8
2882 && !AGGREGATE_TYPE_P(TREE_TYPE(val
))
2883 && !FLOAT_TYPE_P(TREE_TYPE(val
)))
2885 val
= convert_to_integer(uint64_type_node
, val
);
2888 static tree send_small_fndecl
;
2889 tree ret
= Gogo::call_builtin(&send_small_fndecl
,
2901 : boolean_false_node
));
2902 // This can panic if there are too many operations on a
2904 TREE_NOTHROW(send_small_fndecl
) = 0;
2909 gcc_assert(!for_select
);
2910 static tree send_nonblocking_small_fndecl
;
2911 tree ret
= Gogo::call_builtin(&send_nonblocking_small_fndecl
,
2913 "__go_send_nonblocking_small",
2920 // This can panic if there are too many operations on a
2922 TREE_NOTHROW(send_nonblocking_small_fndecl
) = 0;
2929 if (TREE_ADDRESSABLE(TREE_TYPE(val
)) || TREE_CODE(val
) == VAR_DECL
)
2931 make_tmp
= NULL_TREE
;
2932 val
= build_fold_addr_expr(val
);
2934 TREE_ADDRESSABLE(val
) = 1;
2938 tree tmp
= create_tmp_var(TREE_TYPE(val
), get_name(val
));
2939 DECL_IGNORED_P(tmp
) = 0;
2940 DECL_INITIAL(tmp
) = val
;
2941 TREE_ADDRESSABLE(tmp
) = 1;
2942 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
2943 SET_EXPR_LOCATION(make_tmp
, location
);
2944 val
= build_fold_addr_expr(tmp
);
2946 val
= fold_convert(ptr_type_node
, val
);
2951 static tree send_big_fndecl
;
2952 call
= Gogo::call_builtin(&send_big_fndecl
,
2964 : boolean_false_node
));
2965 // This can panic if there are too many operations on a
2967 TREE_NOTHROW(send_big_fndecl
) = 0;
2971 gcc_assert(!for_select
);
2972 static tree send_nonblocking_big_fndecl
;
2973 call
= Gogo::call_builtin(&send_nonblocking_big_fndecl
,
2975 "__go_send_nonblocking_big",
2982 // This can panic if there are too many operations on a
2984 TREE_NOTHROW(send_nonblocking_big_fndecl
) = 0;
2987 if (make_tmp
== NULL_TREE
)
2991 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(call
), make_tmp
, call
);
2992 SET_EXPR_LOCATION(ret
, location
);
2998 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2999 // This does a blocking receive and returns the value read from the
3000 // channel. If FOR_SELECT is true, this is being done because it was
3001 // chosen in a select statement.
3004 Gogo::receive_from_channel(tree type_tree
, tree channel
, bool for_select
,
3005 source_location location
)
3007 if (int_size_in_bytes(type_tree
) <= 8
3008 && !AGGREGATE_TYPE_P(type_tree
)
3009 && !FLOAT_TYPE_P(type_tree
))
3011 static tree receive_small_fndecl
;
3012 tree call
= Gogo::call_builtin(&receive_small_fndecl
,
3014 "__go_receive_small",
3022 : boolean_false_node
));
3023 // This can panic if there are too many operations on a closed
3025 TREE_NOTHROW(receive_small_fndecl
) = 0;
3026 int bitsize
= GET_MODE_BITSIZE(TYPE_MODE(type_tree
));
3027 tree int_type_tree
= go_type_for_size(bitsize
, 1);
3028 return fold_convert_loc(location
, type_tree
,
3029 fold_convert_loc(location
, int_type_tree
,
3034 tree tmp
= create_tmp_var(type_tree
, get_name(type_tree
));
3035 DECL_IGNORED_P(tmp
) = 0;
3036 TREE_ADDRESSABLE(tmp
) = 1;
3037 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
3038 SET_EXPR_LOCATION(make_tmp
, location
);
3039 tree tmpaddr
= build_fold_addr_expr(tmp
);
3040 tmpaddr
= fold_convert(ptr_type_node
, tmpaddr
);
3041 static tree receive_big_fndecl
;
3042 tree call
= Gogo::call_builtin(&receive_big_fndecl
,
3054 : boolean_false_node
));
3055 // This can panic if there are too many operations on a closed
3057 TREE_NOTHROW(receive_big_fndecl
) = 0;
3058 return build2(COMPOUND_EXPR
, type_tree
, make_tmp
,
3059 build2(COMPOUND_EXPR
, type_tree
, call
, tmp
));
3063 // Return the type of a function trampoline. This is like
3064 // get_trampoline_type in tree-nested.c.
3067 Gogo::trampoline_type_tree()
3069 static tree type_tree
;
3070 if (type_tree
== NULL_TREE
)
3074 go_trampoline_info(&size
, &align
);
3075 tree t
= build_index_type(build_int_cst(integer_type_node
, size
- 1));
3076 t
= build_array_type(char_type_node
, t
);
3078 type_tree
= Gogo::builtin_struct(NULL
, "__go_trampoline", NULL_TREE
, 1,
3080 t
= TYPE_FIELDS(type_tree
);
3081 DECL_ALIGN(t
) = align
;
3082 DECL_USER_ALIGN(t
) = 1;
3084 go_preserve_from_gc(type_tree
);
3089 // Make a trampoline which calls FNADDR passing CLOSURE.
3092 Gogo::make_trampoline(tree fnaddr
, tree closure
, source_location location
)
3094 tree trampoline_type
= Gogo::trampoline_type_tree();
3095 tree trampoline_size
= TYPE_SIZE_UNIT(trampoline_type
);
3097 closure
= save_expr(closure
);
3099 // We allocate the trampoline using a special function which will
3100 // mark it as executable.
3101 static tree trampoline_fndecl
;
3102 tree x
= Gogo::call_builtin(&trampoline_fndecl
,
3104 "__go_allocate_trampoline",
3110 fold_convert_loc(location
, ptr_type_node
,
3115 // Initialize the trampoline.
3116 tree ini
= build_call_expr(implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
],
3117 3, x
, fnaddr
, closure
);
3119 // On some targets the trampoline address needs to be adjusted. For
3120 // example, when compiling in Thumb mode on the ARM, the address
3121 // needs to have the low bit set.
3122 x
= build_call_expr(implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
],
3124 x
= fold_convert(TREE_TYPE(fnaddr
), x
);
3126 return build2(COMPOUND_EXPR
, TREE_TYPE(x
), ini
, x
);