1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2014 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
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
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.
28 #include "stringpool.h"
29 #include "stor-layout.h"
31 #include "tree-iterator.h"
32 #include "basic-block.h"
33 #include "gimple-expr.h"
44 // A class wrapping a tree.
65 // In gcc, types, expressions, and statements are all trees.
66 class Btype
: public Gcc_tree
74 class Bexpression
: public Gcc_tree
82 class Bstatement
: public Gcc_tree
90 class Bfunction
: public Gcc_tree
98 class Bblock
: public Gcc_tree
106 class Bvariable
: public Gcc_tree
114 class Blabel
: public Gcc_tree
122 // This file implements the interface between the Go frontend proper
123 // and the gcc IR. This implements specific instantiations of
124 // abstract classes defined by the Go frontend proper. The Go
125 // frontend proper class methods of these classes to generate the
126 // backend representation.
128 class Gcc_backend
: public Backend
135 { return this->make_type(error_mark_node
); }
139 { return this->make_type(void_type_node
); }
143 { return this->make_type(boolean_type_node
); }
146 integer_type(bool, int);
155 pointer_type(Btype
*);
158 function_type(const Btyped_identifier
&,
159 const std::vector
<Btyped_identifier
>&,
160 const std::vector
<Btyped_identifier
>&,
165 struct_type(const std::vector
<Btyped_identifier
>&);
168 array_type(Btype
*, Bexpression
*);
171 placeholder_pointer_type(const std::string
&, Location
, bool);
174 set_placeholder_pointer_type(Btype
*, Btype
*);
177 set_placeholder_function_type(Btype
*, Btype
*);
180 placeholder_struct_type(const std::string
&, Location
);
183 set_placeholder_struct_type(Btype
* placeholder
,
184 const std::vector
<Btyped_identifier
>&);
187 placeholder_array_type(const std::string
&, Location
);
190 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
193 named_type(const std::string
&, Btype
*, Location
);
196 circular_pointer_type(Btype
*, bool);
199 is_circular_pointer_type(Btype
*);
205 type_alignment(Btype
*);
208 type_field_alignment(Btype
*);
211 type_field_offset(Btype
*, size_t index
);
216 zero_expression(Btype
*);
220 { return this->make_expression(error_mark_node
); }
223 var_expression(Bvariable
* var
, Location
);
226 indirect_expression(Bexpression
* expr
, bool known_valid
, Location
);
229 integer_constant_expression(Btype
* btype
, mpz_t val
);
232 float_constant_expression(Btype
* btype
, mpfr_t val
);
235 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
238 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
241 function_code_expression(Bfunction
*, Location
);
244 address_expression(Bexpression
*, Location
);
247 struct_field_expression(Bexpression
*, size_t, Location
);
253 { return this->make_statement(error_mark_node
); }
256 expression_statement(Bexpression
*);
259 init_statement(Bvariable
* var
, Bexpression
* init
);
262 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
265 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
269 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
273 switch_statement(Bexpression
* value
,
274 const std::vector
<std::vector
<Bexpression
*> >& cases
,
275 const std::vector
<Bstatement
*>& statements
,
279 compound_statement(Bstatement
*, Bstatement
*);
282 statement_list(const std::vector
<Bstatement
*>&);
287 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
291 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
294 block_statement(Bblock
*);
300 { return new Bvariable(error_mark_node
); }
303 global_variable(const std::string
& package_name
,
304 const std::string
& pkgpath
,
305 const std::string
& name
,
309 bool in_unique_section
,
313 global_variable_set_init(Bvariable
*, Bexpression
*);
316 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
320 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
324 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
325 Location
, Bstatement
**);
328 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
331 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
332 Location
, Bexpression
*);
335 immutable_struct_reference(const std::string
&, Btype
*, Location
);
340 label(Bfunction
*, const std::string
& name
, Location
);
343 label_definition_statement(Blabel
*);
346 goto_statement(Blabel
*, Location
);
349 label_address(Blabel
*, Location
);
355 { return this->make_function(error_mark_node
); }
358 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
359 bool is_visible
, bool is_declaration
, bool is_inlinable
,
360 bool disable_split_stack
, bool in_unique_section
, Location
);
363 // Make a Bexpression from a tree.
365 make_expression(tree t
)
366 { return new Bexpression(t
); }
368 // Make a Bstatement from a tree.
370 make_statement(tree t
)
371 { return new Bstatement(t
); }
373 // Make a Btype from a tree.
376 { return new Btype(t
); }
379 make_function(tree t
)
380 { return new Bfunction(t
); }
383 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
386 fill_in_array(Btype
*, Btype
*, Bexpression
*);
389 non_zero_size_type(tree
);
392 // A helper function.
395 get_identifier_from_string(const std::string
& str
)
397 return get_identifier_with_length(str
.data(), str
.length());
400 // Get an unnamed integer type.
403 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
408 if (bits
== INT_TYPE_SIZE
)
409 type
= unsigned_type_node
;
410 else if (bits
== CHAR_TYPE_SIZE
)
411 type
= unsigned_char_type_node
;
412 else if (bits
== SHORT_TYPE_SIZE
)
413 type
= short_unsigned_type_node
;
414 else if (bits
== LONG_TYPE_SIZE
)
415 type
= long_unsigned_type_node
;
416 else if (bits
== LONG_LONG_TYPE_SIZE
)
417 type
= long_long_unsigned_type_node
;
419 type
= make_unsigned_type(bits
);
423 if (bits
== INT_TYPE_SIZE
)
424 type
= integer_type_node
;
425 else if (bits
== CHAR_TYPE_SIZE
)
426 type
= signed_char_type_node
;
427 else if (bits
== SHORT_TYPE_SIZE
)
428 type
= short_integer_type_node
;
429 else if (bits
== LONG_TYPE_SIZE
)
430 type
= long_integer_type_node
;
431 else if (bits
== LONG_LONG_TYPE_SIZE
)
432 type
= long_long_integer_type_node
;
434 type
= make_signed_type(bits
);
436 return this->make_type(type
);
439 // Get an unnamed float type.
442 Gcc_backend::float_type(int bits
)
445 if (bits
== FLOAT_TYPE_SIZE
)
446 type
= float_type_node
;
447 else if (bits
== DOUBLE_TYPE_SIZE
)
448 type
= double_type_node
;
449 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
450 type
= long_double_type_node
;
453 type
= make_node(REAL_TYPE
);
454 TYPE_PRECISION(type
) = bits
;
457 return this->make_type(type
);
460 // Get an unnamed complex type.
463 Gcc_backend::complex_type(int bits
)
466 if (bits
== FLOAT_TYPE_SIZE
* 2)
467 type
= complex_float_type_node
;
468 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
469 type
= complex_double_type_node
;
470 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
471 type
= complex_long_double_type_node
;
474 type
= make_node(REAL_TYPE
);
475 TYPE_PRECISION(type
) = bits
/ 2;
477 type
= build_complex_type(type
);
479 return this->make_type(type
);
482 // Get a pointer type.
485 Gcc_backend::pointer_type(Btype
* to_type
)
487 tree to_type_tree
= to_type
->get_tree();
488 if (to_type_tree
== error_mark_node
)
489 return this->error_type();
490 tree type
= build_pointer_type(to_type_tree
);
491 return this->make_type(type
);
494 // Make a function type.
497 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
498 const std::vector
<Btyped_identifier
>& parameters
,
499 const std::vector
<Btyped_identifier
>& results
,
500 Btype
* result_struct
,
503 tree args
= NULL_TREE
;
505 if (receiver
.btype
!= NULL
)
507 tree t
= receiver
.btype
->get_tree();
508 if (t
== error_mark_node
)
509 return this->error_type();
510 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
511 pp
= &TREE_CHAIN(*pp
);
514 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
515 p
!= parameters
.end();
518 tree t
= p
->btype
->get_tree();
519 if (t
== error_mark_node
)
520 return this->error_type();
521 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
522 pp
= &TREE_CHAIN(*pp
);
525 // Varargs is handled entirely at the Go level. When converted to
526 // GENERIC functions are not varargs.
527 *pp
= void_list_node
;
531 result
= void_type_node
;
532 else if (results
.size() == 1)
533 result
= results
.front().btype
->get_tree();
536 gcc_assert(result_struct
!= NULL
);
537 result
= result_struct
->get_tree();
539 if (result
== error_mark_node
)
540 return this->error_type();
542 tree fntype
= build_function_type(result
, args
);
543 if (fntype
== error_mark_node
)
544 return this->error_type();
546 return this->make_type(build_pointer_type(fntype
));
549 // Make a struct type.
552 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
554 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
557 // Fill in the fields of a struct type.
560 Gcc_backend::fill_in_struct(Btype
* fill
,
561 const std::vector
<Btyped_identifier
>& fields
)
563 tree fill_tree
= fill
->get_tree();
564 tree field_trees
= NULL_TREE
;
565 tree
* pp
= &field_trees
;
566 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
570 tree name_tree
= get_identifier_from_string(p
->name
);
571 tree type_tree
= p
->btype
->get_tree();
572 if (type_tree
== error_mark_node
)
573 return this->error_type();
574 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
576 DECL_CONTEXT(field
) = fill_tree
;
578 pp
= &DECL_CHAIN(field
);
580 TYPE_FIELDS(fill_tree
) = field_trees
;
581 layout_type(fill_tree
);
585 // Make an array type.
588 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
590 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
591 element_btype
, length
);
594 // Fill in an array type.
597 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
600 tree element_type_tree
= element_type
->get_tree();
601 tree length_tree
= length
->get_tree();
602 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
603 return this->error_type();
605 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
607 length_tree
= fold_convert(sizetype
, length_tree
);
609 // build_index_type takes the maximum index, which is one less than
611 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
615 tree fill_tree
= fill
->get_tree();
616 TREE_TYPE(fill_tree
) = element_type_tree
;
617 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
618 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
619 layout_type(fill_tree
);
621 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
622 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
623 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
624 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
625 TYPE_CANONICAL(fill_tree
) =
626 build_array_type(TYPE_CANONICAL(element_type_tree
),
627 TYPE_CANONICAL(index_type_tree
));
632 // Create a placeholder for a pointer type.
635 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
636 Location location
, bool)
638 tree ret
= build_distinct_type_copy(ptr_type_node
);
641 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
642 get_identifier_from_string(name
),
644 TYPE_NAME(ret
) = decl
;
646 return this->make_type(ret
);
649 // Set the real target type for a placeholder pointer type.
652 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
655 tree pt
= placeholder
->get_tree();
656 if (pt
== error_mark_node
)
658 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
659 tree tt
= to_type
->get_tree();
660 if (tt
== error_mark_node
)
662 placeholder
->set_tree(error_mark_node
);
665 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
666 TREE_TYPE(pt
) = TREE_TYPE(tt
);
667 if (TYPE_NAME(pt
) != NULL_TREE
)
669 // Build the data structure gcc wants to see for a typedef.
670 tree copy
= build_variant_type_copy(pt
);
671 TYPE_NAME(copy
) = NULL_TREE
;
672 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
677 // Set the real values for a placeholder function type.
680 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
682 return this->set_placeholder_pointer_type(placeholder
, ft
);
685 // Create a placeholder for a struct type.
688 Gcc_backend::placeholder_struct_type(const std::string
& name
,
691 tree ret
= make_node(RECORD_TYPE
);
694 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
695 get_identifier_from_string(name
),
697 TYPE_NAME(ret
) = decl
;
699 return this->make_type(ret
);
702 // Fill in the fields of a placeholder struct type.
705 Gcc_backend::set_placeholder_struct_type(
707 const std::vector
<Btyped_identifier
>& fields
)
709 tree t
= placeholder
->get_tree();
710 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
711 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
713 if (TYPE_NAME(t
) != NULL_TREE
)
715 // Build the data structure gcc wants to see for a typedef.
716 tree copy
= build_distinct_type_copy(t
);
717 TYPE_NAME(copy
) = NULL_TREE
;
718 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
721 return r
->get_tree() != error_mark_node
;
724 // Create a placeholder for an array type.
727 Gcc_backend::placeholder_array_type(const std::string
& name
,
730 tree ret
= make_node(ARRAY_TYPE
);
731 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
732 get_identifier_from_string(name
),
734 TYPE_NAME(ret
) = decl
;
735 return this->make_type(ret
);
738 // Fill in the fields of a placeholder array type.
741 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
742 Btype
* element_btype
,
745 tree t
= placeholder
->get_tree();
746 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
747 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
749 // Build the data structure gcc wants to see for a typedef.
750 tree copy
= build_distinct_type_copy(t
);
751 TYPE_NAME(copy
) = NULL_TREE
;
752 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
754 return r
->get_tree() != error_mark_node
;
757 // Return a named version of a type.
760 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
763 tree type
= btype
->get_tree();
764 if (type
== error_mark_node
)
765 return this->error_type();
767 // The middle-end expects a basic type to have a name. In Go every
768 // basic type will have a name. The first time we see a basic type,
769 // give it whatever Go name we have at this point.
770 if (TYPE_NAME(type
) == NULL_TREE
771 && location
.gcc_location() == BUILTINS_LOCATION
772 && (TREE_CODE(type
) == INTEGER_TYPE
773 || TREE_CODE(type
) == REAL_TYPE
774 || TREE_CODE(type
) == COMPLEX_TYPE
775 || TREE_CODE(type
) == BOOLEAN_TYPE
))
777 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
778 get_identifier_from_string(name
),
780 TYPE_NAME(type
) = decl
;
781 return this->make_type(type
);
784 tree copy
= build_variant_type_copy(type
);
785 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
786 get_identifier_from_string(name
),
788 DECL_ORIGINAL_TYPE(decl
) = type
;
789 TYPE_NAME(copy
) = decl
;
790 return this->make_type(copy
);
793 // Return a pointer type used as a marker for a circular type.
796 Gcc_backend::circular_pointer_type(Btype
*, bool)
798 return this->make_type(ptr_type_node
);
801 // Return whether we might be looking at a circular type.
804 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
806 return btype
->get_tree() == ptr_type_node
;
809 // Return the size of a type.
812 Gcc_backend::type_size(Btype
* btype
)
814 tree t
= btype
->get_tree();
815 if (t
== error_mark_node
)
817 t
= TYPE_SIZE_UNIT(t
);
818 gcc_assert(TREE_CODE(t
) == INTEGER_CST
);
819 gcc_assert(TREE_INT_CST_HIGH(t
) == 0);
820 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
821 size_t ret
= static_cast<size_t>(val_wide
);
822 gcc_assert(ret
== val_wide
);
826 // Return the alignment of a type.
829 Gcc_backend::type_alignment(Btype
* btype
)
831 tree t
= btype
->get_tree();
832 if (t
== error_mark_node
)
834 return TYPE_ALIGN_UNIT(t
);
837 // Return the alignment of a struct field of type BTYPE.
840 Gcc_backend::type_field_alignment(Btype
* btype
)
842 tree t
= btype
->get_tree();
843 if (t
== error_mark_node
)
845 return go_field_alignment(t
);
848 // Return the offset of a field in a struct.
851 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
853 tree struct_tree
= btype
->get_tree();
854 if (struct_tree
== error_mark_node
)
856 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
857 tree field
= TYPE_FIELDS(struct_tree
);
858 for (; index
> 0; --index
)
860 field
= DECL_CHAIN(field
);
861 gcc_assert(field
!= NULL_TREE
);
863 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
864 gcc_assert(offset_wide
>= 0);
865 size_t ret
= static_cast<size_t>(offset_wide
);
866 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
870 // Return the zero value for a type.
873 Gcc_backend::zero_expression(Btype
* btype
)
875 tree t
= btype
->get_tree();
877 if (t
== error_mark_node
)
878 ret
= error_mark_node
;
880 ret
= build_zero_cst(t
);
881 return tree_to_expr(ret
);
884 // An expression that references a variable.
887 Gcc_backend::var_expression(Bvariable
* var
, Location
)
889 tree ret
= var
->get_tree();
890 if (ret
== error_mark_node
)
891 return this->error_expression();
892 return tree_to_expr(ret
);
895 // An expression that indirectly references an expression.
898 Gcc_backend::indirect_expression(Bexpression
* expr
, bool known_valid
,
901 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
904 TREE_THIS_NOTRAP(ret
) = 1;
905 return tree_to_expr(ret
);
908 // Return a typed value as a constant integer.
911 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
913 tree t
= btype
->get_tree();
914 if (t
== error_mark_node
)
915 return this->error_expression();
917 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
918 return tree_to_expr(ret
);
921 // Return a typed value as a constant floating-point number.
924 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
926 tree t
= btype
->get_tree();
928 if (t
== error_mark_node
)
929 return this->error_expression();
932 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
934 real_convert(&r2
, TYPE_MODE(t
), &r1
);
935 ret
= build_real(t
, r2
);
936 return tree_to_expr(ret
);
939 // Return a typed real and imaginary value as a constant complex number.
942 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
944 tree t
= btype
->get_tree();
946 if (t
== error_mark_node
)
947 return this->error_expression();
950 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
952 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
955 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
957 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
959 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
960 build_real(TREE_TYPE(t
), r4
));
961 return tree_to_expr(ret
);
964 // An expression that converts an expression to a different type.
967 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
, Location
)
969 tree type_tree
= type
->get_tree();
970 tree expr_tree
= expr
->get_tree();
971 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
972 return this->error_expression();
974 tree ret
= fold_convert(type_tree
, expr_tree
);
975 return tree_to_expr(ret
);
978 // Get the address of a function.
981 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
983 tree func
= bfunc
->get_tree();
984 if (func
== error_mark_node
)
985 return this->error_expression();
987 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
988 return this->make_expression(ret
);
991 // Get the address of an expression.
994 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
996 tree expr
= bexpr
->get_tree();
997 if (expr
== error_mark_node
)
998 return this->error_expression();
1000 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1001 return this->make_expression(ret
);
1004 // Return an expression for the field at INDEX in BSTRUCT.
1007 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1010 tree struct_tree
= bstruct
->get_tree();
1011 if (struct_tree
== error_mark_node
1012 || TREE_TYPE(struct_tree
) == error_mark_node
)
1013 return this->error_expression();
1014 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1015 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1016 if (field
== NULL_TREE
)
1018 // This can happen for a type which refers to itself indirectly
1019 // and then turns out to be erroneous.
1020 return this->error_expression();
1022 for (unsigned int i
= index
; i
> 0; --i
)
1024 field
= DECL_CHAIN(field
);
1025 gcc_assert(field
!= NULL_TREE
);
1027 if (TREE_TYPE(field
) == error_mark_node
)
1028 return this->error_expression();
1029 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1030 TREE_TYPE(field
), struct_tree
, field
,
1032 if (TREE_CONSTANT(struct_tree
))
1033 TREE_CONSTANT(ret
) = 1;
1034 return tree_to_expr(ret
);
1037 // An expression as a statement.
1040 Gcc_backend::expression_statement(Bexpression
* expr
)
1042 return this->make_statement(expr
->get_tree());
1045 // Variable initialization.
1048 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1050 tree var_tree
= var
->get_tree();
1051 tree init_tree
= init
->get_tree();
1052 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1053 return this->error_statement();
1054 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1056 // To avoid problems with GNU ld, we don't make zero-sized
1057 // externally visible variables. That might lead us to doing an
1058 // initialization of a zero-sized expression to a non-zero sized
1059 // variable, or vice-versa. Avoid crashes by omitting the
1060 // initializer. Such initializations don't mean anything anyhow.
1061 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1062 && init_tree
!= NULL_TREE
1063 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1065 DECL_INITIAL(var_tree
) = init_tree
;
1066 init_tree
= NULL_TREE
;
1069 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1070 void_type_node
, var_tree
);
1071 if (init_tree
!= NULL_TREE
)
1072 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1073 void_type_node
, init_tree
, ret
);
1075 return this->make_statement(ret
);
1081 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1084 tree lhs_tree
= lhs
->get_tree();
1085 tree rhs_tree
= rhs
->get_tree();
1086 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1087 return this->error_statement();
1089 // To avoid problems with GNU ld, we don't make zero-sized
1090 // externally visible variables. That might lead us to doing an
1091 // assignment of a zero-sized expression to a non-zero sized
1092 // expression; avoid crashes here by avoiding assignments of
1093 // zero-sized expressions. Such assignments don't really mean
1095 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1096 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1097 return this->compound_statement(this->expression_statement(lhs
),
1098 this->expression_statement(rhs
));
1100 // Sometimes the same unnamed Go type can be created multiple times
1101 // and thus have multiple tree representations. Make sure this does
1102 // not confuse the middle-end.
1103 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1105 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1106 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1107 if (POINTER_TYPE_P(lhs_type_tree
)
1108 || INTEGRAL_TYPE_P(lhs_type_tree
)
1109 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1110 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1111 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1113 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1114 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1116 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1117 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1118 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1120 lhs_type_tree
, rhs_tree
);
1124 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1127 lhs_tree
, rhs_tree
));
1133 Gcc_backend::return_statement(Bfunction
* bfunction
,
1134 const std::vector
<Bexpression
*>& vals
,
1137 tree fntree
= bfunction
->get_tree();
1138 if (fntree
== error_mark_node
)
1139 return this->error_statement();
1140 tree result
= DECL_RESULT(fntree
);
1141 if (result
== error_mark_node
)
1142 return this->error_statement();
1145 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1147 else if (vals
.size() == 1)
1149 tree val
= vals
.front()->get_tree();
1150 if (val
== error_mark_node
)
1151 return this->error_statement();
1152 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1153 void_type_node
, result
,
1154 vals
.front()->get_tree());
1155 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1156 void_type_node
, set
);
1160 // To return multiple values, copy the values into a temporary
1161 // variable of the right structure type, and then assign the
1162 // temporary variable to the DECL_RESULT in the return
1164 tree stmt_list
= NULL_TREE
;
1165 tree rettype
= TREE_TYPE(result
);
1166 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1167 tree field
= TYPE_FIELDS(rettype
);
1168 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1170 p
++, field
= DECL_CHAIN(field
))
1172 gcc_assert(field
!= NULL_TREE
);
1173 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1174 TREE_TYPE(field
), rettmp
, field
,
1176 tree val
= (*p
)->get_tree();
1177 if (val
== error_mark_node
)
1178 return this->error_statement();
1179 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1181 ref
, (*p
)->get_tree());
1182 append_to_statement_list(set
, &stmt_list
);
1184 gcc_assert(field
== NULL_TREE
);
1185 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1188 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1189 void_type_node
, set
);
1190 append_to_statement_list(ret_expr
, &stmt_list
);
1193 return this->make_statement(ret
);
1199 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
1200 Bblock
* else_block
, Location location
)
1202 tree cond_tree
= condition
->get_tree();
1203 tree then_tree
= then_block
->get_tree();
1204 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
1205 if (cond_tree
== error_mark_node
1206 || then_tree
== error_mark_node
1207 || else_tree
== error_mark_node
)
1208 return this->error_statement();
1209 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
1210 cond_tree
, then_tree
, else_tree
);
1211 return this->make_statement(ret
);
1217 Gcc_backend::switch_statement(
1219 const std::vector
<std::vector
<Bexpression
*> >& cases
,
1220 const std::vector
<Bstatement
*>& statements
,
1221 Location switch_location
)
1223 gcc_assert(cases
.size() == statements
.size());
1225 tree stmt_list
= NULL_TREE
;
1226 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
1227 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
1228 ps
!= statements
.end();
1233 source_location loc
= (*ps
!= NULL
1234 ? EXPR_LOCATION((*ps
)->get_tree())
1235 : UNKNOWN_LOCATION
);
1236 tree label
= create_artificial_label(loc
);
1237 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
1238 append_to_statement_list(c
, &stmt_list
);
1242 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
1246 tree t
= (*pcv
)->get_tree();
1247 if (t
== error_mark_node
)
1248 return this->error_statement();
1249 source_location loc
= EXPR_LOCATION(t
);
1250 tree label
= create_artificial_label(loc
);
1251 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
1252 append_to_statement_list(c
, &stmt_list
);
1258 tree t
= (*ps
)->get_tree();
1259 if (t
== error_mark_node
)
1260 return this->error_statement();
1261 append_to_statement_list(t
, &stmt_list
);
1265 tree tv
= value
->get_tree();
1266 if (tv
== error_mark_node
)
1267 return this->error_statement();
1268 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
1269 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
1270 return this->make_statement(t
);
1273 // Pair of statements.
1276 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
1278 tree stmt_list
= NULL_TREE
;
1279 tree t
= s1
->get_tree();
1280 if (t
== error_mark_node
)
1281 return this->error_statement();
1282 append_to_statement_list(t
, &stmt_list
);
1284 if (t
== error_mark_node
)
1285 return this->error_statement();
1286 append_to_statement_list(t
, &stmt_list
);
1287 return this->make_statement(stmt_list
);
1290 // List of statements.
1293 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
1295 tree stmt_list
= NULL_TREE
;
1296 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1297 p
!= statements
.end();
1300 tree t
= (*p
)->get_tree();
1301 if (t
== error_mark_node
)
1302 return this->error_statement();
1303 append_to_statement_list(t
, &stmt_list
);
1305 return this->make_statement(stmt_list
);
1308 // Make a block. For some reason gcc uses a dual structure for
1309 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1310 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1314 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
1315 const std::vector
<Bvariable
*>& vars
,
1316 Location start_location
,
1319 tree block_tree
= make_node(BLOCK
);
1320 if (enclosing
== NULL
)
1322 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1323 // until we have a proper representation of the init function.
1325 if (function
== NULL
)
1326 fndecl
= current_function_decl
;
1328 fndecl
= function
->get_tree();
1329 gcc_assert(fndecl
!= NULL_TREE
);
1331 // We may have already created a block for local variables when
1332 // we take the address of a parameter.
1333 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1335 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
1336 DECL_INITIAL(fndecl
) = block_tree
;
1340 tree superblock_tree
= DECL_INITIAL(fndecl
);
1341 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1343 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1345 pp
= &BLOCK_CHAIN(*pp
))
1352 tree superbind_tree
= enclosing
->get_tree();
1353 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
1354 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
1356 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1358 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1360 pp
= &BLOCK_CHAIN(*pp
))
1365 tree
* pp
= &BLOCK_VARS(block_tree
);
1366 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
1370 *pp
= (*pv
)->get_tree();
1371 if (*pp
!= error_mark_node
)
1372 pp
= &DECL_CHAIN(*pp
);
1376 TREE_USED(block_tree
) = 1;
1378 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
1379 void_type_node
, BLOCK_VARS(block_tree
),
1380 NULL_TREE
, block_tree
);
1381 TREE_SIDE_EFFECTS(bind_tree
) = 1;
1383 return new Bblock(bind_tree
);
1386 // Add statements to a block.
1389 Gcc_backend::block_add_statements(Bblock
* bblock
,
1390 const std::vector
<Bstatement
*>& statements
)
1392 tree stmt_list
= NULL_TREE
;
1393 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1394 p
!= statements
.end();
1397 tree s
= (*p
)->get_tree();
1398 if (s
!= error_mark_node
)
1399 append_to_statement_list(s
, &stmt_list
);
1402 tree bind_tree
= bblock
->get_tree();
1403 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1404 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
1407 // Return a block as a statement.
1410 Gcc_backend::block_statement(Bblock
* bblock
)
1412 tree bind_tree
= bblock
->get_tree();
1413 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1414 return this->make_statement(bind_tree
);
1417 // This is not static because we declare it with GTY(()) in go-c.h.
1418 tree go_non_zero_struct
;
1420 // Return a type corresponding to TYPE with non-zero size.
1423 Gcc_backend::non_zero_size_type(tree type
)
1425 if (int_size_in_bytes(type
) != 0)
1428 switch (TREE_CODE(type
))
1431 if (TYPE_FIELDS(type
) != NULL_TREE
)
1433 tree ns
= make_node(RECORD_TYPE
);
1434 tree field_trees
= NULL_TREE
;
1435 tree
*pp
= &field_trees
;
1436 for (tree field
= TYPE_FIELDS(type
);
1438 field
= DECL_CHAIN(field
))
1440 tree ft
= TREE_TYPE(field
);
1441 if (field
== TYPE_FIELDS(type
))
1442 ft
= non_zero_size_type(ft
);
1443 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
1444 DECL_NAME(field
), ft
);
1445 DECL_CONTEXT(f
) = ns
;
1447 pp
= &DECL_CHAIN(f
);
1449 TYPE_FIELDS(ns
) = field_trees
;
1454 if (go_non_zero_struct
== NULL_TREE
)
1456 type
= make_node(RECORD_TYPE
);
1457 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
1458 get_identifier("dummy"),
1460 DECL_CONTEXT(field
) = type
;
1461 TYPE_FIELDS(type
) = field
;
1463 go_non_zero_struct
= type
;
1465 return go_non_zero_struct
;
1469 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
1470 return build_array_type_nelts(element_type
, 1);
1480 // Make a global variable.
1483 Gcc_backend::global_variable(const std::string
& package_name
,
1484 const std::string
& pkgpath
,
1485 const std::string
& name
,
1489 bool in_unique_section
,
1492 tree type_tree
= btype
->get_tree();
1493 if (type_tree
== error_mark_node
)
1494 return this->error_variable();
1496 // The GNU linker does not like dynamic variables with zero size.
1497 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
1498 type_tree
= this->non_zero_size_type(type_tree
);
1500 std::string
var_name(package_name
);
1501 var_name
.push_back('.');
1502 var_name
.append(name
);
1503 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1504 get_identifier_from_string(var_name
),
1507 DECL_EXTERNAL(decl
) = 1;
1509 TREE_STATIC(decl
) = 1;
1512 TREE_PUBLIC(decl
) = 1;
1514 std::string
asm_name(pkgpath
);
1515 asm_name
.push_back('.');
1516 asm_name
.append(name
);
1517 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1519 TREE_USED(decl
) = 1;
1521 if (in_unique_section
)
1522 resolve_unique_section (decl
, 0, 1);
1524 go_preserve_from_gc(decl
);
1526 return new Bvariable(decl
);
1529 // Set the initial value of a global variable.
1532 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
1534 tree expr_tree
= expr
->get_tree();
1535 if (expr_tree
== error_mark_node
)
1537 gcc_assert(TREE_CONSTANT(expr_tree
));
1538 tree var_decl
= var
->get_tree();
1539 if (var_decl
== error_mark_node
)
1541 DECL_INITIAL(var_decl
) = expr_tree
;
1543 // If this variable goes in a unique section, it may need to go into
1544 // a different one now that DECL_INITIAL is set.
1545 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl
))
1547 DECL_SECTION_NAME (var_decl
) = NULL_TREE
;
1548 resolve_unique_section (var_decl
,
1549 compute_reloc_for_constant (expr_tree
),
1554 // Make a local variable.
1557 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
1558 Btype
* btype
, bool is_address_taken
,
1561 tree type_tree
= btype
->get_tree();
1562 if (type_tree
== error_mark_node
)
1563 return this->error_variable();
1564 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1565 get_identifier_from_string(name
),
1567 DECL_CONTEXT(decl
) = function
->get_tree();
1568 TREE_USED(decl
) = 1;
1569 if (is_address_taken
)
1570 TREE_ADDRESSABLE(decl
) = 1;
1571 go_preserve_from_gc(decl
);
1572 return new Bvariable(decl
);
1575 // Make a function parameter variable.
1578 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
1579 Btype
* btype
, bool is_address_taken
,
1582 tree type_tree
= btype
->get_tree();
1583 if (type_tree
== error_mark_node
)
1584 return this->error_variable();
1585 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
1586 get_identifier_from_string(name
),
1588 DECL_CONTEXT(decl
) = function
->get_tree();
1589 DECL_ARG_TYPE(decl
) = type_tree
;
1590 TREE_USED(decl
) = 1;
1591 if (is_address_taken
)
1592 TREE_ADDRESSABLE(decl
) = 1;
1593 go_preserve_from_gc(decl
);
1594 return new Bvariable(decl
);
1597 // Make a temporary variable.
1600 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
1601 Btype
* btype
, Bexpression
* binit
,
1602 bool is_address_taken
,
1604 Bstatement
** pstatement
)
1606 tree type_tree
= btype
->get_tree();
1607 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
1608 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1610 *pstatement
= this->error_statement();
1611 return this->error_variable();
1615 // We can only use create_tmp_var if the type is not addressable.
1616 if (!TREE_ADDRESSABLE(type_tree
))
1617 var
= create_tmp_var(type_tree
, "GOTMP");
1620 gcc_assert(bblock
!= NULL
);
1621 var
= build_decl(location
.gcc_location(), VAR_DECL
,
1622 create_tmp_var_name("GOTMP"),
1624 DECL_ARTIFICIAL(var
) = 1;
1625 DECL_IGNORED_P(var
) = 1;
1627 // FIXME: Permitting function to be NULL here is a temporary
1628 // measure until we have a proper representation of the init
1630 if (function
!= NULL
)
1631 DECL_CONTEXT(var
) = function
->get_tree();
1634 gcc_assert(current_function_decl
!= NULL_TREE
);
1635 DECL_CONTEXT(var
) = current_function_decl
;
1638 // We have to add this variable to the BLOCK and the BIND_EXPR.
1639 tree bind_tree
= bblock
->get_tree();
1640 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1641 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
1642 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
1643 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
1644 BLOCK_VARS(block_tree
) = var
;
1645 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
1648 if (init_tree
!= NULL_TREE
)
1649 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
1652 if (is_address_taken
)
1653 TREE_ADDRESSABLE(var
) = 1;
1655 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
1657 void_type_node
, var
));
1658 return new Bvariable(var
);
1661 // Create a named immutable initialized data structure.
1664 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
1665 bool, Btype
* btype
, Location location
)
1667 tree type_tree
= btype
->get_tree();
1668 if (type_tree
== error_mark_node
)
1669 return this->error_variable();
1670 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1671 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1672 get_identifier_from_string(name
),
1673 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1674 TREE_STATIC(decl
) = 1;
1675 TREE_READONLY(decl
) = 1;
1676 TREE_CONSTANT(decl
) = 1;
1677 TREE_USED(decl
) = 1;
1678 DECL_ARTIFICIAL(decl
) = 1;
1680 TREE_PUBLIC(decl
) = 1;
1682 // We don't call rest_of_decl_compilation until we have the
1685 go_preserve_from_gc(decl
);
1686 return new Bvariable(decl
);
1689 // Set the initializer for a variable created by immutable_struct.
1690 // This is where we finish compiling the variable.
1693 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
1694 bool, bool is_common
, Btype
*, Location
,
1695 Bexpression
* initializer
)
1697 tree decl
= var
->get_tree();
1698 tree init_tree
= initializer
->get_tree();
1699 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
1702 DECL_INITIAL(decl
) = init_tree
;
1704 // We can't call make_decl_one_only until we set DECL_INITIAL.
1706 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
1708 // These variables are often unneeded in the final program, so put
1709 // them in their own section so that linker GC can discard them.
1710 resolve_unique_section(decl
,
1711 compute_reloc_for_constant (init_tree
),
1714 rest_of_decl_compilation(decl
, 1, 0);
1717 // Return a reference to an immutable initialized data structure
1718 // defined in another package.
1721 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
1724 tree type_tree
= btype
->get_tree();
1725 if (type_tree
== error_mark_node
)
1726 return this->error_variable();
1727 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1728 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1729 get_identifier_from_string(name
),
1730 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1731 TREE_READONLY(decl
) = 1;
1732 TREE_CONSTANT(decl
) = 1;
1733 DECL_ARTIFICIAL(decl
) = 1;
1734 TREE_PUBLIC(decl
) = 1;
1735 DECL_EXTERNAL(decl
) = 1;
1736 go_preserve_from_gc(decl
);
1737 return new Bvariable(decl
);
1743 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
1748 decl
= create_artificial_label(location
.gcc_location());
1751 tree id
= get_identifier_from_string(name
);
1752 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
1754 DECL_CONTEXT(decl
) = function
->get_tree();
1756 return new Blabel(decl
);
1759 // Make a statement which defines a label.
1762 Gcc_backend::label_definition_statement(Blabel
* label
)
1764 tree lab
= label
->get_tree();
1765 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
1766 void_type_node
, lab
);
1767 return this->make_statement(ret
);
1770 // Make a goto statement.
1773 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
1775 tree lab
= label
->get_tree();
1776 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
1778 return this->make_statement(ret
);
1781 // Get the address of a label.
1784 Gcc_backend::label_address(Blabel
* label
, Location location
)
1786 tree lab
= label
->get_tree();
1788 TREE_ADDRESSABLE(lab
) = 1;
1789 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
1790 build_fold_addr_expr_loc(location
.gcc_location(),
1792 return this->make_expression(ret
);
1795 // Declare or define a new function.
1798 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
1799 const std::string
& asm_name
, bool is_visible
,
1800 bool is_declaration
, bool is_inlinable
,
1801 bool disable_split_stack
, bool in_unique_section
,
1804 tree functype
= fntype
->get_tree();
1805 if (functype
!= error_mark_node
)
1807 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
1808 functype
= TREE_TYPE(functype
);
1810 tree id
= get_identifier_from_string(name
);
1811 if (functype
== error_mark_node
|| id
== error_mark_node
)
1812 return this->error_function();
1814 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
1815 if (!asm_name
.empty())
1816 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1818 TREE_PUBLIC(decl
) = 1;
1820 DECL_EXTERNAL(decl
) = 1;
1823 tree restype
= TREE_TYPE(functype
);
1825 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
1826 DECL_ARTIFICIAL(resdecl
) = 1;
1827 DECL_IGNORED_P(resdecl
) = 1;
1828 DECL_CONTEXT(resdecl
) = decl
;
1829 DECL_RESULT(decl
) = resdecl
;
1832 DECL_UNINLINABLE(decl
) = 1;
1833 if (disable_split_stack
)
1835 tree attr
= get_identifier("__no_split_stack__");
1836 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1838 if (in_unique_section
)
1839 resolve_unique_section(decl
, 0, 1);
1841 go_preserve_from_gc(decl
);
1842 return new Bfunction(decl
);
1845 // The single backend.
1847 static Gcc_backend gcc_backend
;
1849 // Return the backend generator.
1854 return &gcc_backend
;
1857 // FIXME: Temporary functions while converting to the new backend
1861 tree_to_type(tree t
)
1863 return new Btype(t
);
1867 tree_to_expr(tree t
)
1869 return new Bexpression(t
);
1873 tree_to_stat(tree t
)
1875 return new Bstatement(t
);
1879 tree_to_function(tree t
)
1881 return new Bfunction(t
);
1885 tree_to_block(tree t
)
1887 gcc_assert(TREE_CODE(t
) == BIND_EXPR
);
1888 return new Bblock(t
);
1892 type_to_tree(Btype
* bt
)
1894 return bt
->get_tree();
1898 expr_to_tree(Bexpression
* be
)
1900 return be
->get_tree();
1904 stat_to_tree(Bstatement
* bs
)
1906 return bs
->get_tree();
1910 block_to_tree(Bblock
* bb
)
1912 return bb
->get_tree();
1916 var_to_tree(Bvariable
* bv
)
1918 return bv
->get_tree();
1922 function_to_tree(Bfunction
* bf
)
1924 return bf
->get_tree();