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);
499 current_function_decl
= NULL_TREE
;
503 // Search for references to VAR in any statements or called functions.
505 class Find_var
: public Traverse
508 // A hash table we use to avoid looping. The index is the name of a
509 // named object. We only look through objects defined in this
511 typedef Unordered_set(std::string
) Seen_objects
;
513 Find_var(Named_object
* var
, Seen_objects
* seen_objects
)
514 : Traverse(traverse_expressions
),
515 var_(var
), seen_objects_(seen_objects
), found_(false)
518 // Whether the variable was found.
521 { return this->found_
; }
524 expression(Expression
**);
527 // The variable we are looking for.
529 // Names of objects we have already seen.
530 Seen_objects
* seen_objects_
;
531 // True if the variable was found.
535 // See if EXPR refers to VAR, looking through function calls and
536 // variable initializations.
539 Find_var::expression(Expression
** pexpr
)
541 Expression
* e
= *pexpr
;
543 Var_expression
* ve
= e
->var_expression();
546 Named_object
* v
= ve
->named_object();
550 return TRAVERSE_EXIT
;
553 if (v
->is_variable() && v
->package() == NULL
)
555 Expression
* init
= v
->var_value()->init();
558 std::pair
<Seen_objects::iterator
, bool> ins
=
559 this->seen_objects_
->insert(v
->name());
562 // This is the first time we have seen this name.
563 if (Expression::traverse(&init
, this) == TRAVERSE_EXIT
)
564 return TRAVERSE_EXIT
;
570 // We traverse the code of any function we see. Note that this
571 // means that we will traverse the code of a function whose address
572 // is taken even if it is not called.
573 Func_expression
* fe
= e
->func_expression();
576 const Named_object
* f
= fe
->named_object();
577 if (f
->is_function() && f
->package() == NULL
)
579 std::pair
<Seen_objects::iterator
, bool> ins
=
580 this->seen_objects_
->insert(f
->name());
583 // This is the first time we have seen this name.
584 if (f
->func_value()->block()->traverse(this) == TRAVERSE_EXIT
)
585 return TRAVERSE_EXIT
;
590 return TRAVERSE_CONTINUE
;
593 // Return true if EXPR refers to VAR.
596 expression_requires(Expression
* expr
, Block
* preinit
, Named_object
* var
)
598 Find_var::Seen_objects seen_objects
;
599 Find_var
find_var(var
, &seen_objects
);
601 Expression::traverse(&expr
, &find_var
);
603 preinit
->traverse(&find_var
);
605 return find_var
.found();
608 // Sort variable initializations. If the initialization expression
609 // for variable A refers directly or indirectly to the initialization
610 // expression for variable B, then we must initialize B before A.
616 : var_(NULL
), init_(NULL_TREE
), waiting_(0)
619 Var_init(Named_object
* var
, tree init
)
620 : var_(var
), init_(init
), waiting_(0)
623 // Return the variable.
626 { return this->var_
; }
628 // Return the initialization expression.
631 { return this->init_
; }
633 // Return the number of variables waiting for this one to be
637 { return this->waiting_
; }
639 // Increment the number waiting.
642 { ++this->waiting_
; }
645 // The variable being initialized.
647 // The initialization expression to run.
649 // The number of variables which are waiting for this one.
653 typedef std::list
<Var_init
> Var_inits
;
655 // Sort the variable initializations. The rule we follow is that we
656 // emit them in the order they appear in the array, except that if the
657 // initialization expression for a variable V1 depends upon another
658 // variable V2 then we initialize V1 after V2.
661 sort_var_inits(Var_inits
* var_inits
)
664 while (!var_inits
->empty())
666 Var_inits::iterator p1
= var_inits
->begin();
667 Named_object
* var
= p1
->var();
668 Expression
* init
= var
->var_value()->init();
669 Block
* preinit
= var
->var_value()->preinit();
671 // Start walking through the list to see which variables VAR
672 // needs to wait for. We can skip P1->WAITING variables--that
673 // is the number we've already checked.
674 Var_inits::iterator p2
= p1
;
676 for (size_t i
= p1
->waiting(); i
> 0; --i
)
679 for (; p2
!= var_inits
->end(); ++p2
)
681 if (expression_requires(init
, preinit
, p2
->var()))
684 if (expression_requires(p2
->var()->var_value()->init(),
685 p2
->var()->var_value()->preinit(),
688 error_at(var
->location(),
689 ("initialization expressions for %qs and "
690 "%qs depend upon each other"),
691 var
->message_name().c_str(),
692 p2
->var()->message_name().c_str());
693 inform(p2
->var()->location(), "%qs defined here",
694 p2
->var()->message_name().c_str());
695 p2
= var_inits
->end();
699 // We can't emit P1 until P2 is emitted. Move P1.
700 // Note that the WAITING loop always executes at
701 // least once, which is what we want.
702 p2
->increment_waiting();
703 Var_inits::iterator p3
= p2
;
704 for (size_t i
= p2
->waiting(); i
> 0; --i
)
706 var_inits
->splice(p3
, *var_inits
, p1
);
712 if (p2
== var_inits
->end())
714 // VAR does not depends upon any other initialization expressions.
716 // Check for a loop of VAR on itself. We only do this if
717 // INIT is not NULL; when INIT is NULL, it means that
718 // PREINIT sets VAR, which we will interpret as a loop.
719 if (init
!= NULL
&& expression_requires(init
, preinit
, var
))
720 error_at(var
->location(),
721 "initialization expression for %qs depends upon itself",
722 var
->message_name().c_str());
723 ready
.splice(ready
.end(), *var_inits
, p1
);
727 // Now READY is the list in the desired initialization order.
728 var_inits
->swap(ready
);
731 // Write out the global definitions.
734 Gogo::write_globals()
736 this->convert_named_types();
737 this->build_interface_method_tables();
739 Bindings
* bindings
= this->current_bindings();
740 size_t count_definitions
= bindings
->size_definitions();
741 size_t count
= count_definitions
;
743 tree
* vec
= new tree
[count
];
745 tree init_fndecl
= NULL_TREE
;
746 tree init_stmt_list
= NULL_TREE
;
748 if (this->is_main_package())
749 this->init_imports(&init_stmt_list
);
751 // A list of variable initializations.
754 // A list of variables which need to be registered with the garbage
756 std::vector
<Named_object
*> var_gc
;
757 var_gc
.reserve(count
);
759 tree var_init_stmt_list
= NULL_TREE
;
761 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
762 p
!= bindings
->end_definitions();
765 Named_object
* no
= *p
;
767 go_assert(!no
->is_type_declaration() && !no
->is_function_declaration());
768 // There is nothing to do for a package.
769 if (no
->is_package())
776 // There is nothing to do for an object which was imported from
777 // a different package into the global scope.
778 if (no
->package() != NULL
)
785 // There is nothing useful we can output for constants which
786 // have ideal or non-integeral type.
789 Type
* type
= no
->const_value()->type();
791 type
= no
->const_value()->expr()->type();
792 if (type
->is_abstract() || type
->integer_type() == NULL
)
800 if (!no
->is_variable())
802 vec
[i
] = no
->get_tree(this, NULL
);
803 if (vec
[i
] == error_mark_node
)
805 go_assert(saw_errors());
813 Bvariable
* var
= no
->get_backend_variable(this, NULL
);
814 vec
[i
] = var_to_tree(var
);
815 if (vec
[i
] == error_mark_node
)
817 go_assert(saw_errors());
823 // Check for a sink variable, which may be used to run an
824 // initializer purely for its side effects.
825 bool is_sink
= no
->name()[0] == '_' && no
->name()[1] == '.';
827 tree var_init_tree
= NULL_TREE
;
828 if (!no
->var_value()->has_pre_init())
830 tree init
= no
->var_value()->get_init_tree(this, NULL
);
831 if (init
== error_mark_node
)
832 go_assert(saw_errors());
833 else if (init
== NULL_TREE
)
835 else if (TREE_CONSTANT(init
))
837 if (expression_requires(no
->var_value()->init(), NULL
, no
))
838 error_at(no
->location(),
839 "initialization expression for %qs depends "
841 no
->message_name().c_str());
842 this->backend()->global_variable_set_init(var
,
846 || int_size_in_bytes(TREE_TYPE(init
)) == 0
847 || int_size_in_bytes(TREE_TYPE(vec
[i
])) == 0)
848 var_init_tree
= init
;
850 var_init_tree
= fold_build2_loc(no
->location().gcc_location(),
851 MODIFY_EXPR
, void_type_node
,
856 // We are going to create temporary variables which
857 // means that we need an fndecl.
858 if (init_fndecl
== NULL_TREE
)
859 init_fndecl
= this->initialization_function_decl();
860 current_function_decl
= init_fndecl
;
861 if (DECL_STRUCT_FUNCTION(init_fndecl
) == NULL
)
862 push_struct_function(init_fndecl
);
864 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl
));
866 tree var_decl
= is_sink
? NULL_TREE
: vec
[i
];
867 var_init_tree
= no
->var_value()->get_init_block(this, NULL
,
870 current_function_decl
= NULL_TREE
;
874 if (var_init_tree
!= NULL_TREE
&& var_init_tree
!= error_mark_node
)
876 if (no
->var_value()->init() == NULL
877 && !no
->var_value()->has_pre_init())
878 append_to_statement_list(var_init_tree
, &var_init_stmt_list
);
880 var_inits
.push_back(Var_init(no
, var_init_tree
));
883 if (!is_sink
&& no
->var_value()->type()->has_pointer())
884 var_gc
.push_back(no
);
888 // Register global variables with the garbage collector.
889 this->register_gc_vars(var_gc
, &init_stmt_list
);
891 // Simple variable initializations, after all variables are
893 append_to_statement_list(var_init_stmt_list
, &init_stmt_list
);
895 // Complex variable initializations, first sorting them into a
897 if (!var_inits
.empty())
899 sort_var_inits(&var_inits
);
900 for (Var_inits::const_iterator p
= var_inits
.begin();
901 p
!= var_inits
.end();
903 append_to_statement_list(p
->init(), &init_stmt_list
);
906 // After all the variables are initialized, call the "init"
907 // functions if there are any.
908 for (std::vector
<Named_object
*>::const_iterator p
=
909 this->init_functions_
.begin();
910 p
!= this->init_functions_
.end();
913 tree decl
= (*p
)->get_tree(this, NULL
);
914 tree call
= build_call_expr(decl
, 0);
915 append_to_statement_list(call
, &init_stmt_list
);
918 // Set up a magic function to do all the initialization actions.
919 // This will be called if this package is imported.
920 if (init_stmt_list
!= NULL_TREE
921 || this->need_init_fn_
922 || this->is_main_package())
923 this->write_initialization_function(init_fndecl
, init_stmt_list
);
925 // We should not have seen any new bindings created during the
927 go_assert(count_definitions
== this->current_bindings()->size_definitions());
929 // Pass everything back to the middle-end.
931 wrapup_global_declarations(vec
, count
);
933 finalize_compilation_unit();
935 check_global_declarations(vec
, count
);
936 emit_debug_global_declarations(vec
, count
);
941 // Get a tree for the identifier for a named object.
944 Named_object::get_id(Gogo
* gogo
)
946 go_assert(!this->is_variable() && !this->is_result_variable());
947 std::string decl_name
;
948 if (this->is_function_declaration()
949 && !this->func_declaration_value()->asm_name().empty())
950 decl_name
= this->func_declaration_value()->asm_name();
951 else if (this->is_type()
952 && Linemap::is_predeclared_location(this->type_value()->location()))
954 // We don't need the package name for builtin types.
955 decl_name
= Gogo::unpack_hidden_name(this->name_
);
959 std::string package_name
;
960 if (this->package_
== NULL
)
961 package_name
= gogo
->package_name();
963 package_name
= this->package_
->name();
965 decl_name
= package_name
+ '.' + Gogo::unpack_hidden_name(this->name_
);
967 Function_type
* fntype
;
968 if (this->is_function())
969 fntype
= this->func_value()->type();
970 else if (this->is_function_declaration())
971 fntype
= this->func_declaration_value()->type();
974 if (fntype
!= NULL
&& fntype
->is_method())
976 decl_name
.push_back('.');
977 decl_name
.append(fntype
->receiver()->type()->mangled_name(gogo
));
982 const Named_object
* in_function
= this->type_value()->in_function();
983 if (in_function
!= NULL
)
984 decl_name
+= '$' + in_function
->name();
986 return get_identifier_from_string(decl_name
);
989 // Get a tree for a named object.
992 Named_object::get_tree(Gogo
* gogo
, Named_object
* function
)
994 if (this->tree_
!= NULL_TREE
)
998 if (this->classification_
== NAMED_OBJECT_TYPE
)
1001 name
= this->get_id(gogo
);
1003 switch (this->classification_
)
1005 case NAMED_OBJECT_CONST
:
1007 Named_constant
* named_constant
= this->u_
.const_value
;
1008 Translate_context
subcontext(gogo
, function
, NULL
, NULL
);
1009 tree expr_tree
= named_constant
->expr()->get_tree(&subcontext
);
1010 if (expr_tree
== error_mark_node
)
1011 decl
= error_mark_node
;
1014 Type
* type
= named_constant
->type();
1015 if (type
!= NULL
&& !type
->is_abstract())
1017 if (type
->is_error())
1018 expr_tree
= error_mark_node
;
1021 Btype
* btype
= type
->get_backend(gogo
);
1022 expr_tree
= fold_convert(type_to_tree(btype
), expr_tree
);
1025 if (expr_tree
== error_mark_node
)
1026 decl
= error_mark_node
;
1027 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
1029 decl
= build_decl(named_constant
->location().gcc_location(),
1030 CONST_DECL
, name
, TREE_TYPE(expr_tree
));
1031 DECL_INITIAL(decl
) = expr_tree
;
1032 TREE_CONSTANT(decl
) = 1;
1033 TREE_READONLY(decl
) = 1;
1037 // A CONST_DECL is only for an enum constant, so we
1038 // shouldn't use for non-integral types. Instead we
1039 // just return the constant itself, rather than a
1047 case NAMED_OBJECT_TYPE
:
1049 Named_type
* named_type
= this->u_
.type_value
;
1050 tree type_tree
= type_to_tree(named_type
->get_backend(gogo
));
1051 if (type_tree
== error_mark_node
)
1052 decl
= error_mark_node
;
1055 decl
= TYPE_NAME(type_tree
);
1056 go_assert(decl
!= NULL_TREE
);
1058 // We need to produce a type descriptor for every named
1059 // type, and for a pointer to every named type, since
1060 // other files or packages might refer to them. We need
1061 // to do this even for hidden types, because they might
1062 // still be returned by some function. Simply calling the
1063 // type_descriptor method is enough to create the type
1064 // descriptor, even though we don't do anything with it.
1065 if (this->package_
== NULL
)
1068 type_descriptor_pointer(gogo
,
1069 Linemap::predeclared_location());
1070 Type
* pn
= Type::make_pointer_type(named_type
);
1071 pn
->type_descriptor_pointer(gogo
,
1072 Linemap::predeclared_location());
1078 case NAMED_OBJECT_TYPE_DECLARATION
:
1079 error("reference to undefined type %qs",
1080 this->message_name().c_str());
1081 return error_mark_node
;
1083 case NAMED_OBJECT_VAR
:
1084 case NAMED_OBJECT_RESULT_VAR
:
1085 case NAMED_OBJECT_SINK
:
1088 case NAMED_OBJECT_FUNC
:
1090 Function
* func
= this->u_
.func_value
;
1091 decl
= func
->get_or_make_decl(gogo
, this, name
);
1092 if (decl
!= error_mark_node
)
1094 if (func
->block() != NULL
)
1096 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
1097 push_struct_function(decl
);
1099 push_cfun(DECL_STRUCT_FUNCTION(decl
));
1101 cfun
->function_end_locus
=
1102 func
->block()->end_location().gcc_location();
1104 current_function_decl
= decl
;
1106 func
->build_tree(gogo
, this);
1108 gimplify_function_tree(decl
);
1110 cgraph_finalize_function(decl
, true);
1112 current_function_decl
= NULL_TREE
;
1119 case NAMED_OBJECT_ERRONEOUS
:
1120 decl
= error_mark_node
;
1127 if (TREE_TYPE(decl
) == error_mark_node
)
1128 decl
= error_mark_node
;
1134 if (ret
!= error_mark_node
)
1135 go_preserve_from_gc(ret
);
1140 // Get the initial value of a variable as a tree. This does not
1141 // consider whether the variable is in the heap--it returns the
1142 // initial value as though it were always stored in the stack.
1145 Variable::get_init_tree(Gogo
* gogo
, Named_object
* function
)
1147 go_assert(this->preinit_
== NULL
);
1148 if (this->init_
== NULL
)
1150 go_assert(!this->is_parameter_
);
1151 if (this->is_global_
|| this->is_in_heap())
1153 Btype
* btype
= this->type_
->get_backend(gogo
);
1154 return expr_to_tree(gogo
->backend()->zero_expression(btype
));
1158 Translate_context
context(gogo
, function
, NULL
, NULL
);
1159 tree rhs_tree
= this->init_
->get_tree(&context
);
1160 return Expression::convert_for_assignment(&context
, this->type(),
1161 this->init_
->type(),
1162 rhs_tree
, this->location());
1166 // Get the initial value of a variable when a block is required.
1167 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1170 Variable::get_init_block(Gogo
* gogo
, Named_object
* function
, tree var_decl
)
1172 go_assert(this->preinit_
!= NULL
);
1174 // We want to add the variable assignment to the end of the preinit
1175 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1176 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1177 // regular statements.
1179 Translate_context
context(gogo
, function
, NULL
, NULL
);
1180 Bblock
* bblock
= this->preinit_
->get_backend(&context
);
1181 tree block_tree
= block_to_tree(bblock
);
1182 if (block_tree
== error_mark_node
)
1183 return error_mark_node
;
1184 go_assert(TREE_CODE(block_tree
) == BIND_EXPR
);
1185 tree statements
= BIND_EXPR_BODY(block_tree
);
1186 while (statements
!= NULL_TREE
1187 && (TREE_CODE(statements
) == TRY_FINALLY_EXPR
1188 || TREE_CODE(statements
) == TRY_CATCH_EXPR
))
1189 statements
= TREE_OPERAND(statements
, 0);
1191 // It's possible to have pre-init statements without an initializer
1192 // if the pre-init statements set the variable.
1193 if (this->init_
!= NULL
)
1195 tree rhs_tree
= this->init_
->get_tree(&context
);
1196 if (rhs_tree
== error_mark_node
)
1197 return error_mark_node
;
1198 if (var_decl
== NULL_TREE
)
1199 append_to_statement_list(rhs_tree
, &statements
);
1202 tree val
= Expression::convert_for_assignment(&context
, this->type(),
1203 this->init_
->type(),
1206 if (val
== error_mark_node
)
1207 return error_mark_node
;
1208 tree set
= fold_build2_loc(this->location().gcc_location(),
1209 MODIFY_EXPR
, void_type_node
, var_decl
,
1211 append_to_statement_list(set
, &statements
);
1218 // Get a tree for a function decl.
1221 Function::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1223 if (this->fndecl_
== NULL_TREE
)
1225 tree functype
= type_to_tree(this->type_
->get_backend(gogo
));
1226 if (functype
== error_mark_node
)
1227 this->fndecl_
= error_mark_node
;
1230 // The type of a function comes back as a pointer, but we
1231 // want the real function type for a function declaration.
1232 go_assert(POINTER_TYPE_P(functype
));
1233 functype
= TREE_TYPE(functype
);
1234 tree decl
= build_decl(this->location().gcc_location(), FUNCTION_DECL
,
1237 this->fndecl_
= decl
;
1239 if (no
->package() != NULL
)
1241 else if (this->enclosing_
!= NULL
|| Gogo::is_thunk(no
))
1243 else if (Gogo::unpack_hidden_name(no
->name()) == "init"
1244 && !this->type_
->is_method())
1246 else if (Gogo::unpack_hidden_name(no
->name()) == "main"
1247 && gogo
->is_main_package())
1248 TREE_PUBLIC(decl
) = 1;
1249 // Methods have to be public even if they are hidden because
1250 // they can be pulled into type descriptors when using
1251 // anonymous fields.
1252 else if (!Gogo::is_hidden_name(no
->name())
1253 || this->type_
->is_method())
1255 TREE_PUBLIC(decl
) = 1;
1256 std::string asm_name
= gogo
->unique_prefix();
1257 asm_name
.append(1, '.');
1258 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1259 SET_DECL_ASSEMBLER_NAME(decl
,
1260 get_identifier_from_string(asm_name
));
1263 // Why do we have to do this in the frontend?
1264 tree restype
= TREE_TYPE(functype
);
1266 build_decl(this->location().gcc_location(), RESULT_DECL
, NULL_TREE
,
1268 DECL_ARTIFICIAL(resdecl
) = 1;
1269 DECL_IGNORED_P(resdecl
) = 1;
1270 DECL_CONTEXT(resdecl
) = decl
;
1271 DECL_RESULT(decl
) = resdecl
;
1273 if (this->enclosing_
!= NULL
)
1274 DECL_STATIC_CHAIN(decl
) = 1;
1276 // If a function calls the predeclared recover function, we
1277 // can't inline it, because recover behaves differently in a
1278 // function passed directly to defer. If this is a recover
1279 // thunk that we built to test whether a function can be
1280 // recovered, we can't inline it, because that will mess up
1281 // our return address comparison.
1282 if (this->calls_recover_
|| this->is_recover_thunk_
)
1283 DECL_UNINLINABLE(decl
) = 1;
1285 // If this is a thunk created to call a function which calls
1286 // the predeclared recover function, we need to disable
1287 // stack splitting for the thunk.
1288 if (this->is_recover_thunk_
)
1290 tree attr
= get_identifier("__no_split_stack__");
1291 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1294 go_preserve_from_gc(decl
);
1296 if (this->closure_var_
!= NULL
)
1298 push_struct_function(decl
);
1300 Bvariable
* bvar
= this->closure_var_
->get_backend_variable(gogo
,
1302 tree closure_decl
= var_to_tree(bvar
);
1303 if (closure_decl
== error_mark_node
)
1304 this->fndecl_
= error_mark_node
;
1307 DECL_ARTIFICIAL(closure_decl
) = 1;
1308 DECL_IGNORED_P(closure_decl
) = 1;
1309 TREE_USED(closure_decl
) = 1;
1310 DECL_ARG_TYPE(closure_decl
) = TREE_TYPE(closure_decl
);
1311 TREE_READONLY(closure_decl
) = 1;
1313 DECL_STRUCT_FUNCTION(decl
)->static_chain_decl
= closure_decl
;
1320 return this->fndecl_
;
1323 // Get a tree for a function declaration.
1326 Function_declaration::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1328 if (this->fndecl_
== NULL_TREE
)
1330 // Let Go code use an asm declaration to pick up a builtin
1332 if (!this->asm_name_
.empty())
1334 std::map
<std::string
, tree
>::const_iterator p
=
1335 builtin_functions
.find(this->asm_name_
);
1336 if (p
!= builtin_functions
.end())
1338 this->fndecl_
= p
->second
;
1339 return this->fndecl_
;
1343 tree functype
= type_to_tree(this->fntype_
->get_backend(gogo
));
1345 if (functype
== error_mark_node
)
1346 decl
= error_mark_node
;
1349 // The type of a function comes back as a pointer, but we
1350 // want the real function type for a function declaration.
1351 go_assert(POINTER_TYPE_P(functype
));
1352 functype
= TREE_TYPE(functype
);
1353 decl
= build_decl(this->location().gcc_location(), FUNCTION_DECL
, id
,
1355 TREE_PUBLIC(decl
) = 1;
1356 DECL_EXTERNAL(decl
) = 1;
1358 if (this->asm_name_
.empty())
1360 std::string asm_name
= (no
->package() == NULL
1361 ? gogo
->unique_prefix()
1362 : no
->package()->unique_prefix());
1363 asm_name
.append(1, '.');
1364 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1365 SET_DECL_ASSEMBLER_NAME(decl
,
1366 get_identifier_from_string(asm_name
));
1369 this->fndecl_
= decl
;
1370 go_preserve_from_gc(decl
);
1372 return this->fndecl_
;
1375 // We always pass the receiver to a method as a pointer. If the
1376 // receiver is actually declared as a non-pointer type, then we copy
1377 // the value into a local variable, so that it has the right type. In
1378 // this function we create the real PARM_DECL to use, and set
1379 // DEC_INITIAL of the var_decl to be the value passed in.
1382 Function::make_receiver_parm_decl(Gogo
* gogo
, Named_object
* no
, tree var_decl
)
1384 if (var_decl
== error_mark_node
)
1385 return error_mark_node
;
1386 go_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1387 tree val_type
= TREE_TYPE(var_decl
);
1388 bool is_in_heap
= no
->var_value()->is_in_heap();
1391 go_assert(POINTER_TYPE_P(val_type
));
1392 val_type
= TREE_TYPE(val_type
);
1395 source_location loc
= DECL_SOURCE_LOCATION(var_decl
);
1396 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1398 tree id
= get_identifier_from_string(name
);
1399 tree parm_decl
= build_decl(loc
, PARM_DECL
, id
, build_pointer_type(val_type
));
1400 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1401 DECL_ARG_TYPE(parm_decl
) = TREE_TYPE(parm_decl
);
1403 go_assert(DECL_INITIAL(var_decl
) == NULL_TREE
);
1404 tree init
= build_fold_indirect_ref_loc(loc
, parm_decl
);
1408 tree size
= TYPE_SIZE_UNIT(val_type
);
1409 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
,
1411 space
= save_expr(space
);
1412 space
= fold_convert(build_pointer_type(val_type
), space
);
1413 tree spaceref
= build_fold_indirect_ref_loc(no
->location().gcc_location(),
1415 TREE_THIS_NOTRAP(spaceref
) = 1;
1416 tree set
= fold_build2_loc(loc
, MODIFY_EXPR
, void_type_node
,
1418 init
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(space
), set
, space
);
1421 DECL_INITIAL(var_decl
) = init
;
1426 // If we take the address of a parameter, then we need to copy it into
1427 // the heap. We will access it as a local variable via an
1431 Function::copy_parm_to_heap(Gogo
* gogo
, Named_object
* no
, tree var_decl
)
1433 if (var_decl
== error_mark_node
)
1434 return error_mark_node
;
1435 go_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1436 Location
loc(DECL_SOURCE_LOCATION(var_decl
));
1438 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1440 tree id
= get_identifier_from_string(name
);
1442 tree type
= TREE_TYPE(var_decl
);
1443 go_assert(POINTER_TYPE_P(type
));
1444 type
= TREE_TYPE(type
);
1446 tree parm_decl
= build_decl(loc
.gcc_location(), PARM_DECL
, id
, type
);
1447 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1448 DECL_ARG_TYPE(parm_decl
) = type
;
1450 tree size
= TYPE_SIZE_UNIT(type
);
1451 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
, loc
);
1452 space
= save_expr(space
);
1453 space
= fold_convert(TREE_TYPE(var_decl
), space
);
1454 tree spaceref
= build_fold_indirect_ref_loc(loc
.gcc_location(), space
);
1455 TREE_THIS_NOTRAP(spaceref
) = 1;
1456 tree init
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
1457 build2(MODIFY_EXPR
, void_type_node
, spaceref
, parm_decl
),
1459 DECL_INITIAL(var_decl
) = init
;
1464 // Get a tree for function code.
1467 Function::build_tree(Gogo
* gogo
, Named_object
* named_function
)
1469 tree fndecl
= this->fndecl_
;
1470 go_assert(fndecl
!= NULL_TREE
);
1472 tree params
= NULL_TREE
;
1475 tree declare_vars
= NULL_TREE
;
1476 for (Bindings::const_definitions_iterator p
=
1477 this->block_
->bindings()->begin_definitions();
1478 p
!= this->block_
->bindings()->end_definitions();
1481 if ((*p
)->is_variable() && (*p
)->var_value()->is_parameter())
1483 Bvariable
* bvar
= (*p
)->get_backend_variable(gogo
, named_function
);
1484 *pp
= var_to_tree(bvar
);
1486 // We always pass the receiver to a method as a pointer. If
1487 // the receiver is declared as a non-pointer type, then we
1488 // copy the value into a local variable.
1489 if ((*p
)->var_value()->is_receiver()
1490 && (*p
)->var_value()->type()->points_to() == NULL
)
1492 tree parm_decl
= this->make_receiver_parm_decl(gogo
, *p
, *pp
);
1494 if (var
!= error_mark_node
)
1496 go_assert(TREE_CODE(var
) == VAR_DECL
);
1497 DECL_CHAIN(var
) = declare_vars
;
1502 else if ((*p
)->var_value()->is_in_heap())
1504 // If we take the address of a parameter, then we need
1505 // to copy it into the heap.
1506 tree parm_decl
= this->copy_parm_to_heap(gogo
, *p
, *pp
);
1508 if (var
!= error_mark_node
)
1510 go_assert(TREE_CODE(var
) == VAR_DECL
);
1511 DECL_CHAIN(var
) = declare_vars
;
1517 if (*pp
!= error_mark_node
)
1519 go_assert(TREE_CODE(*pp
) == PARM_DECL
);
1520 pp
= &DECL_CHAIN(*pp
);
1523 else if ((*p
)->is_result_variable())
1525 Bvariable
* bvar
= (*p
)->get_backend_variable(gogo
, named_function
);
1526 tree var_decl
= var_to_tree(bvar
);
1528 Type
* type
= (*p
)->result_var_value()->type();
1530 if (!(*p
)->result_var_value()->is_in_heap())
1532 Btype
* btype
= type
->get_backend(gogo
);
1533 init
= expr_to_tree(gogo
->backend()->zero_expression(btype
));
1537 Location loc
= (*p
)->location();
1538 tree type_tree
= type_to_tree(type
->get_backend(gogo
));
1539 tree space
= gogo
->allocate_memory(type
,
1540 TYPE_SIZE_UNIT(type_tree
),
1542 tree ptr_type_tree
= build_pointer_type(type_tree
);
1543 init
= fold_convert_loc(loc
.gcc_location(), ptr_type_tree
, space
);
1546 if (var_decl
!= error_mark_node
)
1548 go_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1549 DECL_INITIAL(var_decl
) = init
;
1550 DECL_CHAIN(var_decl
) = declare_vars
;
1551 declare_vars
= var_decl
;
1557 DECL_ARGUMENTS(fndecl
) = params
;
1559 if (this->block_
!= NULL
)
1561 go_assert(DECL_INITIAL(fndecl
) == NULL_TREE
);
1563 // Declare variables if necessary.
1564 tree bind
= NULL_TREE
;
1565 tree defer_init
= NULL_TREE
;
1566 if (declare_vars
!= NULL_TREE
|| this->defer_stack_
!= NULL
)
1568 tree block
= make_node(BLOCK
);
1569 BLOCK_SUPERCONTEXT(block
) = fndecl
;
1570 DECL_INITIAL(fndecl
) = block
;
1571 BLOCK_VARS(block
) = declare_vars
;
1572 TREE_USED(block
) = 1;
1574 bind
= build3(BIND_EXPR
, void_type_node
, BLOCK_VARS(block
),
1576 TREE_SIDE_EFFECTS(bind
) = 1;
1578 if (this->defer_stack_
!= NULL
)
1580 Translate_context
dcontext(gogo
, named_function
, this->block_
,
1581 tree_to_block(bind
));
1582 Bstatement
* bdi
= this->defer_stack_
->get_backend(&dcontext
);
1583 defer_init
= stat_to_tree(bdi
);
1587 // Build the trees for all the statements in the function.
1588 Translate_context
context(gogo
, named_function
, NULL
, NULL
);
1589 Bblock
* bblock
= this->block_
->get_backend(&context
);
1590 tree code
= block_to_tree(bblock
);
1592 tree init
= NULL_TREE
;
1593 tree except
= NULL_TREE
;
1594 tree fini
= NULL_TREE
;
1596 // Initialize variables if necessary.
1597 for (tree v
= declare_vars
; v
!= NULL_TREE
; v
= DECL_CHAIN(v
))
1599 tree dv
= build1(DECL_EXPR
, void_type_node
, v
);
1600 SET_EXPR_LOCATION(dv
, DECL_SOURCE_LOCATION(v
));
1601 append_to_statement_list(dv
, &init
);
1604 // If we have a defer stack, initialize it at the start of a
1606 if (defer_init
!= NULL_TREE
&& defer_init
!= error_mark_node
)
1608 SET_EXPR_LOCATION(defer_init
,
1609 this->block_
->start_location().gcc_location());
1610 append_to_statement_list(defer_init
, &init
);
1612 // Clean up the defer stack when we leave the function.
1613 this->build_defer_wrapper(gogo
, named_function
, &except
, &fini
);
1616 if (code
!= NULL_TREE
&& code
!= error_mark_node
)
1618 if (init
!= NULL_TREE
)
1619 code
= build2(COMPOUND_EXPR
, void_type_node
, init
, code
);
1620 if (except
!= NULL_TREE
)
1621 code
= build2(TRY_CATCH_EXPR
, void_type_node
, code
,
1622 build2(CATCH_EXPR
, void_type_node
, NULL
, except
));
1623 if (fini
!= NULL_TREE
)
1624 code
= build2(TRY_FINALLY_EXPR
, void_type_node
, code
, fini
);
1627 // Stick the code into the block we built for the receiver, if
1629 if (bind
!= NULL_TREE
&& code
!= NULL_TREE
&& code
!= error_mark_node
)
1631 BIND_EXPR_BODY(bind
) = code
;
1635 DECL_SAVED_TREE(fndecl
) = code
;
1639 // Build the wrappers around function code needed if the function has
1640 // any defer statements. This sets *EXCEPT to an exception handler
1641 // and *FINI to a finally handler.
1644 Function::build_defer_wrapper(Gogo
* gogo
, Named_object
* named_function
,
1645 tree
*except
, tree
*fini
)
1647 Location end_loc
= this->block_
->end_location();
1649 // Add an exception handler. This is used if a panic occurs. Its
1650 // purpose is to stop the stack unwinding if a deferred function
1651 // calls recover. There are more details in
1652 // libgo/runtime/go-unwind.c.
1654 tree stmt_list
= NULL_TREE
;
1656 Expression
* call
= Runtime::make_call(Runtime::CHECK_DEFER
, end_loc
, 1,
1657 this->defer_stack(end_loc
));
1658 Translate_context
context(gogo
, named_function
, NULL
, NULL
);
1659 tree call_tree
= call
->get_tree(&context
);
1660 if (call_tree
!= error_mark_node
)
1661 append_to_statement_list(call_tree
, &stmt_list
);
1663 tree retval
= this->return_value(gogo
, named_function
, end_loc
, &stmt_list
);
1665 if (retval
== NULL_TREE
)
1668 set
= fold_build2_loc(end_loc
.gcc_location(), MODIFY_EXPR
, void_type_node
,
1669 DECL_RESULT(this->fndecl_
), retval
);
1670 tree ret_stmt
= fold_build1_loc(end_loc
.gcc_location(), RETURN_EXPR
,
1671 void_type_node
, set
);
1672 append_to_statement_list(ret_stmt
, &stmt_list
);
1674 go_assert(*except
== NULL_TREE
);
1675 *except
= stmt_list
;
1677 // Add some finally code to run the defer functions. This is used
1678 // both in the normal case, when no panic occurs, and also if a
1679 // panic occurs to run any further defer functions. Of course, it
1680 // is possible for a defer function to call panic which should be
1681 // caught by another defer function. To handle that we use a loop.
1683 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1684 // if (return values are named) return named_vals;
1688 tree label
= create_artificial_label(end_loc
.gcc_location());
1689 tree define_label
= fold_build1_loc(end_loc
.gcc_location(), LABEL_EXPR
,
1690 void_type_node
, label
);
1691 append_to_statement_list(define_label
, &stmt_list
);
1693 call
= Runtime::make_call(Runtime::UNDEFER
, end_loc
, 1,
1694 this->defer_stack(end_loc
));
1695 tree undefer
= call
->get_tree(&context
);
1697 call
= Runtime::make_call(Runtime::CHECK_DEFER
, end_loc
, 1,
1698 this->defer_stack(end_loc
));
1699 tree defer
= call
->get_tree(&context
);
1701 if (undefer
== error_mark_node
|| defer
== error_mark_node
)
1704 tree jump
= fold_build1_loc(end_loc
.gcc_location(), GOTO_EXPR
, void_type_node
,
1706 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer
, jump
);
1707 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
1708 tree try_catch
= build2(TRY_CATCH_EXPR
, void_type_node
, undefer
, catch_body
);
1710 append_to_statement_list(try_catch
, &stmt_list
);
1712 if (this->type_
->results() != NULL
1713 && !this->type_
->results()->empty()
1714 && !this->type_
->results()->front().name().empty())
1716 // If the result variables are named, and we are returning from
1717 // this function rather than panicing through it, we need to
1718 // return them again, because they might have been changed by a
1719 // defer function. The runtime routines set the defer_stack
1720 // variable to true if we are returning from this function.
1721 retval
= this->return_value(gogo
, named_function
, end_loc
,
1723 set
= fold_build2_loc(end_loc
.gcc_location(), MODIFY_EXPR
, void_type_node
,
1724 DECL_RESULT(this->fndecl_
), retval
);
1725 ret_stmt
= fold_build1_loc(end_loc
.gcc_location(), RETURN_EXPR
,
1726 void_type_node
, set
);
1729 Expression::make_temporary_reference(this->defer_stack_
, end_loc
);
1730 tree tref
= ref
->get_tree(&context
);
1731 tree s
= build3_loc(end_loc
.gcc_location(), COND_EXPR
, void_type_node
,
1732 tref
, ret_stmt
, NULL_TREE
);
1734 append_to_statement_list(s
, &stmt_list
);
1738 go_assert(*fini
== NULL_TREE
);
1742 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1743 // also add statements to STMT_LIST, which need to be executed before
1744 // the assignment. This is used for a return statement with no
1748 Function::return_value(Gogo
* gogo
, Named_object
* named_function
,
1749 Location location
, tree
* stmt_list
) const
1751 const Typed_identifier_list
* results
= this->type_
->results();
1752 if (results
== NULL
|| results
->empty())
1755 go_assert(this->results_
!= NULL
);
1756 if (this->results_
->size() != results
->size())
1758 go_assert(saw_errors());
1759 return error_mark_node
;
1763 if (results
->size() == 1)
1766 this->results_
->front()->get_backend_variable(gogo
,
1768 tree ret
= var_to_tree(bvar
);
1769 if (this->results_
->front()->result_var_value()->is_in_heap())
1770 ret
= build_fold_indirect_ref_loc(location
.gcc_location(), ret
);
1775 tree rettype
= TREE_TYPE(DECL_RESULT(this->fndecl_
));
1776 retval
= create_tmp_var(rettype
, "RESULT");
1777 tree field
= TYPE_FIELDS(rettype
);
1779 for (Typed_identifier_list::const_iterator pr
= results
->begin();
1780 pr
!= results
->end();
1781 ++pr
, ++index
, field
= DECL_CHAIN(field
))
1783 go_assert(field
!= NULL
);
1784 Named_object
* no
= (*this->results_
)[index
];
1785 Bvariable
* bvar
= no
->get_backend_variable(gogo
, named_function
);
1786 tree val
= var_to_tree(bvar
);
1787 if (no
->result_var_value()->is_in_heap())
1788 val
= build_fold_indirect_ref_loc(location
.gcc_location(), val
);
1789 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1791 build3(COMPONENT_REF
, TREE_TYPE(field
),
1792 retval
, field
, NULL_TREE
),
1794 append_to_statement_list(set
, stmt_list
);
1800 // Return the integer type to use for a size.
1804 go_type_for_size(unsigned int bits
, int unsignedp
)
1810 name
= unsignedp
? "uint8" : "int8";
1813 name
= unsignedp
? "uint16" : "int16";
1816 name
= unsignedp
? "uint32" : "int32";
1819 name
= unsignedp
? "uint64" : "int64";
1822 if (bits
== POINTER_SIZE
&& unsignedp
)
1827 Type
* type
= Type::lookup_integer_type(name
);
1828 return type_to_tree(type
->get_backend(go_get_gogo()));
1831 // Return the type to use for a mode.
1835 go_type_for_mode(enum machine_mode mode
, int unsignedp
)
1837 // FIXME: This static_cast should be in machmode.h.
1838 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
1840 return go_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
1841 else if (mc
== MODE_FLOAT
)
1844 switch (GET_MODE_BITSIZE (mode
))
1847 type
= Type::lookup_float_type("float32");
1850 type
= Type::lookup_float_type("float64");
1853 // We have to check for long double in order to support
1854 // i386 excess precision.
1855 if (mode
== TYPE_MODE(long_double_type_node
))
1856 return long_double_type_node
;
1859 return type_to_tree(type
->get_backend(go_get_gogo()));
1861 else if (mc
== MODE_COMPLEX_FLOAT
)
1864 switch (GET_MODE_BITSIZE (mode
))
1867 type
= Type::lookup_complex_type("complex64");
1870 type
= Type::lookup_complex_type("complex128");
1873 // We have to check for long double in order to support
1874 // i386 excess precision.
1875 if (mode
== TYPE_MODE(complex_long_double_type_node
))
1876 return complex_long_double_type_node
;
1879 return type_to_tree(type
->get_backend(go_get_gogo()));
1885 // Return a tree which allocates SIZE bytes which will holds value of
1889 Gogo::allocate_memory(Type
* type
, tree size
, Location location
)
1891 // If the package imports unsafe, then it may play games with
1892 // pointers that look like integers.
1893 if (this->imported_unsafe_
|| type
->has_pointer())
1895 static tree new_fndecl
;
1896 return Gogo::call_builtin(&new_fndecl
,
1906 static tree new_nopointers_fndecl
;
1907 return Gogo::call_builtin(&new_nopointers_fndecl
,
1909 "__go_new_nopointers",
1917 // Build a builtin struct with a list of fields. The name is
1918 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
1919 // node; this exists so that the struct can have fields which point to
1920 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
1921 // are NFIELDS fields. Each field is a name (a const char*) followed
1922 // by a type (a tree).
1925 Gogo::builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
1928 if (ptype
!= NULL
&& *ptype
!= NULL_TREE
)
1932 va_start(ap
, nfields
);
1934 tree fields
= NULL_TREE
;
1935 for (int i
= 0; i
< nfields
; ++i
)
1937 const char* field_name
= va_arg(ap
, const char*);
1938 tree type
= va_arg(ap
, tree
);
1939 if (type
== error_mark_node
)
1942 *ptype
= error_mark_node
;
1943 return error_mark_node
;
1945 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
,
1946 get_identifier(field_name
), type
);
1947 DECL_CHAIN(field
) = fields
;
1953 if (struct_type
== NULL_TREE
)
1954 struct_type
= make_node(RECORD_TYPE
);
1955 finish_builtin_struct(struct_type
, struct_name
, fields
, NULL_TREE
);
1959 go_preserve_from_gc(struct_type
);
1960 *ptype
= struct_type
;
1966 // Return a type to use for pointer to const char for a string.
1969 Gogo::const_char_pointer_type_tree()
1972 if (type
== NULL_TREE
)
1974 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1976 type
= build_pointer_type(const_char_type
);
1977 go_preserve_from_gc(type
);
1982 // Return a tree for a string constant.
1985 Gogo::string_constant_tree(const std::string
& val
)
1987 tree index_type
= build_index_type(size_int(val
.length()));
1988 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1990 tree string_type
= build_array_type(const_char_type
, index_type
);
1991 string_type
= build_variant_type_copy(string_type
);
1992 TYPE_STRING_FLAG(string_type
) = 1;
1993 tree string_val
= build_string(val
.length(), val
.data());
1994 TREE_TYPE(string_val
) = string_type
;
1998 // Return a tree for a Go string constant.
2001 Gogo::go_string_constant_tree(const std::string
& val
)
2003 tree string_type
= type_to_tree(Type::make_string_type()->get_backend(this));
2005 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2007 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2008 tree field
= TYPE_FIELDS(string_type
);
2009 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__data") == 0);
2011 tree str
= Gogo::string_constant_tree(val
);
2012 elt
->value
= fold_convert(TREE_TYPE(field
),
2013 build_fold_addr_expr(str
));
2015 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2016 field
= DECL_CHAIN(field
);
2017 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__length") == 0);
2019 elt
->value
= build_int_cst_type(TREE_TYPE(field
), val
.length());
2021 tree constructor
= build_constructor(string_type
, init
);
2022 TREE_READONLY(constructor
) = 1;
2023 TREE_CONSTANT(constructor
) = 1;
2028 // Return a tree for a pointer to a Go string constant. This is only
2029 // used for type descriptors, so we return a pointer to a constant
2033 Gogo::ptr_go_string_constant_tree(const std::string
& val
)
2035 tree pval
= this->go_string_constant_tree(val
);
2037 tree decl
= build_decl(UNKNOWN_LOCATION
, VAR_DECL
,
2038 create_tmp_var_name("SP"), TREE_TYPE(pval
));
2039 DECL_EXTERNAL(decl
) = 0;
2040 TREE_PUBLIC(decl
) = 0;
2041 TREE_USED(decl
) = 1;
2042 TREE_READONLY(decl
) = 1;
2043 TREE_CONSTANT(decl
) = 1;
2044 TREE_STATIC(decl
) = 1;
2045 DECL_ARTIFICIAL(decl
) = 1;
2046 DECL_INITIAL(decl
) = pval
;
2047 rest_of_decl_compilation(decl
, 1, 0);
2049 return build_fold_addr_expr(decl
);
2052 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2053 // the slice. VALUES is the value pointer and COUNT is the number of
2054 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2055 // the capacity and the count are the same.
2058 Gogo::slice_constructor(tree slice_type_tree
, tree values
, tree count
,
2061 go_assert(TREE_CODE(slice_type_tree
) == RECORD_TYPE
);
2063 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
2065 tree field
= TYPE_FIELDS(slice_type_tree
);
2066 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
2067 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2069 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field
))
2070 == TYPE_MAIN_VARIANT(TREE_TYPE(values
)));
2071 elt
->value
= values
;
2073 count
= fold_convert(sizetype
, count
);
2074 if (capacity
== NULL_TREE
)
2076 count
= save_expr(count
);
2080 field
= DECL_CHAIN(field
);
2081 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
2082 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2084 elt
->value
= fold_convert(TREE_TYPE(field
), count
);
2086 field
= DECL_CHAIN(field
);
2087 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
2088 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2090 elt
->value
= fold_convert(TREE_TYPE(field
), capacity
);
2092 return build_constructor(slice_type_tree
, init
);
2095 // Build an interface method table for a type: a list of function
2096 // pointers, one for each interface method. This is used for
2100 Gogo::interface_method_table_for_type(const Interface_type
* interface
,
2104 const Typed_identifier_list
* interface_methods
= interface
->methods();
2105 go_assert(!interface_methods
->empty());
2107 std::string mangled_name
= ((is_pointer
? "__go_pimt__" : "__go_imt_")
2108 + interface
->mangled_name(this)
2110 + type
->mangled_name(this));
2112 tree id
= get_identifier_from_string(mangled_name
);
2114 // See whether this interface has any hidden methods.
2115 bool has_hidden_methods
= false;
2116 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2117 p
!= interface_methods
->end();
2120 if (Gogo::is_hidden_name(p
->name()))
2122 has_hidden_methods
= true;
2127 // We already know that the named type is convertible to the
2128 // interface. If the interface has hidden methods, and the named
2129 // type is defined in a different package, then the interface
2130 // conversion table will be defined by that other package.
2131 if (has_hidden_methods
&& type
->named_object()->package() != NULL
)
2133 tree array_type
= build_array_type(const_ptr_type_node
, NULL
);
2134 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2135 TREE_READONLY(decl
) = 1;
2136 TREE_CONSTANT(decl
) = 1;
2137 TREE_PUBLIC(decl
) = 1;
2138 DECL_EXTERNAL(decl
) = 1;
2139 go_preserve_from_gc(decl
);
2143 size_t count
= interface_methods
->size();
2144 VEC(constructor_elt
, gc
)* pointers
= VEC_alloc(constructor_elt
, gc
,
2147 // The first element is the type descriptor.
2148 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2149 elt
->index
= size_zero_node
;
2154 td_type
= Type::make_pointer_type(type
);
2155 tree tdp
= td_type
->type_descriptor_pointer(this,
2156 Linemap::predeclared_location());
2157 elt
->value
= fold_convert(const_ptr_type_node
, tdp
);
2160 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2161 p
!= interface_methods
->end();
2165 Method
* m
= type
->method_function(p
->name(), &is_ambiguous
);
2166 go_assert(m
!= NULL
);
2168 Named_object
* no
= m
->named_object();
2170 tree fnid
= no
->get_id(this);
2173 if (no
->is_function())
2174 fndecl
= no
->func_value()->get_or_make_decl(this, no
, fnid
);
2175 else if (no
->is_function_declaration())
2176 fndecl
= no
->func_declaration_value()->get_or_make_decl(this, no
,
2180 fndecl
= build_fold_addr_expr(fndecl
);
2182 elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2183 elt
->index
= size_int(i
);
2184 elt
->value
= fold_convert(const_ptr_type_node
, fndecl
);
2186 go_assert(i
== count
+ 1);
2188 tree array_type
= build_array_type(const_ptr_type_node
,
2189 build_index_type(size_int(count
)));
2190 tree constructor
= build_constructor(array_type
, pointers
);
2192 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2193 TREE_STATIC(decl
) = 1;
2194 TREE_USED(decl
) = 1;
2195 TREE_READONLY(decl
) = 1;
2196 TREE_CONSTANT(decl
) = 1;
2197 DECL_INITIAL(decl
) = constructor
;
2199 // If the interface type has hidden methods, then this is the only
2200 // definition of the table. Otherwise it is a comdat table which
2201 // may be defined in multiple packages.
2202 if (has_hidden_methods
)
2203 TREE_PUBLIC(decl
) = 1;
2206 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2207 resolve_unique_section(decl
, 1, 0);
2210 rest_of_decl_compilation(decl
, 1, 0);
2212 go_preserve_from_gc(decl
);
2217 // Mark a function as a builtin library function.
2220 Gogo::mark_fndecl_as_builtin_library(tree fndecl
)
2222 DECL_EXTERNAL(fndecl
) = 1;
2223 TREE_PUBLIC(fndecl
) = 1;
2224 DECL_ARTIFICIAL(fndecl
) = 1;
2225 TREE_NOTHROW(fndecl
) = 1;
2226 DECL_VISIBILITY(fndecl
) = VISIBILITY_DEFAULT
;
2227 DECL_VISIBILITY_SPECIFIED(fndecl
) = 1;
2230 // Build a call to a builtin function.
2233 Gogo::call_builtin(tree
* pdecl
, Location location
, const char* name
,
2234 int nargs
, tree rettype
, ...)
2236 if (rettype
== error_mark_node
)
2237 return error_mark_node
;
2239 tree
* types
= new tree
[nargs
];
2240 tree
* args
= new tree
[nargs
];
2243 va_start(ap
, rettype
);
2244 for (int i
= 0; i
< nargs
; ++i
)
2246 types
[i
] = va_arg(ap
, tree
);
2247 args
[i
] = va_arg(ap
, tree
);
2248 if (types
[i
] == error_mark_node
|| args
[i
] == error_mark_node
)
2252 return error_mark_node
;
2257 if (*pdecl
== NULL_TREE
)
2259 tree fnid
= get_identifier(name
);
2261 tree argtypes
= NULL_TREE
;
2262 tree
* pp
= &argtypes
;
2263 for (int i
= 0; i
< nargs
; ++i
)
2265 *pp
= tree_cons(NULL_TREE
, types
[i
], NULL_TREE
);
2266 pp
= &TREE_CHAIN(*pp
);
2268 *pp
= void_list_node
;
2270 tree fntype
= build_function_type(rettype
, argtypes
);
2272 *pdecl
= build_decl(BUILTINS_LOCATION
, FUNCTION_DECL
, fnid
, fntype
);
2273 Gogo::mark_fndecl_as_builtin_library(*pdecl
);
2274 go_preserve_from_gc(*pdecl
);
2277 tree fnptr
= build_fold_addr_expr(*pdecl
);
2278 if (CAN_HAVE_LOCATION_P(fnptr
))
2279 SET_EXPR_LOCATION(fnptr
, location
.gcc_location());
2281 tree ret
= build_call_array(rettype
, fnptr
, nargs
, args
);
2282 SET_EXPR_LOCATION(ret
, location
.gcc_location());
2290 // Build a call to the runtime error function.
2293 Gogo::runtime_error(int code
, Location location
)
2295 static tree runtime_error_fndecl
;
2296 tree ret
= Gogo::call_builtin(&runtime_error_fndecl
,
2298 "__go_runtime_error",
2302 build_int_cst(integer_type_node
, code
));
2303 if (ret
== error_mark_node
)
2304 return error_mark_node
;
2305 // The runtime error function panics and does not return.
2306 TREE_NOTHROW(runtime_error_fndecl
) = 0;
2307 TREE_THIS_VOLATILE(runtime_error_fndecl
) = 1;
2311 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2312 // TYPE_DESCRIPTOR_TREE is the channel's type descriptor. This does a
2313 // blocking receive and returns the value read from the channel.
2316 Gogo::receive_from_channel(tree type_tree
, tree type_descriptor_tree
,
2317 tree channel
, Location location
)
2319 if (type_tree
== error_mark_node
|| channel
== error_mark_node
)
2320 return error_mark_node
;
2322 if (int_size_in_bytes(type_tree
) <= 8
2323 && !AGGREGATE_TYPE_P(type_tree
)
2324 && !FLOAT_TYPE_P(type_tree
))
2326 static tree receive_small_fndecl
;
2327 tree call
= Gogo::call_builtin(&receive_small_fndecl
,
2329 "__go_receive_small",
2332 TREE_TYPE(type_descriptor_tree
),
2333 type_descriptor_tree
,
2336 if (call
== error_mark_node
)
2337 return error_mark_node
;
2338 // This can panic if there are too many operations on a closed
2340 TREE_NOTHROW(receive_small_fndecl
) = 0;
2341 int bitsize
= GET_MODE_BITSIZE(TYPE_MODE(type_tree
));
2342 tree int_type_tree
= go_type_for_size(bitsize
, 1);
2343 return fold_convert_loc(location
.gcc_location(), type_tree
,
2344 fold_convert_loc(location
.gcc_location(),
2345 int_type_tree
, call
));
2349 tree tmp
= create_tmp_var(type_tree
, get_name(type_tree
));
2350 DECL_IGNORED_P(tmp
) = 0;
2351 TREE_ADDRESSABLE(tmp
) = 1;
2352 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
2353 SET_EXPR_LOCATION(make_tmp
, location
.gcc_location());
2354 tree tmpaddr
= build_fold_addr_expr(tmp
);
2355 tmpaddr
= fold_convert(ptr_type_node
, tmpaddr
);
2356 static tree receive_big_fndecl
;
2357 tree call
= Gogo::call_builtin(&receive_big_fndecl
,
2362 TREE_TYPE(type_descriptor_tree
),
2363 type_descriptor_tree
,
2368 if (call
== error_mark_node
)
2369 return error_mark_node
;
2370 // This can panic if there are too many operations on a closed
2372 TREE_NOTHROW(receive_big_fndecl
) = 0;
2373 return build2(COMPOUND_EXPR
, type_tree
, make_tmp
,
2374 build2(COMPOUND_EXPR
, type_tree
, call
, tmp
));
2378 // Return the type of a function trampoline. This is like
2379 // get_trampoline_type in tree-nested.c.
2382 Gogo::trampoline_type_tree()
2384 static tree type_tree
;
2385 if (type_tree
== NULL_TREE
)
2389 go_trampoline_info(&size
, &align
);
2390 tree t
= build_index_type(build_int_cst(integer_type_node
, size
- 1));
2391 t
= build_array_type(char_type_node
, t
);
2393 type_tree
= Gogo::builtin_struct(NULL
, "__go_trampoline", NULL_TREE
, 1,
2395 t
= TYPE_FIELDS(type_tree
);
2396 DECL_ALIGN(t
) = align
;
2397 DECL_USER_ALIGN(t
) = 1;
2399 go_preserve_from_gc(type_tree
);
2404 // Make a trampoline which calls FNADDR passing CLOSURE.
2407 Gogo::make_trampoline(tree fnaddr
, tree closure
, Location location
)
2409 tree trampoline_type
= Gogo::trampoline_type_tree();
2410 tree trampoline_size
= TYPE_SIZE_UNIT(trampoline_type
);
2412 closure
= save_expr(closure
);
2414 // We allocate the trampoline using a special function which will
2415 // mark it as executable.
2416 static tree trampoline_fndecl
;
2417 tree x
= Gogo::call_builtin(&trampoline_fndecl
,
2419 "__go_allocate_trampoline",
2425 fold_convert_loc(location
.gcc_location(),
2426 ptr_type_node
, closure
));
2427 if (x
== error_mark_node
)
2428 return error_mark_node
;
2432 // Initialize the trampoline.
2433 tree calldecl
= builtin_decl_implicit(BUILT_IN_INIT_HEAP_TRAMPOLINE
);
2434 tree ini
= build_call_expr(calldecl
, 3, x
, fnaddr
, closure
);
2436 // On some targets the trampoline address needs to be adjusted. For
2437 // example, when compiling in Thumb mode on the ARM, the address
2438 // needs to have the low bit set.
2439 x
= build_call_expr(builtin_decl_explicit(BUILT_IN_ADJUST_TRAMPOLINE
), 1, x
);
2440 x
= fold_convert(TREE_TYPE(fnaddr
), x
);
2442 return build2(COMPOUND_EXPR
, TREE_TYPE(x
), ini
, x
);