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"
34 #include "basic-block.h"
35 #include "gimple-expr.h"
37 #include "langhooks.h"
49 // A class wrapping a tree.
70 // In gcc, types, expressions, and statements are all trees.
71 class Btype
: public Gcc_tree
79 class Bexpression
: public Gcc_tree
87 class Bstatement
: public Gcc_tree
95 class Bfunction
: public Gcc_tree
103 class Bblock
: public Gcc_tree
111 class Bvariable
: public Gcc_tree
119 class Blabel
: public Gcc_tree
127 // This file implements the interface between the Go frontend proper
128 // and the gcc IR. This implements specific instantiations of
129 // abstract classes defined by the Go frontend proper. The Go
130 // frontend proper class methods of these classes to generate the
131 // backend representation.
133 class Gcc_backend
: public Backend
142 { return this->make_type(error_mark_node
); }
146 { return this->make_type(void_type_node
); }
150 { return this->make_type(boolean_type_node
); }
153 integer_type(bool, int);
162 pointer_type(Btype
*);
165 function_type(const Btyped_identifier
&,
166 const std::vector
<Btyped_identifier
>&,
167 const std::vector
<Btyped_identifier
>&,
172 struct_type(const std::vector
<Btyped_identifier
>&);
175 array_type(Btype
*, Bexpression
*);
178 placeholder_pointer_type(const std::string
&, Location
, bool);
181 set_placeholder_pointer_type(Btype
*, Btype
*);
184 set_placeholder_function_type(Btype
*, Btype
*);
187 placeholder_struct_type(const std::string
&, Location
);
190 set_placeholder_struct_type(Btype
* placeholder
,
191 const std::vector
<Btyped_identifier
>&);
194 placeholder_array_type(const std::string
&, Location
);
197 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
200 named_type(const std::string
&, Btype
*, Location
);
203 circular_pointer_type(Btype
*, bool);
206 is_circular_pointer_type(Btype
*);
212 type_alignment(Btype
*);
215 type_field_alignment(Btype
*);
218 type_field_offset(Btype
*, size_t index
);
223 zero_expression(Btype
*);
227 { return this->make_expression(error_mark_node
); }
230 nil_pointer_expression()
231 { return this->make_expression(null_pointer_node
); }
234 var_expression(Bvariable
* var
, Location
);
237 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
240 named_constant_expression(Btype
* btype
, const std::string
& name
,
241 Bexpression
* val
, Location
);
244 integer_constant_expression(Btype
* btype
, mpz_t val
);
247 float_constant_expression(Btype
* btype
, mpfr_t val
);
250 complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
);
253 string_constant_expression(const std::string
& val
);
256 boolean_constant_expression(bool val
);
259 real_part_expression(Bexpression
* bcomplex
, Location
);
262 imag_part_expression(Bexpression
* bcomplex
, Location
);
265 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
268 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
271 function_code_expression(Bfunction
*, Location
);
274 address_expression(Bexpression
*, Location
);
277 struct_field_expression(Bexpression
*, size_t, Location
);
280 compound_expression(Bstatement
*, Bexpression
*, Location
);
283 conditional_expression(Btype
*, Bexpression
*, Bexpression
*, Bexpression
*,
287 unary_expression(Operator
, Bexpression
*, Location
);
290 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
293 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
296 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
297 const std::vector
<Bexpression
*>&, Location
);
300 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
303 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
306 call_expression(Bexpression
* fn
, const std::vector
<Bexpression
*>& args
,
313 { return this->make_statement(error_mark_node
); }
316 expression_statement(Bexpression
*);
319 init_statement(Bvariable
* var
, Bexpression
* init
);
322 assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
, Location
);
325 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
329 if_statement(Bexpression
* condition
, Bblock
* then_block
, Bblock
* else_block
,
333 switch_statement(Bfunction
* function
, Bexpression
* value
,
334 const std::vector
<std::vector
<Bexpression
*> >& cases
,
335 const std::vector
<Bstatement
*>& statements
,
339 compound_statement(Bstatement
*, Bstatement
*);
342 statement_list(const std::vector
<Bstatement
*>&);
345 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
346 Bstatement
* finally_stmt
, Location
);
351 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
355 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
358 block_statement(Bblock
*);
364 { return new Bvariable(error_mark_node
); }
367 global_variable(const std::string
& package_name
,
368 const std::string
& pkgpath
,
369 const std::string
& name
,
373 bool in_unique_section
,
377 global_variable_set_init(Bvariable
*, Bexpression
*);
380 local_variable(Bfunction
*, const std::string
&, Btype
*, bool,
384 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
388 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
389 Location
, Bstatement
**);
392 implicit_variable(const std::string
&, Btype
*, bool, bool, bool,
396 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
397 bool, bool, bool, Bexpression
*);
400 implicit_variable_reference(const std::string
&, Btype
*);
403 immutable_struct(const std::string
&, bool, bool, Btype
*, Location
);
406 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
407 Location
, Bexpression
*);
410 immutable_struct_reference(const std::string
&, Btype
*, Location
);
415 label(Bfunction
*, const std::string
& name
, Location
);
418 label_definition_statement(Blabel
*);
421 goto_statement(Blabel
*, Location
);
424 label_address(Blabel
*, Location
);
430 { return this->make_function(error_mark_node
); }
433 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
434 bool is_visible
, bool is_declaration
, bool is_inlinable
,
435 bool disable_split_stack
, bool in_unique_section
, Location
);
438 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
439 Bexpression
* defer
, Location
);
442 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
445 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
448 lookup_builtin(const std::string
&);
451 write_global_definitions(const std::vector
<Btype
*>&,
452 const std::vector
<Bexpression
*>&,
453 const std::vector
<Bfunction
*>&,
454 const std::vector
<Bvariable
*>&);
457 // Make a Bexpression from a tree.
459 make_expression(tree t
)
460 { return new Bexpression(t
); }
462 // Make a Bstatement from a tree.
464 make_statement(tree t
)
465 { return new Bstatement(t
); }
467 // Make a Btype from a tree.
470 { return new Btype(t
); }
473 make_function(tree t
)
474 { return new Bfunction(t
); }
477 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
480 fill_in_array(Btype
*, Btype
*, Bexpression
*);
483 non_zero_size_type(tree
);
487 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
488 tree fntype
, bool const_p
);
490 // A mapping of the GCC built-ins exposed to GCCGo.
491 std::map
<std::string
, Bfunction
*> builtin_functions_
;
494 // A helper function.
497 get_identifier_from_string(const std::string
& str
)
499 return get_identifier_with_length(str
.data(), str
.length());
502 // Define the built-in functions that are exposed to GCCGo.
504 Gcc_backend::Gcc_backend()
506 /* We need to define the fetch_and_add functions, since we use them
508 tree t
= this->integer_type(BITS_PER_UNIT
, 1)->get_tree();
509 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
510 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
511 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
514 t
= this->integer_type(BITS_PER_UNIT
* 2, 1)->get_tree();
515 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
516 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
517 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
520 t
= this->integer_type(BITS_PER_UNIT
* 4, 1)->get_tree();
521 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
522 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
523 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
526 t
= this->integer_type(BITS_PER_UNIT
* 8, 1)->get_tree();
527 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
528 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
529 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
532 // We use __builtin_expect for magic import functions.
533 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
534 build_function_type_list(long_integer_type_node
,
535 long_integer_type_node
,
536 long_integer_type_node
,
540 // We use __builtin_memcmp for struct comparisons.
541 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
542 build_function_type_list(integer_type_node
,
549 // We provide some functions for the math library.
550 tree math_function_type
= build_function_type_list(double_type_node
,
553 tree math_function_type_long
=
554 build_function_type_list(long_double_type_node
, long_double_type_node
,
555 long_double_type_node
, NULL_TREE
);
556 tree math_function_type_two
= build_function_type_list(double_type_node
,
560 tree math_function_type_long_two
=
561 build_function_type_list(long_double_type_node
, long_double_type_node
,
562 long_double_type_node
, NULL_TREE
);
563 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
564 math_function_type
, true);
565 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
566 math_function_type_long
, true);
567 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
568 math_function_type
, true);
569 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
570 math_function_type_long
, true);
571 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
572 math_function_type
, true);
573 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
574 math_function_type_long
, true);
575 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
576 math_function_type_two
, true);
577 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
578 math_function_type_long_two
, true);
579 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
580 math_function_type
, true);
581 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
582 math_function_type_long
, true);
583 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
584 math_function_type
, true);
585 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
586 math_function_type_long
, true);
587 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
588 math_function_type
, true);
589 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
590 math_function_type_long
, true);
591 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
592 math_function_type
, true);
593 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
594 math_function_type_long
, true);
595 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
596 math_function_type
, true);
597 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
598 math_function_type_long
, true);
599 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
600 math_function_type
, true);
601 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
602 math_function_type_long
, true);
603 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
604 math_function_type_two
, true);
605 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
606 math_function_type_long_two
, true);
607 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
608 build_function_type_list(double_type_node
,
613 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
614 build_function_type_list(long_double_type_node
,
615 long_double_type_node
,
619 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
620 math_function_type
, true);
621 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
622 math_function_type_long
, true);
623 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
624 math_function_type
, true);
625 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
626 math_function_type_long
, true);
627 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
628 math_function_type
, true);
629 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
630 math_function_type_long
, true);
631 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
632 math_function_type
, true);
633 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
634 math_function_type_long
, true);
635 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
636 math_function_type
, true);
637 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
638 math_function_type_long
, true);
639 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
640 math_function_type
, true);
641 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
642 math_function_type_long
, true);
643 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
644 math_function_type
, true);
645 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
646 math_function_type_long
, true);
647 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
648 math_function_type
, true);
649 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
650 math_function_type_long
, true);
652 // We use __builtin_return_address in the thunk we build for
653 // functions which call recover.
654 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
656 build_function_type_list(ptr_type_node
,
661 // The compiler uses __builtin_trap for some exception handling
663 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
664 build_function_type(void_type_node
, void_list_node
),
668 // Get an unnamed integer type.
671 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
676 if (bits
== INT_TYPE_SIZE
)
677 type
= unsigned_type_node
;
678 else if (bits
== CHAR_TYPE_SIZE
)
679 type
= unsigned_char_type_node
;
680 else if (bits
== SHORT_TYPE_SIZE
)
681 type
= short_unsigned_type_node
;
682 else if (bits
== LONG_TYPE_SIZE
)
683 type
= long_unsigned_type_node
;
684 else if (bits
== LONG_LONG_TYPE_SIZE
)
685 type
= long_long_unsigned_type_node
;
687 type
= make_unsigned_type(bits
);
691 if (bits
== INT_TYPE_SIZE
)
692 type
= integer_type_node
;
693 else if (bits
== CHAR_TYPE_SIZE
)
694 type
= signed_char_type_node
;
695 else if (bits
== SHORT_TYPE_SIZE
)
696 type
= short_integer_type_node
;
697 else if (bits
== LONG_TYPE_SIZE
)
698 type
= long_integer_type_node
;
699 else if (bits
== LONG_LONG_TYPE_SIZE
)
700 type
= long_long_integer_type_node
;
702 type
= make_signed_type(bits
);
704 return this->make_type(type
);
707 // Get an unnamed float type.
710 Gcc_backend::float_type(int bits
)
713 if (bits
== FLOAT_TYPE_SIZE
)
714 type
= float_type_node
;
715 else if (bits
== DOUBLE_TYPE_SIZE
)
716 type
= double_type_node
;
717 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
718 type
= long_double_type_node
;
721 type
= make_node(REAL_TYPE
);
722 TYPE_PRECISION(type
) = bits
;
725 return this->make_type(type
);
728 // Get an unnamed complex type.
731 Gcc_backend::complex_type(int bits
)
734 if (bits
== FLOAT_TYPE_SIZE
* 2)
735 type
= complex_float_type_node
;
736 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
737 type
= complex_double_type_node
;
738 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
739 type
= complex_long_double_type_node
;
742 type
= make_node(REAL_TYPE
);
743 TYPE_PRECISION(type
) = bits
/ 2;
745 type
= build_complex_type(type
);
747 return this->make_type(type
);
750 // Get a pointer type.
753 Gcc_backend::pointer_type(Btype
* to_type
)
755 tree to_type_tree
= to_type
->get_tree();
756 if (to_type_tree
== error_mark_node
)
757 return this->error_type();
758 tree type
= build_pointer_type(to_type_tree
);
759 return this->make_type(type
);
762 // Make a function type.
765 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
766 const std::vector
<Btyped_identifier
>& parameters
,
767 const std::vector
<Btyped_identifier
>& results
,
768 Btype
* result_struct
,
771 tree args
= NULL_TREE
;
773 if (receiver
.btype
!= NULL
)
775 tree t
= receiver
.btype
->get_tree();
776 if (t
== error_mark_node
)
777 return this->error_type();
778 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
779 pp
= &TREE_CHAIN(*pp
);
782 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
783 p
!= parameters
.end();
786 tree t
= p
->btype
->get_tree();
787 if (t
== error_mark_node
)
788 return this->error_type();
789 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
790 pp
= &TREE_CHAIN(*pp
);
793 // Varargs is handled entirely at the Go level. When converted to
794 // GENERIC functions are not varargs.
795 *pp
= void_list_node
;
799 result
= void_type_node
;
800 else if (results
.size() == 1)
801 result
= results
.front().btype
->get_tree();
804 gcc_assert(result_struct
!= NULL
);
805 result
= result_struct
->get_tree();
807 if (result
== error_mark_node
)
808 return this->error_type();
810 tree fntype
= build_function_type(result
, args
);
811 if (fntype
== error_mark_node
)
812 return this->error_type();
814 return this->make_type(build_pointer_type(fntype
));
817 // Make a struct type.
820 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
822 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
825 // Fill in the fields of a struct type.
828 Gcc_backend::fill_in_struct(Btype
* fill
,
829 const std::vector
<Btyped_identifier
>& fields
)
831 tree fill_tree
= fill
->get_tree();
832 tree field_trees
= NULL_TREE
;
833 tree
* pp
= &field_trees
;
834 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
838 tree name_tree
= get_identifier_from_string(p
->name
);
839 tree type_tree
= p
->btype
->get_tree();
840 if (type_tree
== error_mark_node
)
841 return this->error_type();
842 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
844 DECL_CONTEXT(field
) = fill_tree
;
846 pp
= &DECL_CHAIN(field
);
848 TYPE_FIELDS(fill_tree
) = field_trees
;
849 layout_type(fill_tree
);
853 // Make an array type.
856 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
858 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
859 element_btype
, length
);
862 // Fill in an array type.
865 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
868 tree element_type_tree
= element_type
->get_tree();
869 tree length_tree
= length
->get_tree();
870 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
871 return this->error_type();
873 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
875 length_tree
= fold_convert(sizetype
, length_tree
);
877 // build_index_type takes the maximum index, which is one less than
879 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
883 tree fill_tree
= fill
->get_tree();
884 TREE_TYPE(fill_tree
) = element_type_tree
;
885 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
886 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
887 layout_type(fill_tree
);
889 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
890 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
891 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
892 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
893 TYPE_CANONICAL(fill_tree
) =
894 build_array_type(TYPE_CANONICAL(element_type_tree
),
895 TYPE_CANONICAL(index_type_tree
));
900 // Create a placeholder for a pointer type.
903 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
904 Location location
, bool)
906 tree ret
= build_distinct_type_copy(ptr_type_node
);
909 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
910 get_identifier_from_string(name
),
912 TYPE_NAME(ret
) = decl
;
914 return this->make_type(ret
);
917 // Set the real target type for a placeholder pointer type.
920 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
923 tree pt
= placeholder
->get_tree();
924 if (pt
== error_mark_node
)
926 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
927 tree tt
= to_type
->get_tree();
928 if (tt
== error_mark_node
)
930 placeholder
->set_tree(error_mark_node
);
933 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
934 TREE_TYPE(pt
) = TREE_TYPE(tt
);
935 if (TYPE_NAME(pt
) != NULL_TREE
)
937 // Build the data structure gcc wants to see for a typedef.
938 tree copy
= build_variant_type_copy(pt
);
939 TYPE_NAME(copy
) = NULL_TREE
;
940 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
945 // Set the real values for a placeholder function type.
948 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
950 return this->set_placeholder_pointer_type(placeholder
, ft
);
953 // Create a placeholder for a struct type.
956 Gcc_backend::placeholder_struct_type(const std::string
& name
,
959 tree ret
= make_node(RECORD_TYPE
);
962 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
963 get_identifier_from_string(name
),
965 TYPE_NAME(ret
) = decl
;
967 return this->make_type(ret
);
970 // Fill in the fields of a placeholder struct type.
973 Gcc_backend::set_placeholder_struct_type(
975 const std::vector
<Btyped_identifier
>& fields
)
977 tree t
= placeholder
->get_tree();
978 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
979 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
981 if (TYPE_NAME(t
) != NULL_TREE
)
983 // Build the data structure gcc wants to see for a typedef.
984 tree copy
= build_distinct_type_copy(t
);
985 TYPE_NAME(copy
) = NULL_TREE
;
986 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
989 return r
->get_tree() != error_mark_node
;
992 // Create a placeholder for an array type.
995 Gcc_backend::placeholder_array_type(const std::string
& name
,
998 tree ret
= make_node(ARRAY_TYPE
);
999 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1000 get_identifier_from_string(name
),
1002 TYPE_NAME(ret
) = decl
;
1003 return this->make_type(ret
);
1006 // Fill in the fields of a placeholder array type.
1009 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1010 Btype
* element_btype
,
1011 Bexpression
* length
)
1013 tree t
= placeholder
->get_tree();
1014 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1015 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1017 // Build the data structure gcc wants to see for a typedef.
1018 tree copy
= build_distinct_type_copy(t
);
1019 TYPE_NAME(copy
) = NULL_TREE
;
1020 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1022 return r
->get_tree() != error_mark_node
;
1025 // Return a named version of a type.
1028 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1031 tree type
= btype
->get_tree();
1032 if (type
== error_mark_node
)
1033 return this->error_type();
1035 // The middle-end expects a basic type to have a name. In Go every
1036 // basic type will have a name. The first time we see a basic type,
1037 // give it whatever Go name we have at this point.
1038 if (TYPE_NAME(type
) == NULL_TREE
1039 && location
.gcc_location() == BUILTINS_LOCATION
1040 && (TREE_CODE(type
) == INTEGER_TYPE
1041 || TREE_CODE(type
) == REAL_TYPE
1042 || TREE_CODE(type
) == COMPLEX_TYPE
1043 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1045 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1046 get_identifier_from_string(name
),
1048 TYPE_NAME(type
) = decl
;
1049 return this->make_type(type
);
1052 tree copy
= build_variant_type_copy(type
);
1053 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1054 get_identifier_from_string(name
),
1056 DECL_ORIGINAL_TYPE(decl
) = type
;
1057 TYPE_NAME(copy
) = decl
;
1058 return this->make_type(copy
);
1061 // Return a pointer type used as a marker for a circular type.
1064 Gcc_backend::circular_pointer_type(Btype
*, bool)
1066 return this->make_type(ptr_type_node
);
1069 // Return whether we might be looking at a circular type.
1072 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1074 return btype
->get_tree() == ptr_type_node
;
1077 // Return the size of a type.
1080 Gcc_backend::type_size(Btype
* btype
)
1082 tree t
= btype
->get_tree();
1083 if (t
== error_mark_node
)
1085 t
= TYPE_SIZE_UNIT(t
);
1086 gcc_assert(tree_fits_uhwi_p (t
));
1087 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1088 size_t ret
= static_cast<size_t>(val_wide
);
1089 gcc_assert(ret
== val_wide
);
1093 // Return the alignment of a type.
1096 Gcc_backend::type_alignment(Btype
* btype
)
1098 tree t
= btype
->get_tree();
1099 if (t
== error_mark_node
)
1101 return TYPE_ALIGN_UNIT(t
);
1104 // Return the alignment of a struct field of type BTYPE.
1107 Gcc_backend::type_field_alignment(Btype
* btype
)
1109 tree t
= btype
->get_tree();
1110 if (t
== error_mark_node
)
1112 return go_field_alignment(t
);
1115 // Return the offset of a field in a struct.
1118 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1120 tree struct_tree
= btype
->get_tree();
1121 if (struct_tree
== error_mark_node
)
1123 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1124 tree field
= TYPE_FIELDS(struct_tree
);
1125 for (; index
> 0; --index
)
1127 field
= DECL_CHAIN(field
);
1128 gcc_assert(field
!= NULL_TREE
);
1130 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1131 gcc_assert(offset_wide
>= 0);
1132 size_t ret
= static_cast<size_t>(offset_wide
);
1133 gcc_assert(ret
== static_cast<unsigned HOST_WIDE_INT
>(offset_wide
));
1137 // Return the zero value for a type.
1140 Gcc_backend::zero_expression(Btype
* btype
)
1142 tree t
= btype
->get_tree();
1144 if (t
== error_mark_node
)
1145 ret
= error_mark_node
;
1147 ret
= build_zero_cst(t
);
1148 return this->make_expression(ret
);
1151 // An expression that references a variable.
1154 Gcc_backend::var_expression(Bvariable
* var
, Location
)
1156 tree ret
= var
->get_tree();
1157 if (ret
== error_mark_node
)
1158 return this->error_expression();
1159 return this->make_expression(ret
);
1162 // An expression that indirectly references an expression.
1165 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1166 bool known_valid
, Location location
)
1168 tree expr_tree
= expr
->get_tree();
1169 tree type_tree
= btype
->get_tree();
1170 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1171 return this->error_expression();
1173 // If the type of EXPR is a recursive pointer type, then we
1174 // need to insert a cast before indirecting.
1175 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1176 if (VOID_TYPE_P(target_type_tree
))
1177 expr_tree
= fold_convert_loc(location
.gcc_location(),
1178 build_pointer_type(type_tree
), expr_tree
);
1180 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1183 TREE_THIS_NOTRAP(ret
) = 1;
1184 return this->make_expression(ret
);
1187 // Return an expression that declares a constant named NAME with the
1188 // constant value VAL in BTYPE.
1191 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1192 Bexpression
* val
, Location location
)
1194 tree type_tree
= btype
->get_tree();
1195 tree const_val
= val
->get_tree();
1196 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1197 return this->error_expression();
1199 tree name_tree
= get_identifier_from_string(name
);
1200 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1202 DECL_INITIAL(decl
) = const_val
;
1203 TREE_CONSTANT(decl
) = 1;
1204 TREE_READONLY(decl
) = 1;
1206 go_preserve_from_gc(decl
);
1207 return this->make_expression(decl
);
1210 // Return a typed value as a constant integer.
1213 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1215 tree t
= btype
->get_tree();
1216 if (t
== error_mark_node
)
1217 return this->error_expression();
1219 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1220 return this->make_expression(ret
);
1223 // Return a typed value as a constant floating-point number.
1226 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1228 tree t
= btype
->get_tree();
1230 if (t
== error_mark_node
)
1231 return this->error_expression();
1234 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1236 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1237 ret
= build_real(t
, r2
);
1238 return this->make_expression(ret
);
1241 // Return a typed real and imaginary value as a constant complex number.
1244 Gcc_backend::complex_constant_expression(Btype
* btype
, mpfr_t real
, mpfr_t imag
)
1246 tree t
= btype
->get_tree();
1248 if (t
== error_mark_node
)
1249 return this->error_expression();
1252 real_from_mpfr(&r1
, real
, TREE_TYPE(t
), GMP_RNDN
);
1254 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1257 real_from_mpfr(&r3
, imag
, TREE_TYPE(t
), GMP_RNDN
);
1259 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1261 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1262 build_real(TREE_TYPE(t
), r4
));
1263 return this->make_expression(ret
);
1266 // Make a constant string expression.
1269 Gcc_backend::string_constant_expression(const std::string
& val
)
1271 tree index_type
= build_index_type(size_int(val
.length()));
1272 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1274 tree string_type
= build_array_type(const_char_type
, index_type
);
1275 string_type
= build_variant_type_copy(string_type
);
1276 TYPE_STRING_FLAG(string_type
) = 1;
1277 tree string_val
= build_string(val
.length(), val
.data());
1278 TREE_TYPE(string_val
) = string_type
;
1280 return this->make_expression(string_val
);
1283 // Make a constant boolean expression.
1286 Gcc_backend::boolean_constant_expression(bool val
)
1288 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1289 return this->make_expression(bool_cst
);
1292 // Return the real part of a complex expression.
1295 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1297 tree complex_tree
= bcomplex
->get_tree();
1298 if (complex_tree
== error_mark_node
)
1299 return this->error_expression();
1300 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1301 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1302 TREE_TYPE(TREE_TYPE(complex_tree
)),
1304 return this->make_expression(ret
);
1307 // Return the imaginary part of a complex expression.
1310 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1312 tree complex_tree
= bcomplex
->get_tree();
1313 if (complex_tree
== error_mark_node
)
1314 return this->error_expression();
1315 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1316 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1317 TREE_TYPE(TREE_TYPE(complex_tree
)),
1319 return this->make_expression(ret
);
1322 // Make a complex expression given its real and imaginary parts.
1325 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1328 tree real_tree
= breal
->get_tree();
1329 tree imag_tree
= bimag
->get_tree();
1330 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1331 return this->error_expression();
1332 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1333 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1334 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1335 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1336 build_complex_type(TREE_TYPE(real_tree
)),
1337 real_tree
, imag_tree
);
1338 return this->make_expression(ret
);
1341 // An expression that converts an expression to a different type.
1344 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1347 tree type_tree
= type
->get_tree();
1348 tree expr_tree
= expr
->get_tree();
1349 if (type_tree
== error_mark_node
1350 || expr_tree
== error_mark_node
1351 || TREE_TYPE(expr_tree
) == error_mark_node
)
1352 return this->error_expression();
1355 if (this->type_size(type
) == 0)
1357 // Do not convert zero-sized types.
1360 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1361 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1362 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1363 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1364 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1365 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1366 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1367 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1368 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1369 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1370 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1371 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1372 type_tree
, expr_tree
);
1374 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1376 return this->make_expression(ret
);
1379 // Get the address of a function.
1382 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1384 tree func
= bfunc
->get_tree();
1385 if (func
== error_mark_node
)
1386 return this->error_expression();
1388 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1389 return this->make_expression(ret
);
1392 // Get the address of an expression.
1395 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1397 tree expr
= bexpr
->get_tree();
1398 if (expr
== error_mark_node
)
1399 return this->error_expression();
1401 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1402 return this->make_expression(ret
);
1405 // Return an expression for the field at INDEX in BSTRUCT.
1408 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1411 tree struct_tree
= bstruct
->get_tree();
1412 if (struct_tree
== error_mark_node
1413 || TREE_TYPE(struct_tree
) == error_mark_node
)
1414 return this->error_expression();
1415 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1416 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1417 if (field
== NULL_TREE
)
1419 // This can happen for a type which refers to itself indirectly
1420 // and then turns out to be erroneous.
1421 return this->error_expression();
1423 for (unsigned int i
= index
; i
> 0; --i
)
1425 field
= DECL_CHAIN(field
);
1426 gcc_assert(field
!= NULL_TREE
);
1428 if (TREE_TYPE(field
) == error_mark_node
)
1429 return this->error_expression();
1430 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1431 TREE_TYPE(field
), struct_tree
, field
,
1433 if (TREE_CONSTANT(struct_tree
))
1434 TREE_CONSTANT(ret
) = 1;
1435 return this->make_expression(ret
);
1438 // Return an expression that executes BSTAT before BEXPR.
1441 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1444 tree stat
= bstat
->get_tree();
1445 tree expr
= bexpr
->get_tree();
1446 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1447 return this->error_expression();
1448 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1449 TREE_TYPE(expr
), stat
, expr
);
1450 return this->make_expression(ret
);
1453 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1454 // ELSE_EXPR otherwise.
1457 Gcc_backend::conditional_expression(Btype
* btype
, Bexpression
* condition
,
1458 Bexpression
* then_expr
,
1459 Bexpression
* else_expr
, Location location
)
1461 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1462 tree cond_tree
= condition
->get_tree();
1463 tree then_tree
= then_expr
->get_tree();
1464 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1465 if (type_tree
== error_mark_node
1466 || cond_tree
== error_mark_node
1467 || then_tree
== error_mark_node
1468 || else_tree
== error_mark_node
)
1469 return this->error_expression();
1470 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1471 cond_tree
, then_tree
, else_tree
);
1472 return this->make_expression(ret
);
1475 // Return an expression for the unary operation OP EXPR.
1478 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1480 tree expr_tree
= expr
->get_tree();
1481 if (expr_tree
== error_mark_node
1482 || TREE_TYPE(expr_tree
) == error_mark_node
)
1483 return this->error_expression();
1485 tree type_tree
= TREE_TYPE(expr_tree
);
1486 enum tree_code code
;
1489 case OPERATOR_MINUS
:
1491 tree computed_type
= excess_precision_type(type_tree
);
1492 if (computed_type
!= NULL_TREE
)
1494 expr_tree
= convert(computed_type
, expr_tree
);
1495 type_tree
= computed_type
;
1501 code
= TRUTH_NOT_EXPR
;
1504 code
= BIT_NOT_EXPR
;
1511 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1513 return this->make_expression(ret
);
1516 // Convert a gofrontend operator to an equivalent tree_code.
1518 static enum tree_code
1519 operator_to_tree_code(Operator op
, tree type
)
1521 enum tree_code code
;
1527 case OPERATOR_NOTEQ
:
1543 code
= TRUTH_ORIF_EXPR
;
1545 case OPERATOR_ANDAND
:
1546 code
= TRUTH_ANDIF_EXPR
;
1551 case OPERATOR_MINUS
:
1555 code
= BIT_IOR_EXPR
;
1558 code
= BIT_XOR_EXPR
;
1564 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1567 code
= TRUNC_DIV_EXPR
;
1570 code
= TRUNC_MOD_EXPR
;
1572 case OPERATOR_LSHIFT
:
1575 case OPERATOR_RSHIFT
:
1579 code
= BIT_AND_EXPR
;
1581 case OPERATOR_BITCLEAR
:
1582 code
= BIT_AND_EXPR
;
1591 // Return an expression for the binary operation LEFT OP RIGHT.
1594 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1595 Bexpression
* right
, Location location
)
1597 tree left_tree
= left
->get_tree();
1598 tree right_tree
= right
->get_tree();
1599 if (left_tree
== error_mark_node
1600 || right_tree
== error_mark_node
)
1601 return this->error_expression();
1602 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1604 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1605 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1606 tree computed_type
= excess_precision_type(type_tree
);
1607 if (computed_type
!= NULL_TREE
)
1609 left_tree
= convert(computed_type
, left_tree
);
1610 right_tree
= convert(computed_type
, right_tree
);
1611 type_tree
= computed_type
;
1614 // For comparison operators, the resulting type should be boolean.
1618 case OPERATOR_NOTEQ
:
1623 type_tree
= boolean_type_node
;
1629 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1630 left_tree
, right_tree
);
1631 return this->make_expression(ret
);
1634 // Return an expression that constructs BTYPE with VALS.
1637 Gcc_backend::constructor_expression(Btype
* btype
,
1638 const std::vector
<Bexpression
*>& vals
,
1641 tree type_tree
= btype
->get_tree();
1642 if (type_tree
== error_mark_node
)
1643 return this->error_expression();
1645 vec
<constructor_elt
, va_gc
> *init
;
1646 vec_alloc(init
, vals
.size());
1648 bool is_constant
= true;
1649 tree field
= TYPE_FIELDS(type_tree
);
1650 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1652 ++p
, field
= DECL_CHAIN(field
))
1654 gcc_assert(field
!= NULL_TREE
);
1655 tree val
= (*p
)->get_tree();
1656 if (TREE_TYPE(field
) == error_mark_node
1657 || val
== error_mark_node
1658 || TREE_TYPE(val
) == error_mark_node
)
1659 return this->error_expression();
1661 constructor_elt empty
= {NULL
, NULL
};
1662 constructor_elt
* elt
= init
->quick_push(empty
);
1664 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
1666 if (!TREE_CONSTANT(elt
->value
))
1667 is_constant
= false;
1669 gcc_assert(field
== NULL_TREE
);
1670 tree ret
= build_constructor(type_tree
, init
);
1672 TREE_CONSTANT(ret
) = 1;
1674 return this->make_expression(ret
);
1678 Gcc_backend::array_constructor_expression(
1679 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1680 const std::vector
<Bexpression
*>& vals
, Location
)
1682 tree type_tree
= array_btype
->get_tree();
1683 if (type_tree
== error_mark_node
)
1684 return this->error_expression();
1686 gcc_assert(indexes
.size() == vals
.size());
1687 vec
<constructor_elt
, va_gc
> *init
;
1688 vec_alloc(init
, vals
.size());
1690 bool is_constant
= true;
1691 for (size_t i
= 0; i
< vals
.size(); ++i
)
1693 tree index
= size_int(indexes
[i
]);
1694 tree val
= (vals
[i
])->get_tree();
1696 if (index
== error_mark_node
1697 || val
== error_mark_node
)
1698 return this->error_expression();
1700 if (!TREE_CONSTANT(val
))
1701 is_constant
= false;
1703 constructor_elt empty
= {NULL
, NULL
};
1704 constructor_elt
* elt
= init
->quick_push(empty
);
1709 tree ret
= build_constructor(type_tree
, init
);
1711 TREE_CONSTANT(ret
) = 1;
1712 return this->make_expression(ret
);
1715 // Return an expression for the address of BASE[INDEX].
1718 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1721 tree base_tree
= base
->get_tree();
1722 tree index_tree
= index
->get_tree();
1723 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1724 if (base_tree
== error_mark_node
1725 || TREE_TYPE(base_tree
) == error_mark_node
1726 || index_tree
== error_mark_node
1727 || element_type_tree
== error_mark_node
)
1728 return this->error_expression();
1730 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1731 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1732 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1733 index_tree
, element_size
);
1734 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1735 TREE_TYPE(base_tree
), base_tree
, offset
);
1736 return this->make_expression(ptr
);
1739 // Return an expression representing ARRAY[INDEX]
1742 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1745 tree array_tree
= array
->get_tree();
1746 tree index_tree
= index
->get_tree();
1747 if (array_tree
== error_mark_node
1748 || TREE_TYPE(array_tree
) == error_mark_node
1749 || index_tree
== error_mark_node
)
1750 return this->error_expression();
1752 tree ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1753 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1754 index_tree
, NULL_TREE
, NULL_TREE
);
1755 return this->make_expression(ret
);
1758 // Create an expression for a call to FN_EXPR with FN_ARGS.
1760 Gcc_backend::call_expression(Bexpression
* fn_expr
,
1761 const std::vector
<Bexpression
*>& fn_args
,
1764 tree fn
= fn_expr
->get_tree();
1765 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1766 return this->error_expression();
1768 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1769 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1771 size_t nargs
= fn_args
.size();
1772 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1773 for (size_t i
= 0; i
< nargs
; ++i
)
1775 args
[i
] = fn_args
.at(i
)->get_tree();
1776 if (args
[i
] == error_mark_node
)
1777 return this->error_expression();
1781 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1782 fndecl
= TREE_OPERAND(fndecl
, 0);
1784 // This is to support builtin math functions when using 80387 math.
1785 tree excess_type
= NULL_TREE
;
1787 && TREE_CODE(fndecl
) == FUNCTION_DECL
1788 && DECL_IS_BUILTIN(fndecl
)
1789 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1791 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1792 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1793 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1794 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1796 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1797 if (excess_type
!= NULL_TREE
)
1799 tree excess_fndecl
= mathfn_built_in(excess_type
,
1800 DECL_FUNCTION_CODE(fndecl
));
1801 if (excess_fndecl
== NULL_TREE
)
1802 excess_type
= NULL_TREE
;
1805 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1807 for (size_t i
= 0; i
< nargs
; ++i
)
1809 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1810 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1811 args
[i
] = ::convert(excess_type
, args
[i
]);
1818 build_call_array_loc(location
.gcc_location(),
1819 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1822 if (excess_type
!= NULL_TREE
)
1824 // Calling convert here can undo our excess precision change.
1825 // That may or may not be a bug in convert_to_real.
1826 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1830 return this->make_expression(ret
);
1833 // An expression as a statement.
1836 Gcc_backend::expression_statement(Bexpression
* expr
)
1838 return this->make_statement(expr
->get_tree());
1841 // Variable initialization.
1844 Gcc_backend::init_statement(Bvariable
* var
, Bexpression
* init
)
1846 tree var_tree
= var
->get_tree();
1847 tree init_tree
= init
->get_tree();
1848 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
1849 return this->error_statement();
1850 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
1852 // To avoid problems with GNU ld, we don't make zero-sized
1853 // externally visible variables. That might lead us to doing an
1854 // initialization of a zero-sized expression to a non-zero sized
1855 // variable, or vice-versa. Avoid crashes by omitting the
1856 // initializer. Such initializations don't mean anything anyhow.
1857 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
1858 && init_tree
!= NULL_TREE
1859 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
1861 DECL_INITIAL(var_tree
) = init_tree
;
1862 init_tree
= NULL_TREE
;
1865 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
1866 void_type_node
, var_tree
);
1867 if (init_tree
!= NULL_TREE
)
1868 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
1869 void_type_node
, init_tree
, ret
);
1871 return this->make_statement(ret
);
1877 Gcc_backend::assignment_statement(Bexpression
* lhs
, Bexpression
* rhs
,
1880 tree lhs_tree
= lhs
->get_tree();
1881 tree rhs_tree
= rhs
->get_tree();
1882 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
1883 return this->error_statement();
1885 // To avoid problems with GNU ld, we don't make zero-sized
1886 // externally visible variables. That might lead us to doing an
1887 // assignment of a zero-sized expression to a non-zero sized
1888 // expression; avoid crashes here by avoiding assignments of
1889 // zero-sized expressions. Such assignments don't really mean
1891 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
1892 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
1893 return this->compound_statement(this->expression_statement(lhs
),
1894 this->expression_statement(rhs
));
1896 // Sometimes the same unnamed Go type can be created multiple times
1897 // and thus have multiple tree representations. Make sure this does
1898 // not confuse the middle-end.
1899 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
1901 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
1902 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
1903 if (POINTER_TYPE_P(lhs_type_tree
)
1904 || INTEGRAL_TYPE_P(lhs_type_tree
)
1905 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
1906 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
1907 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
1909 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
1910 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
1912 gcc_assert(int_size_in_bytes(lhs_type_tree
)
1913 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
1914 rhs_tree
= fold_build1_loc(location
.gcc_location(),
1916 lhs_type_tree
, rhs_tree
);
1920 return this->make_statement(fold_build2_loc(location
.gcc_location(),
1923 lhs_tree
, rhs_tree
));
1929 Gcc_backend::return_statement(Bfunction
* bfunction
,
1930 const std::vector
<Bexpression
*>& vals
,
1933 tree fntree
= bfunction
->get_tree();
1934 if (fntree
== error_mark_node
)
1935 return this->error_statement();
1936 tree result
= DECL_RESULT(fntree
);
1937 if (result
== error_mark_node
)
1938 return this->error_statement();
1942 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
1944 else if (vals
.size() == 1)
1946 tree val
= vals
.front()->get_tree();
1947 if (val
== error_mark_node
)
1948 return this->error_statement();
1949 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1950 void_type_node
, result
,
1951 vals
.front()->get_tree());
1952 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1953 void_type_node
, set
);
1957 // To return multiple values, copy the values into a temporary
1958 // variable of the right structure type, and then assign the
1959 // temporary variable to the DECL_RESULT in the return
1961 tree stmt_list
= NULL_TREE
;
1962 tree rettype
= TREE_TYPE(result
);
1964 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
1965 push_struct_function(fntree
);
1967 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
1968 tree rettmp
= create_tmp_var(rettype
, "RESULT");
1971 tree field
= TYPE_FIELDS(rettype
);
1972 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1974 p
++, field
= DECL_CHAIN(field
))
1976 gcc_assert(field
!= NULL_TREE
);
1977 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1978 TREE_TYPE(field
), rettmp
, field
,
1980 tree val
= (*p
)->get_tree();
1981 if (val
== error_mark_node
)
1982 return this->error_statement();
1983 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1985 ref
, (*p
)->get_tree());
1986 append_to_statement_list(set
, &stmt_list
);
1988 gcc_assert(field
== NULL_TREE
);
1989 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
1992 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
1993 void_type_node
, set
);
1994 append_to_statement_list(ret_expr
, &stmt_list
);
1997 return this->make_statement(ret
);
2000 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2001 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2002 // NULL, it will always be executed. This is used for handling defers in Go
2003 // functions. In C++, the resulting code is of this form:
2004 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2007 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2008 Bstatement
* except_stmt
,
2009 Bstatement
* finally_stmt
,
2012 tree stat_tree
= bstat
->get_tree();
2013 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2014 tree finally_tree
= finally_stmt
== NULL
2016 : finally_stmt
->get_tree();
2018 if (stat_tree
== error_mark_node
2019 || except_tree
== error_mark_node
2020 || finally_tree
== error_mark_node
)
2021 return this->error_statement();
2023 if (except_tree
!= NULL_TREE
)
2024 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2025 void_type_node
, stat_tree
,
2026 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2027 void_type_node
, NULL
, except_tree
));
2028 if (finally_tree
!= NULL_TREE
)
2029 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2030 void_type_node
, stat_tree
, finally_tree
);
2031 return this->make_statement(stat_tree
);
2037 Gcc_backend::if_statement(Bexpression
* condition
, Bblock
* then_block
,
2038 Bblock
* else_block
, Location location
)
2040 tree cond_tree
= condition
->get_tree();
2041 tree then_tree
= then_block
->get_tree();
2042 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2043 if (cond_tree
== error_mark_node
2044 || then_tree
== error_mark_node
2045 || else_tree
== error_mark_node
)
2046 return this->error_statement();
2047 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2048 cond_tree
, then_tree
, else_tree
);
2049 return this->make_statement(ret
);
2055 Gcc_backend::switch_statement(
2056 Bfunction
* function
,
2058 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2059 const std::vector
<Bstatement
*>& statements
,
2060 Location switch_location
)
2062 gcc_assert(cases
.size() == statements
.size());
2064 tree decl
= function
->get_tree();
2065 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2066 push_struct_function(decl
);
2068 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2070 tree stmt_list
= NULL_TREE
;
2071 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2072 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2073 ps
!= statements
.end();
2078 source_location loc
= (*ps
!= NULL
2079 ? EXPR_LOCATION((*ps
)->get_tree())
2080 : UNKNOWN_LOCATION
);
2081 tree label
= create_artificial_label(loc
);
2082 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2083 append_to_statement_list(c
, &stmt_list
);
2087 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2091 tree t
= (*pcv
)->get_tree();
2092 if (t
== error_mark_node
)
2093 return this->error_statement();
2094 source_location loc
= EXPR_LOCATION(t
);
2095 tree label
= create_artificial_label(loc
);
2096 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2097 append_to_statement_list(c
, &stmt_list
);
2103 tree t
= (*ps
)->get_tree();
2104 if (t
== error_mark_node
)
2105 return this->error_statement();
2106 append_to_statement_list(t
, &stmt_list
);
2111 tree tv
= value
->get_tree();
2112 if (tv
== error_mark_node
)
2113 return this->error_statement();
2114 tree t
= build3_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2115 NULL_TREE
, tv
, stmt_list
, NULL_TREE
);
2116 return this->make_statement(t
);
2119 // Pair of statements.
2122 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2124 tree stmt_list
= NULL_TREE
;
2125 tree t
= s1
->get_tree();
2126 if (t
== error_mark_node
)
2127 return this->error_statement();
2128 append_to_statement_list(t
, &stmt_list
);
2130 if (t
== error_mark_node
)
2131 return this->error_statement();
2132 append_to_statement_list(t
, &stmt_list
);
2134 // If neither statement has any side effects, stmt_list can be NULL
2136 if (stmt_list
== NULL_TREE
)
2137 stmt_list
= integer_zero_node
;
2139 return this->make_statement(stmt_list
);
2142 // List of statements.
2145 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2147 tree stmt_list
= NULL_TREE
;
2148 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2149 p
!= statements
.end();
2152 tree t
= (*p
)->get_tree();
2153 if (t
== error_mark_node
)
2154 return this->error_statement();
2155 append_to_statement_list(t
, &stmt_list
);
2157 return this->make_statement(stmt_list
);
2160 // Make a block. For some reason gcc uses a dual structure for
2161 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2162 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2166 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2167 const std::vector
<Bvariable
*>& vars
,
2168 Location start_location
,
2171 tree block_tree
= make_node(BLOCK
);
2172 if (enclosing
== NULL
)
2174 tree fndecl
= function
->get_tree();
2175 gcc_assert(fndecl
!= NULL_TREE
);
2177 // We may have already created a block for local variables when
2178 // we take the address of a parameter.
2179 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2181 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2182 DECL_INITIAL(fndecl
) = block_tree
;
2186 tree superblock_tree
= DECL_INITIAL(fndecl
);
2187 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2189 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2191 pp
= &BLOCK_CHAIN(*pp
))
2198 tree superbind_tree
= enclosing
->get_tree();
2199 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2200 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2202 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2204 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2206 pp
= &BLOCK_CHAIN(*pp
))
2211 tree
* pp
= &BLOCK_VARS(block_tree
);
2212 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2216 *pp
= (*pv
)->get_tree();
2217 if (*pp
!= error_mark_node
)
2218 pp
= &DECL_CHAIN(*pp
);
2222 TREE_USED(block_tree
) = 1;
2224 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2225 void_type_node
, BLOCK_VARS(block_tree
),
2226 NULL_TREE
, block_tree
);
2227 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2228 return new Bblock(bind_tree
);
2231 // Add statements to a block.
2234 Gcc_backend::block_add_statements(Bblock
* bblock
,
2235 const std::vector
<Bstatement
*>& statements
)
2237 tree stmt_list
= NULL_TREE
;
2238 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2239 p
!= statements
.end();
2242 tree s
= (*p
)->get_tree();
2243 if (s
!= error_mark_node
)
2244 append_to_statement_list(s
, &stmt_list
);
2247 tree bind_tree
= bblock
->get_tree();
2248 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2249 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2252 // Return a block as a statement.
2255 Gcc_backend::block_statement(Bblock
* bblock
)
2257 tree bind_tree
= bblock
->get_tree();
2258 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2259 return this->make_statement(bind_tree
);
2262 // This is not static because we declare it with GTY(()) in go-c.h.
2263 tree go_non_zero_struct
;
2265 // Return a type corresponding to TYPE with non-zero size.
2268 Gcc_backend::non_zero_size_type(tree type
)
2270 if (int_size_in_bytes(type
) != 0)
2273 switch (TREE_CODE(type
))
2276 if (TYPE_FIELDS(type
) != NULL_TREE
)
2278 tree ns
= make_node(RECORD_TYPE
);
2279 tree field_trees
= NULL_TREE
;
2280 tree
*pp
= &field_trees
;
2281 for (tree field
= TYPE_FIELDS(type
);
2283 field
= DECL_CHAIN(field
))
2285 tree ft
= TREE_TYPE(field
);
2286 if (field
== TYPE_FIELDS(type
))
2287 ft
= non_zero_size_type(ft
);
2288 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2289 DECL_NAME(field
), ft
);
2290 DECL_CONTEXT(f
) = ns
;
2292 pp
= &DECL_CHAIN(f
);
2294 TYPE_FIELDS(ns
) = field_trees
;
2299 if (go_non_zero_struct
== NULL_TREE
)
2301 type
= make_node(RECORD_TYPE
);
2302 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2303 get_identifier("dummy"),
2305 DECL_CONTEXT(field
) = type
;
2306 TYPE_FIELDS(type
) = field
;
2308 go_non_zero_struct
= type
;
2310 return go_non_zero_struct
;
2314 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2315 return build_array_type_nelts(element_type
, 1);
2325 // Make a global variable.
2328 Gcc_backend::global_variable(const std::string
& package_name
,
2329 const std::string
& pkgpath
,
2330 const std::string
& name
,
2334 bool in_unique_section
,
2337 tree type_tree
= btype
->get_tree();
2338 if (type_tree
== error_mark_node
)
2339 return this->error_variable();
2341 // The GNU linker does not like dynamic variables with zero size.
2342 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2343 type_tree
= this->non_zero_size_type(type_tree
);
2345 std::string
var_name(package_name
);
2346 var_name
.push_back('.');
2347 var_name
.append(name
);
2348 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2349 get_identifier_from_string(var_name
),
2352 DECL_EXTERNAL(decl
) = 1;
2354 TREE_STATIC(decl
) = 1;
2357 TREE_PUBLIC(decl
) = 1;
2359 std::string
asm_name(pkgpath
);
2360 asm_name
.push_back('.');
2361 asm_name
.append(name
);
2362 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2364 TREE_USED(decl
) = 1;
2366 if (in_unique_section
)
2367 resolve_unique_section (decl
, 0, 1);
2369 go_preserve_from_gc(decl
);
2371 return new Bvariable(decl
);
2374 // Set the initial value of a global variable.
2377 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2379 tree expr_tree
= expr
->get_tree();
2380 if (expr_tree
== error_mark_node
)
2382 gcc_assert(TREE_CONSTANT(expr_tree
));
2383 tree var_decl
= var
->get_tree();
2384 if (var_decl
== error_mark_node
)
2386 DECL_INITIAL(var_decl
) = expr_tree
;
2388 // If this variable goes in a unique section, it may need to go into
2389 // a different one now that DECL_INITIAL is set.
2390 if (symtab_node::get(var_decl
)
2391 && symtab_node::get(var_decl
)->implicit_section
)
2393 set_decl_section_name (var_decl
, NULL
);
2394 resolve_unique_section (var_decl
,
2395 compute_reloc_for_constant (expr_tree
),
2400 // Make a local variable.
2403 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2404 Btype
* btype
, bool is_address_taken
,
2407 tree type_tree
= btype
->get_tree();
2408 if (type_tree
== error_mark_node
)
2409 return this->error_variable();
2410 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2411 get_identifier_from_string(name
),
2413 DECL_CONTEXT(decl
) = function
->get_tree();
2414 TREE_USED(decl
) = 1;
2415 if (is_address_taken
)
2416 TREE_ADDRESSABLE(decl
) = 1;
2417 go_preserve_from_gc(decl
);
2418 return new Bvariable(decl
);
2421 // Make a function parameter variable.
2424 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2425 Btype
* btype
, bool is_address_taken
,
2428 tree type_tree
= btype
->get_tree();
2429 if (type_tree
== error_mark_node
)
2430 return this->error_variable();
2431 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2432 get_identifier_from_string(name
),
2434 DECL_CONTEXT(decl
) = function
->get_tree();
2435 DECL_ARG_TYPE(decl
) = type_tree
;
2436 TREE_USED(decl
) = 1;
2437 if (is_address_taken
)
2438 TREE_ADDRESSABLE(decl
) = 1;
2439 go_preserve_from_gc(decl
);
2440 return new Bvariable(decl
);
2443 // Make a temporary variable.
2446 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2447 Btype
* btype
, Bexpression
* binit
,
2448 bool is_address_taken
,
2450 Bstatement
** pstatement
)
2452 gcc_assert(function
!= NULL
);
2453 tree decl
= function
->get_tree();
2454 tree type_tree
= btype
->get_tree();
2455 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2456 if (type_tree
== error_mark_node
2457 || init_tree
== error_mark_node
2458 || decl
== error_mark_node
)
2460 *pstatement
= this->error_statement();
2461 return this->error_variable();
2465 // We can only use create_tmp_var if the type is not addressable.
2466 if (!TREE_ADDRESSABLE(type_tree
))
2468 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2469 push_struct_function(decl
);
2471 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2473 var
= create_tmp_var(type_tree
, "GOTMP");
2478 gcc_assert(bblock
!= NULL
);
2479 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2480 create_tmp_var_name("GOTMP"),
2482 DECL_ARTIFICIAL(var
) = 1;
2483 DECL_IGNORED_P(var
) = 1;
2485 DECL_CONTEXT(var
) = decl
;
2487 // We have to add this variable to the BLOCK and the BIND_EXPR.
2488 tree bind_tree
= bblock
->get_tree();
2489 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2490 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2491 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2492 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2493 BLOCK_VARS(block_tree
) = var
;
2494 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2497 if (init_tree
!= NULL_TREE
)
2498 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
2501 if (is_address_taken
)
2502 TREE_ADDRESSABLE(var
) = 1;
2504 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2506 void_type_node
, var
));
2507 return new Bvariable(var
);
2510 // Create an implicit variable that is compiler-defined. This is used when
2511 // generating GC root variables and storing the values of a slice initializer.
2514 Gcc_backend::implicit_variable(const std::string
& name
, Btype
* type
,
2515 bool is_hidden
, bool is_constant
,
2516 bool is_common
, size_t alignment
)
2518 tree type_tree
= type
->get_tree();
2519 if (type_tree
== error_mark_node
)
2520 return this->error_variable();
2522 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2523 get_identifier_from_string(name
), type_tree
);
2524 DECL_EXTERNAL(decl
) = 0;
2525 TREE_PUBLIC(decl
) = !is_hidden
;
2526 TREE_STATIC(decl
) = 1;
2527 TREE_USED(decl
) = 1;
2528 DECL_ARTIFICIAL(decl
) = 1;
2531 DECL_COMMON(decl
) = 1;
2533 // When the initializer for one implicit_variable refers to another,
2534 // it needs to know the visibility of the referenced struct so that
2535 // compute_reloc_for_constant will return the right value. On many
2536 // systems calling make_decl_one_only will mark the decl as weak,
2537 // which will change the return value of compute_reloc_for_constant.
2538 // We can't reliably call make_decl_one_only yet, because we don't
2539 // yet know the initializer. This issue doesn't arise in C because
2540 // Go initializers, unlike C initializers, can be indirectly
2541 // recursive. To ensure that compute_reloc_for_constant computes
2542 // the right value if some other initializer refers to this one, we
2543 // mark this symbol as weak here. We undo that below in
2544 // immutable_struct_set_init before calling mark_decl_one_only.
2545 DECL_WEAK(decl
) = 1;
2549 TREE_READONLY(decl
) = 1;
2550 TREE_CONSTANT(decl
) = 1;
2554 DECL_ALIGN(decl
) = alignment
* BITS_PER_UNIT
;
2555 DECL_USER_ALIGN(decl
) = 1;
2558 go_preserve_from_gc(decl
);
2559 return new Bvariable(decl
);
2562 // Set the initalizer for a variable created by implicit_variable.
2563 // This is where we finish compiling the variable.
2566 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2567 Btype
*, bool, bool, bool is_common
,
2570 tree decl
= var
->get_tree();
2573 init_tree
= NULL_TREE
;
2575 init_tree
= init
->get_tree();
2576 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2579 DECL_INITIAL(decl
) = init_tree
;
2581 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2582 // See the comment where DECL_WEAK is set in implicit_variable.
2585 DECL_WEAK(decl
) = 0;
2586 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2589 resolve_unique_section(decl
, 2, 1);
2591 rest_of_decl_compilation(decl
, 1, 0);
2594 // Return a reference to an implicit variable defined in another package.
2597 Gcc_backend::implicit_variable_reference(const std::string
& name
, Btype
* btype
)
2599 tree type_tree
= btype
->get_tree();
2600 if (type_tree
== error_mark_node
)
2601 return this->error_variable();
2603 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2604 get_identifier_from_string(name
), type_tree
);
2605 DECL_EXTERNAL(decl
) = 0;
2606 TREE_PUBLIC(decl
) = 1;
2607 TREE_STATIC(decl
) = 1;
2608 DECL_ARTIFICIAL(decl
) = 1;
2609 go_preserve_from_gc(decl
);
2610 return new Bvariable(decl
);
2613 // Create a named immutable initialized data structure.
2616 Gcc_backend::immutable_struct(const std::string
& name
, bool is_hidden
,
2617 bool is_common
, Btype
* btype
, Location location
)
2619 tree type_tree
= btype
->get_tree();
2620 if (type_tree
== error_mark_node
)
2621 return this->error_variable();
2622 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2623 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2624 get_identifier_from_string(name
),
2625 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2626 TREE_STATIC(decl
) = 1;
2627 TREE_USED(decl
) = 1;
2628 TREE_READONLY(decl
) = 1;
2629 TREE_CONSTANT(decl
) = 1;
2630 DECL_ARTIFICIAL(decl
) = 1;
2632 TREE_PUBLIC(decl
) = 1;
2634 // When the initializer for one immutable_struct refers to another,
2635 // it needs to know the visibility of the referenced struct so that
2636 // compute_reloc_for_constant will return the right value. On many
2637 // systems calling make_decl_one_only will mark the decl as weak,
2638 // which will change the return value of compute_reloc_for_constant.
2639 // We can't reliably call make_decl_one_only yet, because we don't
2640 // yet know the initializer. This issue doesn't arise in C because
2641 // Go initializers, unlike C initializers, can be indirectly
2642 // recursive. To ensure that compute_reloc_for_constant computes
2643 // the right value if some other initializer refers to this one, we
2644 // mark this symbol as weak here. We undo that below in
2645 // immutable_struct_set_init before calling mark_decl_one_only.
2647 DECL_WEAK(decl
) = 1;
2649 // We don't call rest_of_decl_compilation until we have the
2652 go_preserve_from_gc(decl
);
2653 return new Bvariable(decl
);
2656 // Set the initializer for a variable created by immutable_struct.
2657 // This is where we finish compiling the variable.
2660 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2661 bool, bool is_common
, Btype
*, Location
,
2662 Bexpression
* initializer
)
2664 tree decl
= var
->get_tree();
2665 tree init_tree
= initializer
->get_tree();
2666 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2669 DECL_INITIAL(decl
) = init_tree
;
2671 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2672 // See the comment where DECL_WEAK is set in immutable_struct.
2675 DECL_WEAK(decl
) = 0;
2676 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2679 // These variables are often unneeded in the final program, so put
2680 // them in their own section so that linker GC can discard them.
2681 resolve_unique_section(decl
,
2682 compute_reloc_for_constant (init_tree
),
2685 rest_of_decl_compilation(decl
, 1, 0);
2688 // Return a reference to an immutable initialized data structure
2689 // defined in another package.
2692 Gcc_backend::immutable_struct_reference(const std::string
& name
, Btype
* btype
,
2695 tree type_tree
= btype
->get_tree();
2696 if (type_tree
== error_mark_node
)
2697 return this->error_variable();
2698 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2699 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2700 get_identifier_from_string(name
),
2701 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2702 TREE_READONLY(decl
) = 1;
2703 TREE_CONSTANT(decl
) = 1;
2704 DECL_ARTIFICIAL(decl
) = 1;
2705 TREE_PUBLIC(decl
) = 1;
2706 DECL_EXTERNAL(decl
) = 1;
2707 go_preserve_from_gc(decl
);
2708 return new Bvariable(decl
);
2714 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2720 tree func_tree
= function
->get_tree();
2721 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2722 push_struct_function(func_tree
);
2724 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2726 decl
= create_artificial_label(location
.gcc_location());
2732 tree id
= get_identifier_from_string(name
);
2733 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
2735 DECL_CONTEXT(decl
) = function
->get_tree();
2737 return new Blabel(decl
);
2740 // Make a statement which defines a label.
2743 Gcc_backend::label_definition_statement(Blabel
* label
)
2745 tree lab
= label
->get_tree();
2746 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
2747 void_type_node
, lab
);
2748 return this->make_statement(ret
);
2751 // Make a goto statement.
2754 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
2756 tree lab
= label
->get_tree();
2757 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
2759 return this->make_statement(ret
);
2762 // Get the address of a label.
2765 Gcc_backend::label_address(Blabel
* label
, Location location
)
2767 tree lab
= label
->get_tree();
2769 TREE_ADDRESSABLE(lab
) = 1;
2770 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
2771 build_fold_addr_expr_loc(location
.gcc_location(),
2773 return this->make_expression(ret
);
2776 // Declare or define a new function.
2779 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
2780 const std::string
& asm_name
, bool is_visible
,
2781 bool is_declaration
, bool is_inlinable
,
2782 bool disable_split_stack
, bool in_unique_section
,
2785 tree functype
= fntype
->get_tree();
2786 if (functype
!= error_mark_node
)
2788 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
2789 functype
= TREE_TYPE(functype
);
2791 tree id
= get_identifier_from_string(name
);
2792 if (functype
== error_mark_node
|| id
== error_mark_node
)
2793 return this->error_function();
2795 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
2796 if (!asm_name
.empty())
2797 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2799 TREE_PUBLIC(decl
) = 1;
2801 DECL_EXTERNAL(decl
) = 1;
2804 tree restype
= TREE_TYPE(functype
);
2806 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
2807 DECL_ARTIFICIAL(resdecl
) = 1;
2808 DECL_IGNORED_P(resdecl
) = 1;
2809 DECL_CONTEXT(resdecl
) = decl
;
2810 DECL_RESULT(decl
) = resdecl
;
2813 DECL_UNINLINABLE(decl
) = 1;
2814 if (disable_split_stack
)
2816 tree attr
= get_identifier("__no_split_stack__");
2817 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
2819 if (in_unique_section
)
2820 resolve_unique_section(decl
, 0, 1);
2822 go_preserve_from_gc(decl
);
2823 return new Bfunction(decl
);
2826 // Create a statement that runs all deferred calls for FUNCTION. This should
2827 // be a statement that looks like this in C++:
2829 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2832 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
2833 Bexpression
* defer
, Location location
)
2835 tree undefer_tree
= undefer
->get_tree();
2836 tree defer_tree
= defer
->get_tree();
2837 tree fntree
= function
->get_tree();
2839 if (undefer_tree
== error_mark_node
2840 || defer_tree
== error_mark_node
2841 || fntree
== error_mark_node
)
2842 return this->error_statement();
2844 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2845 push_struct_function(fntree
);
2847 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2849 tree stmt_list
= NULL
;
2850 Blabel
* blabel
= this->label(function
, "", location
);
2851 Bstatement
* label_def
= this->label_definition_statement(blabel
);
2852 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
2854 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
2855 tree jump
= jump_stmt
->get_tree();
2856 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
2857 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
2859 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
2860 append_to_statement_list(try_catch
, &stmt_list
);
2863 return this->make_statement(stmt_list
);
2866 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2867 // This will only be called for a function definition.
2870 Gcc_backend::function_set_parameters(Bfunction
* function
,
2871 const std::vector
<Bvariable
*>& param_vars
)
2873 tree func_tree
= function
->get_tree();
2874 if (func_tree
== error_mark_node
)
2877 tree params
= NULL_TREE
;
2879 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
2880 pv
!= param_vars
.end();
2883 *pp
= (*pv
)->get_tree();
2884 gcc_assert(*pp
!= error_mark_node
);
2885 pp
= &DECL_CHAIN(*pp
);
2888 DECL_ARGUMENTS(func_tree
) = params
;
2892 // Set the function body for FUNCTION using the code in CODE_BLOCK.
2895 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
2897 tree func_tree
= function
->get_tree();
2898 tree code
= code_stmt
->get_tree();
2900 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
2902 DECL_SAVED_TREE(func_tree
) = code
;
2906 // Look up a named built-in function in the current backend implementation.
2907 // Returns NULL if no built-in function by that name exists.
2910 Gcc_backend::lookup_builtin(const std::string
& name
)
2912 if (this->builtin_functions_
.count(name
) != 0)
2913 return this->builtin_functions_
[name
];
2917 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2918 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
2921 Gcc_backend::write_global_definitions(
2922 const std::vector
<Btype
*>& type_decls
,
2923 const std::vector
<Bexpression
*>& constant_decls
,
2924 const std::vector
<Bfunction
*>& function_decls
,
2925 const std::vector
<Bvariable
*>& variable_decls
)
2927 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
2928 + function_decls
.size() + variable_decls
.size();
2930 tree
* defs
= new tree
[count_definitions
];
2932 // Convert all non-erroneous declarations into Gimple form.
2934 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
2935 p
!= variable_decls
.end();
2938 if ((*p
)->get_tree() != error_mark_node
)
2940 defs
[i
] = (*p
)->get_tree();
2941 go_preserve_from_gc(defs
[i
]);
2946 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
2947 p
!= type_decls
.end();
2950 tree type_tree
= (*p
)->get_tree();
2951 if (type_tree
!= error_mark_node
2952 && IS_TYPE_OR_DECL_P(type_tree
))
2954 defs
[i
] = TYPE_NAME(type_tree
);
2955 gcc_assert(defs
[i
] != NULL
);
2956 go_preserve_from_gc(defs
[i
]);
2960 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
2961 p
!= constant_decls
.end();
2964 if ((*p
)->get_tree() != error_mark_node
)
2966 defs
[i
] = (*p
)->get_tree();
2967 go_preserve_from_gc(defs
[i
]);
2971 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
2972 p
!= function_decls
.end();
2975 tree decl
= (*p
)->get_tree();
2976 if (decl
!= error_mark_node
)
2978 go_preserve_from_gc(decl
);
2979 gimplify_function_tree(decl
);
2980 cgraph_node::finalize_function(decl
, true);
2987 // Pass everything back to the middle-end.
2989 wrapup_global_declarations(defs
, i
);
2991 symtab
->finalize_compilation_unit();
2993 check_global_declarations(defs
, i
);
2994 emit_debug_global_declarations(defs
, i
);
2999 // Define a builtin function. BCODE is the builtin function code
3000 // defined by builtins.def. NAME is the name of the builtin function.
3001 // LIBNAME is the name of the corresponding library function, and is
3002 // NULL if there isn't one. FNTYPE is the type of the function.
3003 // CONST_P is true if the function has the const attribute.
3006 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3007 const char* libname
, tree fntype
, bool const_p
)
3009 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3010 libname
, NULL_TREE
);
3012 TREE_READONLY(decl
) = 1;
3013 set_builtin_decl(bcode
, decl
, true);
3014 this->builtin_functions_
[name
] = this->make_function(decl
);
3015 if (libname
!= NULL
)
3017 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3020 TREE_READONLY(decl
) = 1;
3021 this->builtin_functions_
[libname
] = this->make_function(decl
);
3025 // Return the backend generator.
3030 return new Gcc_backend();