2013-11-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / go / go-gcc.cc
blob83f91d5daf5479b5f07cf2245d4d5998a13e09c6
1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
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.
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.
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/>.
21 #include "go-system.h"
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>
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
30 #include "varasm.h"
31 #include "tree-iterator.h"
32 #include "gimple.h"
33 #include "toplev.h"
34 #include "output.h"
35 #include "real.h"
36 #include "realmpfr.h"
38 #include "go-c.h"
40 #include "gogo.h"
41 #include "backend.h"
43 // A class wrapping a tree.
45 class Gcc_tree
47 public:
48 Gcc_tree(tree t)
49 : t_(t)
50 { }
52 tree
53 get_tree() const
54 { return this->t_; }
56 void
57 set_tree(tree t)
58 { this->t_ = t; }
60 private:
61 tree t_;
64 // In gcc, types, expressions, and statements are all trees.
65 class Btype : public Gcc_tree
67 public:
68 Btype(tree t)
69 : Gcc_tree(t)
70 { }
73 class Bexpression : public Gcc_tree
75 public:
76 Bexpression(tree t)
77 : Gcc_tree(t)
78 { }
81 class Bstatement : public Gcc_tree
83 public:
84 Bstatement(tree t)
85 : Gcc_tree(t)
86 { }
89 class Bfunction : public Gcc_tree
91 public:
92 Bfunction(tree t)
93 : Gcc_tree(t)
94 { }
97 class Bblock : public Gcc_tree
99 public:
100 Bblock(tree t)
101 : Gcc_tree(t)
105 class Bvariable : public Gcc_tree
107 public:
108 Bvariable(tree t)
109 : Gcc_tree(t)
113 class Blabel : public Gcc_tree
115 public:
116 Blabel(tree t)
117 : Gcc_tree(t)
121 // This file implements the interface between the Go frontend proper
122 // and the gcc IR. This implements specific instantiations of
123 // abstract classes defined by the Go frontend proper. The Go
124 // frontend proper class methods of these classes to generate the
125 // backend representation.
127 class Gcc_backend : public Backend
129 public:
130 // Types.
132 Btype*
133 error_type()
134 { return this->make_type(error_mark_node); }
136 Btype*
137 void_type()
138 { return this->make_type(void_type_node); }
140 Btype*
141 bool_type()
142 { return this->make_type(boolean_type_node); }
144 Btype*
145 integer_type(bool, int);
147 Btype*
148 float_type(int);
150 Btype*
151 complex_type(int);
153 Btype*
154 pointer_type(Btype*);
156 Btype*
157 function_type(const Btyped_identifier&,
158 const std::vector<Btyped_identifier>&,
159 const std::vector<Btyped_identifier>&,
160 const Location);
162 Btype*
163 struct_type(const std::vector<Btyped_identifier>&);
165 Btype*
166 array_type(Btype*, Bexpression*);
168 Btype*
169 placeholder_pointer_type(const std::string&, Location, bool);
171 bool
172 set_placeholder_pointer_type(Btype*, Btype*);
174 bool
175 set_placeholder_function_type(Btype*, Btype*);
177 Btype*
178 placeholder_struct_type(const std::string&, Location);
180 bool
181 set_placeholder_struct_type(Btype* placeholder,
182 const std::vector<Btyped_identifier>&);
184 Btype*
185 placeholder_array_type(const std::string&, Location);
187 bool
188 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
190 Btype*
191 named_type(const std::string&, Btype*, Location);
193 Btype*
194 circular_pointer_type(Btype*, bool);
196 bool
197 is_circular_pointer_type(Btype*);
199 size_t
200 type_size(Btype*);
202 size_t
203 type_alignment(Btype*);
205 size_t
206 type_field_alignment(Btype*);
208 size_t
209 type_field_offset(Btype*, size_t index);
211 // Expressions.
213 Bexpression*
214 zero_expression(Btype*);
216 Bexpression*
217 error_expression()
218 { return this->make_expression(error_mark_node); }
220 Bexpression*
221 var_expression(Bvariable* var, Location);
223 Bexpression*
224 indirect_expression(Bexpression* expr, bool known_valid, Location);
226 Bexpression*
227 integer_constant_expression(Btype* btype, mpz_t val);
229 Bexpression*
230 float_constant_expression(Btype* btype, mpfr_t val);
232 Bexpression*
233 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag);
235 Bexpression*
236 convert_expression(Btype* type, Bexpression* expr, Location);
238 Bexpression*
239 function_code_expression(Bfunction*, Location);
241 Bexpression*
242 address_expression(Bexpression*, Location);
244 // Statements.
246 Bstatement*
247 error_statement()
248 { return this->make_statement(error_mark_node); }
250 Bstatement*
251 expression_statement(Bexpression*);
253 Bstatement*
254 init_statement(Bvariable* var, Bexpression* init);
256 Bstatement*
257 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
259 Bstatement*
260 return_statement(Bfunction*, const std::vector<Bexpression*>&,
261 Location);
263 Bstatement*
264 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
265 Location);
267 Bstatement*
268 switch_statement(Bexpression* value,
269 const std::vector<std::vector<Bexpression*> >& cases,
270 const std::vector<Bstatement*>& statements,
271 Location);
273 Bstatement*
274 compound_statement(Bstatement*, Bstatement*);
276 Bstatement*
277 statement_list(const std::vector<Bstatement*>&);
279 // Blocks.
281 Bblock*
282 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
283 Location, Location);
285 void
286 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
288 Bstatement*
289 block_statement(Bblock*);
291 // Variables.
293 Bvariable*
294 error_variable()
295 { return new Bvariable(error_mark_node); }
297 Bvariable*
298 global_variable(const std::string& package_name,
299 const std::string& pkgpath,
300 const std::string& name,
301 Btype* btype,
302 bool is_external,
303 bool is_hidden,
304 bool in_unique_section,
305 Location location);
307 void
308 global_variable_set_init(Bvariable*, Bexpression*);
310 Bvariable*
311 local_variable(Bfunction*, const std::string&, Btype*, bool,
312 Location);
314 Bvariable*
315 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
316 Location);
318 Bvariable*
319 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
320 Location, Bstatement**);
322 Bvariable*
323 immutable_struct(const std::string&, bool, bool, Btype*, Location);
325 void
326 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
327 Location, Bexpression*);
329 Bvariable*
330 immutable_struct_reference(const std::string&, Btype*, Location);
332 // Labels.
334 Blabel*
335 label(Bfunction*, const std::string& name, Location);
337 Bstatement*
338 label_definition_statement(Blabel*);
340 Bstatement*
341 goto_statement(Blabel*, Location);
343 Bexpression*
344 label_address(Blabel*, Location);
346 // Functions.
348 Bfunction*
349 error_function()
350 { return this->make_function(error_mark_node); }
352 Bfunction*
353 function(Btype* fntype, const std::string& name, const std::string& asm_name,
354 bool is_visible, bool is_declaration, bool is_inlinable,
355 bool disable_split_stack, bool in_unique_section, Location);
357 private:
358 // Make a Bexpression from a tree.
359 Bexpression*
360 make_expression(tree t)
361 { return new Bexpression(t); }
363 // Make a Bstatement from a tree.
364 Bstatement*
365 make_statement(tree t)
366 { return new Bstatement(t); }
368 // Make a Btype from a tree.
369 Btype*
370 make_type(tree t)
371 { return new Btype(t); }
373 Bfunction*
374 make_function(tree t)
375 { return new Bfunction(t); }
377 Btype*
378 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
380 Btype*
381 fill_in_array(Btype*, Btype*, Bexpression*);
383 tree
384 non_zero_size_type(tree);
387 // A helper function.
389 static inline tree
390 get_identifier_from_string(const std::string& str)
392 return get_identifier_with_length(str.data(), str.length());
395 // Get an unnamed integer type.
397 Btype*
398 Gcc_backend::integer_type(bool is_unsigned, int bits)
400 tree type;
401 if (is_unsigned)
403 if (bits == INT_TYPE_SIZE)
404 type = unsigned_type_node;
405 else if (bits == CHAR_TYPE_SIZE)
406 type = unsigned_char_type_node;
407 else if (bits == SHORT_TYPE_SIZE)
408 type = short_unsigned_type_node;
409 else if (bits == LONG_TYPE_SIZE)
410 type = long_unsigned_type_node;
411 else if (bits == LONG_LONG_TYPE_SIZE)
412 type = long_long_unsigned_type_node;
413 else
414 type = make_unsigned_type(bits);
416 else
418 if (bits == INT_TYPE_SIZE)
419 type = integer_type_node;
420 else if (bits == CHAR_TYPE_SIZE)
421 type = signed_char_type_node;
422 else if (bits == SHORT_TYPE_SIZE)
423 type = short_integer_type_node;
424 else if (bits == LONG_TYPE_SIZE)
425 type = long_integer_type_node;
426 else if (bits == LONG_LONG_TYPE_SIZE)
427 type = long_long_integer_type_node;
428 else
429 type = make_signed_type(bits);
431 return this->make_type(type);
434 // Get an unnamed float type.
436 Btype*
437 Gcc_backend::float_type(int bits)
439 tree type;
440 if (bits == FLOAT_TYPE_SIZE)
441 type = float_type_node;
442 else if (bits == DOUBLE_TYPE_SIZE)
443 type = double_type_node;
444 else if (bits == LONG_DOUBLE_TYPE_SIZE)
445 type = long_double_type_node;
446 else
448 type = make_node(REAL_TYPE);
449 TYPE_PRECISION(type) = bits;
450 layout_type(type);
452 return this->make_type(type);
455 // Get an unnamed complex type.
457 Btype*
458 Gcc_backend::complex_type(int bits)
460 tree type;
461 if (bits == FLOAT_TYPE_SIZE * 2)
462 type = complex_float_type_node;
463 else if (bits == DOUBLE_TYPE_SIZE * 2)
464 type = complex_double_type_node;
465 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
466 type = complex_long_double_type_node;
467 else
469 type = make_node(REAL_TYPE);
470 TYPE_PRECISION(type) = bits / 2;
471 layout_type(type);
472 type = build_complex_type(type);
474 return this->make_type(type);
477 // Get a pointer type.
479 Btype*
480 Gcc_backend::pointer_type(Btype* to_type)
482 tree to_type_tree = to_type->get_tree();
483 if (to_type_tree == error_mark_node)
484 return this->error_type();
485 tree type = build_pointer_type(to_type_tree);
486 return this->make_type(type);
489 // Make a function type.
491 Btype*
492 Gcc_backend::function_type(const Btyped_identifier& receiver,
493 const std::vector<Btyped_identifier>& parameters,
494 const std::vector<Btyped_identifier>& results,
495 Location location)
497 tree args = NULL_TREE;
498 tree* pp = &args;
499 if (receiver.btype != NULL)
501 tree t = receiver.btype->get_tree();
502 if (t == error_mark_node)
503 return this->error_type();
504 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
505 pp = &TREE_CHAIN(*pp);
508 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
509 p != parameters.end();
510 ++p)
512 tree t = p->btype->get_tree();
513 if (t == error_mark_node)
514 return this->error_type();
515 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
516 pp = &TREE_CHAIN(*pp);
519 // Varargs is handled entirely at the Go level. When converted to
520 // GENERIC functions are not varargs.
521 *pp = void_list_node;
523 tree result;
524 if (results.empty())
525 result = void_type_node;
526 else if (results.size() == 1)
527 result = results.front().btype->get_tree();
528 else
530 result = make_node(RECORD_TYPE);
531 tree field_trees = NULL_TREE;
532 pp = &field_trees;
533 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
534 p != results.end();
535 ++p)
537 const std::string name = (p->name.empty()
538 ? "UNNAMED"
539 : p->name);
540 tree name_tree = get_identifier_from_string(name);
541 tree field_type_tree = p->btype->get_tree();
542 if (field_type_tree == error_mark_node)
543 return this->error_type();
544 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
545 tree field = build_decl(location.gcc_location(), FIELD_DECL,
546 name_tree, field_type_tree);
547 DECL_CONTEXT(field) = result;
548 *pp = field;
549 pp = &DECL_CHAIN(field);
551 TYPE_FIELDS(result) = field_trees;
552 layout_type(result);
554 if (result == error_mark_node)
555 return this->error_type();
557 tree fntype = build_function_type(result, args);
558 if (fntype == error_mark_node)
559 return this->error_type();
561 return this->make_type(build_pointer_type(fntype));
564 // Make a struct type.
566 Btype*
567 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
569 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
572 // Fill in the fields of a struct type.
574 Btype*
575 Gcc_backend::fill_in_struct(Btype* fill,
576 const std::vector<Btyped_identifier>& fields)
578 tree fill_tree = fill->get_tree();
579 tree field_trees = NULL_TREE;
580 tree* pp = &field_trees;
581 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
582 p != fields.end();
583 ++p)
585 tree name_tree = get_identifier_from_string(p->name);
586 tree type_tree = p->btype->get_tree();
587 if (type_tree == error_mark_node)
588 return this->error_type();
589 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
590 type_tree);
591 DECL_CONTEXT(field) = fill_tree;
592 *pp = field;
593 pp = &DECL_CHAIN(field);
595 TYPE_FIELDS(fill_tree) = field_trees;
596 layout_type(fill_tree);
597 return fill;
600 // Make an array type.
602 Btype*
603 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
605 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
606 element_btype, length);
609 // Fill in an array type.
611 Btype*
612 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
613 Bexpression* length)
615 tree element_type_tree = element_type->get_tree();
616 tree length_tree = length->get_tree();
617 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
618 return this->error_type();
620 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
622 length_tree = fold_convert(sizetype, length_tree);
624 // build_index_type takes the maximum index, which is one less than
625 // the length.
626 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
627 length_tree,
628 size_one_node));
630 tree fill_tree = fill->get_tree();
631 TREE_TYPE(fill_tree) = element_type_tree;
632 TYPE_DOMAIN(fill_tree) = index_type_tree;
633 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
634 layout_type(fill_tree);
636 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
637 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
638 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
639 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
640 TYPE_CANONICAL(fill_tree) =
641 build_array_type(TYPE_CANONICAL(element_type_tree),
642 TYPE_CANONICAL(index_type_tree));
644 return fill;
647 // Create a placeholder for a pointer type.
649 Btype*
650 Gcc_backend::placeholder_pointer_type(const std::string& name,
651 Location location, bool)
653 tree ret = build_distinct_type_copy(ptr_type_node);
654 if (!name.empty())
656 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
657 get_identifier_from_string(name),
658 ret);
659 TYPE_NAME(ret) = decl;
661 return this->make_type(ret);
664 // Set the real target type for a placeholder pointer type.
666 bool
667 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
668 Btype* to_type)
670 tree pt = placeholder->get_tree();
671 if (pt == error_mark_node)
672 return false;
673 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
674 tree tt = to_type->get_tree();
675 if (tt == error_mark_node)
677 placeholder->set_tree(error_mark_node);
678 return false;
680 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
681 TREE_TYPE(pt) = TREE_TYPE(tt);
682 if (TYPE_NAME(pt) != NULL_TREE)
684 // Build the data structure gcc wants to see for a typedef.
685 tree copy = build_variant_type_copy(pt);
686 TYPE_NAME(copy) = NULL_TREE;
687 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
689 return true;
692 // Set the real values for a placeholder function type.
694 bool
695 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
697 return this->set_placeholder_pointer_type(placeholder, ft);
700 // Create a placeholder for a struct type.
702 Btype*
703 Gcc_backend::placeholder_struct_type(const std::string& name,
704 Location location)
706 tree ret = make_node(RECORD_TYPE);
707 if (!name.empty())
709 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
710 get_identifier_from_string(name),
711 ret);
712 TYPE_NAME(ret) = decl;
714 return this->make_type(ret);
717 // Fill in the fields of a placeholder struct type.
719 bool
720 Gcc_backend::set_placeholder_struct_type(
721 Btype* placeholder,
722 const std::vector<Btyped_identifier>& fields)
724 tree t = placeholder->get_tree();
725 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
726 Btype* r = this->fill_in_struct(placeholder, fields);
728 if (TYPE_NAME(t) != NULL_TREE)
730 // Build the data structure gcc wants to see for a typedef.
731 tree copy = build_distinct_type_copy(t);
732 TYPE_NAME(copy) = NULL_TREE;
733 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
736 return r->get_tree() != error_mark_node;
739 // Create a placeholder for an array type.
741 Btype*
742 Gcc_backend::placeholder_array_type(const std::string& name,
743 Location location)
745 tree ret = make_node(ARRAY_TYPE);
746 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
747 get_identifier_from_string(name),
748 ret);
749 TYPE_NAME(ret) = decl;
750 return this->make_type(ret);
753 // Fill in the fields of a placeholder array type.
755 bool
756 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
757 Btype* element_btype,
758 Bexpression* length)
760 tree t = placeholder->get_tree();
761 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
762 Btype* r = this->fill_in_array(placeholder, element_btype, length);
764 // Build the data structure gcc wants to see for a typedef.
765 tree copy = build_distinct_type_copy(t);
766 TYPE_NAME(copy) = NULL_TREE;
767 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
769 return r->get_tree() != error_mark_node;
772 // Return a named version of a type.
774 Btype*
775 Gcc_backend::named_type(const std::string& name, Btype* btype,
776 Location location)
778 tree type = btype->get_tree();
779 if (type == error_mark_node)
780 return this->error_type();
782 // The middle-end expects a basic type to have a name. In Go every
783 // basic type will have a name. The first time we see a basic type,
784 // give it whatever Go name we have at this point.
785 if (TYPE_NAME(type) == NULL_TREE
786 && location.gcc_location() == BUILTINS_LOCATION
787 && (TREE_CODE(type) == INTEGER_TYPE
788 || TREE_CODE(type) == REAL_TYPE
789 || TREE_CODE(type) == COMPLEX_TYPE
790 || TREE_CODE(type) == BOOLEAN_TYPE))
792 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
793 get_identifier_from_string(name),
794 type);
795 TYPE_NAME(type) = decl;
796 return this->make_type(type);
799 tree copy = build_variant_type_copy(type);
800 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
801 get_identifier_from_string(name),
802 copy);
803 DECL_ORIGINAL_TYPE(decl) = type;
804 TYPE_NAME(copy) = decl;
805 return this->make_type(copy);
808 // Return a pointer type used as a marker for a circular type.
810 Btype*
811 Gcc_backend::circular_pointer_type(Btype*, bool)
813 return this->make_type(ptr_type_node);
816 // Return whether we might be looking at a circular type.
818 bool
819 Gcc_backend::is_circular_pointer_type(Btype* btype)
821 return btype->get_tree() == ptr_type_node;
824 // Return the size of a type.
826 size_t
827 Gcc_backend::type_size(Btype* btype)
829 tree t = btype->get_tree();
830 if (t == error_mark_node)
831 return 1;
832 t = TYPE_SIZE_UNIT(t);
833 gcc_assert(TREE_CODE(t) == INTEGER_CST);
834 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
835 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
836 size_t ret = static_cast<size_t>(val_wide);
837 gcc_assert(ret == val_wide);
838 return ret;
841 // Return the alignment of a type.
843 size_t
844 Gcc_backend::type_alignment(Btype* btype)
846 tree t = btype->get_tree();
847 if (t == error_mark_node)
848 return 1;
849 return TYPE_ALIGN_UNIT(t);
852 // Return the alignment of a struct field of type BTYPE.
854 size_t
855 Gcc_backend::type_field_alignment(Btype* btype)
857 tree t = btype->get_tree();
858 if (t == error_mark_node)
859 return 1;
860 return go_field_alignment(t);
863 // Return the offset of a field in a struct.
865 size_t
866 Gcc_backend::type_field_offset(Btype* btype, size_t index)
868 tree struct_tree = btype->get_tree();
869 if (struct_tree == error_mark_node)
870 return 0;
871 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
872 tree field = TYPE_FIELDS(struct_tree);
873 for (; index > 0; --index)
875 field = DECL_CHAIN(field);
876 gcc_assert(field != NULL_TREE);
878 HOST_WIDE_INT offset_wide = int_byte_position(field);
879 gcc_assert(offset_wide >= 0);
880 size_t ret = static_cast<size_t>(offset_wide);
881 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
882 return ret;
885 // Return the zero value for a type.
887 Bexpression*
888 Gcc_backend::zero_expression(Btype* btype)
890 tree t = btype->get_tree();
891 tree ret;
892 if (t == error_mark_node)
893 ret = error_mark_node;
894 else
895 ret = build_zero_cst(t);
896 return tree_to_expr(ret);
899 // An expression that references a variable.
901 Bexpression*
902 Gcc_backend::var_expression(Bvariable* var, Location)
904 tree ret = var->get_tree();
905 if (ret == error_mark_node)
906 return this->error_expression();
907 return tree_to_expr(ret);
910 // An expression that indirectly references an expression.
912 Bexpression*
913 Gcc_backend::indirect_expression(Bexpression* expr, bool known_valid,
914 Location location)
916 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
917 expr->get_tree());
918 if (known_valid)
919 TREE_THIS_NOTRAP(ret) = 1;
920 return tree_to_expr(ret);
923 // Return a typed value as a constant integer.
925 Bexpression*
926 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
928 tree t = btype->get_tree();
929 if (t == error_mark_node)
930 return this->error_expression();
932 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
933 return tree_to_expr(ret);
936 // Return a typed value as a constant floating-point number.
938 Bexpression*
939 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
941 tree t = btype->get_tree();
942 tree ret;
943 if (t == error_mark_node)
944 return this->error_expression();
946 REAL_VALUE_TYPE r1;
947 real_from_mpfr(&r1, val, t, GMP_RNDN);
948 REAL_VALUE_TYPE r2;
949 real_convert(&r2, TYPE_MODE(t), &r1);
950 ret = build_real(t, r2);
951 return tree_to_expr(ret);
954 // Return a typed real and imaginary value as a constant complex number.
956 Bexpression*
957 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
959 tree t = btype->get_tree();
960 tree ret;
961 if (t == error_mark_node)
962 return this->error_expression();
964 REAL_VALUE_TYPE r1;
965 real_from_mpfr(&r1, real, TREE_TYPE(t), GMP_RNDN);
966 REAL_VALUE_TYPE r2;
967 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
969 REAL_VALUE_TYPE r3;
970 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
971 REAL_VALUE_TYPE r4;
972 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
974 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
975 build_real(TREE_TYPE(t), r4));
976 return tree_to_expr(ret);
979 // An expression that converts an expression to a different type.
981 Bexpression*
982 Gcc_backend::convert_expression(Btype* type, Bexpression* expr, Location)
984 tree type_tree = type->get_tree();
985 tree expr_tree = expr->get_tree();
986 if (type_tree == error_mark_node || expr_tree == error_mark_node)
987 return this->error_expression();
989 tree ret = fold_convert(type_tree, expr_tree);
990 return tree_to_expr(ret);
993 // Get the address of a function.
995 Bexpression*
996 Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
998 tree func = bfunc->get_tree();
999 if (func == error_mark_node)
1000 return this->error_expression();
1002 tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
1003 return this->make_expression(ret);
1006 // Get the address of an expression.
1008 Bexpression*
1009 Gcc_backend::address_expression(Bexpression* bexpr, Location location)
1011 tree expr = bexpr->get_tree();
1012 if (expr == error_mark_node)
1013 return this->error_expression();
1015 tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
1016 return this->make_expression(ret);
1019 // An expression as a statement.
1021 Bstatement*
1022 Gcc_backend::expression_statement(Bexpression* expr)
1024 return this->make_statement(expr->get_tree());
1027 // Variable initialization.
1029 Bstatement*
1030 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
1032 tree var_tree = var->get_tree();
1033 tree init_tree = init->get_tree();
1034 if (var_tree == error_mark_node || init_tree == error_mark_node)
1035 return this->error_statement();
1036 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
1038 // To avoid problems with GNU ld, we don't make zero-sized
1039 // externally visible variables. That might lead us to doing an
1040 // initialization of a zero-sized expression to a non-zero sized
1041 // variable, or vice-versa. Avoid crashes by omitting the
1042 // initializer. Such initializations don't mean anything anyhow.
1043 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
1044 && init_tree != NULL_TREE
1045 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
1047 DECL_INITIAL(var_tree) = init_tree;
1048 init_tree = NULL_TREE;
1051 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1052 void_type_node, var_tree);
1053 if (init_tree != NULL_TREE)
1054 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1055 void_type_node, init_tree, ret);
1057 return this->make_statement(ret);
1060 // Assignment.
1062 Bstatement*
1063 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1064 Location location)
1066 tree lhs_tree = lhs->get_tree();
1067 tree rhs_tree = rhs->get_tree();
1068 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1069 return this->error_statement();
1071 // To avoid problems with GNU ld, we don't make zero-sized
1072 // externally visible variables. That might lead us to doing an
1073 // assignment of a zero-sized expression to a non-zero sized
1074 // expression; avoid crashes here by avoiding assignments of
1075 // zero-sized expressions. Such assignments don't really mean
1076 // anything anyhow.
1077 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1078 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1079 return this->compound_statement(this->expression_statement(lhs),
1080 this->expression_statement(rhs));
1082 // Sometimes the same unnamed Go type can be created multiple times
1083 // and thus have multiple tree representations. Make sure this does
1084 // not confuse the middle-end.
1085 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1087 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1088 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1089 if (POINTER_TYPE_P(lhs_type_tree)
1090 || INTEGRAL_TYPE_P(lhs_type_tree)
1091 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1092 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1093 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1094 rhs_tree);
1095 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1096 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1098 gcc_assert(int_size_in_bytes(lhs_type_tree)
1099 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1100 rhs_tree = fold_build1_loc(location.gcc_location(),
1101 VIEW_CONVERT_EXPR,
1102 lhs_type_tree, rhs_tree);
1106 return this->make_statement(fold_build2_loc(location.gcc_location(),
1107 MODIFY_EXPR,
1108 void_type_node,
1109 lhs_tree, rhs_tree));
1112 // Return.
1114 Bstatement*
1115 Gcc_backend::return_statement(Bfunction* bfunction,
1116 const std::vector<Bexpression*>& vals,
1117 Location location)
1119 tree fntree = bfunction->get_tree();
1120 if (fntree == error_mark_node)
1121 return this->error_statement();
1122 tree result = DECL_RESULT(fntree);
1123 if (result == error_mark_node)
1124 return this->error_statement();
1125 tree ret;
1126 if (vals.empty())
1127 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1128 NULL_TREE);
1129 else if (vals.size() == 1)
1131 tree val = vals.front()->get_tree();
1132 if (val == error_mark_node)
1133 return this->error_statement();
1134 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1135 void_type_node, result,
1136 vals.front()->get_tree());
1137 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1138 void_type_node, set);
1140 else
1142 // To return multiple values, copy the values into a temporary
1143 // variable of the right structure type, and then assign the
1144 // temporary variable to the DECL_RESULT in the return
1145 // statement.
1146 tree stmt_list = NULL_TREE;
1147 tree rettype = TREE_TYPE(result);
1148 tree rettmp = create_tmp_var(rettype, "RESULT");
1149 tree field = TYPE_FIELDS(rettype);
1150 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1151 p != vals.end();
1152 p++, field = DECL_CHAIN(field))
1154 gcc_assert(field != NULL_TREE);
1155 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1156 TREE_TYPE(field), rettmp, field,
1157 NULL_TREE);
1158 tree val = (*p)->get_tree();
1159 if (val == error_mark_node)
1160 return this->error_statement();
1161 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1162 void_type_node,
1163 ref, (*p)->get_tree());
1164 append_to_statement_list(set, &stmt_list);
1166 gcc_assert(field == NULL_TREE);
1167 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1168 void_type_node,
1169 result, rettmp);
1170 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1171 void_type_node, set);
1172 append_to_statement_list(ret_expr, &stmt_list);
1173 ret = stmt_list;
1175 return this->make_statement(ret);
1178 // If.
1180 Bstatement*
1181 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1182 Bblock* else_block, Location location)
1184 tree cond_tree = condition->get_tree();
1185 tree then_tree = then_block->get_tree();
1186 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1187 if (cond_tree == error_mark_node
1188 || then_tree == error_mark_node
1189 || else_tree == error_mark_node)
1190 return this->error_statement();
1191 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1192 cond_tree, then_tree, else_tree);
1193 return this->make_statement(ret);
1196 // Switch.
1198 Bstatement*
1199 Gcc_backend::switch_statement(
1200 Bexpression* value,
1201 const std::vector<std::vector<Bexpression*> >& cases,
1202 const std::vector<Bstatement*>& statements,
1203 Location switch_location)
1205 gcc_assert(cases.size() == statements.size());
1207 tree stmt_list = NULL_TREE;
1208 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1209 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1210 ps != statements.end();
1211 ++ps, ++pc)
1213 if (pc->empty())
1215 source_location loc = (*ps != NULL
1216 ? EXPR_LOCATION((*ps)->get_tree())
1217 : UNKNOWN_LOCATION);
1218 tree label = create_artificial_label(loc);
1219 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1220 append_to_statement_list(c, &stmt_list);
1222 else
1224 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1225 pcv != pc->end();
1226 ++pcv)
1228 tree t = (*pcv)->get_tree();
1229 if (t == error_mark_node)
1230 return this->error_statement();
1231 source_location loc = EXPR_LOCATION(t);
1232 tree label = create_artificial_label(loc);
1233 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1234 append_to_statement_list(c, &stmt_list);
1238 if (*ps != NULL)
1240 tree t = (*ps)->get_tree();
1241 if (t == error_mark_node)
1242 return this->error_statement();
1243 append_to_statement_list(t, &stmt_list);
1247 tree tv = value->get_tree();
1248 if (tv == error_mark_node)
1249 return this->error_statement();
1250 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1251 NULL_TREE, tv, stmt_list, NULL_TREE);
1252 return this->make_statement(t);
1255 // Pair of statements.
1257 Bstatement*
1258 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1260 tree stmt_list = NULL_TREE;
1261 tree t = s1->get_tree();
1262 if (t == error_mark_node)
1263 return this->error_statement();
1264 append_to_statement_list(t, &stmt_list);
1265 t = s2->get_tree();
1266 if (t == error_mark_node)
1267 return this->error_statement();
1268 append_to_statement_list(t, &stmt_list);
1269 return this->make_statement(stmt_list);
1272 // List of statements.
1274 Bstatement*
1275 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1277 tree stmt_list = NULL_TREE;
1278 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1279 p != statements.end();
1280 ++p)
1282 tree t = (*p)->get_tree();
1283 if (t == error_mark_node)
1284 return this->error_statement();
1285 append_to_statement_list(t, &stmt_list);
1287 return this->make_statement(stmt_list);
1290 // Make a block. For some reason gcc uses a dual structure for
1291 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1292 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1293 // the Bblock.
1295 Bblock*
1296 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1297 const std::vector<Bvariable*>& vars,
1298 Location start_location,
1299 Location)
1301 tree block_tree = make_node(BLOCK);
1302 if (enclosing == NULL)
1304 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1305 // until we have a proper representation of the init function.
1306 tree fndecl;
1307 if (function == NULL)
1308 fndecl = current_function_decl;
1309 else
1310 fndecl = function->get_tree();
1311 gcc_assert(fndecl != NULL_TREE);
1313 // We may have already created a block for local variables when
1314 // we take the address of a parameter.
1315 if (DECL_INITIAL(fndecl) == NULL_TREE)
1317 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1318 DECL_INITIAL(fndecl) = block_tree;
1320 else
1322 tree superblock_tree = DECL_INITIAL(fndecl);
1323 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1324 tree* pp;
1325 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1326 *pp != NULL_TREE;
1327 pp = &BLOCK_CHAIN(*pp))
1329 *pp = block_tree;
1332 else
1334 tree superbind_tree = enclosing->get_tree();
1335 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1336 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1338 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1339 tree* pp;
1340 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1341 *pp != NULL_TREE;
1342 pp = &BLOCK_CHAIN(*pp))
1344 *pp = block_tree;
1347 tree* pp = &BLOCK_VARS(block_tree);
1348 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1349 pv != vars.end();
1350 ++pv)
1352 *pp = (*pv)->get_tree();
1353 if (*pp != error_mark_node)
1354 pp = &DECL_CHAIN(*pp);
1356 *pp = NULL_TREE;
1358 TREE_USED(block_tree) = 1;
1360 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1361 void_type_node, BLOCK_VARS(block_tree),
1362 NULL_TREE, block_tree);
1363 TREE_SIDE_EFFECTS(bind_tree) = 1;
1365 return new Bblock(bind_tree);
1368 // Add statements to a block.
1370 void
1371 Gcc_backend::block_add_statements(Bblock* bblock,
1372 const std::vector<Bstatement*>& statements)
1374 tree stmt_list = NULL_TREE;
1375 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1376 p != statements.end();
1377 ++p)
1379 tree s = (*p)->get_tree();
1380 if (s != error_mark_node)
1381 append_to_statement_list(s, &stmt_list);
1384 tree bind_tree = bblock->get_tree();
1385 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1386 BIND_EXPR_BODY(bind_tree) = stmt_list;
1389 // Return a block as a statement.
1391 Bstatement*
1392 Gcc_backend::block_statement(Bblock* bblock)
1394 tree bind_tree = bblock->get_tree();
1395 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1396 return this->make_statement(bind_tree);
1399 // This is not static because we declare it with GTY(()) in go-c.h.
1400 tree go_non_zero_struct;
1402 // Return a type corresponding to TYPE with non-zero size.
1404 tree
1405 Gcc_backend::non_zero_size_type(tree type)
1407 if (int_size_in_bytes(type) != 0)
1408 return type;
1410 switch (TREE_CODE(type))
1412 case RECORD_TYPE:
1413 if (TYPE_FIELDS(type) != NULL_TREE)
1415 tree ns = make_node(RECORD_TYPE);
1416 tree field_trees = NULL_TREE;
1417 tree *pp = &field_trees;
1418 for (tree field = TYPE_FIELDS(type);
1419 field != NULL_TREE;
1420 field = DECL_CHAIN(field))
1422 tree ft = TREE_TYPE(field);
1423 if (field == TYPE_FIELDS(type))
1424 ft = non_zero_size_type(ft);
1425 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
1426 DECL_NAME(field), ft);
1427 DECL_CONTEXT(f) = ns;
1428 *pp = f;
1429 pp = &DECL_CHAIN(f);
1431 TYPE_FIELDS(ns) = field_trees;
1432 layout_type(ns);
1433 return ns;
1436 if (go_non_zero_struct == NULL_TREE)
1438 type = make_node(RECORD_TYPE);
1439 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1440 get_identifier("dummy"),
1441 boolean_type_node);
1442 DECL_CONTEXT(field) = type;
1443 TYPE_FIELDS(type) = field;
1444 layout_type(type);
1445 go_non_zero_struct = type;
1447 return go_non_zero_struct;
1449 case ARRAY_TYPE:
1451 tree element_type = non_zero_size_type(TREE_TYPE(type));
1452 return build_array_type_nelts(element_type, 1);
1455 default:
1456 gcc_unreachable();
1459 gcc_unreachable();
1462 // Make a global variable.
1464 Bvariable*
1465 Gcc_backend::global_variable(const std::string& package_name,
1466 const std::string& pkgpath,
1467 const std::string& name,
1468 Btype* btype,
1469 bool is_external,
1470 bool is_hidden,
1471 bool in_unique_section,
1472 Location location)
1474 tree type_tree = btype->get_tree();
1475 if (type_tree == error_mark_node)
1476 return this->error_variable();
1478 // The GNU linker does not like dynamic variables with zero size.
1479 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1480 type_tree = this->non_zero_size_type(type_tree);
1482 std::string var_name(package_name);
1483 var_name.push_back('.');
1484 var_name.append(name);
1485 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1486 get_identifier_from_string(var_name),
1487 type_tree);
1488 if (is_external)
1489 DECL_EXTERNAL(decl) = 1;
1490 else
1491 TREE_STATIC(decl) = 1;
1492 if (!is_hidden)
1494 TREE_PUBLIC(decl) = 1;
1496 std::string asm_name(pkgpath);
1497 asm_name.push_back('.');
1498 asm_name.append(name);
1499 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1501 TREE_USED(decl) = 1;
1503 if (in_unique_section)
1504 resolve_unique_section (decl, 0, 1);
1506 go_preserve_from_gc(decl);
1508 return new Bvariable(decl);
1511 // Set the initial value of a global variable.
1513 void
1514 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1516 tree expr_tree = expr->get_tree();
1517 if (expr_tree == error_mark_node)
1518 return;
1519 gcc_assert(TREE_CONSTANT(expr_tree));
1520 tree var_decl = var->get_tree();
1521 if (var_decl == error_mark_node)
1522 return;
1523 DECL_INITIAL(var_decl) = expr_tree;
1525 // If this variable goes in a unique section, it may need to go into
1526 // a different one now that DECL_INITIAL is set.
1527 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl))
1529 DECL_SECTION_NAME (var_decl) = NULL_TREE;
1530 resolve_unique_section (var_decl,
1531 compute_reloc_for_constant (expr_tree),
1536 // Make a local variable.
1538 Bvariable*
1539 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1540 Btype* btype, bool is_address_taken,
1541 Location location)
1543 tree type_tree = btype->get_tree();
1544 if (type_tree == error_mark_node)
1545 return this->error_variable();
1546 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1547 get_identifier_from_string(name),
1548 type_tree);
1549 DECL_CONTEXT(decl) = function->get_tree();
1550 TREE_USED(decl) = 1;
1551 if (is_address_taken)
1552 TREE_ADDRESSABLE(decl) = 1;
1553 go_preserve_from_gc(decl);
1554 return new Bvariable(decl);
1557 // Make a function parameter variable.
1559 Bvariable*
1560 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1561 Btype* btype, bool is_address_taken,
1562 Location location)
1564 tree type_tree = btype->get_tree();
1565 if (type_tree == error_mark_node)
1566 return this->error_variable();
1567 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1568 get_identifier_from_string(name),
1569 type_tree);
1570 DECL_CONTEXT(decl) = function->get_tree();
1571 DECL_ARG_TYPE(decl) = type_tree;
1572 TREE_USED(decl) = 1;
1573 if (is_address_taken)
1574 TREE_ADDRESSABLE(decl) = 1;
1575 go_preserve_from_gc(decl);
1576 return new Bvariable(decl);
1579 // Make a temporary variable.
1581 Bvariable*
1582 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1583 Btype* btype, Bexpression* binit,
1584 bool is_address_taken,
1585 Location location,
1586 Bstatement** pstatement)
1588 tree type_tree = btype->get_tree();
1589 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1590 if (type_tree == error_mark_node || init_tree == error_mark_node)
1592 *pstatement = this->error_statement();
1593 return this->error_variable();
1596 tree var;
1597 // We can only use create_tmp_var if the type is not addressable.
1598 if (!TREE_ADDRESSABLE(type_tree))
1599 var = create_tmp_var(type_tree, "GOTMP");
1600 else
1602 gcc_assert(bblock != NULL);
1603 var = build_decl(location.gcc_location(), VAR_DECL,
1604 create_tmp_var_name("GOTMP"),
1605 type_tree);
1606 DECL_ARTIFICIAL(var) = 1;
1607 DECL_IGNORED_P(var) = 1;
1608 TREE_USED(var) = 1;
1609 // FIXME: Permitting function to be NULL here is a temporary
1610 // measure until we have a proper representation of the init
1611 // function.
1612 if (function != NULL)
1613 DECL_CONTEXT(var) = function->get_tree();
1614 else
1616 gcc_assert(current_function_decl != NULL_TREE);
1617 DECL_CONTEXT(var) = current_function_decl;
1620 // We have to add this variable to the BLOCK and the BIND_EXPR.
1621 tree bind_tree = bblock->get_tree();
1622 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1623 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1624 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1625 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1626 BLOCK_VARS(block_tree) = var;
1627 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1630 if (init_tree != NULL_TREE)
1631 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1632 init_tree);
1634 if (is_address_taken)
1635 TREE_ADDRESSABLE(var) = 1;
1637 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1638 DECL_EXPR,
1639 void_type_node, var));
1640 return new Bvariable(var);
1643 // Create a named immutable initialized data structure.
1645 Bvariable*
1646 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
1647 bool, Btype* btype, Location location)
1649 tree type_tree = btype->get_tree();
1650 if (type_tree == error_mark_node)
1651 return this->error_variable();
1652 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1653 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1654 get_identifier_from_string(name),
1655 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1656 TREE_STATIC(decl) = 1;
1657 TREE_READONLY(decl) = 1;
1658 TREE_CONSTANT(decl) = 1;
1659 TREE_USED(decl) = 1;
1660 DECL_ARTIFICIAL(decl) = 1;
1661 if (!is_hidden)
1662 TREE_PUBLIC(decl) = 1;
1664 // We don't call rest_of_decl_compilation until we have the
1665 // initializer.
1667 go_preserve_from_gc(decl);
1668 return new Bvariable(decl);
1671 // Set the initializer for a variable created by immutable_struct.
1672 // This is where we finish compiling the variable.
1674 void
1675 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1676 bool, bool is_common, Btype*, Location,
1677 Bexpression* initializer)
1679 tree decl = var->get_tree();
1680 tree init_tree = initializer->get_tree();
1681 if (decl == error_mark_node || init_tree == error_mark_node)
1682 return;
1684 DECL_INITIAL(decl) = init_tree;
1686 // We can't call make_decl_one_only until we set DECL_INITIAL.
1687 if (is_common)
1688 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1690 // These variables are often unneeded in the final program, so put
1691 // them in their own section so that linker GC can discard them.
1692 resolve_unique_section(decl,
1693 compute_reloc_for_constant (init_tree),
1696 rest_of_decl_compilation(decl, 1, 0);
1699 // Return a reference to an immutable initialized data structure
1700 // defined in another package.
1702 Bvariable*
1703 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1704 Location location)
1706 tree type_tree = btype->get_tree();
1707 if (type_tree == error_mark_node)
1708 return this->error_variable();
1709 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1710 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1711 get_identifier_from_string(name),
1712 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1713 TREE_READONLY(decl) = 1;
1714 TREE_CONSTANT(decl) = 1;
1715 DECL_ARTIFICIAL(decl) = 1;
1716 TREE_PUBLIC(decl) = 1;
1717 DECL_EXTERNAL(decl) = 1;
1718 go_preserve_from_gc(decl);
1719 return new Bvariable(decl);
1722 // Make a label.
1724 Blabel*
1725 Gcc_backend::label(Bfunction* function, const std::string& name,
1726 Location location)
1728 tree decl;
1729 if (name.empty())
1730 decl = create_artificial_label(location.gcc_location());
1731 else
1733 tree id = get_identifier_from_string(name);
1734 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1735 void_type_node);
1736 DECL_CONTEXT(decl) = function->get_tree();
1738 return new Blabel(decl);
1741 // Make a statement which defines a label.
1743 Bstatement*
1744 Gcc_backend::label_definition_statement(Blabel* label)
1746 tree lab = label->get_tree();
1747 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1748 void_type_node, lab);
1749 return this->make_statement(ret);
1752 // Make a goto statement.
1754 Bstatement*
1755 Gcc_backend::goto_statement(Blabel* label, Location location)
1757 tree lab = label->get_tree();
1758 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1759 lab);
1760 return this->make_statement(ret);
1763 // Get the address of a label.
1765 Bexpression*
1766 Gcc_backend::label_address(Blabel* label, Location location)
1768 tree lab = label->get_tree();
1769 TREE_USED(lab) = 1;
1770 TREE_ADDRESSABLE(lab) = 1;
1771 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1772 build_fold_addr_expr_loc(location.gcc_location(),
1773 lab));
1774 return this->make_expression(ret);
1777 // Declare or define a new function.
1779 Bfunction*
1780 Gcc_backend::function(Btype* fntype, const std::string& name,
1781 const std::string& asm_name, bool is_visible,
1782 bool is_declaration, bool is_inlinable,
1783 bool disable_split_stack, bool in_unique_section,
1784 Location location)
1786 tree functype = fntype->get_tree();
1787 if (functype != error_mark_node)
1789 gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
1790 functype = TREE_TYPE(functype);
1792 tree id = get_identifier_from_string(name);
1793 if (functype == error_mark_node || id == error_mark_node)
1794 return this->error_function();
1796 tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
1797 if (!asm_name.empty())
1798 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1799 if (is_visible)
1800 TREE_PUBLIC(decl) = 1;
1801 if (is_declaration)
1802 DECL_EXTERNAL(decl) = 1;
1803 else
1805 tree restype = TREE_TYPE(functype);
1806 tree resdecl =
1807 build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
1808 DECL_ARTIFICIAL(resdecl) = 1;
1809 DECL_IGNORED_P(resdecl) = 1;
1810 DECL_CONTEXT(resdecl) = decl;
1811 DECL_RESULT(decl) = resdecl;
1813 if (!is_inlinable)
1814 DECL_UNINLINABLE(decl) = 1;
1815 if (disable_split_stack)
1817 tree attr = get_identifier("__no_split_stack__");
1818 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1820 if (in_unique_section)
1821 resolve_unique_section(decl, 0, 1);
1823 go_preserve_from_gc(decl);
1824 return new Bfunction(decl);
1827 // The single backend.
1829 static Gcc_backend gcc_backend;
1831 // Return the backend generator.
1833 Backend*
1834 go_get_backend()
1836 return &gcc_backend;
1839 // FIXME: Temporary functions while converting to the new backend
1840 // interface.
1842 Btype*
1843 tree_to_type(tree t)
1845 return new Btype(t);
1848 Bexpression*
1849 tree_to_expr(tree t)
1851 return new Bexpression(t);
1854 Bstatement*
1855 tree_to_stat(tree t)
1857 return new Bstatement(t);
1860 Bfunction*
1861 tree_to_function(tree t)
1863 return new Bfunction(t);
1866 Bblock*
1867 tree_to_block(tree t)
1869 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1870 return new Bblock(t);
1873 tree
1874 type_to_tree(Btype* bt)
1876 return bt->get_tree();
1879 tree
1880 expr_to_tree(Bexpression* be)
1882 return be->get_tree();
1885 tree
1886 stat_to_tree(Bstatement* bs)
1888 return bs->get_tree();
1891 tree
1892 block_to_tree(Bblock* bb)
1894 return bb->get_tree();
1897 tree
1898 var_to_tree(Bvariable* bv)
1900 return bv->get_tree();
1903 tree
1904 function_to_tree(Bfunction* bf)
1906 return bf->get_tree();