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"
26 #ifndef ENABLE_BUILD_WITH_CXX
32 #include "expressions.h"
33 #include "statements.h"
38 // Whether we have seen any errors.
43 return errorcount != 0 || sorrycount != 0;
49 get_identifier_from_string(const std::string& str)
51 return get_identifier_with_length(str.data(), str.length());
56 static std::map<std::string, tree> builtin_functions;
58 // Define a builtin function. BCODE is the builtin function code
59 // defined by builtins.def. NAME is the name of the builtin function.
60 // LIBNAME is the name of the corresponding library function, and is
61 // NULL if there isn't one. FNTYPE is the type of the function.
62 // CONST_P is true if the function has the const attribute.
65 define_builtin(built_in_function bcode, const char* name, const char* libname,
66 tree fntype, bool const_p)
68 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
71 TREE_READONLY(decl) = 1;
72 set_builtin_decl(bcode, decl, true);
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_SYNC_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_SYNC_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_SYNC_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_SYNC_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_memcmp for struct comparisons.
120 define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
121 build_function_type_list(integer_type_node,
128 // We provide some functions for the math library.
129 tree math_function_type = build_function_type_list(double_type_node,
132 tree math_function_type_long =
133 build_function_type_list(long_double_type_node, long_double_type_node,
134 long_double_type_node, NULL_TREE);
135 tree math_function_type_two = build_function_type_list(double_type_node,
139 tree math_function_type_long_two =
140 build_function_type_list(long_double_type_node, long_double_type_node,
141 long_double_type_node, NULL_TREE);
142 define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
143 math_function_type, true);
144 define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
145 math_function_type_long, true);
146 define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
147 math_function_type, true);
148 define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
149 math_function_type_long, true);
150 define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
151 math_function_type, true);
152 define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
153 math_function_type_long, true);
154 define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
155 math_function_type_two, true);
156 define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
157 math_function_type_long_two, true);
158 define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
159 math_function_type, true);
160 define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
161 math_function_type_long, true);
162 define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
163 math_function_type, true);
164 define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
165 math_function_type_long, true);
166 define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
167 math_function_type, true);
168 define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
169 math_function_type_long, true);
170 define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
171 math_function_type, true);
172 define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
173 math_function_type_long, true);
174 define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
175 math_function_type, true);
176 define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
177 math_function_type_long, true);
178 define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
179 math_function_type, true);
180 define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
181 math_function_type_long, true);
182 define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
183 math_function_type_two, true);
184 define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
185 math_function_type_long_two, true);
186 define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
187 build_function_type_list(double_type_node,
192 define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
193 build_function_type_list(long_double_type_node,
194 long_double_type_node,
198 define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
199 math_function_type, true);
200 define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
201 math_function_type_long, true);
202 define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
203 math_function_type, true);
204 define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
205 math_function_type_long, true);
206 define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
207 math_function_type, true);
208 define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
209 math_function_type_long, true);
210 define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
211 math_function_type, true);
212 define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
213 math_function_type_long, true);
214 define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
215 math_function_type, true);
216 define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
217 math_function_type_long, true);
218 define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
219 math_function_type, true);
220 define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
221 math_function_type_long, true);
222 define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
223 math_function_type, true);
224 define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
225 math_function_type_long, true);
226 define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
227 math_function_type, true);
228 define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
229 math_function_type_long, true);
231 // We use __builtin_return_address in the thunk we build for
232 // functions which call recover.
233 define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", NULL,
234 build_function_type_list(ptr_type_node,
239 // The compiler uses __builtin_trap for some exception handling
241 define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
242 build_function_type(void_type_node, void_list_node),
246 // Get the name to use for the import control function. If there is a
247 // global function or variable, then we know that that name must be
248 // unique in the link, and we use it as the basis for our name.
251 Gogo::get_init_fn_name()
253 if (this->init_fn_name_.empty())
255 go_assert(this->package_ != NULL);
256 if (this->is_main_package())
258 // Use a name which the runtime knows.
259 this->init_fn_name_ = "__go_init_main";
263 std::string s = this->unique_prefix();
265 s.append(this->package_name());
266 s.append("..import");
267 this->init_fn_name_ = s;
271 return this->init_fn_name_;
274 // Add statements to INIT_STMT_LIST which run the initialization
275 // functions for imported packages. This is only used for the "main"
279 Gogo::init_imports(tree* init_stmt_list)
281 go_assert(this->is_main_package());
283 if (this->imported_init_fns_.empty())
286 tree fntype = build_function_type(void_type_node, void_list_node);
288 // We must call them in increasing priority order.
289 std::vector<Import_init> v;
290 for (std::set<Import_init>::const_iterator p =
291 this->imported_init_fns_.begin();
292 p != this->imported_init_fns_.end();
295 std::sort(v.begin(), v.end());
297 for (std::vector<Import_init>::const_iterator p = v.begin();
301 std::string user_name = p->package_name() + ".init";
302 tree decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
303 get_identifier_from_string(user_name),
305 const std::string& init_name(p->init_name());
306 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(init_name));
307 TREE_PUBLIC(decl) = 1;
308 DECL_EXTERNAL(decl) = 1;
309 append_to_statement_list(build_call_expr(decl, 0), init_stmt_list);
313 // Register global variables with the garbage collector. We need to
314 // register all variables which can hold a pointer value. They become
315 // roots during the mark phase. We build a struct that is easy to
316 // hook into a list of roots.
318 // struct __go_gc_root_list
320 // struct __go_gc_root_list* __next;
321 // struct __go_gc_root
328 // The last entry in the roots array has a NULL decl field.
331 Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
332 tree* init_stmt_list)
337 size_t count = var_gc.size();
339 tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
345 tree index_type = build_index_type(size_int(count));
346 tree array_type = build_array_type(root_type, index_type);
348 tree root_list_type = make_node(RECORD_TYPE);
349 root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
352 build_pointer_type(root_list_type),
356 // Build an initialier for the __roots array.
358 VEC(constructor_elt,gc)* roots_init = VEC_alloc(constructor_elt, gc,
362 for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
366 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
368 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
369 tree field = TYPE_FIELDS(root_type);
371 Bvariable* bvar = (*p)->get_backend_variable(this, NULL);
372 tree decl = var_to_tree(bvar);
373 go_assert(TREE_CODE(decl) == VAR_DECL);
374 elt->value = build_fold_addr_expr(decl);
376 elt = VEC_quick_push(constructor_elt, init, NULL);
377 field = DECL_CHAIN(field);
379 elt->value = DECL_SIZE_UNIT(decl);
381 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
382 elt->index = size_int(i);
383 elt->value = build_constructor(root_type, init);
386 // The list ends with a NULL entry.
388 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
390 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
391 tree field = TYPE_FIELDS(root_type);
393 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
395 elt = VEC_quick_push(constructor_elt, init, NULL);
396 field = DECL_CHAIN(field);
398 elt->value = size_zero_node;
400 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
401 elt->index = size_int(i);
402 elt->value = build_constructor(root_type, init);
404 // Build a constructor for the struct.
406 VEC(constructor_elt,gc*) root_list_init = VEC_alloc(constructor_elt, gc, 2);
408 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
409 field = TYPE_FIELDS(root_list_type);
411 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
413 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
414 field = DECL_CHAIN(field);
416 elt->value = build_constructor(array_type, roots_init);
418 // Build a decl to register.
420 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
421 create_tmp_var_name("gc"), root_list_type);
422 DECL_EXTERNAL(decl) = 0;
423 TREE_PUBLIC(decl) = 0;
424 TREE_STATIC(decl) = 1;
425 DECL_ARTIFICIAL(decl) = 1;
426 DECL_INITIAL(decl) = build_constructor(root_list_type, root_list_init);
427 rest_of_decl_compilation(decl, 1, 0);
429 static tree register_gc_fndecl;
430 tree call = Gogo::call_builtin(®ister_gc_fndecl,
431 Linemap::predeclared_location(),
432 "__go_register_gc_roots",
435 build_pointer_type(root_list_type),
436 build_fold_addr_expr(decl));
437 if (call != error_mark_node)
438 append_to_statement_list(call, init_stmt_list);
441 // Build the decl for the initialization function.
444 Gogo::initialization_function_decl()
446 // The tedious details of building your own function. There doesn't
447 // seem to be a helper function for this.
448 std::string name = this->package_name() + ".init";
449 tree fndecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL,
450 get_identifier_from_string(name),
451 build_function_type(void_type_node,
453 const std::string& asm_name(this->get_init_fn_name());
454 SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
456 tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
458 DECL_ARTIFICIAL(resdecl) = 1;
459 DECL_CONTEXT(resdecl) = fndecl;
460 DECL_RESULT(fndecl) = resdecl;
462 TREE_STATIC(fndecl) = 1;
463 TREE_USED(fndecl) = 1;
464 DECL_ARTIFICIAL(fndecl) = 1;
465 TREE_PUBLIC(fndecl) = 1;
467 DECL_INITIAL(fndecl) = make_node(BLOCK);
468 TREE_USED(DECL_INITIAL(fndecl)) = 1;
473 // Create the magic initialization function. INIT_STMT_LIST is the
474 // code that it needs to run.
477 Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
479 // Make sure that we thought we needed an initialization function,
480 // as otherwise we will not have reported it in the export data.
481 go_assert(this->is_main_package() || this->need_init_fn_);
483 if (fndecl == NULL_TREE)
484 fndecl = this->initialization_function_decl();
486 DECL_SAVED_TREE(fndecl) = init_stmt_list;
488 current_function_decl = fndecl;
489 if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
490 push_struct_function(fndecl);
492 push_cfun(DECL_STRUCT_FUNCTION(fndecl));
493 cfun->function_end_locus = BUILTINS_LOCATION;
495 gimplify_function_tree(fndecl);
497 cgraph_add_new_function(fndecl, false);
498 cgraph_mark_needed_node(cgraph_get_node(fndecl));
500 current_function_decl = NULL_TREE;
504 // Search for references to VAR in any statements or called functions.
506 class Find_var : public Traverse
509 // A hash table we use to avoid looping. The index is the name of a
510 // named object. We only look through objects defined in this
512 typedef Unordered_set(std::string) Seen_objects;
514 Find_var(Named_object* var, Seen_objects* seen_objects)
515 : Traverse(traverse_expressions),
516 var_(var), seen_objects_(seen_objects), found_(false)
519 // Whether the variable was found.
522 { return this->found_; }
525 expression(Expression**);
528 // The variable we are looking for.
530 // Names of objects we have already seen.
531 Seen_objects* seen_objects_;
532 // True if the variable was found.
536 // See if EXPR refers to VAR, looking through function calls and
537 // variable initializations.
540 Find_var::expression(Expression** pexpr)
542 Expression* e = *pexpr;
544 Var_expression* ve = e->var_expression();
547 Named_object* v = ve->named_object();
551 return TRAVERSE_EXIT;
554 if (v->is_variable() && v->package() == NULL)
556 Expression* init = v->var_value()->init();
559 std::pair<Seen_objects::iterator, bool> ins =
560 this->seen_objects_->insert(v->name());
563 // This is the first time we have seen this name.
564 if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
565 return TRAVERSE_EXIT;
571 // We traverse the code of any function we see. Note that this
572 // means that we will traverse the code of a function whose address
573 // is taken even if it is not called.
574 Func_expression* fe = e->func_expression();
577 const Named_object* f = fe->named_object();
578 if (f->is_function() && f->package() == NULL)
580 std::pair<Seen_objects::iterator, bool> ins =
581 this->seen_objects_->insert(f->name());
584 // This is the first time we have seen this name.
585 if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
586 return TRAVERSE_EXIT;
591 return TRAVERSE_CONTINUE;
594 // Return true if EXPR refers to VAR.
597 expression_requires(Expression* expr, Block* preinit, Named_object* var)
599 Find_var::Seen_objects seen_objects;
600 Find_var find_var(var, &seen_objects);
602 Expression::traverse(&expr, &find_var);
604 preinit->traverse(&find_var);
606 return find_var.found();
609 // Sort variable initializations. If the initialization expression
610 // for variable A refers directly or indirectly to the initialization
611 // expression for variable B, then we must initialize B before A.
617 : var_(NULL), init_(NULL_TREE), waiting_(0)
620 Var_init(Named_object* var, tree init)
621 : var_(var), init_(init), waiting_(0)
624 // Return the variable.
627 { return this->var_; }
629 // Return the initialization expression.
632 { return this->init_; }
634 // Return the number of variables waiting for this one to be
638 { return this->waiting_; }
640 // Increment the number waiting.
643 { ++this->waiting_; }
646 // The variable being initialized.
648 // The initialization expression to run.
650 // The number of variables which are waiting for this one.
654 typedef std::list<Var_init> Var_inits;
656 // Sort the variable initializations. The rule we follow is that we
657 // emit them in the order they appear in the array, except that if the
658 // initialization expression for a variable V1 depends upon another
659 // variable V2 then we initialize V1 after V2.
662 sort_var_inits(Var_inits* var_inits)
665 while (!var_inits->empty())
667 Var_inits::iterator p1 = var_inits->begin();
668 Named_object* var = p1->var();
669 Expression* init = var->var_value()->init();
670 Block* preinit = var->var_value()->preinit();
672 // Start walking through the list to see which variables VAR
673 // needs to wait for. We can skip P1->WAITING variables--that
674 // is the number we've already checked.
675 Var_inits::iterator p2 = p1;
677 for (size_t i = p1->waiting(); i > 0; --i)
680 for (; p2 != var_inits->end(); ++p2)
682 if (expression_requires(init, preinit, p2->var()))
685 if (expression_requires(p2->var()->var_value()->init(),
686 p2->var()->var_value()->preinit(),
689 error_at(var->location(),
690 ("initialization expressions for %qs and "
691 "%qs depend upon each other"),
692 var->message_name().c_str(),
693 p2->var()->message_name().c_str());
694 inform(p2->var()->location(), "%qs defined here",
695 p2->var()->message_name().c_str());
696 p2 = var_inits->end();
700 // We can't emit P1 until P2 is emitted. Move P1.
701 // Note that the WAITING loop always executes at
702 // least once, which is what we want.
703 p2->increment_waiting();
704 Var_inits::iterator p3 = p2;
705 for (size_t i = p2->waiting(); i > 0; --i)
707 var_inits->splice(p3, *var_inits, p1);
713 if (p2 == var_inits->end())
715 // VAR does not depends upon any other initialization expressions.
717 // Check for a loop of VAR on itself. We only do this if
718 // INIT is not NULL; when INIT is NULL, it means that
719 // PREINIT sets VAR, which we will interpret as a loop.
720 if (init != NULL && expression_requires(init, preinit, var))
721 error_at(var->location(),
722 "initialization expression for %qs depends upon itself",
723 var->message_name().c_str());
724 ready.splice(ready.end(), *var_inits, p1);
728 // Now READY is the list in the desired initialization order.
729 var_inits->swap(ready);
732 // Write out the global definitions.
735 Gogo::write_globals()
737 this->convert_named_types();
738 this->build_interface_method_tables();
740 Bindings* bindings = this->current_bindings();
741 size_t count_definitions = bindings->size_definitions();
742 size_t count = count_definitions;
744 tree* vec = new tree[count];
746 tree init_fndecl = NULL_TREE;
747 tree init_stmt_list = NULL_TREE;
749 if (this->is_main_package())
750 this->init_imports(&init_stmt_list);
752 // A list of variable initializations.
755 // A list of variables which need to be registered with the garbage
757 std::vector<Named_object*> var_gc;
758 var_gc.reserve(count);
760 tree var_init_stmt_list = NULL_TREE;
762 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
763 p != bindings->end_definitions();
766 Named_object* no = *p;
768 go_assert(!no->is_type_declaration() && !no->is_function_declaration());
769 // There is nothing to do for a package.
770 if (no->is_package())
777 // There is nothing to do for an object which was imported from
778 // a different package into the global scope.
779 if (no->package() != NULL)
786 // There is nothing useful we can output for constants which
787 // have ideal or non-integeral type.
790 Type* type = no->const_value()->type();
792 type = no->const_value()->expr()->type();
793 if (type->is_abstract() || type->integer_type() == NULL)
801 if (!no->is_variable())
803 vec[i] = no->get_tree(this, NULL);
804 if (vec[i] == error_mark_node)
806 go_assert(saw_errors());
814 Bvariable* var = no->get_backend_variable(this, NULL);
815 vec[i] = var_to_tree(var);
816 if (vec[i] == error_mark_node)
818 go_assert(saw_errors());
824 // Check for a sink variable, which may be used to run an
825 // initializer purely for its side effects.
826 bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
828 tree var_init_tree = NULL_TREE;
829 if (!no->var_value()->has_pre_init())
831 tree init = no->var_value()->get_init_tree(this, NULL);
832 if (init == error_mark_node)
833 go_assert(saw_errors());
834 else if (init == NULL_TREE)
836 else if (TREE_CONSTANT(init))
838 if (expression_requires(no->var_value()->init(), NULL, no))
839 error_at(no->location(),
840 "initialization expression for %qs depends "
842 no->message_name().c_str());
843 this->backend()->global_variable_set_init(var,
847 || int_size_in_bytes(TREE_TYPE(init)) == 0
848 || int_size_in_bytes(TREE_TYPE(vec[i])) == 0)
849 var_init_tree = init;
851 var_init_tree = fold_build2_loc(no->location().gcc_location(),
852 MODIFY_EXPR, void_type_node,
857 // We are going to create temporary variables which
858 // means that we need an fndecl.
859 if (init_fndecl == NULL_TREE)
860 init_fndecl = this->initialization_function_decl();
861 current_function_decl = init_fndecl;
862 if (DECL_STRUCT_FUNCTION(init_fndecl) == NULL)
863 push_struct_function(init_fndecl);
865 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
867 tree var_decl = is_sink ? NULL_TREE : vec[i];
868 var_init_tree = no->var_value()->get_init_block(this, NULL,
871 current_function_decl = NULL_TREE;
875 if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
877 if (no->var_value()->init() == NULL
878 && !no->var_value()->has_pre_init())
879 append_to_statement_list(var_init_tree, &var_init_stmt_list);
881 var_inits.push_back(Var_init(no, var_init_tree));
884 if (!is_sink && no->var_value()->type()->has_pointer())
885 var_gc.push_back(no);
889 // Register global variables with the garbage collector.
890 this->register_gc_vars(var_gc, &init_stmt_list);
892 // Simple variable initializations, after all variables are
894 append_to_statement_list(var_init_stmt_list, &init_stmt_list);
896 // Complex variable initializations, first sorting them into a
898 if (!var_inits.empty())
900 sort_var_inits(&var_inits);
901 for (Var_inits::const_iterator p = var_inits.begin();
902 p != var_inits.end();
904 append_to_statement_list(p->init(), &init_stmt_list);
907 // After all the variables are initialized, call the "init"
908 // functions if there are any.
909 for (std::vector<Named_object*>::const_iterator p =
910 this->init_functions_.begin();
911 p != this->init_functions_.end();
914 tree decl = (*p)->get_tree(this, NULL);
915 tree call = build_call_expr(decl, 0);
916 append_to_statement_list(call, &init_stmt_list);
919 // Set up a magic function to do all the initialization actions.
920 // This will be called if this package is imported.
921 if (init_stmt_list != NULL_TREE
922 || this->need_init_fn_
923 || this->is_main_package())
924 this->write_initialization_function(init_fndecl, init_stmt_list);
926 // We should not have seen any new bindings created during the
928 go_assert(count_definitions == this->current_bindings()->size_definitions());
930 // Pass everything back to the middle-end.
932 wrapup_global_declarations(vec, count);
934 cgraph_finalize_compilation_unit();
936 check_global_declarations(vec, count);
937 emit_debug_global_declarations(vec, count);
942 // Get a tree for the identifier for a named object.
945 Named_object::get_id(Gogo* gogo)
947 go_assert(!this->is_variable() && !this->is_result_variable());
948 std::string decl_name;
949 if (this->is_function_declaration()
950 && !this->func_declaration_value()->asm_name().empty())
951 decl_name = this->func_declaration_value()->asm_name();
952 else if (this->is_type()
953 && Linemap::is_predeclared_location(this->type_value()->location()))
955 // We don't need the package name for builtin types.
956 decl_name = Gogo::unpack_hidden_name(this->name_);
960 std::string package_name;
961 if (this->package_ == NULL)
962 package_name = gogo->package_name();
964 package_name = this->package_->name();
966 decl_name = package_name + '.' + Gogo::unpack_hidden_name(this->name_);
968 Function_type* fntype;
969 if (this->is_function())
970 fntype = this->func_value()->type();
971 else if (this->is_function_declaration())
972 fntype = this->func_declaration_value()->type();
975 if (fntype != NULL && fntype->is_method())
977 decl_name.push_back('.');
978 decl_name.append(fntype->receiver()->type()->mangled_name(gogo));
983 const Named_object* in_function = this->type_value()->in_function();
984 if (in_function != NULL)
985 decl_name += '$' + in_function->name();
987 return get_identifier_from_string(decl_name);
990 // Get a tree for a named object.
993 Named_object::get_tree(Gogo* gogo, Named_object* function)
995 if (this->tree_ != NULL_TREE)
999 if (this->classification_ == NAMED_OBJECT_TYPE)
1002 name = this->get_id(gogo);
1004 switch (this->classification_)
1006 case NAMED_OBJECT_CONST:
1008 Named_constant* named_constant = this->u_.const_value;
1009 Translate_context subcontext(gogo, function, NULL, NULL);
1010 tree expr_tree = named_constant->expr()->get_tree(&subcontext);
1011 if (expr_tree == error_mark_node)
1012 decl = error_mark_node;
1015 Type* type = named_constant->type();
1016 if (type != NULL && !type->is_abstract())
1018 if (type->is_error())
1019 expr_tree = error_mark_node;
1022 Btype* btype = type->get_backend(gogo);
1023 expr_tree = fold_convert(type_to_tree(btype), expr_tree);
1026 if (expr_tree == error_mark_node)
1027 decl = error_mark_node;
1028 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
1030 decl = build_decl(named_constant->location().gcc_location(),
1031 CONST_DECL, name, TREE_TYPE(expr_tree));
1032 DECL_INITIAL(decl) = expr_tree;
1033 TREE_CONSTANT(decl) = 1;
1034 TREE_READONLY(decl) = 1;
1038 // A CONST_DECL is only for an enum constant, so we
1039 // shouldn't use for non-integral types. Instead we
1040 // just return the constant itself, rather than a
1048 case NAMED_OBJECT_TYPE:
1050 Named_type* named_type = this->u_.type_value;
1051 tree type_tree = type_to_tree(named_type->get_backend(gogo));
1052 if (type_tree == error_mark_node)
1053 decl = error_mark_node;
1056 decl = TYPE_NAME(type_tree);
1057 go_assert(decl != NULL_TREE);
1059 // We need to produce a type descriptor for every named
1060 // type, and for a pointer to every named type, since
1061 // other files or packages might refer to them. We need
1062 // to do this even for hidden types, because they might
1063 // still be returned by some function. Simply calling the
1064 // type_descriptor method is enough to create the type
1065 // descriptor, even though we don't do anything with it.
1066 if (this->package_ == NULL)
1069 type_descriptor_pointer(gogo,
1070 Linemap::predeclared_location());
1071 Type* pn = Type::make_pointer_type(named_type);
1072 pn->type_descriptor_pointer(gogo,
1073 Linemap::predeclared_location());
1079 case NAMED_OBJECT_TYPE_DECLARATION:
1080 error("reference to undefined type %qs",
1081 this->message_name().c_str());
1082 return error_mark_node;
1084 case NAMED_OBJECT_VAR:
1085 case NAMED_OBJECT_RESULT_VAR:
1086 case NAMED_OBJECT_SINK:
1089 case NAMED_OBJECT_FUNC:
1091 Function* func = this->u_.func_value;
1092 decl = func->get_or_make_decl(gogo, this, name);
1093 if (decl != error_mark_node)
1095 if (func->block() != NULL)
1097 if (DECL_STRUCT_FUNCTION(decl) == NULL)
1098 push_struct_function(decl);
1100 push_cfun(DECL_STRUCT_FUNCTION(decl));
1102 cfun->function_end_locus =
1103 func->block()->end_location().gcc_location();
1105 current_function_decl = decl;
1107 func->build_tree(gogo, this);
1109 gimplify_function_tree(decl);
1111 cgraph_finalize_function(decl, true);
1113 current_function_decl = NULL_TREE;
1120 case NAMED_OBJECT_ERRONEOUS:
1121 decl = error_mark_node;
1128 if (TREE_TYPE(decl) == error_mark_node)
1129 decl = error_mark_node;
1135 if (ret != error_mark_node)
1136 go_preserve_from_gc(ret);
1141 // Get the initial value of a variable as a tree. This does not
1142 // consider whether the variable is in the heap--it returns the
1143 // initial value as though it were always stored in the stack.
1146 Variable::get_init_tree(Gogo* gogo, Named_object* function)
1148 go_assert(this->preinit_ == NULL);
1149 if (this->init_ == NULL)
1151 go_assert(!this->is_parameter_);
1152 if (this->is_global_ || this->is_in_heap())
1154 Btype* btype = this->type_->get_backend(gogo);
1155 return expr_to_tree(gogo->backend()->zero_expression(btype));
1159 Translate_context context(gogo, function, NULL, NULL);
1160 tree rhs_tree = this->init_->get_tree(&context);
1161 return Expression::convert_for_assignment(&context, this->type(),
1162 this->init_->type(),
1163 rhs_tree, this->location());
1167 // Get the initial value of a variable when a block is required.
1168 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1171 Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
1173 go_assert(this->preinit_ != NULL);
1175 // We want to add the variable assignment to the end of the preinit
1176 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1177 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1178 // regular statements.
1180 Translate_context context(gogo, function, NULL, NULL);
1181 Bblock* bblock = this->preinit_->get_backend(&context);
1182 tree block_tree = block_to_tree(bblock);
1183 if (block_tree == error_mark_node)
1184 return error_mark_node;
1185 go_assert(TREE_CODE(block_tree) == BIND_EXPR);
1186 tree statements = BIND_EXPR_BODY(block_tree);
1187 while (statements != NULL_TREE
1188 && (TREE_CODE(statements) == TRY_FINALLY_EXPR
1189 || TREE_CODE(statements) == TRY_CATCH_EXPR))
1190 statements = TREE_OPERAND(statements, 0);
1192 // It's possible to have pre-init statements without an initializer
1193 // if the pre-init statements set the variable.
1194 if (this->init_ != NULL)
1196 tree rhs_tree = this->init_->get_tree(&context);
1197 if (rhs_tree == error_mark_node)
1198 return error_mark_node;
1199 if (var_decl == NULL_TREE)
1200 append_to_statement_list(rhs_tree, &statements);
1203 tree val = Expression::convert_for_assignment(&context, this->type(),
1204 this->init_->type(),
1207 if (val == error_mark_node)
1208 return error_mark_node;
1209 tree set = fold_build2_loc(this->location().gcc_location(),
1210 MODIFY_EXPR, void_type_node, var_decl,
1212 append_to_statement_list(set, &statements);
1219 // Get a tree for a function decl.
1222 Function::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1224 if (this->fndecl_ == NULL_TREE)
1226 tree functype = type_to_tree(this->type_->get_backend(gogo));
1227 if (functype == error_mark_node)
1228 this->fndecl_ = error_mark_node;
1231 // The type of a function comes back as a pointer, but we
1232 // want the real function type for a function declaration.
1233 go_assert(POINTER_TYPE_P(functype));
1234 functype = TREE_TYPE(functype);
1235 tree decl = build_decl(this->location().gcc_location(), FUNCTION_DECL,
1238 this->fndecl_ = decl;
1240 if (no->package() != NULL)
1242 else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
1244 else if (Gogo::unpack_hidden_name(no->name()) == "init"
1245 && !this->type_->is_method())
1247 else if (Gogo::unpack_hidden_name(no->name()) == "main"
1248 && gogo->is_main_package())
1249 TREE_PUBLIC(decl) = 1;
1250 // Methods have to be public even if they are hidden because
1251 // they can be pulled into type descriptors when using
1252 // anonymous fields.
1253 else if (!Gogo::is_hidden_name(no->name())
1254 || this->type_->is_method())
1256 TREE_PUBLIC(decl) = 1;
1257 std::string asm_name = gogo->unique_prefix();
1258 asm_name.append(1, '.');
1259 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1260 SET_DECL_ASSEMBLER_NAME(decl,
1261 get_identifier_from_string(asm_name));
1264 // Why do we have to do this in the frontend?
1265 tree restype = TREE_TYPE(functype);
1267 build_decl(this->location().gcc_location(), RESULT_DECL, NULL_TREE,
1269 DECL_ARTIFICIAL(resdecl) = 1;
1270 DECL_IGNORED_P(resdecl) = 1;
1271 DECL_CONTEXT(resdecl) = decl;
1272 DECL_RESULT(decl) = resdecl;
1274 if (this->enclosing_ != NULL)
1275 DECL_STATIC_CHAIN(decl) = 1;
1277 // If a function calls the predeclared recover function, we
1278 // can't inline it, because recover behaves differently in a
1279 // function passed directly to defer. If this is a recover
1280 // thunk that we built to test whether a function can be
1281 // recovered, we can't inline it, because that will mess up
1282 // our return address comparison.
1283 if (this->calls_recover_ || this->is_recover_thunk_)
1284 DECL_UNINLINABLE(decl) = 1;
1286 // If this is a thunk created to call a function which calls
1287 // the predeclared recover function, we need to disable
1288 // stack splitting for the thunk.
1289 if (this->is_recover_thunk_)
1291 tree attr = get_identifier("__no_split_stack__");
1292 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1295 go_preserve_from_gc(decl);
1297 if (this->closure_var_ != NULL)
1299 push_struct_function(decl);
1301 Bvariable* bvar = this->closure_var_->get_backend_variable(gogo,
1303 tree closure_decl = var_to_tree(bvar);
1304 if (closure_decl == error_mark_node)
1305 this->fndecl_ = error_mark_node;
1308 DECL_ARTIFICIAL(closure_decl) = 1;
1309 DECL_IGNORED_P(closure_decl) = 1;
1310 TREE_USED(closure_decl) = 1;
1311 DECL_ARG_TYPE(closure_decl) = TREE_TYPE(closure_decl);
1312 TREE_READONLY(closure_decl) = 1;
1314 DECL_STRUCT_FUNCTION(decl)->static_chain_decl = closure_decl;
1321 return this->fndecl_;
1324 // Get a tree for a function declaration.
1327 Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1329 if (this->fndecl_ == NULL_TREE)
1331 // Let Go code use an asm declaration to pick up a builtin
1333 if (!this->asm_name_.empty())
1335 std::map<std::string, tree>::const_iterator p =
1336 builtin_functions.find(this->asm_name_);
1337 if (p != builtin_functions.end())
1339 this->fndecl_ = p->second;
1340 return this->fndecl_;
1344 tree functype = type_to_tree(this->fntype_->get_backend(gogo));
1346 if (functype == error_mark_node)
1347 decl = error_mark_node;
1350 // The type of a function comes back as a pointer, but we
1351 // want the real function type for a function declaration.
1352 go_assert(POINTER_TYPE_P(functype));
1353 functype = TREE_TYPE(functype);
1354 decl = build_decl(this->location().gcc_location(), FUNCTION_DECL, id,
1356 TREE_PUBLIC(decl) = 1;
1357 DECL_EXTERNAL(decl) = 1;
1359 if (this->asm_name_.empty())
1361 std::string asm_name = (no->package() == NULL
1362 ? gogo->unique_prefix()
1363 : no->package()->unique_prefix());
1364 asm_name.append(1, '.');
1365 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1366 SET_DECL_ASSEMBLER_NAME(decl,
1367 get_identifier_from_string(asm_name));
1370 this->fndecl_ = decl;
1371 go_preserve_from_gc(decl);
1373 return this->fndecl_;
1376 // We always pass the receiver to a method as a pointer. If the
1377 // receiver is actually declared as a non-pointer type, then we copy
1378 // the value into a local variable, so that it has the right type. In
1379 // this function we create the real PARM_DECL to use, and set
1380 // DEC_INITIAL of the var_decl to be the value passed in.
1383 Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
1385 if (var_decl == error_mark_node)
1386 return error_mark_node;
1387 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1388 tree val_type = TREE_TYPE(var_decl);
1389 bool is_in_heap = no->var_value()->is_in_heap();
1392 go_assert(POINTER_TYPE_P(val_type));
1393 val_type = TREE_TYPE(val_type);
1396 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1397 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1399 tree id = get_identifier_from_string(name);
1400 tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
1401 DECL_CONTEXT(parm_decl) = current_function_decl;
1402 DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
1404 go_assert(DECL_INITIAL(var_decl) == NULL_TREE);
1405 tree init = build_fold_indirect_ref_loc(loc, parm_decl);
1409 tree size = TYPE_SIZE_UNIT(val_type);
1410 tree space = gogo->allocate_memory(no->var_value()->type(), size,
1412 space = save_expr(space);
1413 space = fold_convert(build_pointer_type(val_type), space);
1414 tree spaceref = build_fold_indirect_ref_loc(no->location().gcc_location(),
1416 TREE_THIS_NOTRAP(spaceref) = 1;
1417 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1419 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space), set, space);
1422 DECL_INITIAL(var_decl) = init;
1427 // If we take the address of a parameter, then we need to copy it into
1428 // the heap. We will access it as a local variable via an
1432 Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree var_decl)
1434 if (var_decl == error_mark_node)
1435 return error_mark_node;
1436 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1437 Location loc(DECL_SOURCE_LOCATION(var_decl));
1439 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1441 tree id = get_identifier_from_string(name);
1443 tree type = TREE_TYPE(var_decl);
1444 go_assert(POINTER_TYPE_P(type));
1445 type = TREE_TYPE(type);
1447 tree parm_decl = build_decl(loc.gcc_location(), PARM_DECL, id, type);
1448 DECL_CONTEXT(parm_decl) = current_function_decl;
1449 DECL_ARG_TYPE(parm_decl) = type;
1451 tree size = TYPE_SIZE_UNIT(type);
1452 tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
1453 space = save_expr(space);
1454 space = fold_convert(TREE_TYPE(var_decl), space);
1455 tree spaceref = build_fold_indirect_ref_loc(loc.gcc_location(), space);
1456 TREE_THIS_NOTRAP(spaceref) = 1;
1457 tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
1458 build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
1460 DECL_INITIAL(var_decl) = init;
1465 // Get a tree for function code.
1468 Function::build_tree(Gogo* gogo, Named_object* named_function)
1470 tree fndecl = this->fndecl_;
1471 go_assert(fndecl != NULL_TREE);
1473 tree params = NULL_TREE;
1476 tree declare_vars = NULL_TREE;
1477 for (Bindings::const_definitions_iterator p =
1478 this->block_->bindings()->begin_definitions();
1479 p != this->block_->bindings()->end_definitions();
1482 if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
1484 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1485 *pp = var_to_tree(bvar);
1487 // We always pass the receiver to a method as a pointer. If
1488 // the receiver is declared as a non-pointer type, then we
1489 // copy the value into a local variable.
1490 if ((*p)->var_value()->is_receiver()
1491 && (*p)->var_value()->type()->points_to() == NULL)
1493 tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
1495 if (var != error_mark_node)
1497 go_assert(TREE_CODE(var) == VAR_DECL);
1498 DECL_CHAIN(var) = declare_vars;
1503 else if ((*p)->var_value()->is_in_heap())
1505 // If we take the address of a parameter, then we need
1506 // to copy it into the heap.
1507 tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
1509 if (var != error_mark_node)
1511 go_assert(TREE_CODE(var) == VAR_DECL);
1512 DECL_CHAIN(var) = declare_vars;
1518 if (*pp != error_mark_node)
1520 go_assert(TREE_CODE(*pp) == PARM_DECL);
1521 pp = &DECL_CHAIN(*pp);
1524 else if ((*p)->is_result_variable())
1526 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1527 tree var_decl = var_to_tree(bvar);
1529 Type* type = (*p)->result_var_value()->type();
1531 if (!(*p)->result_var_value()->is_in_heap())
1533 Btype* btype = type->get_backend(gogo);
1534 init = expr_to_tree(gogo->backend()->zero_expression(btype));
1538 Location loc = (*p)->location();
1539 tree type_tree = type_to_tree(type->get_backend(gogo));
1540 tree space = gogo->allocate_memory(type,
1541 TYPE_SIZE_UNIT(type_tree),
1543 tree ptr_type_tree = build_pointer_type(type_tree);
1544 init = fold_convert_loc(loc.gcc_location(), ptr_type_tree, space);
1547 if (var_decl != error_mark_node)
1549 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1550 DECL_INITIAL(var_decl) = init;
1551 DECL_CHAIN(var_decl) = declare_vars;
1552 declare_vars = var_decl;
1558 DECL_ARGUMENTS(fndecl) = params;
1560 if (this->block_ != NULL)
1562 go_assert(DECL_INITIAL(fndecl) == NULL_TREE);
1564 // Declare variables if necessary.
1565 tree bind = NULL_TREE;
1566 tree defer_init = NULL_TREE;
1567 if (declare_vars != NULL_TREE || this->defer_stack_ != NULL)
1569 tree block = make_node(BLOCK);
1570 BLOCK_SUPERCONTEXT(block) = fndecl;
1571 DECL_INITIAL(fndecl) = block;
1572 BLOCK_VARS(block) = declare_vars;
1573 TREE_USED(block) = 1;
1575 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
1577 TREE_SIDE_EFFECTS(bind) = 1;
1579 if (this->defer_stack_ != NULL)
1581 Translate_context dcontext(gogo, named_function, this->block_,
1582 tree_to_block(bind));
1583 Bstatement* bdi = this->defer_stack_->get_backend(&dcontext);
1584 defer_init = stat_to_tree(bdi);
1588 // Build the trees for all the statements in the function.
1589 Translate_context context(gogo, named_function, NULL, NULL);
1590 Bblock* bblock = this->block_->get_backend(&context);
1591 tree code = block_to_tree(bblock);
1593 tree init = NULL_TREE;
1594 tree except = NULL_TREE;
1595 tree fini = NULL_TREE;
1597 // Initialize variables if necessary.
1598 for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
1600 tree dv = build1(DECL_EXPR, void_type_node, v);
1601 SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
1602 append_to_statement_list(dv, &init);
1605 // If we have a defer stack, initialize it at the start of a
1607 if (defer_init != NULL_TREE && defer_init != error_mark_node)
1609 SET_EXPR_LOCATION(defer_init,
1610 this->block_->start_location().gcc_location());
1611 append_to_statement_list(defer_init, &init);
1613 // Clean up the defer stack when we leave the function.
1614 this->build_defer_wrapper(gogo, named_function, &except, &fini);
1617 if (code != NULL_TREE && code != error_mark_node)
1619 if (init != NULL_TREE)
1620 code = build2(COMPOUND_EXPR, void_type_node, init, code);
1621 if (except != NULL_TREE)
1622 code = build2(TRY_CATCH_EXPR, void_type_node, code,
1623 build2(CATCH_EXPR, void_type_node, NULL, except));
1624 if (fini != NULL_TREE)
1625 code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
1628 // Stick the code into the block we built for the receiver, if
1630 if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
1632 BIND_EXPR_BODY(bind) = code;
1636 DECL_SAVED_TREE(fndecl) = code;
1640 // Build the wrappers around function code needed if the function has
1641 // any defer statements. This sets *EXCEPT to an exception handler
1642 // and *FINI to a finally handler.
1645 Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
1646 tree *except, tree *fini)
1648 Location end_loc = this->block_->end_location();
1650 // Add an exception handler. This is used if a panic occurs. Its
1651 // purpose is to stop the stack unwinding if a deferred function
1652 // calls recover. There are more details in
1653 // libgo/runtime/go-unwind.c.
1655 tree stmt_list = NULL_TREE;
1657 Expression* call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1658 this->defer_stack(end_loc));
1659 Translate_context context(gogo, named_function, NULL, NULL);
1660 tree call_tree = call->get_tree(&context);
1661 if (call_tree != error_mark_node)
1662 append_to_statement_list(call_tree, &stmt_list);
1664 tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
1666 if (retval == NULL_TREE)
1669 set = fold_build2_loc(end_loc.gcc_location(), MODIFY_EXPR, void_type_node,
1670 DECL_RESULT(this->fndecl_), retval);
1671 tree ret_stmt = fold_build1_loc(end_loc.gcc_location(), RETURN_EXPR,
1672 void_type_node, set);
1673 append_to_statement_list(ret_stmt, &stmt_list);
1675 go_assert(*except == NULL_TREE);
1676 *except = stmt_list;
1678 // Add some finally code to run the defer functions. This is used
1679 // both in the normal case, when no panic occurs, and also if a
1680 // panic occurs to run any further defer functions. Of course, it
1681 // is possible for a defer function to call panic which should be
1682 // caught by another defer function. To handle that we use a loop.
1684 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1685 // if (return values are named) return named_vals;
1689 tree label = create_artificial_label(end_loc.gcc_location());
1690 tree define_label = fold_build1_loc(end_loc.gcc_location(), LABEL_EXPR,
1691 void_type_node, label);
1692 append_to_statement_list(define_label, &stmt_list);
1694 call = Runtime::make_call(Runtime::UNDEFER, end_loc, 1,
1695 this->defer_stack(end_loc));
1696 tree undefer = call->get_tree(&context);
1698 call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1699 this->defer_stack(end_loc));
1700 tree defer = call->get_tree(&context);
1702 if (undefer == error_mark_node || defer == error_mark_node)
1705 tree jump = fold_build1_loc(end_loc.gcc_location(), GOTO_EXPR, void_type_node,
1707 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
1708 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
1709 tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
1711 append_to_statement_list(try_catch, &stmt_list);
1713 if (this->type_->results() != NULL
1714 && !this->type_->results()->empty()
1715 && !this->type_->results()->front().name().empty())
1717 // If the result variables are named, and we are returning from
1718 // this function rather than panicing through it, we need to
1719 // return them again, because they might have been changed by a
1720 // defer function. The runtime routines set the defer_stack
1721 // variable to true if we are returning from this function.
1722 retval = this->return_value(gogo, named_function, end_loc,
1724 set = fold_build2_loc(end_loc.gcc_location(), MODIFY_EXPR, void_type_node,
1725 DECL_RESULT(this->fndecl_), retval);
1726 ret_stmt = fold_build1_loc(end_loc.gcc_location(), RETURN_EXPR,
1727 void_type_node, set);
1730 Expression::make_temporary_reference(this->defer_stack_, end_loc);
1731 tree tref = ref->get_tree(&context);
1732 tree s = build3_loc(end_loc.gcc_location(), COND_EXPR, void_type_node,
1733 tref, ret_stmt, NULL_TREE);
1735 append_to_statement_list(s, &stmt_list);
1739 go_assert(*fini == NULL_TREE);
1743 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1744 // also add statements to STMT_LIST, which need to be executed before
1745 // the assignment. This is used for a return statement with no
1749 Function::return_value(Gogo* gogo, Named_object* named_function,
1750 Location location, tree* stmt_list) const
1752 const Typed_identifier_list* results = this->type_->results();
1753 if (results == NULL || results->empty())
1756 go_assert(this->results_ != NULL);
1757 if (this->results_->size() != results->size())
1759 go_assert(saw_errors());
1760 return error_mark_node;
1764 if (results->size() == 1)
1767 this->results_->front()->get_backend_variable(gogo,
1769 tree ret = var_to_tree(bvar);
1770 if (this->results_->front()->result_var_value()->is_in_heap())
1771 ret = build_fold_indirect_ref_loc(location.gcc_location(), ret);
1776 tree rettype = TREE_TYPE(DECL_RESULT(this->fndecl_));
1777 retval = create_tmp_var(rettype, "RESULT");
1778 tree field = TYPE_FIELDS(rettype);
1780 for (Typed_identifier_list::const_iterator pr = results->begin();
1781 pr != results->end();
1782 ++pr, ++index, field = DECL_CHAIN(field))
1784 go_assert(field != NULL);
1785 Named_object* no = (*this->results_)[index];
1786 Bvariable* bvar = no->get_backend_variable(gogo, named_function);
1787 tree val = var_to_tree(bvar);
1788 if (no->result_var_value()->is_in_heap())
1789 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
1790 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1792 build3(COMPONENT_REF, TREE_TYPE(field),
1793 retval, field, NULL_TREE),
1795 append_to_statement_list(set, stmt_list);
1801 // Return the integer type to use for a size.
1805 go_type_for_size(unsigned int bits, int unsignedp)
1811 name = unsignedp ? "uint8" : "int8";
1814 name = unsignedp ? "uint16" : "int16";
1817 name = unsignedp ? "uint32" : "int32";
1820 name = unsignedp ? "uint64" : "int64";
1823 if (bits == POINTER_SIZE && unsignedp)
1828 Type* type = Type::lookup_integer_type(name);
1829 return type_to_tree(type->get_backend(go_get_gogo()));
1832 // Return the type to use for a mode.
1836 go_type_for_mode(enum machine_mode mode, int unsignedp)
1838 // FIXME: This static_cast should be in machmode.h.
1839 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
1841 return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
1842 else if (mc == MODE_FLOAT)
1845 switch (GET_MODE_BITSIZE (mode))
1848 type = Type::lookup_float_type("float32");
1851 type = Type::lookup_float_type("float64");
1854 // We have to check for long double in order to support
1855 // i386 excess precision.
1856 if (mode == TYPE_MODE(long_double_type_node))
1857 return long_double_type_node;
1860 return type_to_tree(type->get_backend(go_get_gogo()));
1862 else if (mc == MODE_COMPLEX_FLOAT)
1865 switch (GET_MODE_BITSIZE (mode))
1868 type = Type::lookup_complex_type("complex64");
1871 type = Type::lookup_complex_type("complex128");
1874 // We have to check for long double in order to support
1875 // i386 excess precision.
1876 if (mode == TYPE_MODE(complex_long_double_type_node))
1877 return complex_long_double_type_node;
1880 return type_to_tree(type->get_backend(go_get_gogo()));
1886 // Return a tree which allocates SIZE bytes which will holds value of
1890 Gogo::allocate_memory(Type* type, tree size, Location location)
1892 // If the package imports unsafe, then it may play games with
1893 // pointers that look like integers.
1894 if (this->imported_unsafe_ || type->has_pointer())
1896 static tree new_fndecl;
1897 return Gogo::call_builtin(&new_fndecl,
1907 static tree new_nopointers_fndecl;
1908 return Gogo::call_builtin(&new_nopointers_fndecl,
1910 "__go_new_nopointers",
1918 // Build a builtin struct with a list of fields. The name is
1919 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
1920 // node; this exists so that the struct can have fields which point to
1921 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
1922 // are NFIELDS fields. Each field is a name (a const char*) followed
1923 // by a type (a tree).
1926 Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
1929 if (ptype != NULL && *ptype != NULL_TREE)
1933 va_start(ap, nfields);
1935 tree fields = NULL_TREE;
1936 for (int i = 0; i < nfields; ++i)
1938 const char* field_name = va_arg(ap, const char*);
1939 tree type = va_arg(ap, tree);
1940 if (type == error_mark_node)
1943 *ptype = error_mark_node;
1944 return error_mark_node;
1946 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
1947 get_identifier(field_name), type);
1948 DECL_CHAIN(field) = fields;
1954 if (struct_type == NULL_TREE)
1955 struct_type = make_node(RECORD_TYPE);
1956 finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
1960 go_preserve_from_gc(struct_type);
1961 *ptype = struct_type;
1967 // Return a type to use for pointer to const char for a string.
1970 Gogo::const_char_pointer_type_tree()
1973 if (type == NULL_TREE)
1975 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1977 type = build_pointer_type(const_char_type);
1978 go_preserve_from_gc(type);
1983 // Return a tree for a string constant.
1986 Gogo::string_constant_tree(const std::string& val)
1988 tree index_type = build_index_type(size_int(val.length()));
1989 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1991 tree string_type = build_array_type(const_char_type, index_type);
1992 string_type = build_variant_type_copy(string_type);
1993 TYPE_STRING_FLAG(string_type) = 1;
1994 tree string_val = build_string(val.length(), val.data());
1995 TREE_TYPE(string_val) = string_type;
1999 // Return a tree for a Go string constant.
2002 Gogo::go_string_constant_tree(const std::string& val)
2004 tree string_type = type_to_tree(Type::make_string_type()->get_backend(this));
2006 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2008 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2009 tree field = TYPE_FIELDS(string_type);
2010 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
2012 tree str = Gogo::string_constant_tree(val);
2013 elt->value = fold_convert(TREE_TYPE(field),
2014 build_fold_addr_expr(str));
2016 elt = VEC_quick_push(constructor_elt, init, NULL);
2017 field = DECL_CHAIN(field);
2018 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
2020 elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
2022 tree constructor = build_constructor(string_type, init);
2023 TREE_READONLY(constructor) = 1;
2024 TREE_CONSTANT(constructor) = 1;
2029 // Return a tree for a pointer to a Go string constant. This is only
2030 // used for type descriptors, so we return a pointer to a constant
2034 Gogo::ptr_go_string_constant_tree(const std::string& val)
2036 tree pval = this->go_string_constant_tree(val);
2038 tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
2039 create_tmp_var_name("SP"), TREE_TYPE(pval));
2040 DECL_EXTERNAL(decl) = 0;
2041 TREE_PUBLIC(decl) = 0;
2042 TREE_USED(decl) = 1;
2043 TREE_READONLY(decl) = 1;
2044 TREE_CONSTANT(decl) = 1;
2045 TREE_STATIC(decl) = 1;
2046 DECL_ARTIFICIAL(decl) = 1;
2047 DECL_INITIAL(decl) = pval;
2048 rest_of_decl_compilation(decl, 1, 0);
2050 return build_fold_addr_expr(decl);
2053 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2054 // the slice. VALUES is the value pointer and COUNT is the number of
2055 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2056 // the capacity and the count are the same.
2059 Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
2062 go_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
2064 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
2066 tree field = TYPE_FIELDS(slice_type_tree);
2067 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
2068 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2070 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
2071 == TYPE_MAIN_VARIANT(TREE_TYPE(values)));
2072 elt->value = values;
2074 count = fold_convert(sizetype, count);
2075 if (capacity == NULL_TREE)
2077 count = save_expr(count);
2081 field = DECL_CHAIN(field);
2082 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
2083 elt = VEC_quick_push(constructor_elt, init, NULL);
2085 elt->value = fold_convert(TREE_TYPE(field), count);
2087 field = DECL_CHAIN(field);
2088 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
2089 elt = VEC_quick_push(constructor_elt, init, NULL);
2091 elt->value = fold_convert(TREE_TYPE(field), capacity);
2093 return build_constructor(slice_type_tree, init);
2096 // Build an interface method table for a type: a list of function
2097 // pointers, one for each interface method. This is used for
2101 Gogo::interface_method_table_for_type(const Interface_type* interface,
2105 const Typed_identifier_list* interface_methods = interface->methods();
2106 go_assert(!interface_methods->empty());
2108 std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
2109 + interface->mangled_name(this)
2111 + type->mangled_name(this));
2113 tree id = get_identifier_from_string(mangled_name);
2115 // See whether this interface has any hidden methods.
2116 bool has_hidden_methods = false;
2117 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2118 p != interface_methods->end();
2121 if (Gogo::is_hidden_name(p->name()))
2123 has_hidden_methods = true;
2128 // We already know that the named type is convertible to the
2129 // interface. If the interface has hidden methods, and the named
2130 // type is defined in a different package, then the interface
2131 // conversion table will be defined by that other package.
2132 if (has_hidden_methods && type->named_object()->package() != NULL)
2134 tree array_type = build_array_type(const_ptr_type_node, NULL);
2135 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2136 TREE_READONLY(decl) = 1;
2137 TREE_CONSTANT(decl) = 1;
2138 TREE_PUBLIC(decl) = 1;
2139 DECL_EXTERNAL(decl) = 1;
2140 go_preserve_from_gc(decl);
2144 size_t count = interface_methods->size();
2145 VEC(constructor_elt, gc)* pointers = VEC_alloc(constructor_elt, gc,
2148 // The first element is the type descriptor.
2149 constructor_elt* elt = VEC_quick_push(constructor_elt, pointers, NULL);
2150 elt->index = size_zero_node;
2155 td_type = Type::make_pointer_type(type);
2156 tree tdp = td_type->type_descriptor_pointer(this,
2157 Linemap::predeclared_location());
2158 elt->value = fold_convert(const_ptr_type_node, tdp);
2161 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2162 p != interface_methods->end();
2166 Method* m = type->method_function(p->name(), &is_ambiguous);
2167 go_assert(m != NULL);
2169 Named_object* no = m->named_object();
2171 tree fnid = no->get_id(this);
2174 if (no->is_function())
2175 fndecl = no->func_value()->get_or_make_decl(this, no, fnid);
2176 else if (no->is_function_declaration())
2177 fndecl = no->func_declaration_value()->get_or_make_decl(this, no,
2181 fndecl = build_fold_addr_expr(fndecl);
2183 elt = VEC_quick_push(constructor_elt, pointers, NULL);
2184 elt->index = size_int(i);
2185 elt->value = fold_convert(const_ptr_type_node, fndecl);
2187 go_assert(i == count + 1);
2189 tree array_type = build_array_type(const_ptr_type_node,
2190 build_index_type(size_int(count)));
2191 tree constructor = build_constructor(array_type, pointers);
2193 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2194 TREE_STATIC(decl) = 1;
2195 TREE_USED(decl) = 1;
2196 TREE_READONLY(decl) = 1;
2197 TREE_CONSTANT(decl) = 1;
2198 DECL_INITIAL(decl) = constructor;
2200 // If the interface type has hidden methods, then this is the only
2201 // definition of the table. Otherwise it is a comdat table which
2202 // may be defined in multiple packages.
2203 if (has_hidden_methods)
2204 TREE_PUBLIC(decl) = 1;
2207 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2208 resolve_unique_section(decl, 1, 0);
2211 rest_of_decl_compilation(decl, 1, 0);
2213 go_preserve_from_gc(decl);
2218 // Mark a function as a builtin library function.
2221 Gogo::mark_fndecl_as_builtin_library(tree fndecl)
2223 DECL_EXTERNAL(fndecl) = 1;
2224 TREE_PUBLIC(fndecl) = 1;
2225 DECL_ARTIFICIAL(fndecl) = 1;
2226 TREE_NOTHROW(fndecl) = 1;
2227 DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
2228 DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
2231 // Build a call to a builtin function.
2234 Gogo::call_builtin(tree* pdecl, Location location, const char* name,
2235 int nargs, tree rettype, ...)
2237 if (rettype == error_mark_node)
2238 return error_mark_node;
2240 tree* types = new tree[nargs];
2241 tree* args = new tree[nargs];
2244 va_start(ap, rettype);
2245 for (int i = 0; i < nargs; ++i)
2247 types[i] = va_arg(ap, tree);
2248 args[i] = va_arg(ap, tree);
2249 if (types[i] == error_mark_node || args[i] == error_mark_node)
2253 return error_mark_node;
2258 if (*pdecl == NULL_TREE)
2260 tree fnid = get_identifier(name);
2262 tree argtypes = NULL_TREE;
2263 tree* pp = &argtypes;
2264 for (int i = 0; i < nargs; ++i)
2266 *pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
2267 pp = &TREE_CHAIN(*pp);
2269 *pp = void_list_node;
2271 tree fntype = build_function_type(rettype, argtypes);
2273 *pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
2274 Gogo::mark_fndecl_as_builtin_library(*pdecl);
2275 go_preserve_from_gc(*pdecl);
2278 tree fnptr = build_fold_addr_expr(*pdecl);
2279 if (CAN_HAVE_LOCATION_P(fnptr))
2280 SET_EXPR_LOCATION(fnptr, location.gcc_location());
2282 tree ret = build_call_array(rettype, fnptr, nargs, args);
2283 SET_EXPR_LOCATION(ret, location.gcc_location());
2291 // Build a call to the runtime error function.
2294 Gogo::runtime_error(int code, Location location)
2296 static tree runtime_error_fndecl;
2297 tree ret = Gogo::call_builtin(&runtime_error_fndecl,
2299 "__go_runtime_error",
2303 build_int_cst(integer_type_node, code));
2304 if (ret == error_mark_node)
2305 return error_mark_node;
2306 // The runtime error function panics and does not return.
2307 TREE_NOTHROW(runtime_error_fndecl) = 0;
2308 TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
2312 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2313 // TYPE_DESCRIPTOR_TREE is the channel's type descriptor. This does a
2314 // blocking receive and returns the value read from the channel.
2317 Gogo::receive_from_channel(tree type_tree, tree type_descriptor_tree,
2318 tree channel, Location location)
2320 if (type_tree == error_mark_node || channel == error_mark_node)
2321 return error_mark_node;
2323 if (int_size_in_bytes(type_tree) <= 8
2324 && !AGGREGATE_TYPE_P(type_tree)
2325 && !FLOAT_TYPE_P(type_tree))
2327 static tree receive_small_fndecl;
2328 tree call = Gogo::call_builtin(&receive_small_fndecl,
2330 "__go_receive_small",
2333 TREE_TYPE(type_descriptor_tree),
2334 type_descriptor_tree,
2337 if (call == error_mark_node)
2338 return error_mark_node;
2339 // This can panic if there are too many operations on a closed
2341 TREE_NOTHROW(receive_small_fndecl) = 0;
2342 int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
2343 tree int_type_tree = go_type_for_size(bitsize, 1);
2344 return fold_convert_loc(location.gcc_location(), type_tree,
2345 fold_convert_loc(location.gcc_location(),
2346 int_type_tree, call));
2350 tree tmp = create_tmp_var(type_tree, get_name(type_tree));
2351 DECL_IGNORED_P(tmp) = 0;
2352 TREE_ADDRESSABLE(tmp) = 1;
2353 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
2354 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
2355 tree tmpaddr = build_fold_addr_expr(tmp);
2356 tmpaddr = fold_convert(ptr_type_node, tmpaddr);
2357 static tree receive_big_fndecl;
2358 tree call = Gogo::call_builtin(&receive_big_fndecl,
2363 TREE_TYPE(type_descriptor_tree),
2364 type_descriptor_tree,
2369 if (call == error_mark_node)
2370 return error_mark_node;
2371 // This can panic if there are too many operations on a closed
2373 TREE_NOTHROW(receive_big_fndecl) = 0;
2374 return build2(COMPOUND_EXPR, type_tree, make_tmp,
2375 build2(COMPOUND_EXPR, type_tree, call, tmp));
2379 // Return the type of a function trampoline. This is like
2380 // get_trampoline_type in tree-nested.c.
2383 Gogo::trampoline_type_tree()
2385 static tree type_tree;
2386 if (type_tree == NULL_TREE)
2390 go_trampoline_info(&size, &align);
2391 tree t = build_index_type(build_int_cst(integer_type_node, size - 1));
2392 t = build_array_type(char_type_node, t);
2394 type_tree = Gogo::builtin_struct(NULL, "__go_trampoline", NULL_TREE, 1,
2396 t = TYPE_FIELDS(type_tree);
2397 DECL_ALIGN(t) = align;
2398 DECL_USER_ALIGN(t) = 1;
2400 go_preserve_from_gc(type_tree);
2405 // Make a trampoline which calls FNADDR passing CLOSURE.
2408 Gogo::make_trampoline(tree fnaddr, tree closure, Location location)
2410 tree trampoline_type = Gogo::trampoline_type_tree();
2411 tree trampoline_size = TYPE_SIZE_UNIT(trampoline_type);
2413 closure = save_expr(closure);
2415 // We allocate the trampoline using a special function which will
2416 // mark it as executable.
2417 static tree trampoline_fndecl;
2418 tree x = Gogo::call_builtin(&trampoline_fndecl,
2420 "__go_allocate_trampoline",
2426 fold_convert_loc(location.gcc_location(),
2427 ptr_type_node, closure));
2428 if (x == error_mark_node)
2429 return error_mark_node;
2433 // Initialize the trampoline.
2434 tree calldecl = builtin_decl_implicit(BUILT_IN_INIT_HEAP_TRAMPOLINE);
2435 tree ini = build_call_expr(calldecl, 3, x, fnaddr, closure);
2437 // On some targets the trampoline address needs to be adjusted. For
2438 // example, when compiling in Thumb mode on the ARM, the address
2439 // needs to have the low bit set.
2440 x = build_call_expr(builtin_decl_explicit(BUILT_IN_ADJUST_TRAMPOLINE), 1, x);
2441 x = fold_convert(TREE_TYPE(fnaddr), x);
2443 return build2(COMPOUND_EXPR, TREE_TYPE(x), ini, x);