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 "stringpool.h"
29 #include "stor-layout.h"
31 #include "tree-iterator.h"
43 // A class wrapping a tree.
64 // In gcc, types, expressions, and statements are all trees.
65 class Btype
: public Gcc_tree
73 class Bexpression
: public Gcc_tree
81 class Bstatement
: public Gcc_tree
89 class Bfunction
: public Gcc_tree
97 class Bblock
: public Gcc_tree
105 class Bvariable
: public Gcc_tree
113 class Blabel
: public Gcc_tree
121 // This file implements the interface between the Go frontend proper
122 // and the gcc IR. This implements specific instantiations of
123 // abstract classes defined by the Go frontend proper. The Go
124 // frontend proper class methods of these classes to generate the
125 // backend representation.
127 class Gcc_backend
: public Backend
134 { return this->make_type(error_mark_node
); }
138 { return this->make_type(void_type_node
); }
142 { return this->make_type(boolean_type_node
); }
145 integer_type(bool, int);
154 pointer_type(Btype
*);
157 function_type(const Btyped_identifier
&,
158 const std::vector
<Btyped_identifier
>&,
159 const std::vector
<Btyped_identifier
>&,
163 struct_type(const std::vector
<Btyped_identifier
>&);
166 array_type(Btype
*, Bexpression
*);
169 placeholder_pointer_type(const std::string
&, Location
, bool);
172 set_placeholder_pointer_type(Btype
*, Btype
*);
175 set_placeholder_function_type(Btype
*, Btype
*);
178 placeholder_struct_type(const std::string
&, Location
);
181 set_placeholder_struct_type(Btype
* placeholder
,
182 const std::vector
<Btyped_identifier
>&);
185 placeholder_array_type(const std::string
&, Location
);
188 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
191 named_type(const std::string
&, Btype
*, Location
);
194 circular_pointer_type(Btype
*, bool);
197 is_circular_pointer_type(Btype
*);
203 type_alignment(Btype
*);
206 type_field_alignment(Btype
*);
209 type_field_offset(Btype
*, size_t index
);
214 zero_expression(Btype
*);
218 { return this->make_expression(error_mark_node
); }
221 var_expression(Bvariable
* var
, Location
);
224 indirect_expression(Bexpression
* expr
, bool known_valid
, Location
);
227 integer_constant_expression(Btype
* btype
, mpz_t val
);
230 float_constant_expression(Btype
* btype
, mpfr_t val
);
233 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
236 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
239 function_code_expression(Bfunction
*, Location
);
242 address_expression(Bexpression
*, Location
);
248 { return this->make_statement(error_mark_node
); }
251 expression_statement(Bexpression
*);
254 init_statement(Bvariable
* var
, Bexpression
* init
);
257 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
260 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
264 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
268 switch_statement(Bexpression
* value
,
269 const std::vector
<std::vector
<Bexpression
*> >& cases
,
270 const std::vector
<Bstatement
*>& statements
,
274 compound_statement(Bstatement
*, Bstatement
*);
277 statement_list(const std::vector
<Bstatement
*>&);
282 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
286 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
289 block_statement(Bblock
*);
295 { return new Bvariable(error_mark_node
); }
298 global_variable(const std::string
& package_name
,
299 const std::string
& pkgpath
,
300 const std::string
& name
,
304 bool in_unique_section
,
308 global_variable_set_init(Bvariable
*, Bexpression
*);
311 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
315 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
319 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
320 Location
, Bstatement
**);
323 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
326 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
327 Location
, Bexpression
*);
330 immutable_struct_reference(const std::string
&, Btype
*, Location
);
335 label(Bfunction
*, const std::string
& name
, Location
);
338 label_definition_statement(Blabel
*);
341 goto_statement(Blabel
*, Location
);
344 label_address(Blabel
*, Location
);
350 { return this->make_function(error_mark_node
); }
353 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
354 bool is_visible
, bool is_declaration
, bool is_inlinable
,
355 bool disable_split_stack
, bool in_unique_section
, Location
);
358 // Make a Bexpression from a tree.
360 make_expression(tree t
)
361 { return new Bexpression(t
); }
363 // Make a Bstatement from a tree.
365 make_statement(tree t
)
366 { return new Bstatement(t
); }
368 // Make a Btype from a tree.
371 { return new Btype(t
); }
374 make_function(tree t
)
375 { return new Bfunction(t
); }
378 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
381 fill_in_array(Btype
*, Btype
*, Bexpression
*);
384 non_zero_size_type(tree
);
387 // A helper function.
390 get_identifier_from_string(const std::string
& str
)
392 return get_identifier_with_length(str
.data(), str
.length());
395 // Get an unnamed integer type.
398 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
403 if (bits
== INT_TYPE_SIZE
)
404 type
= unsigned_type_node
;
405 else if (bits
== CHAR_TYPE_SIZE
)
406 type
= unsigned_char_type_node
;
407 else if (bits
== SHORT_TYPE_SIZE
)
408 type
= short_unsigned_type_node
;
409 else if (bits
== LONG_TYPE_SIZE
)
410 type
= long_unsigned_type_node
;
411 else if (bits
== LONG_LONG_TYPE_SIZE
)
412 type
= long_long_unsigned_type_node
;
414 type
= make_unsigned_type(bits
);
418 if (bits
== INT_TYPE_SIZE
)
419 type
= integer_type_node
;
420 else if (bits
== CHAR_TYPE_SIZE
)
421 type
= signed_char_type_node
;
422 else if (bits
== SHORT_TYPE_SIZE
)
423 type
= short_integer_type_node
;
424 else if (bits
== LONG_TYPE_SIZE
)
425 type
= long_integer_type_node
;
426 else if (bits
== LONG_LONG_TYPE_SIZE
)
427 type
= long_long_integer_type_node
;
429 type
= make_signed_type(bits
);
431 return this->make_type(type
);
434 // Get an unnamed float type.
437 Gcc_backend::float_type(int bits
)
440 if (bits
== FLOAT_TYPE_SIZE
)
441 type
= float_type_node
;
442 else if (bits
== DOUBLE_TYPE_SIZE
)
443 type
= double_type_node
;
444 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
445 type
= long_double_type_node
;
448 type
= make_node(REAL_TYPE
);
449 TYPE_PRECISION(type
) = bits
;
452 return this->make_type(type
);
455 // Get an unnamed complex type.
458 Gcc_backend::complex_type(int bits
)
461 if (bits
== FLOAT_TYPE_SIZE
* 2)
462 type
= complex_float_type_node
;
463 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
464 type
= complex_double_type_node
;
465 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
466 type
= complex_long_double_type_node
;
469 type
= make_node(REAL_TYPE
);
470 TYPE_PRECISION(type
) = bits
/ 2;
472 type
= build_complex_type(type
);
474 return this->make_type(type
);
477 // Get a pointer type.
480 Gcc_backend::pointer_type(Btype
* to_type
)
482 tree to_type_tree
= to_type
->get_tree();
483 if (to_type_tree
== error_mark_node
)
484 return this->error_type();
485 tree type
= build_pointer_type(to_type_tree
);
486 return this->make_type(type
);
489 // Make a function type.
492 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
493 const std::vector
<Btyped_identifier
>& parameters
,
494 const std::vector
<Btyped_identifier
>& results
,
497 tree args
= NULL_TREE
;
499 if (receiver
.btype
!= NULL
)
501 tree t
= receiver
.btype
->get_tree();
502 if (t
== error_mark_node
)
503 return this->error_type();
504 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
505 pp
= &TREE_CHAIN(*pp
);
508 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
509 p
!= parameters
.end();
512 tree t
= p
->btype
->get_tree();
513 if (t
== error_mark_node
)
514 return this->error_type();
515 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
516 pp
= &TREE_CHAIN(*pp
);
519 // Varargs is handled entirely at the Go level. When converted to
520 // GENERIC functions are not varargs.
521 *pp
= void_list_node
;
525 result
= void_type_node
;
526 else if (results
.size() == 1)
527 result
= results
.front().btype
->get_tree();
530 result
= make_node(RECORD_TYPE
);
531 tree field_trees
= NULL_TREE
;
533 for (std::vector
<Btyped_identifier
>::const_iterator p
= results
.begin();
537 const std::string name
= (p
->name
.empty()
540 tree name_tree
= get_identifier_from_string(name
);
541 tree field_type_tree
= p
->btype
->get_tree();
542 if (field_type_tree
== error_mark_node
)
543 return this->error_type();
544 gcc_assert(TYPE_SIZE(field_type_tree
) != NULL_TREE
);
545 tree field
= build_decl(location
.gcc_location(), FIELD_DECL
,
546 name_tree
, field_type_tree
);
547 DECL_CONTEXT(field
) = result
;
549 pp
= &DECL_CHAIN(field
);
551 TYPE_FIELDS(result
) = field_trees
;
554 if (result
== error_mark_node
)
555 return this->error_type();
557 tree fntype
= build_function_type(result
, args
);
558 if (fntype
== error_mark_node
)
559 return this->error_type();
561 return this->make_type(build_pointer_type(fntype
));
564 // Make a struct type.
567 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
569 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
572 // Fill in the fields of a struct type.
575 Gcc_backend::fill_in_struct(Btype
* fill
,
576 const std::vector
<Btyped_identifier
>& fields
)
578 tree fill_tree
= fill
->get_tree();
579 tree field_trees
= NULL_TREE
;
580 tree
* pp
= &field_trees
;
581 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
585 tree name_tree
= get_identifier_from_string(p
->name
);
586 tree type_tree
= p
->btype
->get_tree();
587 if (type_tree
== error_mark_node
)
588 return this->error_type();
589 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
591 DECL_CONTEXT(field
) = fill_tree
;
593 pp
= &DECL_CHAIN(field
);
595 TYPE_FIELDS(fill_tree
) = field_trees
;
596 layout_type(fill_tree
);
600 // Make an array type.
603 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
605 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
606 element_btype
, length
);
609 // Fill in an array type.
612 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
615 tree element_type_tree
= element_type
->get_tree();
616 tree length_tree
= length
->get_tree();
617 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
618 return this->error_type();
620 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
622 length_tree
= fold_convert(sizetype
, length_tree
);
624 // build_index_type takes the maximum index, which is one less than
626 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
630 tree fill_tree
= fill
->get_tree();
631 TREE_TYPE(fill_tree
) = element_type_tree
;
632 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
633 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
634 layout_type(fill_tree
);
636 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
637 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
638 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
639 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
640 TYPE_CANONICAL(fill_tree
) =
641 build_array_type(TYPE_CANONICAL(element_type_tree
),
642 TYPE_CANONICAL(index_type_tree
));
647 // Create a placeholder for a pointer type.
650 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
651 Location location
, bool)
653 tree ret
= build_distinct_type_copy(ptr_type_node
);
656 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
657 get_identifier_from_string(name
),
659 TYPE_NAME(ret
) = decl
;
661 return this->make_type(ret
);
664 // Set the real target type for a placeholder pointer type.
667 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
670 tree pt
= placeholder
->get_tree();
671 if (pt
== error_mark_node
)
673 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
674 tree tt
= to_type
->get_tree();
675 if (tt
== error_mark_node
)
677 placeholder
->set_tree(error_mark_node
);
680 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
681 TREE_TYPE(pt
) = TREE_TYPE(tt
);
682 if (TYPE_NAME(pt
) != NULL_TREE
)
684 // Build the data structure gcc wants to see for a typedef.
685 tree copy
= build_variant_type_copy(pt
);
686 TYPE_NAME(copy
) = NULL_TREE
;
687 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
692 // Set the real values for a placeholder function type.
695 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
697 return this->set_placeholder_pointer_type(placeholder
, ft
);
700 // Create a placeholder for a struct type.
703 Gcc_backend::placeholder_struct_type(const std::string
& name
,
706 tree ret
= make_node(RECORD_TYPE
);
709 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
710 get_identifier_from_string(name
),
712 TYPE_NAME(ret
) = decl
;
714 return this->make_type(ret
);
717 // Fill in the fields of a placeholder struct type.
720 Gcc_backend::set_placeholder_struct_type(
722 const std::vector
<Btyped_identifier
>& fields
)
724 tree t
= placeholder
->get_tree();
725 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
726 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
728 if (TYPE_NAME(t
) != NULL_TREE
)
730 // Build the data structure gcc wants to see for a typedef.
731 tree copy
= build_distinct_type_copy(t
);
732 TYPE_NAME(copy
) = NULL_TREE
;
733 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
736 return r
->get_tree() != error_mark_node
;
739 // Create a placeholder for an array type.
742 Gcc_backend::placeholder_array_type(const std::string
& name
,
745 tree ret
= make_node(ARRAY_TYPE
);
746 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
747 get_identifier_from_string(name
),
749 TYPE_NAME(ret
) = decl
;
750 return this->make_type(ret
);
753 // Fill in the fields of a placeholder array type.
756 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
757 Btype
* element_btype
,
760 tree t
= placeholder
->get_tree();
761 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
762 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
764 // Build the data structure gcc wants to see for a typedef.
765 tree copy
= build_distinct_type_copy(t
);
766 TYPE_NAME(copy
) = NULL_TREE
;
767 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
769 return r
->get_tree() != error_mark_node
;
772 // Return a named version of a type.
775 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
778 tree type
= btype
->get_tree();
779 if (type
== error_mark_node
)
780 return this->error_type();
782 // The middle-end expects a basic type to have a name. In Go every
783 // basic type will have a name. The first time we see a basic type,
784 // give it whatever Go name we have at this point.
785 if (TYPE_NAME(type
) == NULL_TREE
786 && location
.gcc_location() == BUILTINS_LOCATION
787 && (TREE_CODE(type
) == INTEGER_TYPE
788 || TREE_CODE(type
) == REAL_TYPE
789 || TREE_CODE(type
) == COMPLEX_TYPE
790 || TREE_CODE(type
) == BOOLEAN_TYPE
))
792 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
793 get_identifier_from_string(name
),
795 TYPE_NAME(type
) = decl
;
796 return this->make_type(type
);
799 tree copy
= build_variant_type_copy(type
);
800 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
801 get_identifier_from_string(name
),
803 DECL_ORIGINAL_TYPE(decl
) = type
;
804 TYPE_NAME(copy
) = decl
;
805 return this->make_type(copy
);
808 // Return a pointer type used as a marker for a circular type.
811 Gcc_backend::circular_pointer_type(Btype
*, bool)
813 return this->make_type(ptr_type_node
);
816 // Return whether we might be looking at a circular type.
819 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
821 return btype
->get_tree() == ptr_type_node
;
824 // Return the size of a type.
827 Gcc_backend::type_size(Btype
* btype
)
829 tree t
= btype
->get_tree();
830 if (t
== error_mark_node
)
832 t
= TYPE_SIZE_UNIT(t
);
833 gcc_assert(TREE_CODE(t
) == INTEGER_CST
);
834 gcc_assert(TREE_INT_CST_HIGH(t
) == 0);
835 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
836 size_t ret
= static_cast<size_t>(val_wide
);
837 gcc_assert(ret
== val_wide
);
841 // Return the alignment of a type.
844 Gcc_backend::type_alignment(Btype
* btype
)
846 tree t
= btype
->get_tree();
847 if (t
== error_mark_node
)
849 return TYPE_ALIGN_UNIT(t
);
852 // Return the alignment of a struct field of type BTYPE.
855 Gcc_backend::type_field_alignment(Btype
* btype
)
857 tree t
= btype
->get_tree();
858 if (t
== error_mark_node
)
860 return go_field_alignment(t
);
863 // Return the offset of a field in a struct.
866 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
868 tree struct_tree
= btype
->get_tree();
869 if (struct_tree
== error_mark_node
)
871 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
872 tree field
= TYPE_FIELDS(struct_tree
);
873 for (; index
> 0; --index
)
875 field
= DECL_CHAIN(field
);
876 gcc_assert(field
!= NULL_TREE
);
878 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
879 gcc_assert(offset_wide
>= 0);
880 size_t ret
= static_cast<size_t>(offset_wide
);
881 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
885 // Return the zero value for a type.
888 Gcc_backend::zero_expression(Btype
* btype
)
890 tree t
= btype
->get_tree();
892 if (t
== error_mark_node
)
893 ret
= error_mark_node
;
895 ret
= build_zero_cst(t
);
896 return tree_to_expr(ret
);
899 // An expression that references a variable.
902 Gcc_backend::var_expression(Bvariable
* var
, Location
)
904 tree ret
= var
->get_tree();
905 if (ret
== error_mark_node
)
906 return this->error_expression();
907 return tree_to_expr(ret
);
910 // An expression that indirectly references an expression.
913 Gcc_backend::indirect_expression(Bexpression
* expr
, bool known_valid
,
916 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
919 TREE_THIS_NOTRAP(ret
) = 1;
920 return tree_to_expr(ret
);
923 // Return a typed value as a constant integer.
926 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
928 tree t
= btype
->get_tree();
929 if (t
== error_mark_node
)
930 return this->error_expression();
932 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
933 return tree_to_expr(ret
);
936 // Return a typed value as a constant floating-point number.
939 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
941 tree t
= btype
->get_tree();
943 if (t
== error_mark_node
)
944 return this->error_expression();
947 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
949 real_convert(&r2
, TYPE_MODE(t
), &r1
);
950 ret
= build_real(t
, r2
);
951 return tree_to_expr(ret
);
954 // Return a typed real and imaginary value as a constant complex number.
957 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
959 tree t
= btype
->get_tree();
961 if (t
== error_mark_node
)
962 return this->error_expression();
965 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
967 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
970 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
972 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
974 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
975 build_real(TREE_TYPE(t
), r4
));
976 return tree_to_expr(ret
);
979 // An expression that converts an expression to a different type.
982 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
, Location
)
984 tree type_tree
= type
->get_tree();
985 tree expr_tree
= expr
->get_tree();
986 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
987 return this->error_expression();
989 tree ret
= fold_convert(type_tree
, expr_tree
);
990 return tree_to_expr(ret
);
993 // Get the address of a function.
996 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
998 tree func
= bfunc
->get_tree();
999 if (func
== error_mark_node
)
1000 return this->error_expression();
1002 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1003 return this->make_expression(ret
);
1006 // Get the address of an expression.
1009 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1011 tree expr
= bexpr
->get_tree();
1012 if (expr
== error_mark_node
)
1013 return this->error_expression();
1015 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1016 return this->make_expression(ret
);
1019 // An expression as a statement.
1022 Gcc_backend::expression_statement(Bexpression
* expr
)
1024 return this->make_statement(expr
->get_tree());
1027 // Variable initialization.
1030 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1032 tree var_tree
= var
->get_tree();
1033 tree init_tree
= init
->get_tree();
1034 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1035 return this->error_statement();
1036 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1038 // To avoid problems with GNU ld, we don't make zero-sized
1039 // externally visible variables. That might lead us to doing an
1040 // initialization of a zero-sized expression to a non-zero sized
1041 // variable, or vice-versa. Avoid crashes by omitting the
1042 // initializer. Such initializations don't mean anything anyhow.
1043 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1044 && init_tree
!= NULL_TREE
1045 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1047 DECL_INITIAL(var_tree
) = init_tree
;
1048 init_tree
= NULL_TREE
;
1051 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1052 void_type_node
, var_tree
);
1053 if (init_tree
!= NULL_TREE
)
1054 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1055 void_type_node
, init_tree
, ret
);
1057 return this->make_statement(ret
);
1063 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1066 tree lhs_tree
= lhs
->get_tree();
1067 tree rhs_tree
= rhs
->get_tree();
1068 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1069 return this->error_statement();
1071 // To avoid problems with GNU ld, we don't make zero-sized
1072 // externally visible variables. That might lead us to doing an
1073 // assignment of a zero-sized expression to a non-zero sized
1074 // expression; avoid crashes here by avoiding assignments of
1075 // zero-sized expressions. Such assignments don't really mean
1077 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1078 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1079 return this->compound_statement(this->expression_statement(lhs
),
1080 this->expression_statement(rhs
));
1082 // Sometimes the same unnamed Go type can be created multiple times
1083 // and thus have multiple tree representations. Make sure this does
1084 // not confuse the middle-end.
1085 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1087 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1088 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1089 if (POINTER_TYPE_P(lhs_type_tree
)
1090 || INTEGRAL_TYPE_P(lhs_type_tree
)
1091 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1092 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1093 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1095 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1096 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1098 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1099 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1100 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1102 lhs_type_tree
, rhs_tree
);
1106 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1109 lhs_tree
, rhs_tree
));
1115 Gcc_backend::return_statement(Bfunction
* bfunction
,
1116 const std::vector
<Bexpression
*>& vals
,
1119 tree fntree
= bfunction
->get_tree();
1120 if (fntree
== error_mark_node
)
1121 return this->error_statement();
1122 tree result
= DECL_RESULT(fntree
);
1123 if (result
== error_mark_node
)
1124 return this->error_statement();
1127 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1129 else if (vals
.size() == 1)
1131 tree val
= vals
.front()->get_tree();
1132 if (val
== error_mark_node
)
1133 return this->error_statement();
1134 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1135 void_type_node
, result
,
1136 vals
.front()->get_tree());
1137 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1138 void_type_node
, set
);
1142 // To return multiple values, copy the values into a temporary
1143 // variable of the right structure type, and then assign the
1144 // temporary variable to the DECL_RESULT in the return
1146 tree stmt_list
= NULL_TREE
;
1147 tree rettype
= TREE_TYPE(result
);
1148 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1149 tree field
= TYPE_FIELDS(rettype
);
1150 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1152 p
++, field
= DECL_CHAIN(field
))
1154 gcc_assert(field
!= NULL_TREE
);
1155 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1156 TREE_TYPE(field
), rettmp
, field
,
1158 tree val
= (*p
)->get_tree();
1159 if (val
== error_mark_node
)
1160 return this->error_statement();
1161 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1163 ref
, (*p
)->get_tree());
1164 append_to_statement_list(set
, &stmt_list
);
1166 gcc_assert(field
== NULL_TREE
);
1167 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1170 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1171 void_type_node
, set
);
1172 append_to_statement_list(ret_expr
, &stmt_list
);
1175 return this->make_statement(ret
);
1181 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
1182 Bblock
* else_block
, Location location
)
1184 tree cond_tree
= condition
->get_tree();
1185 tree then_tree
= then_block
->get_tree();
1186 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
1187 if (cond_tree
== error_mark_node
1188 || then_tree
== error_mark_node
1189 || else_tree
== error_mark_node
)
1190 return this->error_statement();
1191 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
1192 cond_tree
, then_tree
, else_tree
);
1193 return this->make_statement(ret
);
1199 Gcc_backend::switch_statement(
1201 const std::vector
<std::vector
<Bexpression
*> >& cases
,
1202 const std::vector
<Bstatement
*>& statements
,
1203 Location switch_location
)
1205 gcc_assert(cases
.size() == statements
.size());
1207 tree stmt_list
= NULL_TREE
;
1208 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
1209 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
1210 ps
!= statements
.end();
1215 source_location loc
= (*ps
!= NULL
1216 ? EXPR_LOCATION((*ps
)->get_tree())
1217 : UNKNOWN_LOCATION
);
1218 tree label
= create_artificial_label(loc
);
1219 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
1220 append_to_statement_list(c
, &stmt_list
);
1224 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
1228 tree t
= (*pcv
)->get_tree();
1229 if (t
== error_mark_node
)
1230 return this->error_statement();
1231 source_location loc
= EXPR_LOCATION(t
);
1232 tree label
= create_artificial_label(loc
);
1233 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
1234 append_to_statement_list(c
, &stmt_list
);
1240 tree t
= (*ps
)->get_tree();
1241 if (t
== error_mark_node
)
1242 return this->error_statement();
1243 append_to_statement_list(t
, &stmt_list
);
1247 tree tv
= value
->get_tree();
1248 if (tv
== error_mark_node
)
1249 return this->error_statement();
1250 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
1251 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
1252 return this->make_statement(t
);
1255 // Pair of statements.
1258 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
1260 tree stmt_list
= NULL_TREE
;
1261 tree t
= s1
->get_tree();
1262 if (t
== error_mark_node
)
1263 return this->error_statement();
1264 append_to_statement_list(t
, &stmt_list
);
1266 if (t
== error_mark_node
)
1267 return this->error_statement();
1268 append_to_statement_list(t
, &stmt_list
);
1269 return this->make_statement(stmt_list
);
1272 // List of statements.
1275 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
1277 tree stmt_list
= NULL_TREE
;
1278 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1279 p
!= statements
.end();
1282 tree t
= (*p
)->get_tree();
1283 if (t
== error_mark_node
)
1284 return this->error_statement();
1285 append_to_statement_list(t
, &stmt_list
);
1287 return this->make_statement(stmt_list
);
1290 // Make a block. For some reason gcc uses a dual structure for
1291 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1292 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1296 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
1297 const std::vector
<Bvariable
*>& vars
,
1298 Location start_location
,
1301 tree block_tree
= make_node(BLOCK
);
1302 if (enclosing
== NULL
)
1304 // FIXME: Permitting FUNCTION to be NULL is a temporary measure
1305 // until we have a proper representation of the init function.
1307 if (function
== NULL
)
1308 fndecl
= current_function_decl
;
1310 fndecl
= function
->get_tree();
1311 gcc_assert(fndecl
!= NULL_TREE
);
1313 // We may have already created a block for local variables when
1314 // we take the address of a parameter.
1315 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1317 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
1318 DECL_INITIAL(fndecl
) = block_tree
;
1322 tree superblock_tree
= DECL_INITIAL(fndecl
);
1323 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1325 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1327 pp
= &BLOCK_CHAIN(*pp
))
1334 tree superbind_tree
= enclosing
->get_tree();
1335 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
1336 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
1338 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
1340 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1342 pp
= &BLOCK_CHAIN(*pp
))
1347 tree
* pp
= &BLOCK_VARS(block_tree
);
1348 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
1352 *pp
= (*pv
)->get_tree();
1353 if (*pp
!= error_mark_node
)
1354 pp
= &DECL_CHAIN(*pp
);
1358 TREE_USED(block_tree
) = 1;
1360 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
1361 void_type_node
, BLOCK_VARS(block_tree
),
1362 NULL_TREE
, block_tree
);
1363 TREE_SIDE_EFFECTS(bind_tree
) = 1;
1365 return new Bblock(bind_tree
);
1368 // Add statements to a block.
1371 Gcc_backend::block_add_statements(Bblock
* bblock
,
1372 const std::vector
<Bstatement
*>& statements
)
1374 tree stmt_list
= NULL_TREE
;
1375 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
1376 p
!= statements
.end();
1379 tree s
= (*p
)->get_tree();
1380 if (s
!= error_mark_node
)
1381 append_to_statement_list(s
, &stmt_list
);
1384 tree bind_tree
= bblock
->get_tree();
1385 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1386 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
1389 // Return a block as a statement.
1392 Gcc_backend::block_statement(Bblock
* bblock
)
1394 tree bind_tree
= bblock
->get_tree();
1395 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1396 return this->make_statement(bind_tree
);
1399 // This is not static because we declare it with GTY(()) in go-c.h.
1400 tree go_non_zero_struct
;
1402 // Return a type corresponding to TYPE with non-zero size.
1405 Gcc_backend::non_zero_size_type(tree type
)
1407 if (int_size_in_bytes(type
) != 0)
1410 switch (TREE_CODE(type
))
1413 if (TYPE_FIELDS(type
) != NULL_TREE
)
1415 tree ns
= make_node(RECORD_TYPE
);
1416 tree field_trees
= NULL_TREE
;
1417 tree
*pp
= &field_trees
;
1418 for (tree field
= TYPE_FIELDS(type
);
1420 field
= DECL_CHAIN(field
))
1422 tree ft
= TREE_TYPE(field
);
1423 if (field
== TYPE_FIELDS(type
))
1424 ft
= non_zero_size_type(ft
);
1425 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
1426 DECL_NAME(field
), ft
);
1427 DECL_CONTEXT(f
) = ns
;
1429 pp
= &DECL_CHAIN(f
);
1431 TYPE_FIELDS(ns
) = field_trees
;
1436 if (go_non_zero_struct
== NULL_TREE
)
1438 type
= make_node(RECORD_TYPE
);
1439 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
1440 get_identifier("dummy"),
1442 DECL_CONTEXT(field
) = type
;
1443 TYPE_FIELDS(type
) = field
;
1445 go_non_zero_struct
= type
;
1447 return go_non_zero_struct
;
1451 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
1452 return build_array_type_nelts(element_type
, 1);
1462 // Make a global variable.
1465 Gcc_backend::global_variable(const std::string
& package_name
,
1466 const std::string
& pkgpath
,
1467 const std::string
& name
,
1471 bool in_unique_section
,
1474 tree type_tree
= btype
->get_tree();
1475 if (type_tree
== error_mark_node
)
1476 return this->error_variable();
1478 // The GNU linker does not like dynamic variables with zero size.
1479 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
1480 type_tree
= this->non_zero_size_type(type_tree
);
1482 std::string
var_name(package_name
);
1483 var_name
.push_back('.');
1484 var_name
.append(name
);
1485 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1486 get_identifier_from_string(var_name
),
1489 DECL_EXTERNAL(decl
) = 1;
1491 TREE_STATIC(decl
) = 1;
1494 TREE_PUBLIC(decl
) = 1;
1496 std::string
asm_name(pkgpath
);
1497 asm_name
.push_back('.');
1498 asm_name
.append(name
);
1499 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1501 TREE_USED(decl
) = 1;
1503 if (in_unique_section
)
1504 resolve_unique_section (decl
, 0, 1);
1506 go_preserve_from_gc(decl
);
1508 return new Bvariable(decl
);
1511 // Set the initial value of a global variable.
1514 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
1516 tree expr_tree
= expr
->get_tree();
1517 if (expr_tree
== error_mark_node
)
1519 gcc_assert(TREE_CONSTANT(expr_tree
));
1520 tree var_decl
= var
->get_tree();
1521 if (var_decl
== error_mark_node
)
1523 DECL_INITIAL(var_decl
) = expr_tree
;
1525 // If this variable goes in a unique section, it may need to go into
1526 // a different one now that DECL_INITIAL is set.
1527 if (DECL_HAS_IMPLICIT_SECTION_NAME_P (var_decl
))
1529 DECL_SECTION_NAME (var_decl
) = NULL_TREE
;
1530 resolve_unique_section (var_decl
,
1531 compute_reloc_for_constant (expr_tree
),
1536 // Make a local variable.
1539 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
1540 Btype
* btype
, bool is_address_taken
,
1543 tree type_tree
= btype
->get_tree();
1544 if (type_tree
== error_mark_node
)
1545 return this->error_variable();
1546 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1547 get_identifier_from_string(name
),
1549 DECL_CONTEXT(decl
) = function
->get_tree();
1550 TREE_USED(decl
) = 1;
1551 if (is_address_taken
)
1552 TREE_ADDRESSABLE(decl
) = 1;
1553 go_preserve_from_gc(decl
);
1554 return new Bvariable(decl
);
1557 // Make a function parameter variable.
1560 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
1561 Btype
* btype
, bool is_address_taken
,
1564 tree type_tree
= btype
->get_tree();
1565 if (type_tree
== error_mark_node
)
1566 return this->error_variable();
1567 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
1568 get_identifier_from_string(name
),
1570 DECL_CONTEXT(decl
) = function
->get_tree();
1571 DECL_ARG_TYPE(decl
) = type_tree
;
1572 TREE_USED(decl
) = 1;
1573 if (is_address_taken
)
1574 TREE_ADDRESSABLE(decl
) = 1;
1575 go_preserve_from_gc(decl
);
1576 return new Bvariable(decl
);
1579 // Make a temporary variable.
1582 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
1583 Btype
* btype
, Bexpression
* binit
,
1584 bool is_address_taken
,
1586 Bstatement
** pstatement
)
1588 tree type_tree
= btype
->get_tree();
1589 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
1590 if (type_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1592 *pstatement
= this->error_statement();
1593 return this->error_variable();
1597 // We can only use create_tmp_var if the type is not addressable.
1598 if (!TREE_ADDRESSABLE(type_tree
))
1599 var
= create_tmp_var(type_tree
, "GOTMP");
1602 gcc_assert(bblock
!= NULL
);
1603 var
= build_decl(location
.gcc_location(), VAR_DECL
,
1604 create_tmp_var_name("GOTMP"),
1606 DECL_ARTIFICIAL(var
) = 1;
1607 DECL_IGNORED_P(var
) = 1;
1609 // FIXME: Permitting function to be NULL here is a temporary
1610 // measure until we have a proper representation of the init
1612 if (function
!= NULL
)
1613 DECL_CONTEXT(var
) = function
->get_tree();
1616 gcc_assert(current_function_decl
!= NULL_TREE
);
1617 DECL_CONTEXT(var
) = current_function_decl
;
1620 // We have to add this variable to the BLOCK and the BIND_EXPR.
1621 tree bind_tree
= bblock
->get_tree();
1622 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
1623 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
1624 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
1625 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
1626 BLOCK_VARS(block_tree
) = var
;
1627 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
1630 if (init_tree
!= NULL_TREE
)
1631 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
1634 if (is_address_taken
)
1635 TREE_ADDRESSABLE(var
) = 1;
1637 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
1639 void_type_node
, var
));
1640 return new Bvariable(var
);
1643 // Create a named immutable initialized data structure.
1646 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
1647 bool, Btype
* btype
, Location location
)
1649 tree type_tree
= btype
->get_tree();
1650 if (type_tree
== error_mark_node
)
1651 return this->error_variable();
1652 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1653 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1654 get_identifier_from_string(name
),
1655 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1656 TREE_STATIC(decl
) = 1;
1657 TREE_READONLY(decl
) = 1;
1658 TREE_CONSTANT(decl
) = 1;
1659 TREE_USED(decl
) = 1;
1660 DECL_ARTIFICIAL(decl
) = 1;
1662 TREE_PUBLIC(decl
) = 1;
1664 // We don't call rest_of_decl_compilation until we have the
1667 go_preserve_from_gc(decl
);
1668 return new Bvariable(decl
);
1671 // Set the initializer for a variable created by immutable_struct.
1672 // This is where we finish compiling the variable.
1675 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
1676 bool, bool is_common
, Btype
*, Location
,
1677 Bexpression
* initializer
)
1679 tree decl
= var
->get_tree();
1680 tree init_tree
= initializer
->get_tree();
1681 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
1684 DECL_INITIAL(decl
) = init_tree
;
1686 // We can't call make_decl_one_only until we set DECL_INITIAL.
1688 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
1690 // These variables are often unneeded in the final program, so put
1691 // them in their own section so that linker GC can discard them.
1692 resolve_unique_section(decl
,
1693 compute_reloc_for_constant (init_tree
),
1696 rest_of_decl_compilation(decl
, 1, 0);
1699 // Return a reference to an immutable initialized data structure
1700 // defined in another package.
1703 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
1706 tree type_tree
= btype
->get_tree();
1707 if (type_tree
== error_mark_node
)
1708 return this->error_variable();
1709 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
1710 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
1711 get_identifier_from_string(name
),
1712 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
1713 TREE_READONLY(decl
) = 1;
1714 TREE_CONSTANT(decl
) = 1;
1715 DECL_ARTIFICIAL(decl
) = 1;
1716 TREE_PUBLIC(decl
) = 1;
1717 DECL_EXTERNAL(decl
) = 1;
1718 go_preserve_from_gc(decl
);
1719 return new Bvariable(decl
);
1725 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
1730 decl
= create_artificial_label(location
.gcc_location());
1733 tree id
= get_identifier_from_string(name
);
1734 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
1736 DECL_CONTEXT(decl
) = function
->get_tree();
1738 return new Blabel(decl
);
1741 // Make a statement which defines a label.
1744 Gcc_backend::label_definition_statement(Blabel
* label
)
1746 tree lab
= label
->get_tree();
1747 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
1748 void_type_node
, lab
);
1749 return this->make_statement(ret
);
1752 // Make a goto statement.
1755 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
1757 tree lab
= label
->get_tree();
1758 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
1760 return this->make_statement(ret
);
1763 // Get the address of a label.
1766 Gcc_backend::label_address(Blabel
* label
, Location location
)
1768 tree lab
= label
->get_tree();
1770 TREE_ADDRESSABLE(lab
) = 1;
1771 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
1772 build_fold_addr_expr_loc(location
.gcc_location(),
1774 return this->make_expression(ret
);
1777 // Declare or define a new function.
1780 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
1781 const std::string
& asm_name
, bool is_visible
,
1782 bool is_declaration
, bool is_inlinable
,
1783 bool disable_split_stack
, bool in_unique_section
,
1786 tree functype
= fntype
->get_tree();
1787 if (functype
!= error_mark_node
)
1789 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
1790 functype
= TREE_TYPE(functype
);
1792 tree id
= get_identifier_from_string(name
);
1793 if (functype
== error_mark_node
|| id
== error_mark_node
)
1794 return this->error_function();
1796 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
1797 if (!asm_name
.empty())
1798 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
1800 TREE_PUBLIC(decl
) = 1;
1802 DECL_EXTERNAL(decl
) = 1;
1805 tree restype
= TREE_TYPE(functype
);
1807 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
1808 DECL_ARTIFICIAL(resdecl
) = 1;
1809 DECL_IGNORED_P(resdecl
) = 1;
1810 DECL_CONTEXT(resdecl
) = decl
;
1811 DECL_RESULT(decl
) = resdecl
;
1814 DECL_UNINLINABLE(decl
) = 1;
1815 if (disable_split_stack
)
1817 tree attr
= get_identifier("__no_split_stack__");
1818 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1820 if (in_unique_section
)
1821 resolve_unique_section(decl
, 0, 1);
1823 go_preserve_from_gc(decl
);
1824 return new Bfunction(decl
);
1827 // The single backend.
1829 static Gcc_backend gcc_backend
;
1831 // Return the backend generator.
1836 return &gcc_backend
;
1839 // FIXME: Temporary functions while converting to the new backend
1843 tree_to_type(tree t
)
1845 return new Btype(t
);
1849 tree_to_expr(tree t
)
1851 return new Bexpression(t
);
1855 tree_to_stat(tree t
)
1857 return new Bstatement(t
);
1861 tree_to_function(tree t
)
1863 return new Bfunction(t
);
1867 tree_to_block(tree t
)
1869 gcc_assert(TREE_CODE(t
) == BIND_EXPR
);
1870 return new Bblock(t
);
1874 type_to_tree(Btype
* bt
)
1876 return bt
->get_tree();
1880 expr_to_tree(Bexpression
* be
)
1882 return be
->get_tree();
1886 stat_to_tree(Bstatement
* bs
)
1888 return bs
->get_tree();
1892 block_to_tree(Bblock
* bb
)
1894 return bb
->get_tree();
1898 var_to_tree(Bvariable
* bv
)
1900 return bv
->get_tree();
1904 function_to_tree(Bfunction
* bf
)
1906 return bf
->get_tree();