1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2013 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
5 // This file is part of GCC.
7 // GCC is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3, or (at your option) any later
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 "tree-iterator.h"
40 // A class wrapping a tree.
61 // In gcc, types, expressions, and statements are all trees.
62 class Btype
: public Gcc_tree
70 class Bexpression
: public Gcc_tree
78 class Bstatement
: public Gcc_tree
86 class Bfunction
: public Gcc_tree
94 class Bblock
: public Gcc_tree
102 class Bvariable
: public Gcc_tree
110 class Blabel
: public Gcc_tree
118 // This file implements the interface between the Go frontend proper
119 // and the gcc IR. This implements specific instantiations of
120 // abstract classes defined by the Go frontend proper. The Go
121 // frontend proper class methods of these classes to generate the
122 // backend representation.
124 class Gcc_backend
: public Backend
131 { return this->make_type(error_mark_node
); }
135 { return this->make_type(void_type_node
); }
139 { return this->make_type(boolean_type_node
); }
142 integer_type(bool, int);
151 pointer_type(Btype
*);
154 function_type(const Btyped_identifier
&,
155 const std::vector
<Btyped_identifier
>&,
156 const std::vector
<Btyped_identifier
>&,
160 struct_type(const std::vector
<Btyped_identifier
>&);
163 array_type(Btype
*, Bexpression
*);
166 placeholder_pointer_type(const std::string
&, Location
, bool);
169 set_placeholder_pointer_type(Btype
*, Btype
*);
172 set_placeholder_function_type(Btype
*, Btype
*);
175 placeholder_struct_type(const std::string
&, Location
);
178 set_placeholder_struct_type(Btype
* placeholder
,
179 const std::vector
<Btyped_identifier
>&);
182 placeholder_array_type(const std::string
&, Location
);
185 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
188 named_type(const std::string
&, Btype
*, Location
);
191 circular_pointer_type(Btype
*, bool);
194 is_circular_pointer_type(Btype
*);
200 type_alignment(Btype
*);
203 type_field_alignment(Btype
*);
206 type_field_offset(Btype
*, size_t index
);
211 zero_expression(Btype
*);
215 { return this->make_expression(error_mark_node
); }
218 var_expression(Bvariable
* var
, Location
);
221 indirect_expression(Bexpression
* expr
, bool known_valid
, Location
);
224 integer_constant_expression(Btype
* btype
, mpz_t val
);
227 float_constant_expression(Btype
* btype
, mpfr_t val
);
230 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
233 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
236 function_code_expression(Bfunction
*, Location
);
239 address_expression(Bexpression
*, Location
);
245 { return this->make_statement(error_mark_node
); }
248 expression_statement(Bexpression
*);
251 init_statement(Bvariable
* var
, Bexpression
* init
);
254 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
257 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
261 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
265 switch_statement(Bexpression
* value
,
266 const std::vector
<std::vector
<Bexpression
*> >& cases
,
267 const std::vector
<Bstatement
*>& statements
,
271 compound_statement(Bstatement
*, Bstatement
*);
274 statement_list(const std::vector
<Bstatement
*>&);
279 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
283 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
286 block_statement(Bblock
*);
292 { return new Bvariable(error_mark_node
); }
295 global_variable(const std::string
& package_name
,
296 const std::string
& pkgpath
,
297 const std::string
& name
,
301 bool in_unique_section
,
305 global_variable_set_init(Bvariable
*, Bexpression
*);
308 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
312 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
316 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
317 Location
, Bstatement
**);
320 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
323 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
324 Location
, Bexpression
*);
327 immutable_struct_reference(const std::string
&, Btype
*, Location
);
332 label(Bfunction
*, const std::string
& name
, Location
);
335 label_definition_statement(Blabel
*);
338 goto_statement(Blabel
*, Location
);
341 label_address(Blabel
*, Location
);
347 { return this->make_function(error_mark_node
); }
350 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
351 bool is_visible
, bool is_declaration
, bool is_inlinable
,
352 bool disable_split_stack
, bool in_unique_section
, Location
);
355 // Make a Bexpression from a tree.
357 make_expression(tree t
)
358 { return new Bexpression(t
); }
360 // Make a Bstatement from a tree.
362 make_statement(tree t
)
363 { return new Bstatement(t
); }
365 // Make a Btype from a tree.
368 { return new Btype(t
); }
371 make_function(tree t
)
372 { return new Bfunction(t
); }
375 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
378 fill_in_array(Btype
*, Btype
*, Bexpression
*);
381 non_zero_size_type(tree
);
384 // A helper function.
387 get_identifier_from_string(const std::string
& str
)
389 return get_identifier_with_length(str
.data(), str
.length());
392 // Get an unnamed integer type.
395 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
400 if (bits
== INT_TYPE_SIZE
)
401 type
= unsigned_type_node
;
402 else if (bits
== CHAR_TYPE_SIZE
)
403 type
= unsigned_char_type_node
;
404 else if (bits
== SHORT_TYPE_SIZE
)
405 type
= short_unsigned_type_node
;
406 else if (bits
== LONG_TYPE_SIZE
)
407 type
= long_unsigned_type_node
;
408 else if (bits
== LONG_LONG_TYPE_SIZE
)
409 type
= long_long_unsigned_type_node
;
411 type
= make_unsigned_type(bits
);
415 if (bits
== INT_TYPE_SIZE
)
416 type
= integer_type_node
;
417 else if (bits
== CHAR_TYPE_SIZE
)
418 type
= signed_char_type_node
;
419 else if (bits
== SHORT_TYPE_SIZE
)
420 type
= short_integer_type_node
;
421 else if (bits
== LONG_TYPE_SIZE
)
422 type
= long_integer_type_node
;
423 else if (bits
== LONG_LONG_TYPE_SIZE
)
424 type
= long_long_integer_type_node
;
426 type
= make_signed_type(bits
);
428 return this->make_type(type
);
431 // Get an unnamed float type.
434 Gcc_backend::float_type(int bits
)
437 if (bits
== FLOAT_TYPE_SIZE
)
438 type
= float_type_node
;
439 else if (bits
== DOUBLE_TYPE_SIZE
)
440 type
= double_type_node
;
441 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
442 type
= long_double_type_node
;
445 type
= make_node(REAL_TYPE
);
446 TYPE_PRECISION(type
) = bits
;
449 return this->make_type(type
);
452 // Get an unnamed complex type.
455 Gcc_backend::complex_type(int bits
)
458 if (bits
== FLOAT_TYPE_SIZE
* 2)
459 type
= complex_float_type_node
;
460 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
461 type
= complex_double_type_node
;
462 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
463 type
= complex_long_double_type_node
;
466 type
= make_node(REAL_TYPE
);
467 TYPE_PRECISION(type
) = bits
/ 2;
469 type
= build_complex_type(type
);
471 return this->make_type(type
);
474 // Get a pointer type.
477 Gcc_backend::pointer_type(Btype
* to_type
)
479 tree to_type_tree
= to_type
->get_tree();
480 if (to_type_tree
== error_mark_node
)
481 return this->error_type();
482 tree type
= build_pointer_type(to_type_tree
);
483 return this->make_type(type
);
486 // Make a function type.
489 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
490 const std::vector
<Btyped_identifier
>& parameters
,
491 const std::vector
<Btyped_identifier
>& results
,
494 tree args
= NULL_TREE
;
496 if (receiver
.btype
!= NULL
)
498 tree t
= receiver
.btype
->get_tree();
499 if (t
== error_mark_node
)
500 return this->error_type();
501 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
502 pp
= &TREE_CHAIN(*pp
);
505 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
506 p
!= parameters
.end();
509 tree t
= p
->btype
->get_tree();
510 if (t
== error_mark_node
)
511 return this->error_type();
512 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
513 pp
= &TREE_CHAIN(*pp
);
516 // Varargs is handled entirely at the Go level. When converted to
517 // GENERIC functions are not varargs.
518 *pp
= void_list_node
;
522 result
= void_type_node
;
523 else if (results
.size() == 1)
524 result
= results
.front().btype
->get_tree();
527 result
= make_node(RECORD_TYPE
);
528 tree field_trees
= NULL_TREE
;
530 for (std::vector
<Btyped_identifier
>::const_iterator p
= results
.begin();
534 const std::string name
= (p
->name
.empty()
537 tree name_tree
= get_identifier_from_string(name
);
538 tree field_type_tree
= p
->btype
->get_tree();
539 if (field_type_tree
== error_mark_node
)
540 return this->error_type();
541 gcc_assert(TYPE_SIZE(field_type_tree
) != NULL_TREE
);
542 tree field
= build_decl(location
.gcc_location(), FIELD_DECL
,
543 name_tree
, field_type_tree
);
544 DECL_CONTEXT(field
) = result
;
546 pp
= &DECL_CHAIN(field
);
548 TYPE_FIELDS(result
) = field_trees
;
551 if (result
== error_mark_node
)
552 return this->error_type();
554 tree fntype
= build_function_type(result
, args
);
555 if (fntype
== error_mark_node
)
556 return this->error_type();
558 return this->make_type(build_pointer_type(fntype
));
561 // Make a struct type.
564 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
566 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
569 // Fill in the fields of a struct type.
572 Gcc_backend::fill_in_struct(Btype
* fill
,
573 const std::vector
<Btyped_identifier
>& fields
)
575 tree fill_tree
= fill
->get_tree();
576 tree field_trees
= NULL_TREE
;
577 tree
* pp
= &field_trees
;
578 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
582 tree name_tree
= get_identifier_from_string(p
->name
);
583 tree type_tree
= p
->btype
->get_tree();
584 if (type_tree
== error_mark_node
)
585 return this->error_type();
586 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
588 DECL_CONTEXT(field
) = fill_tree
;
590 pp
= &DECL_CHAIN(field
);
592 TYPE_FIELDS(fill_tree
) = field_trees
;
593 layout_type(fill_tree
);
597 // Make an array type.
600 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
602 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
603 element_btype
, length
);
606 // Fill in an array type.
609 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
612 tree element_type_tree
= element_type
->get_tree();
613 tree length_tree
= length
->get_tree();
614 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
615 return this->error_type();
617 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
619 length_tree
= fold_convert(sizetype
, length_tree
);
621 // build_index_type takes the maximum index, which is one less than
623 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
627 tree fill_tree
= fill
->get_tree();
628 TREE_TYPE(fill_tree
) = element_type_tree
;
629 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
630 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
631 layout_type(fill_tree
);
633 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
634 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
635 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
636 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
637 TYPE_CANONICAL(fill_tree
) =
638 build_array_type(TYPE_CANONICAL(element_type_tree
),
639 TYPE_CANONICAL(index_type_tree
));
644 // Create a placeholder for a pointer type.
647 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
648 Location location
, bool)
650 tree ret
= build_distinct_type_copy(ptr_type_node
);
653 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
654 get_identifier_from_string(name
),
656 TYPE_NAME(ret
) = decl
;
658 return this->make_type(ret
);
661 // Set the real target type for a placeholder pointer type.
664 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
667 tree pt
= placeholder
->get_tree();
668 if (pt
== error_mark_node
)
670 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
671 tree tt
= to_type
->get_tree();
672 if (tt
== error_mark_node
)
674 placeholder
->set_tree(error_mark_node
);
677 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
678 TREE_TYPE(pt
) = TREE_TYPE(tt
);
679 if (TYPE_NAME(pt
) != NULL_TREE
)
681 // Build the data structure gcc wants to see for a typedef.
682 tree copy
= build_variant_type_copy(pt
);
683 TYPE_NAME(copy
) = NULL_TREE
;
684 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
689 // Set the real values for a placeholder function type.
692 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
694 return this->set_placeholder_pointer_type(placeholder
, ft
);
697 // Create a placeholder for a struct type.
700 Gcc_backend::placeholder_struct_type(const std::string
& name
,
703 tree ret
= make_node(RECORD_TYPE
);
706 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
707 get_identifier_from_string(name
),
709 TYPE_NAME(ret
) = decl
;
711 return this->make_type(ret
);
714 // Fill in the fields of a placeholder struct type.
717 Gcc_backend::set_placeholder_struct_type(
719 const std::vector
<Btyped_identifier
>& fields
)
721 tree t
= placeholder
->get_tree();
722 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
723 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
725 if (TYPE_NAME(t
) != NULL_TREE
)
727 // Build the data structure gcc wants to see for a typedef.
728 tree copy
= build_distinct_type_copy(t
);
729 TYPE_NAME(copy
) = NULL_TREE
;
730 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
733 return r
->get_tree() != error_mark_node
;
736 // Create a placeholder for an array type.
739 Gcc_backend::placeholder_array_type(const std::string
& name
,
742 tree ret
= make_node(ARRAY_TYPE
);
743 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
744 get_identifier_from_string(name
),
746 TYPE_NAME(ret
) = decl
;
747 return this->make_type(ret
);
750 // Fill in the fields of a placeholder array type.
753 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
754 Btype
* element_btype
,
757 tree t
= placeholder
->get_tree();
758 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
759 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
761 // Build the data structure gcc wants to see for a typedef.
762 tree copy
= build_distinct_type_copy(t
);
763 TYPE_NAME(copy
) = NULL_TREE
;
764 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
766 return r
->get_tree() != error_mark_node
;
769 // Return a named version of a type.
772 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
775 tree type
= btype
->get_tree();
776 if (type
== error_mark_node
)
777 return this->error_type();
779 // The middle-end expects a basic type to have a name. In Go every
780 // basic type will have a name. The first time we see a basic type,
781 // give it whatever Go name we have at this point.
782 if (TYPE_NAME(type
) == NULL_TREE
783 && location
.gcc_location() == BUILTINS_LOCATION
784 && (TREE_CODE(type
) == INTEGER_TYPE
785 || TREE_CODE(type
) == REAL_TYPE
786 || TREE_CODE(type
) == COMPLEX_TYPE
787 || TREE_CODE(type
) == BOOLEAN_TYPE
))
789 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
790 get_identifier_from_string(name
),
792 TYPE_NAME(type
) = decl
;
793 return this->make_type(type
);
796 tree copy
= build_variant_type_copy(type
);
797 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
798 get_identifier_from_string(name
),
800 DECL_ORIGINAL_TYPE(decl
) = type
;
801 TYPE_NAME(copy
) = decl
;
802 return this->make_type(copy
);
805 // Return a pointer type used as a marker for a circular type.
808 Gcc_backend::circular_pointer_type(Btype
*, bool)
810 return this->make_type(ptr_type_node
);
813 // Return whether we might be looking at a circular type.
816 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
818 return btype
->get_tree() == ptr_type_node
;
821 // Return the size of a type.
824 Gcc_backend::type_size(Btype
* btype
)
826 tree t
= btype
->get_tree();
827 if (t
== error_mark_node
)
829 t
= TYPE_SIZE_UNIT(t
);
830 gcc_assert(TREE_CODE(t
) == INTEGER_CST
);
831 gcc_assert(TREE_INT_CST_HIGH(t
) == 0);
832 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
833 size_t ret
= static_cast<size_t>(val_wide
);
834 gcc_assert(ret
== val_wide
);
838 // Return the alignment of a type.
841 Gcc_backend::type_alignment(Btype
* btype
)
843 tree t
= btype
->get_tree();
844 if (t
== error_mark_node
)
846 return TYPE_ALIGN_UNIT(t
);
849 // Return the alignment of a struct field of type BTYPE.
852 Gcc_backend::type_field_alignment(Btype
* btype
)
854 tree t
= btype
->get_tree();
855 if (t
== error_mark_node
)
857 return go_field_alignment(t
);
860 // Return the offset of a field in a struct.
863 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
865 tree struct_tree
= btype
->get_tree();
866 if (struct_tree
== error_mark_node
)
868 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
869 tree field
= TYPE_FIELDS(struct_tree
);
870 for (; index
> 0; --index
)
872 field
= DECL_CHAIN(field
);
873 gcc_assert(field
!= NULL_TREE
);
875 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
876 gcc_assert(offset_wide
>= 0);
877 size_t ret
= static_cast<size_t>(offset_wide
);
878 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
882 // Return the zero value for a type.
885 Gcc_backend::zero_expression(Btype
* btype
)
887 tree t
= btype
->get_tree();
889 if (t
== error_mark_node
)
890 ret
= error_mark_node
;
892 ret
= build_zero_cst(t
);
893 return tree_to_expr(ret
);
896 // An expression that references a variable.
899 Gcc_backend::var_expression(Bvariable
* var
, Location
)
901 tree ret
= var
->get_tree();
902 if (ret
== error_mark_node
)
903 return this->error_expression();
904 return tree_to_expr(ret
);
907 // An expression that indirectly references an expression.
910 Gcc_backend::indirect_expression(Bexpression
* expr
, bool known_valid
,
913 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
916 TREE_THIS_NOTRAP(ret
) = 1;
917 return tree_to_expr(ret
);
920 // Return a typed value as a constant integer.
923 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
925 tree t
= btype
->get_tree();
926 if (t
== error_mark_node
)
927 return this->error_expression();
929 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
930 return tree_to_expr(ret
);
933 // Return a typed value as a constant floating-point number.
936 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
938 tree t
= btype
->get_tree();
940 if (t
== error_mark_node
)
941 return this->error_expression();
944 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
946 real_convert(&r2
, TYPE_MODE(t
), &r1
);
947 ret
= build_real(t
, r2
);
948 return tree_to_expr(ret
);
951 // Return a typed real and imaginary value as a constant complex number.
954 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
956 tree t
= btype
->get_tree();
958 if (t
== error_mark_node
)
959 return this->error_expression();
962 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
964 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
967 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
969 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
971 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
972 build_real(TREE_TYPE(t
), r4
));
973 return tree_to_expr(ret
);
976 // An expression that converts an expression to a different type.
979 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
, Location
)
981 tree type_tree
= type
->get_tree();
982 tree expr_tree
= expr
->get_tree();
983 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
984 return this->error_expression();
986 tree ret
= fold_convert(type_tree
, expr_tree
);
987 return tree_to_expr(ret
);
990 // Get the address of a function.
993 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
995 tree func
= bfunc
->get_tree();
996 if (func
== error_mark_node
)
997 return this->error_expression();
999 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1000 return this->make_expression(ret
);
1003 // Get the address of an expression.
1006 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1008 tree expr
= bexpr
->get_tree();
1009 if (expr
== error_mark_node
)
1010 return this->error_expression();
1012 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1013 return this->make_expression(ret
);
1016 // An expression as a statement.
1019 Gcc_backend::expression_statement(Bexpression
* expr
)
1021 return this->make_statement(expr
->get_tree());
1024 // Variable initialization.
1027 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1029 tree var_tree
= var
->get_tree();
1030 tree init_tree
= init
->get_tree();
1031 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1032 return this->error_statement();
1033 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1035 // To avoid problems with GNU ld, we don't make zero-sized
1036 // externally visible variables. That might lead us to doing an
1037 // initialization of a zero-sized expression to a non-zero sized
1038 // variable, or vice-versa. Avoid crashes by omitting the
1039 // initializer. Such initializations don't mean anything anyhow.
1040 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1041 && init_tree
!= NULL_TREE
1042 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1044 DECL_INITIAL(var_tree
) = init_tree
;
1045 init_tree
= NULL_TREE
;
1048 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1049 void_type_node
, var_tree
);
1050 if (init_tree
!= NULL_TREE
)
1051 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1052 void_type_node
, init_tree
, ret
);
1054 return this->make_statement(ret
);
1060 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1063 tree lhs_tree
= lhs
->get_tree();
1064 tree rhs_tree
= rhs
->get_tree();
1065 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1066 return this->error_statement();
1068 // To avoid problems with GNU ld, we don't make zero-sized
1069 // externally visible variables. That might lead us to doing an
1070 // assignment of a zero-sized expression to a non-zero sized
1071 // expression; avoid crashes here by avoiding assignments of
1072 // zero-sized expressions. Such assignments don't really mean
1074 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1075 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1076 return this->compound_statement(this->expression_statement(lhs
),
1077 this->expression_statement(rhs
));
1079 // Sometimes the same unnamed Go type can be created multiple times
1080 // and thus have multiple tree representations. Make sure this does
1081 // not confuse the middle-end.
1082 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1084 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1085 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1086 if (POINTER_TYPE_P(lhs_type_tree
)
1087 || INTEGRAL_TYPE_P(lhs_type_tree
)
1088 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1089 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1090 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1092 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1093 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1095 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1096 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1097 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1099 lhs_type_tree
, rhs_tree
);
1103 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1106 lhs_tree
, rhs_tree
));
1112 Gcc_backend::return_statement(Bfunction
* bfunction
,
1113 const std::vector
<Bexpression
*>& vals
,
1116 tree fntree
= bfunction
->get_tree();
1117 if (fntree
== error_mark_node
)
1118 return this->error_statement();
1119 tree result
= DECL_RESULT(fntree
);
1120 if (result
== error_mark_node
)
1121 return this->error_statement();
1124 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1126 else if (vals
.size() == 1)
1128 tree val
= vals
.front()->get_tree();
1129 if (val
== error_mark_node
)
1130 return this->error_statement();
1131 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1132 void_type_node
, result
,
1133 vals
.front()->get_tree());
1134 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1135 void_type_node
, set
);
1139 // To return multiple values, copy the values into a temporary
1140 // variable of the right structure type, and then assign the
1141 // temporary variable to the DECL_RESULT in the return
1143 tree stmt_list
= NULL_TREE
;
1144 tree rettype
= TREE_TYPE(result
);
1145 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1146 tree field
= TYPE_FIELDS(rettype
);
1147 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1149 p
++, field
= DECL_CHAIN(field
))
1151 gcc_assert(field
!= NULL_TREE
);
1152 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1153 TREE_TYPE(field
), rettmp
, field
,
1155 tree val
= (*p
)->get_tree();
1156 if (val
== error_mark_node
)
1157 return this->error_statement();
1158 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1160 ref
, (*p
)->get_tree());
1161 append_to_statement_list(set
, &stmt_list
);
1163 gcc_assert(field
== NULL_TREE
);
1164 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1167 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1168 void_type_node
, set
);
1169 append_to_statement_list(ret_expr
, &stmt_list
);
1172 return this->make_statement(ret
);
1178 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
1179 Bblock
* else_block
, Location location
)
1181 tree cond_tree
= condition
->get_tree();
1182 tree then_tree
= then_block
->get_tree();
1183 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
1184 if (cond_tree
== error_mark_node
1185 || then_tree
== error_mark_node
1186 || else_tree
== error_mark_node
)
1187 return this->error_statement();
1188 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
1189 cond_tree
, then_tree
, else_tree
);
1190 return this->make_statement(ret
);
1196 Gcc_backend::switch_statement(
1198 const std::vector
<std::vector
<Bexpression
*> >& cases
,
1199 const std::vector
<Bstatement
*>& statements
,
1200 Location switch_location
)
1202 gcc_assert(cases
.size() == statements
.size());
1204 tree stmt_list
= NULL_TREE
;
1205 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
1206 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
1207 ps
!= statements
.end();
1212 source_location loc
= (*ps
!= NULL
1213 ? EXPR_LOCATION((*ps
)->get_tree())
1214 : UNKNOWN_LOCATION
);
1215 tree label
= create_artificial_label(loc
);
1216 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
1217 append_to_statement_list(c
, &stmt_list
);
1221 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
1225 tree t
= (*pcv
)->get_tree();
1226 if (t
== error_mark_node
)
1227 return this->error_statement();
1228 source_location loc
= EXPR_LOCATION(t
);
1229 tree label
= create_artificial_label(loc
);
1230 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
1231 append_to_statement_list(c
, &stmt_list
);
1237 tree t
= (*ps
)->get_tree();
1238 if (t
== error_mark_node
)
1239 return this->error_statement();
1240 append_to_statement_list(t
, &stmt_list
);
1244 tree tv
= value
->get_tree();
1245 if (tv
== error_mark_node
)
1246 return this->error_statement();
1247 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
1248 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
1249 return this->make_statement(t
);
1252 // Pair of statements.
1255 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
1257 tree stmt_list
= NULL_TREE
;
1258 tree t
= s1
->get_tree();
1259 if (t
== error_mark_node
)
1260 return this->error_statement();
1261 append_to_statement_list(t
, &stmt_list
);
1263 if (t
== error_mark_node
)
1264 return this->error_statement();
1265 append_to_statement_list(t
, &stmt_list
);
1266 return this->make_statement(stmt_list
);
1269 // List of statements.
1272 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
1274 tree stmt_list
= NULL_TREE
;
1275 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1276 p
!= statements
.end();
1279 tree t
= (*p
)->get_tree();
1280 if (t
== error_mark_node
)
1281 return this->error_statement();
1282 append_to_statement_list(t
, &stmt_list
);
1284 return this->make_statement(stmt_list
);
1287 // Make a block. For some reason gcc uses a dual structure for
1288 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1289 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1293 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
1294 const std::vector
<Bvariable
*>& vars
,
1295 Location start_location
,
1298 tree block_tree
= make_node(BLOCK
);
1299 if (enclosing
== NULL
)
1301 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1302 // until we have a proper representation of the init function.
1304 if (function
== NULL
)
1305 fndecl
= current_function_decl
;
1307 fndecl
= function
->get_tree();
1308 gcc_assert(fndecl
!= NULL_TREE
);
1310 // We may have already created a block for local variables when
1311 // we take the address of a parameter.
1312 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1314 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
1315 DECL_INITIAL(fndecl
) = block_tree
;
1319 tree superblock_tree
= DECL_INITIAL(fndecl
);
1320 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1322 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1324 pp
= &BLOCK_CHAIN(*pp
))
1331 tree superbind_tree
= enclosing
->get_tree();
1332 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
1333 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
1335 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1337 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1339 pp
= &BLOCK_CHAIN(*pp
))
1344 tree
* pp
= &BLOCK_VARS(block_tree
);
1345 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
1349 *pp
= (*pv
)->get_tree();
1350 if (*pp
!= error_mark_node
)
1351 pp
= &DECL_CHAIN(*pp
);
1355 TREE_USED(block_tree
) = 1;
1357 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
1358 void_type_node
, BLOCK_VARS(block_tree
),
1359 NULL_TREE
, block_tree
);
1360 TREE_SIDE_EFFECTS(bind_tree
) = 1;
1362 return new Bblock(bind_tree
);
1365 // Add statements to a block.
1368 Gcc_backend::block_add_statements(Bblock
* bblock
,
1369 const std::vector
<Bstatement
*>& statements
)
1371 tree stmt_list
= NULL_TREE
;
1372 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1373 p
!= statements
.end();
1376 tree s
= (*p
)->get_tree();
1377 if (s
!= error_mark_node
)
1378 append_to_statement_list(s
, &stmt_list
);
1381 tree bind_tree
= bblock
->get_tree();
1382 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1383 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
1386 // Return a block as a statement.
1389 Gcc_backend::block_statement(Bblock
* bblock
)
1391 tree bind_tree
= bblock
->get_tree();
1392 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1393 return this->make_statement(bind_tree
);
1396 // This is not static because we declare it with GTY(()) in go-c.h.
1397 tree go_non_zero_struct
;
1399 // Return a type corresponding to TYPE with non-zero size.
1402 Gcc_backend::non_zero_size_type(tree type
)
1404 if (int_size_in_bytes(type
) != 0)
1407 switch (TREE_CODE(type
))
1410 if (TYPE_FIELDS(type
) != NULL_TREE
)
1412 tree ns
= make_node(RECORD_TYPE
);
1413 tree field_trees
= NULL_TREE
;
1414 tree
*pp
= &field_trees
;
1415 for (tree field
= TYPE_FIELDS(type
);
1417 field
= DECL_CHAIN(field
))
1419 tree ft
= TREE_TYPE(field
);
1420 if (field
== TYPE_FIELDS(type
))
1421 ft
= non_zero_size_type(ft
);
1422 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
1423 DECL_NAME(field
), ft
);
1424 DECL_CONTEXT(f
) = ns
;
1426 pp
= &DECL_CHAIN(f
);
1428 TYPE_FIELDS(ns
) = field_trees
;
1433 if (go_non_zero_struct
== NULL_TREE
)
1435 type
= make_node(RECORD_TYPE
);
1436 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
1437 get_identifier("dummy"),
1439 DECL_CONTEXT(field
) = type
;
1440 TYPE_FIELDS(type
) = field
;
1442 go_non_zero_struct
= type
;
1444 return go_non_zero_struct
;
1448 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
1449 return build_array_type_nelts(element_type
, 1);
1459 // Make a global variable.
1462 Gcc_backend::global_variable(const std::string
& package_name
,
1463 const std::string
& pkgpath
,
1464 const std::string
& name
,
1468 bool in_unique_section
,
1471 tree type_tree
= btype
->get_tree();
1472 if (type_tree
== error_mark_node
)
1473 return this->error_variable();
1475 // The GNU linker does not like dynamic variables with zero size.
1476 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
1477 type_tree
= this->non_zero_size_type(type_tree
);
1479 std::string
var_name(package_name
);
1480 var_name
.push_back('.');
1481 var_name
.append(name
);
1482 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1483 get_identifier_from_string(var_name
),
1486 DECL_EXTERNAL(decl
) = 1;
1488 TREE_STATIC(decl
) = 1;
1491 TREE_PUBLIC(decl
) = 1;
1493 std::string
asm_name(pkgpath
);
1494 asm_name
.push_back('.');
1495 asm_name
.append(name
);
1496 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1498 TREE_USED(decl
) = 1;
1500 if (in_unique_section
)
1501 resolve_unique_section (decl
, 0, 1);
1503 go_preserve_from_gc(decl
);
1505 return new Bvariable(decl
);
1508 // Set the initial value of a global variable.
1511 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
1513 tree expr_tree
= expr
->get_tree();
1514 if (expr_tree
== error_mark_node
)
1516 gcc_assert(TREE_CONSTANT(expr_tree
));
1517 tree var_decl
= var
->get_tree();
1518 if (var_decl
== error_mark_node
)
1520 DECL_INITIAL(var_decl
) = expr_tree
;
1522 // If this variable goes in a unique section, it may need to go into
1523 // a different one now that DECL_INITIAL is set.
1524 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl
))
1526 DECL_SECTION_NAME (var_decl
) = NULL_TREE
;
1527 resolve_unique_section (var_decl
,
1528 compute_reloc_for_constant (expr_tree
),
1533 // Make a local variable.
1536 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
1537 Btype
* btype
, bool is_address_taken
,
1540 tree type_tree
= btype
->get_tree();
1541 if (type_tree
== error_mark_node
)
1542 return this->error_variable();
1543 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1544 get_identifier_from_string(name
),
1546 DECL_CONTEXT(decl
) = function
->get_tree();
1547 TREE_USED(decl
) = 1;
1548 if (is_address_taken
)
1549 TREE_ADDRESSABLE(decl
) = 1;
1550 go_preserve_from_gc(decl
);
1551 return new Bvariable(decl
);
1554 // Make a function parameter variable.
1557 Gcc_backend::parameter_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(), PARM_DECL
,
1565 get_identifier_from_string(name
),
1567 DECL_CONTEXT(decl
) = function
->get_tree();
1568 DECL_ARG_TYPE(decl
) = type_tree
;
1569 TREE_USED(decl
) = 1;
1570 if (is_address_taken
)
1571 TREE_ADDRESSABLE(decl
) = 1;
1572 go_preserve_from_gc(decl
);
1573 return new Bvariable(decl
);
1576 // Make a temporary variable.
1579 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
1580 Btype
* btype
, Bexpression
* binit
,
1581 bool is_address_taken
,
1583 Bstatement
** pstatement
)
1585 tree type_tree
= btype
->get_tree();
1586 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
1587 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1589 *pstatement
= this->error_statement();
1590 return this->error_variable();
1594 // We can only use create_tmp_var if the type is not addressable.
1595 if (!TREE_ADDRESSABLE(type_tree
))
1596 var
= create_tmp_var(type_tree
, "GOTMP");
1599 gcc_assert(bblock
!= NULL
);
1600 var
= build_decl(location
.gcc_location(), VAR_DECL
,
1601 create_tmp_var_name("GOTMP"),
1603 DECL_ARTIFICIAL(var
) = 1;
1604 DECL_IGNORED_P(var
) = 1;
1606 // FIXME: Permitting function to be NULL here is a temporary
1607 // measure until we have a proper representation of the init
1609 if (function
!= NULL
)
1610 DECL_CONTEXT(var
) = function
->get_tree();
1613 gcc_assert(current_function_decl
!= NULL_TREE
);
1614 DECL_CONTEXT(var
) = current_function_decl
;
1617 // We have to add this variable to the BLOCK and the BIND_EXPR.
1618 tree bind_tree
= bblock
->get_tree();
1619 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1620 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
1621 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
1622 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
1623 BLOCK_VARS(block_tree
) = var
;
1624 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
1627 if (init_tree
!= NULL_TREE
)
1628 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
1631 if (is_address_taken
)
1632 TREE_ADDRESSABLE(var
) = 1;
1634 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
1636 void_type_node
, var
));
1637 return new Bvariable(var
);
1640 // Create a named immutable initialized data structure.
1643 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
1644 bool, Btype
* btype
, Location location
)
1646 tree type_tree
= btype
->get_tree();
1647 if (type_tree
== error_mark_node
)
1648 return this->error_variable();
1649 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1650 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1651 get_identifier_from_string(name
),
1652 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1653 TREE_STATIC(decl
) = 1;
1654 TREE_READONLY(decl
) = 1;
1655 TREE_CONSTANT(decl
) = 1;
1656 TREE_USED(decl
) = 1;
1657 DECL_ARTIFICIAL(decl
) = 1;
1659 TREE_PUBLIC(decl
) = 1;
1661 // We don't call rest_of_decl_compilation until we have the
1664 go_preserve_from_gc(decl
);
1665 return new Bvariable(decl
);
1668 // Set the initializer for a variable created by immutable_struct.
1669 // This is where we finish compiling the variable.
1672 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
1673 bool, bool is_common
, Btype
*, Location
,
1674 Bexpression
* initializer
)
1676 tree decl
= var
->get_tree();
1677 tree init_tree
= initializer
->get_tree();
1678 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
1681 DECL_INITIAL(decl
) = init_tree
;
1683 // We can't call make_decl_one_only until we set DECL_INITIAL.
1685 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
1687 // These variables are often unneeded in the final program, so put
1688 // them in their own section so that linker GC can discard them.
1689 resolve_unique_section(decl
,
1690 compute_reloc_for_constant (init_tree
),
1693 rest_of_decl_compilation(decl
, 1, 0);
1696 // Return a reference to an immutable initialized data structure
1697 // defined in another package.
1700 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
1703 tree type_tree
= btype
->get_tree();
1704 if (type_tree
== error_mark_node
)
1705 return this->error_variable();
1706 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1707 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1708 get_identifier_from_string(name
),
1709 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1710 TREE_READONLY(decl
) = 1;
1711 TREE_CONSTANT(decl
) = 1;
1712 DECL_ARTIFICIAL(decl
) = 1;
1713 TREE_PUBLIC(decl
) = 1;
1714 DECL_EXTERNAL(decl
) = 1;
1715 go_preserve_from_gc(decl
);
1716 return new Bvariable(decl
);
1722 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
1727 decl
= create_artificial_label(location
.gcc_location());
1730 tree id
= get_identifier_from_string(name
);
1731 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
1733 DECL_CONTEXT(decl
) = function
->get_tree();
1735 return new Blabel(decl
);
1738 // Make a statement which defines a label.
1741 Gcc_backend::label_definition_statement(Blabel
* label
)
1743 tree lab
= label
->get_tree();
1744 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
1745 void_type_node
, lab
);
1746 return this->make_statement(ret
);
1749 // Make a goto statement.
1752 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
1754 tree lab
= label
->get_tree();
1755 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
1757 return this->make_statement(ret
);
1760 // Get the address of a label.
1763 Gcc_backend::label_address(Blabel
* label
, Location location
)
1765 tree lab
= label
->get_tree();
1767 TREE_ADDRESSABLE(lab
) = 1;
1768 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
1769 build_fold_addr_expr_loc(location
.gcc_location(),
1771 return this->make_expression(ret
);
1774 // Declare or define a new function.
1777 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
1778 const std::string
& asm_name
, bool is_visible
,
1779 bool is_declaration
, bool is_inlinable
,
1780 bool disable_split_stack
, bool in_unique_section
,
1783 tree functype
= fntype
->get_tree();
1784 if (functype
!= error_mark_node
)
1786 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
1787 functype
= TREE_TYPE(functype
);
1789 tree id
= get_identifier_from_string(name
);
1790 if (functype
== error_mark_node
|| id
== error_mark_node
)
1791 return this->error_function();
1793 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
1794 if (!asm_name
.empty())
1795 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1797 TREE_PUBLIC(decl
) = 1;
1799 DECL_EXTERNAL(decl
) = 1;
1802 tree restype
= TREE_TYPE(functype
);
1804 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
1805 DECL_ARTIFICIAL(resdecl
) = 1;
1806 DECL_IGNORED_P(resdecl
) = 1;
1807 DECL_CONTEXT(resdecl
) = decl
;
1808 DECL_RESULT(decl
) = resdecl
;
1811 DECL_UNINLINABLE(decl
) = 1;
1812 if (disable_split_stack
)
1814 tree attr
= get_identifier("__no_split_stack__");
1815 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1817 if (in_unique_section
)
1818 resolve_unique_section(decl
, 0, 1);
1820 go_preserve_from_gc(decl
);
1821 return new Bfunction(decl
);
1824 // The single backend.
1826 static Gcc_backend gcc_backend
;
1828 // Return the backend generator.
1833 return &gcc_backend
;
1836 // FIXME: Temporary functions while converting to the new backend
1840 tree_to_type(tree t
)
1842 return new Btype(t
);
1846 tree_to_expr(tree t
)
1848 return new Bexpression(t
);
1852 tree_to_stat(tree t
)
1854 return new Bstatement(t
);
1858 tree_to_function(tree t
)
1860 return new Bfunction(t
);
1864 tree_to_block(tree t
)
1866 gcc_assert(TREE_CODE(t
) == BIND_EXPR
);
1867 return new Bblock(t
);
1871 type_to_tree(Btype
* bt
)
1873 return bt
->get_tree();
1877 expr_to_tree(Bexpression
* be
)
1879 return be
->get_tree();
1883 stat_to_tree(Bstatement
* bs
)
1885 return bs
->get_tree();
1889 block_to_tree(Bblock
* bb
)
1891 return bb
->get_tree();
1895 var_to_tree(Bvariable
* bv
)
1897 return bv
->get_tree();
1901 function_to_tree(Bfunction
* bf
)
1903 return bf
->get_tree();