OSDN Git Service

96c171835c420e4efc55412fec087f9ab9dfddc7
[pf3gnuchains/gcc-fork.git] / gcc / go / go-gcc.cc
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011, 2012 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
4
5 // This file is part of GCC.
6
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
10 // version.
11
12 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 // for more details.
16
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING3.  If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include "go-system.h"
22
23 // This has to be included outside of extern "C", so we have to
24 // include it here before tree.h includes it later.
25 #include <gmp.h>
26
27 #ifndef ENABLE_BUILD_WITH_CXX
28 extern "C"
29 {
30 #endif
31
32 #include "tree.h"
33 #include "tree-iterator.h"
34 #include "gimple.h"
35 #include "toplev.h"
36
37 #ifndef ENABLE_BUILD_WITH_CXX
38 }
39 #endif
40
41 #include "go-c.h"
42
43 #include "gogo.h"
44 #include "backend.h"
45
46 // A class wrapping a tree.
47
48 class Gcc_tree
49 {
50  public:
51   Gcc_tree(tree t)
52     : t_(t)
53   { }
54
55   tree
56   get_tree() const
57   { return this->t_; }
58
59   void
60   set_tree(tree t)
61   { this->t_ = t; }
62
63  private:
64   tree t_;
65 };
66
67 // In gcc, types, expressions, and statements are all trees.
68 class Btype : public Gcc_tree
69 {
70  public:
71   Btype(tree t)
72     : Gcc_tree(t)
73   { }
74 };
75
76 class Bexpression : public Gcc_tree
77 {
78  public:
79   Bexpression(tree t)
80     : Gcc_tree(t)
81   { }
82 };
83
84 class Bstatement : public Gcc_tree
85 {
86  public:
87   Bstatement(tree t)
88     : Gcc_tree(t)
89   { }
90 };
91
92 class Bfunction : public Gcc_tree
93 {
94  public:
95   Bfunction(tree t)
96     : Gcc_tree(t)
97   { }
98 };
99
100 class Bblock : public Gcc_tree
101 {
102  public:
103   Bblock(tree t)
104     : Gcc_tree(t)
105   { }
106 };
107
108 class Bvariable : public Gcc_tree
109 {
110  public:
111   Bvariable(tree t)
112     : Gcc_tree(t)
113   { }
114 };
115
116 class Blabel : public Gcc_tree
117 {
118  public:
119   Blabel(tree t)
120     : Gcc_tree(t)
121   { }
122 };
123
124 // This file implements the interface between the Go frontend proper
125 // and the gcc IR.  This implements specific instantiations of
126 // abstract classes defined by the Go frontend proper.  The Go
127 // frontend proper class methods of these classes to generate the
128 // backend representation.
129
130 class Gcc_backend : public Backend
131 {
132  public:
133   // Types.
134
135   Btype*
136   error_type()
137   { return this->make_type(error_mark_node); }
138
139   Btype*
140   void_type()
141   { return this->make_type(void_type_node); }
142
143   Btype*
144   bool_type()
145   { return this->make_type(boolean_type_node); }
146
147   Btype*
148   integer_type(bool, int);
149
150   Btype*
151   float_type(int);
152
153   Btype*
154   complex_type(int);
155
156   Btype*
157   pointer_type(Btype*);
158
159   Btype*
160   function_type(const Btyped_identifier&,
161                 const std::vector<Btyped_identifier>&,
162                 const std::vector<Btyped_identifier>&,
163                 const Location);
164
165   Btype*
166   struct_type(const std::vector<Btyped_identifier>&);
167
168   Btype*
169   array_type(Btype*, Bexpression*);
170
171   Btype*
172   placeholder_pointer_type(const std::string&, Location, bool);
173
174   bool
175   set_placeholder_pointer_type(Btype*, Btype*);
176
177   bool
178   set_placeholder_function_type(Btype*, Btype*);
179
180   Btype*
181   placeholder_struct_type(const std::string&, Location);
182
183   bool
184   set_placeholder_struct_type(Btype* placeholder,
185                               const std::vector<Btyped_identifier>&);
186
187   Btype*
188   placeholder_array_type(const std::string&, Location);
189
190   bool
191   set_placeholder_array_type(Btype*, Btype*, Bexpression*);
192
193   Btype*
194   named_type(const std::string&, Btype*, Location);
195
196   Btype*
197   circular_pointer_type(Btype*, bool);
198
199   bool
200   is_circular_pointer_type(Btype*);
201
202   size_t
203   type_size(Btype*);
204
205   size_t
206   type_alignment(Btype*);
207
208   size_t
209   type_field_alignment(Btype*);
210
211   size_t
212   type_field_offset(Btype*, size_t index);
213
214   // Expressions.
215
216   Bexpression*
217   zero_expression(Btype*);
218
219   // Statements.
220
221   Bstatement*
222   error_statement()
223   { return this->make_statement(error_mark_node); }
224
225   Bstatement*
226   expression_statement(Bexpression*);
227
228   Bstatement*
229   init_statement(Bvariable* var, Bexpression* init);
230
231   Bstatement*
232   assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
233
234   Bstatement*
235   return_statement(Bfunction*, const std::vector<Bexpression*>&,
236                    Location);
237
238   Bstatement*
239   if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
240                Location);
241
242   Bstatement*
243   switch_statement(Bexpression* value,
244                    const std::vector<std::vector<Bexpression*> >& cases,
245                    const std::vector<Bstatement*>& statements,
246                    Location);
247
248   Bstatement*
249   compound_statement(Bstatement*, Bstatement*);
250
251   Bstatement*
252   statement_list(const std::vector<Bstatement*>&);
253
254   // Blocks.
255
256   Bblock*
257   block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
258         Location, Location);
259
260   void
261   block_add_statements(Bblock*, const std::vector<Bstatement*>&);
262
263   Bstatement*
264   block_statement(Bblock*);
265
266   // Variables.
267
268   Bvariable*
269   error_variable()
270   { return new Bvariable(error_mark_node); }
271
272   Bvariable*
273   global_variable(const std::string& package_name,
274                   const std::string& unique_prefix,
275                   const std::string& name,
276                   Btype* btype,
277                   bool is_external,
278                   bool is_hidden,
279                   Location location);
280
281   void
282   global_variable_set_init(Bvariable*, Bexpression*);
283
284   Bvariable*
285   local_variable(Bfunction*, const std::string&, Btype*, bool,
286                  Location);
287
288   Bvariable*
289   parameter_variable(Bfunction*, const std::string&, Btype*, bool,
290                      Location);
291
292   Bvariable*
293   temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
294                      Location, Bstatement**);
295
296   Bvariable*
297   immutable_struct(const std::string&, bool, Btype*, Location);
298
299   void
300   immutable_struct_set_init(Bvariable*, const std::string&, bool, Btype*,
301                             Location, Bexpression*);
302
303   Bvariable*
304   immutable_struct_reference(const std::string&, Btype*, Location);
305
306   // Labels.
307
308   Blabel*
309   label(Bfunction*, const std::string& name, Location);
310
311   Bstatement*
312   label_definition_statement(Blabel*);
313
314   Bstatement*
315   goto_statement(Blabel*, Location);
316
317   Bexpression*
318   label_address(Blabel*, Location);
319
320  private:
321   // Make a Bexpression from a tree.
322   Bexpression*
323   make_expression(tree t)
324   { return new Bexpression(t); }
325
326   // Make a Bstatement from a tree.
327   Bstatement*
328   make_statement(tree t)
329   { return new Bstatement(t); }
330
331   // Make a Btype from a tree.
332   Btype*
333   make_type(tree t)
334   { return new Btype(t); }
335
336   Btype*
337   fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
338
339   Btype*
340   fill_in_array(Btype*, Btype*, Bexpression*);
341
342   tree
343   non_zero_size_type(tree);
344 };
345
346 // A helper function.
347
348 static inline tree
349 get_identifier_from_string(const std::string& str)
350 {
351   return get_identifier_with_length(str.data(), str.length());
352 }
353
354 // Get an unnamed integer type.
355
356 Btype*
357 Gcc_backend::integer_type(bool is_unsigned, int bits)
358 {
359   tree type;
360   if (is_unsigned)
361     {
362       if (bits == INT_TYPE_SIZE)
363         type = unsigned_type_node;
364       else if (bits == CHAR_TYPE_SIZE)
365         type = unsigned_char_type_node;
366       else if (bits == SHORT_TYPE_SIZE)
367         type = short_unsigned_type_node;
368       else if (bits == LONG_TYPE_SIZE)
369         type = long_unsigned_type_node;
370       else if (bits == LONG_LONG_TYPE_SIZE)
371         type = long_long_unsigned_type_node;
372       else
373         type = make_unsigned_type(bits);
374     }
375   else
376     {
377       if (bits == INT_TYPE_SIZE)
378         type = integer_type_node;
379       else if (bits == CHAR_TYPE_SIZE)
380         type = signed_char_type_node;
381       else if (bits == SHORT_TYPE_SIZE)
382         type = short_integer_type_node;
383       else if (bits == LONG_TYPE_SIZE)
384         type = long_integer_type_node;
385       else if (bits == LONG_LONG_TYPE_SIZE)
386         type = long_long_integer_type_node;
387       else
388         type = make_signed_type(bits);
389     }
390   return this->make_type(type);
391 }
392
393 // Get an unnamed float type.
394
395 Btype*
396 Gcc_backend::float_type(int bits)
397 {
398   tree type;
399   if (bits == FLOAT_TYPE_SIZE)
400     type = float_type_node;
401   else if (bits == DOUBLE_TYPE_SIZE)
402     type = double_type_node;
403   else if (bits == LONG_DOUBLE_TYPE_SIZE)
404     type = long_double_type_node;
405   else
406     {
407       type = make_node(REAL_TYPE);
408       TYPE_PRECISION(type) = bits;
409       layout_type(type);
410     }
411   return this->make_type(type);
412 }
413
414 // Get an unnamed complex type.
415
416 Btype*
417 Gcc_backend::complex_type(int bits)
418 {
419   tree type;
420   if (bits == FLOAT_TYPE_SIZE * 2)
421     type = complex_float_type_node;
422   else if (bits == DOUBLE_TYPE_SIZE * 2)
423     type = complex_double_type_node;
424   else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
425     type = complex_long_double_type_node;
426   else
427     {
428       type = make_node(REAL_TYPE);
429       TYPE_PRECISION(type) = bits / 2;
430       layout_type(type);
431       type = build_complex_type(type);
432     }
433   return this->make_type(type);
434 }
435
436 // Get a pointer type.
437
438 Btype*
439 Gcc_backend::pointer_type(Btype* to_type)
440 {
441   tree to_type_tree = to_type->get_tree();
442   if (to_type_tree == error_mark_node)
443     return this->error_type();
444   tree type = build_pointer_type(to_type_tree);
445   return this->make_type(type);
446 }
447
448 // Make a function type.
449
450 Btype*
451 Gcc_backend::function_type(const Btyped_identifier& receiver,
452                            const std::vector<Btyped_identifier>& parameters,
453                            const std::vector<Btyped_identifier>& results,
454                            Location location)
455 {
456   tree args = NULL_TREE;
457   tree* pp = &args;
458   if (receiver.btype != NULL)
459     {
460       tree t = receiver.btype->get_tree();
461       if (t == error_mark_node)
462         return this->error_type();
463       *pp = tree_cons(NULL_TREE, t, NULL_TREE);
464       pp = &TREE_CHAIN(*pp);
465     }
466
467   for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
468        p != parameters.end();
469        ++p)
470     {
471       tree t = p->btype->get_tree();
472       if (t == error_mark_node)
473         return this->error_type();
474       *pp = tree_cons(NULL_TREE, t, NULL_TREE);
475       pp = &TREE_CHAIN(*pp);
476     }
477
478   // Varargs is handled entirely at the Go level.  When converted to
479   // GENERIC functions are not varargs.
480   *pp = void_list_node;
481
482   tree result;
483   if (results.empty())
484     result = void_type_node;
485   else if (results.size() == 1)
486     result = results.front().btype->get_tree();
487   else
488     {
489       result = make_node(RECORD_TYPE);
490       tree field_trees = NULL_TREE;
491       pp = &field_trees;
492       for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
493            p != results.end();
494            ++p)
495         {
496           const std::string name = (p->name.empty()
497                                     ? "UNNAMED"
498                                     : p->name);
499           tree name_tree = get_identifier_from_string(name);
500           tree field_type_tree = p->btype->get_tree();
501           if (field_type_tree == error_mark_node)
502             return this->error_type();
503           gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
504           tree field = build_decl(location.gcc_location(), FIELD_DECL,
505                                   name_tree, field_type_tree);
506           DECL_CONTEXT(field) = result;
507           *pp = field;
508           pp = &DECL_CHAIN(field);
509         }
510       TYPE_FIELDS(result) = field_trees;
511       layout_type(result);
512     }
513   if (result == error_mark_node)
514     return this->error_type();
515
516   tree fntype = build_function_type(result, args);
517   if (fntype == error_mark_node)
518     return this->error_type();
519
520   return this->make_type(build_pointer_type(fntype));
521 }
522
523 // Make a struct type.
524
525 Btype*
526 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
527 {
528   return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
529 }
530
531 // Fill in the fields of a struct type.
532
533 Btype*
534 Gcc_backend::fill_in_struct(Btype* fill,
535                             const std::vector<Btyped_identifier>& fields)
536 {
537   tree fill_tree = fill->get_tree();
538   tree field_trees = NULL_TREE;
539   tree* pp = &field_trees;
540   for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
541        p != fields.end();
542        ++p)
543     {
544       tree name_tree = get_identifier_from_string(p->name);
545       tree type_tree = p->btype->get_tree();
546       if (type_tree == error_mark_node)
547         return this->error_type();
548       tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
549                               type_tree);
550       DECL_CONTEXT(field) = fill_tree;
551       *pp = field;
552       pp = &DECL_CHAIN(field);
553     }
554   TYPE_FIELDS(fill_tree) = field_trees;
555   layout_type(fill_tree);
556   return fill;
557 }
558
559 // Make an array type.
560
561 Btype*
562 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
563 {
564   return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
565                              element_btype, length);
566 }
567
568 // Fill in an array type.
569
570 Btype*
571 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
572                            Bexpression* length)
573 {
574   tree element_type_tree = element_type->get_tree();
575   tree length_tree = length->get_tree();
576   if (element_type_tree == error_mark_node || length_tree == error_mark_node)
577     return this->error_type();
578
579   gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
580
581   length_tree = fold_convert(sizetype, length_tree);
582
583   // build_index_type takes the maximum index, which is one less than
584   // the length.
585   tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
586                                                       length_tree,
587                                                       size_one_node));
588
589   tree fill_tree = fill->get_tree();
590   TREE_TYPE(fill_tree) = element_type_tree;
591   TYPE_DOMAIN(fill_tree) = index_type_tree;
592   TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
593   layout_type(fill_tree);
594
595   if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
596     SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
597   else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
598            || TYPE_CANONICAL(index_type_tree) != index_type_tree)
599     TYPE_CANONICAL(fill_tree) =
600       build_array_type(TYPE_CANONICAL(element_type_tree),
601                        TYPE_CANONICAL(index_type_tree));
602
603   return fill;
604 }
605
606 // Create a placeholder for a pointer type.
607
608 Btype*
609 Gcc_backend::placeholder_pointer_type(const std::string& name,
610                                       Location location, bool)
611 {
612   tree ret = build_distinct_type_copy(ptr_type_node);
613   if (!name.empty())
614     {
615       tree decl = build_decl(location.gcc_location(), TYPE_DECL,
616                              get_identifier_from_string(name),
617                              ret);
618       TYPE_NAME(ret) = decl;
619     }
620   return this->make_type(ret);
621 }
622
623 // Set the real target type for a placeholder pointer type.
624
625 bool
626 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
627                                           Btype* to_type)
628 {
629   tree pt = placeholder->get_tree();
630   if (pt == error_mark_node)
631     return false;
632   gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
633   tree tt = to_type->get_tree();
634   if (tt == error_mark_node)
635     {
636       placeholder->set_tree(error_mark_node);
637       return false;
638     }
639   gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
640   TREE_TYPE(pt) = TREE_TYPE(tt);
641   if (TYPE_NAME(pt) != NULL_TREE)
642     {
643       // Build the data structure gcc wants to see for a typedef.
644       tree copy = build_variant_type_copy(pt);
645       TYPE_NAME(copy) = NULL_TREE;
646       DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
647     }
648   return true;
649 }
650
651 // Set the real values for a placeholder function type.
652
653 bool
654 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
655 {
656   return this->set_placeholder_pointer_type(placeholder, ft);
657 }
658
659 // Create a placeholder for a struct type.
660
661 Btype*
662 Gcc_backend::placeholder_struct_type(const std::string& name,
663                                      Location location)
664 {
665   tree ret = make_node(RECORD_TYPE);
666   if (!name.empty())
667     {
668       tree decl = build_decl(location.gcc_location(), TYPE_DECL,
669                              get_identifier_from_string(name),
670                              ret);
671       TYPE_NAME(ret) = decl;
672     }
673   return this->make_type(ret);
674 }
675
676 // Fill in the fields of a placeholder struct type.
677
678 bool
679 Gcc_backend::set_placeholder_struct_type(
680     Btype* placeholder,
681     const std::vector<Btyped_identifier>& fields)
682 {
683   tree t = placeholder->get_tree();
684   gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
685   Btype* r = this->fill_in_struct(placeholder, fields);
686
687   if (TYPE_NAME(t) != NULL_TREE)
688     {
689       // Build the data structure gcc wants to see for a typedef.
690       tree copy = build_distinct_type_copy(t);
691       TYPE_NAME(copy) = NULL_TREE;
692       DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
693     }
694
695   return r->get_tree() != error_mark_node;
696 }
697
698 // Create a placeholder for an array type.
699
700 Btype*
701 Gcc_backend::placeholder_array_type(const std::string& name,
702                                     Location location)
703 {
704   tree ret = make_node(ARRAY_TYPE);
705   tree decl = build_decl(location.gcc_location(), TYPE_DECL,
706                          get_identifier_from_string(name),
707                          ret);
708   TYPE_NAME(ret) = decl;
709   return this->make_type(ret);
710 }
711
712 // Fill in the fields of a placeholder array type.
713
714 bool
715 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
716                                         Btype* element_btype,
717                                         Bexpression* length)
718 {
719   tree t = placeholder->get_tree();
720   gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
721   Btype* r = this->fill_in_array(placeholder, element_btype, length);
722
723   // Build the data structure gcc wants to see for a typedef.
724   tree copy = build_distinct_type_copy(t);
725   TYPE_NAME(copy) = NULL_TREE;
726   DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
727
728   return r->get_tree() != error_mark_node;
729 }
730
731 // Return a named version of a type.
732
733 Btype*
734 Gcc_backend::named_type(const std::string& name, Btype* btype,
735                         Location location)
736 {
737   tree type = btype->get_tree();
738   if (type == error_mark_node)
739     return this->error_type();
740
741   // The middle-end expects a basic type to have a name.  In Go every
742   // basic type will have a name.  The first time we see a basic type,
743   // give it whatever Go name we have at this point.
744   if (TYPE_NAME(type) == NULL_TREE
745       && location.gcc_location() == BUILTINS_LOCATION
746       && (TREE_CODE(type) == INTEGER_TYPE
747           || TREE_CODE(type) == REAL_TYPE
748           || TREE_CODE(type) == COMPLEX_TYPE
749           || TREE_CODE(type) == BOOLEAN_TYPE))
750     {
751       tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
752                              get_identifier_from_string(name),
753                              type);
754       TYPE_NAME(type) = decl;
755       return this->make_type(type);
756     }
757
758   tree copy = build_variant_type_copy(type);
759   tree decl = build_decl(location.gcc_location(), TYPE_DECL,
760                          get_identifier_from_string(name),
761                          copy);
762   DECL_ORIGINAL_TYPE(decl) = type;
763   TYPE_NAME(copy) = decl;
764   return this->make_type(copy);
765 }
766
767 // Return a pointer type used as a marker for a circular type.
768
769 Btype*
770 Gcc_backend::circular_pointer_type(Btype*, bool)
771 {
772   return this->make_type(ptr_type_node);
773 }
774
775 // Return whether we might be looking at a circular type.
776
777 bool
778 Gcc_backend::is_circular_pointer_type(Btype* btype)
779 {
780   return btype->get_tree() == ptr_type_node;
781 }
782
783 // Return the size of a type.
784
785 size_t
786 Gcc_backend::type_size(Btype* btype)
787 {
788   tree t = btype->get_tree();
789   if (t == error_mark_node)
790     return 1;
791   t = TYPE_SIZE_UNIT(t);
792   gcc_assert(TREE_CODE(t) == INTEGER_CST);
793   gcc_assert(TREE_INT_CST_HIGH(t) == 0);
794   unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
795   size_t ret = static_cast<size_t>(val_wide);
796   gcc_assert(ret == val_wide);
797   return ret;
798 }
799
800 // Return the alignment of a type.
801
802 size_t
803 Gcc_backend::type_alignment(Btype* btype)
804 {
805   tree t = btype->get_tree();
806   if (t == error_mark_node)
807     return 1;
808   return TYPE_ALIGN_UNIT(t);
809 }
810
811 // Return the alignment of a struct field of type BTYPE.
812
813 size_t
814 Gcc_backend::type_field_alignment(Btype* btype)
815 {
816   tree t = btype->get_tree();
817   if (t == error_mark_node)
818     return 1;
819   return go_field_alignment(t);
820 }
821
822 // Return the offset of a field in a struct.
823
824 size_t
825 Gcc_backend::type_field_offset(Btype* btype, size_t index)
826 {
827   tree struct_tree = btype->get_tree();
828   if (struct_tree == error_mark_node)
829     return 0;
830   gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
831   tree field = TYPE_FIELDS(struct_tree);
832   for (; index > 0; --index)
833     {
834       field = DECL_CHAIN(field);
835       gcc_assert(field != NULL_TREE);
836     }
837   HOST_WIDE_INT offset_wide = int_byte_position(field);
838   gcc_assert(offset_wide >= 0);
839   size_t ret = static_cast<size_t>(offset_wide);
840   gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
841   return ret;
842 }
843
844 // Return the zero value for a type.
845
846 Bexpression*
847 Gcc_backend::zero_expression(Btype* btype)
848 {
849   tree t = btype->get_tree();
850   tree ret;
851   if (t == error_mark_node)
852     ret = error_mark_node;
853   else
854     ret = build_zero_cst(t);
855   return tree_to_expr(ret);
856 }
857
858 // An expression as a statement.
859
860 Bstatement*
861 Gcc_backend::expression_statement(Bexpression* expr)
862 {
863   return this->make_statement(expr->get_tree());
864 }
865
866 // Variable initialization.
867
868 Bstatement*
869 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
870 {
871   tree var_tree = var->get_tree();
872   tree init_tree = init->get_tree();
873   if (var_tree == error_mark_node || init_tree == error_mark_node)
874     return this->error_statement();
875   gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
876
877   // To avoid problems with GNU ld, we don't make zero-sized
878   // externally visible variables.  That might lead us to doing an
879   // initialization of a zero-sized expression to a non-zero sized
880   // variable, or vice-versa.  Avoid crashes by omitting the
881   // initializer.  Such initializations don't mean anything anyhow.
882   if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
883       && init_tree != NULL_TREE
884       && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
885     {
886       DECL_INITIAL(var_tree) = init_tree;
887       init_tree = NULL_TREE;
888     }
889
890   tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
891                         void_type_node, var_tree);
892   if (init_tree != NULL_TREE)
893     ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
894                      void_type_node, init_tree, ret);
895
896   return this->make_statement(ret);
897 }
898
899 // Assignment.
900
901 Bstatement*
902 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
903                                   Location location)
904 {
905   tree lhs_tree = lhs->get_tree();
906   tree rhs_tree = rhs->get_tree();
907   if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
908     return this->error_statement();
909
910   // To avoid problems with GNU ld, we don't make zero-sized
911   // externally visible variables.  That might lead us to doing an
912   // assignment of a zero-sized expression to a non-zero sized
913   // expression; avoid crashes here by avoiding assignments of
914   // zero-sized expressions.  Such assignments don't really mean
915   // anything anyhow.
916   if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
917       || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
918     return this->compound_statement(this->expression_statement(lhs),
919                                     this->expression_statement(rhs));
920
921   // Sometimes the same unnamed Go type can be created multiple times
922   // and thus have multiple tree representations.  Make sure this does
923   // not confuse the middle-end.
924   if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
925     {
926       tree lhs_type_tree = TREE_TYPE(lhs_tree);
927       gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
928       if (POINTER_TYPE_P(lhs_type_tree)
929           || INTEGRAL_TYPE_P(lhs_type_tree)
930           || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
931           || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
932         rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
933                                     rhs_tree);
934       else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
935                || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
936         {
937           gcc_assert(int_size_in_bytes(lhs_type_tree)
938                      == int_size_in_bytes(TREE_TYPE(rhs_tree)));
939           rhs_tree = fold_build1_loc(location.gcc_location(),
940                                      VIEW_CONVERT_EXPR,
941                                      lhs_type_tree, rhs_tree);
942         }
943     }
944
945   return this->make_statement(fold_build2_loc(location.gcc_location(),
946                                               MODIFY_EXPR,
947                                               void_type_node,
948                                               lhs_tree, rhs_tree));
949 }
950
951 // Return.
952
953 Bstatement*
954 Gcc_backend::return_statement(Bfunction* bfunction,
955                               const std::vector<Bexpression*>& vals,
956                               Location location)
957 {
958   tree fntree = bfunction->get_tree();
959   if (fntree == error_mark_node)
960     return this->error_statement();
961   tree result = DECL_RESULT(fntree);
962   if (result == error_mark_node)
963     return this->error_statement();
964   tree ret;
965   if (vals.empty())
966     ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
967                           NULL_TREE);
968   else if (vals.size() == 1)
969     {
970       tree val = vals.front()->get_tree();
971       if (val == error_mark_node)
972         return this->error_statement();
973       tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
974                                  void_type_node, result,
975                                  vals.front()->get_tree());
976       ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
977                             void_type_node, set);
978     }
979   else
980     {
981       // To return multiple values, copy the values into a temporary
982       // variable of the right structure type, and then assign the
983       // temporary variable to the DECL_RESULT in the return
984       // statement.
985       tree stmt_list = NULL_TREE;
986       tree rettype = TREE_TYPE(result);
987       tree rettmp = create_tmp_var(rettype, "RESULT");
988       tree field = TYPE_FIELDS(rettype);
989       for (std::vector<Bexpression*>::const_iterator p = vals.begin();
990            p != vals.end();
991            p++, field = DECL_CHAIN(field))
992         {
993           gcc_assert(field != NULL_TREE);
994           tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
995                                      TREE_TYPE(field), rettmp, field,
996                                      NULL_TREE);
997           tree val = (*p)->get_tree();
998           if (val == error_mark_node)
999             return this->error_statement();
1000           tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1001                                      void_type_node,
1002                                      ref, (*p)->get_tree());
1003           append_to_statement_list(set, &stmt_list);
1004         }
1005       gcc_assert(field == NULL_TREE);
1006       tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1007                                  void_type_node,
1008                                  result, rettmp);
1009       tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1010                                       void_type_node, set);
1011       append_to_statement_list(ret_expr, &stmt_list);
1012       ret = stmt_list;
1013     }
1014   return this->make_statement(ret);
1015 }
1016
1017 // If.
1018
1019 Bstatement*
1020 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1021                           Bblock* else_block, Location location)
1022 {
1023   tree cond_tree = condition->get_tree();
1024   tree then_tree = then_block->get_tree();
1025   tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1026   if (cond_tree == error_mark_node
1027       || then_tree == error_mark_node
1028       || else_tree == error_mark_node)
1029     return this->error_statement();
1030   tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1031                         cond_tree, then_tree, else_tree);
1032   return this->make_statement(ret);
1033 }
1034
1035 // Switch.
1036
1037 Bstatement*
1038 Gcc_backend::switch_statement(
1039     Bexpression* value,
1040     const std::vector<std::vector<Bexpression*> >& cases,
1041     const std::vector<Bstatement*>& statements,
1042     Location switch_location)
1043 {
1044   gcc_assert(cases.size() == statements.size());
1045
1046   tree stmt_list = NULL_TREE;
1047   std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1048   for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1049        ps != statements.end();
1050        ++ps, ++pc)
1051     {
1052       if (pc->empty())
1053         {
1054           source_location loc = (*ps != NULL
1055                                  ? EXPR_LOCATION((*ps)->get_tree())
1056                                  : UNKNOWN_LOCATION);
1057           tree label = create_artificial_label(loc);
1058           tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1059           append_to_statement_list(c, &stmt_list);
1060         }
1061       else
1062         {
1063           for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1064                pcv != pc->end();
1065                ++pcv)
1066             {
1067               tree t = (*pcv)->get_tree();
1068               if (t == error_mark_node)
1069                 return this->error_statement();
1070               source_location loc = EXPR_LOCATION(t);
1071               tree label = create_artificial_label(loc);
1072               tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1073               append_to_statement_list(c, &stmt_list);
1074             }
1075         }
1076
1077       if (*ps != NULL)
1078         {
1079           tree t = (*ps)->get_tree();
1080           if (t == error_mark_node)
1081             return this->error_statement();
1082           append_to_statement_list(t, &stmt_list);
1083         }
1084     }
1085
1086   tree tv = value->get_tree();
1087   if (tv == error_mark_node)
1088     return this->error_statement();
1089   tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1090                       void_type_node, tv, stmt_list, NULL_TREE);
1091   return this->make_statement(t);
1092 }
1093
1094 // Pair of statements.
1095
1096 Bstatement*
1097 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1098 {
1099   tree stmt_list = NULL_TREE;
1100   tree t = s1->get_tree();
1101   if (t == error_mark_node)
1102     return this->error_statement();
1103   append_to_statement_list(t, &stmt_list);
1104   t = s2->get_tree();
1105   if (t == error_mark_node)
1106     return this->error_statement();
1107   append_to_statement_list(t, &stmt_list);
1108   return this->make_statement(stmt_list);
1109 }
1110
1111 // List of statements.
1112
1113 Bstatement*
1114 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1115 {
1116   tree stmt_list = NULL_TREE;
1117   for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1118        p != statements.end();
1119        ++p)
1120     {
1121       tree t = (*p)->get_tree();
1122       if (t == error_mark_node)
1123         return this->error_statement();
1124       append_to_statement_list(t, &stmt_list);
1125     }
1126   return this->make_statement(stmt_list);
1127 }
1128
1129 // Make a block.  For some reason gcc uses a dual structure for
1130 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes.  Since the
1131 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1132 // the Bblock.
1133
1134 Bblock*
1135 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1136                    const std::vector<Bvariable*>& vars,
1137                    Location start_location,
1138                    Location)
1139 {
1140   tree block_tree = make_node(BLOCK);
1141   if (enclosing == NULL)
1142     {
1143       // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1144       // until we have a proper representation of the init function.
1145       tree fndecl;
1146       if (function == NULL)
1147         fndecl = current_function_decl;
1148       else
1149         fndecl = function->get_tree();
1150       gcc_assert(fndecl != NULL_TREE);
1151
1152       // We may have already created a block for local variables when
1153       // we take the address of a parameter.
1154       if (DECL_INITIAL(fndecl) == NULL_TREE)
1155         {
1156           BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1157           DECL_INITIAL(fndecl) = block_tree;
1158         }
1159       else
1160         {
1161           tree superblock_tree = DECL_INITIAL(fndecl);
1162           BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1163           tree* pp;
1164           for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1165                *pp != NULL_TREE;
1166                pp = &BLOCK_CHAIN(*pp))
1167             ;
1168           *pp = block_tree;
1169         }
1170     }
1171   else
1172     {
1173       tree superbind_tree = enclosing->get_tree();
1174       tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1175       gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1176
1177       BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1178       tree* pp;
1179       for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1180            *pp != NULL_TREE;
1181            pp = &BLOCK_CHAIN(*pp))
1182         ;
1183       *pp = block_tree;
1184     }
1185
1186   tree* pp = &BLOCK_VARS(block_tree);
1187   for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1188        pv != vars.end();
1189        ++pv)
1190     {
1191       *pp = (*pv)->get_tree();
1192       if (*pp != error_mark_node)
1193         pp = &DECL_CHAIN(*pp);
1194     }
1195   *pp = NULL_TREE;
1196
1197   TREE_USED(block_tree) = 1;
1198
1199   tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1200                               void_type_node, BLOCK_VARS(block_tree),
1201                               NULL_TREE, block_tree);
1202   TREE_SIDE_EFFECTS(bind_tree) = 1;
1203
1204   return new Bblock(bind_tree);
1205 }
1206
1207 // Add statements to a block.
1208
1209 void
1210 Gcc_backend::block_add_statements(Bblock* bblock,
1211                                   const std::vector<Bstatement*>& statements)
1212 {
1213   tree stmt_list = NULL_TREE;
1214   for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1215        p != statements.end();
1216        ++p)
1217     {
1218       tree s = (*p)->get_tree();
1219       if (s != error_mark_node)
1220         append_to_statement_list(s, &stmt_list);
1221     }
1222
1223   tree bind_tree = bblock->get_tree();
1224   gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1225   BIND_EXPR_BODY(bind_tree) = stmt_list;
1226 }
1227
1228 // Return a block as a statement.
1229
1230 Bstatement*
1231 Gcc_backend::block_statement(Bblock* bblock)
1232 {
1233   tree bind_tree = bblock->get_tree();
1234   gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1235   return this->make_statement(bind_tree);
1236 }
1237
1238 // This is not static because we declare it with GTY(()) in go-c.h.
1239 tree go_non_zero_struct;
1240
1241 // Return a type corresponding to TYPE with non-zero size.
1242
1243 tree
1244 Gcc_backend::non_zero_size_type(tree type)
1245 {
1246   if (int_size_in_bytes(type) != 0)
1247     return type;
1248
1249   switch (TREE_CODE(type))
1250     {
1251     case RECORD_TYPE:
1252       {
1253         if (go_non_zero_struct == NULL_TREE)
1254           {
1255             type = make_node(RECORD_TYPE);
1256             tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1257                                     get_identifier("dummy"),
1258                                     boolean_type_node);
1259             DECL_CONTEXT(field) = type;
1260             TYPE_FIELDS(type) = field;
1261             layout_type(type);
1262             go_non_zero_struct = type;
1263           }
1264         return go_non_zero_struct;
1265       }
1266
1267     case ARRAY_TYPE:
1268       {
1269         tree element_type = non_zero_size_type(TREE_TYPE(type));
1270         return build_array_type_nelts(element_type, 1);
1271       }
1272
1273     default:
1274       gcc_unreachable();
1275     }
1276
1277   gcc_unreachable();
1278 }
1279
1280 // Make a global variable.
1281
1282 Bvariable*
1283 Gcc_backend::global_variable(const std::string& package_name,
1284                              const std::string& unique_prefix,
1285                              const std::string& name,
1286                              Btype* btype,
1287                              bool is_external,
1288                              bool is_hidden,
1289                              Location location)
1290 {
1291   tree type_tree = btype->get_tree();
1292   if (type_tree == error_mark_node)
1293     return this->error_variable();
1294
1295   // The GNU linker does not like dynamic variables with zero size.
1296   if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1297     type_tree = this->non_zero_size_type(type_tree);
1298
1299   std::string var_name(package_name);
1300   var_name.push_back('.');
1301   var_name.append(name);
1302   tree decl = build_decl(location.gcc_location(), VAR_DECL,
1303                          get_identifier_from_string(var_name),
1304                          type_tree);
1305   if (is_external)
1306     DECL_EXTERNAL(decl) = 1;
1307   else
1308     TREE_STATIC(decl) = 1;
1309   if (!is_hidden)
1310     {
1311       TREE_PUBLIC(decl) = 1;
1312
1313       std::string asm_name(unique_prefix);
1314       asm_name.push_back('.');
1315       asm_name.append(var_name);
1316       SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1317     }
1318   TREE_USED(decl) = 1;
1319
1320   go_preserve_from_gc(decl);
1321
1322   return new Bvariable(decl);
1323 }
1324
1325 // Set the initial value of a global variable.
1326
1327 void
1328 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1329 {
1330   tree expr_tree = expr->get_tree();
1331   if (expr_tree == error_mark_node)
1332     return;
1333   gcc_assert(TREE_CONSTANT(expr_tree));
1334   tree var_decl = var->get_tree();
1335   if (var_decl == error_mark_node)
1336     return;
1337   DECL_INITIAL(var_decl) = expr_tree;
1338 }
1339
1340 // Make a local variable.
1341
1342 Bvariable*
1343 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1344                             Btype* btype, bool is_address_taken,
1345                             Location location)
1346 {
1347   tree type_tree = btype->get_tree();
1348   if (type_tree == error_mark_node)
1349     return this->error_variable();
1350   tree decl = build_decl(location.gcc_location(), VAR_DECL,
1351                          get_identifier_from_string(name),
1352                          type_tree);
1353   DECL_CONTEXT(decl) = function->get_tree();
1354   TREE_USED(decl) = 1;
1355   if (is_address_taken)
1356     TREE_ADDRESSABLE(decl) = 1;
1357   go_preserve_from_gc(decl);
1358   return new Bvariable(decl);
1359 }
1360
1361 // Make a function parameter variable.
1362
1363 Bvariable*
1364 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1365                                 Btype* btype, bool is_address_taken,
1366                                 Location location)
1367 {
1368   tree type_tree = btype->get_tree();
1369   if (type_tree == error_mark_node)
1370     return this->error_variable();
1371   tree decl = build_decl(location.gcc_location(), PARM_DECL,
1372                          get_identifier_from_string(name),
1373                          type_tree);
1374   DECL_CONTEXT(decl) = function->get_tree();
1375   DECL_ARG_TYPE(decl) = type_tree;
1376   TREE_USED(decl) = 1;
1377   if (is_address_taken)
1378     TREE_ADDRESSABLE(decl) = 1;
1379   go_preserve_from_gc(decl);
1380   return new Bvariable(decl);
1381 }
1382
1383 // Make a temporary variable.
1384
1385 Bvariable*
1386 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1387                                 Btype* btype, Bexpression* binit,
1388                                 bool is_address_taken,
1389                                 Location location,
1390                                 Bstatement** pstatement)
1391 {
1392   tree type_tree = btype->get_tree();
1393   tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1394   if (type_tree == error_mark_node || init_tree == error_mark_node)
1395     {
1396       *pstatement = this->error_statement();
1397       return this->error_variable();
1398     }
1399
1400   tree var;
1401   // We can only use create_tmp_var if the type is not addressable.
1402   if (!TREE_ADDRESSABLE(type_tree))
1403     var = create_tmp_var(type_tree, "GOTMP");
1404   else
1405     {
1406       gcc_assert(bblock != NULL);
1407       var = build_decl(location.gcc_location(), VAR_DECL,
1408                        create_tmp_var_name("GOTMP"),
1409                        type_tree);
1410       DECL_ARTIFICIAL(var) = 1;
1411       DECL_IGNORED_P(var) = 1;
1412       TREE_USED(var) = 1;
1413       // FIXME: Permitting function to be NULL here is a temporary
1414       // measure until we have a proper representation of the init
1415       // function.
1416       if (function != NULL)
1417         DECL_CONTEXT(var) = function->get_tree();
1418       else
1419         {
1420           gcc_assert(current_function_decl != NULL_TREE);
1421           DECL_CONTEXT(var) = current_function_decl;
1422         }
1423
1424       // We have to add this variable to the BLOCK and the BIND_EXPR.
1425       tree bind_tree = bblock->get_tree();
1426       gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1427       tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1428       gcc_assert(TREE_CODE(block_tree) == BLOCK);
1429       DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1430       BLOCK_VARS(block_tree) = var;
1431       BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1432     }
1433
1434   if (init_tree != NULL_TREE)
1435     DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1436                                          init_tree);
1437
1438   if (is_address_taken)
1439     TREE_ADDRESSABLE(var) = 1;
1440
1441   *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1442                                                 DECL_EXPR,
1443                                                 void_type_node, var));
1444   return new Bvariable(var);
1445 }
1446
1447 // Create a named immutable initialized data structure.
1448
1449 Bvariable*
1450 Gcc_backend::immutable_struct(const std::string& name, bool, Btype* btype,
1451                               Location location)
1452 {
1453   tree type_tree = btype->get_tree();
1454   if (type_tree == error_mark_node)
1455     return this->error_variable();
1456   gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1457   tree decl = build_decl(location.gcc_location(), VAR_DECL,
1458                          get_identifier_from_string(name),
1459                          build_qualified_type(type_tree, TYPE_QUAL_CONST));
1460   TREE_STATIC(decl) = 1;
1461   TREE_READONLY(decl) = 1;
1462   TREE_CONSTANT(decl) = 1;
1463   TREE_USED(decl) = 1;
1464   DECL_ARTIFICIAL(decl) = 1;
1465
1466   // We don't call rest_of_decl_compilation until we have the
1467   // initializer.
1468
1469   go_preserve_from_gc(decl);
1470   return new Bvariable(decl);
1471 }
1472
1473 // Set the initializer for a variable created by immutable_struct.
1474 // This is where we finish compiling the variable.
1475
1476 void
1477 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1478                                        bool is_common, Btype*,
1479                                        Location,
1480                                        Bexpression* initializer)
1481 {
1482   tree decl = var->get_tree();
1483   tree init_tree = initializer->get_tree();
1484   if (decl == error_mark_node || init_tree == error_mark_node)
1485     return;
1486
1487   DECL_INITIAL(decl) = init_tree;
1488
1489   // We can't call make_decl_one_only until we set DECL_INITIAL.
1490   if (!is_common)
1491     TREE_PUBLIC(decl) = 1;
1492   else
1493     {
1494       make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1495       resolve_unique_section(decl, 1, 0);
1496     }
1497
1498   rest_of_decl_compilation(decl, 1, 0);
1499 }
1500
1501 // Return a reference to an immutable initialized data structure
1502 // defined in another package.
1503
1504 Bvariable*
1505 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1506                                         Location location)
1507 {
1508   tree type_tree = btype->get_tree();
1509   if (type_tree == error_mark_node)
1510     return this->error_variable();
1511   gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1512   tree decl = build_decl(location.gcc_location(), VAR_DECL,
1513                          get_identifier_from_string(name),
1514                          build_qualified_type(type_tree, TYPE_QUAL_CONST));
1515   TREE_READONLY(decl) = 1;
1516   TREE_CONSTANT(decl) = 1;
1517   DECL_ARTIFICIAL(decl) = 1;
1518   TREE_PUBLIC(decl) = 1;
1519   DECL_EXTERNAL(decl) = 1;
1520   go_preserve_from_gc(decl);
1521   return new Bvariable(decl);
1522 }
1523
1524 // Make a label.
1525
1526 Blabel*
1527 Gcc_backend::label(Bfunction* function, const std::string& name,
1528                    Location location)
1529 {
1530   tree decl;
1531   if (name.empty())
1532     decl = create_artificial_label(location.gcc_location());
1533   else
1534     {
1535       tree id = get_identifier_from_string(name);
1536       decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1537                         void_type_node);
1538       DECL_CONTEXT(decl) = function->get_tree();
1539     }
1540   return new Blabel(decl);
1541 }
1542
1543 // Make a statement which defines a label.
1544
1545 Bstatement*
1546 Gcc_backend::label_definition_statement(Blabel* label)
1547 {
1548   tree lab = label->get_tree();
1549   tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1550                              void_type_node, lab);
1551   return this->make_statement(ret);
1552 }
1553
1554 // Make a goto statement.
1555
1556 Bstatement*
1557 Gcc_backend::goto_statement(Blabel* label, Location location)
1558 {
1559   tree lab = label->get_tree();
1560   tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1561                              lab);
1562   return this->make_statement(ret);
1563 }
1564
1565 // Get the address of a label.
1566
1567 Bexpression*
1568 Gcc_backend::label_address(Blabel* label, Location location)
1569 {
1570   tree lab = label->get_tree();
1571   TREE_USED(lab) = 1;
1572   TREE_ADDRESSABLE(lab) = 1;
1573   tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1574                               build_fold_addr_expr_loc(location.gcc_location(),
1575                                                        lab));
1576   return this->make_expression(ret);
1577 }
1578
1579 // The single backend.
1580
1581 static Gcc_backend gcc_backend;
1582
1583 // Return the backend generator.
1584
1585 Backend*
1586 go_get_backend()
1587 {
1588   return &gcc_backend;
1589 }
1590
1591 // FIXME: Temporary functions while converting to the new backend
1592 // interface.
1593
1594 Btype*
1595 tree_to_type(tree t)
1596 {
1597   return new Btype(t);
1598 }
1599
1600 Bexpression*
1601 tree_to_expr(tree t)
1602 {
1603   return new Bexpression(t);
1604 }
1605
1606 Bstatement*
1607 tree_to_stat(tree t)
1608 {
1609   return new Bstatement(t);
1610 }
1611
1612 Bfunction*
1613 tree_to_function(tree t)
1614 {
1615   return new Bfunction(t);
1616 }
1617
1618 Bblock*
1619 tree_to_block(tree t)
1620 {
1621   gcc_assert(TREE_CODE(t) == BIND_EXPR);
1622   return new Bblock(t);
1623 }
1624
1625 tree
1626 type_to_tree(Btype* bt)
1627 {
1628   return bt->get_tree();
1629 }
1630
1631 tree
1632 expr_to_tree(Bexpression* be)
1633 {
1634   return be->get_tree();
1635 }
1636
1637 tree
1638 stat_to_tree(Bstatement* bs)
1639 {
1640   return bs->get_tree();
1641 }
1642
1643 tree
1644 block_to_tree(Bblock* bb)
1645 {
1646   return bb->get_tree();
1647 }
1648
1649 tree
1650 var_to_tree(Bvariable* bv)
1651 {
1652   return bv->get_tree();
1653 }