Merge from branches/gcc-4_8-branch up to rev 204657.
[official-gcc.git] / gcc-4_8-branch / gcc / go / gofrontend / gogo-tree.cc
blobca80869da6ad1b02a272789d21e93ea891dd6048
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.
7 #include "go-system.h"
9 #include "toplev.h"
10 #include "tree.h"
11 #include "gimple.h"
12 #include "tree-iterator.h"
13 #include "cgraph.h"
14 #include "langhooks.h"
15 #include "convert.h"
16 #include "output.h"
17 #include "diagnostic.h"
18 #include "go-c.h"
20 #include "types.h"
21 #include "expressions.h"
22 #include "statements.h"
23 #include "runtime.h"
24 #include "backend.h"
25 #include "gogo.h"
27 // Whether we have seen any errors.
29 bool
30 saw_errors()
32 return errorcount != 0 || sorrycount != 0;
35 // A helper function.
37 static inline tree
38 get_identifier_from_string(const std::string& str)
40 return get_identifier_with_length(str.data(), str.length());
43 // Builtin functions.
45 static std::map<std::string, tree> builtin_functions;
47 // Define a builtin function. BCODE is the builtin function code
48 // defined by builtins.def. NAME is the name of the builtin function.
49 // LIBNAME is the name of the corresponding library function, and is
50 // NULL if there isn't one. FNTYPE is the type of the function.
51 // CONST_P is true if the function has the const attribute.
53 static void
54 define_builtin(built_in_function bcode, const char* name, const char* libname,
55 tree fntype, bool const_p)
57 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
58 libname, NULL_TREE);
59 if (const_p)
60 TREE_READONLY(decl) = 1;
61 set_builtin_decl(bcode, decl, true);
62 builtin_functions[name] = decl;
63 if (libname != NULL)
65 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
66 NULL, NULL_TREE);
67 if (const_p)
68 TREE_READONLY(decl) = 1;
69 builtin_functions[libname] = decl;
73 // Create trees for implicit builtin functions.
75 void
76 Gogo::define_builtin_function_trees()
78 /* We need to define the fetch_and_add functions, since we use them
79 for ++ and --. */
80 tree t = go_type_for_size(BITS_PER_UNIT, 1);
81 tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
82 define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1", NULL,
83 build_function_type_list(t, p, t, NULL_TREE), false);
85 t = go_type_for_size(BITS_PER_UNIT * 2, 1);
86 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
87 define_builtin (BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2", NULL,
88 build_function_type_list(t, p, t, NULL_TREE), false);
90 t = go_type_for_size(BITS_PER_UNIT * 4, 1);
91 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
92 define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4", NULL,
93 build_function_type_list(t, p, t, NULL_TREE), false);
95 t = go_type_for_size(BITS_PER_UNIT * 8, 1);
96 p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
97 define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8", NULL,
98 build_function_type_list(t, p, t, NULL_TREE), false);
100 // We use __builtin_expect for magic import functions.
101 define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
102 build_function_type_list(long_integer_type_node,
103 long_integer_type_node,
104 long_integer_type_node,
105 NULL_TREE),
106 true);
108 // We use __builtin_memcmp for struct comparisons.
109 define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
110 build_function_type_list(integer_type_node,
111 const_ptr_type_node,
112 const_ptr_type_node,
113 size_type_node,
114 NULL_TREE),
115 false);
117 // We provide some functions for the math library.
118 tree math_function_type = build_function_type_list(double_type_node,
119 double_type_node,
120 NULL_TREE);
121 tree math_function_type_long =
122 build_function_type_list(long_double_type_node, long_double_type_node,
123 long_double_type_node, NULL_TREE);
124 tree math_function_type_two = build_function_type_list(double_type_node,
125 double_type_node,
126 double_type_node,
127 NULL_TREE);
128 tree math_function_type_long_two =
129 build_function_type_list(long_double_type_node, long_double_type_node,
130 long_double_type_node, NULL_TREE);
131 define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
132 math_function_type, true);
133 define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
134 math_function_type_long, true);
135 define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
136 math_function_type, true);
137 define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
138 math_function_type_long, true);
139 define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
140 math_function_type, true);
141 define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
142 math_function_type_long, true);
143 define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
144 math_function_type_two, true);
145 define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
146 math_function_type_long_two, true);
147 define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
148 math_function_type, true);
149 define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
150 math_function_type_long, true);
151 define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
152 math_function_type, true);
153 define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
154 math_function_type_long, true);
155 define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
156 math_function_type, true);
157 define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
158 math_function_type_long, true);
159 define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
160 math_function_type, true);
161 define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
162 math_function_type_long, true);
163 define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
164 math_function_type, true);
165 define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
166 math_function_type_long, true);
167 define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
168 math_function_type, true);
169 define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
170 math_function_type_long, true);
171 define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
172 math_function_type_two, true);
173 define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
174 math_function_type_long_two, true);
175 define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
176 build_function_type_list(double_type_node,
177 double_type_node,
178 integer_type_node,
179 NULL_TREE),
180 true);
181 define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
182 build_function_type_list(long_double_type_node,
183 long_double_type_node,
184 integer_type_node,
185 NULL_TREE),
186 true);
187 define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
188 math_function_type, true);
189 define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
190 math_function_type_long, true);
191 define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
192 math_function_type, true);
193 define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
194 math_function_type_long, true);
195 define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
196 math_function_type, true);
197 define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
198 math_function_type_long, true);
199 define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
200 math_function_type, true);
201 define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
202 math_function_type_long, true);
203 define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
204 math_function_type, true);
205 define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
206 math_function_type_long, true);
207 define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
208 math_function_type, true);
209 define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
210 math_function_type_long, true);
211 define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
212 math_function_type, true);
213 define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
214 math_function_type_long, true);
215 define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
216 math_function_type, true);
217 define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
218 math_function_type_long, true);
220 // We use __builtin_return_address in the thunk we build for
221 // functions which call recover.
222 define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", NULL,
223 build_function_type_list(ptr_type_node,
224 unsigned_type_node,
225 NULL_TREE),
226 false);
228 // The compiler uses __builtin_trap for some exception handling
229 // cases.
230 define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
231 build_function_type(void_type_node, void_list_node),
232 false);
235 // Get the name to use for the import control function. If there is a
236 // global function or variable, then we know that that name must be
237 // unique in the link, and we use it as the basis for our name.
239 const std::string&
240 Gogo::get_init_fn_name()
242 if (this->init_fn_name_.empty())
244 go_assert(this->package_ != NULL);
245 if (this->is_main_package())
247 // Use a name which the runtime knows.
248 this->init_fn_name_ = "__go_init_main";
250 else
252 std::string s = this->pkgpath_symbol();
253 s.append("..import");
254 this->init_fn_name_ = s;
258 return this->init_fn_name_;
261 // Add statements to INIT_STMT_LIST which run the initialization
262 // functions for imported packages. This is only used for the "main"
263 // package.
265 void
266 Gogo::init_imports(tree* init_stmt_list)
268 go_assert(this->is_main_package());
270 if (this->imported_init_fns_.empty())
271 return;
273 tree fntype = build_function_type(void_type_node, void_list_node);
275 // We must call them in increasing priority order.
276 std::vector<Import_init> v;
277 for (std::set<Import_init>::const_iterator p =
278 this->imported_init_fns_.begin();
279 p != this->imported_init_fns_.end();
280 ++p)
281 v.push_back(*p);
282 std::sort(v.begin(), v.end());
284 for (std::vector<Import_init>::const_iterator p = v.begin();
285 p != v.end();
286 ++p)
288 std::string user_name = p->package_name() + ".init";
289 tree decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
290 get_identifier_from_string(user_name),
291 fntype);
292 const std::string& init_name(p->init_name());
293 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(init_name));
294 TREE_PUBLIC(decl) = 1;
295 DECL_EXTERNAL(decl) = 1;
296 append_to_statement_list(build_call_expr(decl, 0), init_stmt_list);
300 // Register global variables with the garbage collector. We need to
301 // register all variables which can hold a pointer value. They become
302 // roots during the mark phase. We build a struct that is easy to
303 // hook into a list of roots.
305 // struct __go_gc_root_list
306 // {
307 // struct __go_gc_root_list* __next;
308 // struct __go_gc_root
309 // {
310 // void* __decl;
311 // size_t __size;
312 // } __roots[];
313 // };
315 // The last entry in the roots array has a NULL decl field.
317 void
318 Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
319 tree* init_stmt_list)
321 if (var_gc.empty())
322 return;
324 size_t count = var_gc.size();
326 tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
327 "__next",
328 ptr_type_node,
329 "__size",
330 sizetype);
332 tree index_type = build_index_type(size_int(count));
333 tree array_type = build_array_type(root_type, index_type);
335 tree root_list_type = make_node(RECORD_TYPE);
336 root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
337 root_list_type, 2,
338 "__next",
339 build_pointer_type(root_list_type),
340 "__roots",
341 array_type);
343 // Build an initialier for the __roots array.
345 vec<constructor_elt, va_gc> *roots_init;
346 vec_alloc(roots_init, count + 1);
348 size_t i = 0;
349 for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
350 p != var_gc.end();
351 ++p, ++i)
353 vec<constructor_elt, va_gc> *init;
354 vec_alloc(init, 2);
356 constructor_elt empty = {NULL, NULL};
357 constructor_elt* elt = init->quick_push(empty);
358 tree field = TYPE_FIELDS(root_type);
359 elt->index = field;
360 Bvariable* bvar = (*p)->get_backend_variable(this, NULL);
361 tree decl = var_to_tree(bvar);
362 go_assert(TREE_CODE(decl) == VAR_DECL);
363 elt->value = build_fold_addr_expr(decl);
365 elt = init->quick_push(empty);
366 field = DECL_CHAIN(field);
367 elt->index = field;
368 elt->value = DECL_SIZE_UNIT(decl);
370 elt = roots_init->quick_push(empty);
371 elt->index = size_int(i);
372 elt->value = build_constructor(root_type, init);
375 // The list ends with a NULL entry.
377 vec<constructor_elt, va_gc> *init;
378 vec_alloc(init, 2);
380 constructor_elt empty = {NULL, NULL};
381 constructor_elt* elt = init->quick_push(empty);
382 tree field = TYPE_FIELDS(root_type);
383 elt->index = field;
384 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
386 elt = init->quick_push(empty);
387 field = DECL_CHAIN(field);
388 elt->index = field;
389 elt->value = size_zero_node;
391 elt = roots_init->quick_push(empty);
392 elt->index = size_int(i);
393 elt->value = build_constructor(root_type, init);
395 // Build a constructor for the struct.
397 vec<constructor_elt, va_gc> *root_list_init;
398 vec_alloc(root_list_init, 2);
400 elt = root_list_init->quick_push(empty);
401 field = TYPE_FIELDS(root_list_type);
402 elt->index = field;
403 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
405 elt = root_list_init->quick_push(empty);
406 field = DECL_CHAIN(field);
407 elt->index = field;
408 elt->value = build_constructor(array_type, roots_init);
410 // Build a decl to register.
412 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
413 create_tmp_var_name("gc"), root_list_type);
414 DECL_EXTERNAL(decl) = 0;
415 TREE_PUBLIC(decl) = 0;
416 TREE_STATIC(decl) = 1;
417 DECL_ARTIFICIAL(decl) = 1;
418 DECL_INITIAL(decl) = build_constructor(root_list_type, root_list_init);
419 rest_of_decl_compilation(decl, 1, 0);
421 static tree register_gc_fndecl;
422 tree call = Gogo::call_builtin(&register_gc_fndecl,
423 Linemap::predeclared_location(),
424 "__go_register_gc_roots",
426 void_type_node,
427 build_pointer_type(root_list_type),
428 build_fold_addr_expr(decl));
429 if (call != error_mark_node)
430 append_to_statement_list(call, init_stmt_list);
433 // Build the decl for the initialization function.
435 tree
436 Gogo::initialization_function_decl()
438 // The tedious details of building your own function. There doesn't
439 // seem to be a helper function for this.
440 std::string name = this->package_name() + ".init";
441 tree fndecl = build_decl(this->package_->location().gcc_location(),
442 FUNCTION_DECL, get_identifier_from_string(name),
443 build_function_type(void_type_node,
444 void_list_node));
445 const std::string& asm_name(this->get_init_fn_name());
446 SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
448 tree resdecl = build_decl(this->package_->location().gcc_location(),
449 RESULT_DECL, NULL_TREE, void_type_node);
450 DECL_ARTIFICIAL(resdecl) = 1;
451 DECL_CONTEXT(resdecl) = fndecl;
452 DECL_RESULT(fndecl) = resdecl;
454 TREE_STATIC(fndecl) = 1;
455 TREE_USED(fndecl) = 1;
456 DECL_ARTIFICIAL(fndecl) = 1;
457 TREE_PUBLIC(fndecl) = 1;
459 DECL_INITIAL(fndecl) = make_node(BLOCK);
460 TREE_USED(DECL_INITIAL(fndecl)) = 1;
462 return fndecl;
465 // Create the magic initialization function. INIT_STMT_LIST is the
466 // code that it needs to run.
468 void
469 Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
471 // Make sure that we thought we needed an initialization function,
472 // as otherwise we will not have reported it in the export data.
473 go_assert(this->is_main_package() || this->need_init_fn_);
475 if (fndecl == NULL_TREE)
476 fndecl = this->initialization_function_decl();
478 DECL_SAVED_TREE(fndecl) = init_stmt_list;
480 if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
481 push_struct_function(fndecl);
482 else
483 push_cfun(DECL_STRUCT_FUNCTION(fndecl));
484 cfun->function_start_locus = this->package_->location().gcc_location();
485 cfun->function_end_locus = cfun->function_start_locus;
487 gimplify_function_tree(fndecl);
489 cgraph_add_new_function(fndecl, false);
491 pop_cfun();
494 // Search for references to VAR in any statements or called functions.
496 class Find_var : public Traverse
498 public:
499 // A hash table we use to avoid looping. The index is the name of a
500 // named object. We only look through objects defined in this
501 // package.
502 typedef Unordered_set(const void*) Seen_objects;
504 Find_var(Named_object* var, Seen_objects* seen_objects)
505 : Traverse(traverse_expressions),
506 var_(var), seen_objects_(seen_objects), found_(false)
509 // Whether the variable was found.
510 bool
511 found() const
512 { return this->found_; }
515 expression(Expression**);
517 private:
518 // The variable we are looking for.
519 Named_object* var_;
520 // Names of objects we have already seen.
521 Seen_objects* seen_objects_;
522 // True if the variable was found.
523 bool found_;
526 // See if EXPR refers to VAR, looking through function calls and
527 // variable initializations.
530 Find_var::expression(Expression** pexpr)
532 Expression* e = *pexpr;
534 Var_expression* ve = e->var_expression();
535 if (ve != NULL)
537 Named_object* v = ve->named_object();
538 if (v == this->var_)
540 this->found_ = true;
541 return TRAVERSE_EXIT;
544 if (v->is_variable() && v->package() == NULL)
546 Expression* init = v->var_value()->init();
547 if (init != NULL)
549 std::pair<Seen_objects::iterator, bool> ins =
550 this->seen_objects_->insert(v);
551 if (ins.second)
553 // This is the first time we have seen this name.
554 if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
555 return TRAVERSE_EXIT;
561 // We traverse the code of any function we see. Note that this
562 // means that we will traverse the code of a function whose address
563 // is taken even if it is not called.
564 Func_expression* fe = e->func_expression();
565 if (fe != NULL)
567 const Named_object* f = fe->named_object();
568 if (f->is_function() && f->package() == NULL)
570 std::pair<Seen_objects::iterator, bool> ins =
571 this->seen_objects_->insert(f);
572 if (ins.second)
574 // This is the first time we have seen this name.
575 if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
576 return TRAVERSE_EXIT;
581 Temporary_reference_expression* tre = e->temporary_reference_expression();
582 if (tre != NULL)
584 Temporary_statement* ts = tre->statement();
585 Expression* init = ts->init();
586 if (init != NULL)
588 std::pair<Seen_objects::iterator, bool> ins =
589 this->seen_objects_->insert(ts);
590 if (ins.second)
592 // This is the first time we have seen this temporary
593 // statement.
594 if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
595 return TRAVERSE_EXIT;
600 return TRAVERSE_CONTINUE;
603 // Return true if EXPR, PREINIT, or DEP refers to VAR.
605 static bool
606 expression_requires(Expression* expr, Block* preinit, Named_object* dep,
607 Named_object* var)
609 Find_var::Seen_objects seen_objects;
610 Find_var find_var(var, &seen_objects);
611 if (expr != NULL)
612 Expression::traverse(&expr, &find_var);
613 if (preinit != NULL)
614 preinit->traverse(&find_var);
615 if (dep != NULL)
617 Expression* init = dep->var_value()->init();
618 if (init != NULL)
619 Expression::traverse(&init, &find_var);
620 if (dep->var_value()->has_pre_init())
621 dep->var_value()->preinit()->traverse(&find_var);
624 return find_var.found();
627 // Sort variable initializations. If the initialization expression
628 // for variable A refers directly or indirectly to the initialization
629 // expression for variable B, then we must initialize B before A.
631 class Var_init
633 public:
634 Var_init()
635 : var_(NULL), init_(NULL_TREE)
638 Var_init(Named_object* var, tree init)
639 : var_(var), init_(init)
642 // Return the variable.
643 Named_object*
644 var() const
645 { return this->var_; }
647 // Return the initialization expression.
648 tree
649 init() const
650 { return this->init_; }
652 private:
653 // The variable being initialized.
654 Named_object* var_;
655 // The initialization expression to run.
656 tree init_;
659 typedef std::list<Var_init> Var_inits;
661 // Sort the variable initializations. The rule we follow is that we
662 // emit them in the order they appear in the array, except that if the
663 // initialization expression for a variable V1 depends upon another
664 // variable V2 then we initialize V1 after V2.
666 static void
667 sort_var_inits(Gogo* gogo, Var_inits* var_inits)
669 typedef std::pair<Named_object*, Named_object*> No_no;
670 typedef std::map<No_no, bool> Cache;
671 Cache cache;
673 Var_inits ready;
674 while (!var_inits->empty())
676 Var_inits::iterator p1 = var_inits->begin();
677 Named_object* var = p1->var();
678 Expression* init = var->var_value()->init();
679 Block* preinit = var->var_value()->preinit();
680 Named_object* dep = gogo->var_depends_on(var->var_value());
682 // Start walking through the list to see which variables VAR
683 // needs to wait for.
684 Var_inits::iterator p2 = p1;
685 ++p2;
687 for (; p2 != var_inits->end(); ++p2)
689 Named_object* p2var = p2->var();
690 No_no key(var, p2var);
691 std::pair<Cache::iterator, bool> ins =
692 cache.insert(std::make_pair(key, false));
693 if (ins.second)
694 ins.first->second = expression_requires(init, preinit, dep, p2var);
695 if (ins.first->second)
697 // Check for cycles.
698 key = std::make_pair(p2var, var);
699 ins = cache.insert(std::make_pair(key, false));
700 if (ins.second)
701 ins.first->second =
702 expression_requires(p2var->var_value()->init(),
703 p2var->var_value()->preinit(),
704 gogo->var_depends_on(p2var->var_value()),
705 var);
706 if (ins.first->second)
708 error_at(var->location(),
709 ("initialization expressions for %qs and "
710 "%qs depend upon each other"),
711 var->message_name().c_str(),
712 p2var->message_name().c_str());
713 inform(p2->var()->location(), "%qs defined here",
714 p2var->message_name().c_str());
715 p2 = var_inits->end();
717 else
719 // We can't emit P1 until P2 is emitted. Move P1.
720 Var_inits::iterator p3 = p2;
721 ++p3;
722 var_inits->splice(p3, *var_inits, p1);
724 break;
728 if (p2 == var_inits->end())
730 // VAR does not depends upon any other initialization expressions.
732 // Check for a loop of VAR on itself. We only do this if
733 // INIT is not NULL and there is no dependency; when INIT is
734 // NULL, it means that PREINIT sets VAR, which we will
735 // interpret as a loop.
736 if (init != NULL && dep == NULL
737 && expression_requires(init, preinit, NULL, var))
738 error_at(var->location(),
739 "initialization expression for %qs depends upon itself",
740 var->message_name().c_str());
741 ready.splice(ready.end(), *var_inits, p1);
745 // Now READY is the list in the desired initialization order.
746 var_inits->swap(ready);
749 // Write out the global definitions.
751 void
752 Gogo::write_globals()
754 this->convert_named_types();
755 this->build_interface_method_tables();
757 Bindings* bindings = this->current_bindings();
759 for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
760 p != bindings->end_declarations();
761 ++p)
763 // If any function declarations needed a descriptor, make sure
764 // we build it.
765 Named_object* no = p->second;
766 if (no->is_function_declaration())
767 no->func_declaration_value()->build_backend_descriptor(this);
770 size_t count_definitions = bindings->size_definitions();
771 size_t count = count_definitions;
773 tree* vec = new tree[count];
775 tree init_fndecl = NULL_TREE;
776 tree init_stmt_list = NULL_TREE;
778 if (this->is_main_package())
779 this->init_imports(&init_stmt_list);
781 // A list of variable initializations.
782 Var_inits var_inits;
784 // A list of variables which need to be registered with the garbage
785 // collector.
786 std::vector<Named_object*> var_gc;
787 var_gc.reserve(count);
789 tree var_init_stmt_list = NULL_TREE;
790 size_t i = 0;
791 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
792 p != bindings->end_definitions();
793 ++p, ++i)
795 Named_object* no = *p;
797 go_assert(i < count);
799 go_assert(!no->is_type_declaration() && !no->is_function_declaration());
800 // There is nothing to do for a package.
801 if (no->is_package())
803 --i;
804 --count;
805 continue;
808 // There is nothing to do for an object which was imported from
809 // a different package into the global scope.
810 if (no->package() != NULL)
812 --i;
813 --count;
814 continue;
817 // Skip blank named functions and constants.
818 if ((no->is_function() && no->func_value()->is_sink())
819 || (no->is_const() && no->const_value()->is_sink()))
821 --i;
822 --count;
823 continue;
826 // There is nothing useful we can output for constants which
827 // have ideal or non-integral type.
828 if (no->is_const())
830 Type* type = no->const_value()->type();
831 if (type == NULL)
832 type = no->const_value()->expr()->type();
833 if (type->is_abstract() || type->integer_type() == NULL)
835 --i;
836 --count;
837 continue;
841 if (!no->is_variable())
843 vec[i] = no->get_tree(this, NULL);
844 if (vec[i] == error_mark_node)
846 go_assert(saw_errors());
847 --i;
848 --count;
849 continue;
852 else
854 Bvariable* var = no->get_backend_variable(this, NULL);
855 vec[i] = var_to_tree(var);
856 if (vec[i] == error_mark_node)
858 go_assert(saw_errors());
859 --i;
860 --count;
861 continue;
864 // Check for a sink variable, which may be used to run an
865 // initializer purely for its side effects.
866 bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
868 tree var_init_tree = NULL_TREE;
869 if (!no->var_value()->has_pre_init())
871 tree init = no->var_value()->get_init_tree(this, NULL);
872 if (init == error_mark_node)
873 go_assert(saw_errors());
874 else if (init == NULL_TREE)
876 else if (TREE_CONSTANT(init))
878 if (expression_requires(no->var_value()->init(), NULL,
879 this->var_depends_on(no->var_value()),
880 no))
881 error_at(no->location(),
882 "initialization expression for %qs depends "
883 "upon itself",
884 no->message_name().c_str());
885 this->backend()->global_variable_set_init(var,
886 tree_to_expr(init));
888 else if (is_sink
889 || int_size_in_bytes(TREE_TYPE(init)) == 0
890 || int_size_in_bytes(TREE_TYPE(vec[i])) == 0)
891 var_init_tree = init;
892 else
893 var_init_tree = fold_build2_loc(no->location().gcc_location(),
894 MODIFY_EXPR, void_type_node,
895 vec[i], init);
897 else
899 // We are going to create temporary variables which
900 // means that we need an fndecl.
901 if (init_fndecl == NULL_TREE)
902 init_fndecl = this->initialization_function_decl();
903 if (DECL_STRUCT_FUNCTION(init_fndecl) == NULL)
904 push_struct_function(init_fndecl);
905 else
906 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
907 tree var_decl = is_sink ? NULL_TREE : vec[i];
908 var_init_tree = no->var_value()->get_init_block(this, NULL,
909 var_decl);
910 pop_cfun();
913 if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
915 if (no->var_value()->init() == NULL
916 && !no->var_value()->has_pre_init())
917 append_to_statement_list(var_init_tree, &var_init_stmt_list);
918 else
919 var_inits.push_back(Var_init(no, var_init_tree));
921 else if (this->var_depends_on(no->var_value()) != NULL)
923 // This variable is initialized from something that is
924 // not in its init or preinit. This variable needs to
925 // participate in dependency analysis sorting, in case
926 // some other variable depends on this one.
927 var_inits.push_back(Var_init(no, integer_zero_node));
930 if (!is_sink && no->var_value()->type()->has_pointer())
931 var_gc.push_back(no);
935 // Register global variables with the garbage collector.
936 this->register_gc_vars(var_gc, &init_stmt_list);
938 // Simple variable initializations, after all variables are
939 // registered.
940 append_to_statement_list(var_init_stmt_list, &init_stmt_list);
942 // Complex variable initializations, first sorting them into a
943 // workable order.
944 if (!var_inits.empty())
946 sort_var_inits(this, &var_inits);
947 for (Var_inits::const_iterator p = var_inits.begin();
948 p != var_inits.end();
949 ++p)
950 append_to_statement_list(p->init(), &init_stmt_list);
953 // After all the variables are initialized, call the "init"
954 // functions if there are any.
955 for (std::vector<Named_object*>::const_iterator p =
956 this->init_functions_.begin();
957 p != this->init_functions_.end();
958 ++p)
960 tree decl = (*p)->get_tree(this, NULL);
961 tree call = build_call_expr(decl, 0);
962 append_to_statement_list(call, &init_stmt_list);
965 // Set up a magic function to do all the initialization actions.
966 // This will be called if this package is imported.
967 if (init_stmt_list != NULL_TREE
968 || this->need_init_fn_
969 || this->is_main_package())
970 this->write_initialization_function(init_fndecl, init_stmt_list);
972 // We should not have seen any new bindings created during the
973 // conversion.
974 go_assert(count_definitions == this->current_bindings()->size_definitions());
976 // Pass everything back to the middle-end.
978 wrapup_global_declarations(vec, count);
980 finalize_compilation_unit();
982 check_global_declarations(vec, count);
983 emit_debug_global_declarations(vec, count);
985 delete[] vec;
988 // Get a tree for a named object.
990 tree
991 Named_object::get_tree(Gogo* gogo, Named_object* function)
993 if (this->tree_ != NULL_TREE)
994 return this->tree_;
996 if (Gogo::is_erroneous_name(this->name_))
998 this->tree_ = error_mark_node;
999 return error_mark_node;
1002 tree decl;
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;
1012 else
1014 Type* type = named_constant->type();
1015 if (type != NULL && !type->is_abstract())
1017 if (type->is_error())
1018 expr_tree = error_mark_node;
1019 else
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 tree name = get_identifier_from_string(this->get_id(gogo));
1030 decl = build_decl(named_constant->location().gcc_location(),
1031 CONST_DECL, name, TREE_TYPE(expr_tree));
1032 DECL_INITIAL(decl) = expr_tree;
1033 TREE_CONSTANT(decl) = 1;
1034 TREE_READONLY(decl) = 1;
1036 else
1038 // A CONST_DECL is only for an enum constant, so we
1039 // shouldn't use for non-integral types. Instead we
1040 // just return the constant itself, rather than a
1041 // decl.
1042 decl = expr_tree;
1046 break;
1048 case NAMED_OBJECT_TYPE:
1050 Named_type* named_type = this->u_.type_value;
1051 tree type_tree = type_to_tree(named_type->get_backend(gogo));
1052 if (type_tree == error_mark_node)
1053 decl = error_mark_node;
1054 else
1056 decl = TYPE_NAME(type_tree);
1057 go_assert(decl != NULL_TREE);
1059 // We need to produce a type descriptor for every named
1060 // type, and for a pointer to every named type, since
1061 // other files or packages might refer to them. We need
1062 // to do this even for hidden types, because they might
1063 // still be returned by some function. Simply calling the
1064 // type_descriptor method is enough to create the type
1065 // descriptor, even though we don't do anything with it.
1066 if (this->package_ == NULL)
1068 named_type->
1069 type_descriptor_pointer(gogo,
1070 Linemap::predeclared_location());
1071 Type* pn = Type::make_pointer_type(named_type);
1072 pn->type_descriptor_pointer(gogo,
1073 Linemap::predeclared_location());
1077 break;
1079 case NAMED_OBJECT_TYPE_DECLARATION:
1080 error("reference to undefined type %qs",
1081 this->message_name().c_str());
1082 return error_mark_node;
1084 case NAMED_OBJECT_VAR:
1085 case NAMED_OBJECT_RESULT_VAR:
1086 case NAMED_OBJECT_SINK:
1087 go_unreachable();
1089 case NAMED_OBJECT_FUNC:
1091 Function* func = this->u_.func_value;
1092 decl = function_to_tree(func->get_or_make_decl(gogo, this));
1093 if (decl != error_mark_node)
1095 if (func->block() != NULL)
1097 if (DECL_STRUCT_FUNCTION(decl) == NULL)
1098 push_struct_function(decl);
1099 else
1100 push_cfun(DECL_STRUCT_FUNCTION(decl));
1102 cfun->function_start_locus = func->location().gcc_location();
1103 cfun->function_end_locus =
1104 func->block()->end_location().gcc_location();
1106 func->build_tree(gogo, this);
1108 gimplify_function_tree(decl);
1110 cgraph_finalize_function(decl, true);
1112 pop_cfun();
1116 break;
1118 case NAMED_OBJECT_ERRONEOUS:
1119 decl = error_mark_node;
1120 break;
1122 default:
1123 go_unreachable();
1126 if (TREE_TYPE(decl) == error_mark_node)
1127 decl = error_mark_node;
1129 tree ret = decl;
1131 this->tree_ = ret;
1133 if (ret != error_mark_node)
1134 go_preserve_from_gc(ret);
1136 return ret;
1139 // Get the initial value of a variable as a tree. This does not
1140 // consider whether the variable is in the heap--it returns the
1141 // initial value as though it were always stored in the stack.
1143 tree
1144 Variable::get_init_tree(Gogo* gogo, Named_object* function)
1146 go_assert(this->preinit_ == NULL);
1147 if (this->init_ == NULL)
1149 go_assert(!this->is_parameter_);
1150 if (this->is_global_ || this->is_in_heap())
1151 return NULL;
1152 Btype* btype = this->type_->get_backend(gogo);
1153 return expr_to_tree(gogo->backend()->zero_expression(btype));
1155 else
1157 Translate_context context(gogo, function, NULL, NULL);
1158 tree rhs_tree = this->init_->get_tree(&context);
1159 return Expression::convert_for_assignment(&context, this->type(),
1160 this->init_->type(),
1161 rhs_tree, this->location());
1165 // Get the initial value of a variable when a block is required.
1166 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1168 tree
1169 Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
1171 go_assert(this->preinit_ != NULL);
1173 // We want to add the variable assignment to the end of the preinit
1174 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1175 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1176 // regular statements.
1178 Translate_context context(gogo, function, NULL, NULL);
1179 Bblock* bblock = this->preinit_->get_backend(&context);
1180 tree block_tree = block_to_tree(bblock);
1181 if (block_tree == error_mark_node)
1182 return error_mark_node;
1183 go_assert(TREE_CODE(block_tree) == BIND_EXPR);
1184 tree statements = BIND_EXPR_BODY(block_tree);
1185 while (statements != NULL_TREE
1186 && (TREE_CODE(statements) == TRY_FINALLY_EXPR
1187 || TREE_CODE(statements) == TRY_CATCH_EXPR))
1188 statements = TREE_OPERAND(statements, 0);
1190 // It's possible to have pre-init statements without an initializer
1191 // if the pre-init statements set the variable.
1192 if (this->init_ != NULL)
1194 tree rhs_tree = this->init_->get_tree(&context);
1195 if (rhs_tree == error_mark_node)
1196 return error_mark_node;
1197 if (var_decl == NULL_TREE)
1198 append_to_statement_list(rhs_tree, &statements);
1199 else
1201 tree val = Expression::convert_for_assignment(&context, this->type(),
1202 this->init_->type(),
1203 rhs_tree,
1204 this->location());
1205 if (val == error_mark_node)
1206 return error_mark_node;
1207 tree set = fold_build2_loc(this->location().gcc_location(),
1208 MODIFY_EXPR, void_type_node, var_decl,
1209 val);
1210 append_to_statement_list(set, &statements);
1214 return block_tree;
1217 // Get the backend representation.
1219 Bfunction*
1220 Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no)
1222 if (this->fndecl_ == NULL)
1224 // Let Go code use an asm declaration to pick up a builtin
1225 // function.
1226 if (!this->asm_name_.empty())
1228 std::map<std::string, tree>::const_iterator p =
1229 builtin_functions.find(this->asm_name_);
1230 if (p != builtin_functions.end())
1232 this->fndecl_ = tree_to_function(p->second);
1233 return this->fndecl_;
1237 std::string asm_name;
1238 if (this->asm_name_.empty())
1240 asm_name = (no->package() == NULL
1241 ? gogo->pkgpath_symbol()
1242 : no->package()->pkgpath_symbol());
1243 asm_name.append(1, '.');
1244 asm_name.append(Gogo::unpack_hidden_name(no->name()));
1245 if (this->fntype_->is_method())
1247 asm_name.append(1, '.');
1248 Type* rtype = this->fntype_->receiver()->type();
1249 asm_name.append(rtype->mangled_name(gogo));
1253 Btype* functype = this->fntype_->get_backend_fntype(gogo);
1254 this->fndecl_ =
1255 gogo->backend()->function(functype, no->get_id(gogo), asm_name,
1256 true, true, true, false, false,
1257 this->location());
1260 return this->fndecl_;
1263 // Return the function's decl after it has been built.
1265 tree
1266 Function::get_decl() const
1268 go_assert(this->fndecl_ != NULL);
1269 return function_to_tree(this->fndecl_);
1272 // We always pass the receiver to a method as a pointer. If the
1273 // receiver is actually declared as a non-pointer type, then we copy
1274 // the value into a local variable, so that it has the right type. In
1275 // this function we create the real PARM_DECL to use, and set
1276 // DEC_INITIAL of the var_decl to be the value passed in.
1278 tree
1279 Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
1281 if (var_decl == error_mark_node)
1282 return error_mark_node;
1283 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1284 tree val_type = TREE_TYPE(var_decl);
1285 bool is_in_heap = no->var_value()->is_in_heap();
1286 if (is_in_heap)
1288 go_assert(POINTER_TYPE_P(val_type));
1289 val_type = TREE_TYPE(val_type);
1292 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1293 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1294 name += ".pointer";
1295 tree id = get_identifier_from_string(name);
1296 tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
1297 DECL_CONTEXT(parm_decl) = current_function_decl;
1298 DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
1300 go_assert(DECL_INITIAL(var_decl) == NULL_TREE);
1301 tree init = build_fold_indirect_ref_loc(loc, parm_decl);
1303 if (is_in_heap)
1305 tree size = TYPE_SIZE_UNIT(val_type);
1306 tree space = gogo->allocate_memory(no->var_value()->type(), size,
1307 no->location());
1308 space = save_expr(space);
1309 space = fold_convert(build_pointer_type(val_type), space);
1310 tree spaceref = build_fold_indirect_ref_loc(no->location().gcc_location(),
1311 space);
1312 TREE_THIS_NOTRAP(spaceref) = 1;
1313 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1314 spaceref, init);
1315 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space), set, space);
1318 DECL_INITIAL(var_decl) = init;
1320 return parm_decl;
1323 // If we take the address of a parameter, then we need to copy it into
1324 // the heap. We will access it as a local variable via an
1325 // indirection.
1327 tree
1328 Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree var_decl)
1330 if (var_decl == error_mark_node)
1331 return error_mark_node;
1332 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1333 Location loc(DECL_SOURCE_LOCATION(var_decl));
1335 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1336 name += ".param";
1337 tree id = get_identifier_from_string(name);
1339 tree type = TREE_TYPE(var_decl);
1340 go_assert(POINTER_TYPE_P(type));
1341 type = TREE_TYPE(type);
1343 tree parm_decl = build_decl(loc.gcc_location(), PARM_DECL, id, type);
1344 DECL_CONTEXT(parm_decl) = current_function_decl;
1345 DECL_ARG_TYPE(parm_decl) = type;
1347 tree size = TYPE_SIZE_UNIT(type);
1348 tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
1349 space = save_expr(space);
1350 space = fold_convert(TREE_TYPE(var_decl), space);
1351 tree spaceref = build_fold_indirect_ref_loc(loc.gcc_location(), space);
1352 TREE_THIS_NOTRAP(spaceref) = 1;
1353 tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
1354 build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
1355 space);
1356 DECL_INITIAL(var_decl) = init;
1358 return parm_decl;
1361 // Get a tree for function code.
1363 void
1364 Function::build_tree(Gogo* gogo, Named_object* named_function)
1366 tree fndecl = this->get_decl();
1367 go_assert(fndecl != NULL_TREE);
1369 tree params = NULL_TREE;
1370 tree* pp = &params;
1372 tree declare_vars = NULL_TREE;
1373 for (Bindings::const_definitions_iterator p =
1374 this->block_->bindings()->begin_definitions();
1375 p != this->block_->bindings()->end_definitions();
1376 ++p)
1378 if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
1380 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1381 *pp = var_to_tree(bvar);
1383 // We always pass the receiver to a method as a pointer. If
1384 // the receiver is declared as a non-pointer type, then we
1385 // copy the value into a local variable.
1386 if ((*p)->var_value()->is_receiver()
1387 && (*p)->var_value()->type()->points_to() == NULL)
1389 tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
1390 tree var = *pp;
1391 if (var != error_mark_node)
1393 go_assert(TREE_CODE(var) == VAR_DECL);
1394 DECL_CHAIN(var) = declare_vars;
1395 declare_vars = var;
1397 *pp = parm_decl;
1399 else if ((*p)->var_value()->is_in_heap())
1401 // If we take the address of a parameter, then we need
1402 // to copy it into the heap.
1403 tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
1404 tree var = *pp;
1405 if (var != error_mark_node)
1407 go_assert(TREE_CODE(var) == VAR_DECL);
1408 DECL_CHAIN(var) = declare_vars;
1409 declare_vars = var;
1411 *pp = parm_decl;
1414 if (*pp != error_mark_node)
1416 go_assert(TREE_CODE(*pp) == PARM_DECL);
1417 pp = &DECL_CHAIN(*pp);
1420 else if ((*p)->is_result_variable())
1422 Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1423 tree var_decl = var_to_tree(bvar);
1425 Type* type = (*p)->result_var_value()->type();
1426 tree init;
1427 if (!(*p)->result_var_value()->is_in_heap())
1429 Btype* btype = type->get_backend(gogo);
1430 init = expr_to_tree(gogo->backend()->zero_expression(btype));
1432 else
1434 Location loc = (*p)->location();
1435 tree type_tree = type_to_tree(type->get_backend(gogo));
1436 tree space = gogo->allocate_memory(type,
1437 TYPE_SIZE_UNIT(type_tree),
1438 loc);
1439 tree ptr_type_tree = build_pointer_type(type_tree);
1440 init = fold_convert_loc(loc.gcc_location(), ptr_type_tree, space);
1443 if (var_decl != error_mark_node)
1445 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1446 DECL_INITIAL(var_decl) = init;
1447 DECL_CHAIN(var_decl) = declare_vars;
1448 declare_vars = var_decl;
1453 *pp = NULL_TREE;
1455 DECL_ARGUMENTS(fndecl) = params;
1457 // If we need a closure variable, fetch it by calling a runtime
1458 // function. The caller will have called __go_set_closure before
1459 // the function call.
1460 if (this->closure_var_ != NULL)
1462 Bvariable* bvar =
1463 this->closure_var_->get_backend_variable(gogo, named_function);
1464 tree var_decl = var_to_tree(bvar);
1465 if (var_decl != error_mark_node)
1467 go_assert(TREE_CODE(var_decl) == VAR_DECL);
1468 static tree get_closure_fndecl;
1469 tree get_closure = Gogo::call_builtin(&get_closure_fndecl,
1470 this->location_,
1471 "__go_get_closure",
1473 ptr_type_node);
1475 // Mark the __go_get_closure function as pure, since it
1476 // depends only on the global variable g.
1477 DECL_PURE_P(get_closure_fndecl) = 1;
1479 get_closure = fold_convert_loc(this->location_.gcc_location(),
1480 TREE_TYPE(var_decl), get_closure);
1481 DECL_INITIAL(var_decl) = get_closure;
1482 DECL_CHAIN(var_decl) = declare_vars;
1483 declare_vars = var_decl;
1487 if (this->block_ != NULL)
1489 go_assert(DECL_INITIAL(fndecl) == NULL_TREE);
1491 // Declare variables if necessary.
1492 tree bind = NULL_TREE;
1493 tree defer_init = NULL_TREE;
1494 if (declare_vars != NULL_TREE || this->defer_stack_ != NULL)
1496 tree block = make_node(BLOCK);
1497 BLOCK_SUPERCONTEXT(block) = fndecl;
1498 DECL_INITIAL(fndecl) = block;
1499 BLOCK_VARS(block) = declare_vars;
1500 TREE_USED(block) = 1;
1502 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
1503 NULL_TREE, block);
1504 TREE_SIDE_EFFECTS(bind) = 1;
1506 if (this->defer_stack_ != NULL)
1508 Translate_context dcontext(gogo, named_function, this->block_,
1509 tree_to_block(bind));
1510 Bstatement* bdi = this->defer_stack_->get_backend(&dcontext);
1511 defer_init = stat_to_tree(bdi);
1515 // Build the trees for all the statements in the function.
1516 Translate_context context(gogo, named_function, NULL, NULL);
1517 Bblock* bblock = this->block_->get_backend(&context);
1518 tree code = block_to_tree(bblock);
1520 tree init = NULL_TREE;
1521 tree except = NULL_TREE;
1522 tree fini = NULL_TREE;
1524 // Initialize variables if necessary.
1525 for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
1527 tree dv = build1(DECL_EXPR, void_type_node, v);
1528 SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
1529 append_to_statement_list(dv, &init);
1532 // If we have a defer stack, initialize it at the start of a
1533 // function.
1534 if (defer_init != NULL_TREE && defer_init != error_mark_node)
1536 SET_EXPR_LOCATION(defer_init,
1537 this->block_->start_location().gcc_location());
1538 append_to_statement_list(defer_init, &init);
1540 // Clean up the defer stack when we leave the function.
1541 this->build_defer_wrapper(gogo, named_function, &except, &fini);
1544 if (code != NULL_TREE && code != error_mark_node)
1546 if (init != NULL_TREE)
1547 code = build2(COMPOUND_EXPR, void_type_node, init, code);
1548 if (except != NULL_TREE)
1549 code = build2(TRY_CATCH_EXPR, void_type_node, code,
1550 build2(CATCH_EXPR, void_type_node, NULL, except));
1551 if (fini != NULL_TREE)
1552 code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
1555 // Stick the code into the block we built for the receiver, if
1556 // we built on.
1557 if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
1559 BIND_EXPR_BODY(bind) = code;
1560 code = bind;
1563 DECL_SAVED_TREE(fndecl) = code;
1566 // If we created a descriptor for the function, make sure we emit it.
1567 if (this->descriptor_ != NULL)
1569 Translate_context context(gogo, NULL, NULL, NULL);
1570 this->descriptor_->get_tree(&context);
1574 // Build the wrappers around function code needed if the function has
1575 // any defer statements. This sets *EXCEPT to an exception handler
1576 // and *FINI to a finally handler.
1578 void
1579 Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
1580 tree *except, tree *fini)
1582 Location end_loc = this->block_->end_location();
1584 // Add an exception handler. This is used if a panic occurs. Its
1585 // purpose is to stop the stack unwinding if a deferred function
1586 // calls recover. There are more details in
1587 // libgo/runtime/go-unwind.c.
1589 tree stmt_list = NULL_TREE;
1591 Expression* call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1592 this->defer_stack(end_loc));
1593 Translate_context context(gogo, named_function, NULL, NULL);
1594 tree call_tree = call->get_tree(&context);
1595 if (call_tree != error_mark_node)
1596 append_to_statement_list(call_tree, &stmt_list);
1598 tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
1599 tree set;
1600 if (retval == NULL_TREE)
1601 set = NULL_TREE;
1602 else
1603 set = fold_build2_loc(end_loc.gcc_location(), MODIFY_EXPR, void_type_node,
1604 DECL_RESULT(this->get_decl()), retval);
1605 tree ret_stmt = fold_build1_loc(end_loc.gcc_location(), RETURN_EXPR,
1606 void_type_node, set);
1607 append_to_statement_list(ret_stmt, &stmt_list);
1609 go_assert(*except == NULL_TREE);
1610 *except = stmt_list;
1612 // Add some finally code to run the defer functions. This is used
1613 // both in the normal case, when no panic occurs, and also if a
1614 // panic occurs to run any further defer functions. Of course, it
1615 // is possible for a defer function to call panic which should be
1616 // caught by another defer function. To handle that we use a loop.
1617 // finish:
1618 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1619 // if (return values are named) return named_vals;
1621 stmt_list = NULL;
1623 tree label = create_artificial_label(end_loc.gcc_location());
1624 tree define_label = fold_build1_loc(end_loc.gcc_location(), LABEL_EXPR,
1625 void_type_node, label);
1626 append_to_statement_list(define_label, &stmt_list);
1628 call = Runtime::make_call(Runtime::UNDEFER, end_loc, 1,
1629 this->defer_stack(end_loc));
1630 tree undefer = call->get_tree(&context);
1632 call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1633 this->defer_stack(end_loc));
1634 tree defer = call->get_tree(&context);
1636 if (undefer == error_mark_node || defer == error_mark_node)
1637 return;
1639 tree jump = fold_build1_loc(end_loc.gcc_location(), GOTO_EXPR, void_type_node,
1640 label);
1641 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
1642 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
1643 tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
1645 append_to_statement_list(try_catch, &stmt_list);
1647 if (this->type_->results() != NULL
1648 && !this->type_->results()->empty()
1649 && !this->type_->results()->front().name().empty())
1651 // If the result variables are named, and we are returning from
1652 // this function rather than panicing through it, we need to
1653 // return them again, because they might have been changed by a
1654 // defer function. The runtime routines set the defer_stack
1655 // variable to true if we are returning from this function.
1656 retval = this->return_value(gogo, named_function, end_loc,
1657 &stmt_list);
1658 set = fold_build2_loc(end_loc.gcc_location(), MODIFY_EXPR, void_type_node,
1659 DECL_RESULT(this->get_decl()), retval);
1660 ret_stmt = fold_build1_loc(end_loc.gcc_location(), RETURN_EXPR,
1661 void_type_node, set);
1663 Expression* ref =
1664 Expression::make_temporary_reference(this->defer_stack_, end_loc);
1665 tree tref = ref->get_tree(&context);
1666 tree s = build3_loc(end_loc.gcc_location(), COND_EXPR, void_type_node,
1667 tref, ret_stmt, NULL_TREE);
1669 append_to_statement_list(s, &stmt_list);
1673 go_assert(*fini == NULL_TREE);
1674 *fini = stmt_list;
1677 // Return the value to assign to DECL_RESULT(this->get_decl()). This may
1678 // also add statements to STMT_LIST, which need to be executed before
1679 // the assignment. This is used for a return statement with no
1680 // explicit values.
1682 tree
1683 Function::return_value(Gogo* gogo, Named_object* named_function,
1684 Location location, tree* stmt_list) const
1686 const Typed_identifier_list* results = this->type_->results();
1687 if (results == NULL || results->empty())
1688 return NULL_TREE;
1690 go_assert(this->results_ != NULL);
1691 if (this->results_->size() != results->size())
1693 go_assert(saw_errors());
1694 return error_mark_node;
1697 tree retval;
1698 if (results->size() == 1)
1700 Bvariable* bvar =
1701 this->results_->front()->get_backend_variable(gogo,
1702 named_function);
1703 tree ret = var_to_tree(bvar);
1704 if (this->results_->front()->result_var_value()->is_in_heap())
1705 ret = build_fold_indirect_ref_loc(location.gcc_location(), ret);
1706 return ret;
1708 else
1710 tree rettype = TREE_TYPE(DECL_RESULT(this->get_decl()));
1711 retval = create_tmp_var(rettype, "RESULT");
1712 tree field = TYPE_FIELDS(rettype);
1713 int index = 0;
1714 for (Typed_identifier_list::const_iterator pr = results->begin();
1715 pr != results->end();
1716 ++pr, ++index, field = DECL_CHAIN(field))
1718 go_assert(field != NULL);
1719 Named_object* no = (*this->results_)[index];
1720 Bvariable* bvar = no->get_backend_variable(gogo, named_function);
1721 tree val = var_to_tree(bvar);
1722 if (no->result_var_value()->is_in_heap())
1723 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
1724 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1725 void_type_node,
1726 build3(COMPONENT_REF, TREE_TYPE(field),
1727 retval, field, NULL_TREE),
1728 val);
1729 append_to_statement_list(set, stmt_list);
1731 return retval;
1735 // Build the descriptor for a function declaration. This won't
1736 // necessarily happen if the package has just a declaration for the
1737 // function and no other reference to it, but we may still need the
1738 // descriptor for references from other packages.
1739 void
1740 Function_declaration::build_backend_descriptor(Gogo* gogo)
1742 if (this->descriptor_ != NULL)
1744 Translate_context context(gogo, NULL, NULL, NULL);
1745 this->descriptor_->get_tree(&context);
1749 // Return the integer type to use for a size.
1751 GO_EXTERN_C
1752 tree
1753 go_type_for_size(unsigned int bits, int unsignedp)
1755 const char* name;
1756 switch (bits)
1758 case 8:
1759 name = unsignedp ? "uint8" : "int8";
1760 break;
1761 case 16:
1762 name = unsignedp ? "uint16" : "int16";
1763 break;
1764 case 32:
1765 name = unsignedp ? "uint32" : "int32";
1766 break;
1767 case 64:
1768 name = unsignedp ? "uint64" : "int64";
1769 break;
1770 default:
1771 if (bits == POINTER_SIZE && unsignedp)
1772 name = "uintptr";
1773 else
1774 return NULL_TREE;
1776 Type* type = Type::lookup_integer_type(name);
1777 return type_to_tree(type->get_backend(go_get_gogo()));
1780 // Return the type to use for a mode.
1782 GO_EXTERN_C
1783 tree
1784 go_type_for_mode(enum machine_mode mode, int unsignedp)
1786 // FIXME: This static_cast should be in machmode.h.
1787 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
1788 if (mc == MODE_INT)
1789 return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
1790 else if (mc == MODE_FLOAT)
1792 Type* type;
1793 switch (GET_MODE_BITSIZE (mode))
1795 case 32:
1796 type = Type::lookup_float_type("float32");
1797 break;
1798 case 64:
1799 type = Type::lookup_float_type("float64");
1800 break;
1801 default:
1802 // We have to check for long double in order to support
1803 // i386 excess precision.
1804 if (mode == TYPE_MODE(long_double_type_node))
1805 return long_double_type_node;
1806 return NULL_TREE;
1808 return type_to_tree(type->get_backend(go_get_gogo()));
1810 else if (mc == MODE_COMPLEX_FLOAT)
1812 Type *type;
1813 switch (GET_MODE_BITSIZE (mode))
1815 case 64:
1816 type = Type::lookup_complex_type("complex64");
1817 break;
1818 case 128:
1819 type = Type::lookup_complex_type("complex128");
1820 break;
1821 default:
1822 // We have to check for long double in order to support
1823 // i386 excess precision.
1824 if (mode == TYPE_MODE(complex_long_double_type_node))
1825 return complex_long_double_type_node;
1826 return NULL_TREE;
1828 return type_to_tree(type->get_backend(go_get_gogo()));
1830 else
1831 return NULL_TREE;
1834 // Return a tree which allocates SIZE bytes which will holds value of
1835 // type TYPE.
1837 tree
1838 Gogo::allocate_memory(Type* type, tree size, Location location)
1840 // If the package imports unsafe, then it may play games with
1841 // pointers that look like integers.
1842 if (this->imported_unsafe_ || type->has_pointer())
1844 static tree new_fndecl;
1845 return Gogo::call_builtin(&new_fndecl,
1846 location,
1847 "__go_new",
1849 ptr_type_node,
1850 sizetype,
1851 size);
1853 else
1855 static tree new_nopointers_fndecl;
1856 return Gogo::call_builtin(&new_nopointers_fndecl,
1857 location,
1858 "__go_new_nopointers",
1860 ptr_type_node,
1861 sizetype,
1862 size);
1866 // Build a builtin struct with a list of fields. The name is
1867 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
1868 // node; this exists so that the struct can have fields which point to
1869 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
1870 // are NFIELDS fields. Each field is a name (a const char*) followed
1871 // by a type (a tree).
1873 tree
1874 Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
1875 int nfields, ...)
1877 if (ptype != NULL && *ptype != NULL_TREE)
1878 return *ptype;
1880 va_list ap;
1881 va_start(ap, nfields);
1883 tree fields = NULL_TREE;
1884 for (int i = 0; i < nfields; ++i)
1886 const char* field_name = va_arg(ap, const char*);
1887 tree type = va_arg(ap, tree);
1888 if (type == error_mark_node)
1890 if (ptype != NULL)
1891 *ptype = error_mark_node;
1892 return error_mark_node;
1894 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
1895 get_identifier(field_name), type);
1896 DECL_CHAIN(field) = fields;
1897 fields = field;
1900 va_end(ap);
1902 if (struct_type == NULL_TREE)
1903 struct_type = make_node(RECORD_TYPE);
1904 finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
1906 if (ptype != NULL)
1908 go_preserve_from_gc(struct_type);
1909 *ptype = struct_type;
1912 return struct_type;
1915 // Return a type to use for pointer to const char for a string.
1917 tree
1918 Gogo::const_char_pointer_type_tree()
1920 static tree type;
1921 if (type == NULL_TREE)
1923 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1924 TYPE_QUAL_CONST);
1925 type = build_pointer_type(const_char_type);
1926 go_preserve_from_gc(type);
1928 return type;
1931 // Return a tree for a string constant.
1933 tree
1934 Gogo::string_constant_tree(const std::string& val)
1936 tree index_type = build_index_type(size_int(val.length()));
1937 tree const_char_type = build_qualified_type(unsigned_char_type_node,
1938 TYPE_QUAL_CONST);
1939 tree string_type = build_array_type(const_char_type, index_type);
1940 string_type = build_variant_type_copy(string_type);
1941 TYPE_STRING_FLAG(string_type) = 1;
1942 tree string_val = build_string(val.length(), val.data());
1943 TREE_TYPE(string_val) = string_type;
1944 return string_val;
1947 // Return a tree for a Go string constant.
1949 tree
1950 Gogo::go_string_constant_tree(const std::string& val)
1952 tree string_type = type_to_tree(Type::make_string_type()->get_backend(this));
1954 vec<constructor_elt, va_gc> *init;
1955 vec_alloc(init, 2);
1957 constructor_elt empty = {NULL, NULL};
1958 constructor_elt* elt = init->quick_push(empty);
1959 tree field = TYPE_FIELDS(string_type);
1960 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
1961 elt->index = field;
1962 tree str = Gogo::string_constant_tree(val);
1963 elt->value = fold_convert(TREE_TYPE(field),
1964 build_fold_addr_expr(str));
1966 elt = init->quick_push(empty);
1967 field = DECL_CHAIN(field);
1968 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
1969 elt->index = field;
1970 elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
1972 tree constructor = build_constructor(string_type, init);
1973 TREE_READONLY(constructor) = 1;
1974 TREE_CONSTANT(constructor) = 1;
1976 return constructor;
1979 // Return a tree for a pointer to a Go string constant. This is only
1980 // used for type descriptors, so we return a pointer to a constant
1981 // decl.
1983 tree
1984 Gogo::ptr_go_string_constant_tree(const std::string& val)
1986 tree pval = this->go_string_constant_tree(val);
1988 tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
1989 create_tmp_var_name("SP"), TREE_TYPE(pval));
1990 DECL_EXTERNAL(decl) = 0;
1991 TREE_PUBLIC(decl) = 0;
1992 TREE_USED(decl) = 1;
1993 TREE_READONLY(decl) = 1;
1994 TREE_CONSTANT(decl) = 1;
1995 TREE_STATIC(decl) = 1;
1996 DECL_ARTIFICIAL(decl) = 1;
1997 DECL_INITIAL(decl) = pval;
1998 rest_of_decl_compilation(decl, 1, 0);
2000 return build_fold_addr_expr(decl);
2003 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2004 // the slice. VALUES is the value pointer and COUNT is the number of
2005 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2006 // the capacity and the count are the same.
2008 tree
2009 Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
2010 tree capacity)
2012 go_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
2014 vec<constructor_elt, va_gc> *init;
2015 vec_alloc(init, 3);
2017 tree field = TYPE_FIELDS(slice_type_tree);
2018 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
2019 constructor_elt empty = {NULL, NULL};
2020 constructor_elt* elt = init->quick_push(empty);
2021 elt->index = field;
2022 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
2023 == TYPE_MAIN_VARIANT(TREE_TYPE(values)));
2024 elt->value = values;
2026 count = fold_convert(sizetype, count);
2027 if (capacity == NULL_TREE)
2029 count = save_expr(count);
2030 capacity = count;
2033 field = DECL_CHAIN(field);
2034 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
2035 elt = init->quick_push(empty);
2036 elt->index = field;
2037 elt->value = fold_convert(TREE_TYPE(field), count);
2039 field = DECL_CHAIN(field);
2040 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
2041 elt = init->quick_push(empty);
2042 elt->index = field;
2043 elt->value = fold_convert(TREE_TYPE(field), capacity);
2045 return build_constructor(slice_type_tree, init);
2048 // Build an interface method table for a type: a list of function
2049 // pointers, one for each interface method. This is used for
2050 // interfaces.
2052 tree
2053 Gogo::interface_method_table_for_type(const Interface_type* interface,
2054 Type* type, bool is_pointer)
2056 const Typed_identifier_list* interface_methods = interface->methods();
2057 go_assert(!interface_methods->empty());
2059 std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
2060 + interface->mangled_name(this)
2061 + "__"
2062 + type->mangled_name(this));
2064 tree id = get_identifier_from_string(mangled_name);
2066 // See whether this interface has any hidden methods.
2067 bool has_hidden_methods = false;
2068 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2069 p != interface_methods->end();
2070 ++p)
2072 if (Gogo::is_hidden_name(p->name()))
2074 has_hidden_methods = true;
2075 break;
2079 // We already know that the named type is convertible to the
2080 // interface. If the interface has hidden methods, and the named
2081 // type is defined in a different package, then the interface
2082 // conversion table will be defined by that other package.
2083 if (has_hidden_methods
2084 && type->named_type() != NULL
2085 && type->named_type()->named_object()->package() != NULL)
2087 tree array_type = build_array_type(const_ptr_type_node, NULL);
2088 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2089 TREE_READONLY(decl) = 1;
2090 TREE_CONSTANT(decl) = 1;
2091 TREE_PUBLIC(decl) = 1;
2092 DECL_EXTERNAL(decl) = 1;
2093 go_preserve_from_gc(decl);
2094 return decl;
2097 size_t count = interface_methods->size();
2098 vec<constructor_elt, va_gc> *pointers;
2099 vec_alloc(pointers, count + 1);
2101 // The first element is the type descriptor.
2102 constructor_elt empty = {NULL, NULL};
2103 constructor_elt* elt = pointers->quick_push(empty);
2104 elt->index = size_zero_node;
2105 Type* td_type;
2106 if (!is_pointer)
2107 td_type = type;
2108 else
2109 td_type = Type::make_pointer_type(type);
2110 tree tdp = td_type->type_descriptor_pointer(this,
2111 Linemap::predeclared_location());
2112 elt->value = fold_convert(const_ptr_type_node, tdp);
2114 Named_type* nt = type->named_type();
2115 Struct_type* st = type->struct_type();
2116 go_assert(nt != NULL || st != NULL);
2117 size_t i = 1;
2118 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2119 p != interface_methods->end();
2120 ++p, ++i)
2122 bool is_ambiguous;
2123 Method* m;
2124 if (nt != NULL)
2125 m = nt->method_function(p->name(), &is_ambiguous);
2126 else
2127 m = st->method_function(p->name(), &is_ambiguous);
2128 go_assert(m != NULL);
2130 Named_object* no = m->named_object();
2131 Bfunction* bf;
2132 if (no->is_function())
2133 bf = no->func_value()->get_or_make_decl(this, no);
2134 else if (no->is_function_declaration())
2135 bf = no->func_declaration_value()->get_or_make_decl(this, no);
2136 else
2137 go_unreachable();
2138 tree fndecl = build_fold_addr_expr(function_to_tree(bf));
2140 elt = pointers->quick_push(empty);
2141 elt->index = size_int(i);
2142 elt->value = fold_convert(const_ptr_type_node, fndecl);
2144 go_assert(i == count + 1);
2146 tree array_type = build_array_type(const_ptr_type_node,
2147 build_index_type(size_int(count)));
2148 tree constructor = build_constructor(array_type, pointers);
2150 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2151 TREE_STATIC(decl) = 1;
2152 TREE_USED(decl) = 1;
2153 TREE_READONLY(decl) = 1;
2154 TREE_CONSTANT(decl) = 1;
2155 DECL_INITIAL(decl) = constructor;
2157 // If the interface type has hidden methods, and the table is for a
2158 // named type, then this is the only definition of the table.
2159 // Otherwise it is a comdat table which may be defined in multiple
2160 // packages.
2161 if (has_hidden_methods && type->named_type() != NULL)
2162 TREE_PUBLIC(decl) = 1;
2163 else
2165 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2166 resolve_unique_section(decl, 1, 0);
2169 rest_of_decl_compilation(decl, 1, 0);
2171 go_preserve_from_gc(decl);
2173 return decl;
2176 // Mark a function as a builtin library function.
2178 void
2179 Gogo::mark_fndecl_as_builtin_library(tree fndecl)
2181 DECL_EXTERNAL(fndecl) = 1;
2182 TREE_PUBLIC(fndecl) = 1;
2183 DECL_ARTIFICIAL(fndecl) = 1;
2184 TREE_NOTHROW(fndecl) = 1;
2185 DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
2186 DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
2189 // Build a call to a builtin function.
2191 tree
2192 Gogo::call_builtin(tree* pdecl, Location location, const char* name,
2193 int nargs, tree rettype, ...)
2195 if (rettype == error_mark_node)
2196 return error_mark_node;
2198 tree* types = new tree[nargs];
2199 tree* args = new tree[nargs];
2201 va_list ap;
2202 va_start(ap, rettype);
2203 for (int i = 0; i < nargs; ++i)
2205 types[i] = va_arg(ap, tree);
2206 args[i] = va_arg(ap, tree);
2207 if (types[i] == error_mark_node || args[i] == error_mark_node)
2209 delete[] types;
2210 delete[] args;
2211 return error_mark_node;
2214 va_end(ap);
2216 if (*pdecl == NULL_TREE)
2218 tree fnid = get_identifier(name);
2220 tree argtypes = NULL_TREE;
2221 tree* pp = &argtypes;
2222 for (int i = 0; i < nargs; ++i)
2224 *pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
2225 pp = &TREE_CHAIN(*pp);
2227 *pp = void_list_node;
2229 tree fntype = build_function_type(rettype, argtypes);
2231 *pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
2232 Gogo::mark_fndecl_as_builtin_library(*pdecl);
2233 go_preserve_from_gc(*pdecl);
2236 tree fnptr = build_fold_addr_expr(*pdecl);
2237 if (CAN_HAVE_LOCATION_P(fnptr))
2238 SET_EXPR_LOCATION(fnptr, location.gcc_location());
2240 tree ret = build_call_array(rettype, fnptr, nargs, args);
2241 SET_EXPR_LOCATION(ret, location.gcc_location());
2243 delete[] types;
2244 delete[] args;
2246 return ret;
2249 // Build a call to the runtime error function.
2251 tree
2252 Gogo::runtime_error(int code, Location location)
2254 Type* int32_type = Type::lookup_integer_type("int32");
2255 tree int32_type_tree = type_to_tree(int32_type->get_backend(this));
2257 static tree runtime_error_fndecl;
2258 tree ret = Gogo::call_builtin(&runtime_error_fndecl,
2259 location,
2260 "__go_runtime_error",
2262 void_type_node,
2263 int32_type_tree,
2264 build_int_cst(int32_type_tree, code));
2265 if (ret == error_mark_node)
2266 return error_mark_node;
2267 // The runtime error function panics and does not return.
2268 TREE_NOTHROW(runtime_error_fndecl) = 0;
2269 TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
2270 return ret;
2273 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2274 // TYPE_DESCRIPTOR_TREE is the channel's type descriptor. This does a
2275 // blocking receive and returns the value read from the channel.
2277 tree
2278 Gogo::receive_from_channel(tree type_tree, tree type_descriptor_tree,
2279 tree channel, Location location)
2281 if (type_tree == error_mark_node || channel == error_mark_node)
2282 return error_mark_node;
2284 if (int_size_in_bytes(type_tree) <= 8
2285 && !AGGREGATE_TYPE_P(type_tree)
2286 && !FLOAT_TYPE_P(type_tree))
2288 static tree receive_small_fndecl;
2289 tree call = Gogo::call_builtin(&receive_small_fndecl,
2290 location,
2291 "__go_receive_small",
2293 uint64_type_node,
2294 TREE_TYPE(type_descriptor_tree),
2295 type_descriptor_tree,
2296 ptr_type_node,
2297 channel);
2298 if (call == error_mark_node)
2299 return error_mark_node;
2300 // This can panic if there are too many operations on a closed
2301 // channel.
2302 TREE_NOTHROW(receive_small_fndecl) = 0;
2303 int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
2304 tree int_type_tree = go_type_for_size(bitsize, 1);
2305 return fold_convert_loc(location.gcc_location(), type_tree,
2306 fold_convert_loc(location.gcc_location(),
2307 int_type_tree, call));
2309 else
2311 tree tmp = create_tmp_var(type_tree, get_name(type_tree));
2312 DECL_IGNORED_P(tmp) = 0;
2313 TREE_ADDRESSABLE(tmp) = 1;
2314 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
2315 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
2316 tree tmpaddr = build_fold_addr_expr(tmp);
2317 tmpaddr = fold_convert(ptr_type_node, tmpaddr);
2318 static tree receive_big_fndecl;
2319 tree call = Gogo::call_builtin(&receive_big_fndecl,
2320 location,
2321 "__go_receive_big",
2323 void_type_node,
2324 TREE_TYPE(type_descriptor_tree),
2325 type_descriptor_tree,
2326 ptr_type_node,
2327 channel,
2328 ptr_type_node,
2329 tmpaddr);
2330 if (call == error_mark_node)
2331 return error_mark_node;
2332 // This can panic if there are too many operations on a closed
2333 // channel.
2334 TREE_NOTHROW(receive_big_fndecl) = 0;
2335 return build2(COMPOUND_EXPR, type_tree, make_tmp,
2336 build2(COMPOUND_EXPR, type_tree, call, tmp));