* testsuite/20_util/shared_ptr/cons/58659.cc: Use VERIFY instead of
[official-gcc.git] / gcc / go / go-gcc.cc
blobfcfd41b34ab3cfdf00e3ade32734ea88686ceb48
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 "tree-iterator.h"
29 #include "gimple.h"
30 #include "toplev.h"
31 #include "output.h"
32 #include "real.h"
33 #include "realmpfr.h"
35 #include "go-c.h"
37 #include "gogo.h"
38 #include "backend.h"
40 // A class wrapping a tree.
42 class Gcc_tree
44 public:
45 Gcc_tree(tree t)
46 : t_(t)
47 { }
49 tree
50 get_tree() const
51 { return this->t_; }
53 void
54 set_tree(tree t)
55 { this->t_ = t; }
57 private:
58 tree t_;
61 // In gcc, types, expressions, and statements are all trees.
62 class Btype : public Gcc_tree
64 public:
65 Btype(tree t)
66 : Gcc_tree(t)
67 { }
70 class Bexpression : public Gcc_tree
72 public:
73 Bexpression(tree t)
74 : Gcc_tree(t)
75 { }
78 class Bstatement : public Gcc_tree
80 public:
81 Bstatement(tree t)
82 : Gcc_tree(t)
83 { }
86 class Bfunction : public Gcc_tree
88 public:
89 Bfunction(tree t)
90 : Gcc_tree(t)
91 { }
94 class Bblock : public Gcc_tree
96 public:
97 Bblock(tree t)
98 : Gcc_tree(t)
99 { }
102 class Bvariable : public Gcc_tree
104 public:
105 Bvariable(tree t)
106 : Gcc_tree(t)
110 class Blabel : public Gcc_tree
112 public:
113 Blabel(tree t)
114 : Gcc_tree(t)
118 // This file implements the interface between the Go frontend proper
119 // and the gcc IR. This implements specific instantiations of
120 // abstract classes defined by the Go frontend proper. The Go
121 // frontend proper class methods of these classes to generate the
122 // backend representation.
124 class Gcc_backend : public Backend
126 public:
127 // Types.
129 Btype*
130 error_type()
131 { return this->make_type(error_mark_node); }
133 Btype*
134 void_type()
135 { return this->make_type(void_type_node); }
137 Btype*
138 bool_type()
139 { return this->make_type(boolean_type_node); }
141 Btype*
142 integer_type(bool, int);
144 Btype*
145 float_type(int);
147 Btype*
148 complex_type(int);
150 Btype*
151 pointer_type(Btype*);
153 Btype*
154 function_type(const Btyped_identifier&,
155 const std::vector<Btyped_identifier>&,
156 const std::vector<Btyped_identifier>&,
157 const Location);
159 Btype*
160 struct_type(const std::vector<Btyped_identifier>&);
162 Btype*
163 array_type(Btype*, Bexpression*);
165 Btype*
166 placeholder_pointer_type(const std::string&, Location, bool);
168 bool
169 set_placeholder_pointer_type(Btype*, Btype*);
171 bool
172 set_placeholder_function_type(Btype*, Btype*);
174 Btype*
175 placeholder_struct_type(const std::string&, Location);
177 bool
178 set_placeholder_struct_type(Btype* placeholder,
179 const std::vector<Btyped_identifier>&);
181 Btype*
182 placeholder_array_type(const std::string&, Location);
184 bool
185 set_placeholder_array_type(Btype*, Btype*, Bexpression*);
187 Btype*
188 named_type(const std::string&, Btype*, Location);
190 Btype*
191 circular_pointer_type(Btype*, bool);
193 bool
194 is_circular_pointer_type(Btype*);
196 size_t
197 type_size(Btype*);
199 size_t
200 type_alignment(Btype*);
202 size_t
203 type_field_alignment(Btype*);
205 size_t
206 type_field_offset(Btype*, size_t index);
208 // Expressions.
210 Bexpression*
211 zero_expression(Btype*);
213 Bexpression*
214 error_expression()
215 { return this->make_expression(error_mark_node); }
217 Bexpression*
218 var_expression(Bvariable* var, Location);
220 Bexpression*
221 indirect_expression(Bexpression* expr, bool known_valid, Location);
223 Bexpression*
224 integer_constant_expression(Btype* btype, mpz_t val);
226 Bexpression*
227 float_constant_expression(Btype* btype, mpfr_t val);
229 Bexpression*
230 complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag);
232 Bexpression*
233 convert_expression(Btype* type, Bexpression* expr, Location);
235 // Statements.
237 Bstatement*
238 error_statement()
239 { return this->make_statement(error_mark_node); }
241 Bstatement*
242 expression_statement(Bexpression*);
244 Bstatement*
245 init_statement(Bvariable* var, Bexpression* init);
247 Bstatement*
248 assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
250 Bstatement*
251 return_statement(Bfunction*, const std::vector<Bexpression*>&,
252 Location);
254 Bstatement*
255 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
256 Location);
258 Bstatement*
259 switch_statement(Bexpression* value,
260 const std::vector<std::vector<Bexpression*> >& cases,
261 const std::vector<Bstatement*>& statements,
262 Location);
264 Bstatement*
265 compound_statement(Bstatement*, Bstatement*);
267 Bstatement*
268 statement_list(const std::vector<Bstatement*>&);
270 // Blocks.
272 Bblock*
273 block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
274 Location, Location);
276 void
277 block_add_statements(Bblock*, const std::vector<Bstatement*>&);
279 Bstatement*
280 block_statement(Bblock*);
282 // Variables.
284 Bvariable*
285 error_variable()
286 { return new Bvariable(error_mark_node); }
288 Bvariable*
289 global_variable(const std::string& package_name,
290 const std::string& pkgpath,
291 const std::string& name,
292 Btype* btype,
293 bool is_external,
294 bool is_hidden,
295 bool in_unique_section,
296 Location location);
298 void
299 global_variable_set_init(Bvariable*, Bexpression*);
301 Bvariable*
302 local_variable(Bfunction*, const std::string&, Btype*, bool,
303 Location);
305 Bvariable*
306 parameter_variable(Bfunction*, const std::string&, Btype*, bool,
307 Location);
309 Bvariable*
310 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
311 Location, Bstatement**);
313 Bvariable*
314 immutable_struct(const std::string&, bool, bool, Btype*, Location);
316 void
317 immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
318 Location, Bexpression*);
320 Bvariable*
321 immutable_struct_reference(const std::string&, Btype*, Location);
323 // Labels.
325 Blabel*
326 label(Bfunction*, const std::string& name, Location);
328 Bstatement*
329 label_definition_statement(Blabel*);
331 Bstatement*
332 goto_statement(Blabel*, Location);
334 Bexpression*
335 label_address(Blabel*, Location);
337 private:
338 // Make a Bexpression from a tree.
339 Bexpression*
340 make_expression(tree t)
341 { return new Bexpression(t); }
343 // Make a Bstatement from a tree.
344 Bstatement*
345 make_statement(tree t)
346 { return new Bstatement(t); }
348 // Make a Btype from a tree.
349 Btype*
350 make_type(tree t)
351 { return new Btype(t); }
353 Btype*
354 fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
356 Btype*
357 fill_in_array(Btype*, Btype*, Bexpression*);
359 tree
360 non_zero_size_type(tree);
363 // A helper function.
365 static inline tree
366 get_identifier_from_string(const std::string& str)
368 return get_identifier_with_length(str.data(), str.length());
371 // Get an unnamed integer type.
373 Btype*
374 Gcc_backend::integer_type(bool is_unsigned, int bits)
376 tree type;
377 if (is_unsigned)
379 if (bits == INT_TYPE_SIZE)
380 type = unsigned_type_node;
381 else if (bits == CHAR_TYPE_SIZE)
382 type = unsigned_char_type_node;
383 else if (bits == SHORT_TYPE_SIZE)
384 type = short_unsigned_type_node;
385 else if (bits == LONG_TYPE_SIZE)
386 type = long_unsigned_type_node;
387 else if (bits == LONG_LONG_TYPE_SIZE)
388 type = long_long_unsigned_type_node;
389 else
390 type = make_unsigned_type(bits);
392 else
394 if (bits == INT_TYPE_SIZE)
395 type = integer_type_node;
396 else if (bits == CHAR_TYPE_SIZE)
397 type = signed_char_type_node;
398 else if (bits == SHORT_TYPE_SIZE)
399 type = short_integer_type_node;
400 else if (bits == LONG_TYPE_SIZE)
401 type = long_integer_type_node;
402 else if (bits == LONG_LONG_TYPE_SIZE)
403 type = long_long_integer_type_node;
404 else
405 type = make_signed_type(bits);
407 return this->make_type(type);
410 // Get an unnamed float type.
412 Btype*
413 Gcc_backend::float_type(int bits)
415 tree type;
416 if (bits == FLOAT_TYPE_SIZE)
417 type = float_type_node;
418 else if (bits == DOUBLE_TYPE_SIZE)
419 type = double_type_node;
420 else if (bits == LONG_DOUBLE_TYPE_SIZE)
421 type = long_double_type_node;
422 else
424 type = make_node(REAL_TYPE);
425 TYPE_PRECISION(type) = bits;
426 layout_type(type);
428 return this->make_type(type);
431 // Get an unnamed complex type.
433 Btype*
434 Gcc_backend::complex_type(int bits)
436 tree type;
437 if (bits == FLOAT_TYPE_SIZE * 2)
438 type = complex_float_type_node;
439 else if (bits == DOUBLE_TYPE_SIZE * 2)
440 type = complex_double_type_node;
441 else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
442 type = complex_long_double_type_node;
443 else
445 type = make_node(REAL_TYPE);
446 TYPE_PRECISION(type) = bits / 2;
447 layout_type(type);
448 type = build_complex_type(type);
450 return this->make_type(type);
453 // Get a pointer type.
455 Btype*
456 Gcc_backend::pointer_type(Btype* to_type)
458 tree to_type_tree = to_type->get_tree();
459 if (to_type_tree == error_mark_node)
460 return this->error_type();
461 tree type = build_pointer_type(to_type_tree);
462 return this->make_type(type);
465 // Make a function type.
467 Btype*
468 Gcc_backend::function_type(const Btyped_identifier& receiver,
469 const std::vector<Btyped_identifier>& parameters,
470 const std::vector<Btyped_identifier>& results,
471 Location location)
473 tree args = NULL_TREE;
474 tree* pp = &args;
475 if (receiver.btype != NULL)
477 tree t = receiver.btype->get_tree();
478 if (t == error_mark_node)
479 return this->error_type();
480 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
481 pp = &TREE_CHAIN(*pp);
484 for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
485 p != parameters.end();
486 ++p)
488 tree t = p->btype->get_tree();
489 if (t == error_mark_node)
490 return this->error_type();
491 *pp = tree_cons(NULL_TREE, t, NULL_TREE);
492 pp = &TREE_CHAIN(*pp);
495 // Varargs is handled entirely at the Go level. When converted to
496 // GENERIC functions are not varargs.
497 *pp = void_list_node;
499 tree result;
500 if (results.empty())
501 result = void_type_node;
502 else if (results.size() == 1)
503 result = results.front().btype->get_tree();
504 else
506 result = make_node(RECORD_TYPE);
507 tree field_trees = NULL_TREE;
508 pp = &field_trees;
509 for (std::vector<Btyped_identifier>::const_iterator p = results.begin();
510 p != results.end();
511 ++p)
513 const std::string name = (p->name.empty()
514 ? "UNNAMED"
515 : p->name);
516 tree name_tree = get_identifier_from_string(name);
517 tree field_type_tree = p->btype->get_tree();
518 if (field_type_tree == error_mark_node)
519 return this->error_type();
520 gcc_assert(TYPE_SIZE(field_type_tree) != NULL_TREE);
521 tree field = build_decl(location.gcc_location(), FIELD_DECL,
522 name_tree, field_type_tree);
523 DECL_CONTEXT(field) = result;
524 *pp = field;
525 pp = &DECL_CHAIN(field);
527 TYPE_FIELDS(result) = field_trees;
528 layout_type(result);
530 if (result == error_mark_node)
531 return this->error_type();
533 tree fntype = build_function_type(result, args);
534 if (fntype == error_mark_node)
535 return this->error_type();
537 return this->make_type(build_pointer_type(fntype));
540 // Make a struct type.
542 Btype*
543 Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
545 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
548 // Fill in the fields of a struct type.
550 Btype*
551 Gcc_backend::fill_in_struct(Btype* fill,
552 const std::vector<Btyped_identifier>& fields)
554 tree fill_tree = fill->get_tree();
555 tree field_trees = NULL_TREE;
556 tree* pp = &field_trees;
557 for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
558 p != fields.end();
559 ++p)
561 tree name_tree = get_identifier_from_string(p->name);
562 tree type_tree = p->btype->get_tree();
563 if (type_tree == error_mark_node)
564 return this->error_type();
565 tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
566 type_tree);
567 DECL_CONTEXT(field) = fill_tree;
568 *pp = field;
569 pp = &DECL_CHAIN(field);
571 TYPE_FIELDS(fill_tree) = field_trees;
572 layout_type(fill_tree);
573 return fill;
576 // Make an array type.
578 Btype*
579 Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
581 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
582 element_btype, length);
585 // Fill in an array type.
587 Btype*
588 Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
589 Bexpression* length)
591 tree element_type_tree = element_type->get_tree();
592 tree length_tree = length->get_tree();
593 if (element_type_tree == error_mark_node || length_tree == error_mark_node)
594 return this->error_type();
596 gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
598 length_tree = fold_convert(sizetype, length_tree);
600 // build_index_type takes the maximum index, which is one less than
601 // the length.
602 tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
603 length_tree,
604 size_one_node));
606 tree fill_tree = fill->get_tree();
607 TREE_TYPE(fill_tree) = element_type_tree;
608 TYPE_DOMAIN(fill_tree) = index_type_tree;
609 TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
610 layout_type(fill_tree);
612 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
613 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
614 else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
615 || TYPE_CANONICAL(index_type_tree) != index_type_tree)
616 TYPE_CANONICAL(fill_tree) =
617 build_array_type(TYPE_CANONICAL(element_type_tree),
618 TYPE_CANONICAL(index_type_tree));
620 return fill;
623 // Create a placeholder for a pointer type.
625 Btype*
626 Gcc_backend::placeholder_pointer_type(const std::string& name,
627 Location location, bool)
629 tree ret = build_distinct_type_copy(ptr_type_node);
630 if (!name.empty())
632 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
633 get_identifier_from_string(name),
634 ret);
635 TYPE_NAME(ret) = decl;
637 return this->make_type(ret);
640 // Set the real target type for a placeholder pointer type.
642 bool
643 Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
644 Btype* to_type)
646 tree pt = placeholder->get_tree();
647 if (pt == error_mark_node)
648 return false;
649 gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
650 tree tt = to_type->get_tree();
651 if (tt == error_mark_node)
653 placeholder->set_tree(error_mark_node);
654 return false;
656 gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
657 TREE_TYPE(pt) = TREE_TYPE(tt);
658 if (TYPE_NAME(pt) != NULL_TREE)
660 // Build the data structure gcc wants to see for a typedef.
661 tree copy = build_variant_type_copy(pt);
662 TYPE_NAME(copy) = NULL_TREE;
663 DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
665 return true;
668 // Set the real values for a placeholder function type.
670 bool
671 Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
673 return this->set_placeholder_pointer_type(placeholder, ft);
676 // Create a placeholder for a struct type.
678 Btype*
679 Gcc_backend::placeholder_struct_type(const std::string& name,
680 Location location)
682 tree ret = make_node(RECORD_TYPE);
683 if (!name.empty())
685 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
686 get_identifier_from_string(name),
687 ret);
688 TYPE_NAME(ret) = decl;
690 return this->make_type(ret);
693 // Fill in the fields of a placeholder struct type.
695 bool
696 Gcc_backend::set_placeholder_struct_type(
697 Btype* placeholder,
698 const std::vector<Btyped_identifier>& fields)
700 tree t = placeholder->get_tree();
701 gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
702 Btype* r = this->fill_in_struct(placeholder, fields);
704 if (TYPE_NAME(t) != NULL_TREE)
706 // Build the data structure gcc wants to see for a typedef.
707 tree copy = build_distinct_type_copy(t);
708 TYPE_NAME(copy) = NULL_TREE;
709 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
712 return r->get_tree() != error_mark_node;
715 // Create a placeholder for an array type.
717 Btype*
718 Gcc_backend::placeholder_array_type(const std::string& name,
719 Location location)
721 tree ret = make_node(ARRAY_TYPE);
722 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
723 get_identifier_from_string(name),
724 ret);
725 TYPE_NAME(ret) = decl;
726 return this->make_type(ret);
729 // Fill in the fields of a placeholder array type.
731 bool
732 Gcc_backend::set_placeholder_array_type(Btype* placeholder,
733 Btype* element_btype,
734 Bexpression* length)
736 tree t = placeholder->get_tree();
737 gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
738 Btype* r = this->fill_in_array(placeholder, element_btype, length);
740 // Build the data structure gcc wants to see for a typedef.
741 tree copy = build_distinct_type_copy(t);
742 TYPE_NAME(copy) = NULL_TREE;
743 DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
745 return r->get_tree() != error_mark_node;
748 // Return a named version of a type.
750 Btype*
751 Gcc_backend::named_type(const std::string& name, Btype* btype,
752 Location location)
754 tree type = btype->get_tree();
755 if (type == error_mark_node)
756 return this->error_type();
758 // The middle-end expects a basic type to have a name. In Go every
759 // basic type will have a name. The first time we see a basic type,
760 // give it whatever Go name we have at this point.
761 if (TYPE_NAME(type) == NULL_TREE
762 && location.gcc_location() == BUILTINS_LOCATION
763 && (TREE_CODE(type) == INTEGER_TYPE
764 || TREE_CODE(type) == REAL_TYPE
765 || TREE_CODE(type) == COMPLEX_TYPE
766 || TREE_CODE(type) == BOOLEAN_TYPE))
768 tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
769 get_identifier_from_string(name),
770 type);
771 TYPE_NAME(type) = decl;
772 return this->make_type(type);
775 tree copy = build_variant_type_copy(type);
776 tree decl = build_decl(location.gcc_location(), TYPE_DECL,
777 get_identifier_from_string(name),
778 copy);
779 DECL_ORIGINAL_TYPE(decl) = type;
780 TYPE_NAME(copy) = decl;
781 return this->make_type(copy);
784 // Return a pointer type used as a marker for a circular type.
786 Btype*
787 Gcc_backend::circular_pointer_type(Btype*, bool)
789 return this->make_type(ptr_type_node);
792 // Return whether we might be looking at a circular type.
794 bool
795 Gcc_backend::is_circular_pointer_type(Btype* btype)
797 return btype->get_tree() == ptr_type_node;
800 // Return the size of a type.
802 size_t
803 Gcc_backend::type_size(Btype* btype)
805 tree t = btype->get_tree();
806 if (t == error_mark_node)
807 return 1;
808 t = TYPE_SIZE_UNIT(t);
809 gcc_assert(TREE_CODE(t) == INTEGER_CST);
810 gcc_assert(TREE_INT_CST_HIGH(t) == 0);
811 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
812 size_t ret = static_cast<size_t>(val_wide);
813 gcc_assert(ret == val_wide);
814 return ret;
817 // Return the alignment of a type.
819 size_t
820 Gcc_backend::type_alignment(Btype* btype)
822 tree t = btype->get_tree();
823 if (t == error_mark_node)
824 return 1;
825 return TYPE_ALIGN_UNIT(t);
828 // Return the alignment of a struct field of type BTYPE.
830 size_t
831 Gcc_backend::type_field_alignment(Btype* btype)
833 tree t = btype->get_tree();
834 if (t == error_mark_node)
835 return 1;
836 return go_field_alignment(t);
839 // Return the offset of a field in a struct.
841 size_t
842 Gcc_backend::type_field_offset(Btype* btype, size_t index)
844 tree struct_tree = btype->get_tree();
845 if (struct_tree == error_mark_node)
846 return 0;
847 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
848 tree field = TYPE_FIELDS(struct_tree);
849 for (; index > 0; --index)
851 field = DECL_CHAIN(field);
852 gcc_assert(field != NULL_TREE);
854 HOST_WIDE_INT offset_wide = int_byte_position(field);
855 gcc_assert(offset_wide >= 0);
856 size_t ret = static_cast<size_t>(offset_wide);
857 gcc_assert(ret == static_cast<unsigned HOST_WIDE_INT>(offset_wide));
858 return ret;
861 // Return the zero value for a type.
863 Bexpression*
864 Gcc_backend::zero_expression(Btype* btype)
866 tree t = btype->get_tree();
867 tree ret;
868 if (t == error_mark_node)
869 ret = error_mark_node;
870 else
871 ret = build_zero_cst(t);
872 return tree_to_expr(ret);
875 // An expression that references a variable.
877 Bexpression*
878 Gcc_backend::var_expression(Bvariable* var, Location)
880 tree ret = var->get_tree();
881 if (ret == error_mark_node)
882 return this->error_expression();
883 return tree_to_expr(ret);
886 // An expression that indirectly references an expression.
888 Bexpression*
889 Gcc_backend::indirect_expression(Bexpression* expr, bool known_valid,
890 Location location)
892 tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
893 expr->get_tree());
894 if (known_valid)
895 TREE_THIS_NOTRAP(ret) = 1;
896 return tree_to_expr(ret);
899 // Return a typed value as a constant integer.
901 Bexpression*
902 Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
904 tree t = btype->get_tree();
905 if (t == error_mark_node)
906 return this->error_expression();
908 tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
909 return tree_to_expr(ret);
912 // Return a typed value as a constant floating-point number.
914 Bexpression*
915 Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
917 tree t = btype->get_tree();
918 tree ret;
919 if (t == error_mark_node)
920 return this->error_expression();
922 REAL_VALUE_TYPE r1;
923 real_from_mpfr(&r1, val, t, GMP_RNDN);
924 REAL_VALUE_TYPE r2;
925 real_convert(&r2, TYPE_MODE(t), &r1);
926 ret = build_real(t, r2);
927 return tree_to_expr(ret);
930 // Return a typed real and imaginary value as a constant complex number.
932 Bexpression*
933 Gcc_backend::complex_constant_expression(Btype* btype, mpfr_t real, mpfr_t imag)
935 tree t = btype->get_tree();
936 tree ret;
937 if (t == error_mark_node)
938 return this->error_expression();
940 REAL_VALUE_TYPE r1;
941 real_from_mpfr(&r1, real, TREE_TYPE(t), GMP_RNDN);
942 REAL_VALUE_TYPE r2;
943 real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
945 REAL_VALUE_TYPE r3;
946 real_from_mpfr(&r3, imag, TREE_TYPE(t), GMP_RNDN);
947 REAL_VALUE_TYPE r4;
948 real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
950 ret = build_complex(t, build_real(TREE_TYPE(t), r2),
951 build_real(TREE_TYPE(t), r4));
952 return tree_to_expr(ret);
955 // An expression that converts an expression to a different type.
957 Bexpression*
958 Gcc_backend::convert_expression(Btype* type, Bexpression* expr, Location)
960 tree type_tree = type->get_tree();
961 tree expr_tree = expr->get_tree();
962 if (type_tree == error_mark_node || expr_tree == error_mark_node)
963 return this->error_expression();
965 tree ret = fold_convert(type_tree, expr_tree);
966 return tree_to_expr(ret);
969 // An expression as a statement.
971 Bstatement*
972 Gcc_backend::expression_statement(Bexpression* expr)
974 return this->make_statement(expr->get_tree());
977 // Variable initialization.
979 Bstatement*
980 Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
982 tree var_tree = var->get_tree();
983 tree init_tree = init->get_tree();
984 if (var_tree == error_mark_node || init_tree == error_mark_node)
985 return this->error_statement();
986 gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
988 // To avoid problems with GNU ld, we don't make zero-sized
989 // externally visible variables. That might lead us to doing an
990 // initialization of a zero-sized expression to a non-zero sized
991 // variable, or vice-versa. Avoid crashes by omitting the
992 // initializer. Such initializations don't mean anything anyhow.
993 if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
994 && init_tree != NULL_TREE
995 && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
997 DECL_INITIAL(var_tree) = init_tree;
998 init_tree = NULL_TREE;
1001 tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
1002 void_type_node, var_tree);
1003 if (init_tree != NULL_TREE)
1004 ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
1005 void_type_node, init_tree, ret);
1007 return this->make_statement(ret);
1010 // Assignment.
1012 Bstatement*
1013 Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
1014 Location location)
1016 tree lhs_tree = lhs->get_tree();
1017 tree rhs_tree = rhs->get_tree();
1018 if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
1019 return this->error_statement();
1021 // To avoid problems with GNU ld, we don't make zero-sized
1022 // externally visible variables. That might lead us to doing an
1023 // assignment of a zero-sized expression to a non-zero sized
1024 // expression; avoid crashes here by avoiding assignments of
1025 // zero-sized expressions. Such assignments don't really mean
1026 // anything anyhow.
1027 if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
1028 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
1029 return this->compound_statement(this->expression_statement(lhs),
1030 this->expression_statement(rhs));
1032 // Sometimes the same unnamed Go type can be created multiple times
1033 // and thus have multiple tree representations. Make sure this does
1034 // not confuse the middle-end.
1035 if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
1037 tree lhs_type_tree = TREE_TYPE(lhs_tree);
1038 gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
1039 if (POINTER_TYPE_P(lhs_type_tree)
1040 || INTEGRAL_TYPE_P(lhs_type_tree)
1041 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
1042 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
1043 rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
1044 rhs_tree);
1045 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
1046 || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
1048 gcc_assert(int_size_in_bytes(lhs_type_tree)
1049 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
1050 rhs_tree = fold_build1_loc(location.gcc_location(),
1051 VIEW_CONVERT_EXPR,
1052 lhs_type_tree, rhs_tree);
1056 return this->make_statement(fold_build2_loc(location.gcc_location(),
1057 MODIFY_EXPR,
1058 void_type_node,
1059 lhs_tree, rhs_tree));
1062 // Return.
1064 Bstatement*
1065 Gcc_backend::return_statement(Bfunction* bfunction,
1066 const std::vector<Bexpression*>& vals,
1067 Location location)
1069 tree fntree = bfunction->get_tree();
1070 if (fntree == error_mark_node)
1071 return this->error_statement();
1072 tree result = DECL_RESULT(fntree);
1073 if (result == error_mark_node)
1074 return this->error_statement();
1075 tree ret;
1076 if (vals.empty())
1077 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
1078 NULL_TREE);
1079 else if (vals.size() == 1)
1081 tree val = vals.front()->get_tree();
1082 if (val == error_mark_node)
1083 return this->error_statement();
1084 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1085 void_type_node, result,
1086 vals.front()->get_tree());
1087 ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1088 void_type_node, set);
1090 else
1092 // To return multiple values, copy the values into a temporary
1093 // variable of the right structure type, and then assign the
1094 // temporary variable to the DECL_RESULT in the return
1095 // statement.
1096 tree stmt_list = NULL_TREE;
1097 tree rettype = TREE_TYPE(result);
1098 tree rettmp = create_tmp_var(rettype, "RESULT");
1099 tree field = TYPE_FIELDS(rettype);
1100 for (std::vector<Bexpression*>::const_iterator p = vals.begin();
1101 p != vals.end();
1102 p++, field = DECL_CHAIN(field))
1104 gcc_assert(field != NULL_TREE);
1105 tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
1106 TREE_TYPE(field), rettmp, field,
1107 NULL_TREE);
1108 tree val = (*p)->get_tree();
1109 if (val == error_mark_node)
1110 return this->error_statement();
1111 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1112 void_type_node,
1113 ref, (*p)->get_tree());
1114 append_to_statement_list(set, &stmt_list);
1116 gcc_assert(field == NULL_TREE);
1117 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
1118 void_type_node,
1119 result, rettmp);
1120 tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
1121 void_type_node, set);
1122 append_to_statement_list(ret_expr, &stmt_list);
1123 ret = stmt_list;
1125 return this->make_statement(ret);
1128 // If.
1130 Bstatement*
1131 Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
1132 Bblock* else_block, Location location)
1134 tree cond_tree = condition->get_tree();
1135 tree then_tree = then_block->get_tree();
1136 tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
1137 if (cond_tree == error_mark_node
1138 || then_tree == error_mark_node
1139 || else_tree == error_mark_node)
1140 return this->error_statement();
1141 tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
1142 cond_tree, then_tree, else_tree);
1143 return this->make_statement(ret);
1146 // Switch.
1148 Bstatement*
1149 Gcc_backend::switch_statement(
1150 Bexpression* value,
1151 const std::vector<std::vector<Bexpression*> >& cases,
1152 const std::vector<Bstatement*>& statements,
1153 Location switch_location)
1155 gcc_assert(cases.size() == statements.size());
1157 tree stmt_list = NULL_TREE;
1158 std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
1159 for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
1160 ps != statements.end();
1161 ++ps, ++pc)
1163 if (pc->empty())
1165 source_location loc = (*ps != NULL
1166 ? EXPR_LOCATION((*ps)->get_tree())
1167 : UNKNOWN_LOCATION);
1168 tree label = create_artificial_label(loc);
1169 tree c = build_case_label(NULL_TREE, NULL_TREE, label);
1170 append_to_statement_list(c, &stmt_list);
1172 else
1174 for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
1175 pcv != pc->end();
1176 ++pcv)
1178 tree t = (*pcv)->get_tree();
1179 if (t == error_mark_node)
1180 return this->error_statement();
1181 source_location loc = EXPR_LOCATION(t);
1182 tree label = create_artificial_label(loc);
1183 tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
1184 append_to_statement_list(c, &stmt_list);
1188 if (*ps != NULL)
1190 tree t = (*ps)->get_tree();
1191 if (t == error_mark_node)
1192 return this->error_statement();
1193 append_to_statement_list(t, &stmt_list);
1197 tree tv = value->get_tree();
1198 if (tv == error_mark_node)
1199 return this->error_statement();
1200 tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
1201 NULL_TREE, tv, stmt_list, NULL_TREE);
1202 return this->make_statement(t);
1205 // Pair of statements.
1207 Bstatement*
1208 Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
1210 tree stmt_list = NULL_TREE;
1211 tree t = s1->get_tree();
1212 if (t == error_mark_node)
1213 return this->error_statement();
1214 append_to_statement_list(t, &stmt_list);
1215 t = s2->get_tree();
1216 if (t == error_mark_node)
1217 return this->error_statement();
1218 append_to_statement_list(t, &stmt_list);
1219 return this->make_statement(stmt_list);
1222 // List of statements.
1224 Bstatement*
1225 Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
1227 tree stmt_list = NULL_TREE;
1228 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1229 p != statements.end();
1230 ++p)
1232 tree t = (*p)->get_tree();
1233 if (t == error_mark_node)
1234 return this->error_statement();
1235 append_to_statement_list(t, &stmt_list);
1237 return this->make_statement(stmt_list);
1240 // Make a block. For some reason gcc uses a dual structure for
1241 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1242 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1243 // the Bblock.
1245 Bblock*
1246 Gcc_backend::block(Bfunction* function, Bblock* enclosing,
1247 const std::vector<Bvariable*>& vars,
1248 Location start_location,
1249 Location)
1251 tree block_tree = make_node(BLOCK);
1252 if (enclosing == NULL)
1254 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1255 // until we have a proper representation of the init function.
1256 tree fndecl;
1257 if (function == NULL)
1258 fndecl = current_function_decl;
1259 else
1260 fndecl = function->get_tree();
1261 gcc_assert(fndecl != NULL_TREE);
1263 // We may have already created a block for local variables when
1264 // we take the address of a parameter.
1265 if (DECL_INITIAL(fndecl) == NULL_TREE)
1267 BLOCK_SUPERCONTEXT(block_tree) = fndecl;
1268 DECL_INITIAL(fndecl) = block_tree;
1270 else
1272 tree superblock_tree = DECL_INITIAL(fndecl);
1273 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1274 tree* pp;
1275 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1276 *pp != NULL_TREE;
1277 pp = &BLOCK_CHAIN(*pp))
1279 *pp = block_tree;
1282 else
1284 tree superbind_tree = enclosing->get_tree();
1285 tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
1286 gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
1288 BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
1289 tree* pp;
1290 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1291 *pp != NULL_TREE;
1292 pp = &BLOCK_CHAIN(*pp))
1294 *pp = block_tree;
1297 tree* pp = &BLOCK_VARS(block_tree);
1298 for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
1299 pv != vars.end();
1300 ++pv)
1302 *pp = (*pv)->get_tree();
1303 if (*pp != error_mark_node)
1304 pp = &DECL_CHAIN(*pp);
1306 *pp = NULL_TREE;
1308 TREE_USED(block_tree) = 1;
1310 tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
1311 void_type_node, BLOCK_VARS(block_tree),
1312 NULL_TREE, block_tree);
1313 TREE_SIDE_EFFECTS(bind_tree) = 1;
1315 return new Bblock(bind_tree);
1318 // Add statements to a block.
1320 void
1321 Gcc_backend::block_add_statements(Bblock* bblock,
1322 const std::vector<Bstatement*>& statements)
1324 tree stmt_list = NULL_TREE;
1325 for (std::vector<Bstatement*>::const_iterator p = statements.begin();
1326 p != statements.end();
1327 ++p)
1329 tree s = (*p)->get_tree();
1330 if (s != error_mark_node)
1331 append_to_statement_list(s, &stmt_list);
1334 tree bind_tree = bblock->get_tree();
1335 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1336 BIND_EXPR_BODY(bind_tree) = stmt_list;
1339 // Return a block as a statement.
1341 Bstatement*
1342 Gcc_backend::block_statement(Bblock* bblock)
1344 tree bind_tree = bblock->get_tree();
1345 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1346 return this->make_statement(bind_tree);
1349 // This is not static because we declare it with GTY(()) in go-c.h.
1350 tree go_non_zero_struct;
1352 // Return a type corresponding to TYPE with non-zero size.
1354 tree
1355 Gcc_backend::non_zero_size_type(tree type)
1357 if (int_size_in_bytes(type) != 0)
1358 return type;
1360 switch (TREE_CODE(type))
1362 case RECORD_TYPE:
1363 if (TYPE_FIELDS(type) != NULL_TREE)
1365 tree ns = make_node(RECORD_TYPE);
1366 tree field_trees = NULL_TREE;
1367 tree *pp = &field_trees;
1368 for (tree field = TYPE_FIELDS(type);
1369 field != NULL_TREE;
1370 field = DECL_CHAIN(field))
1372 tree ft = TREE_TYPE(field);
1373 if (field == TYPE_FIELDS(type))
1374 ft = non_zero_size_type(ft);
1375 tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
1376 DECL_NAME(field), ft);
1377 DECL_CONTEXT(f) = ns;
1378 *pp = f;
1379 pp = &DECL_CHAIN(f);
1381 TYPE_FIELDS(ns) = field_trees;
1382 layout_type(ns);
1383 return ns;
1386 if (go_non_zero_struct == NULL_TREE)
1388 type = make_node(RECORD_TYPE);
1389 tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
1390 get_identifier("dummy"),
1391 boolean_type_node);
1392 DECL_CONTEXT(field) = type;
1393 TYPE_FIELDS(type) = field;
1394 layout_type(type);
1395 go_non_zero_struct = type;
1397 return go_non_zero_struct;
1399 case ARRAY_TYPE:
1401 tree element_type = non_zero_size_type(TREE_TYPE(type));
1402 return build_array_type_nelts(element_type, 1);
1405 default:
1406 gcc_unreachable();
1409 gcc_unreachable();
1412 // Make a global variable.
1414 Bvariable*
1415 Gcc_backend::global_variable(const std::string& package_name,
1416 const std::string& pkgpath,
1417 const std::string& name,
1418 Btype* btype,
1419 bool is_external,
1420 bool is_hidden,
1421 bool in_unique_section,
1422 Location location)
1424 tree type_tree = btype->get_tree();
1425 if (type_tree == error_mark_node)
1426 return this->error_variable();
1428 // The GNU linker does not like dynamic variables with zero size.
1429 if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
1430 type_tree = this->non_zero_size_type(type_tree);
1432 std::string var_name(package_name);
1433 var_name.push_back('.');
1434 var_name.append(name);
1435 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1436 get_identifier_from_string(var_name),
1437 type_tree);
1438 if (is_external)
1439 DECL_EXTERNAL(decl) = 1;
1440 else
1441 TREE_STATIC(decl) = 1;
1442 if (!is_hidden)
1444 TREE_PUBLIC(decl) = 1;
1446 std::string asm_name(pkgpath);
1447 asm_name.push_back('.');
1448 asm_name.append(name);
1449 SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
1451 TREE_USED(decl) = 1;
1453 if (in_unique_section)
1454 resolve_unique_section (decl, 0, 1);
1456 go_preserve_from_gc(decl);
1458 return new Bvariable(decl);
1461 // Set the initial value of a global variable.
1463 void
1464 Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
1466 tree expr_tree = expr->get_tree();
1467 if (expr_tree == error_mark_node)
1468 return;
1469 gcc_assert(TREE_CONSTANT(expr_tree));
1470 tree var_decl = var->get_tree();
1471 if (var_decl == error_mark_node)
1472 return;
1473 DECL_INITIAL(var_decl) = expr_tree;
1475 // If this variable goes in a unique section, it may need to go into
1476 // a different one now that DECL_INITIAL is set.
1477 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl))
1479 DECL_SECTION_NAME (var_decl) = NULL_TREE;
1480 resolve_unique_section (var_decl,
1481 compute_reloc_for_constant (expr_tree),
1486 // Make a local variable.
1488 Bvariable*
1489 Gcc_backend::local_variable(Bfunction* function, const std::string& name,
1490 Btype* btype, bool is_address_taken,
1491 Location location)
1493 tree type_tree = btype->get_tree();
1494 if (type_tree == error_mark_node)
1495 return this->error_variable();
1496 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1497 get_identifier_from_string(name),
1498 type_tree);
1499 DECL_CONTEXT(decl) = function->get_tree();
1500 TREE_USED(decl) = 1;
1501 if (is_address_taken)
1502 TREE_ADDRESSABLE(decl) = 1;
1503 go_preserve_from_gc(decl);
1504 return new Bvariable(decl);
1507 // Make a function parameter variable.
1509 Bvariable*
1510 Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
1511 Btype* btype, bool is_address_taken,
1512 Location location)
1514 tree type_tree = btype->get_tree();
1515 if (type_tree == error_mark_node)
1516 return this->error_variable();
1517 tree decl = build_decl(location.gcc_location(), PARM_DECL,
1518 get_identifier_from_string(name),
1519 type_tree);
1520 DECL_CONTEXT(decl) = function->get_tree();
1521 DECL_ARG_TYPE(decl) = type_tree;
1522 TREE_USED(decl) = 1;
1523 if (is_address_taken)
1524 TREE_ADDRESSABLE(decl) = 1;
1525 go_preserve_from_gc(decl);
1526 return new Bvariable(decl);
1529 // Make a temporary variable.
1531 Bvariable*
1532 Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
1533 Btype* btype, Bexpression* binit,
1534 bool is_address_taken,
1535 Location location,
1536 Bstatement** pstatement)
1538 tree type_tree = btype->get_tree();
1539 tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
1540 if (type_tree == error_mark_node || init_tree == error_mark_node)
1542 *pstatement = this->error_statement();
1543 return this->error_variable();
1546 tree var;
1547 // We can only use create_tmp_var if the type is not addressable.
1548 if (!TREE_ADDRESSABLE(type_tree))
1549 var = create_tmp_var(type_tree, "GOTMP");
1550 else
1552 gcc_assert(bblock != NULL);
1553 var = build_decl(location.gcc_location(), VAR_DECL,
1554 create_tmp_var_name("GOTMP"),
1555 type_tree);
1556 DECL_ARTIFICIAL(var) = 1;
1557 DECL_IGNORED_P(var) = 1;
1558 TREE_USED(var) = 1;
1559 // FIXME: Permitting function to be NULL here is a temporary
1560 // measure until we have a proper representation of the init
1561 // function.
1562 if (function != NULL)
1563 DECL_CONTEXT(var) = function->get_tree();
1564 else
1566 gcc_assert(current_function_decl != NULL_TREE);
1567 DECL_CONTEXT(var) = current_function_decl;
1570 // We have to add this variable to the BLOCK and the BIND_EXPR.
1571 tree bind_tree = bblock->get_tree();
1572 gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
1573 tree block_tree = BIND_EXPR_BLOCK(bind_tree);
1574 gcc_assert(TREE_CODE(block_tree) == BLOCK);
1575 DECL_CHAIN(var) = BLOCK_VARS(block_tree);
1576 BLOCK_VARS(block_tree) = var;
1577 BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
1580 if (init_tree != NULL_TREE)
1581 DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
1582 init_tree);
1584 if (is_address_taken)
1585 TREE_ADDRESSABLE(var) = 1;
1587 *pstatement = this->make_statement(build1_loc(location.gcc_location(),
1588 DECL_EXPR,
1589 void_type_node, var));
1590 return new Bvariable(var);
1593 // Create a named immutable initialized data structure.
1595 Bvariable*
1596 Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
1597 bool, Btype* btype, Location location)
1599 tree type_tree = btype->get_tree();
1600 if (type_tree == error_mark_node)
1601 return this->error_variable();
1602 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1603 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1604 get_identifier_from_string(name),
1605 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1606 TREE_STATIC(decl) = 1;
1607 TREE_READONLY(decl) = 1;
1608 TREE_CONSTANT(decl) = 1;
1609 TREE_USED(decl) = 1;
1610 DECL_ARTIFICIAL(decl) = 1;
1611 if (!is_hidden)
1612 TREE_PUBLIC(decl) = 1;
1614 // We don't call rest_of_decl_compilation until we have the
1615 // initializer.
1617 go_preserve_from_gc(decl);
1618 return new Bvariable(decl);
1621 // Set the initializer for a variable created by immutable_struct.
1622 // This is where we finish compiling the variable.
1624 void
1625 Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
1626 bool, bool is_common, Btype*, Location,
1627 Bexpression* initializer)
1629 tree decl = var->get_tree();
1630 tree init_tree = initializer->get_tree();
1631 if (decl == error_mark_node || init_tree == error_mark_node)
1632 return;
1634 DECL_INITIAL(decl) = init_tree;
1636 // We can't call make_decl_one_only until we set DECL_INITIAL.
1637 if (is_common)
1638 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
1640 // These variables are often unneeded in the final program, so put
1641 // them in their own section so that linker GC can discard them.
1642 resolve_unique_section(decl,
1643 compute_reloc_for_constant (init_tree),
1646 rest_of_decl_compilation(decl, 1, 0);
1649 // Return a reference to an immutable initialized data structure
1650 // defined in another package.
1652 Bvariable*
1653 Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
1654 Location location)
1656 tree type_tree = btype->get_tree();
1657 if (type_tree == error_mark_node)
1658 return this->error_variable();
1659 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
1660 tree decl = build_decl(location.gcc_location(), VAR_DECL,
1661 get_identifier_from_string(name),
1662 build_qualified_type(type_tree, TYPE_QUAL_CONST));
1663 TREE_READONLY(decl) = 1;
1664 TREE_CONSTANT(decl) = 1;
1665 DECL_ARTIFICIAL(decl) = 1;
1666 TREE_PUBLIC(decl) = 1;
1667 DECL_EXTERNAL(decl) = 1;
1668 go_preserve_from_gc(decl);
1669 return new Bvariable(decl);
1672 // Make a label.
1674 Blabel*
1675 Gcc_backend::label(Bfunction* function, const std::string& name,
1676 Location location)
1678 tree decl;
1679 if (name.empty())
1680 decl = create_artificial_label(location.gcc_location());
1681 else
1683 tree id = get_identifier_from_string(name);
1684 decl = build_decl(location.gcc_location(), LABEL_DECL, id,
1685 void_type_node);
1686 DECL_CONTEXT(decl) = function->get_tree();
1688 return new Blabel(decl);
1691 // Make a statement which defines a label.
1693 Bstatement*
1694 Gcc_backend::label_definition_statement(Blabel* label)
1696 tree lab = label->get_tree();
1697 tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
1698 void_type_node, lab);
1699 return this->make_statement(ret);
1702 // Make a goto statement.
1704 Bstatement*
1705 Gcc_backend::goto_statement(Blabel* label, Location location)
1707 tree lab = label->get_tree();
1708 tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
1709 lab);
1710 return this->make_statement(ret);
1713 // Get the address of a label.
1715 Bexpression*
1716 Gcc_backend::label_address(Blabel* label, Location location)
1718 tree lab = label->get_tree();
1719 TREE_USED(lab) = 1;
1720 TREE_ADDRESSABLE(lab) = 1;
1721 tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
1722 build_fold_addr_expr_loc(location.gcc_location(),
1723 lab));
1724 return this->make_expression(ret);
1727 // The single backend.
1729 static Gcc_backend gcc_backend;
1731 // Return the backend generator.
1733 Backend*
1734 go_get_backend()
1736 return &gcc_backend;
1739 // FIXME: Temporary functions while converting to the new backend
1740 // interface.
1742 Btype*
1743 tree_to_type(tree t)
1745 return new Btype(t);
1748 Bexpression*
1749 tree_to_expr(tree t)
1751 return new Bexpression(t);
1754 Bstatement*
1755 tree_to_stat(tree t)
1757 return new Bstatement(t);
1760 Bfunction*
1761 tree_to_function(tree t)
1763 return new Bfunction(t);
1766 Bblock*
1767 tree_to_block(tree t)
1769 gcc_assert(TREE_CODE(t) == BIND_EXPR);
1770 return new Bblock(t);
1773 tree
1774 type_to_tree(Btype* bt)
1776 return bt->get_tree();
1779 tree
1780 expr_to_tree(Bexpression* be)
1782 return be->get_tree();
1785 tree
1786 stat_to_tree(Bstatement* bs)
1788 return bs->get_tree();
1791 tree
1792 block_to_tree(Bblock* bb)
1794 return bb->get_tree();
1797 tree
1798 var_to_tree(Bvariable* bv)
1800 return bv->get_tree();