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
);
250 compound_expression(Bstatement
*, Bexpression
*, Location
);
253 conditional_expression(Btype
*, Bexpression
*, Bexpression
*, Bexpression
*,
257 unary_expression(Operator
, Bexpression
*, Location
);
260 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
266 { return this->make_statement(error_mark_node
); }
269 expression_statement(Bexpression
*);
272 init_statement(Bvariable
* var
, Bexpression
* init
);
275 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
278 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
282 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
286 switch_statement(Bexpression
* value
,
287 const std::vector
<std::vector
<Bexpression
*> >& cases
,
288 const std::vector
<Bstatement
*>& statements
,
292 compound_statement(Bstatement
*, Bstatement
*);
295 statement_list(const std::vector
<Bstatement
*>&);
300 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
304 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
307 block_statement(Bblock
*);
313 { return new Bvariable(error_mark_node
); }
316 global_variable(const std::string
& package_name
,
317 const std::string
& pkgpath
,
318 const std::string
& name
,
322 bool in_unique_section
,
326 global_variable_set_init(Bvariable
*, Bexpression
*);
329 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
333 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
337 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
338 Location
, Bstatement
**);
341 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
344 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
345 Location
, Bexpression
*);
348 immutable_struct_reference(const std::string
&, Btype
*, Location
);
353 label(Bfunction
*, const std::string
& name
, Location
);
356 label_definition_statement(Blabel
*);
359 goto_statement(Blabel
*, Location
);
362 label_address(Blabel
*, Location
);
368 { return this->make_function(error_mark_node
); }
371 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
372 bool is_visible
, bool is_declaration
, bool is_inlinable
,
373 bool disable_split_stack
, bool in_unique_section
, Location
);
376 // Make a Bexpression from a tree.
378 make_expression(tree t
)
379 { return new Bexpression(t
); }
381 // Make a Bstatement from a tree.
383 make_statement(tree t
)
384 { return new Bstatement(t
); }
386 // Make a Btype from a tree.
389 { return new Btype(t
); }
392 make_function(tree t
)
393 { return new Bfunction(t
); }
396 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
399 fill_in_array(Btype
*, Btype
*, Bexpression
*);
402 non_zero_size_type(tree
);
405 // A helper function.
408 get_identifier_from_string(const std::string
& str
)
410 return get_identifier_with_length(str
.data(), str
.length());
413 // Get an unnamed integer type.
416 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
421 if (bits
== INT_TYPE_SIZE
)
422 type
= unsigned_type_node
;
423 else if (bits
== CHAR_TYPE_SIZE
)
424 type
= unsigned_char_type_node
;
425 else if (bits
== SHORT_TYPE_SIZE
)
426 type
= short_unsigned_type_node
;
427 else if (bits
== LONG_TYPE_SIZE
)
428 type
= long_unsigned_type_node
;
429 else if (bits
== LONG_LONG_TYPE_SIZE
)
430 type
= long_long_unsigned_type_node
;
432 type
= make_unsigned_type(bits
);
436 if (bits
== INT_TYPE_SIZE
)
437 type
= integer_type_node
;
438 else if (bits
== CHAR_TYPE_SIZE
)
439 type
= signed_char_type_node
;
440 else if (bits
== SHORT_TYPE_SIZE
)
441 type
= short_integer_type_node
;
442 else if (bits
== LONG_TYPE_SIZE
)
443 type
= long_integer_type_node
;
444 else if (bits
== LONG_LONG_TYPE_SIZE
)
445 type
= long_long_integer_type_node
;
447 type
= make_signed_type(bits
);
449 return this->make_type(type
);
452 // Get an unnamed float type.
455 Gcc_backend::float_type(int bits
)
458 if (bits
== FLOAT_TYPE_SIZE
)
459 type
= float_type_node
;
460 else if (bits
== DOUBLE_TYPE_SIZE
)
461 type
= double_type_node
;
462 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
463 type
= long_double_type_node
;
466 type
= make_node(REAL_TYPE
);
467 TYPE_PRECISION(type
) = bits
;
470 return this->make_type(type
);
473 // Get an unnamed complex type.
476 Gcc_backend::complex_type(int bits
)
479 if (bits
== FLOAT_TYPE_SIZE
* 2)
480 type
= complex_float_type_node
;
481 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
482 type
= complex_double_type_node
;
483 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
484 type
= complex_long_double_type_node
;
487 type
= make_node(REAL_TYPE
);
488 TYPE_PRECISION(type
) = bits
/ 2;
490 type
= build_complex_type(type
);
492 return this->make_type(type
);
495 // Get a pointer type.
498 Gcc_backend::pointer_type(Btype
* to_type
)
500 tree to_type_tree
= to_type
->get_tree();
501 if (to_type_tree
== error_mark_node
)
502 return this->error_type();
503 tree type
= build_pointer_type(to_type_tree
);
504 return this->make_type(type
);
507 // Make a function type.
510 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
511 const std::vector
<Btyped_identifier
>& parameters
,
512 const std::vector
<Btyped_identifier
>& results
,
513 Btype
* result_struct
,
516 tree args
= NULL_TREE
;
518 if (receiver
.btype
!= NULL
)
520 tree t
= receiver
.btype
->get_tree();
521 if (t
== error_mark_node
)
522 return this->error_type();
523 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
524 pp
= &TREE_CHAIN(*pp
);
527 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
528 p
!= parameters
.end();
531 tree t
= p
->btype
->get_tree();
532 if (t
== error_mark_node
)
533 return this->error_type();
534 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
535 pp
= &TREE_CHAIN(*pp
);
538 // Varargs is handled entirely at the Go level. When converted to
539 // GENERIC functions are not varargs.
540 *pp
= void_list_node
;
544 result
= void_type_node
;
545 else if (results
.size() == 1)
546 result
= results
.front().btype
->get_tree();
549 gcc_assert(result_struct
!= NULL
);
550 result
= result_struct
->get_tree();
552 if (result
== error_mark_node
)
553 return this->error_type();
555 tree fntype
= build_function_type(result
, args
);
556 if (fntype
== error_mark_node
)
557 return this->error_type();
559 return this->make_type(build_pointer_type(fntype
));
562 // Make a struct type.
565 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
567 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
570 // Fill in the fields of a struct type.
573 Gcc_backend::fill_in_struct(Btype
* fill
,
574 const std::vector
<Btyped_identifier
>& fields
)
576 tree fill_tree
= fill
->get_tree();
577 tree field_trees
= NULL_TREE
;
578 tree
* pp
= &field_trees
;
579 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
583 tree name_tree
= get_identifier_from_string(p
->name
);
584 tree type_tree
= p
->btype
->get_tree();
585 if (type_tree
== error_mark_node
)
586 return this->error_type();
587 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
589 DECL_CONTEXT(field
) = fill_tree
;
591 pp
= &DECL_CHAIN(field
);
593 TYPE_FIELDS(fill_tree
) = field_trees
;
594 layout_type(fill_tree
);
598 // Make an array type.
601 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
603 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
604 element_btype
, length
);
607 // Fill in an array type.
610 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
613 tree element_type_tree
= element_type
->get_tree();
614 tree length_tree
= length
->get_tree();
615 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
616 return this->error_type();
618 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
620 length_tree
= fold_convert(sizetype
, length_tree
);
622 // build_index_type takes the maximum index, which is one less than
624 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
628 tree fill_tree
= fill
->get_tree();
629 TREE_TYPE(fill_tree
) = element_type_tree
;
630 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
631 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
632 layout_type(fill_tree
);
634 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
635 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
636 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
637 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
638 TYPE_CANONICAL(fill_tree
) =
639 build_array_type(TYPE_CANONICAL(element_type_tree
),
640 TYPE_CANONICAL(index_type_tree
));
645 // Create a placeholder for a pointer type.
648 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
649 Location location
, bool)
651 tree ret
= build_distinct_type_copy(ptr_type_node
);
654 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
655 get_identifier_from_string(name
),
657 TYPE_NAME(ret
) = decl
;
659 return this->make_type(ret
);
662 // Set the real target type for a placeholder pointer type.
665 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
668 tree pt
= placeholder
->get_tree();
669 if (pt
== error_mark_node
)
671 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
672 tree tt
= to_type
->get_tree();
673 if (tt
== error_mark_node
)
675 placeholder
->set_tree(error_mark_node
);
678 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
679 TREE_TYPE(pt
) = TREE_TYPE(tt
);
680 if (TYPE_NAME(pt
) != NULL_TREE
)
682 // Build the data structure gcc wants to see for a typedef.
683 tree copy
= build_variant_type_copy(pt
);
684 TYPE_NAME(copy
) = NULL_TREE
;
685 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
690 // Set the real values for a placeholder function type.
693 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
695 return this->set_placeholder_pointer_type(placeholder
, ft
);
698 // Create a placeholder for a struct type.
701 Gcc_backend::placeholder_struct_type(const std::string
& name
,
704 tree ret
= make_node(RECORD_TYPE
);
707 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
708 get_identifier_from_string(name
),
710 TYPE_NAME(ret
) = decl
;
712 return this->make_type(ret
);
715 // Fill in the fields of a placeholder struct type.
718 Gcc_backend::set_placeholder_struct_type(
720 const std::vector
<Btyped_identifier
>& fields
)
722 tree t
= placeholder
->get_tree();
723 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
724 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
726 if (TYPE_NAME(t
) != NULL_TREE
)
728 // Build the data structure gcc wants to see for a typedef.
729 tree copy
= build_distinct_type_copy(t
);
730 TYPE_NAME(copy
) = NULL_TREE
;
731 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
734 return r
->get_tree() != error_mark_node
;
737 // Create a placeholder for an array type.
740 Gcc_backend::placeholder_array_type(const std::string
& name
,
743 tree ret
= make_node(ARRAY_TYPE
);
744 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
745 get_identifier_from_string(name
),
747 TYPE_NAME(ret
) = decl
;
748 return this->make_type(ret
);
751 // Fill in the fields of a placeholder array type.
754 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
755 Btype
* element_btype
,
758 tree t
= placeholder
->get_tree();
759 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
760 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
762 // Build the data structure gcc wants to see for a typedef.
763 tree copy
= build_distinct_type_copy(t
);
764 TYPE_NAME(copy
) = NULL_TREE
;
765 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
767 return r
->get_tree() != error_mark_node
;
770 // Return a named version of a type.
773 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
776 tree type
= btype
->get_tree();
777 if (type
== error_mark_node
)
778 return this->error_type();
780 // The middle-end expects a basic type to have a name. In Go every
781 // basic type will have a name. The first time we see a basic type,
782 // give it whatever Go name we have at this point.
783 if (TYPE_NAME(type
) == NULL_TREE
784 && location
.gcc_location() == BUILTINS_LOCATION
785 && (TREE_CODE(type
) == INTEGER_TYPE
786 || TREE_CODE(type
) == REAL_TYPE
787 || TREE_CODE(type
) == COMPLEX_TYPE
788 || TREE_CODE(type
) == BOOLEAN_TYPE
))
790 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
791 get_identifier_from_string(name
),
793 TYPE_NAME(type
) = decl
;
794 return this->make_type(type
);
797 tree copy
= build_variant_type_copy(type
);
798 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
799 get_identifier_from_string(name
),
801 DECL_ORIGINAL_TYPE(decl
) = type
;
802 TYPE_NAME(copy
) = decl
;
803 return this->make_type(copy
);
806 // Return a pointer type used as a marker for a circular type.
809 Gcc_backend::circular_pointer_type(Btype
*, bool)
811 return this->make_type(ptr_type_node
);
814 // Return whether we might be looking at a circular type.
817 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
819 return btype
->get_tree() == ptr_type_node
;
822 // Return the size of a type.
825 Gcc_backend::type_size(Btype
* btype
)
827 tree t
= btype
->get_tree();
828 if (t
== error_mark_node
)
830 t
= TYPE_SIZE_UNIT(t
);
831 gcc_assert(TREE_CODE(t
) == INTEGER_CST
);
832 gcc_assert(TREE_INT_CST_HIGH(t
) == 0);
833 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
834 size_t ret
= static_cast<size_t>(val_wide
);
835 gcc_assert(ret
== val_wide
);
839 // Return the alignment of a type.
842 Gcc_backend::type_alignment(Btype
* btype
)
844 tree t
= btype
->get_tree();
845 if (t
== error_mark_node
)
847 return TYPE_ALIGN_UNIT(t
);
850 // Return the alignment of a struct field of type BTYPE.
853 Gcc_backend::type_field_alignment(Btype
* btype
)
855 tree t
= btype
->get_tree();
856 if (t
== error_mark_node
)
858 return go_field_alignment(t
);
861 // Return the offset of a field in a struct.
864 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
866 tree struct_tree
= btype
->get_tree();
867 if (struct_tree
== error_mark_node
)
869 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
870 tree field
= TYPE_FIELDS(struct_tree
);
871 for (; index
> 0; --index
)
873 field
= DECL_CHAIN(field
);
874 gcc_assert(field
!= NULL_TREE
);
876 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
877 gcc_assert(offset_wide
>= 0);
878 size_t ret
= static_cast<size_t>(offset_wide
);
879 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
883 // Return the zero value for a type.
886 Gcc_backend::zero_expression(Btype
* btype
)
888 tree t
= btype
->get_tree();
890 if (t
== error_mark_node
)
891 ret
= error_mark_node
;
893 ret
= build_zero_cst(t
);
894 return tree_to_expr(ret
);
897 // An expression that references a variable.
900 Gcc_backend::var_expression(Bvariable
* var
, Location
)
902 tree ret
= var
->get_tree();
903 if (ret
== error_mark_node
)
904 return this->error_expression();
905 return tree_to_expr(ret
);
908 // An expression that indirectly references an expression.
911 Gcc_backend::indirect_expression(Bexpression
* expr
, bool known_valid
,
914 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
917 TREE_THIS_NOTRAP(ret
) = 1;
918 return tree_to_expr(ret
);
921 // Return a typed value as a constant integer.
924 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
926 tree t
= btype
->get_tree();
927 if (t
== error_mark_node
)
928 return this->error_expression();
930 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
931 return tree_to_expr(ret
);
934 // Return a typed value as a constant floating-point number.
937 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
939 tree t
= btype
->get_tree();
941 if (t
== error_mark_node
)
942 return this->error_expression();
945 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
947 real_convert(&r2
, TYPE_MODE(t
), &r1
);
948 ret
= build_real(t
, r2
);
949 return tree_to_expr(ret
);
952 // Return a typed real and imaginary value as a constant complex number.
955 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
957 tree t
= btype
->get_tree();
959 if (t
== error_mark_node
)
960 return this->error_expression();
963 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
965 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
968 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
970 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
972 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
973 build_real(TREE_TYPE(t
), r4
));
974 return tree_to_expr(ret
);
977 // An expression that converts an expression to a different type.
980 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
, Location
)
982 tree type_tree
= type
->get_tree();
983 tree expr_tree
= expr
->get_tree();
984 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
985 return this->error_expression();
987 tree ret
= fold_convert(type_tree
, expr_tree
);
988 return tree_to_expr(ret
);
991 // Get the address of a function.
994 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
996 tree func
= bfunc
->get_tree();
997 if (func
== error_mark_node
)
998 return this->error_expression();
1000 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1001 return this->make_expression(ret
);
1004 // Get the address of an expression.
1007 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1009 tree expr
= bexpr
->get_tree();
1010 if (expr
== error_mark_node
)
1011 return this->error_expression();
1013 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1014 return this->make_expression(ret
);
1017 // Return an expression for the field at INDEX in BSTRUCT.
1020 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1023 tree struct_tree
= bstruct
->get_tree();
1024 if (struct_tree
== error_mark_node
1025 || TREE_TYPE(struct_tree
) == error_mark_node
)
1026 return this->error_expression();
1027 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1028 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1029 if (field
== NULL_TREE
)
1031 // This can happen for a type which refers to itself indirectly
1032 // and then turns out to be erroneous.
1033 return this->error_expression();
1035 for (unsigned int i
= index
; i
> 0; --i
)
1037 field
= DECL_CHAIN(field
);
1038 gcc_assert(field
!= NULL_TREE
);
1040 if (TREE_TYPE(field
) == error_mark_node
)
1041 return this->error_expression();
1042 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1043 TREE_TYPE(field
), struct_tree
, field
,
1045 if (TREE_CONSTANT(struct_tree
))
1046 TREE_CONSTANT(ret
) = 1;
1047 return tree_to_expr(ret
);
1050 // Return an expression that executes BSTAT before BEXPR.
1053 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1056 tree stat
= bstat
->get_tree();
1057 tree expr
= bexpr
->get_tree();
1058 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1059 return this->error_expression();
1060 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1061 TREE_TYPE(expr
), stat
, expr
);
1062 return this->make_expression(ret
);
1065 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1066 // ELSE_EXPR otherwise.
1069 Gcc_backend::conditional_expression(Btype
* btype
, Bexpression
* condition
,
1070 Bexpression
* then_expr
,
1071 Bexpression
* else_expr
, Location location
)
1073 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1074 tree cond_tree
= condition
->get_tree();
1075 tree then_tree
= then_expr
->get_tree();
1076 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1077 if (type_tree
== error_mark_node
1078 || cond_tree
== error_mark_node
1079 || then_tree
== error_mark_node
1080 || else_tree
== error_mark_node
)
1081 return this->error_expression();
1082 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1083 cond_tree
, then_tree
, else_tree
);
1084 return this->make_expression(ret
);
1087 // Return an expression for the unary operation OP EXPR.
1090 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1092 tree expr_tree
= expr
->get_tree();
1093 if (expr_tree
== error_mark_node
1094 || TREE_TYPE(expr_tree
) == error_mark_node
)
1095 return this->error_expression();
1097 tree type_tree
= TREE_TYPE(expr_tree
);
1098 enum tree_code code
;
1101 case OPERATOR_MINUS
:
1103 tree computed_type
= excess_precision_type(type_tree
);
1104 if (computed_type
!= NULL_TREE
)
1106 expr_tree
= convert(computed_type
, expr_tree
);
1107 type_tree
= computed_type
;
1113 code
= TRUTH_NOT_EXPR
;
1116 code
= BIT_NOT_EXPR
;
1123 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1125 return this->make_expression(ret
);
1128 // Convert a gofrontend operator to an equivalent tree_code.
1130 static enum tree_code
1131 operator_to_tree_code(Operator op
, tree type
)
1133 enum tree_code code
;
1139 case OPERATOR_NOTEQ
:
1155 code
= TRUTH_ORIF_EXPR
;
1157 case OPERATOR_ANDAND
:
1158 code
= TRUTH_ANDIF_EXPR
;
1163 case OPERATOR_MINUS
:
1167 code
= BIT_IOR_EXPR
;
1170 code
= BIT_XOR_EXPR
;
1176 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1179 code
= TRUNC_DIV_EXPR
;
1182 code
= TRUNC_MOD_EXPR
;
1184 case OPERATOR_LSHIFT
:
1187 case OPERATOR_RSHIFT
:
1191 code
= BIT_AND_EXPR
;
1193 case OPERATOR_BITCLEAR
:
1194 code
= BIT_AND_EXPR
;
1203 // Return an expression for the binary operation LEFT OP RIGHT.
1206 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1207 Bexpression
* right
, Location location
)
1209 tree left_tree
= left
->get_tree();
1210 tree right_tree
= right
->get_tree();
1211 if (left_tree
== error_mark_node
1212 || right_tree
== error_mark_node
)
1213 return this->error_expression();
1214 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1216 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1217 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1218 tree computed_type
= excess_precision_type(type_tree
);
1219 if (computed_type
!= NULL_TREE
)
1221 left_tree
= convert(computed_type
, left_tree
);
1222 right_tree
= convert(computed_type
, right_tree
);
1223 type_tree
= computed_type
;
1226 // For comparison operators, the resulting type should be boolean.
1230 case OPERATOR_NOTEQ
:
1235 type_tree
= boolean_type_node
;
1241 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1242 left_tree
, right_tree
);
1243 return this->make_expression(ret
);
1246 // An expression as a statement.
1249 Gcc_backend::expression_statement(Bexpression
* expr
)
1251 return this->make_statement(expr
->get_tree());
1254 // Variable initialization.
1257 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1259 tree var_tree
= var
->get_tree();
1260 tree init_tree
= init
->get_tree();
1261 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1262 return this->error_statement();
1263 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1265 // To avoid problems with GNU ld, we don't make zero-sized
1266 // externally visible variables. That might lead us to doing an
1267 // initialization of a zero-sized expression to a non-zero sized
1268 // variable, or vice-versa. Avoid crashes by omitting the
1269 // initializer. Such initializations don't mean anything anyhow.
1270 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1271 && init_tree
!= NULL_TREE
1272 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1274 DECL_INITIAL(var_tree
) = init_tree
;
1275 init_tree
= NULL_TREE
;
1278 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1279 void_type_node
, var_tree
);
1280 if (init_tree
!= NULL_TREE
)
1281 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1282 void_type_node
, init_tree
, ret
);
1284 return this->make_statement(ret
);
1290 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1293 tree lhs_tree
= lhs
->get_tree();
1294 tree rhs_tree
= rhs
->get_tree();
1295 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1296 return this->error_statement();
1298 // To avoid problems with GNU ld, we don't make zero-sized
1299 // externally visible variables. That might lead us to doing an
1300 // assignment of a zero-sized expression to a non-zero sized
1301 // expression; avoid crashes here by avoiding assignments of
1302 // zero-sized expressions. Such assignments don't really mean
1304 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1305 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1306 return this->compound_statement(this->expression_statement(lhs
),
1307 this->expression_statement(rhs
));
1309 // Sometimes the same unnamed Go type can be created multiple times
1310 // and thus have multiple tree representations. Make sure this does
1311 // not confuse the middle-end.
1312 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1314 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1315 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1316 if (POINTER_TYPE_P(lhs_type_tree
)
1317 || INTEGRAL_TYPE_P(lhs_type_tree
)
1318 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1319 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1320 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1322 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1323 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1325 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1326 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1327 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1329 lhs_type_tree
, rhs_tree
);
1333 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1336 lhs_tree
, rhs_tree
));
1342 Gcc_backend::return_statement(Bfunction
* bfunction
,
1343 const std::vector
<Bexpression
*>& vals
,
1346 tree fntree
= bfunction
->get_tree();
1347 if (fntree
== error_mark_node
)
1348 return this->error_statement();
1349 tree result
= DECL_RESULT(fntree
);
1350 if (result
== error_mark_node
)
1351 return this->error_statement();
1354 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1356 else if (vals
.size() == 1)
1358 tree val
= vals
.front()->get_tree();
1359 if (val
== error_mark_node
)
1360 return this->error_statement();
1361 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1362 void_type_node
, result
,
1363 vals
.front()->get_tree());
1364 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1365 void_type_node
, set
);
1369 // To return multiple values, copy the values into a temporary
1370 // variable of the right structure type, and then assign the
1371 // temporary variable to the DECL_RESULT in the return
1373 tree stmt_list
= NULL_TREE
;
1374 tree rettype
= TREE_TYPE(result
);
1375 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1376 tree field
= TYPE_FIELDS(rettype
);
1377 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1379 p
++, field
= DECL_CHAIN(field
))
1381 gcc_assert(field
!= NULL_TREE
);
1382 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1383 TREE_TYPE(field
), rettmp
, field
,
1385 tree val
= (*p
)->get_tree();
1386 if (val
== error_mark_node
)
1387 return this->error_statement();
1388 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1390 ref
, (*p
)->get_tree());
1391 append_to_statement_list(set
, &stmt_list
);
1393 gcc_assert(field
== NULL_TREE
);
1394 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1397 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1398 void_type_node
, set
);
1399 append_to_statement_list(ret_expr
, &stmt_list
);
1402 return this->make_statement(ret
);
1408 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
1409 Bblock
* else_block
, Location location
)
1411 tree cond_tree
= condition
->get_tree();
1412 tree then_tree
= then_block
->get_tree();
1413 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
1414 if (cond_tree
== error_mark_node
1415 || then_tree
== error_mark_node
1416 || else_tree
== error_mark_node
)
1417 return this->error_statement();
1418 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
1419 cond_tree
, then_tree
, else_tree
);
1420 return this->make_statement(ret
);
1426 Gcc_backend::switch_statement(
1428 const std::vector
<std::vector
<Bexpression
*> >& cases
,
1429 const std::vector
<Bstatement
*>& statements
,
1430 Location switch_location
)
1432 gcc_assert(cases
.size() == statements
.size());
1434 tree stmt_list
= NULL_TREE
;
1435 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
1436 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
1437 ps
!= statements
.end();
1442 source_location loc
= (*ps
!= NULL
1443 ? EXPR_LOCATION((*ps
)->get_tree())
1444 : UNKNOWN_LOCATION
);
1445 tree label
= create_artificial_label(loc
);
1446 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
1447 append_to_statement_list(c
, &stmt_list
);
1451 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
1455 tree t
= (*pcv
)->get_tree();
1456 if (t
== error_mark_node
)
1457 return this->error_statement();
1458 source_location loc
= EXPR_LOCATION(t
);
1459 tree label
= create_artificial_label(loc
);
1460 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
1461 append_to_statement_list(c
, &stmt_list
);
1467 tree t
= (*ps
)->get_tree();
1468 if (t
== error_mark_node
)
1469 return this->error_statement();
1470 append_to_statement_list(t
, &stmt_list
);
1474 tree tv
= value
->get_tree();
1475 if (tv
== error_mark_node
)
1476 return this->error_statement();
1477 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
1478 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
1479 return this->make_statement(t
);
1482 // Pair of statements.
1485 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
1487 tree stmt_list
= NULL_TREE
;
1488 tree t
= s1
->get_tree();
1489 if (t
== error_mark_node
)
1490 return this->error_statement();
1491 append_to_statement_list(t
, &stmt_list
);
1493 if (t
== error_mark_node
)
1494 return this->error_statement();
1495 append_to_statement_list(t
, &stmt_list
);
1496 return this->make_statement(stmt_list
);
1499 // List of statements.
1502 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
1504 tree stmt_list
= NULL_TREE
;
1505 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1506 p
!= statements
.end();
1509 tree t
= (*p
)->get_tree();
1510 if (t
== error_mark_node
)
1511 return this->error_statement();
1512 append_to_statement_list(t
, &stmt_list
);
1514 return this->make_statement(stmt_list
);
1517 // Make a block. For some reason gcc uses a dual structure for
1518 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1519 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1523 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
1524 const std::vector
<Bvariable
*>& vars
,
1525 Location start_location
,
1528 tree block_tree
= make_node(BLOCK
);
1529 if (enclosing
== NULL
)
1531 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1532 // until we have a proper representation of the init function.
1534 if (function
== NULL
)
1535 fndecl
= current_function_decl
;
1537 fndecl
= function
->get_tree();
1538 gcc_assert(fndecl
!= NULL_TREE
);
1540 // We may have already created a block for local variables when
1541 // we take the address of a parameter.
1542 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1544 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
1545 DECL_INITIAL(fndecl
) = block_tree
;
1549 tree superblock_tree
= DECL_INITIAL(fndecl
);
1550 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1552 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1554 pp
= &BLOCK_CHAIN(*pp
))
1561 tree superbind_tree
= enclosing
->get_tree();
1562 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
1563 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
1565 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1567 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1569 pp
= &BLOCK_CHAIN(*pp
))
1574 tree
* pp
= &BLOCK_VARS(block_tree
);
1575 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
1579 *pp
= (*pv
)->get_tree();
1580 if (*pp
!= error_mark_node
)
1581 pp
= &DECL_CHAIN(*pp
);
1585 TREE_USED(block_tree
) = 1;
1587 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
1588 void_type_node
, BLOCK_VARS(block_tree
),
1589 NULL_TREE
, block_tree
);
1590 TREE_SIDE_EFFECTS(bind_tree
) = 1;
1592 return new Bblock(bind_tree
);
1595 // Add statements to a block.
1598 Gcc_backend::block_add_statements(Bblock
* bblock
,
1599 const std::vector
<Bstatement
*>& statements
)
1601 tree stmt_list
= NULL_TREE
;
1602 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1603 p
!= statements
.end();
1606 tree s
= (*p
)->get_tree();
1607 if (s
!= error_mark_node
)
1608 append_to_statement_list(s
, &stmt_list
);
1611 tree bind_tree
= bblock
->get_tree();
1612 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1613 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
1616 // Return a block as a statement.
1619 Gcc_backend::block_statement(Bblock
* bblock
)
1621 tree bind_tree
= bblock
->get_tree();
1622 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1623 return this->make_statement(bind_tree
);
1626 // This is not static because we declare it with GTY(()) in go-c.h.
1627 tree go_non_zero_struct
;
1629 // Return a type corresponding to TYPE with non-zero size.
1632 Gcc_backend::non_zero_size_type(tree type
)
1634 if (int_size_in_bytes(type
) != 0)
1637 switch (TREE_CODE(type
))
1640 if (TYPE_FIELDS(type
) != NULL_TREE
)
1642 tree ns
= make_node(RECORD_TYPE
);
1643 tree field_trees
= NULL_TREE
;
1644 tree
*pp
= &field_trees
;
1645 for (tree field
= TYPE_FIELDS(type
);
1647 field
= DECL_CHAIN(field
))
1649 tree ft
= TREE_TYPE(field
);
1650 if (field
== TYPE_FIELDS(type
))
1651 ft
= non_zero_size_type(ft
);
1652 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
1653 DECL_NAME(field
), ft
);
1654 DECL_CONTEXT(f
) = ns
;
1656 pp
= &DECL_CHAIN(f
);
1658 TYPE_FIELDS(ns
) = field_trees
;
1663 if (go_non_zero_struct
== NULL_TREE
)
1665 type
= make_node(RECORD_TYPE
);
1666 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
1667 get_identifier("dummy"),
1669 DECL_CONTEXT(field
) = type
;
1670 TYPE_FIELDS(type
) = field
;
1672 go_non_zero_struct
= type
;
1674 return go_non_zero_struct
;
1678 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
1679 return build_array_type_nelts(element_type
, 1);
1689 // Make a global variable.
1692 Gcc_backend::global_variable(const std::string
& package_name
,
1693 const std::string
& pkgpath
,
1694 const std::string
& name
,
1698 bool in_unique_section
,
1701 tree type_tree
= btype
->get_tree();
1702 if (type_tree
== error_mark_node
)
1703 return this->error_variable();
1705 // The GNU linker does not like dynamic variables with zero size.
1706 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
1707 type_tree
= this->non_zero_size_type(type_tree
);
1709 std::string
var_name(package_name
);
1710 var_name
.push_back('.');
1711 var_name
.append(name
);
1712 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1713 get_identifier_from_string(var_name
),
1716 DECL_EXTERNAL(decl
) = 1;
1718 TREE_STATIC(decl
) = 1;
1721 TREE_PUBLIC(decl
) = 1;
1723 std::string
asm_name(pkgpath
);
1724 asm_name
.push_back('.');
1725 asm_name
.append(name
);
1726 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1728 TREE_USED(decl
) = 1;
1730 if (in_unique_section
)
1731 resolve_unique_section (decl
, 0, 1);
1733 go_preserve_from_gc(decl
);
1735 return new Bvariable(decl
);
1738 // Set the initial value of a global variable.
1741 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
1743 tree expr_tree
= expr
->get_tree();
1744 if (expr_tree
== error_mark_node
)
1746 gcc_assert(TREE_CONSTANT(expr_tree
));
1747 tree var_decl
= var
->get_tree();
1748 if (var_decl
== error_mark_node
)
1750 DECL_INITIAL(var_decl
) = expr_tree
;
1752 // If this variable goes in a unique section, it may need to go into
1753 // a different one now that DECL_INITIAL is set.
1754 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl
))
1756 DECL_SECTION_NAME (var_decl
) = NULL_TREE
;
1757 resolve_unique_section (var_decl
,
1758 compute_reloc_for_constant (expr_tree
),
1763 // Make a local variable.
1766 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
1767 Btype
* btype
, bool is_address_taken
,
1770 tree type_tree
= btype
->get_tree();
1771 if (type_tree
== error_mark_node
)
1772 return this->error_variable();
1773 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1774 get_identifier_from_string(name
),
1776 DECL_CONTEXT(decl
) = function
->get_tree();
1777 TREE_USED(decl
) = 1;
1778 if (is_address_taken
)
1779 TREE_ADDRESSABLE(decl
) = 1;
1780 go_preserve_from_gc(decl
);
1781 return new Bvariable(decl
);
1784 // Make a function parameter variable.
1787 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
1788 Btype
* btype
, bool is_address_taken
,
1791 tree type_tree
= btype
->get_tree();
1792 if (type_tree
== error_mark_node
)
1793 return this->error_variable();
1794 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
1795 get_identifier_from_string(name
),
1797 DECL_CONTEXT(decl
) = function
->get_tree();
1798 DECL_ARG_TYPE(decl
) = type_tree
;
1799 TREE_USED(decl
) = 1;
1800 if (is_address_taken
)
1801 TREE_ADDRESSABLE(decl
) = 1;
1802 go_preserve_from_gc(decl
);
1803 return new Bvariable(decl
);
1806 // Make a temporary variable.
1809 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
1810 Btype
* btype
, Bexpression
* binit
,
1811 bool is_address_taken
,
1813 Bstatement
** pstatement
)
1815 tree type_tree
= btype
->get_tree();
1816 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
1817 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1819 *pstatement
= this->error_statement();
1820 return this->error_variable();
1824 // We can only use create_tmp_var if the type is not addressable.
1825 if (!TREE_ADDRESSABLE(type_tree
))
1826 var
= create_tmp_var(type_tree
, "GOTMP");
1829 gcc_assert(bblock
!= NULL
);
1830 var
= build_decl(location
.gcc_location(), VAR_DECL
,
1831 create_tmp_var_name("GOTMP"),
1833 DECL_ARTIFICIAL(var
) = 1;
1834 DECL_IGNORED_P(var
) = 1;
1836 // FIXME: Permitting function to be NULL here is a temporary
1837 // measure until we have a proper representation of the init
1839 if (function
!= NULL
)
1840 DECL_CONTEXT(var
) = function
->get_tree();
1843 gcc_assert(current_function_decl
!= NULL_TREE
);
1844 DECL_CONTEXT(var
) = current_function_decl
;
1847 // We have to add this variable to the BLOCK and the BIND_EXPR.
1848 tree bind_tree
= bblock
->get_tree();
1849 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1850 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
1851 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
1852 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
1853 BLOCK_VARS(block_tree
) = var
;
1854 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
1857 if (init_tree
!= NULL_TREE
)
1858 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
1861 if (is_address_taken
)
1862 TREE_ADDRESSABLE(var
) = 1;
1864 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
1866 void_type_node
, var
));
1867 return new Bvariable(var
);
1870 // Create a named immutable initialized data structure.
1873 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
1874 bool is_common
, Btype
* btype
, Location location
)
1876 tree type_tree
= btype
->get_tree();
1877 if (type_tree
== error_mark_node
)
1878 return this->error_variable();
1879 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1880 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1881 get_identifier_from_string(name
),
1882 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1883 TREE_STATIC(decl
) = 1;
1884 TREE_READONLY(decl
) = 1;
1885 TREE_CONSTANT(decl
) = 1;
1886 TREE_USED(decl
) = 1;
1887 DECL_ARTIFICIAL(decl
) = 1;
1889 TREE_PUBLIC(decl
) = 1;
1891 // When the initializer for one immutable_struct refers to another,
1892 // it needs to know the visibility of the referenced struct so that
1893 // compute_reloc_for_constant will return the right value. On many
1894 // systems calling make_decl_one_only will mark the decl as weak,
1895 // which will change the return value of compute_reloc_for_constant.
1896 // We can't reliably call make_decl_one_only yet, because we don't
1897 // yet know the initializer. This issue doesn't arise in C because
1898 // Go initializers, unlike C initializers, can be indirectly
1899 // recursive. To ensure that compute_reloc_for_constant computes
1900 // the right value if some other initializer refers to this one, we
1901 // mark this symbol as weak here. We undo that below in
1902 // immutable_struct_set_init before calling mark_decl_one_only.
1904 DECL_WEAK(decl
) = 1;
1906 // We don't call rest_of_decl_compilation until we have the
1909 go_preserve_from_gc(decl
);
1910 return new Bvariable(decl
);
1913 // Set the initializer for a variable created by immutable_struct.
1914 // This is where we finish compiling the variable.
1917 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
1918 bool, bool is_common
, Btype
*, Location
,
1919 Bexpression
* initializer
)
1921 tree decl
= var
->get_tree();
1922 tree init_tree
= initializer
->get_tree();
1923 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
1926 DECL_INITIAL(decl
) = init_tree
;
1928 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
1929 // See the comment where DECL_WEAK is set in immutable_struct.
1932 DECL_WEAK(decl
) = 0;
1933 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
1936 // These variables are often unneeded in the final program, so put
1937 // them in their own section so that linker GC can discard them.
1938 resolve_unique_section(decl
,
1939 compute_reloc_for_constant (init_tree
),
1942 rest_of_decl_compilation(decl
, 1, 0);
1945 // Return a reference to an immutable initialized data structure
1946 // defined in another package.
1949 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
1952 tree type_tree
= btype
->get_tree();
1953 if (type_tree
== error_mark_node
)
1954 return this->error_variable();
1955 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1956 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1957 get_identifier_from_string(name
),
1958 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1959 TREE_READONLY(decl
) = 1;
1960 TREE_CONSTANT(decl
) = 1;
1961 DECL_ARTIFICIAL(decl
) = 1;
1962 TREE_PUBLIC(decl
) = 1;
1963 DECL_EXTERNAL(decl
) = 1;
1964 go_preserve_from_gc(decl
);
1965 return new Bvariable(decl
);
1971 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
1976 decl
= create_artificial_label(location
.gcc_location());
1979 tree id
= get_identifier_from_string(name
);
1980 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
1982 DECL_CONTEXT(decl
) = function
->get_tree();
1984 return new Blabel(decl
);
1987 // Make a statement which defines a label.
1990 Gcc_backend::label_definition_statement(Blabel
* label
)
1992 tree lab
= label
->get_tree();
1993 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
1994 void_type_node
, lab
);
1995 return this->make_statement(ret
);
1998 // Make a goto statement.
2001 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
2003 tree lab
= label
->get_tree();
2004 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
2006 return this->make_statement(ret
);
2009 // Get the address of a label.
2012 Gcc_backend::label_address(Blabel
* label
, Location location
)
2014 tree lab
= label
->get_tree();
2016 TREE_ADDRESSABLE(lab
) = 1;
2017 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
2018 build_fold_addr_expr_loc(location
.gcc_location(),
2020 return this->make_expression(ret
);
2023 // Declare or define a new function.
2026 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
2027 const std::string
& asm_name
, bool is_visible
,
2028 bool is_declaration
, bool is_inlinable
,
2029 bool disable_split_stack
, bool in_unique_section
,
2032 tree functype
= fntype
->get_tree();
2033 if (functype
!= error_mark_node
)
2035 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
2036 functype
= TREE_TYPE(functype
);
2038 tree id
= get_identifier_from_string(name
);
2039 if (functype
== error_mark_node
|| id
== error_mark_node
)
2040 return this->error_function();
2042 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
2043 if (!asm_name
.empty())
2044 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2046 TREE_PUBLIC(decl
) = 1;
2048 DECL_EXTERNAL(decl
) = 1;
2051 tree restype
= TREE_TYPE(functype
);
2053 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
2054 DECL_ARTIFICIAL(resdecl
) = 1;
2055 DECL_IGNORED_P(resdecl
) = 1;
2056 DECL_CONTEXT(resdecl
) = decl
;
2057 DECL_RESULT(decl
) = resdecl
;
2060 DECL_UNINLINABLE(decl
) = 1;
2061 if (disable_split_stack
)
2063 tree attr
= get_identifier("__no_split_stack__");
2064 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
2066 if (in_unique_section
)
2067 resolve_unique_section(decl
, 0, 1);
2069 go_preserve_from_gc(decl
);
2070 return new Bfunction(decl
);
2073 // The single backend.
2075 static Gcc_backend gcc_backend
;
2077 // Return the backend generator.
2082 return &gcc_backend
;
2085 // FIXME: Temporary functions while converting to the new backend
2089 tree_to_type(tree t
)
2091 return new Btype(t
);
2095 tree_to_expr(tree t
)
2097 return new Bexpression(t
);
2101 tree_to_stat(tree t
)
2103 return new Bstatement(t
);
2107 tree_to_function(tree t
)
2109 return new Bfunction(t
);
2113 tree_to_block(tree t
)
2115 gcc_assert(TREE_CODE(t
) == BIND_EXPR
);
2116 return new Bblock(t
);
2120 type_to_tree(Btype
* bt
)
2122 return bt
->get_tree();
2126 expr_to_tree(Bexpression
* be
)
2128 return be
->get_tree();
2132 stat_to_tree(Bstatement
* bs
)
2134 return bs
->get_tree();
2138 block_to_tree(Bblock
* bb
)
2140 return bb
->get_tree();
2144 var_to_tree(Bvariable
* bv
)
2146 return bv
->get_tree();
2150 function_to_tree(Bfunction
* bf
)
2152 return bf
->get_tree();