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 built_in_decls[bcode] = decl;
73 implicit_built_in_decls[bcode] = decl;
74 builtin_functions[name] = decl;
77 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
80 TREE_READONLY(decl) = 1;
81 builtin_functions[libname] = decl;
85 // Create trees for implicit builtin functions.
88 Gogo::define_builtin_function_trees()
90 /* We need to define the fetch_and_add functions, since we use them
92 tree t = go_type_for_size(BITS_PER_UNIT, 1);
93 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
94 define_builtin(BUILT_IN_ADD_AND_FETCH_1, "__sync_fetch_and_add_1", NULL,
95 build_function_type_list(t, p, t, NULL_TREE), false);
97 t = go_type_for_size(BITS_PER_UNIT * 2, 1);
98 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
99 define_builtin (BUILT_IN_ADD_AND_FETCH_2, "__sync_fetch_and_add_2", NULL,
100 build_function_type_list(t, p, t, NULL_TREE), false);
102 t = go_type_for_size(BITS_PER_UNIT * 4, 1);
103 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
104 define_builtin(BUILT_IN_ADD_AND_FETCH_4, "__sync_fetch_and_add_4", NULL,
105 build_function_type_list(t, p, t, NULL_TREE), false);
107 t = go_type_for_size(BITS_PER_UNIT * 8, 1);
108 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
109 define_builtin(BUILT_IN_ADD_AND_FETCH_8, "__sync_fetch_and_add_8", NULL,
110 build_function_type_list(t, p, t, NULL_TREE), false);
112 // We use __builtin_expect for magic import functions.
113 define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
114 build_function_type_list(long_integer_type_node,
115 long_integer_type_node,
116 long_integer_type_node,
120 // We use __builtin_memmove for the predeclared copy function.
121 define_builtin(BUILT_IN_MEMMOVE, "__builtin_memmove", "memmove",
122 build_function_type_list(ptr_type_node,
129 // We provide sqrt for the math library.
130 define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
131 build_function_type_list(double_type_node,
135 define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
136 build_function_type_list(long_double_type_node,
137 long_double_type_node,
141 // We use __builtin_return_address in the thunk we build for
142 // functions which call recover.
143 define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", NULL,
144 build_function_type_list(ptr_type_node,
149 // The compiler uses __builtin_trap for some exception handling
151 define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
152 build_function_type(void_type_node, void_list_node),
156 // Get the name to use for the import control function. If there is a
157 // global function or variable, then we know that that name must be
158 // unique in the link, and we use it as the basis for our name.
161 Gogo::get_init_fn_name()
163 if (this->init_fn_name_.empty())
165 go_assert(this->package_ != NULL);
166 if (this->is_main_package())
168 // Use a name which the runtime knows.
169 this->init_fn_name_ = "__go_init_main";
173 std::string s = this->unique_prefix();
175 s.append(this->package_name());
176 s.append("..import");
177 this->init_fn_name_ = s;
181 return this->init_fn_name_;
184 // Add statements to INIT_STMT_LIST which run the initialization
185 // functions for imported packages. This is only used for the "main"
189 Gogo::init_imports(tree* init_stmt_list)
191 go_assert(this->is_main_package());
193 if (this->imported_init_fns_.empty())
196 tree fntype = build_function_type(void_type_node, void_list_node);
198 // We must call them in increasing priority order.
199 std::vector<Import_init> v;
200 for (std::set<Import_init>::const_iterator p =
201 this->imported_init_fns_.begin();
202 p != this->imported_init_fns_.end();
205 std::sort(v.begin(), v.end());
207 for (std::vector<Import_init>::const_iterator p = v.begin();
211 std::string user_name = p->package_name() + ".init";
212 tree decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
213 get_identifier_from_string(user_name),
215 const std::string& init_name(p->init_name());
216 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(init_name));
217 TREE_PUBLIC(decl) = 1;
218 DECL_EXTERNAL(decl) = 1;
219 append_to_statement_list(build_call_expr(decl, 0), init_stmt_list);
223 // Register global variables with the garbage collector. We need to
224 // register all variables which can hold a pointer value. They become
225 // roots during the mark phase. We build a struct that is easy to
226 // hook into a list of roots.
228 // struct __go_gc_root_list
230 // struct __go_gc_root_list* __next;
231 // struct __go_gc_root
238 // The last entry in the roots array has a NULL decl field.
241 Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
242 tree* init_stmt_list)
247 size_t count = var_gc.size();
249 tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
255 tree index_type = build_index_type(size_int(count));
256 tree array_type = build_array_type(root_type, index_type);
258 tree root_list_type = make_node(RECORD_TYPE);
259 root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
262 build_pointer_type(root_list_type),
266 // Build an initialier for the __roots array.
268 VEC(constructor_elt,gc)* roots_init = VEC_alloc(constructor_elt, gc,
272 for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
276 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
278 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
279 tree field = TYPE_FIELDS(root_type);
281 Bvariable* bvar = (*p)->get_backend_variable(this, NULL);
282 tree decl = var_to_tree(bvar);
283 go_assert(TREE_CODE(decl) == VAR_DECL);
284 elt->value = build_fold_addr_expr(decl);
286 elt = VEC_quick_push(constructor_elt, init, NULL);
287 field = DECL_CHAIN(field);
289 elt->value = DECL_SIZE_UNIT(decl);
291 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
292 elt->index = size_int(i);
293 elt->value = build_constructor(root_type, init);
296 // The list ends with a NULL entry.
298 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
300 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
301 tree field = TYPE_FIELDS(root_type);
303 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
305 elt = VEC_quick_push(constructor_elt, init, NULL);
306 field = DECL_CHAIN(field);
308 elt->value = size_zero_node;
310 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
311 elt->index = size_int(i);
312 elt->value = build_constructor(root_type, init);
314 // Build a constructor for the struct.
316 VEC(constructor_elt,gc*) root_list_init = VEC_alloc(constructor_elt, gc, 2);
318 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
319 field = TYPE_FIELDS(root_list_type);
321 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
323 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
324 field = DECL_CHAIN(field);
326 elt->value = build_constructor(array_type, roots_init);
328 // Build a decl to register.
330 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
331 create_tmp_var_name("gc"), root_list_type);
332 DECL_EXTERNAL(decl) = 0;
333 TREE_PUBLIC(decl) = 0;
334 TREE_STATIC(decl) = 1;
335 DECL_ARTIFICIAL(decl) = 1;
336 DECL_INITIAL(decl) = build_constructor(root_list_type, root_list_init);
337 rest_of_decl_compilation(decl, 1, 0);
339 static tree register_gc_fndecl;
340 tree call = Gogo::call_builtin(®ister_gc_fndecl, BUILTINS_LOCATION,
341 "__go_register_gc_roots",
344 build_pointer_type(root_list_type),
345 build_fold_addr_expr(decl));
346 if (call != error_mark_node)
347 append_to_statement_list(call, init_stmt_list);
350 // Build the decl for the initialization function.
353 Gogo::initialization_function_decl()
355 // The tedious details of building your own function. There doesn't
356 // seem to be a helper function for this.
357 std::string name = this->package_name() + ".init";
358 tree fndecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL,
359 get_identifier_from_string(name),
360 build_function_type(void_type_node,
362 const std::string& asm_name(this->get_init_fn_name());
363 SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
365 tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
367 DECL_ARTIFICIAL(resdecl) = 1;
368 DECL_CONTEXT(resdecl) = fndecl;
369 DECL_RESULT(fndecl) = resdecl;
371 TREE_STATIC(fndecl) = 1;
372 TREE_USED(fndecl) = 1;
373 DECL_ARTIFICIAL(fndecl) = 1;
374 TREE_PUBLIC(fndecl) = 1;
376 DECL_INITIAL(fndecl) = make_node(BLOCK);
377 TREE_USED(DECL_INITIAL(fndecl)) = 1;
382 // Create the magic initialization function. INIT_STMT_LIST is the
383 // code that it needs to run.
386 Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
388 // Make sure that we thought we needed an initialization function,
389 // as otherwise we will not have reported it in the export data.
390 go_assert(this->is_main_package() || this->need_init_fn_);
392 if (fndecl == NULL_TREE)
393 fndecl = this->initialization_function_decl();
395 DECL_SAVED_TREE(fndecl) = init_stmt_list;
397 current_function_decl = fndecl;
398 if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
399 push_struct_function(fndecl);
401 push_cfun(DECL_STRUCT_FUNCTION(fndecl));
402 cfun->function_end_locus = BUILTINS_LOCATION;
404 gimplify_function_tree(fndecl);
406 cgraph_add_new_function(fndecl, false);
407 cgraph_mark_needed_node(cgraph_get_node(fndecl));
409 current_function_decl = NULL_TREE;
413 // Search for references to VAR in any statements or called functions.
415 class Find_var : public Traverse
418 // A hash table we use to avoid looping. The index is the name of a
419 // named object. We only look through objects defined in this
421 typedef Unordered_set(std::string) Seen_objects;
423 Find_var(Named_object* var, Seen_objects* seen_objects)
424 : Traverse(traverse_expressions),
425 var_(var), seen_objects_(seen_objects), found_(false)
428 // Whether the variable was found.
431 { return this->found_; }
434 expression(Expression**);
437 // The variable we are looking for.
439 // Names of objects we have already seen.
440 Seen_objects* seen_objects_;
441 // True if the variable was found.
445 // See if EXPR refers to VAR, looking through function calls and
446 // variable initializations.
449 Find_var::expression(Expression** pexpr)
451 Expression* e = *pexpr;
453 Var_expression* ve = e->var_expression();
456 Named_object* v = ve->named_object();
460 return TRAVERSE_EXIT;
463 if (v->is_variable() && v->package() == NULL)
465 Expression* init = v->var_value()->init();
468 std::pair<Seen_objects::iterator, bool> ins =
469 this->seen_objects_->insert(v->name());
472 // This is the first time we have seen this name.
473 if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
474 return TRAVERSE_EXIT;
480 // We traverse the code of any function we see. Note that this
481 // means that we will traverse the code of a function whose address
482 // is taken even if it is not called.
483 Func_expression* fe = e->func_expression();
486 const Named_object* f = fe->named_object();
487 if (f->is_function() && f->package() == NULL)
489 std::pair<Seen_objects::iterator, bool> ins =
490 this->seen_objects_->insert(f->name());
493 // This is the first time we have seen this name.
494 if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
495 return TRAVERSE_EXIT;
500 return TRAVERSE_CONTINUE;
503 // Return true if EXPR refers to VAR.
506 expression_requires(Expression* expr, Block* preinit, Named_object* var)
508 Find_var::Seen_objects seen_objects;
509 Find_var find_var(var, &seen_objects);
511 Expression::traverse(&expr, &find_var);
513 preinit->traverse(&find_var);
515 return find_var.found();
518 // Sort variable initializations. If the initialization expression
519 // for variable A refers directly or indirectly to the initialization
520 // expression for variable B, then we must initialize B before A.
526 : var_(NULL), init_(NULL_TREE), waiting_(0)
529 Var_init(Named_object* var, tree init)
530 : var_(var), init_(init), waiting_(0)
533 // Return the variable.
536 { return this->var_; }
538 // Return the initialization expression.
541 { return this->init_; }
543 // Return the number of variables waiting for this one to be
547 { return this->waiting_; }
549 // Increment the number waiting.
552 { ++this->waiting_; }
555 // The variable being initialized.
557 // The initialization expression to run.
559 // The number of variables which are waiting for this one.
563 typedef std::list<Var_init> Var_inits;
565 // Sort the variable initializations. The rule we follow is that we
566 // emit them in the order they appear in the array, except that if the
567 // initialization expression for a variable V1 depends upon another
568 // variable V2 then we initialize V1 after V2.
571 sort_var_inits(Var_inits* var_inits)
574 while (!var_inits->empty())
576 Var_inits::iterator p1 = var_inits->begin();
577 Named_object* var = p1->var();
578 Expression* init = var->var_value()->init();
579 Block* preinit = var->var_value()->preinit();
581 // Start walking through the list to see which variables VAR
582 // needs to wait for. We can skip P1->WAITING variables--that
583 // is the number we've already checked.
584 Var_inits::iterator p2 = p1;
586 for (size_t i = p1->waiting(); i > 0; --i)
589 for (; p2 != var_inits->end(); ++p2)
591 if (expression_requires(init, preinit, p2->var()))
594 if (expression_requires(p2->var()->var_value()->init(),
595 p2->var()->var_value()->preinit(),
598 error_at(var->location(),
599 ("initialization expressions for %qs and "
600 "%qs depend upon each other"),
601 var->message_name().c_str(),
602 p2->var()->message_name().c_str());
603 inform(p2->var()->location(), "%qs defined here",
604 p2->var()->message_name().c_str());
605 p2 = var_inits->end();
609 // We can't emit P1 until P2 is emitted. Move P1.
610 // Note that the WAITING loop always executes at
611 // least once, which is what we want.
612 p2->increment_waiting();
613 Var_inits::iterator p3 = p2;
614 for (size_t i = p2->waiting(); i > 0; --i)
616 var_inits->splice(p3, *var_inits, p1);
622 if (p2 == var_inits->end())
624 // VAR does not depends upon any other initialization expressions.
626 // Check for a loop of VAR on itself. We only do this if
627 // INIT is not NULL; when INIT is NULL, it means that
628 // PREINIT sets VAR, which we will interpret as a loop.
629 if (init != NULL && expression_requires(init, preinit, var))
630 error_at(var->location(),
631 "initialization expression for %qs depends upon itself",
632 var->message_name().c_str());
633 ready.splice(ready.end(), *var_inits, p1);
637 // Now READY is the list in the desired initialization order.
638 var_inits->swap(ready);
641 // Write out the global definitions.
644 Gogo::write_globals()
646 this->convert_named_types();
647 this->build_interface_method_tables();
649 Bindings* bindings = this->current_bindings();
650 size_t count = bindings->size_definitions();
652 tree* vec = new tree[count];
654 tree init_fndecl = NULL_TREE;
655 tree init_stmt_list = NULL_TREE;
657 if (this->is_main_package())
658 this->init_imports(&init_stmt_list);
660 // A list of variable initializations.
663 // A list of variables which need to be registered with the garbage
665 std::vector<Named_object*> var_gc;
666 var_gc.reserve(count);
668 tree var_init_stmt_list = NULL_TREE;
670 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
671 p != bindings->end_definitions();
674 Named_object* no = *p;
676 go_assert(!no->is_type_declaration() && !no->is_function_declaration());
677 // There is nothing to do for a package.
678 if (no->is_package())
685 // There is nothing to do for an object which was imported from
686 // a different package into the global scope.
687 if (no->package() != NULL)
694 // There is nothing useful we can output for constants which
695 // have ideal or non-integeral type.
698 Type* type = no->const_value()->type();
700 type = no->const_value()->expr()->type();
701 if (type->is_abstract() || type->integer_type() == NULL)
709 if (!no->is_variable())
711 vec[i] = no->get_tree(this, NULL);
712 if (vec[i] == error_mark_node)
714 go_assert(saw_errors());
722 Bvariable* var = no->get_backend_variable(this, NULL);
723 vec[i] = var_to_tree(var);
724 if (vec[i] == error_mark_node)
726 go_assert(saw_errors());
732 // Check for a sink variable, which may be used to run an
733 // initializer purely for its side effects.
734 bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
736 tree var_init_tree = NULL_TREE;
737 if (!no->var_value()->has_pre_init())
739 tree init = no->var_value()->get_init_tree(this, NULL);
740 if (init == error_mark_node)
741 go_assert(saw_errors());
742 else if (init == NULL_TREE)
744 else if (TREE_CONSTANT(init))
745 this->backend()->global_variable_set_init(var,
748 var_init_tree = init;
750 var_init_tree = fold_build2_loc(no->location(), MODIFY_EXPR,
751 void_type_node, vec[i], init);
755 // We are going to create temporary variables which
756 // means that we need an fndecl.
757 if (init_fndecl == NULL_TREE)
758 init_fndecl = this->initialization_function_decl();
759 current_function_decl = init_fndecl;
760 if (DECL_STRUCT_FUNCTION(init_fndecl) == NULL)
761 push_struct_function(init_fndecl);
763 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
765 tree var_decl = is_sink ? NULL_TREE : vec[i];
766 var_init_tree = no->var_value()->get_init_block(this, NULL,
769 current_function_decl = NULL_TREE;
773 if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
775 if (no->var_value()->init() == NULL
776 && !no->var_value()->has_pre_init())
777 append_to_statement_list(var_init_tree, &var_init_stmt_list);
779 var_inits.push_back(Var_init(no, var_init_tree));
782 if (!is_sink && no->var_value()->type()->has_pointer())
783 var_gc.push_back(no);
787 // Register global variables with the garbage collector.
788 this->register_gc_vars(var_gc, &init_stmt_list);
790 // Simple variable initializations, after all variables are
792 append_to_statement_list(var_init_stmt_list, &init_stmt_list);
794 // Complex variable initializations, first sorting them into a
796 if (!var_inits.empty())
798 sort_var_inits(&var_inits);
799 for (Var_inits::const_iterator p = var_inits.begin();
800 p != var_inits.end();
802 append_to_statement_list(p->init(), &init_stmt_list);
805 // After all the variables are initialized, call the "init"
806 // functions if there are any.
807 for (std::vector<Named_object*>::const_iterator p =
808 this->init_functions_.begin();
809 p != this->init_functions_.end();
812 tree decl = (*p)->get_tree(this, NULL);
813 tree call = build_call_expr(decl, 0);
814 append_to_statement_list(call, &init_stmt_list);
817 // Set up a magic function to do all the initialization actions.
818 // This will be called if this package is imported.
819 if (init_stmt_list != NULL_TREE
820 || this->need_init_fn_
821 || this->is_main_package())
822 this->write_initialization_function(init_fndecl, init_stmt_list);
824 // Pass everything back to the middle-end.
826 wrapup_global_declarations(vec, count);
828 cgraph_finalize_compilation_unit();
830 check_global_declarations(vec, count);
831 emit_debug_global_declarations(vec, count);
836 // Get a tree for the identifier for a named object.
839 Named_object::get_id(Gogo* gogo)
841 go_assert(!this->is_variable() && !this->is_result_variable());
842 std::string decl_name;
843 if (this->is_function_declaration()
844 && !this->func_declaration_value()->asm_name().empty())
845 decl_name = this->func_declaration_value()->asm_name();
846 else if (this->is_type()
847 && this->type_value()->location() == BUILTINS_LOCATION)
849 // We don't need the package name for builtin types.
850 decl_name = Gogo::unpack_hidden_name(this->name_);
854 std::string package_name;
855 if (this->package_ == NULL)
856 package_name = gogo->package_name();
858 package_name = this->package_->name();
860 decl_name = package_name + '.' + Gogo::unpack_hidden_name(this->name_);
862 Function_type* fntype;
863 if (this->is_function())
864 fntype = this->func_value()->type();
865 else if (this->is_function_declaration())
866 fntype = this->func_declaration_value()->type();
869 if (fntype != NULL && fntype->is_method())
871 decl_name.push_back('.');
872 decl_name.append(fntype->receiver()->type()->mangled_name(gogo));
877 const Named_object* in_function = this->type_value()->in_function();
878 if (in_function != NULL)
879 decl_name += '$' + in_function->name();
881 return get_identifier_from_string(decl_name);
884 // Get a tree for a named object.
887 Named_object::get_tree(Gogo* gogo, Named_object* function)
889 if (this->tree_ != NULL_TREE)
893 if (this->classification_ == NAMED_OBJECT_TYPE)
896 name = this->get_id(gogo);
898 switch (this->classification_)
900 case NAMED_OBJECT_CONST:
902 Named_constant* named_constant = this->u_.const_value;
903 Translate_context subcontext(gogo, function, NULL, NULL);
904 tree expr_tree = named_constant->expr()->get_tree(&subcontext);
905 if (expr_tree == error_mark_node)
906 decl = error_mark_node;
909 Type* type = named_constant->type();
910 if (type != NULL && !type->is_abstract())
912 if (type->is_error())
913 expr_tree = error_mark_node;
916 Btype* btype = type->get_backend(gogo);
917 expr_tree = fold_convert(type_to_tree(btype), expr_tree);
920 if (expr_tree == error_mark_node)
921 decl = error_mark_node;
922 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
924 decl = build_decl(named_constant->location(), CONST_DECL,
925 name, TREE_TYPE(expr_tree));
926 DECL_INITIAL(decl) = expr_tree;
927 TREE_CONSTANT(decl) = 1;
928 TREE_READONLY(decl) = 1;
932 // A CONST_DECL is only for an enum constant, so we
933 // shouldn't use for non-integral types. Instead we
934 // just return the constant itself, rather than a
942 case NAMED_OBJECT_TYPE:
944 Named_type* named_type = this->u_.type_value;
945 tree type_tree = type_to_tree(named_type->get_backend(gogo));
946 if (type_tree == error_mark_node)
947 decl = error_mark_node;
950 decl = TYPE_NAME(type_tree);
951 go_assert(decl != NULL_TREE);
953 // We need to produce a type descriptor for every named
954 // type, and for a pointer to every named type, since
955 // other files or packages might refer to them. We need
956 // to do this even for hidden types, because they might
957 // still be returned by some function. Simply calling the
958 // type_descriptor method is enough to create the type
959 // descriptor, even though we don't do anything with it.
960 if (this->package_ == NULL)
962 named_type->type_descriptor_pointer(gogo);
963 Type* pn = Type::make_pointer_type(named_type);
964 pn->type_descriptor_pointer(gogo);
970 case NAMED_OBJECT_TYPE_DECLARATION:
971 error("reference to undefined type %qs",
972 this->message_name().c_str());
973 return error_mark_node;
975 case NAMED_OBJECT_VAR:
976 case NAMED_OBJECT_RESULT_VAR:
977 case NAMED_OBJECT_SINK:
980 case NAMED_OBJECT_FUNC:
982 Function* func = this->u_.func_value;
983 decl = func->get_or_make_decl(gogo, this, name);
984 if (decl != error_mark_node)
986 if (func->block() != NULL)
988 if (DECL_STRUCT_FUNCTION(decl) == NULL)
989 push_struct_function(decl);
991 push_cfun(DECL_STRUCT_FUNCTION(decl));
993 cfun->function_end_locus = func->block()->end_location();
995 current_function_decl = decl;
997 func->build_tree(gogo, this);
999 gimplify_function_tree(decl);
1001 cgraph_finalize_function(decl, true);
1003 current_function_decl = NULL_TREE;
1014 if (TREE_TYPE(decl) == error_mark_node)
1015 decl = error_mark_node;
1021 if (ret != error_mark_node)
1022 go_preserve_from_gc(ret);
1027 // Get the initial value of a variable as a tree. This does not
1028 // consider whether the variable is in the heap--it returns the
1029 // initial value as though it were always stored in the stack.
1032 Variable::get_init_tree(Gogo* gogo, Named_object* function)
1034 go_assert(this->preinit_ == NULL);
1035 if (this->init_ == NULL)
1037 go_assert(!this->is_parameter_);
1038 return this->type_->get_init_tree(gogo,
1040 || this->is_in_heap()));
1044 Translate_context context(gogo, function, NULL, NULL);
1045 tree rhs_tree = this->init_->get_tree(&context);
1046 return Expression::convert_for_assignment(&context, this->type(),
1047 this->init_->type(),
1048 rhs_tree, this->location());
1052 // Get the initial value of a variable when a block is required.
1053 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1056 Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
1058 go_assert(this->preinit_ != NULL);
1060 // We want to add the variable assignment to the end of the preinit
1061 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1062 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1063 // regular statements.
1065 Translate_context context(gogo, function, NULL, NULL);
1066 Bblock* bblock = this->preinit_->get_backend(&context);
1067 tree block_tree = block_to_tree(bblock);
1068 if (block_tree == error_mark_node)
1069 return error_mark_node;
1070 go_assert(TREE_CODE(block_tree) == BIND_EXPR);
1071 tree statements = BIND_EXPR_BODY(block_tree);
1072 while (statements != NULL_TREE
1073 && (TREE_CODE(statements) == TRY_FINALLY_EXPR
1074 || TREE_CODE(statements) == TRY_CATCH_EXPR))
1075 statements = TREE_OPERAND(statements, 0);
1077 // It's possible to have pre-init statements without an initializer
1078 // if the pre-init statements set the variable.
1079 if (this->init_ != NULL)
1081 tree rhs_tree = this->init_->get_tree(&context);
1082 if (rhs_tree == error_mark_node)
1083 return error_mark_node;
1084 if (var_decl == NULL_TREE)
1085 append_to_statement_list(rhs_tree, &statements);
1088 tree val = Expression::convert_for_assignment(&context, this->type(),
1089 this->init_->type(),
1092 if (val == error_mark_node)
1093 return error_mark_node;
1094 tree set = fold_build2_loc(this->location(), MODIFY_EXPR,
1095 void_type_node, var_decl, val);
1096 append_to_statement_list(set, &statements);
1103 // Get a tree for a function decl.
1106 Function::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1108 if (this->fndecl_ == NULL_TREE)
1110 tree functype = type_to_tree(this->type_->get_backend(gogo));
1111 if (functype == error_mark_node)
1112 this->fndecl_ = error_mark_node;
1115 // The type of a function comes back as a pointer, but we
1116 // want the real function type for a function declaration.
1117 go_assert(POINTER_TYPE_P(functype));
1118 functype = TREE_TYPE(functype);
1119 tree decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1121 this->fndecl_ = decl;
1123 if (no->package() != NULL)
1125 else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
1127 else if (Gogo::unpack_hidden_name(no->name()) == "init"
1128 && !this->type_->is_method())
1130 else if (Gogo::unpack_hidden_name(no->name()) == "main"
1131 && gogo->is_main_package())
1132 TREE_PUBLIC(decl) = 1;
1133 // Methods have to be public even if they are hidden because
1134 // they can be pulled into type descriptors when using
1135 // anonymous fields.
1136 else if (!Gogo::is_hidden_name(no->name())
1137 || this->type_->is_method())
1139 TREE_PUBLIC(decl) = 1;
1140 std::string asm_name = gogo->unique_prefix();
1141 asm_name.append(1, '.');
1142 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1143 SET_DECL_ASSEMBLER_NAME(decl,
1144 get_identifier_from_string(asm_name));
1147 // Why do we have to do this in the frontend?
1148 tree restype = TREE_TYPE(functype);
1149 tree resdecl = build_decl(this->location(), RESULT_DECL, NULL_TREE,
1151 DECL_ARTIFICIAL(resdecl) = 1;
1152 DECL_IGNORED_P(resdecl) = 1;
1153 DECL_CONTEXT(resdecl) = decl;
1154 DECL_RESULT(decl) = resdecl;
1156 if (this->enclosing_ != NULL)
1157 DECL_STATIC_CHAIN(decl) = 1;
1159 // If a function calls the predeclared recover function, we
1160 // can't inline it, because recover behaves differently in a
1161 // function passed directly to defer.
1162 if (this->calls_recover_ && !this->is_recover_thunk_)
1163 DECL_UNINLINABLE(decl) = 1;
1165 // If this is a thunk created to call a function which calls
1166 // the predeclared recover function, we need to disable
1167 // stack splitting for the thunk.
1168 if (this->is_recover_thunk_)
1170 tree attr = get_identifier("__no_split_stack__");
1171 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1174 go_preserve_from_gc(decl);
1176 if (this->closure_var_ != NULL)
1178 push_struct_function(decl);
1180 Bvariable* bvar = this->closure_var_->get_backend_variable(gogo,
1182 tree closure_decl = var_to_tree(bvar);
1183 if (closure_decl == error_mark_node)
1184 this->fndecl_ = error_mark_node;
1187 DECL_ARTIFICIAL(closure_decl) = 1;
1188 DECL_IGNORED_P(closure_decl) = 1;
1189 TREE_USED(closure_decl) = 1;
1190 DECL_ARG_TYPE(closure_decl) = TREE_TYPE(closure_decl);
1191 TREE_READONLY(closure_decl) = 1;
1193 DECL_STRUCT_FUNCTION(decl)->static_chain_decl = closure_decl;
1200 return this->fndecl_;
1203 // Get a tree for a function declaration.
1206 Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1208 if (this->fndecl_ == NULL_TREE)
1210 // Let Go code use an asm declaration to pick up a builtin
1212 if (!this->asm_name_.empty())
1214 std::map<std::string, tree>::const_iterator p =
1215 builtin_functions.find(this->asm_name_);
1216 if (p != builtin_functions.end())
1218 this->fndecl_ = p->second;
1219 return this->fndecl_;
1223 tree functype = type_to_tree(this->fntype_->get_backend(gogo));
1225 if (functype == error_mark_node)
1226 decl = error_mark_node;
1229 // The type of a function comes back as a pointer, but we
1230 // want the real function type for a function declaration.
1231 go_assert(POINTER_TYPE_P(functype));
1232 functype = TREE_TYPE(functype);
1233 decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1234 TREE_PUBLIC(decl) = 1;
1235 DECL_EXTERNAL(decl) = 1;
1237 if (this->asm_name_.empty())
1239 std::string asm_name = (no->package() == NULL
1240 ? gogo->unique_prefix()
1241 : no->package()->unique_prefix());
1242 asm_name.append(1, '.');
1243 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1244 SET_DECL_ASSEMBLER_NAME(decl,
1245 get_identifier_from_string(asm_name));
1248 this->fndecl_ = decl;
1249 go_preserve_from_gc(decl);
1251 return this->fndecl_;
1254 // We always pass the receiver to a method as a pointer. If the
1255 // receiver is actually declared as a non-pointer type, then we copy
1256 // the value into a local variable, so that it has the right type. In
1257 // this function we create the real PARM_DECL to use, and set
1258 // DEC_INITIAL of the var_decl to be the value passed in.
1261 Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
1263 if (var_decl == error_mark_node)
1264 return error_mark_node;
1265 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1266 tree val_type = TREE_TYPE(var_decl);
1267 bool is_in_heap = no->var_value()->is_in_heap();
1270 go_assert(POINTER_TYPE_P(val_type));
1271 val_type = TREE_TYPE(val_type);
1274 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1275 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1277 tree id = get_identifier_from_string(name);
1278 tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
1279 DECL_CONTEXT(parm_decl) = current_function_decl;
1280 DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
1282 go_assert(DECL_INITIAL(var_decl) == NULL_TREE);
1283 // The receiver might be passed as a null pointer.
1284 tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node, parm_decl,
1285 fold_convert_loc(loc, TREE_TYPE(parm_decl),
1286 null_pointer_node));
1287 tree ind = build_fold_indirect_ref_loc(loc, parm_decl);
1288 TREE_THIS_NOTRAP(ind) = 1;
1289 tree zero_init = no->var_value()->type()->get_init_tree(gogo, false);
1290 tree init = fold_build3_loc(loc, COND_EXPR, TREE_TYPE(ind),
1291 check, ind, zero_init);
1295 tree size = TYPE_SIZE_UNIT(val_type);
1296 tree space = gogo->allocate_memory(no->var_value()->type(), size,
1298 space = save_expr(space);
1299 space = fold_convert(build_pointer_type(val_type), space);
1300 tree spaceref = build_fold_indirect_ref_loc(no->location(), space);
1301 TREE_THIS_NOTRAP(spaceref) = 1;
1302 tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node,
1304 fold_convert_loc(loc, TREE_TYPE(parm_decl),
1305 null_pointer_node));
1306 tree parmref = build_fold_indirect_ref_loc(no->location(), parm_decl);
1307 TREE_THIS_NOTRAP(parmref) = 1;
1308 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1310 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1311 build3(COND_EXPR, void_type_node,
1312 check, set, NULL_TREE),
1316 DECL_INITIAL(var_decl) = init;
1321 // If we take the address of a parameter, then we need to copy it into
1322 // the heap. We will access it as a local variable via an
1326 Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree var_decl)
1328 if (var_decl == error_mark_node)
1329 return error_mark_node;
1330 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1331 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1333 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1335 tree id = get_identifier_from_string(name);
1337 tree type = TREE_TYPE(var_decl);
1338 go_assert(POINTER_TYPE_P(type));
1339 type = TREE_TYPE(type);
1341 tree parm_decl = build_decl(loc, PARM_DECL, id, type);
1342 DECL_CONTEXT(parm_decl) = current_function_decl;
1343 DECL_ARG_TYPE(parm_decl) = type;
1345 tree size = TYPE_SIZE_UNIT(type);
1346 tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
1347 space = save_expr(space);
1348 space = fold_convert(TREE_TYPE(var_decl), space);
1349 tree spaceref = build_fold_indirect_ref_loc(loc, space);
1350 TREE_THIS_NOTRAP(spaceref) = 1;
1351 tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
1352 build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
1354 DECL_INITIAL(var_decl) = init;
1359 // Get a tree for function code.
1362 Function::build_tree(Gogo* gogo, Named_object* named_function)
1364 tree fndecl = this->fndecl_;
1365 go_assert(fndecl != NULL_TREE);
1367 tree params = NULL_TREE;
1370 tree declare_vars = NULL_TREE;
1371 for (Bindings::const_definitions_iterator p =
1372 this->block_->bindings()->begin_definitions();
1373 p != this->block_->bindings()->end_definitions();
1376 if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
1378 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1379 *pp = var_to_tree(bvar);
1381 // We always pass the receiver to a method as a pointer. If
1382 // the receiver is declared as a non-pointer type, then we
1383 // copy the value into a local variable.
1384 if ((*p)->var_value()->is_receiver()
1385 && (*p)->var_value()->type()->points_to() == NULL)
1387 tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
1389 if (var != error_mark_node)
1391 go_assert(TREE_CODE(var) == VAR_DECL);
1392 DECL_CHAIN(var) = declare_vars;
1397 else if ((*p)->var_value()->is_in_heap())
1399 // If we take the address of a parameter, then we need
1400 // to copy it into the heap.
1401 tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
1403 if (var != error_mark_node)
1405 go_assert(TREE_CODE(var) == VAR_DECL);
1406 DECL_CHAIN(var) = declare_vars;
1412 if (*pp != error_mark_node)
1414 go_assert(TREE_CODE(*pp) == PARM_DECL);
1415 pp = &DECL_CHAIN(*pp);
1418 else if ((*p)->is_result_variable())
1420 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1421 tree var_decl = var_to_tree(bvar);
1423 Type* type = (*p)->result_var_value()->type();
1425 if (!(*p)->result_var_value()->is_in_heap())
1426 init = type->get_init_tree(gogo, false);
1429 source_location loc = (*p)->location();
1430 tree type_tree = type_to_tree(type->get_backend(gogo));
1431 tree space = gogo->allocate_memory(type,
1432 TYPE_SIZE_UNIT(type_tree),
1434 tree ptr_type_tree = build_pointer_type(type_tree);
1435 tree subinit = type->get_init_tree(gogo, true);
1436 if (subinit == NULL_TREE)
1437 init = fold_convert_loc(loc, ptr_type_tree, space);
1440 space = save_expr(space);
1441 space = fold_convert_loc(loc, ptr_type_tree, space);
1442 tree spaceref = build_fold_indirect_ref_loc(loc, space);
1443 TREE_THIS_NOTRAP(spaceref) = 1;
1444 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1446 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1451 if (var_decl != error_mark_node)
1453 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1454 DECL_INITIAL(var_decl) = init;
1455 DECL_CHAIN(var_decl) = declare_vars;
1456 declare_vars = var_decl;
1462 DECL_ARGUMENTS(fndecl) = params;
1464 if (this->block_ != NULL)
1466 go_assert(DECL_INITIAL(fndecl) == NULL_TREE);
1468 // Declare variables if necessary.
1469 tree bind = NULL_TREE;
1470 tree defer_init = NULL_TREE;
1471 if (declare_vars != NULL_TREE || this->defer_stack_ != NULL)
1473 tree block = make_node(BLOCK);
1474 BLOCK_SUPERCONTEXT(block) = fndecl;
1475 DECL_INITIAL(fndecl) = block;
1476 BLOCK_VARS(block) = declare_vars;
1477 TREE_USED(block) = 1;
1479 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
1481 TREE_SIDE_EFFECTS(bind) = 1;
1483 if (this->defer_stack_ != NULL)
1485 Translate_context dcontext(gogo, named_function, this->block_,
1486 tree_to_block(bind));
1487 Bstatement* bdi = this->defer_stack_->get_backend(&dcontext);
1488 defer_init = stat_to_tree(bdi);
1492 // Build the trees for all the statements in the function.
1493 Translate_context context(gogo, named_function, NULL, NULL);
1494 Bblock* bblock = this->block_->get_backend(&context);
1495 tree code = block_to_tree(bblock);
1497 tree init = NULL_TREE;
1498 tree except = NULL_TREE;
1499 tree fini = NULL_TREE;
1501 // Initialize variables if necessary.
1502 for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
1504 tree dv = build1(DECL_EXPR, void_type_node, v);
1505 SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
1506 append_to_statement_list(dv, &init);
1509 // If we have a defer stack, initialize it at the start of a
1511 if (defer_init != NULL_TREE && defer_init != error_mark_node)
1513 SET_EXPR_LOCATION(defer_init, this->block_->start_location());
1514 append_to_statement_list(defer_init, &init);
1516 // Clean up the defer stack when we leave the function.
1517 this->build_defer_wrapper(gogo, named_function, &except, &fini);
1520 if (code != NULL_TREE && code != error_mark_node)
1522 if (init != NULL_TREE)
1523 code = build2(COMPOUND_EXPR, void_type_node, init, code);
1524 if (except != NULL_TREE)
1525 code = build2(TRY_CATCH_EXPR, void_type_node, code,
1526 build2(CATCH_EXPR, void_type_node, NULL, except));
1527 if (fini != NULL_TREE)
1528 code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
1531 // Stick the code into the block we built for the receiver, if
1533 if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
1535 BIND_EXPR_BODY(bind) = code;
1539 DECL_SAVED_TREE(fndecl) = code;
1543 // Build the wrappers around function code needed if the function has
1544 // any defer statements. This sets *EXCEPT to an exception handler
1545 // and *FINI to a finally handler.
1548 Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
1549 tree *except, tree *fini)
1551 source_location end_loc = this->block_->end_location();
1553 // Add an exception handler. This is used if a panic occurs. Its
1554 // purpose is to stop the stack unwinding if a deferred function
1555 // calls recover. There are more details in
1556 // libgo/runtime/go-unwind.c.
1558 tree stmt_list = NULL_TREE;
1560 Expression* call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1561 this->defer_stack(end_loc));
1562 Translate_context context(gogo, named_function, NULL, NULL);
1563 tree call_tree = call->get_tree(&context);
1564 if (call_tree != error_mark_node)
1565 append_to_statement_list(call_tree, &stmt_list);
1567 tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
1569 if (retval == NULL_TREE)
1572 set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1573 DECL_RESULT(this->fndecl_), retval);
1574 tree ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1575 append_to_statement_list(ret_stmt, &stmt_list);
1577 go_assert(*except == NULL_TREE);
1578 *except = stmt_list;
1580 // Add some finally code to run the defer functions. This is used
1581 // both in the normal case, when no panic occurs, and also if a
1582 // panic occurs to run any further defer functions. Of course, it
1583 // is possible for a defer function to call panic which should be
1584 // caught by another defer function. To handle that we use a loop.
1586 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1587 // if (return values are named) return named_vals;
1591 tree label = create_artificial_label(end_loc);
1592 tree define_label = fold_build1_loc(end_loc, LABEL_EXPR, void_type_node,
1594 append_to_statement_list(define_label, &stmt_list);
1596 call = Runtime::make_call(Runtime::UNDEFER, end_loc, 1,
1597 this->defer_stack(end_loc));
1598 tree undefer = call->get_tree(&context);
1600 call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1601 this->defer_stack(end_loc));
1602 tree defer = call->get_tree(&context);
1604 if (undefer == error_mark_node || defer == error_mark_node)
1607 tree jump = fold_build1_loc(end_loc, GOTO_EXPR, void_type_node, label);
1608 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
1609 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
1610 tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
1612 append_to_statement_list(try_catch, &stmt_list);
1614 if (this->type_->results() != NULL
1615 && !this->type_->results()->empty()
1616 && !this->type_->results()->front().name().empty())
1618 // If the result variables are named, we need to return them
1619 // again, because they might have been changed by a defer
1621 retval = this->return_value(gogo, named_function, end_loc,
1623 set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1624 DECL_RESULT(this->fndecl_), retval);
1625 ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1626 append_to_statement_list(ret_stmt, &stmt_list);
1629 go_assert(*fini == NULL_TREE);
1633 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1634 // also add statements to STMT_LIST, which need to be executed before
1635 // the assignment. This is used for a return statement with no
1639 Function::return_value(Gogo* gogo, Named_object* named_function,
1640 source_location location, tree* stmt_list) const
1642 const Typed_identifier_list* results = this->type_->results();
1643 if (results == NULL || results->empty())
1646 go_assert(this->results_ != NULL);
1647 if (this->results_->size() != results->size())
1649 go_assert(saw_errors());
1650 return error_mark_node;
1654 if (results->size() == 1)
1657 this->results_->front()->get_backend_variable(gogo,
1659 tree ret = var_to_tree(bvar);
1660 if (this->results_->front()->result_var_value()->is_in_heap())
1661 ret = build_fold_indirect_ref_loc(location, ret);
1666 tree rettype = TREE_TYPE(DECL_RESULT(this->fndecl_));
1667 retval = create_tmp_var(rettype, "RESULT");
1668 tree field = TYPE_FIELDS(rettype);
1670 for (Typed_identifier_list::const_iterator pr = results->begin();
1671 pr != results->end();
1672 ++pr, ++index, field = DECL_CHAIN(field))
1674 go_assert(field != NULL);
1675 Named_object* no = (*this->results_)[index];
1676 Bvariable* bvar = no->get_backend_variable(gogo, named_function);
1677 tree val = var_to_tree(bvar);
1678 if (no->result_var_value()->is_in_heap())
1679 val = build_fold_indirect_ref_loc(location, val);
1680 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
1681 build3(COMPONENT_REF, TREE_TYPE(field),
1682 retval, field, NULL_TREE),
1684 append_to_statement_list(set, stmt_list);
1690 // Return the integer type to use for a size.
1694 go_type_for_size(unsigned int bits, int unsignedp)
1700 name = unsignedp ? "uint8" : "int8";
1703 name = unsignedp ? "uint16" : "int16";
1706 name = unsignedp ? "uint32" : "int32";
1709 name = unsignedp ? "uint64" : "int64";
1712 if (bits == POINTER_SIZE && unsignedp)
1717 Type* type = Type::lookup_integer_type(name);
1718 return type_to_tree(type->get_backend(go_get_gogo()));
1721 // Return the type to use for a mode.
1725 go_type_for_mode(enum machine_mode mode, int unsignedp)
1727 // FIXME: This static_cast should be in machmode.h.
1728 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
1730 return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
1731 else if (mc == MODE_FLOAT)
1734 switch (GET_MODE_BITSIZE (mode))
1737 type = Type::lookup_float_type("float32");
1740 type = Type::lookup_float_type("float64");
1743 // We have to check for long double in order to support
1744 // i386 excess precision.
1745 if (mode == TYPE_MODE(long_double_type_node))
1746 return long_double_type_node;
1749 return type_to_tree(type->get_backend(go_get_gogo()));
1751 else if (mc == MODE_COMPLEX_FLOAT)
1754 switch (GET_MODE_BITSIZE (mode))
1757 type = Type::lookup_complex_type("complex64");
1760 type = Type::lookup_complex_type("complex128");
1763 // We have to check for long double in order to support
1764 // i386 excess precision.
1765 if (mode == TYPE_MODE(complex_long_double_type_node))
1766 return complex_long_double_type_node;
1769 return type_to_tree(type->get_backend(go_get_gogo()));
1775 // Return a tree which allocates SIZE bytes which will holds value of
1779 Gogo::allocate_memory(Type* type, tree size, source_location location)
1781 // If the package imports unsafe, then it may play games with
1782 // pointers that look like integers.
1783 if (this->imported_unsafe_ || type->has_pointer())
1785 static tree new_fndecl;
1786 return Gogo::call_builtin(&new_fndecl,
1796 static tree new_nopointers_fndecl;
1797 return Gogo::call_builtin(&new_nopointers_fndecl,
1799 "__go_new_nopointers",
1807 // Build a builtin struct with a list of fields. The name is
1808 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
1809 // node; this exists so that the struct can have fields which point to
1810 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
1811 // are NFIELDS fields. Each field is a name (a const char*) followed
1812 // by a type (a tree).
1815 Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
1818 if (ptype != NULL && *ptype != NULL_TREE)
1822 va_start(ap, nfields);
1824 tree fields = NULL_TREE;
1825 for (int i = 0; i < nfields; ++i)
1827 const char* field_name = va_arg(ap, const char*);
1828 tree type = va_arg(ap, tree);
1829 if (type == error_mark_node)
1832 *ptype = error_mark_node;
1833 return error_mark_node;
1835 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
1836 get_identifier(field_name), type);
1837 DECL_CHAIN(field) = fields;
1843 if (struct_type == NULL_TREE)
1844 struct_type = make_node(RECORD_TYPE);
1845 finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
1849 go_preserve_from_gc(struct_type);
1850 *ptype = struct_type;
1856 // Return a type to use for pointer to const char for a string.
1859 Gogo::const_char_pointer_type_tree()
1862 if (type == NULL_TREE)
1864 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1866 type = build_pointer_type(const_char_type);
1867 go_preserve_from_gc(type);
1872 // Return a tree for a string constant.
1875 Gogo::string_constant_tree(const std::string& val)
1877 tree index_type = build_index_type(size_int(val.length()));
1878 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1880 tree string_type = build_array_type(const_char_type, index_type);
1881 string_type = build_variant_type_copy(string_type);
1882 TYPE_STRING_FLAG(string_type) = 1;
1883 tree string_val = build_string(val.length(), val.data());
1884 TREE_TYPE(string_val) = string_type;
1888 // Return a tree for a Go string constant.
1891 Gogo::go_string_constant_tree(const std::string& val)
1893 tree string_type = type_to_tree(Type::make_string_type()->get_backend(this));
1895 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
1897 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
1898 tree field = TYPE_FIELDS(string_type);
1899 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
1901 tree str = Gogo::string_constant_tree(val);
1902 elt->value = fold_convert(TREE_TYPE(field),
1903 build_fold_addr_expr(str));
1905 elt = VEC_quick_push(constructor_elt, init, NULL);
1906 field = DECL_CHAIN(field);
1907 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
1909 elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
1911 tree constructor = build_constructor(string_type, init);
1912 TREE_READONLY(constructor) = 1;
1913 TREE_CONSTANT(constructor) = 1;
1918 // Return a tree for a pointer to a Go string constant. This is only
1919 // used for type descriptors, so we return a pointer to a constant
1923 Gogo::ptr_go_string_constant_tree(const std::string& val)
1925 tree pval = this->go_string_constant_tree(val);
1927 tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
1928 create_tmp_var_name("SP"), TREE_TYPE(pval));
1929 DECL_EXTERNAL(decl) = 0;
1930 TREE_PUBLIC(decl) = 0;
1931 TREE_USED(decl) = 1;
1932 TREE_READONLY(decl) = 1;
1933 TREE_CONSTANT(decl) = 1;
1934 TREE_STATIC(decl) = 1;
1935 DECL_ARTIFICIAL(decl) = 1;
1936 DECL_INITIAL(decl) = pval;
1937 rest_of_decl_compilation(decl, 1, 0);
1939 return build_fold_addr_expr(decl);
1942 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
1943 // the slice. VALUES is the value pointer and COUNT is the number of
1944 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
1945 // the capacity and the count are the same.
1948 Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
1951 go_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
1953 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
1955 tree field = TYPE_FIELDS(slice_type_tree);
1956 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
1957 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
1959 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
1960 == TYPE_MAIN_VARIANT(TREE_TYPE(values)));
1961 elt->value = values;
1963 count = fold_convert(sizetype, count);
1964 if (capacity == NULL_TREE)
1966 count = save_expr(count);
1970 field = DECL_CHAIN(field);
1971 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
1972 elt = VEC_quick_push(constructor_elt, init, NULL);
1974 elt->value = fold_convert(TREE_TYPE(field), count);
1976 field = DECL_CHAIN(field);
1977 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
1978 elt = VEC_quick_push(constructor_elt, init, NULL);
1980 elt->value = fold_convert(TREE_TYPE(field), capacity);
1982 return build_constructor(slice_type_tree, init);
1985 // Build a map descriptor for a map of type MAPTYPE.
1988 Gogo::map_descriptor(Map_type* maptype)
1990 if (this->map_descriptors_ == NULL)
1991 this->map_descriptors_ = new Map_descriptors(10);
1993 std::pair<const Map_type*, tree> val(maptype, NULL);
1994 std::pair<Map_descriptors::iterator, bool> ins =
1995 this->map_descriptors_->insert(val);
1996 Map_descriptors::iterator p = ins.first;
1999 if (p->second == error_mark_node)
2000 return error_mark_node;
2001 go_assert(p->second != NULL_TREE && DECL_P(p->second));
2002 return build_fold_addr_expr(p->second);
2005 Type* keytype = maptype->key_type();
2006 Type* valtype = maptype->val_type();
2008 std::string mangled_name = ("__go_map_" + maptype->mangled_name(this));
2010 tree id = get_identifier_from_string(mangled_name);
2012 // Get the type of the map descriptor. This is __go_map_descriptor
2015 tree struct_type = this->map_descriptor_type();
2017 // The map entry type is a struct with three fields. This struct is
2018 // specific to MAPTYPE. Build it.
2020 tree map_entry_type = make_node(RECORD_TYPE);
2022 Btype* bkey_type = keytype->get_backend(this);
2023 Btype* bval_type = valtype->get_backend(this);
2024 map_entry_type = Gogo::builtin_struct(NULL, "__map", map_entry_type, 3,
2026 build_pointer_type(map_entry_type),
2028 type_to_tree(bkey_type),
2030 type_to_tree(bval_type));
2031 if (map_entry_type == error_mark_node)
2033 p->second = error_mark_node;
2034 return error_mark_node;
2037 tree map_entry_key_field = DECL_CHAIN(TYPE_FIELDS(map_entry_type));
2038 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field)),
2041 tree map_entry_val_field = DECL_CHAIN(map_entry_key_field);
2042 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field)),
2045 // Initialize the entries.
2047 tree map_descriptor_field = TYPE_FIELDS(struct_type);
2048 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field)),
2049 "__map_descriptor") == 0);
2050 tree entry_size_field = DECL_CHAIN(map_descriptor_field);
2051 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field)),
2052 "__entry_size") == 0);
2053 tree key_offset_field = DECL_CHAIN(entry_size_field);
2054 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field)),
2055 "__key_offset") == 0);
2056 tree val_offset_field = DECL_CHAIN(key_offset_field);
2057 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field)),
2058 "__val_offset") == 0);
2060 VEC(constructor_elt, gc)* descriptor = VEC_alloc(constructor_elt, gc, 6);
2062 constructor_elt* elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2063 elt->index = map_descriptor_field;
2064 elt->value = maptype->type_descriptor_pointer(this);
2066 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2067 elt->index = entry_size_field;
2068 elt->value = TYPE_SIZE_UNIT(map_entry_type);
2070 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2071 elt->index = key_offset_field;
2072 elt->value = byte_position(map_entry_key_field);
2074 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2075 elt->index = val_offset_field;
2076 elt->value = byte_position(map_entry_val_field);
2078 tree constructor = build_constructor(struct_type, descriptor);
2080 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, struct_type);
2081 TREE_STATIC(decl) = 1;
2082 TREE_USED(decl) = 1;
2083 TREE_READONLY(decl) = 1;
2084 TREE_CONSTANT(decl) = 1;
2085 DECL_INITIAL(decl) = constructor;
2086 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2087 resolve_unique_section(decl, 1, 0);
2089 rest_of_decl_compilation(decl, 1, 0);
2091 go_preserve_from_gc(decl);
2094 return build_fold_addr_expr(decl);
2097 // Return a tree for the type of a map descriptor. This is struct
2098 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
2102 Gogo::map_descriptor_type()
2104 static tree struct_type;
2105 Type* tdt = Type::make_type_descriptor_type();
2106 tree dtype = type_to_tree(tdt->get_backend(this));
2107 dtype = build_qualified_type(dtype, TYPE_QUAL_CONST);
2108 return Gogo::builtin_struct(&struct_type, "__go_map_descriptor", NULL_TREE,
2111 build_pointer_type(dtype),
2120 // Return the name to use for a type descriptor decl for TYPE. This
2121 // is used when TYPE does not have a name.
2124 Gogo::unnamed_type_descriptor_decl_name(const Type* type)
2126 return "__go_td_" + type->mangled_name(this);
2129 // Return the name to use for a type descriptor decl for a type named
2130 // NAME, defined in the function IN_FUNCTION. IN_FUNCTION will
2131 // normally be NULL.
2134 Gogo::type_descriptor_decl_name(const Named_object* no,
2135 const Named_object* in_function)
2137 std::string ret = "__go_tdn_";
2138 if (no->type_value()->is_builtin())
2139 go_assert(in_function == NULL);
2142 const std::string& unique_prefix(no->package() == NULL
2143 ? this->unique_prefix()
2144 : no->package()->unique_prefix());
2145 const std::string& package_name(no->package() == NULL
2146 ? this->package_name()
2147 : no->package()->name());
2148 ret.append(unique_prefix);
2150 ret.append(package_name);
2152 if (in_function != NULL)
2154 ret.append(Gogo::unpack_hidden_name(in_function->name()));
2158 ret.append(no->name());
2162 // Where a type descriptor decl should be defined.
2164 Gogo::Type_descriptor_location
2165 Gogo::type_descriptor_location(const Type* type)
2167 const Named_type* name = type->named_type();
2170 if (name->named_object()->package() != NULL)
2172 // This is a named type defined in a different package. The
2173 // descriptor should be defined in that package.
2174 return TYPE_DESCRIPTOR_UNDEFINED;
2176 else if (name->is_builtin())
2178 // We create the descriptor for a builtin type whenever we
2180 return TYPE_DESCRIPTOR_COMMON;
2184 // This is a named type defined in this package. The
2185 // descriptor should be defined here.
2186 return TYPE_DESCRIPTOR_DEFINED;
2191 if (type->points_to() != NULL
2192 && type->points_to()->named_type() != NULL
2193 && type->points_to()->named_type()->named_object()->package() != NULL)
2195 // This is an unnamed pointer to a named type defined in a
2196 // different package. The descriptor should be defined in
2198 return TYPE_DESCRIPTOR_UNDEFINED;
2202 // This is an unnamed type. The descriptor could be defined
2203 // in any package where it is needed, and the linker will
2204 // pick one descriptor to keep.
2205 return TYPE_DESCRIPTOR_COMMON;
2210 // Build a type descriptor decl for TYPE. INITIALIZER is a struct
2211 // composite literal which initializers the type descriptor.
2214 Gogo::build_type_descriptor_decl(const Type* type, Expression* initializer,
2217 const Named_type* name = type->named_type();
2219 // We can have multiple instances of unnamed types, but we only want
2220 // to emit the type descriptor once. We use a hash table to handle
2221 // this. This is not necessary for named types, as they are unique,
2222 // and we store the type descriptor decl in the type itself.
2226 if (this->type_descriptor_decls_ == NULL)
2227 this->type_descriptor_decls_ = new Type_descriptor_decls(10);
2229 std::pair<Type_descriptor_decls::iterator, bool> ins =
2230 this->type_descriptor_decls_->insert(std::make_pair(type, NULL_TREE));
2233 // We've already built a type descriptor for this type.
2234 *pdecl = ins.first->second;
2237 phash = &ins.first->second;
2240 std::string decl_name;
2242 decl_name = this->unnamed_type_descriptor_decl_name(type);
2244 decl_name = this->type_descriptor_decl_name(name->named_object(),
2245 name->in_function());
2246 tree id = get_identifier_from_string(decl_name);
2247 Type* init_type = initializer->type();
2248 tree descriptor_type_tree = type_to_tree(init_type->get_backend(this));
2249 if (descriptor_type_tree == error_mark_node)
2251 *pdecl = error_mark_node;
2254 tree decl = build_decl(name == NULL ? BUILTINS_LOCATION : name->location(),
2256 build_qualified_type(descriptor_type_tree,
2258 TREE_READONLY(decl) = 1;
2259 TREE_CONSTANT(decl) = 1;
2260 DECL_ARTIFICIAL(decl) = 1;
2262 go_preserve_from_gc(decl);
2266 // We store the new DECL now because we may need to refer to it when
2267 // expanding INITIALIZER.
2270 // If appropriate, just refer to the exported type identifier.
2271 Gogo::Type_descriptor_location type_descriptor_location =
2272 this->type_descriptor_location(type);
2273 if (type_descriptor_location == TYPE_DESCRIPTOR_UNDEFINED)
2275 TREE_PUBLIC(decl) = 1;
2276 DECL_EXTERNAL(decl) = 1;
2280 TREE_STATIC(decl) = 1;
2281 TREE_USED(decl) = 1;
2283 Translate_context context(this, NULL, NULL, NULL);
2284 context.set_is_const();
2285 tree constructor = initializer->get_tree(&context);
2287 if (constructor == error_mark_node)
2288 go_assert(saw_errors());
2290 DECL_INITIAL(decl) = constructor;
2292 if (type_descriptor_location == TYPE_DESCRIPTOR_DEFINED)
2293 TREE_PUBLIC(decl) = 1;
2296 go_assert(type_descriptor_location == TYPE_DESCRIPTOR_COMMON);
2297 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2298 resolve_unique_section(decl, 1, 0);
2301 rest_of_decl_compilation(decl, 1, 0);
2304 // Build an interface method table for a type: a list of function
2305 // pointers, one for each interface method. This is used for
2309 Gogo::interface_method_table_for_type(const Interface_type* interface,
2313 const Typed_identifier_list* interface_methods = interface->methods();
2314 go_assert(!interface_methods->empty());
2316 std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
2317 + interface->mangled_name(this)
2319 + type->mangled_name(this));
2321 tree id = get_identifier_from_string(mangled_name);
2323 // See whether this interface has any hidden methods.
2324 bool has_hidden_methods = false;
2325 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2326 p != interface_methods->end();
2329 if (Gogo::is_hidden_name(p->name()))
2331 has_hidden_methods = true;
2336 // We already know that the named type is convertible to the
2337 // interface. If the interface has hidden methods, and the named
2338 // type is defined in a different package, then the interface
2339 // conversion table will be defined by that other package.
2340 if (has_hidden_methods && type->named_object()->package() != NULL)
2342 tree array_type = build_array_type(const_ptr_type_node, NULL);
2343 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2344 TREE_READONLY(decl) = 1;
2345 TREE_CONSTANT(decl) = 1;
2346 TREE_PUBLIC(decl) = 1;
2347 DECL_EXTERNAL(decl) = 1;
2348 go_preserve_from_gc(decl);
2352 size_t count = interface_methods->size();
2353 VEC(constructor_elt, gc)* pointers = VEC_alloc(constructor_elt, gc,
2356 // The first element is the type descriptor.
2357 constructor_elt* elt = VEC_quick_push(constructor_elt, pointers, NULL);
2358 elt->index = size_zero_node;
2363 td_type = Type::make_pointer_type(type);
2364 elt->value = fold_convert(const_ptr_type_node,
2365 td_type->type_descriptor_pointer(this));
2368 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2369 p != interface_methods->end();
2373 Method* m = type->method_function(p->name(), &is_ambiguous);
2374 go_assert(m != NULL);
2376 Named_object* no = m->named_object();
2378 tree fnid = no->get_id(this);
2381 if (no->is_function())
2382 fndecl = no->func_value()->get_or_make_decl(this, no, fnid);
2383 else if (no->is_function_declaration())
2384 fndecl = no->func_declaration_value()->get_or_make_decl(this, no,
2388 fndecl = build_fold_addr_expr(fndecl);
2390 elt = VEC_quick_push(constructor_elt, pointers, NULL);
2391 elt->index = size_int(i);
2392 elt->value = fold_convert(const_ptr_type_node, fndecl);
2394 go_assert(i == count + 1);
2396 tree array_type = build_array_type(const_ptr_type_node,
2397 build_index_type(size_int(count)));
2398 tree constructor = build_constructor(array_type, pointers);
2400 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2401 TREE_STATIC(decl) = 1;
2402 TREE_USED(decl) = 1;
2403 TREE_READONLY(decl) = 1;
2404 TREE_CONSTANT(decl) = 1;
2405 DECL_INITIAL(decl) = constructor;
2407 // If the interface type has hidden methods, then this is the only
2408 // definition of the table. Otherwise it is a comdat table which
2409 // may be defined in multiple packages.
2410 if (has_hidden_methods)
2411 TREE_PUBLIC(decl) = 1;
2414 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2415 resolve_unique_section(decl, 1, 0);
2418 rest_of_decl_compilation(decl, 1, 0);
2420 go_preserve_from_gc(decl);
2425 // Mark a function as a builtin library function.
2428 Gogo::mark_fndecl_as_builtin_library(tree fndecl)
2430 DECL_EXTERNAL(fndecl) = 1;
2431 TREE_PUBLIC(fndecl) = 1;
2432 DECL_ARTIFICIAL(fndecl) = 1;
2433 TREE_NOTHROW(fndecl) = 1;
2434 DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
2435 DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
2438 // Build a call to a builtin function.
2441 Gogo::call_builtin(tree* pdecl, source_location location, const char* name,
2442 int nargs, tree rettype, ...)
2444 if (rettype == error_mark_node)
2445 return error_mark_node;
2447 tree* types = new tree[nargs];
2448 tree* args = new tree[nargs];
2451 va_start(ap, rettype);
2452 for (int i = 0; i < nargs; ++i)
2454 types[i] = va_arg(ap, tree);
2455 args[i] = va_arg(ap, tree);
2456 if (types[i] == error_mark_node || args[i] == error_mark_node)
2460 return error_mark_node;
2465 if (*pdecl == NULL_TREE)
2467 tree fnid = get_identifier(name);
2469 tree argtypes = NULL_TREE;
2470 tree* pp = &argtypes;
2471 for (int i = 0; i < nargs; ++i)
2473 *pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
2474 pp = &TREE_CHAIN(*pp);
2476 *pp = void_list_node;
2478 tree fntype = build_function_type(rettype, argtypes);
2480 *pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
2481 Gogo::mark_fndecl_as_builtin_library(*pdecl);
2482 go_preserve_from_gc(*pdecl);
2485 tree fnptr = build_fold_addr_expr(*pdecl);
2486 if (CAN_HAVE_LOCATION_P(fnptr))
2487 SET_EXPR_LOCATION(fnptr, location);
2489 tree ret = build_call_array(rettype, fnptr, nargs, args);
2490 SET_EXPR_LOCATION(ret, location);
2498 // Build a call to the runtime error function.
2501 Gogo::runtime_error(int code, source_location location)
2503 static tree runtime_error_fndecl;
2504 tree ret = Gogo::call_builtin(&runtime_error_fndecl,
2506 "__go_runtime_error",
2510 build_int_cst(integer_type_node, code));
2511 if (ret == error_mark_node)
2512 return error_mark_node;
2513 // The runtime error function panics and does not return.
2514 TREE_NOTHROW(runtime_error_fndecl) = 0;
2515 TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
2519 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2520 // This does a blocking receive and returns the value read from the
2521 // channel. If FOR_SELECT is true, this is being done because it was
2522 // chosen in a select statement.
2525 Gogo::receive_from_channel(tree type_tree, tree channel, bool for_select,
2526 source_location location)
2528 if (type_tree == error_mark_node || channel == error_mark_node)
2529 return error_mark_node;
2531 if (int_size_in_bytes(type_tree) <= 8
2532 && !AGGREGATE_TYPE_P(type_tree)
2533 && !FLOAT_TYPE_P(type_tree))
2535 static tree receive_small_fndecl;
2536 tree call = Gogo::call_builtin(&receive_small_fndecl,
2538 "__go_receive_small",
2546 : boolean_false_node));
2547 if (call == error_mark_node)
2548 return error_mark_node;
2549 // This can panic if there are too many operations on a closed
2551 TREE_NOTHROW(receive_small_fndecl) = 0;
2552 int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
2553 tree int_type_tree = go_type_for_size(bitsize, 1);
2554 return fold_convert_loc(location, type_tree,
2555 fold_convert_loc(location, int_type_tree,
2560 tree tmp = create_tmp_var(type_tree, get_name(type_tree));
2561 DECL_IGNORED_P(tmp) = 0;
2562 TREE_ADDRESSABLE(tmp) = 1;
2563 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
2564 SET_EXPR_LOCATION(make_tmp, location);
2565 tree tmpaddr = build_fold_addr_expr(tmp);
2566 tmpaddr = fold_convert(ptr_type_node, tmpaddr);
2567 static tree receive_big_fndecl;
2568 tree call = Gogo::call_builtin(&receive_big_fndecl,
2580 : boolean_false_node));
2581 if (call == error_mark_node)
2582 return error_mark_node;
2583 // This can panic if there are too many operations on a closed
2585 TREE_NOTHROW(receive_big_fndecl) = 0;
2586 return build2(COMPOUND_EXPR, type_tree, make_tmp,
2587 build2(COMPOUND_EXPR, type_tree, call, tmp));
2591 // Return the type of a function trampoline. This is like
2592 // get_trampoline_type in tree-nested.c.
2595 Gogo::trampoline_type_tree()
2597 static tree type_tree;
2598 if (type_tree == NULL_TREE)
2602 go_trampoline_info(&size, &align);
2603 tree t = build_index_type(build_int_cst(integer_type_node, size - 1));
2604 t = build_array_type(char_type_node, t);
2606 type_tree = Gogo::builtin_struct(NULL, "__go_trampoline", NULL_TREE, 1,
2608 t = TYPE_FIELDS(type_tree);
2609 DECL_ALIGN(t) = align;
2610 DECL_USER_ALIGN(t) = 1;
2612 go_preserve_from_gc(type_tree);
2617 // Make a trampoline which calls FNADDR passing CLOSURE.
2620 Gogo::make_trampoline(tree fnaddr, tree closure, source_location location)
2622 tree trampoline_type = Gogo::trampoline_type_tree();
2623 tree trampoline_size = TYPE_SIZE_UNIT(trampoline_type);
2625 closure = save_expr(closure);
2627 // We allocate the trampoline using a special function which will
2628 // mark it as executable.
2629 static tree trampoline_fndecl;
2630 tree x = Gogo::call_builtin(&trampoline_fndecl,
2632 "__go_allocate_trampoline",
2638 fold_convert_loc(location, ptr_type_node,
2640 if (x == error_mark_node)
2641 return error_mark_node;
2645 // Initialize the trampoline.
2646 tree ini = build_call_expr(implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE],
2647 3, x, fnaddr, closure);
2649 // On some targets the trampoline address needs to be adjusted. For
2650 // example, when compiling in Thumb mode on the ARM, the address
2651 // needs to have the low bit set.
2652 x = build_call_expr(implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
2654 x = fold_convert(TREE_TYPE(fnaddr), x);
2656 return build2(COMPOUND_EXPR, TREE_TYPE(x), ini, x);