OSDN Git Service

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