OSDN Git Service

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