OSDN Git Service

Define go_assert to replace gcc_assert
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / gogo-tree.cc
1 // gogo-tree.cc -- convert Go frontend Gogo IR to gcc trees.
2
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.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "toplev.h"
17 #include "tree.h"
18 #include "gimple.h"
19 #include "tree-iterator.h"
20 #include "cgraph.h"
21 #include "langhooks.h"
22 #include "convert.h"
23 #include "output.h"
24 #include "diagnostic.h"
25
26 #ifndef ENABLE_BUILD_WITH_CXX
27 }
28 #endif
29
30 #include "go-c.h"
31 #include "types.h"
32 #include "expressions.h"
33 #include "statements.h"
34 #include "runtime.h"
35 #include "backend.h"
36 #include "gogo.h"
37
38 // Whether we have seen any errors.
39
40 bool
41 saw_errors()
42 {
43   return errorcount != 0 || sorrycount != 0;
44 }
45
46 // A helper function.
47
48 static inline tree
49 get_identifier_from_string(const std::string& str)
50 {
51   return get_identifier_with_length(str.data(), str.length());
52 }
53
54 // Builtin functions.
55
56 static std::map<std::string, tree> builtin_functions;
57
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.
63
64 static void
65 define_builtin(built_in_function bcode, const char* name, const char* libname,
66                tree fntype, bool const_p)
67 {
68   tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
69                                    libname, NULL_TREE);
70   if (const_p)
71     TREE_READONLY(decl) = 1;
72   built_in_decls[bcode] = decl;
73   implicit_built_in_decls[bcode] = decl;
74   builtin_functions[name] = decl;
75   if (libname != NULL)
76     {
77       decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
78                                   NULL, NULL_TREE);
79       if (const_p)
80         TREE_READONLY(decl) = 1;
81       builtin_functions[libname] = decl;
82     }
83 }
84
85 // Create trees for implicit builtin functions.
86
87 void
88 Gogo::define_builtin_function_trees()
89 {
90   /* We need to define the fetch_and_add functions, since we use them
91      for ++ and --.  */
92   tree t = go_type_for_size(BITS_PER_UNIT, 1);
93   tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
94   define_builtin(BUILT_IN_ADD_AND_FETCH_1, "__sync_fetch_and_add_1", NULL,
95                  build_function_type_list(t, p, t, NULL_TREE), false);
96
97   t = go_type_for_size(BITS_PER_UNIT * 2, 1);
98   p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
99   define_builtin (BUILT_IN_ADD_AND_FETCH_2, "__sync_fetch_and_add_2", NULL,
100                   build_function_type_list(t, p, t, NULL_TREE), false);
101
102   t = go_type_for_size(BITS_PER_UNIT * 4, 1);
103   p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
104   define_builtin(BUILT_IN_ADD_AND_FETCH_4, "__sync_fetch_and_add_4", NULL,
105                  build_function_type_list(t, p, t, NULL_TREE), false);
106
107   t = go_type_for_size(BITS_PER_UNIT * 8, 1);
108   p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
109   define_builtin(BUILT_IN_ADD_AND_FETCH_8, "__sync_fetch_and_add_8", NULL,
110                  build_function_type_list(t, p, t, NULL_TREE), false);
111
112   // We use __builtin_expect for magic import functions.
113   define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
114                  build_function_type_list(long_integer_type_node,
115                                           long_integer_type_node,
116                                           long_integer_type_node,
117                                           NULL_TREE),
118                  true);
119
120   // We use __builtin_memmove for the predeclared copy function.
121   define_builtin(BUILT_IN_MEMMOVE, "__builtin_memmove", "memmove",
122                  build_function_type_list(ptr_type_node,
123                                           ptr_type_node,
124                                           const_ptr_type_node,
125                                           size_type_node,
126                                           NULL_TREE),
127                  false);
128
129   // We provide sqrt for the math library.
130   define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
131                  build_function_type_list(double_type_node,
132                                           double_type_node,
133                                           NULL_TREE),
134                  true);
135   define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
136                  build_function_type_list(long_double_type_node,
137                                           long_double_type_node,
138                                           NULL_TREE),
139                  true);
140
141   // We use __builtin_return_address in the thunk we build for
142   // functions which call recover.
143   define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address", NULL,
144                  build_function_type_list(ptr_type_node,
145                                           unsigned_type_node,
146                                           NULL_TREE),
147                  false);
148
149   // The compiler uses __builtin_trap for some exception handling
150   // cases.
151   define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
152                  build_function_type(void_type_node, void_list_node),
153                  false);
154 }
155
156 // Get the name to use for the import control function.  If there is a
157 // global function or variable, then we know that that name must be
158 // unique in the link, and we use it as the basis for our name.
159
160 const std::string&
161 Gogo::get_init_fn_name()
162 {
163   if (this->init_fn_name_.empty())
164     {
165       go_assert(this->package_ != NULL);
166       if (this->is_main_package())
167         {
168           // Use a name which the runtime knows.
169           this->init_fn_name_ = "__go_init_main";
170         }
171       else
172         {
173           std::string s = this->unique_prefix();
174           s.append(1, '.');
175           s.append(this->package_name());
176           s.append("..import");
177           this->init_fn_name_ = s;
178         }
179     }
180
181   return this->init_fn_name_;
182 }
183
184 // Add statements to INIT_STMT_LIST which run the initialization
185 // functions for imported packages.  This is only used for the "main"
186 // package.
187
188 void
189 Gogo::init_imports(tree* init_stmt_list)
190 {
191   go_assert(this->is_main_package());
192
193   if (this->imported_init_fns_.empty())
194     return;
195
196   tree fntype = build_function_type(void_type_node, void_list_node);
197
198   // We must call them in increasing priority order.
199   std::vector<Import_init> v;
200   for (std::set<Import_init>::const_iterator p =
201          this->imported_init_fns_.begin();
202        p != this->imported_init_fns_.end();
203        ++p)
204     v.push_back(*p);
205   std::sort(v.begin(), v.end());
206
207   for (std::vector<Import_init>::const_iterator p = v.begin();
208        p != v.end();
209        ++p)
210     {
211       std::string user_name = p->package_name() + ".init";
212       tree decl = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL,
213                              get_identifier_from_string(user_name),
214                              fntype);
215       const std::string& init_name(p->init_name());
216       SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(init_name));
217       TREE_PUBLIC(decl) = 1;
218       DECL_EXTERNAL(decl) = 1;
219       append_to_statement_list(build_call_expr(decl, 0), init_stmt_list);
220     }
221 }
222
223 // Register global variables with the garbage collector.  We need to
224 // register all variables which can hold a pointer value.  They become
225 // roots during the mark phase.  We build a struct that is easy to
226 // hook into a list of roots.
227
228 // struct __go_gc_root_list
229 // {
230 //   struct __go_gc_root_list* __next;
231 //   struct __go_gc_root
232 //   {
233 //     void* __decl;
234 //     size_t __size;
235 //   } __roots[];
236 // };
237
238 // The last entry in the roots array has a NULL decl field.
239
240 void
241 Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
242                        tree* init_stmt_list)
243 {
244   if (var_gc.empty())
245     return;
246
247   size_t count = var_gc.size();
248
249   tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
250                                         "__next",
251                                         ptr_type_node,
252                                         "__size",
253                                         sizetype);
254
255   tree index_type = build_index_type(size_int(count));
256   tree array_type = build_array_type(root_type, index_type);
257
258   tree root_list_type = make_node(RECORD_TYPE);
259   root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
260                                         root_list_type, 2,
261                                         "__next",
262                                         build_pointer_type(root_list_type),
263                                         "__roots",
264                                         array_type);
265
266   // Build an initialier for the __roots array.
267
268   VEC(constructor_elt,gc)* roots_init = VEC_alloc(constructor_elt, gc,
269                                                   count + 1);
270
271   size_t i = 0;
272   for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
273        p != var_gc.end();
274        ++p, ++i)
275     {
276       VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
277
278       constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
279       tree field = TYPE_FIELDS(root_type);
280       elt->index = field;
281       Bvariable* bvar = (*p)->get_backend_variable(this, NULL);
282       tree decl = var_to_tree(bvar);
283       go_assert(TREE_CODE(decl) == VAR_DECL);
284       elt->value = build_fold_addr_expr(decl);
285
286       elt = VEC_quick_push(constructor_elt, init, NULL);
287       field = DECL_CHAIN(field);
288       elt->index = field;
289       elt->value = DECL_SIZE_UNIT(decl);
290
291       elt = VEC_quick_push(constructor_elt, roots_init, NULL);
292       elt->index = size_int(i);
293       elt->value = build_constructor(root_type, init);
294     }
295
296   // The list ends with a NULL entry.
297
298   VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
299
300   constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
301   tree field = TYPE_FIELDS(root_type);
302   elt->index = field;
303   elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
304
305   elt = VEC_quick_push(constructor_elt, init, NULL);
306   field = DECL_CHAIN(field);
307   elt->index = field;
308   elt->value = size_zero_node;
309
310   elt = VEC_quick_push(constructor_elt, roots_init, NULL);
311   elt->index = size_int(i);
312   elt->value = build_constructor(root_type, init);
313
314   // Build a constructor for the struct.
315
316   VEC(constructor_elt,gc*) root_list_init = VEC_alloc(constructor_elt, gc, 2);
317
318   elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
319   field = TYPE_FIELDS(root_list_type);
320   elt->index = field;
321   elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
322
323   elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
324   field = DECL_CHAIN(field);
325   elt->index = field;
326   elt->value = build_constructor(array_type, roots_init);
327
328   // Build a decl to register.
329
330   tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
331                          create_tmp_var_name("gc"), root_list_type);
332   DECL_EXTERNAL(decl) = 0;
333   TREE_PUBLIC(decl) = 0;
334   TREE_STATIC(decl) = 1;
335   DECL_ARTIFICIAL(decl) = 1;
336   DECL_INITIAL(decl) = build_constructor(root_list_type, root_list_init);
337   rest_of_decl_compilation(decl, 1, 0);
338
339   static tree register_gc_fndecl;
340   tree call = Gogo::call_builtin(&register_gc_fndecl, BUILTINS_LOCATION,
341                                  "__go_register_gc_roots",
342                                  1,
343                                  void_type_node,
344                                  build_pointer_type(root_list_type),
345                                  build_fold_addr_expr(decl));
346   if (call != error_mark_node)
347     append_to_statement_list(call, init_stmt_list);
348 }
349
350 // Build the decl for the initialization function.
351
352 tree
353 Gogo::initialization_function_decl()
354 {
355   // The tedious details of building your own function.  There doesn't
356   // seem to be a helper function for this.
357   std::string name = this->package_name() + ".init";
358   tree fndecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL,
359                            get_identifier_from_string(name),
360                            build_function_type(void_type_node,
361                                                void_list_node));
362   const std::string& asm_name(this->get_init_fn_name());
363   SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
364
365   tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
366                             void_type_node);
367   DECL_ARTIFICIAL(resdecl) = 1;
368   DECL_CONTEXT(resdecl) = fndecl;
369   DECL_RESULT(fndecl) = resdecl;
370
371   TREE_STATIC(fndecl) = 1;
372   TREE_USED(fndecl) = 1;
373   DECL_ARTIFICIAL(fndecl) = 1;
374   TREE_PUBLIC(fndecl) = 1;
375
376   DECL_INITIAL(fndecl) = make_node(BLOCK);
377   TREE_USED(DECL_INITIAL(fndecl)) = 1;
378
379   return fndecl;
380 }
381
382 // Create the magic initialization function.  INIT_STMT_LIST is the
383 // code that it needs to run.
384
385 void
386 Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
387 {
388   // Make sure that we thought we needed an initialization function,
389   // as otherwise we will not have reported it in the export data.
390   go_assert(this->is_main_package() || this->need_init_fn_);
391
392   if (fndecl == NULL_TREE)
393     fndecl = this->initialization_function_decl();
394
395   DECL_SAVED_TREE(fndecl) = init_stmt_list;
396
397   current_function_decl = fndecl;
398   if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
399     push_struct_function(fndecl);
400   else
401     push_cfun(DECL_STRUCT_FUNCTION(fndecl));
402   cfun->function_end_locus = BUILTINS_LOCATION;
403
404   gimplify_function_tree(fndecl);
405
406   cgraph_add_new_function(fndecl, false);
407   cgraph_mark_needed_node(cgraph_get_node(fndecl));
408
409   current_function_decl = NULL_TREE;
410   pop_cfun();
411 }
412
413 // Search for references to VAR in any statements or called functions.
414
415 class Find_var : public Traverse
416 {
417  public:
418   // A hash table we use to avoid looping.  The index is the name of a
419   // named object.  We only look through objects defined in this
420   // package.
421   typedef Unordered_set(std::string) Seen_objects;
422
423   Find_var(Named_object* var, Seen_objects* seen_objects)
424     : Traverse(traverse_expressions),
425       var_(var), seen_objects_(seen_objects), found_(false)
426   { }
427
428   // Whether the variable was found.
429   bool
430   found() const
431   { return this->found_; }
432
433   int
434   expression(Expression**);
435
436  private:
437   // The variable we are looking for.
438   Named_object* var_;
439   // Names of objects we have already seen.
440   Seen_objects* seen_objects_;
441   // True if the variable was found.
442   bool found_;
443 };
444
445 // See if EXPR refers to VAR, looking through function calls and
446 // variable initializations.
447
448 int
449 Find_var::expression(Expression** pexpr)
450 {
451   Expression* e = *pexpr;
452
453   Var_expression* ve = e->var_expression();
454   if (ve != NULL)
455     {
456       Named_object* v = ve->named_object();
457       if (v == this->var_)
458         {
459           this->found_ = true;
460           return TRAVERSE_EXIT;
461         }
462
463       if (v->is_variable() && v->package() == NULL)
464         {
465           Expression* init = v->var_value()->init();
466           if (init != NULL)
467             {
468               std::pair<Seen_objects::iterator, bool> ins =
469                 this->seen_objects_->insert(v->name());
470               if (ins.second)
471                 {
472                   // This is the first time we have seen this name.
473                   if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
474                     return TRAVERSE_EXIT;
475                 }
476             }
477         }
478     }
479
480   // We traverse the code of any function we see.  Note that this
481   // means that we will traverse the code of a function whose address
482   // is taken even if it is not called.
483   Func_expression* fe = e->func_expression();
484   if (fe != NULL)
485     {
486       const Named_object* f = fe->named_object();
487       if (f->is_function() && f->package() == NULL)
488         {
489           std::pair<Seen_objects::iterator, bool> ins =
490             this->seen_objects_->insert(f->name());
491           if (ins.second)
492             {
493               // This is the first time we have seen this name.
494               if (f->func_value()->block()->traverse(this) == TRAVERSE_EXIT)
495                 return TRAVERSE_EXIT;
496             }
497         }
498     }
499
500   return TRAVERSE_CONTINUE;
501 }
502
503 // Return true if EXPR refers to VAR.
504
505 static bool
506 expression_requires(Expression* expr, Block* preinit, Named_object* var)
507 {
508   Find_var::Seen_objects seen_objects;
509   Find_var find_var(var, &seen_objects);
510   if (expr != NULL)
511     Expression::traverse(&expr, &find_var);
512   if (preinit != NULL)
513     preinit->traverse(&find_var);
514   
515   return find_var.found();
516 }
517
518 // Sort variable initializations.  If the initialization expression
519 // for variable A refers directly or indirectly to the initialization
520 // expression for variable B, then we must initialize B before A.
521
522 class Var_init
523 {
524  public:
525   Var_init()
526     : var_(NULL), init_(NULL_TREE), waiting_(0)
527   { }
528
529   Var_init(Named_object* var, tree init)
530     : var_(var), init_(init), waiting_(0)
531   { }
532
533   // Return the variable.
534   Named_object*
535   var() const
536   { return this->var_; }
537
538   // Return the initialization expression.
539   tree
540   init() const
541   { return this->init_; }
542
543   // Return the number of variables waiting for this one to be
544   // initialized.
545   size_t
546   waiting() const
547   { return this->waiting_; }
548
549   // Increment the number waiting.
550   void
551   increment_waiting()
552   { ++this->waiting_; }
553
554  private:
555   // The variable being initialized.
556   Named_object* var_;
557   // The initialization expression to run.
558   tree init_;
559   // The number of variables which are waiting for this one.
560   size_t waiting_;
561 };
562
563 typedef std::list<Var_init> Var_inits;
564
565 // Sort the variable initializations.  The rule we follow is that we
566 // emit them in the order they appear in the array, except that if the
567 // initialization expression for a variable V1 depends upon another
568 // variable V2 then we initialize V1 after V2.
569
570 static void
571 sort_var_inits(Var_inits* var_inits)
572 {
573   Var_inits ready;
574   while (!var_inits->empty())
575     {
576       Var_inits::iterator p1 = var_inits->begin();
577       Named_object* var = p1->var();
578       Expression* init = var->var_value()->init();
579       Block* preinit = var->var_value()->preinit();
580
581       // Start walking through the list to see which variables VAR
582       // needs to wait for.  We can skip P1->WAITING variables--that
583       // is the number we've already checked.
584       Var_inits::iterator p2 = p1;
585       ++p2;
586       for (size_t i = p1->waiting(); i > 0; --i)
587         ++p2;
588
589       for (; p2 != var_inits->end(); ++p2)
590         {
591           if (expression_requires(init, preinit, p2->var()))
592             {
593               // Check for cycles.
594               if (expression_requires(p2->var()->var_value()->init(),
595                                       p2->var()->var_value()->preinit(),
596                                       var))
597                 {
598                   error_at(var->location(),
599                            ("initialization expressions for %qs and "
600                             "%qs depend upon each other"),
601                            var->message_name().c_str(),
602                            p2->var()->message_name().c_str());
603                   inform(p2->var()->location(), "%qs defined here",
604                          p2->var()->message_name().c_str());
605                   p2 = var_inits->end();
606                 }
607               else
608                 {
609                   // We can't emit P1 until P2 is emitted.  Move P1.
610                   // Note that the WAITING loop always executes at
611                   // least once, which is what we want.
612                   p2->increment_waiting();
613                   Var_inits::iterator p3 = p2;
614                   for (size_t i = p2->waiting(); i > 0; --i)
615                     ++p3;
616                   var_inits->splice(p3, *var_inits, p1);
617                 }
618               break;
619             }
620         }
621
622       if (p2 == var_inits->end())
623         {
624           // VAR does not depends upon any other initialization expressions.
625
626           // Check for a loop of VAR on itself.  We only do this if
627           // INIT is not NULL; when INIT is NULL, it means that
628           // PREINIT sets VAR, which we will interpret as a loop.
629           if (init != NULL && expression_requires(init, preinit, var))
630             error_at(var->location(),
631                      "initialization expression for %qs depends upon itself",
632                      var->message_name().c_str());
633           ready.splice(ready.end(), *var_inits, p1);
634         }
635     }
636
637   // Now READY is the list in the desired initialization order.
638   var_inits->swap(ready);
639 }
640
641 // Write out the global definitions.
642
643 void
644 Gogo::write_globals()
645 {
646   this->convert_named_types();
647   this->build_interface_method_tables();
648
649   Bindings* bindings = this->current_bindings();
650   size_t count = bindings->size_definitions();
651
652   tree* vec = new tree[count];
653
654   tree init_fndecl = NULL_TREE;
655   tree init_stmt_list = NULL_TREE;
656
657   if (this->is_main_package())
658     this->init_imports(&init_stmt_list);
659
660   // A list of variable initializations.
661   Var_inits var_inits;
662
663   // A list of variables which need to be registered with the garbage
664   // collector.
665   std::vector<Named_object*> var_gc;
666   var_gc.reserve(count);
667
668   tree var_init_stmt_list = NULL_TREE;
669   size_t i = 0;
670   for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
671        p != bindings->end_definitions();
672        ++p, ++i)
673     {
674       Named_object* no = *p;
675
676       go_assert(!no->is_type_declaration() && !no->is_function_declaration());
677       // There is nothing to do for a package.
678       if (no->is_package())
679         {
680           --i;
681           --count;
682           continue;
683         }
684
685       // There is nothing to do for an object which was imported from
686       // a different package into the global scope.
687       if (no->package() != NULL)
688         {
689           --i;
690           --count;
691           continue;
692         }
693
694       // There is nothing useful we can output for constants which
695       // have ideal or non-integeral type.
696       if (no->is_const())
697         {
698           Type* type = no->const_value()->type();
699           if (type == NULL)
700             type = no->const_value()->expr()->type();
701           if (type->is_abstract() || type->integer_type() == NULL)
702             {
703               --i;
704               --count;
705               continue;
706             }
707         }
708
709       if (!no->is_variable())
710         {
711           vec[i] = no->get_tree(this, NULL);
712           if (vec[i] == error_mark_node)
713             {
714               go_assert(saw_errors());
715               --i;
716               --count;
717               continue;
718             }
719         }
720       else
721         {
722           Bvariable* var = no->get_backend_variable(this, NULL);
723           vec[i] = var_to_tree(var);
724           if (vec[i] == error_mark_node)
725             {
726               go_assert(saw_errors());
727               --i;
728               --count;
729               continue;
730             }
731
732           // Check for a sink variable, which may be used to run an
733           // initializer purely for its side effects.
734           bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
735
736           tree var_init_tree = NULL_TREE;
737           if (!no->var_value()->has_pre_init())
738             {
739               tree init = no->var_value()->get_init_tree(this, NULL);
740               if (init == error_mark_node)
741                 go_assert(saw_errors());
742               else if (init == NULL_TREE)
743                 ;
744               else if (TREE_CONSTANT(init))
745                 this->backend()->global_variable_set_init(var,
746                                                           tree_to_expr(init));
747               else if (is_sink)
748                 var_init_tree = init;
749               else
750                 var_init_tree = fold_build2_loc(no->location(), MODIFY_EXPR,
751                                                 void_type_node, vec[i], init);
752             }
753           else
754             {
755               // We are going to create temporary variables which
756               // means that we need an fndecl.
757               if (init_fndecl == NULL_TREE)
758                 init_fndecl = this->initialization_function_decl();
759               current_function_decl = init_fndecl;
760               if (DECL_STRUCT_FUNCTION(init_fndecl) == NULL)
761                 push_struct_function(init_fndecl);
762               else
763                 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
764
765               tree var_decl = is_sink ? NULL_TREE : vec[i];
766               var_init_tree = no->var_value()->get_init_block(this, NULL,
767                                                               var_decl);
768
769               current_function_decl = NULL_TREE;
770               pop_cfun();
771             }
772
773           if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
774             {
775               if (no->var_value()->init() == NULL
776                   && !no->var_value()->has_pre_init())
777                 append_to_statement_list(var_init_tree, &var_init_stmt_list);
778               else
779                 var_inits.push_back(Var_init(no, var_init_tree));
780             }
781
782           if (!is_sink && no->var_value()->type()->has_pointer())
783             var_gc.push_back(no);
784         }
785     }
786
787   // Register global variables with the garbage collector.
788   this->register_gc_vars(var_gc, &init_stmt_list);
789
790   // Simple variable initializations, after all variables are
791   // registered.
792   append_to_statement_list(var_init_stmt_list, &init_stmt_list);
793
794   // Complex variable initializations, first sorting them into a
795   // workable order.
796   if (!var_inits.empty())
797     {
798       sort_var_inits(&var_inits);
799       for (Var_inits::const_iterator p = var_inits.begin();
800            p != var_inits.end();
801            ++p)
802         append_to_statement_list(p->init(), &init_stmt_list);
803     }
804
805   // After all the variables are initialized, call the "init"
806   // functions if there are any.
807   for (std::vector<Named_object*>::const_iterator p =
808          this->init_functions_.begin();
809        p != this->init_functions_.end();
810        ++p)
811     {
812       tree decl = (*p)->get_tree(this, NULL);
813       tree call = build_call_expr(decl, 0);
814       append_to_statement_list(call, &init_stmt_list);
815     }
816
817   // Set up a magic function to do all the initialization actions.
818   // This will be called if this package is imported.
819   if (init_stmt_list != NULL_TREE
820       || this->need_init_fn_
821       || this->is_main_package())
822     this->write_initialization_function(init_fndecl, init_stmt_list);
823
824   // Pass everything back to the middle-end.
825
826   wrapup_global_declarations(vec, count);
827
828   cgraph_finalize_compilation_unit();
829
830   check_global_declarations(vec, count);
831   emit_debug_global_declarations(vec, count);
832
833   delete[] vec;
834 }
835
836 // Get a tree for the identifier for a named object.
837
838 tree
839 Named_object::get_id(Gogo* gogo)
840 {
841   go_assert(!this->is_variable() && !this->is_result_variable());
842   std::string decl_name;
843   if (this->is_function_declaration()
844       && !this->func_declaration_value()->asm_name().empty())
845     decl_name = this->func_declaration_value()->asm_name();
846   else if (this->is_type()
847            && this->type_value()->location() == BUILTINS_LOCATION)
848     {
849       // We don't need the package name for builtin types.
850       decl_name = Gogo::unpack_hidden_name(this->name_);
851     }
852   else
853     {
854       std::string package_name;
855       if (this->package_ == NULL)
856         package_name = gogo->package_name();
857       else
858         package_name = this->package_->name();
859
860       decl_name = package_name + '.' + Gogo::unpack_hidden_name(this->name_);
861
862       Function_type* fntype;
863       if (this->is_function())
864         fntype = this->func_value()->type();
865       else if (this->is_function_declaration())
866         fntype = this->func_declaration_value()->type();
867       else
868         fntype = NULL;
869       if (fntype != NULL && fntype->is_method())
870         {
871           decl_name.push_back('.');
872           decl_name.append(fntype->receiver()->type()->mangled_name(gogo));
873         }
874     }
875   if (this->is_type())
876     {
877       const Named_object* in_function = this->type_value()->in_function();
878       if (in_function != NULL)
879         decl_name += '$' + in_function->name();
880     }
881   return get_identifier_from_string(decl_name);
882 }
883
884 // Get a tree for a named object.
885
886 tree
887 Named_object::get_tree(Gogo* gogo, Named_object* function)
888 {
889   if (this->tree_ != NULL_TREE)
890     return this->tree_;
891
892   tree name;
893   if (this->classification_ == NAMED_OBJECT_TYPE)
894     name = NULL_TREE;
895   else
896     name = this->get_id(gogo);
897   tree decl;
898   switch (this->classification_)
899     {
900     case NAMED_OBJECT_CONST:
901       {
902         Named_constant* named_constant = this->u_.const_value;
903         Translate_context subcontext(gogo, function, NULL, NULL);
904         tree expr_tree = named_constant->expr()->get_tree(&subcontext);
905         if (expr_tree == error_mark_node)
906           decl = error_mark_node;
907         else
908           {
909             Type* type = named_constant->type();
910             if (type != NULL && !type->is_abstract())
911               {
912                 if (!type->is_error())
913                   expr_tree = fold_convert(type->get_tree(gogo), expr_tree);
914                 else
915                   expr_tree = error_mark_node;
916               }
917             if (expr_tree == error_mark_node)
918               decl = error_mark_node;
919             else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
920               {
921                 decl = build_decl(named_constant->location(), CONST_DECL,
922                                   name, TREE_TYPE(expr_tree));
923                 DECL_INITIAL(decl) = expr_tree;
924                 TREE_CONSTANT(decl) = 1;
925                 TREE_READONLY(decl) = 1;
926               }
927             else
928               {
929                 // A CONST_DECL is only for an enum constant, so we
930                 // shouldn't use for non-integral types.  Instead we
931                 // just return the constant itself, rather than a
932                 // decl.
933                 decl = expr_tree;
934               }
935           }
936       }
937       break;
938
939     case NAMED_OBJECT_TYPE:
940       {
941         Named_type* named_type = this->u_.type_value;
942         tree type_tree = named_type->get_tree(gogo);
943         if (type_tree == error_mark_node)
944           decl = error_mark_node;
945         else
946           {
947             decl = TYPE_NAME(type_tree);
948             go_assert(decl != NULL_TREE);
949
950             // We need to produce a type descriptor for every named
951             // type, and for a pointer to every named type, since
952             // other files or packages might refer to them.  We need
953             // to do this even for hidden types, because they might
954             // still be returned by some function.  Simply calling the
955             // type_descriptor method is enough to create the type
956             // descriptor, even though we don't do anything with it.
957             if (this->package_ == NULL)
958               {
959                 named_type->type_descriptor_pointer(gogo);
960                 Type* pn = Type::make_pointer_type(named_type);
961                 pn->type_descriptor_pointer(gogo);
962               }
963           }
964       }
965       break;
966
967     case NAMED_OBJECT_TYPE_DECLARATION:
968       error("reference to undefined type %qs",
969             this->message_name().c_str());
970       return error_mark_node;
971
972     case NAMED_OBJECT_VAR:
973     case NAMED_OBJECT_RESULT_VAR:
974     case NAMED_OBJECT_SINK:
975       gcc_unreachable();
976
977     case NAMED_OBJECT_FUNC:
978       {
979         Function* func = this->u_.func_value;
980         decl = func->get_or_make_decl(gogo, this, name);
981         if (decl != error_mark_node)
982           {
983             if (func->block() != NULL)
984               {
985                 if (DECL_STRUCT_FUNCTION(decl) == NULL)
986                   push_struct_function(decl);
987                 else
988                   push_cfun(DECL_STRUCT_FUNCTION(decl));
989
990                 cfun->function_end_locus = func->block()->end_location();
991
992                 current_function_decl = decl;
993
994                 func->build_tree(gogo, this);
995
996                 gimplify_function_tree(decl);
997
998                 cgraph_finalize_function(decl, true);
999
1000                 current_function_decl = NULL_TREE;
1001                 pop_cfun();
1002               }
1003           }
1004       }
1005       break;
1006
1007     default:
1008       gcc_unreachable();
1009     }
1010
1011   if (TREE_TYPE(decl) == error_mark_node)
1012     decl = error_mark_node;
1013
1014   tree ret = decl;
1015
1016   this->tree_ = ret;
1017
1018   if (ret != error_mark_node)
1019     go_preserve_from_gc(ret);
1020
1021   return ret;
1022 }
1023
1024 // Get the initial value of a variable as a tree.  This does not
1025 // consider whether the variable is in the heap--it returns the
1026 // initial value as though it were always stored in the stack.
1027
1028 tree
1029 Variable::get_init_tree(Gogo* gogo, Named_object* function)
1030 {
1031   go_assert(this->preinit_ == NULL);
1032   if (this->init_ == NULL)
1033     {
1034       go_assert(!this->is_parameter_);
1035       return this->type_->get_init_tree(gogo,
1036                                         (this->is_global_
1037                                          || this->is_in_heap()));
1038     }
1039   else
1040     {
1041       Translate_context context(gogo, function, NULL, NULL);
1042       tree rhs_tree = this->init_->get_tree(&context);
1043       return Expression::convert_for_assignment(&context, this->type(),
1044                                                 this->init_->type(),
1045                                                 rhs_tree, this->location());
1046     }
1047 }
1048
1049 // Get the initial value of a variable when a block is required.
1050 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1051
1052 tree
1053 Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
1054 {
1055   go_assert(this->preinit_ != NULL);
1056
1057   // We want to add the variable assignment to the end of the preinit
1058   // block.  The preinit block may have a TRY_FINALLY_EXPR and a
1059   // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1060   // regular statements.
1061
1062   Translate_context context(gogo, function, NULL, NULL);
1063   Bblock* bblock = this->preinit_->get_backend(&context);
1064   tree block_tree = block_to_tree(bblock);
1065   if (block_tree == error_mark_node)
1066     return error_mark_node;
1067   go_assert(TREE_CODE(block_tree) == BIND_EXPR);
1068   tree statements = BIND_EXPR_BODY(block_tree);
1069   while (statements != NULL_TREE
1070          && (TREE_CODE(statements) == TRY_FINALLY_EXPR
1071              || TREE_CODE(statements) == TRY_CATCH_EXPR))
1072     statements = TREE_OPERAND(statements, 0);
1073
1074   // It's possible to have pre-init statements without an initializer
1075   // if the pre-init statements set the variable.
1076   if (this->init_ != NULL)
1077     {
1078       tree rhs_tree = this->init_->get_tree(&context);
1079       if (rhs_tree == error_mark_node)
1080         return error_mark_node;
1081       if (var_decl == NULL_TREE)
1082         append_to_statement_list(rhs_tree, &statements);
1083       else
1084         {
1085           tree val = Expression::convert_for_assignment(&context, this->type(),
1086                                                         this->init_->type(),
1087                                                         rhs_tree,
1088                                                         this->location());
1089           if (val == error_mark_node)
1090             return error_mark_node;
1091           tree set = fold_build2_loc(this->location(), MODIFY_EXPR,
1092                                      void_type_node, var_decl, val);
1093           append_to_statement_list(set, &statements);
1094         }
1095     }
1096
1097   return block_tree;
1098 }
1099
1100 // Get a tree for a function decl.
1101
1102 tree
1103 Function::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1104 {
1105   if (this->fndecl_ == NULL_TREE)
1106     {
1107       tree functype = this->type_->get_tree(gogo);
1108       if (functype == error_mark_node)
1109         this->fndecl_ = error_mark_node;
1110       else
1111         {
1112           // The type of a function comes back as a pointer, but we
1113           // want the real function type for a function declaration.
1114           go_assert(POINTER_TYPE_P(functype));
1115           functype = TREE_TYPE(functype);
1116           tree decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1117
1118           this->fndecl_ = decl;
1119
1120           if (no->package() != NULL)
1121             ;
1122           else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
1123             ;
1124           else if (Gogo::unpack_hidden_name(no->name()) == "init"
1125                    && !this->type_->is_method())
1126             ;
1127           else if (Gogo::unpack_hidden_name(no->name()) == "main"
1128                    && gogo->is_main_package())
1129             TREE_PUBLIC(decl) = 1;
1130           // Methods have to be public even if they are hidden because
1131           // they can be pulled into type descriptors when using
1132           // anonymous fields.
1133           else if (!Gogo::is_hidden_name(no->name())
1134                    || this->type_->is_method())
1135             {
1136               TREE_PUBLIC(decl) = 1;
1137               std::string asm_name = gogo->unique_prefix();
1138               asm_name.append(1, '.');
1139               asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1140               SET_DECL_ASSEMBLER_NAME(decl,
1141                                       get_identifier_from_string(asm_name));
1142             }
1143
1144           // Why do we have to do this in the frontend?
1145           tree restype = TREE_TYPE(functype);
1146           tree resdecl = build_decl(this->location(), RESULT_DECL, NULL_TREE,
1147                                     restype);
1148           DECL_ARTIFICIAL(resdecl) = 1;
1149           DECL_IGNORED_P(resdecl) = 1;
1150           DECL_CONTEXT(resdecl) = decl;
1151           DECL_RESULT(decl) = resdecl;
1152
1153           if (this->enclosing_ != NULL)
1154             DECL_STATIC_CHAIN(decl) = 1;
1155
1156           // If a function calls the predeclared recover function, we
1157           // can't inline it, because recover behaves differently in a
1158           // function passed directly to defer.
1159           if (this->calls_recover_ && !this->is_recover_thunk_)
1160             DECL_UNINLINABLE(decl) = 1;
1161
1162           // If this is a thunk created to call a function which calls
1163           // the predeclared recover function, we need to disable
1164           // stack splitting for the thunk.
1165           if (this->is_recover_thunk_)
1166             {
1167               tree attr = get_identifier("__no_split_stack__");
1168               DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1169             }
1170
1171           go_preserve_from_gc(decl);
1172
1173           if (this->closure_var_ != NULL)
1174             {
1175               push_struct_function(decl);
1176
1177               Bvariable* bvar = this->closure_var_->get_backend_variable(gogo,
1178                                                                          no);
1179               tree closure_decl = var_to_tree(bvar);
1180               if (closure_decl == error_mark_node)
1181                 this->fndecl_ = error_mark_node;
1182               else
1183                 {
1184                   DECL_ARTIFICIAL(closure_decl) = 1;
1185                   DECL_IGNORED_P(closure_decl) = 1;
1186                   TREE_USED(closure_decl) = 1;
1187                   DECL_ARG_TYPE(closure_decl) = TREE_TYPE(closure_decl);
1188                   TREE_READONLY(closure_decl) = 1;
1189
1190                   DECL_STRUCT_FUNCTION(decl)->static_chain_decl = closure_decl;
1191                 }
1192
1193               pop_cfun();
1194             }
1195         }
1196     }
1197   return this->fndecl_;
1198 }
1199
1200 // Get a tree for a function declaration.
1201
1202 tree
1203 Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1204 {
1205   if (this->fndecl_ == NULL_TREE)
1206     {
1207       // Let Go code use an asm declaration to pick up a builtin
1208       // function.
1209       if (!this->asm_name_.empty())
1210         {
1211           std::map<std::string, tree>::const_iterator p =
1212             builtin_functions.find(this->asm_name_);
1213           if (p != builtin_functions.end())
1214             {
1215               this->fndecl_ = p->second;
1216               return this->fndecl_;
1217             }
1218         }
1219
1220       tree functype = this->fntype_->get_tree(gogo);
1221       tree decl;
1222       if (functype == error_mark_node)
1223         decl = error_mark_node;
1224       else
1225         {
1226           // The type of a function comes back as a pointer, but we
1227           // want the real function type for a function declaration.
1228           go_assert(POINTER_TYPE_P(functype));
1229           functype = TREE_TYPE(functype);
1230           decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1231           TREE_PUBLIC(decl) = 1;
1232           DECL_EXTERNAL(decl) = 1;
1233
1234           if (this->asm_name_.empty())
1235             {
1236               std::string asm_name = (no->package() == NULL
1237                                       ? gogo->unique_prefix()
1238                                       : no->package()->unique_prefix());
1239               asm_name.append(1, '.');
1240               asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1241               SET_DECL_ASSEMBLER_NAME(decl,
1242                                       get_identifier_from_string(asm_name));
1243             }
1244         }
1245       this->fndecl_ = decl;
1246       go_preserve_from_gc(decl);
1247     }
1248   return this->fndecl_;
1249 }
1250
1251 // We always pass the receiver to a method as a pointer.  If the
1252 // receiver is actually declared as a non-pointer type, then we copy
1253 // the value into a local variable, so that it has the right type.  In
1254 // this function we create the real PARM_DECL to use, and set
1255 // DEC_INITIAL of the var_decl to be the value passed in.
1256
1257 tree
1258 Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
1259 {
1260   if (var_decl == error_mark_node)
1261     return error_mark_node;
1262   go_assert(TREE_CODE(var_decl) == VAR_DECL);
1263   tree val_type = TREE_TYPE(var_decl);
1264   bool is_in_heap = no->var_value()->is_in_heap();
1265   if (is_in_heap)
1266     {
1267       go_assert(POINTER_TYPE_P(val_type));
1268       val_type = TREE_TYPE(val_type);
1269     }
1270
1271   source_location loc = DECL_SOURCE_LOCATION(var_decl);
1272   std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1273   name += ".pointer";
1274   tree id = get_identifier_from_string(name);
1275   tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
1276   DECL_CONTEXT(parm_decl) = current_function_decl;
1277   DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
1278
1279   go_assert(DECL_INITIAL(var_decl) == NULL_TREE);
1280   // The receiver might be passed as a null pointer.
1281   tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node, parm_decl,
1282                                fold_convert_loc(loc, TREE_TYPE(parm_decl),
1283                                                 null_pointer_node));
1284   tree ind = build_fold_indirect_ref_loc(loc, parm_decl);
1285   TREE_THIS_NOTRAP(ind) = 1;
1286   tree zero_init = no->var_value()->type()->get_init_tree(gogo, false);
1287   tree init = fold_build3_loc(loc, COND_EXPR, TREE_TYPE(ind),
1288                               check, ind, zero_init);
1289
1290   if (is_in_heap)
1291     {
1292       tree size = TYPE_SIZE_UNIT(val_type);
1293       tree space = gogo->allocate_memory(no->var_value()->type(), size,
1294                                          no->location());
1295       space = save_expr(space);
1296       space = fold_convert(build_pointer_type(val_type), space);
1297       tree spaceref = build_fold_indirect_ref_loc(no->location(), space);
1298       TREE_THIS_NOTRAP(spaceref) = 1;
1299       tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node,
1300                                    parm_decl,
1301                                    fold_convert_loc(loc, TREE_TYPE(parm_decl),
1302                                                     null_pointer_node));
1303       tree parmref = build_fold_indirect_ref_loc(no->location(), parm_decl);
1304       TREE_THIS_NOTRAP(parmref) = 1;
1305       tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1306                                  spaceref, parmref);
1307       init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1308                              build3(COND_EXPR, void_type_node,
1309                                     check, set, NULL_TREE),
1310                              space);
1311     }
1312
1313   DECL_INITIAL(var_decl) = init;
1314
1315   return parm_decl;
1316 }
1317
1318 // If we take the address of a parameter, then we need to copy it into
1319 // the heap.  We will access it as a local variable via an
1320 // indirection.
1321
1322 tree
1323 Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree var_decl)
1324 {
1325   if (var_decl == error_mark_node)
1326     return error_mark_node;
1327   go_assert(TREE_CODE(var_decl) == VAR_DECL);
1328   source_location loc = DECL_SOURCE_LOCATION(var_decl);
1329
1330   std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1331   name += ".param";
1332   tree id = get_identifier_from_string(name);
1333
1334   tree type = TREE_TYPE(var_decl);
1335   go_assert(POINTER_TYPE_P(type));
1336   type = TREE_TYPE(type);
1337
1338   tree parm_decl = build_decl(loc, PARM_DECL, id, type);
1339   DECL_CONTEXT(parm_decl) = current_function_decl;
1340   DECL_ARG_TYPE(parm_decl) = type;
1341
1342   tree size = TYPE_SIZE_UNIT(type);
1343   tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
1344   space = save_expr(space);
1345   space = fold_convert(TREE_TYPE(var_decl), space);
1346   tree spaceref = build_fold_indirect_ref_loc(loc, space);
1347   TREE_THIS_NOTRAP(spaceref) = 1;
1348   tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
1349                      build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
1350                      space);
1351   DECL_INITIAL(var_decl) = init;
1352
1353   return parm_decl;
1354 }
1355
1356 // Get a tree for function code.
1357
1358 void
1359 Function::build_tree(Gogo* gogo, Named_object* named_function)
1360 {
1361   tree fndecl = this->fndecl_;
1362   go_assert(fndecl != NULL_TREE);
1363
1364   tree params = NULL_TREE;
1365   tree* pp = &params;
1366
1367   tree declare_vars = NULL_TREE;
1368   for (Bindings::const_definitions_iterator p =
1369          this->block_->bindings()->begin_definitions();
1370        p != this->block_->bindings()->end_definitions();
1371        ++p)
1372     {
1373       if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
1374         {
1375           Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1376           *pp = var_to_tree(bvar);
1377
1378           // We always pass the receiver to a method as a pointer.  If
1379           // the receiver is declared as a non-pointer type, then we
1380           // copy the value into a local variable.
1381           if ((*p)->var_value()->is_receiver()
1382               && (*p)->var_value()->type()->points_to() == NULL)
1383             {
1384               tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
1385               tree var = *pp;
1386               if (var != error_mark_node)
1387                 {
1388                   go_assert(TREE_CODE(var) == VAR_DECL);
1389                   DECL_CHAIN(var) = declare_vars;
1390                   declare_vars = var;
1391                 }
1392               *pp = parm_decl;
1393             }
1394           else if ((*p)->var_value()->is_in_heap())
1395             {
1396               // If we take the address of a parameter, then we need
1397               // to copy it into the heap.
1398               tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
1399               tree var = *pp;
1400               if (var != error_mark_node)
1401                 {
1402                   go_assert(TREE_CODE(var) == VAR_DECL);
1403                   DECL_CHAIN(var) = declare_vars;
1404                   declare_vars = var;
1405                 }
1406               *pp = parm_decl;
1407             }
1408
1409           if (*pp != error_mark_node)
1410             {
1411               go_assert(TREE_CODE(*pp) == PARM_DECL);
1412               pp = &DECL_CHAIN(*pp);
1413             }
1414         }
1415       else if ((*p)->is_result_variable())
1416         {
1417           Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
1418           tree var_decl = var_to_tree(bvar);
1419
1420           Type* type = (*p)->result_var_value()->type();
1421           tree init;
1422           if (!(*p)->result_var_value()->is_in_heap())
1423             init = type->get_init_tree(gogo, false);
1424           else
1425             {
1426               source_location loc = (*p)->location();
1427               tree type_tree = type->get_tree(gogo);
1428               tree space = gogo->allocate_memory(type,
1429                                                  TYPE_SIZE_UNIT(type_tree),
1430                                                  loc);
1431               tree ptr_type_tree = build_pointer_type(type_tree);
1432               tree subinit = type->get_init_tree(gogo, true);
1433               if (subinit == NULL_TREE)
1434                 init = fold_convert_loc(loc, ptr_type_tree, space);
1435               else
1436                 {
1437                   space = save_expr(space);
1438                   space = fold_convert_loc(loc, ptr_type_tree, space);
1439                   tree spaceref = build_fold_indirect_ref_loc(loc, space);
1440                   TREE_THIS_NOTRAP(spaceref) = 1;
1441                   tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1442                                              spaceref, subinit);
1443                   init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1444                                          set, space);
1445                 }
1446             }
1447
1448           if (var_decl != error_mark_node)
1449             {
1450               go_assert(TREE_CODE(var_decl) == VAR_DECL);
1451               DECL_INITIAL(var_decl) = init;
1452               DECL_CHAIN(var_decl) = declare_vars;
1453               declare_vars = var_decl;
1454             }
1455         }
1456     }
1457   *pp = NULL_TREE;
1458
1459   DECL_ARGUMENTS(fndecl) = params;
1460
1461   if (this->block_ != NULL)
1462     {
1463       go_assert(DECL_INITIAL(fndecl) == NULL_TREE);
1464
1465       // Declare variables if necessary.
1466       tree bind = NULL_TREE;
1467       tree defer_init = NULL_TREE;
1468       if (declare_vars != NULL_TREE || this->defer_stack_ != NULL)
1469         {
1470           tree block = make_node(BLOCK);
1471           BLOCK_SUPERCONTEXT(block) = fndecl;
1472           DECL_INITIAL(fndecl) = block;
1473           BLOCK_VARS(block) = declare_vars;
1474           TREE_USED(block) = 1;
1475
1476           bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
1477                         NULL_TREE, block);
1478           TREE_SIDE_EFFECTS(bind) = 1;
1479
1480           if (this->defer_stack_ != NULL)
1481             {
1482               Translate_context dcontext(gogo, named_function, this->block_,
1483                                          tree_to_block(bind));
1484               Bstatement* bdi = this->defer_stack_->get_backend(&dcontext);
1485               defer_init = stat_to_tree(bdi);
1486             }
1487         }
1488
1489       // Build the trees for all the statements in the function.
1490       Translate_context context(gogo, named_function, NULL, NULL);
1491       Bblock* bblock = this->block_->get_backend(&context);
1492       tree code = block_to_tree(bblock);
1493
1494       tree init = NULL_TREE;
1495       tree except = NULL_TREE;
1496       tree fini = NULL_TREE;
1497
1498       // Initialize variables if necessary.
1499       for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
1500         {
1501           tree dv = build1(DECL_EXPR, void_type_node, v);
1502           SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
1503           append_to_statement_list(dv, &init);
1504         }
1505
1506       // If we have a defer stack, initialize it at the start of a
1507       // function.
1508       if (defer_init != NULL_TREE && defer_init != error_mark_node)
1509         {
1510           SET_EXPR_LOCATION(defer_init, this->block_->start_location());
1511           append_to_statement_list(defer_init, &init);
1512
1513           // Clean up the defer stack when we leave the function.
1514           this->build_defer_wrapper(gogo, named_function, &except, &fini);
1515         }
1516
1517       if (code != NULL_TREE && code != error_mark_node)
1518         {
1519           if (init != NULL_TREE)
1520             code = build2(COMPOUND_EXPR, void_type_node, init, code);
1521           if (except != NULL_TREE)
1522             code = build2(TRY_CATCH_EXPR, void_type_node, code,
1523                           build2(CATCH_EXPR, void_type_node, NULL, except));
1524           if (fini != NULL_TREE)
1525             code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
1526         }
1527
1528       // Stick the code into the block we built for the receiver, if
1529       // we built on.
1530       if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
1531         {
1532           BIND_EXPR_BODY(bind) = code;
1533           code = bind;
1534         }
1535
1536       DECL_SAVED_TREE(fndecl) = code;
1537     }
1538 }
1539
1540 // Build the wrappers around function code needed if the function has
1541 // any defer statements.  This sets *EXCEPT to an exception handler
1542 // and *FINI to a finally handler.
1543
1544 void
1545 Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
1546                               tree *except, tree *fini)
1547 {
1548   source_location end_loc = this->block_->end_location();
1549
1550   // Add an exception handler.  This is used if a panic occurs.  Its
1551   // purpose is to stop the stack unwinding if a deferred function
1552   // calls recover.  There are more details in
1553   // libgo/runtime/go-unwind.c.
1554
1555   tree stmt_list = NULL_TREE;
1556
1557   Expression* call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1558                                         this->defer_stack(end_loc));
1559   Translate_context context(gogo, named_function, NULL, NULL);
1560   tree call_tree = call->get_tree(&context);
1561   if (call_tree != error_mark_node)
1562     append_to_statement_list(call_tree, &stmt_list);
1563
1564   tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
1565   tree set;
1566   if (retval == NULL_TREE)
1567     set = NULL_TREE;
1568   else
1569     set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1570                           DECL_RESULT(this->fndecl_), retval);
1571   tree ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1572   append_to_statement_list(ret_stmt, &stmt_list);
1573
1574   go_assert(*except == NULL_TREE);
1575   *except = stmt_list;
1576
1577   // Add some finally code to run the defer functions.  This is used
1578   // both in the normal case, when no panic occurs, and also if a
1579   // panic occurs to run any further defer functions.  Of course, it
1580   // is possible for a defer function to call panic which should be
1581   // caught by another defer function.  To handle that we use a loop.
1582   //  finish:
1583   //   try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1584   //   if (return values are named) return named_vals;
1585
1586   stmt_list = NULL;
1587
1588   tree label = create_artificial_label(end_loc);
1589   tree define_label = fold_build1_loc(end_loc, LABEL_EXPR, void_type_node,
1590                                       label);
1591   append_to_statement_list(define_label, &stmt_list);
1592
1593   call = Runtime::make_call(Runtime::UNDEFER, end_loc, 1,
1594                             this->defer_stack(end_loc));
1595   tree undefer = call->get_tree(&context);
1596
1597   call = Runtime::make_call(Runtime::CHECK_DEFER, end_loc, 1,
1598                             this->defer_stack(end_loc));
1599   tree defer = call->get_tree(&context);
1600
1601   if (undefer == error_mark_node || defer == error_mark_node)
1602     return;
1603
1604   tree jump = fold_build1_loc(end_loc, GOTO_EXPR, void_type_node, label);
1605   tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
1606   catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
1607   tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
1608
1609   append_to_statement_list(try_catch, &stmt_list);
1610
1611   if (this->type_->results() != NULL
1612       && !this->type_->results()->empty()
1613       && !this->type_->results()->front().name().empty())
1614     {
1615       // If the result variables are named, we need to return them
1616       // again, because they might have been changed by a defer
1617       // function.
1618       retval = this->return_value(gogo, named_function, end_loc,
1619                                   &stmt_list);
1620       set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1621                             DECL_RESULT(this->fndecl_), retval);
1622       ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1623       append_to_statement_list(ret_stmt, &stmt_list);
1624     }
1625   
1626   go_assert(*fini == NULL_TREE);
1627   *fini = stmt_list;
1628 }
1629
1630 // Return the value to assign to DECL_RESULT(this->fndecl_).  This may
1631 // also add statements to STMT_LIST, which need to be executed before
1632 // the assignment.  This is used for a return statement with no
1633 // explicit values.
1634
1635 tree
1636 Function::return_value(Gogo* gogo, Named_object* named_function,
1637                        source_location location, tree* stmt_list) const
1638 {
1639   const Typed_identifier_list* results = this->type_->results();
1640   if (results == NULL || results->empty())
1641     return NULL_TREE;
1642
1643   go_assert(this->results_ != NULL);
1644   if (this->results_->size() != results->size())
1645     {
1646       go_assert(saw_errors());
1647       return error_mark_node;
1648     }
1649
1650   tree retval;
1651   if (results->size() == 1)
1652     {
1653       Bvariable* bvar =
1654         this->results_->front()->get_backend_variable(gogo,
1655                                                       named_function);
1656       tree ret = var_to_tree(bvar);
1657       if (this->results_->front()->result_var_value()->is_in_heap())
1658         ret = build_fold_indirect_ref_loc(location, ret);
1659       return ret;
1660     }
1661   else
1662     {
1663       tree rettype = TREE_TYPE(DECL_RESULT(this->fndecl_));
1664       retval = create_tmp_var(rettype, "RESULT");
1665       tree field = TYPE_FIELDS(rettype);
1666       int index = 0;
1667       for (Typed_identifier_list::const_iterator pr = results->begin();
1668            pr != results->end();
1669            ++pr, ++index, field = DECL_CHAIN(field))
1670         {
1671           go_assert(field != NULL);
1672           Named_object* no = (*this->results_)[index];
1673           Bvariable* bvar = no->get_backend_variable(gogo, named_function);
1674           tree val = var_to_tree(bvar);
1675           if (no->result_var_value()->is_in_heap())
1676             val = build_fold_indirect_ref_loc(location, val);
1677           tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
1678                                      build3(COMPONENT_REF, TREE_TYPE(field),
1679                                             retval, field, NULL_TREE),
1680                                      val);
1681           append_to_statement_list(set, stmt_list);
1682         }
1683       return retval;
1684     }
1685 }
1686
1687 // Return the integer type to use for a size.
1688
1689 GO_EXTERN_C
1690 tree
1691 go_type_for_size(unsigned int bits, int unsignedp)
1692 {
1693   const char* name;
1694   switch (bits)
1695     {
1696     case 8:
1697       name = unsignedp ? "uint8" : "int8";
1698       break;
1699     case 16:
1700       name = unsignedp ? "uint16" : "int16";
1701       break;
1702     case 32:
1703       name = unsignedp ? "uint32" : "int32";
1704       break;
1705     case 64:
1706       name = unsignedp ? "uint64" : "int64";
1707       break;
1708     default:
1709       if (bits == POINTER_SIZE && unsignedp)
1710         name = "uintptr";
1711       else
1712         return NULL_TREE;
1713     }
1714   Type* type = Type::lookup_integer_type(name);
1715   return type->get_tree(go_get_gogo());
1716 }
1717
1718 // Return the type to use for a mode.
1719
1720 GO_EXTERN_C
1721 tree
1722 go_type_for_mode(enum machine_mode mode, int unsignedp)
1723 {
1724   // FIXME: This static_cast should be in machmode.h.
1725   enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
1726   if (mc == MODE_INT)
1727     return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
1728   else if (mc == MODE_FLOAT)
1729     {
1730       Type* type;
1731       switch (GET_MODE_BITSIZE (mode))
1732         {
1733         case 32:
1734           type = Type::lookup_float_type("float32");
1735           break;
1736         case 64:
1737           type = Type::lookup_float_type("float64");
1738           break;
1739         default:
1740           // We have to check for long double in order to support
1741           // i386 excess precision.
1742           if (mode == TYPE_MODE(long_double_type_node))
1743             return long_double_type_node;
1744           return NULL_TREE;
1745         }
1746       return type->float_type()->type_tree();
1747     }
1748   else if (mc == MODE_COMPLEX_FLOAT)
1749     {
1750       Type *type;
1751       switch (GET_MODE_BITSIZE (mode))
1752         {
1753         case 64:
1754           type = Type::lookup_complex_type("complex64");
1755           break;
1756         case 128:
1757           type = Type::lookup_complex_type("complex128");
1758           break;
1759         default:
1760           // We have to check for long double in order to support
1761           // i386 excess precision.
1762           if (mode == TYPE_MODE(complex_long_double_type_node))
1763             return complex_long_double_type_node;
1764           return NULL_TREE;
1765         }
1766       return type->complex_type()->type_tree();
1767     }
1768   else
1769     return NULL_TREE;
1770 }
1771
1772 // Return a tree which allocates SIZE bytes which will holds value of
1773 // type TYPE.
1774
1775 tree
1776 Gogo::allocate_memory(Type* type, tree size, source_location location)
1777 {
1778   // If the package imports unsafe, then it may play games with
1779   // pointers that look like integers.
1780   if (this->imported_unsafe_ || type->has_pointer())
1781     {
1782       static tree new_fndecl;
1783       return Gogo::call_builtin(&new_fndecl,
1784                                 location,
1785                                 "__go_new",
1786                                 1,
1787                                 ptr_type_node,
1788                                 sizetype,
1789                                 size);
1790     }
1791   else
1792     {
1793       static tree new_nopointers_fndecl;
1794       return Gogo::call_builtin(&new_nopointers_fndecl,
1795                                 location,
1796                                 "__go_new_nopointers",
1797                                 1,
1798                                 ptr_type_node,
1799                                 sizetype,
1800                                 size);
1801     }
1802 }
1803
1804 // Build a builtin struct with a list of fields.  The name is
1805 // STRUCT_NAME.  STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
1806 // node; this exists so that the struct can have fields which point to
1807 // itself.  If PTYPE is not NULL, store the result in *PTYPE.  There
1808 // are NFIELDS fields.  Each field is a name (a const char*) followed
1809 // by a type (a tree).
1810
1811 tree
1812 Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
1813                      int nfields, ...)
1814 {
1815   if (ptype != NULL && *ptype != NULL_TREE)
1816     return *ptype;
1817
1818   va_list ap;
1819   va_start(ap, nfields);
1820
1821   tree fields = NULL_TREE;
1822   for (int i = 0; i < nfields; ++i)
1823     {
1824       const char* field_name = va_arg(ap, const char*);
1825       tree type = va_arg(ap, tree);
1826       if (type == error_mark_node)
1827         {
1828           if (ptype != NULL)
1829             *ptype = error_mark_node;
1830           return error_mark_node;
1831         }
1832       tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
1833                               get_identifier(field_name), type);
1834       DECL_CHAIN(field) = fields;
1835       fields = field;
1836     }
1837
1838   va_end(ap);
1839
1840   if (struct_type == NULL_TREE)
1841     struct_type = make_node(RECORD_TYPE);
1842   finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
1843
1844   if (ptype != NULL)
1845     {
1846       go_preserve_from_gc(struct_type);
1847       *ptype = struct_type;
1848     }
1849
1850   return struct_type;
1851 }
1852
1853 // Return a type to use for pointer to const char for a string.
1854
1855 tree
1856 Gogo::const_char_pointer_type_tree()
1857 {
1858   static tree type;
1859   if (type == NULL_TREE)
1860     {
1861       tree const_char_type = build_qualified_type(unsigned_char_type_node,
1862                                                   TYPE_QUAL_CONST);
1863       type = build_pointer_type(const_char_type);
1864       go_preserve_from_gc(type);
1865     }
1866   return type;
1867 }
1868
1869 // Return a tree for a string constant.
1870
1871 tree
1872 Gogo::string_constant_tree(const std::string& val)
1873 {
1874   tree index_type = build_index_type(size_int(val.length()));
1875   tree const_char_type = build_qualified_type(unsigned_char_type_node,
1876                                               TYPE_QUAL_CONST);
1877   tree string_type = build_array_type(const_char_type, index_type);
1878   string_type = build_variant_type_copy(string_type);
1879   TYPE_STRING_FLAG(string_type) = 1;
1880   tree string_val = build_string(val.length(), val.data());
1881   TREE_TYPE(string_val) = string_type;
1882   return string_val;
1883 }
1884
1885 // Return a tree for a Go string constant.
1886
1887 tree
1888 Gogo::go_string_constant_tree(const std::string& val)
1889 {
1890   tree string_type = Type::make_string_type()->get_tree(this);
1891
1892   VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
1893
1894   constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
1895   tree field = TYPE_FIELDS(string_type);
1896   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
1897   elt->index = field;
1898   tree str = Gogo::string_constant_tree(val);
1899   elt->value = fold_convert(TREE_TYPE(field),
1900                             build_fold_addr_expr(str));
1901
1902   elt = VEC_quick_push(constructor_elt, init, NULL);
1903   field = DECL_CHAIN(field);
1904   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
1905   elt->index = field;
1906   elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
1907
1908   tree constructor = build_constructor(string_type, init);
1909   TREE_READONLY(constructor) = 1;
1910   TREE_CONSTANT(constructor) = 1;
1911
1912   return constructor;
1913 }
1914
1915 // Return a tree for a pointer to a Go string constant.  This is only
1916 // used for type descriptors, so we return a pointer to a constant
1917 // decl.
1918
1919 tree
1920 Gogo::ptr_go_string_constant_tree(const std::string& val)
1921 {
1922   tree pval = this->go_string_constant_tree(val);
1923
1924   tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
1925                          create_tmp_var_name("SP"), TREE_TYPE(pval));
1926   DECL_EXTERNAL(decl) = 0;
1927   TREE_PUBLIC(decl) = 0;
1928   TREE_USED(decl) = 1;
1929   TREE_READONLY(decl) = 1;
1930   TREE_CONSTANT(decl) = 1;
1931   TREE_STATIC(decl) = 1;
1932   DECL_ARTIFICIAL(decl) = 1;
1933   DECL_INITIAL(decl) = pval;
1934   rest_of_decl_compilation(decl, 1, 0);
1935
1936   return build_fold_addr_expr(decl);
1937 }
1938
1939 // Build the type of the struct that holds a slice for the given
1940 // element type.
1941
1942 tree
1943 Gogo::slice_type_tree(tree element_type_tree)
1944 {
1945   // We use int for the count and capacity fields in a slice header.
1946   // This matches 6g.  The language definition guarantees that we
1947   // can't allocate space of a size which does not fit in int
1948   // anyhow. FIXME: integer_type_node is the the C type "int" but is
1949   // not necessarily the Go type "int".  They will differ when the C
1950   // type "int" has fewer than 32 bits.
1951   return Gogo::builtin_struct(NULL, "__go_slice", NULL_TREE, 3,
1952                               "__values",
1953                               build_pointer_type(element_type_tree),
1954                               "__count",
1955                               integer_type_node,
1956                               "__capacity",
1957                               integer_type_node);
1958 }
1959
1960 // Given the tree for a slice type, return the tree for the type of
1961 // the elements of the slice.
1962
1963 tree
1964 Gogo::slice_element_type_tree(tree slice_type_tree)
1965 {
1966   go_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE
1967              && POINTER_TYPE_P(TREE_TYPE(TYPE_FIELDS(slice_type_tree))));
1968   return TREE_TYPE(TREE_TYPE(TYPE_FIELDS(slice_type_tree)));
1969 }
1970
1971 // Build a constructor for a slice.  SLICE_TYPE_TREE is the type of
1972 // the slice.  VALUES is the value pointer and COUNT is the number of
1973 // entries.  If CAPACITY is not NULL, it is the capacity; otherwise
1974 // the capacity and the count are the same.
1975
1976 tree
1977 Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
1978                         tree capacity)
1979 {
1980   go_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
1981
1982   VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
1983
1984   tree field = TYPE_FIELDS(slice_type_tree);
1985   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
1986   constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
1987   elt->index = field;
1988   go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
1989              == TYPE_MAIN_VARIANT(TREE_TYPE(values)));
1990   elt->value = values;
1991
1992   count = fold_convert(sizetype, count);
1993   if (capacity == NULL_TREE)
1994     {
1995       count = save_expr(count);
1996       capacity = count;
1997     }
1998
1999   field = DECL_CHAIN(field);
2000   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
2001   elt = VEC_quick_push(constructor_elt, init, NULL);
2002   elt->index = field;
2003   elt->value = fold_convert(TREE_TYPE(field), count);
2004
2005   field = DECL_CHAIN(field);
2006   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
2007   elt = VEC_quick_push(constructor_elt, init, NULL);
2008   elt->index = field;
2009   elt->value = fold_convert(TREE_TYPE(field), capacity);
2010
2011   return build_constructor(slice_type_tree, init);
2012 }
2013
2014 // Build a constructor for an empty slice.
2015
2016 tree
2017 Gogo::empty_slice_constructor(tree slice_type_tree)
2018 {
2019   tree element_field = TYPE_FIELDS(slice_type_tree);
2020   tree ret = Gogo::slice_constructor(slice_type_tree,
2021                                      fold_convert(TREE_TYPE(element_field),
2022                                                   null_pointer_node),
2023                                      size_zero_node,
2024                                      size_zero_node);
2025   TREE_CONSTANT(ret) = 1;
2026   return ret;
2027 }
2028
2029 // Build a map descriptor for a map of type MAPTYPE.
2030
2031 tree
2032 Gogo::map_descriptor(Map_type* maptype)
2033 {
2034   if (this->map_descriptors_ == NULL)
2035     this->map_descriptors_ = new Map_descriptors(10);
2036
2037   std::pair<const Map_type*, tree> val(maptype, NULL);
2038   std::pair<Map_descriptors::iterator, bool> ins =
2039     this->map_descriptors_->insert(val);
2040   Map_descriptors::iterator p = ins.first;
2041   if (!ins.second)
2042     {
2043       if (p->second == error_mark_node)
2044         return error_mark_node;
2045       go_assert(p->second != NULL_TREE && DECL_P(p->second));
2046       return build_fold_addr_expr(p->second);
2047     }
2048
2049   Type* keytype = maptype->key_type();
2050   Type* valtype = maptype->val_type();
2051
2052   std::string mangled_name = ("__go_map_" + maptype->mangled_name(this));
2053
2054   tree id = get_identifier_from_string(mangled_name);
2055
2056   // Get the type of the map descriptor.  This is __go_map_descriptor
2057   // in libgo/map.h.
2058
2059   tree struct_type = this->map_descriptor_type();
2060
2061   // The map entry type is a struct with three fields.  This struct is
2062   // specific to MAPTYPE.  Build it.
2063
2064   tree map_entry_type = make_node(RECORD_TYPE);
2065
2066   map_entry_type = Gogo::builtin_struct(NULL, "__map", map_entry_type, 3,
2067                                         "__next",
2068                                         build_pointer_type(map_entry_type),
2069                                         "__key",
2070                                         keytype->get_tree(this),
2071                                         "__val",
2072                                         valtype->get_tree(this));
2073   if (map_entry_type == error_mark_node)
2074     {
2075       p->second = error_mark_node;
2076       return error_mark_node;
2077     }
2078
2079   tree map_entry_key_field = DECL_CHAIN(TYPE_FIELDS(map_entry_type));
2080   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field)),
2081                     "__key") == 0);
2082
2083   tree map_entry_val_field = DECL_CHAIN(map_entry_key_field);
2084   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field)),
2085                     "__val") == 0);
2086
2087   // Initialize the entries.
2088
2089   tree map_descriptor_field = TYPE_FIELDS(struct_type);
2090   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field)),
2091                     "__map_descriptor") == 0);
2092   tree entry_size_field = DECL_CHAIN(map_descriptor_field);
2093   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field)),
2094                     "__entry_size") == 0);
2095   tree key_offset_field = DECL_CHAIN(entry_size_field);
2096   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field)),
2097                     "__key_offset") == 0);
2098   tree val_offset_field = DECL_CHAIN(key_offset_field);
2099   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field)),
2100                     "__val_offset") == 0);
2101
2102   VEC(constructor_elt, gc)* descriptor = VEC_alloc(constructor_elt, gc, 6);
2103
2104   constructor_elt* elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2105   elt->index = map_descriptor_field;
2106   elt->value = maptype->type_descriptor_pointer(this);
2107
2108   elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2109   elt->index = entry_size_field;
2110   elt->value = TYPE_SIZE_UNIT(map_entry_type);
2111
2112   elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2113   elt->index = key_offset_field;
2114   elt->value = byte_position(map_entry_key_field);
2115
2116   elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2117   elt->index = val_offset_field;
2118   elt->value = byte_position(map_entry_val_field);
2119
2120   tree constructor = build_constructor(struct_type, descriptor);
2121
2122   tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, struct_type);
2123   TREE_STATIC(decl) = 1;
2124   TREE_USED(decl) = 1;
2125   TREE_READONLY(decl) = 1;
2126   TREE_CONSTANT(decl) = 1;
2127   DECL_INITIAL(decl) = constructor;
2128   make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2129   resolve_unique_section(decl, 1, 0);
2130
2131   rest_of_decl_compilation(decl, 1, 0);
2132
2133   go_preserve_from_gc(decl);
2134   p->second = decl;
2135
2136   return build_fold_addr_expr(decl);
2137 }
2138
2139 // Return a tree for the type of a map descriptor.  This is struct
2140 // __go_map_descriptor in libgo/runtime/map.h.  This is the same for
2141 // all map types.
2142
2143 tree
2144 Gogo::map_descriptor_type()
2145 {
2146   static tree struct_type;
2147   tree dtype = Type::make_type_descriptor_type()->get_tree(this);
2148   dtype = build_qualified_type(dtype, TYPE_QUAL_CONST);
2149   return Gogo::builtin_struct(&struct_type, "__go_map_descriptor", NULL_TREE,
2150                               4,
2151                               "__map_descriptor",
2152                               build_pointer_type(dtype),
2153                               "__entry_size",
2154                               sizetype,
2155                               "__key_offset",
2156                               sizetype,
2157                               "__val_offset",
2158                               sizetype);
2159 }
2160
2161 // Return the name to use for a type descriptor decl for TYPE.  This
2162 // is used when TYPE does not have a name.
2163
2164 std::string
2165 Gogo::unnamed_type_descriptor_decl_name(const Type* type)
2166 {
2167   return "__go_td_" + type->mangled_name(this);
2168 }
2169
2170 // Return the name to use for a type descriptor decl for a type named
2171 // NAME, defined in the function IN_FUNCTION.  IN_FUNCTION will
2172 // normally be NULL.
2173
2174 std::string
2175 Gogo::type_descriptor_decl_name(const Named_object* no,
2176                                 const Named_object* in_function)
2177 {
2178   std::string ret = "__go_tdn_";
2179   if (no->type_value()->is_builtin())
2180     go_assert(in_function == NULL);
2181   else
2182     {
2183       const std::string& unique_prefix(no->package() == NULL
2184                                        ? this->unique_prefix()
2185                                        : no->package()->unique_prefix());
2186       const std::string& package_name(no->package() == NULL
2187                                       ? this->package_name()
2188                                       : no->package()->name());
2189       ret.append(unique_prefix);
2190       ret.append(1, '.');
2191       ret.append(package_name);
2192       ret.append(1, '.');
2193       if (in_function != NULL)
2194         {
2195           ret.append(Gogo::unpack_hidden_name(in_function->name()));
2196           ret.append(1, '.');
2197         }
2198     }
2199   ret.append(no->name());
2200   return ret;
2201 }
2202
2203 // Where a type descriptor decl should be defined.
2204
2205 Gogo::Type_descriptor_location
2206 Gogo::type_descriptor_location(const Type* type)
2207 {
2208   const Named_type* name = type->named_type();
2209   if (name != NULL)
2210     {
2211       if (name->named_object()->package() != NULL)
2212         {
2213           // This is a named type defined in a different package.  The
2214           // descriptor should be defined in that package.
2215           return TYPE_DESCRIPTOR_UNDEFINED;
2216         }
2217       else if (name->is_builtin())
2218         {
2219           // We create the descriptor for a builtin type whenever we
2220           // need it.
2221           return TYPE_DESCRIPTOR_COMMON;
2222         }
2223       else
2224         {
2225           // This is a named type defined in this package.  The
2226           // descriptor should be defined here.
2227           return TYPE_DESCRIPTOR_DEFINED;
2228         }
2229     }
2230   else
2231     {
2232       if (type->points_to() != NULL
2233           && type->points_to()->named_type() != NULL
2234           && type->points_to()->named_type()->named_object()->package() != NULL)
2235         {
2236           // This is an unnamed pointer to a named type defined in a
2237           // different package.  The descriptor should be defined in
2238           // that package.
2239           return TYPE_DESCRIPTOR_UNDEFINED;
2240         }
2241       else
2242         {
2243           // This is an unnamed type.  The descriptor could be defined
2244           // in any package where it is needed, and the linker will
2245           // pick one descriptor to keep.
2246           return TYPE_DESCRIPTOR_COMMON;
2247         }
2248     }
2249 }
2250
2251 // Build a type descriptor decl for TYPE.  INITIALIZER is a struct
2252 // composite literal which initializers the type descriptor.
2253
2254 void
2255 Gogo::build_type_descriptor_decl(const Type* type, Expression* initializer,
2256                                  tree* pdecl)
2257 {
2258   const Named_type* name = type->named_type();
2259
2260   // We can have multiple instances of unnamed types, but we only want
2261   // to emit the type descriptor once.  We use a hash table to handle
2262   // this.  This is not necessary for named types, as they are unique,
2263   // and we store the type descriptor decl in the type itself.
2264   tree* phash = NULL;
2265   if (name == NULL)
2266     {
2267       if (this->type_descriptor_decls_ == NULL)
2268         this->type_descriptor_decls_ = new Type_descriptor_decls(10);
2269
2270       std::pair<Type_descriptor_decls::iterator, bool> ins =
2271         this->type_descriptor_decls_->insert(std::make_pair(type, NULL_TREE));
2272       if (!ins.second)
2273         {
2274           // We've already built a type descriptor for this type.
2275           *pdecl = ins.first->second;
2276           return;
2277         }
2278       phash = &ins.first->second;
2279     }
2280
2281   std::string decl_name;
2282   if (name == NULL)
2283     decl_name = this->unnamed_type_descriptor_decl_name(type);
2284   else
2285     decl_name = this->type_descriptor_decl_name(name->named_object(),
2286                                                 name->in_function());
2287   tree id = get_identifier_from_string(decl_name);
2288   tree descriptor_type_tree = initializer->type()->get_tree(this);
2289   if (descriptor_type_tree == error_mark_node)
2290     {
2291       *pdecl = error_mark_node;
2292       return;
2293     }
2294   tree decl = build_decl(name == NULL ? BUILTINS_LOCATION : name->location(),
2295                          VAR_DECL, id,
2296                          build_qualified_type(descriptor_type_tree,
2297                                               TYPE_QUAL_CONST));
2298   TREE_READONLY(decl) = 1;
2299   TREE_CONSTANT(decl) = 1;
2300   DECL_ARTIFICIAL(decl) = 1;
2301
2302   go_preserve_from_gc(decl);
2303   if (phash != NULL)
2304     *phash = decl;
2305
2306   // We store the new DECL now because we may need to refer to it when
2307   // expanding INITIALIZER.
2308   *pdecl = decl;
2309
2310   // If appropriate, just refer to the exported type identifier.
2311   Gogo::Type_descriptor_location type_descriptor_location =
2312     this->type_descriptor_location(type);
2313   if (type_descriptor_location == TYPE_DESCRIPTOR_UNDEFINED)
2314     {
2315       TREE_PUBLIC(decl) = 1;
2316       DECL_EXTERNAL(decl) = 1;
2317       return;
2318     }
2319
2320   TREE_STATIC(decl) = 1;
2321   TREE_USED(decl) = 1;
2322
2323   Translate_context context(this, NULL, NULL, NULL);
2324   context.set_is_const();
2325   tree constructor = initializer->get_tree(&context);
2326
2327   if (constructor == error_mark_node)
2328     go_assert(saw_errors());
2329
2330   DECL_INITIAL(decl) = constructor;
2331
2332   if (type_descriptor_location == TYPE_DESCRIPTOR_DEFINED)
2333     TREE_PUBLIC(decl) = 1;
2334   else
2335     {
2336       go_assert(type_descriptor_location == TYPE_DESCRIPTOR_COMMON);
2337       make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2338       resolve_unique_section(decl, 1, 0);
2339     }
2340
2341   rest_of_decl_compilation(decl, 1, 0);
2342 }
2343
2344 // Build an interface method table for a type: a list of function
2345 // pointers, one for each interface method.  This is used for
2346 // interfaces.
2347
2348 tree
2349 Gogo::interface_method_table_for_type(const Interface_type* interface,
2350                                       Named_type* type,
2351                                       bool is_pointer)
2352 {
2353   const Typed_identifier_list* interface_methods = interface->methods();
2354   go_assert(!interface_methods->empty());
2355
2356   std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
2357                               + interface->mangled_name(this)
2358                               + "__"
2359                               + type->mangled_name(this));
2360
2361   tree id = get_identifier_from_string(mangled_name);
2362
2363   // See whether this interface has any hidden methods.
2364   bool has_hidden_methods = false;
2365   for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2366        p != interface_methods->end();
2367        ++p)
2368     {
2369       if (Gogo::is_hidden_name(p->name()))
2370         {
2371           has_hidden_methods = true;
2372           break;
2373         }
2374     }
2375
2376   // We already know that the named type is convertible to the
2377   // interface.  If the interface has hidden methods, and the named
2378   // type is defined in a different package, then the interface
2379   // conversion table will be defined by that other package.
2380   if (has_hidden_methods && type->named_object()->package() != NULL)
2381     {
2382       tree array_type = build_array_type(const_ptr_type_node, NULL);
2383       tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2384       TREE_READONLY(decl) = 1;
2385       TREE_CONSTANT(decl) = 1;
2386       TREE_PUBLIC(decl) = 1;
2387       DECL_EXTERNAL(decl) = 1;
2388       go_preserve_from_gc(decl);
2389       return decl;
2390     }
2391
2392   size_t count = interface_methods->size();
2393   VEC(constructor_elt, gc)* pointers = VEC_alloc(constructor_elt, gc,
2394                                                  count + 1);
2395
2396   // The first element is the type descriptor.
2397   constructor_elt* elt = VEC_quick_push(constructor_elt, pointers, NULL);
2398   elt->index = size_zero_node;
2399   Type* td_type;
2400   if (!is_pointer)
2401     td_type = type;
2402   else
2403     td_type = Type::make_pointer_type(type);
2404   elt->value = fold_convert(const_ptr_type_node,
2405                             td_type->type_descriptor_pointer(this));
2406
2407   size_t i = 1;
2408   for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2409        p != interface_methods->end();
2410        ++p, ++i)
2411     {
2412       bool is_ambiguous;
2413       Method* m = type->method_function(p->name(), &is_ambiguous);
2414       go_assert(m != NULL);
2415
2416       Named_object* no = m->named_object();
2417
2418       tree fnid = no->get_id(this);
2419
2420       tree fndecl;
2421       if (no->is_function())
2422         fndecl = no->func_value()->get_or_make_decl(this, no, fnid);
2423       else if (no->is_function_declaration())
2424         fndecl = no->func_declaration_value()->get_or_make_decl(this, no,
2425                                                                 fnid);
2426       else
2427         gcc_unreachable();
2428       fndecl = build_fold_addr_expr(fndecl);
2429
2430       elt = VEC_quick_push(constructor_elt, pointers, NULL);
2431       elt->index = size_int(i);
2432       elt->value = fold_convert(const_ptr_type_node, fndecl);
2433     }
2434   go_assert(i == count + 1);
2435
2436   tree array_type = build_array_type(const_ptr_type_node,
2437                                      build_index_type(size_int(count)));
2438   tree constructor = build_constructor(array_type, pointers);
2439
2440   tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2441   TREE_STATIC(decl) = 1;
2442   TREE_USED(decl) = 1;
2443   TREE_READONLY(decl) = 1;
2444   TREE_CONSTANT(decl) = 1;
2445   DECL_INITIAL(decl) = constructor;
2446
2447   // If the interface type has hidden methods, then this is the only
2448   // definition of the table.  Otherwise it is a comdat table which
2449   // may be defined in multiple packages.
2450   if (has_hidden_methods)
2451     TREE_PUBLIC(decl) = 1;
2452   else
2453     {
2454       make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2455       resolve_unique_section(decl, 1, 0);
2456     }
2457
2458   rest_of_decl_compilation(decl, 1, 0);
2459
2460   go_preserve_from_gc(decl);
2461
2462   return decl;
2463 }
2464
2465 // Mark a function as a builtin library function.
2466
2467 void
2468 Gogo::mark_fndecl_as_builtin_library(tree fndecl)
2469 {
2470   DECL_EXTERNAL(fndecl) = 1;
2471   TREE_PUBLIC(fndecl) = 1;
2472   DECL_ARTIFICIAL(fndecl) = 1;
2473   TREE_NOTHROW(fndecl) = 1;
2474   DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
2475   DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
2476 }
2477
2478 // Build a call to a builtin function.
2479
2480 tree
2481 Gogo::call_builtin(tree* pdecl, source_location location, const char* name,
2482                    int nargs, tree rettype, ...)
2483 {
2484   if (rettype == error_mark_node)
2485     return error_mark_node;
2486
2487   tree* types = new tree[nargs];
2488   tree* args = new tree[nargs];
2489
2490   va_list ap;
2491   va_start(ap, rettype);
2492   for (int i = 0; i < nargs; ++i)
2493     {
2494       types[i] = va_arg(ap, tree);
2495       args[i] = va_arg(ap, tree);
2496       if (types[i] == error_mark_node || args[i] == error_mark_node)
2497         {
2498           delete[] types;
2499           delete[] args;
2500           return error_mark_node;
2501         }
2502     }
2503   va_end(ap);
2504
2505   if (*pdecl == NULL_TREE)
2506     {
2507       tree fnid = get_identifier(name);
2508
2509       tree argtypes = NULL_TREE;
2510       tree* pp = &argtypes;
2511       for (int i = 0; i < nargs; ++i)
2512         {
2513           *pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
2514           pp = &TREE_CHAIN(*pp);
2515         }
2516       *pp = void_list_node;
2517
2518       tree fntype = build_function_type(rettype, argtypes);
2519
2520       *pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
2521       Gogo::mark_fndecl_as_builtin_library(*pdecl);
2522       go_preserve_from_gc(*pdecl);
2523     }
2524
2525   tree fnptr = build_fold_addr_expr(*pdecl);
2526   if (CAN_HAVE_LOCATION_P(fnptr))
2527     SET_EXPR_LOCATION(fnptr, location);
2528
2529   tree ret = build_call_array(rettype, fnptr, nargs, args);
2530   SET_EXPR_LOCATION(ret, location);
2531
2532   delete[] types;
2533   delete[] args;
2534
2535   return ret;
2536 }
2537
2538 // Build a call to the runtime error function.
2539
2540 tree
2541 Gogo::runtime_error(int code, source_location location)
2542 {
2543   static tree runtime_error_fndecl;
2544   tree ret = Gogo::call_builtin(&runtime_error_fndecl,
2545                                 location,
2546                                 "__go_runtime_error",
2547                                 1,
2548                                 void_type_node,
2549                                 integer_type_node,
2550                                 build_int_cst(integer_type_node, code));
2551   if (ret == error_mark_node)
2552     return error_mark_node;
2553   // The runtime error function panics and does not return.
2554   TREE_NOTHROW(runtime_error_fndecl) = 0;
2555   TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
2556   return ret;
2557 }
2558
2559 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2560 // This does a blocking receive and returns the value read from the
2561 // channel.  If FOR_SELECT is true, this is being done because it was
2562 // chosen in a select statement.
2563
2564 tree
2565 Gogo::receive_from_channel(tree type_tree, tree channel, bool for_select,
2566                            source_location location)
2567 {
2568   if (type_tree == error_mark_node || channel == error_mark_node)
2569     return error_mark_node;
2570
2571   if (int_size_in_bytes(type_tree) <= 8
2572       && !AGGREGATE_TYPE_P(type_tree)
2573       && !FLOAT_TYPE_P(type_tree))
2574     {
2575       static tree receive_small_fndecl;
2576       tree call = Gogo::call_builtin(&receive_small_fndecl,
2577                                      location,
2578                                      "__go_receive_small",
2579                                      2,
2580                                      uint64_type_node,
2581                                      ptr_type_node,
2582                                      channel,
2583                                      boolean_type_node,
2584                                      (for_select
2585                                       ? boolean_true_node
2586                                       : boolean_false_node));
2587       if (call == error_mark_node)
2588         return error_mark_node;
2589       // This can panic if there are too many operations on a closed
2590       // channel.
2591       TREE_NOTHROW(receive_small_fndecl) = 0;
2592       int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
2593       tree int_type_tree = go_type_for_size(bitsize, 1);
2594       return fold_convert_loc(location, type_tree,
2595                               fold_convert_loc(location, int_type_tree,
2596                                                call));
2597     }
2598   else
2599     {
2600       tree tmp = create_tmp_var(type_tree, get_name(type_tree));
2601       DECL_IGNORED_P(tmp) = 0;
2602       TREE_ADDRESSABLE(tmp) = 1;
2603       tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
2604       SET_EXPR_LOCATION(make_tmp, location);
2605       tree tmpaddr = build_fold_addr_expr(tmp);
2606       tmpaddr = fold_convert(ptr_type_node, tmpaddr);
2607       static tree receive_big_fndecl;
2608       tree call = Gogo::call_builtin(&receive_big_fndecl,
2609                                      location,
2610                                      "__go_receive_big",
2611                                      3,
2612                                      boolean_type_node,
2613                                      ptr_type_node,
2614                                      channel,
2615                                      ptr_type_node,
2616                                      tmpaddr,
2617                                      boolean_type_node,
2618                                      (for_select
2619                                       ? boolean_true_node
2620                                       : boolean_false_node));
2621       if (call == error_mark_node)
2622         return error_mark_node;
2623       // This can panic if there are too many operations on a closed
2624       // channel.
2625       TREE_NOTHROW(receive_big_fndecl) = 0;
2626       return build2(COMPOUND_EXPR, type_tree, make_tmp,
2627                     build2(COMPOUND_EXPR, type_tree, call, tmp));
2628     }
2629 }
2630
2631 // Return the type of a function trampoline.  This is like
2632 // get_trampoline_type in tree-nested.c.
2633
2634 tree
2635 Gogo::trampoline_type_tree()
2636 {
2637   static tree type_tree;
2638   if (type_tree == NULL_TREE)
2639     {
2640       unsigned int size;
2641       unsigned int align;
2642       go_trampoline_info(&size, &align);
2643       tree t = build_index_type(build_int_cst(integer_type_node, size - 1));
2644       t = build_array_type(char_type_node, t);
2645
2646       type_tree = Gogo::builtin_struct(NULL, "__go_trampoline", NULL_TREE, 1,
2647                                        "__data", t);
2648       t = TYPE_FIELDS(type_tree);
2649       DECL_ALIGN(t) = align;
2650       DECL_USER_ALIGN(t) = 1;
2651
2652       go_preserve_from_gc(type_tree);
2653     }
2654   return type_tree;
2655 }
2656
2657 // Make a trampoline which calls FNADDR passing CLOSURE.
2658
2659 tree
2660 Gogo::make_trampoline(tree fnaddr, tree closure, source_location location)
2661 {
2662   tree trampoline_type = Gogo::trampoline_type_tree();
2663   tree trampoline_size = TYPE_SIZE_UNIT(trampoline_type);
2664
2665   closure = save_expr(closure);
2666
2667   // We allocate the trampoline using a special function which will
2668   // mark it as executable.
2669   static tree trampoline_fndecl;
2670   tree x = Gogo::call_builtin(&trampoline_fndecl,
2671                               location,
2672                               "__go_allocate_trampoline",
2673                               2,
2674                               ptr_type_node,
2675                               size_type_node,
2676                               trampoline_size,
2677                               ptr_type_node,
2678                               fold_convert_loc(location, ptr_type_node,
2679                                                closure));
2680   if (x == error_mark_node)
2681     return error_mark_node;
2682
2683   x = save_expr(x);
2684
2685   // Initialize the trampoline.
2686   tree ini = build_call_expr(implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE],
2687                              3, x, fnaddr, closure);
2688
2689   // On some targets the trampoline address needs to be adjusted.  For
2690   // example, when compiling in Thumb mode on the ARM, the address
2691   // needs to have the low bit set.
2692   x = build_call_expr(implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
2693                       1, x);
2694   x = fold_convert(TREE_TYPE(fnaddr), x);
2695
2696   return build2(COMPOUND_EXPR, TREE_TYPE(x), ini, x);
2697 }