1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2018 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 "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
32 #include "tree-iterator.h"
37 #include "gimple-expr.h"
39 #include "langhooks.h"
51 // A class wrapping a tree.
72 // In gcc, types, expressions, and statements are all trees.
73 class Btype
: public Gcc_tree
81 class Bexpression
: public Gcc_tree
89 class Bstatement
: public Gcc_tree
97 class Bfunction
: public Gcc_tree
105 class Bblock
: public Gcc_tree
113 class Blabel
: public Gcc_tree
121 // Bvariable is a bit more complicated, because of zero-sized types.
122 // The GNU linker does not permit dynamic variables with zero size.
123 // When we see such a variable, we generate a version of the type with
124 // non-zero size. However, when referring to the global variable, we
125 // want an expression of zero size; otherwise, if, say, the global
126 // variable is passed to a function, we will be passing a
127 // non-zero-sized value to a zero-sized value, which can lead to a
134 : t_(t
), orig_type_(NULL
)
137 Bvariable(tree t
, tree orig_type
)
138 : t_(t
), orig_type_(orig_type
)
141 // Get the tree for use as an expression.
143 get_tree(Location
) const;
145 // Get the actual decl;
155 // Get the tree of a variable for use as an expression. If this is a
156 // zero-sized global, create an expression that refers to the decl but
159 Bvariable::get_tree(Location location
) const
161 if (this->orig_type_
== NULL
162 || this->t_
== error_mark_node
163 || TREE_TYPE(this->t_
) == this->orig_type_
)
165 // Return *(orig_type*)&decl. */
166 tree t
= build_fold_addr_expr_loc(location
.gcc_location(), this->t_
);
167 t
= fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
168 build_pointer_type(this->orig_type_
), t
);
169 return build_fold_indirect_ref_loc(location
.gcc_location(), t
);
172 // This file implements the interface between the Go frontend proper
173 // and the gcc IR. This implements specific instantiations of
174 // abstract classes defined by the Go frontend proper. The Go
175 // frontend proper class methods of these classes to generate the
176 // backend representation.
178 class Gcc_backend
: public Backend
187 { return this->make_type(error_mark_node
); }
191 { return this->make_type(void_type_node
); }
195 { return this->make_type(boolean_type_node
); }
198 integer_type(bool, int);
207 pointer_type(Btype
*);
210 function_type(const Btyped_identifier
&,
211 const std::vector
<Btyped_identifier
>&,
212 const std::vector
<Btyped_identifier
>&,
217 struct_type(const std::vector
<Btyped_identifier
>&);
220 array_type(Btype
*, Bexpression
*);
223 placeholder_pointer_type(const std::string
&, Location
, bool);
226 set_placeholder_pointer_type(Btype
*, Btype
*);
229 set_placeholder_function_type(Btype
*, Btype
*);
232 placeholder_struct_type(const std::string
&, Location
);
235 set_placeholder_struct_type(Btype
* placeholder
,
236 const std::vector
<Btyped_identifier
>&);
239 placeholder_array_type(const std::string
&, Location
);
242 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
245 named_type(const std::string
&, Btype
*, Location
);
248 circular_pointer_type(Btype
*, bool);
251 is_circular_pointer_type(Btype
*);
257 type_alignment(Btype
*);
260 type_field_alignment(Btype
*);
263 type_field_offset(Btype
*, size_t index
);
268 zero_expression(Btype
*);
272 { return this->make_expression(error_mark_node
); }
275 nil_pointer_expression()
276 { return this->make_expression(null_pointer_node
); }
279 var_expression(Bvariable
* var
, Location
);
282 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
285 named_constant_expression(Btype
* btype
, const std::string
& name
,
286 Bexpression
* val
, Location
);
289 integer_constant_expression(Btype
* btype
, mpz_t val
);
292 float_constant_expression(Btype
* btype
, mpfr_t val
);
295 complex_constant_expression(Btype
* btype
, mpc_t val
);
298 string_constant_expression(const std::string
& val
);
301 boolean_constant_expression(bool val
);
304 real_part_expression(Bexpression
* bcomplex
, Location
);
307 imag_part_expression(Bexpression
* bcomplex
, Location
);
310 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
313 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
316 function_code_expression(Bfunction
*, Location
);
319 address_expression(Bexpression
*, Location
);
322 struct_field_expression(Bexpression
*, size_t, Location
);
325 compound_expression(Bstatement
*, Bexpression
*, Location
);
328 conditional_expression(Bfunction
*, Btype
*, Bexpression
*, Bexpression
*,
329 Bexpression
*, Location
);
332 unary_expression(Operator
, Bexpression
*, Location
);
335 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
338 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
341 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
342 const std::vector
<Bexpression
*>&, Location
);
345 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
348 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
351 call_expression(Bfunction
* caller
, Bexpression
* fn
,
352 const std::vector
<Bexpression
*>& args
,
353 Bexpression
* static_chain
, Location
);
356 stack_allocation_expression(int64_t size
, Location
);
362 { return this->make_statement(error_mark_node
); }
365 expression_statement(Bfunction
*, Bexpression
*);
368 init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
);
371 assignment_statement(Bfunction
*, Bexpression
* lhs
, Bexpression
* rhs
,
375 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
379 if_statement(Bfunction
*, Bexpression
* condition
, Bblock
* then_block
,
380 Bblock
* else_block
, Location
);
383 switch_statement(Bfunction
* function
, Bexpression
* value
,
384 const std::vector
<std::vector
<Bexpression
*> >& cases
,
385 const std::vector
<Bstatement
*>& statements
,
389 compound_statement(Bstatement
*, Bstatement
*);
392 statement_list(const std::vector
<Bstatement
*>&);
395 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
396 Bstatement
* finally_stmt
, Location
);
401 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
405 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
408 block_statement(Bblock
*);
414 { return new Bvariable(error_mark_node
); }
417 global_variable(const std::string
& var_name
,
418 const std::string
& asm_name
,
422 bool in_unique_section
,
426 global_variable_set_init(Bvariable
*, Bexpression
*);
429 local_variable(Bfunction
*, const std::string
&, Btype
*, Bvariable
*, bool,
433 parameter_variable(Bfunction
*, const std::string
&, Btype
*, bool,
437 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, Location
);
440 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, bool,
441 Location
, Bstatement
**);
444 implicit_variable(const std::string
&, const std::string
&, Btype
*,
445 bool, bool, bool, int64_t);
448 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
449 bool, bool, bool, Bexpression
*);
452 implicit_variable_reference(const std::string
&, const std::string
&, Btype
*);
455 immutable_struct(const std::string
&, const std::string
&,
456 bool, bool, Btype
*, Location
);
459 immutable_struct_set_init(Bvariable
*, const std::string
&, bool, bool, Btype
*,
460 Location
, Bexpression
*);
463 immutable_struct_reference(const std::string
&, const std::string
&,
469 label(Bfunction
*, const std::string
& name
, Location
);
472 label_definition_statement(Blabel
*);
475 goto_statement(Blabel
*, Location
);
478 label_address(Blabel
*, Location
);
484 { return this->make_function(error_mark_node
); }
487 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
488 bool is_visible
, bool is_declaration
, bool is_inlinable
,
489 bool disable_split_stack
, bool does_not_return
,
490 bool in_unique_section
, Location
);
493 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
494 Bexpression
* defer
, Location
);
497 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
500 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
503 lookup_builtin(const std::string
&);
506 write_global_definitions(const std::vector
<Btype
*>&,
507 const std::vector
<Bexpression
*>&,
508 const std::vector
<Bfunction
*>&,
509 const std::vector
<Bvariable
*>&);
512 write_export_data(const char* bytes
, unsigned int size
);
516 // Make a Bexpression from a tree.
518 make_expression(tree t
)
519 { return new Bexpression(t
); }
521 // Make a Bstatement from a tree.
523 make_statement(tree t
)
524 { return new Bstatement(t
); }
526 // Make a Btype from a tree.
529 { return new Btype(t
); }
532 make_function(tree t
)
533 { return new Bfunction(t
); }
536 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
539 fill_in_array(Btype
*, Btype
*, Bexpression
*);
542 non_zero_size_type(tree
);
546 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
547 tree fntype
, bool const_p
, bool noreturn_p
);
549 // A mapping of the GCC built-ins exposed to GCCGo.
550 std::map
<std::string
, Bfunction
*> builtin_functions_
;
553 // A helper function to create a GCC identifier from a C++ string.
556 get_identifier_from_string(const std::string
& str
)
558 return get_identifier_with_length(str
.data(), str
.length());
561 // Define the built-in functions that are exposed to GCCGo.
563 Gcc_backend::Gcc_backend()
565 /* We need to define the fetch_and_add functions, since we use them
567 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
568 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
569 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
570 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
573 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
574 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
575 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
576 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
579 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
580 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
581 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
582 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
585 t
= this->integer_type(true, BITS_PER_UNIT
* 8)->get_tree();
586 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
587 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
588 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
),
591 // We use __builtin_expect for magic import functions.
592 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
593 build_function_type_list(long_integer_type_node
,
594 long_integer_type_node
,
595 long_integer_type_node
,
599 // We use __builtin_memcmp for struct comparisons.
600 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
601 build_function_type_list(integer_type_node
,
608 // Used by runtime/internal/sys.
609 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
610 build_function_type_list(integer_type_node
,
614 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
615 build_function_type_list(integer_type_node
,
616 long_long_unsigned_type_node
,
619 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
620 build_function_type_list(uint32_type_node
,
624 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
625 build_function_type_list(uint64_type_node
,
630 // We provide some functions for the math library.
631 tree math_function_type
= build_function_type_list(double_type_node
,
634 tree math_function_type_long
=
635 build_function_type_list(long_double_type_node
, long_double_type_node
,
637 tree math_function_type_two
= build_function_type_list(double_type_node
,
641 tree math_function_type_long_two
=
642 build_function_type_list(long_double_type_node
, long_double_type_node
,
643 long_double_type_node
, NULL_TREE
);
644 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
645 math_function_type
, true, false);
646 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
647 math_function_type_long
, true, false);
648 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
649 math_function_type
, true, false);
650 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
651 math_function_type_long
, true, false);
652 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
653 math_function_type
, true, false);
654 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
655 math_function_type_long
, true, false);
656 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
657 math_function_type_two
, true, false);
658 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
659 math_function_type_long_two
, true, false);
660 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
661 math_function_type
, true, false);
662 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
663 math_function_type_long
, true, false);
664 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
665 math_function_type
, true, false);
666 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
667 math_function_type_long
, true, false);
668 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
669 math_function_type
, true, false);
670 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
671 math_function_type_long
, true, false);
672 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
673 math_function_type
, true, false);
674 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
675 math_function_type_long
, true, false);
676 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
677 math_function_type
, true, false);
678 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
679 math_function_type_long
, true, false);
680 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
681 math_function_type
, true, false);
682 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
683 math_function_type_long
, true, false);
684 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
685 math_function_type_two
, true, false);
686 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
687 math_function_type_long_two
, true, false);
688 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
689 build_function_type_list(double_type_node
,
694 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
695 build_function_type_list(long_double_type_node
,
696 long_double_type_node
,
700 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
701 math_function_type
, true, false);
702 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
703 math_function_type_long
, true, false);
704 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
705 math_function_type
, true, false);
706 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
707 math_function_type_long
, true, false);
708 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
709 math_function_type
, true, false);
710 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
711 math_function_type_long
, true, false);
712 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
713 math_function_type
, true, false);
714 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
715 math_function_type_long
, true, false);
716 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
717 math_function_type
, true, false);
718 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
719 math_function_type_long
, true, false);
720 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
721 math_function_type
, true, false);
722 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
723 math_function_type_long
, true, false);
724 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
725 math_function_type
, true, false);
726 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
727 math_function_type_long
, true, false);
728 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
729 math_function_type
, true, false);
730 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
731 math_function_type_long
, true, false);
733 // We use __builtin_return_address in the thunk we build for
734 // functions which call recover, and for runtime.getcallerpc.
735 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
736 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
737 NULL
, t
, false, false);
739 // The runtime calls __builtin_frame_address for runtime.getcallersp.
740 this->define_builtin(BUILT_IN_FRAME_ADDRESS
, "__builtin_frame_address",
741 NULL
, t
, false, false);
743 // The runtime calls __builtin_extract_return_addr when recording
744 // the address to which a function returns.
745 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
746 "__builtin_extract_return_addr", NULL
,
747 build_function_type_list(ptr_type_node
,
752 // The compiler uses __builtin_trap for some exception handling
754 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
755 build_function_type(void_type_node
, void_list_node
),
758 // The runtime uses __builtin_prefetch.
759 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
760 build_varargs_function_type_list(void_type_node
,
765 // The compiler uses __builtin_unreachable for cases that can not
767 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
768 build_function_type(void_type_node
, void_list_node
),
772 // Get an unnamed integer type.
775 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
780 if (bits
== INT_TYPE_SIZE
)
781 type
= unsigned_type_node
;
782 else if (bits
== CHAR_TYPE_SIZE
)
783 type
= unsigned_char_type_node
;
784 else if (bits
== SHORT_TYPE_SIZE
)
785 type
= short_unsigned_type_node
;
786 else if (bits
== LONG_TYPE_SIZE
)
787 type
= long_unsigned_type_node
;
788 else if (bits
== LONG_LONG_TYPE_SIZE
)
789 type
= long_long_unsigned_type_node
;
791 type
= make_unsigned_type(bits
);
795 if (bits
== INT_TYPE_SIZE
)
796 type
= integer_type_node
;
797 else if (bits
== CHAR_TYPE_SIZE
)
798 type
= signed_char_type_node
;
799 else if (bits
== SHORT_TYPE_SIZE
)
800 type
= short_integer_type_node
;
801 else if (bits
== LONG_TYPE_SIZE
)
802 type
= long_integer_type_node
;
803 else if (bits
== LONG_LONG_TYPE_SIZE
)
804 type
= long_long_integer_type_node
;
806 type
= make_signed_type(bits
);
808 return this->make_type(type
);
811 // Get an unnamed float type.
814 Gcc_backend::float_type(int bits
)
817 if (bits
== FLOAT_TYPE_SIZE
)
818 type
= float_type_node
;
819 else if (bits
== DOUBLE_TYPE_SIZE
)
820 type
= double_type_node
;
821 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
822 type
= long_double_type_node
;
825 type
= make_node(REAL_TYPE
);
826 TYPE_PRECISION(type
) = bits
;
829 return this->make_type(type
);
832 // Get an unnamed complex type.
835 Gcc_backend::complex_type(int bits
)
838 if (bits
== FLOAT_TYPE_SIZE
* 2)
839 type
= complex_float_type_node
;
840 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
841 type
= complex_double_type_node
;
842 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
843 type
= complex_long_double_type_node
;
846 type
= make_node(REAL_TYPE
);
847 TYPE_PRECISION(type
) = bits
/ 2;
849 type
= build_complex_type(type
);
851 return this->make_type(type
);
854 // Get a pointer type.
857 Gcc_backend::pointer_type(Btype
* to_type
)
859 tree to_type_tree
= to_type
->get_tree();
860 if (to_type_tree
== error_mark_node
)
861 return this->error_type();
862 tree type
= build_pointer_type(to_type_tree
);
863 return this->make_type(type
);
866 // Make a function type.
869 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
870 const std::vector
<Btyped_identifier
>& parameters
,
871 const std::vector
<Btyped_identifier
>& results
,
872 Btype
* result_struct
,
875 tree args
= NULL_TREE
;
877 if (receiver
.btype
!= NULL
)
879 tree t
= receiver
.btype
->get_tree();
880 if (t
== error_mark_node
)
881 return this->error_type();
882 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
883 pp
= &TREE_CHAIN(*pp
);
886 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
887 p
!= parameters
.end();
890 tree t
= p
->btype
->get_tree();
891 if (t
== error_mark_node
)
892 return this->error_type();
893 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
894 pp
= &TREE_CHAIN(*pp
);
897 // Varargs is handled entirely at the Go level. When converted to
898 // GENERIC functions are not varargs.
899 *pp
= void_list_node
;
903 result
= void_type_node
;
904 else if (results
.size() == 1)
905 result
= results
.front().btype
->get_tree();
908 gcc_assert(result_struct
!= NULL
);
909 result
= result_struct
->get_tree();
911 if (result
== error_mark_node
)
912 return this->error_type();
914 // The libffi library can not represent a zero-sized object. To
915 // avoid causing confusion on 32-bit SPARC, we treat a function that
916 // returns a zero-sized value as returning void. That should do no
917 // harm since there is no actual value to be returned. See
918 // https://gcc.gnu.org/PR72814 for details.
919 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
920 result
= void_type_node
;
922 tree fntype
= build_function_type(result
, args
);
923 if (fntype
== error_mark_node
)
924 return this->error_type();
926 return this->make_type(build_pointer_type(fntype
));
929 // Make a struct type.
932 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
934 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
937 // Fill in the fields of a struct type.
940 Gcc_backend::fill_in_struct(Btype
* fill
,
941 const std::vector
<Btyped_identifier
>& fields
)
943 tree fill_tree
= fill
->get_tree();
944 tree field_trees
= NULL_TREE
;
945 tree
* pp
= &field_trees
;
946 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
950 tree name_tree
= get_identifier_from_string(p
->name
);
951 tree type_tree
= p
->btype
->get_tree();
952 if (type_tree
== error_mark_node
)
953 return this->error_type();
954 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
956 DECL_CONTEXT(field
) = fill_tree
;
958 pp
= &DECL_CHAIN(field
);
960 TYPE_FIELDS(fill_tree
) = field_trees
;
961 layout_type(fill_tree
);
965 // Make an array type.
968 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
970 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
971 element_btype
, length
);
974 // Fill in an array type.
977 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
980 tree element_type_tree
= element_type
->get_tree();
981 tree length_tree
= length
->get_tree();
982 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
983 return this->error_type();
985 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
987 length_tree
= fold_convert(sizetype
, length_tree
);
989 // build_index_type takes the maximum index, which is one less than
991 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
995 tree fill_tree
= fill
->get_tree();
996 TREE_TYPE(fill_tree
) = element_type_tree
;
997 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
998 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
999 layout_type(fill_tree
);
1001 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1002 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1003 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1004 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1005 TYPE_CANONICAL(fill_tree
) =
1006 build_array_type(TYPE_CANONICAL(element_type_tree
),
1007 TYPE_CANONICAL(index_type_tree
));
1012 // Create a placeholder for a pointer type.
1015 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1016 Location location
, bool)
1018 tree ret
= build_distinct_type_copy(ptr_type_node
);
1021 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1022 get_identifier_from_string(name
),
1024 TYPE_NAME(ret
) = decl
;
1026 return this->make_type(ret
);
1029 // Set the real target type for a placeholder pointer type.
1032 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1035 tree pt
= placeholder
->get_tree();
1036 if (pt
== error_mark_node
)
1038 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1039 tree tt
= to_type
->get_tree();
1040 if (tt
== error_mark_node
)
1042 placeholder
->set_tree(error_mark_node
);
1045 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1046 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1047 if (TYPE_NAME(pt
) != NULL_TREE
)
1049 // Build the data structure gcc wants to see for a typedef.
1050 tree copy
= build_variant_type_copy(pt
);
1051 TYPE_NAME(copy
) = NULL_TREE
;
1052 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1057 // Set the real values for a placeholder function type.
1060 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1062 return this->set_placeholder_pointer_type(placeholder
, ft
);
1065 // Create a placeholder for a struct type.
1068 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1071 tree ret
= make_node(RECORD_TYPE
);
1074 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1075 get_identifier_from_string(name
),
1077 TYPE_NAME(ret
) = decl
;
1079 return this->make_type(ret
);
1082 // Fill in the fields of a placeholder struct type.
1085 Gcc_backend::set_placeholder_struct_type(
1087 const std::vector
<Btyped_identifier
>& fields
)
1089 tree t
= placeholder
->get_tree();
1090 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1091 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1093 if (TYPE_NAME(t
) != NULL_TREE
)
1095 // Build the data structure gcc wants to see for a typedef.
1096 tree copy
= build_distinct_type_copy(t
);
1097 TYPE_NAME(copy
) = NULL_TREE
;
1098 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1101 return r
->get_tree() != error_mark_node
;
1104 // Create a placeholder for an array type.
1107 Gcc_backend::placeholder_array_type(const std::string
& name
,
1110 tree ret
= make_node(ARRAY_TYPE
);
1111 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1112 get_identifier_from_string(name
),
1114 TYPE_NAME(ret
) = decl
;
1115 return this->make_type(ret
);
1118 // Fill in the fields of a placeholder array type.
1121 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1122 Btype
* element_btype
,
1123 Bexpression
* length
)
1125 tree t
= placeholder
->get_tree();
1126 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1127 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1129 // Build the data structure gcc wants to see for a typedef.
1130 tree copy
= build_distinct_type_copy(t
);
1131 TYPE_NAME(copy
) = NULL_TREE
;
1132 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1134 return r
->get_tree() != error_mark_node
;
1137 // Return a named version of a type.
1140 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1143 tree type
= btype
->get_tree();
1144 if (type
== error_mark_node
)
1145 return this->error_type();
1147 // The middle-end expects a basic type to have a name. In Go every
1148 // basic type will have a name. The first time we see a basic type,
1149 // give it whatever Go name we have at this point.
1150 if (TYPE_NAME(type
) == NULL_TREE
1151 && location
.gcc_location() == BUILTINS_LOCATION
1152 && (TREE_CODE(type
) == INTEGER_TYPE
1153 || TREE_CODE(type
) == REAL_TYPE
1154 || TREE_CODE(type
) == COMPLEX_TYPE
1155 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1157 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1158 get_identifier_from_string(name
),
1160 TYPE_NAME(type
) = decl
;
1161 return this->make_type(type
);
1164 tree copy
= build_variant_type_copy(type
);
1165 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1166 get_identifier_from_string(name
),
1168 DECL_ORIGINAL_TYPE(decl
) = type
;
1169 TYPE_NAME(copy
) = decl
;
1170 return this->make_type(copy
);
1173 // Return a pointer type used as a marker for a circular type.
1176 Gcc_backend::circular_pointer_type(Btype
*, bool)
1178 return this->make_type(ptr_type_node
);
1181 // Return whether we might be looking at a circular type.
1184 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1186 return btype
->get_tree() == ptr_type_node
;
1189 // Return the size of a type.
1192 Gcc_backend::type_size(Btype
* btype
)
1194 tree t
= btype
->get_tree();
1195 if (t
== error_mark_node
)
1197 t
= TYPE_SIZE_UNIT(t
);
1198 gcc_assert(tree_fits_uhwi_p (t
));
1199 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1200 int64_t ret
= static_cast<int64_t>(val_wide
);
1201 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1206 // Return the alignment of a type.
1209 Gcc_backend::type_alignment(Btype
* btype
)
1211 tree t
= btype
->get_tree();
1212 if (t
== error_mark_node
)
1214 return TYPE_ALIGN_UNIT(t
);
1217 // Return the alignment of a struct field of type BTYPE.
1220 Gcc_backend::type_field_alignment(Btype
* btype
)
1222 tree t
= btype
->get_tree();
1223 if (t
== error_mark_node
)
1225 return go_field_alignment(t
);
1228 // Return the offset of a field in a struct.
1231 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1233 tree struct_tree
= btype
->get_tree();
1234 if (struct_tree
== error_mark_node
)
1236 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1237 tree field
= TYPE_FIELDS(struct_tree
);
1238 for (; index
> 0; --index
)
1240 field
= DECL_CHAIN(field
);
1241 gcc_assert(field
!= NULL_TREE
);
1243 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1244 int64_t ret
= static_cast<int64_t>(offset_wide
);
1245 gcc_assert(ret
== offset_wide
);
1249 // Return the zero value for a type.
1252 Gcc_backend::zero_expression(Btype
* btype
)
1254 tree t
= btype
->get_tree();
1256 if (t
== error_mark_node
)
1257 ret
= error_mark_node
;
1259 ret
= build_zero_cst(t
);
1260 return this->make_expression(ret
);
1263 // An expression that references a variable.
1266 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1268 tree ret
= var
->get_tree(location
);
1269 if (ret
== error_mark_node
)
1270 return this->error_expression();
1271 return this->make_expression(ret
);
1274 // An expression that indirectly references an expression.
1277 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1278 bool known_valid
, Location location
)
1280 tree expr_tree
= expr
->get_tree();
1281 tree type_tree
= btype
->get_tree();
1282 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1283 return this->error_expression();
1285 // If the type of EXPR is a recursive pointer type, then we
1286 // need to insert a cast before indirecting.
1287 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1288 if (VOID_TYPE_P(target_type_tree
))
1289 expr_tree
= fold_convert_loc(location
.gcc_location(),
1290 build_pointer_type(type_tree
), expr_tree
);
1292 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1295 TREE_THIS_NOTRAP(ret
) = 1;
1296 return this->make_expression(ret
);
1299 // Return an expression that declares a constant named NAME with the
1300 // constant value VAL in BTYPE.
1303 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1304 Bexpression
* val
, Location location
)
1306 tree type_tree
= btype
->get_tree();
1307 tree const_val
= val
->get_tree();
1308 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1309 return this->error_expression();
1311 tree name_tree
= get_identifier_from_string(name
);
1312 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1314 DECL_INITIAL(decl
) = const_val
;
1315 TREE_CONSTANT(decl
) = 1;
1316 TREE_READONLY(decl
) = 1;
1318 go_preserve_from_gc(decl
);
1319 return this->make_expression(decl
);
1322 // Return a typed value as a constant integer.
1325 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1327 tree t
= btype
->get_tree();
1328 if (t
== error_mark_node
)
1329 return this->error_expression();
1331 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1332 return this->make_expression(ret
);
1335 // Return a typed value as a constant floating-point number.
1338 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1340 tree t
= btype
->get_tree();
1342 if (t
== error_mark_node
)
1343 return this->error_expression();
1346 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1348 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1349 ret
= build_real(t
, r2
);
1350 return this->make_expression(ret
);
1353 // Return a typed real and imaginary value as a constant complex number.
1356 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1358 tree t
= btype
->get_tree();
1360 if (t
== error_mark_node
)
1361 return this->error_expression();
1364 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1366 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1369 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1371 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1373 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1374 build_real(TREE_TYPE(t
), r4
));
1375 return this->make_expression(ret
);
1378 // Make a constant string expression.
1381 Gcc_backend::string_constant_expression(const std::string
& val
)
1383 tree index_type
= build_index_type(size_int(val
.length()));
1384 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1386 tree string_type
= build_array_type(const_char_type
, index_type
);
1387 TYPE_STRING_FLAG(string_type
) = 1;
1388 tree string_val
= build_string(val
.length(), val
.data());
1389 TREE_TYPE(string_val
) = string_type
;
1391 return this->make_expression(string_val
);
1394 // Make a constant boolean expression.
1397 Gcc_backend::boolean_constant_expression(bool val
)
1399 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1400 return this->make_expression(bool_cst
);
1403 // Return the real part of a complex expression.
1406 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1408 tree complex_tree
= bcomplex
->get_tree();
1409 if (complex_tree
== error_mark_node
)
1410 return this->error_expression();
1411 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1412 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1413 TREE_TYPE(TREE_TYPE(complex_tree
)),
1415 return this->make_expression(ret
);
1418 // Return the imaginary part of a complex expression.
1421 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1423 tree complex_tree
= bcomplex
->get_tree();
1424 if (complex_tree
== error_mark_node
)
1425 return this->error_expression();
1426 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1427 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1428 TREE_TYPE(TREE_TYPE(complex_tree
)),
1430 return this->make_expression(ret
);
1433 // Make a complex expression given its real and imaginary parts.
1436 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1439 tree real_tree
= breal
->get_tree();
1440 tree imag_tree
= bimag
->get_tree();
1441 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1442 return this->error_expression();
1443 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1444 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1445 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1446 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1447 build_complex_type(TREE_TYPE(real_tree
)),
1448 real_tree
, imag_tree
);
1449 return this->make_expression(ret
);
1452 // An expression that converts an expression to a different type.
1455 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1458 tree type_tree
= type
->get_tree();
1459 tree expr_tree
= expr
->get_tree();
1460 if (type_tree
== error_mark_node
1461 || expr_tree
== error_mark_node
1462 || TREE_TYPE(expr_tree
) == error_mark_node
)
1463 return this->error_expression();
1466 if (this->type_size(type
) == 0)
1468 // Do not convert zero-sized types.
1471 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1472 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1473 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1474 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1475 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1476 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1477 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1478 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1479 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1480 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1481 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1482 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1483 type_tree
, expr_tree
);
1485 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1487 return this->make_expression(ret
);
1490 // Get the address of a function.
1493 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1495 tree func
= bfunc
->get_tree();
1496 if (func
== error_mark_node
)
1497 return this->error_expression();
1499 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1500 return this->make_expression(ret
);
1503 // Get the address of an expression.
1506 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1508 tree expr
= bexpr
->get_tree();
1509 if (expr
== error_mark_node
)
1510 return this->error_expression();
1512 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1513 return this->make_expression(ret
);
1516 // Return an expression for the field at INDEX in BSTRUCT.
1519 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1522 tree struct_tree
= bstruct
->get_tree();
1523 if (struct_tree
== error_mark_node
1524 || TREE_TYPE(struct_tree
) == error_mark_node
)
1525 return this->error_expression();
1526 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1527 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1528 if (field
== NULL_TREE
)
1530 // This can happen for a type which refers to itself indirectly
1531 // and then turns out to be erroneous.
1532 return this->error_expression();
1534 for (unsigned int i
= index
; i
> 0; --i
)
1536 field
= DECL_CHAIN(field
);
1537 gcc_assert(field
!= NULL_TREE
);
1539 if (TREE_TYPE(field
) == error_mark_node
)
1540 return this->error_expression();
1541 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1542 TREE_TYPE(field
), struct_tree
, field
,
1544 if (TREE_CONSTANT(struct_tree
))
1545 TREE_CONSTANT(ret
) = 1;
1546 return this->make_expression(ret
);
1549 // Return an expression that executes BSTAT before BEXPR.
1552 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1555 tree stat
= bstat
->get_tree();
1556 tree expr
= bexpr
->get_tree();
1557 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1558 return this->error_expression();
1559 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1560 TREE_TYPE(expr
), stat
, expr
);
1561 return this->make_expression(ret
);
1564 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1565 // ELSE_EXPR otherwise.
1568 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1569 Bexpression
* condition
,
1570 Bexpression
* then_expr
,
1571 Bexpression
* else_expr
, Location location
)
1573 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1574 tree cond_tree
= condition
->get_tree();
1575 tree then_tree
= then_expr
->get_tree();
1576 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1577 if (type_tree
== error_mark_node
1578 || cond_tree
== error_mark_node
1579 || then_tree
== error_mark_node
1580 || else_tree
== error_mark_node
)
1581 return this->error_expression();
1582 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1583 cond_tree
, then_tree
, else_tree
);
1584 return this->make_expression(ret
);
1587 // Return an expression for the unary operation OP EXPR.
1590 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1592 tree expr_tree
= expr
->get_tree();
1593 if (expr_tree
== error_mark_node
1594 || TREE_TYPE(expr_tree
) == error_mark_node
)
1595 return this->error_expression();
1597 tree type_tree
= TREE_TYPE(expr_tree
);
1598 enum tree_code code
;
1601 case OPERATOR_MINUS
:
1603 tree computed_type
= excess_precision_type(type_tree
);
1604 if (computed_type
!= NULL_TREE
)
1606 expr_tree
= convert(computed_type
, expr_tree
);
1607 type_tree
= computed_type
;
1613 code
= TRUTH_NOT_EXPR
;
1616 code
= BIT_NOT_EXPR
;
1623 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1625 return this->make_expression(ret
);
1628 // Convert a gofrontend operator to an equivalent tree_code.
1630 static enum tree_code
1631 operator_to_tree_code(Operator op
, tree type
)
1633 enum tree_code code
;
1639 case OPERATOR_NOTEQ
:
1655 code
= TRUTH_ORIF_EXPR
;
1657 case OPERATOR_ANDAND
:
1658 code
= TRUTH_ANDIF_EXPR
;
1663 case OPERATOR_MINUS
:
1667 code
= BIT_IOR_EXPR
;
1670 code
= BIT_XOR_EXPR
;
1676 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1679 code
= TRUNC_DIV_EXPR
;
1682 code
= TRUNC_MOD_EXPR
;
1684 case OPERATOR_LSHIFT
:
1687 case OPERATOR_RSHIFT
:
1691 code
= BIT_AND_EXPR
;
1693 case OPERATOR_BITCLEAR
:
1694 code
= BIT_AND_EXPR
;
1703 // Return an expression for the binary operation LEFT OP RIGHT.
1706 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1707 Bexpression
* right
, Location location
)
1709 tree left_tree
= left
->get_tree();
1710 tree right_tree
= right
->get_tree();
1711 if (left_tree
== error_mark_node
1712 || right_tree
== error_mark_node
)
1713 return this->error_expression();
1714 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1716 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1717 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1718 tree computed_type
= excess_precision_type(type_tree
);
1719 if (computed_type
!= NULL_TREE
)
1721 left_tree
= convert(computed_type
, left_tree
);
1722 right_tree
= convert(computed_type
, right_tree
);
1723 type_tree
= computed_type
;
1726 // For comparison operators, the resulting type should be boolean.
1730 case OPERATOR_NOTEQ
:
1735 type_tree
= boolean_type_node
;
1741 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1742 left_tree
, right_tree
);
1743 return this->make_expression(ret
);
1746 // Return an expression that constructs BTYPE with VALS.
1749 Gcc_backend::constructor_expression(Btype
* btype
,
1750 const std::vector
<Bexpression
*>& vals
,
1753 tree type_tree
= btype
->get_tree();
1754 if (type_tree
== error_mark_node
)
1755 return this->error_expression();
1757 vec
<constructor_elt
, va_gc
> *init
;
1758 vec_alloc(init
, vals
.size());
1760 tree sink
= NULL_TREE
;
1761 bool is_constant
= true;
1762 tree field
= TYPE_FIELDS(type_tree
);
1763 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1765 ++p
, field
= DECL_CHAIN(field
))
1767 gcc_assert(field
!= NULL_TREE
);
1768 tree val
= (*p
)->get_tree();
1769 if (TREE_TYPE(field
) == error_mark_node
1770 || val
== error_mark_node
1771 || TREE_TYPE(val
) == error_mark_node
)
1772 return this->error_expression();
1774 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1776 // GIMPLE cannot represent indices of zero-sized types so
1777 // trying to construct a map with zero-sized keys might lead
1778 // to errors. Instead, we evaluate each expression that
1779 // would have been added as a map element for its
1780 // side-effects and construct an empty map.
1781 append_to_statement_list(val
, &sink
);
1785 constructor_elt empty
= {NULL
, NULL
};
1786 constructor_elt
* elt
= init
->quick_push(empty
);
1788 elt
->value
= fold_convert_loc(location
.gcc_location(), TREE_TYPE(field
),
1790 if (!TREE_CONSTANT(elt
->value
))
1791 is_constant
= false;
1793 gcc_assert(field
== NULL_TREE
);
1794 tree ret
= build_constructor(type_tree
, init
);
1796 TREE_CONSTANT(ret
) = 1;
1797 if (sink
!= NULL_TREE
)
1798 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1799 type_tree
, sink
, ret
);
1800 return this->make_expression(ret
);
1804 Gcc_backend::array_constructor_expression(
1805 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1806 const std::vector
<Bexpression
*>& vals
, Location location
)
1808 tree type_tree
= array_btype
->get_tree();
1809 if (type_tree
== error_mark_node
)
1810 return this->error_expression();
1812 gcc_assert(indexes
.size() == vals
.size());
1814 tree element_type
= TREE_TYPE(type_tree
);
1815 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1816 vec
<constructor_elt
, va_gc
> *init
;
1817 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1819 tree sink
= NULL_TREE
;
1820 bool is_constant
= true;
1821 for (size_t i
= 0; i
< vals
.size(); ++i
)
1823 tree index
= size_int(indexes
[i
]);
1824 tree val
= (vals
[i
])->get_tree();
1826 if (index
== error_mark_node
1827 || val
== error_mark_node
)
1828 return this->error_expression();
1830 if (element_size
== 0)
1832 // GIMPLE cannot represent arrays of zero-sized types so trying
1833 // to construct an array of zero-sized values might lead to errors.
1834 // Instead, we evaluate each expression that would have been added as
1835 // an array value for its side-effects and construct an empty array.
1836 append_to_statement_list(val
, &sink
);
1840 if (!TREE_CONSTANT(val
))
1841 is_constant
= false;
1843 constructor_elt empty
= {NULL
, NULL
};
1844 constructor_elt
* elt
= init
->quick_push(empty
);
1849 tree ret
= build_constructor(type_tree
, init
);
1851 TREE_CONSTANT(ret
) = 1;
1852 if (sink
!= NULL_TREE
)
1853 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1854 type_tree
, sink
, ret
);
1855 return this->make_expression(ret
);
1858 // Return an expression for the address of BASE[INDEX].
1861 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
1864 tree base_tree
= base
->get_tree();
1865 tree index_tree
= index
->get_tree();
1866 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
1867 if (base_tree
== error_mark_node
1868 || TREE_TYPE(base_tree
) == error_mark_node
1869 || index_tree
== error_mark_node
1870 || element_type_tree
== error_mark_node
)
1871 return this->error_expression();
1873 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
1874 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
1875 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
1876 index_tree
, element_size
);
1877 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
1878 TREE_TYPE(base_tree
), base_tree
, offset
);
1879 return this->make_expression(ptr
);
1882 // Return an expression representing ARRAY[INDEX]
1885 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
1888 tree array_tree
= array
->get_tree();
1889 tree index_tree
= index
->get_tree();
1890 if (array_tree
== error_mark_node
1891 || TREE_TYPE(array_tree
) == error_mark_node
1892 || index_tree
== error_mark_node
)
1893 return this->error_expression();
1895 tree ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
1896 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
1897 index_tree
, NULL_TREE
, NULL_TREE
);
1898 return this->make_expression(ret
);
1901 // Create an expression for a call to FN_EXPR with FN_ARGS.
1903 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
1904 Bexpression
* fn_expr
,
1905 const std::vector
<Bexpression
*>& fn_args
,
1906 Bexpression
* chain_expr
,
1909 tree fn
= fn_expr
->get_tree();
1910 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
1911 return this->error_expression();
1913 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
1914 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
1916 size_t nargs
= fn_args
.size();
1917 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
1918 for (size_t i
= 0; i
< nargs
; ++i
)
1920 args
[i
] = fn_args
.at(i
)->get_tree();
1921 if (args
[i
] == error_mark_node
)
1922 return this->error_expression();
1926 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
1927 fndecl
= TREE_OPERAND(fndecl
, 0);
1929 // This is to support builtin math functions when using 80387 math.
1930 tree excess_type
= NULL_TREE
;
1932 && TREE_CODE(fndecl
) == FUNCTION_DECL
1933 && DECL_IS_BUILTIN(fndecl
)
1934 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
1936 && ((SCALAR_FLOAT_TYPE_P(rettype
)
1937 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
1938 || (COMPLEX_FLOAT_TYPE_P(rettype
)
1939 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
1941 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
1942 if (excess_type
!= NULL_TREE
)
1944 tree excess_fndecl
= mathfn_built_in(excess_type
,
1945 DECL_FUNCTION_CODE(fndecl
));
1946 if (excess_fndecl
== NULL_TREE
)
1947 excess_type
= NULL_TREE
;
1950 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
1952 for (size_t i
= 0; i
< nargs
; ++i
)
1954 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
1955 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
1956 args
[i
] = ::convert(excess_type
, args
[i
]);
1963 build_call_array_loc(location
.gcc_location(),
1964 excess_type
!= NULL_TREE
? excess_type
: rettype
,
1968 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
1970 if (excess_type
!= NULL_TREE
)
1972 // Calling convert here can undo our excess precision change.
1973 // That may or may not be a bug in convert_to_real.
1974 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
1978 return this->make_expression(ret
);
1981 // Return an expression that allocates SIZE bytes on the stack.
1984 Gcc_backend::stack_allocation_expression(int64_t size
, Location location
)
1986 tree alloca
= builtin_decl_explicit(BUILT_IN_ALLOCA
);
1987 tree size_tree
= build_int_cst(integer_type_node
, size
);
1988 tree ret
= build_call_expr_loc(location
.gcc_location(), alloca
, 1, size_tree
);
1989 tree memset
= builtin_decl_explicit(BUILT_IN_MEMSET
);
1990 ret
= build_call_expr_loc(location
.gcc_location(), memset
, 3,
1991 ret
, integer_zero_node
, size_tree
);
1992 return this->make_expression(ret
);
1995 // An expression as a statement.
1998 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2000 return this->make_statement(expr
->get_tree());
2003 // Variable initialization.
2006 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2008 tree var_tree
= var
->get_decl();
2009 tree init_tree
= init
->get_tree();
2010 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2011 return this->error_statement();
2012 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2014 // To avoid problems with GNU ld, we don't make zero-sized
2015 // externally visible variables. That might lead us to doing an
2016 // initialization of a zero-sized expression to a non-zero sized
2017 // variable, or vice-versa. Avoid crashes by omitting the
2018 // initializer. Such initializations don't mean anything anyhow.
2019 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2020 && init_tree
!= NULL_TREE
2021 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2023 DECL_INITIAL(var_tree
) = init_tree
;
2024 init_tree
= NULL_TREE
;
2027 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2028 void_type_node
, var_tree
);
2029 if (init_tree
!= NULL_TREE
)
2030 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2031 void_type_node
, init_tree
, ret
);
2033 return this->make_statement(ret
);
2039 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2040 Bexpression
* rhs
, Location location
)
2042 tree lhs_tree
= lhs
->get_tree();
2043 tree rhs_tree
= rhs
->get_tree();
2044 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2045 return this->error_statement();
2047 // To avoid problems with GNU ld, we don't make zero-sized
2048 // externally visible variables. That might lead us to doing an
2049 // assignment of a zero-sized expression to a non-zero sized
2050 // expression; avoid crashes here by avoiding assignments of
2051 // zero-sized expressions. Such assignments don't really mean
2053 if (int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2054 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2055 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2056 this->expression_statement(bfn
, rhs
));
2058 // Sometimes the same unnamed Go type can be created multiple times
2059 // and thus have multiple tree representations. Make sure this does
2060 // not confuse the middle-end.
2061 if (TREE_TYPE(lhs_tree
) != TREE_TYPE(rhs_tree
))
2063 tree lhs_type_tree
= TREE_TYPE(lhs_tree
);
2064 gcc_assert(TREE_CODE(lhs_type_tree
) == TREE_CODE(TREE_TYPE(rhs_tree
)));
2065 if (POINTER_TYPE_P(lhs_type_tree
)
2066 || INTEGRAL_TYPE_P(lhs_type_tree
)
2067 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
2068 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
2069 rhs_tree
= fold_convert_loc(location
.gcc_location(), lhs_type_tree
,
2071 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
2072 || TREE_CODE(lhs_type_tree
) == ARRAY_TYPE
)
2074 gcc_assert(int_size_in_bytes(lhs_type_tree
)
2075 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
2076 rhs_tree
= fold_build1_loc(location
.gcc_location(),
2078 lhs_type_tree
, rhs_tree
);
2082 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2085 lhs_tree
, rhs_tree
));
2091 Gcc_backend::return_statement(Bfunction
* bfunction
,
2092 const std::vector
<Bexpression
*>& vals
,
2095 tree fntree
= bfunction
->get_tree();
2096 if (fntree
== error_mark_node
)
2097 return this->error_statement();
2098 tree result
= DECL_RESULT(fntree
);
2099 if (result
== error_mark_node
)
2100 return this->error_statement();
2102 // If the result size is zero bytes, we have set the function type
2103 // to have a result type of void, so don't return anything.
2104 // See the function_type method.
2105 tree res_type
= TREE_TYPE(result
);
2106 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2108 tree stmt_list
= NULL_TREE
;
2109 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2113 tree val
= (*p
)->get_tree();
2114 if (val
== error_mark_node
)
2115 return this->error_statement();
2116 append_to_statement_list(val
, &stmt_list
);
2118 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2119 void_type_node
, NULL_TREE
);
2120 append_to_statement_list(ret
, &stmt_list
);
2121 return this->make_statement(stmt_list
);
2126 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2128 else if (vals
.size() == 1)
2130 tree val
= vals
.front()->get_tree();
2131 if (val
== error_mark_node
)
2132 return this->error_statement();
2133 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2134 void_type_node
, result
,
2135 vals
.front()->get_tree());
2136 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2137 void_type_node
, set
);
2141 // To return multiple values, copy the values into a temporary
2142 // variable of the right structure type, and then assign the
2143 // temporary variable to the DECL_RESULT in the return
2145 tree stmt_list
= NULL_TREE
;
2146 tree rettype
= TREE_TYPE(result
);
2148 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2149 push_struct_function(fntree
);
2151 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2152 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2155 tree field
= TYPE_FIELDS(rettype
);
2156 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2158 p
++, field
= DECL_CHAIN(field
))
2160 gcc_assert(field
!= NULL_TREE
);
2161 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2162 TREE_TYPE(field
), rettmp
, field
,
2164 tree val
= (*p
)->get_tree();
2165 if (val
== error_mark_node
)
2166 return this->error_statement();
2167 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2169 ref
, (*p
)->get_tree());
2170 append_to_statement_list(set
, &stmt_list
);
2172 gcc_assert(field
== NULL_TREE
);
2173 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2176 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2177 void_type_node
, set
);
2178 append_to_statement_list(ret_expr
, &stmt_list
);
2181 return this->make_statement(ret
);
2184 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2185 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2186 // NULL, it will always be executed. This is used for handling defers in Go
2187 // functions. In C++, the resulting code is of this form:
2188 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2191 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2192 Bstatement
* except_stmt
,
2193 Bstatement
* finally_stmt
,
2196 tree stat_tree
= bstat
->get_tree();
2197 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2198 tree finally_tree
= finally_stmt
== NULL
2200 : finally_stmt
->get_tree();
2202 if (stat_tree
== error_mark_node
2203 || except_tree
== error_mark_node
2204 || finally_tree
== error_mark_node
)
2205 return this->error_statement();
2207 if (except_tree
!= NULL_TREE
)
2208 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2209 void_type_node
, stat_tree
,
2210 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2211 void_type_node
, NULL
, except_tree
));
2212 if (finally_tree
!= NULL_TREE
)
2213 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2214 void_type_node
, stat_tree
, finally_tree
);
2215 return this->make_statement(stat_tree
);
2221 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2222 Bblock
* then_block
, Bblock
* else_block
,
2225 tree cond_tree
= condition
->get_tree();
2226 tree then_tree
= then_block
->get_tree();
2227 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2228 if (cond_tree
== error_mark_node
2229 || then_tree
== error_mark_node
2230 || else_tree
== error_mark_node
)
2231 return this->error_statement();
2232 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2233 cond_tree
, then_tree
, else_tree
);
2234 return this->make_statement(ret
);
2240 Gcc_backend::switch_statement(
2241 Bfunction
* function
,
2243 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2244 const std::vector
<Bstatement
*>& statements
,
2245 Location switch_location
)
2247 gcc_assert(cases
.size() == statements
.size());
2249 tree decl
= function
->get_tree();
2250 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2251 push_struct_function(decl
);
2253 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2255 tree stmt_list
= NULL_TREE
;
2256 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2257 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2258 ps
!= statements
.end();
2263 source_location loc
= (*ps
!= NULL
2264 ? EXPR_LOCATION((*ps
)->get_tree())
2265 : UNKNOWN_LOCATION
);
2266 tree label
= create_artificial_label(loc
);
2267 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2268 append_to_statement_list(c
, &stmt_list
);
2272 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2276 tree t
= (*pcv
)->get_tree();
2277 if (t
== error_mark_node
)
2278 return this->error_statement();
2279 source_location loc
= EXPR_LOCATION(t
);
2280 tree label
= create_artificial_label(loc
);
2281 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2282 append_to_statement_list(c
, &stmt_list
);
2288 tree t
= (*ps
)->get_tree();
2289 if (t
== error_mark_node
)
2290 return this->error_statement();
2291 append_to_statement_list(t
, &stmt_list
);
2296 tree tv
= value
->get_tree();
2297 if (tv
== error_mark_node
)
2298 return this->error_statement();
2299 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2300 NULL_TREE
, tv
, stmt_list
);
2301 return this->make_statement(t
);
2304 // Pair of statements.
2307 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2309 tree stmt_list
= NULL_TREE
;
2310 tree t
= s1
->get_tree();
2311 if (t
== error_mark_node
)
2312 return this->error_statement();
2313 append_to_statement_list(t
, &stmt_list
);
2315 if (t
== error_mark_node
)
2316 return this->error_statement();
2317 append_to_statement_list(t
, &stmt_list
);
2319 // If neither statement has any side effects, stmt_list can be NULL
2321 if (stmt_list
== NULL_TREE
)
2322 stmt_list
= integer_zero_node
;
2324 return this->make_statement(stmt_list
);
2327 // List of statements.
2330 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2332 tree stmt_list
= NULL_TREE
;
2333 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2334 p
!= statements
.end();
2337 tree t
= (*p
)->get_tree();
2338 if (t
== error_mark_node
)
2339 return this->error_statement();
2340 append_to_statement_list(t
, &stmt_list
);
2342 return this->make_statement(stmt_list
);
2345 // Make a block. For some reason gcc uses a dual structure for
2346 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2347 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2351 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2352 const std::vector
<Bvariable
*>& vars
,
2353 Location start_location
,
2356 tree block_tree
= make_node(BLOCK
);
2357 if (enclosing
== NULL
)
2359 tree fndecl
= function
->get_tree();
2360 gcc_assert(fndecl
!= NULL_TREE
);
2362 // We may have already created a block for local variables when
2363 // we take the address of a parameter.
2364 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2366 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2367 DECL_INITIAL(fndecl
) = block_tree
;
2371 tree superblock_tree
= DECL_INITIAL(fndecl
);
2372 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2374 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2376 pp
= &BLOCK_CHAIN(*pp
))
2383 tree superbind_tree
= enclosing
->get_tree();
2384 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2385 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2387 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2389 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2391 pp
= &BLOCK_CHAIN(*pp
))
2396 tree
* pp
= &BLOCK_VARS(block_tree
);
2397 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2401 *pp
= (*pv
)->get_decl();
2402 if (*pp
!= error_mark_node
)
2403 pp
= &DECL_CHAIN(*pp
);
2407 TREE_USED(block_tree
) = 1;
2409 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2410 void_type_node
, BLOCK_VARS(block_tree
),
2411 NULL_TREE
, block_tree
);
2412 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2413 return new Bblock(bind_tree
);
2416 // Add statements to a block.
2419 Gcc_backend::block_add_statements(Bblock
* bblock
,
2420 const std::vector
<Bstatement
*>& statements
)
2422 tree stmt_list
= NULL_TREE
;
2423 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2424 p
!= statements
.end();
2427 tree s
= (*p
)->get_tree();
2428 if (s
!= error_mark_node
)
2429 append_to_statement_list(s
, &stmt_list
);
2432 tree bind_tree
= bblock
->get_tree();
2433 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2434 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2437 // Return a block as a statement.
2440 Gcc_backend::block_statement(Bblock
* bblock
)
2442 tree bind_tree
= bblock
->get_tree();
2443 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2444 return this->make_statement(bind_tree
);
2447 // This is not static because we declare it with GTY(()) in go-c.h.
2448 tree go_non_zero_struct
;
2450 // Return a type corresponding to TYPE with non-zero size.
2453 Gcc_backend::non_zero_size_type(tree type
)
2455 if (int_size_in_bytes(type
) != 0)
2458 switch (TREE_CODE(type
))
2461 if (TYPE_FIELDS(type
) != NULL_TREE
)
2463 tree ns
= make_node(RECORD_TYPE
);
2464 tree field_trees
= NULL_TREE
;
2465 tree
*pp
= &field_trees
;
2466 for (tree field
= TYPE_FIELDS(type
);
2468 field
= DECL_CHAIN(field
))
2470 tree ft
= TREE_TYPE(field
);
2471 if (field
== TYPE_FIELDS(type
))
2472 ft
= non_zero_size_type(ft
);
2473 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2474 DECL_NAME(field
), ft
);
2475 DECL_CONTEXT(f
) = ns
;
2477 pp
= &DECL_CHAIN(f
);
2479 TYPE_FIELDS(ns
) = field_trees
;
2484 if (go_non_zero_struct
== NULL_TREE
)
2486 type
= make_node(RECORD_TYPE
);
2487 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2488 get_identifier("dummy"),
2490 DECL_CONTEXT(field
) = type
;
2491 TYPE_FIELDS(type
) = field
;
2493 go_non_zero_struct
= type
;
2495 return go_non_zero_struct
;
2499 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2500 return build_array_type_nelts(element_type
, 1);
2510 // Make a global variable.
2513 Gcc_backend::global_variable(const std::string
& var_name
,
2514 const std::string
& asm_name
,
2518 bool in_unique_section
,
2521 tree type_tree
= btype
->get_tree();
2522 if (type_tree
== error_mark_node
)
2523 return this->error_variable();
2525 // The GNU linker does not like dynamic variables with zero size.
2526 tree orig_type_tree
= type_tree
;
2527 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2528 type_tree
= this->non_zero_size_type(type_tree
);
2530 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2531 get_identifier_from_string(var_name
),
2534 DECL_EXTERNAL(decl
) = 1;
2536 TREE_STATIC(decl
) = 1;
2539 TREE_PUBLIC(decl
) = 1;
2540 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2544 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2547 TREE_USED(decl
) = 1;
2549 if (in_unique_section
)
2550 resolve_unique_section (decl
, 0, 1);
2552 go_preserve_from_gc(decl
);
2554 return new Bvariable(decl
, orig_type_tree
);
2557 // Set the initial value of a global variable.
2560 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2562 tree expr_tree
= expr
->get_tree();
2563 if (expr_tree
== error_mark_node
)
2565 gcc_assert(TREE_CONSTANT(expr_tree
));
2566 tree var_decl
= var
->get_decl();
2567 if (var_decl
== error_mark_node
)
2569 DECL_INITIAL(var_decl
) = expr_tree
;
2571 // If this variable goes in a unique section, it may need to go into
2572 // a different one now that DECL_INITIAL is set.
2573 if (symtab_node::get(var_decl
)
2574 && symtab_node::get(var_decl
)->implicit_section
)
2576 set_decl_section_name (var_decl
, NULL
);
2577 resolve_unique_section (var_decl
,
2578 compute_reloc_for_constant (expr_tree
),
2583 // Make a local variable.
2586 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2587 Btype
* btype
, Bvariable
* decl_var
,
2588 bool is_address_taken
, Location location
)
2590 tree type_tree
= btype
->get_tree();
2591 if (type_tree
== error_mark_node
)
2592 return this->error_variable();
2593 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2594 get_identifier_from_string(name
),
2596 DECL_CONTEXT(decl
) = function
->get_tree();
2597 TREE_USED(decl
) = 1;
2598 if (is_address_taken
)
2599 TREE_ADDRESSABLE(decl
) = 1;
2600 if (decl_var
!= NULL
)
2602 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2603 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2605 go_preserve_from_gc(decl
);
2606 return new Bvariable(decl
);
2609 // Make a function parameter variable.
2612 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2613 Btype
* btype
, bool is_address_taken
,
2616 tree type_tree
= btype
->get_tree();
2617 if (type_tree
== error_mark_node
)
2618 return this->error_variable();
2619 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2620 get_identifier_from_string(name
),
2622 DECL_CONTEXT(decl
) = function
->get_tree();
2623 DECL_ARG_TYPE(decl
) = type_tree
;
2624 TREE_USED(decl
) = 1;
2625 if (is_address_taken
)
2626 TREE_ADDRESSABLE(decl
) = 1;
2627 go_preserve_from_gc(decl
);
2628 return new Bvariable(decl
);
2631 // Make a static chain variable.
2634 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2635 Btype
* btype
, Location location
)
2637 tree type_tree
= btype
->get_tree();
2638 if (type_tree
== error_mark_node
)
2639 return this->error_variable();
2640 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2641 get_identifier_from_string(name
), type_tree
);
2642 tree fndecl
= function
->get_tree();
2643 DECL_CONTEXT(decl
) = fndecl
;
2644 DECL_ARG_TYPE(decl
) = type_tree
;
2645 TREE_USED(decl
) = 1;
2646 DECL_ARTIFICIAL(decl
) = 1;
2647 DECL_IGNORED_P(decl
) = 1;
2648 TREE_READONLY(decl
) = 1;
2650 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2653 push_struct_function(fndecl
);
2655 f
= DECL_STRUCT_FUNCTION(fndecl
);
2657 gcc_assert(f
->static_chain_decl
== NULL
);
2658 f
->static_chain_decl
= decl
;
2659 DECL_STATIC_CHAIN(fndecl
) = 1;
2661 go_preserve_from_gc(decl
);
2662 return new Bvariable(decl
);
2665 // Make a temporary variable.
2668 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2669 Btype
* btype
, Bexpression
* binit
,
2670 bool is_address_taken
,
2672 Bstatement
** pstatement
)
2674 gcc_assert(function
!= NULL
);
2675 tree decl
= function
->get_tree();
2676 tree type_tree
= btype
->get_tree();
2677 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2678 if (type_tree
== error_mark_node
2679 || init_tree
== error_mark_node
2680 || decl
== error_mark_node
)
2682 *pstatement
= this->error_statement();
2683 return this->error_variable();
2687 // We can only use create_tmp_var if the type is not addressable.
2688 if (!TREE_ADDRESSABLE(type_tree
))
2690 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2691 push_struct_function(decl
);
2693 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2695 var
= create_tmp_var(type_tree
, "GOTMP");
2700 gcc_assert(bblock
!= NULL
);
2701 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2702 create_tmp_var_name("GOTMP"),
2704 DECL_ARTIFICIAL(var
) = 1;
2705 DECL_IGNORED_P(var
) = 1;
2707 DECL_CONTEXT(var
) = decl
;
2709 // We have to add this variable to the BLOCK and the BIND_EXPR.
2710 tree bind_tree
= bblock
->get_tree();
2711 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2712 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2713 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2714 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2715 BLOCK_VARS(block_tree
) = var
;
2716 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2719 if (this->type_size(btype
) != 0 && init_tree
!= NULL_TREE
)
2720 DECL_INITIAL(var
) = fold_convert_loc(location
.gcc_location(), type_tree
,
2723 if (is_address_taken
)
2724 TREE_ADDRESSABLE(var
) = 1;
2726 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2728 void_type_node
, var
));
2730 // Don't initialize VAR with BINIT, but still evaluate BINIT for
2731 // its side effects.
2732 if (this->type_size(btype
) == 0 && init_tree
!= NULL_TREE
)
2734 this->compound_statement(this->expression_statement(function
, binit
),
2737 return new Bvariable(var
);
2740 // Create an implicit variable that is compiler-defined. This is used when
2741 // generating GC root variables and storing the values of a slice initializer.
2744 Gcc_backend::implicit_variable(const std::string
& name
,
2745 const std::string
& asm_name
,
2746 Btype
* type
, bool is_hidden
, bool is_constant
,
2747 bool is_common
, int64_t alignment
)
2749 tree type_tree
= type
->get_tree();
2750 if (type_tree
== error_mark_node
)
2751 return this->error_variable();
2753 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2754 get_identifier_from_string(name
), type_tree
);
2755 DECL_EXTERNAL(decl
) = 0;
2756 TREE_PUBLIC(decl
) = !is_hidden
;
2757 TREE_STATIC(decl
) = 1;
2758 TREE_USED(decl
) = 1;
2759 DECL_ARTIFICIAL(decl
) = 1;
2762 DECL_COMMON(decl
) = 1;
2764 // When the initializer for one implicit_variable refers to another,
2765 // it needs to know the visibility of the referenced struct so that
2766 // compute_reloc_for_constant will return the right value. On many
2767 // systems calling make_decl_one_only will mark the decl as weak,
2768 // which will change the return value of compute_reloc_for_constant.
2769 // We can't reliably call make_decl_one_only yet, because we don't
2770 // yet know the initializer. This issue doesn't arise in C because
2771 // Go initializers, unlike C initializers, can be indirectly
2772 // recursive. To ensure that compute_reloc_for_constant computes
2773 // the right value if some other initializer refers to this one, we
2774 // mark this symbol as weak here. We undo that below in
2775 // immutable_struct_set_init before calling mark_decl_one_only.
2776 DECL_WEAK(decl
) = 1;
2780 TREE_READONLY(decl
) = 1;
2781 TREE_CONSTANT(decl
) = 1;
2785 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
2786 DECL_USER_ALIGN(decl
) = 1;
2788 if (! asm_name
.empty())
2789 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2791 go_preserve_from_gc(decl
);
2792 return new Bvariable(decl
);
2795 // Set the initalizer for a variable created by implicit_variable.
2796 // This is where we finish compiling the variable.
2799 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
2800 Btype
*, bool, bool, bool is_common
,
2803 tree decl
= var
->get_decl();
2806 init_tree
= NULL_TREE
;
2808 init_tree
= init
->get_tree();
2809 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2812 DECL_INITIAL(decl
) = init_tree
;
2814 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2815 // See the comment where DECL_WEAK is set in implicit_variable.
2818 DECL_WEAK(decl
) = 0;
2819 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2822 resolve_unique_section(decl
, 2, 1);
2824 rest_of_decl_compilation(decl
, 1, 0);
2827 // Return a reference to an implicit variable defined in another package.
2830 Gcc_backend::implicit_variable_reference(const std::string
& name
,
2831 const std::string
& asm_name
,
2834 tree type_tree
= btype
->get_tree();
2835 if (type_tree
== error_mark_node
)
2836 return this->error_variable();
2838 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2839 get_identifier_from_string(name
), type_tree
);
2840 DECL_EXTERNAL(decl
) = 1;
2841 TREE_PUBLIC(decl
) = 1;
2842 TREE_STATIC(decl
) = 0;
2843 DECL_ARTIFICIAL(decl
) = 1;
2844 if (! asm_name
.empty())
2845 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2846 go_preserve_from_gc(decl
);
2847 return new Bvariable(decl
);
2850 // Create a named immutable initialized data structure.
2853 Gcc_backend::immutable_struct(const std::string
& name
,
2854 const std::string
& asm_name
,
2856 bool is_common
, Btype
* btype
, Location location
)
2858 tree type_tree
= btype
->get_tree();
2859 if (type_tree
== error_mark_node
)
2860 return this->error_variable();
2861 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2862 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2863 get_identifier_from_string(name
),
2864 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2865 TREE_STATIC(decl
) = 1;
2866 TREE_USED(decl
) = 1;
2867 TREE_READONLY(decl
) = 1;
2868 TREE_CONSTANT(decl
) = 1;
2869 DECL_ARTIFICIAL(decl
) = 1;
2871 TREE_PUBLIC(decl
) = 1;
2872 if (! asm_name
.empty())
2873 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2875 // When the initializer for one immutable_struct refers to another,
2876 // it needs to know the visibility of the referenced struct so that
2877 // compute_reloc_for_constant will return the right value. On many
2878 // systems calling make_decl_one_only will mark the decl as weak,
2879 // which will change the return value of compute_reloc_for_constant.
2880 // We can't reliably call make_decl_one_only yet, because we don't
2881 // yet know the initializer. This issue doesn't arise in C because
2882 // Go initializers, unlike C initializers, can be indirectly
2883 // recursive. To ensure that compute_reloc_for_constant computes
2884 // the right value if some other initializer refers to this one, we
2885 // mark this symbol as weak here. We undo that below in
2886 // immutable_struct_set_init before calling mark_decl_one_only.
2888 DECL_WEAK(decl
) = 1;
2890 // We don't call rest_of_decl_compilation until we have the
2893 go_preserve_from_gc(decl
);
2894 return new Bvariable(decl
);
2897 // Set the initializer for a variable created by immutable_struct.
2898 // This is where we finish compiling the variable.
2901 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
2902 bool, bool is_common
, Btype
*, Location
,
2903 Bexpression
* initializer
)
2905 tree decl
= var
->get_decl();
2906 tree init_tree
= initializer
->get_tree();
2907 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
2910 DECL_INITIAL(decl
) = init_tree
;
2912 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
2913 // See the comment where DECL_WEAK is set in immutable_struct.
2916 DECL_WEAK(decl
) = 0;
2917 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2920 // These variables are often unneeded in the final program, so put
2921 // them in their own section so that linker GC can discard them.
2922 resolve_unique_section(decl
,
2923 compute_reloc_for_constant (init_tree
),
2926 rest_of_decl_compilation(decl
, 1, 0);
2929 // Return a reference to an immutable initialized data structure
2930 // defined in another package.
2933 Gcc_backend::immutable_struct_reference(const std::string
& name
,
2934 const std::string
& asm_name
,
2938 tree type_tree
= btype
->get_tree();
2939 if (type_tree
== error_mark_node
)
2940 return this->error_variable();
2941 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
2942 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2943 get_identifier_from_string(name
),
2944 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
2945 TREE_READONLY(decl
) = 1;
2946 TREE_CONSTANT(decl
) = 1;
2947 DECL_ARTIFICIAL(decl
) = 1;
2948 TREE_PUBLIC(decl
) = 1;
2949 DECL_EXTERNAL(decl
) = 1;
2950 if (! asm_name
.empty())
2951 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2952 go_preserve_from_gc(decl
);
2953 return new Bvariable(decl
);
2959 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
2965 tree func_tree
= function
->get_tree();
2966 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
2967 push_struct_function(func_tree
);
2969 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
2971 decl
= create_artificial_label(location
.gcc_location());
2977 tree id
= get_identifier_from_string(name
);
2978 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
2980 DECL_CONTEXT(decl
) = function
->get_tree();
2982 return new Blabel(decl
);
2985 // Make a statement which defines a label.
2988 Gcc_backend::label_definition_statement(Blabel
* label
)
2990 tree lab
= label
->get_tree();
2991 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
2992 void_type_node
, lab
);
2993 return this->make_statement(ret
);
2996 // Make a goto statement.
2999 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3001 tree lab
= label
->get_tree();
3002 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3004 return this->make_statement(ret
);
3007 // Get the address of a label.
3010 Gcc_backend::label_address(Blabel
* label
, Location location
)
3012 tree lab
= label
->get_tree();
3014 TREE_ADDRESSABLE(lab
) = 1;
3015 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3016 build_fold_addr_expr_loc(location
.gcc_location(),
3018 return this->make_expression(ret
);
3021 // Declare or define a new function.
3024 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3025 const std::string
& asm_name
, bool is_visible
,
3026 bool is_declaration
, bool is_inlinable
,
3027 bool disable_split_stack
, bool does_not_return
,
3028 bool in_unique_section
, Location location
)
3030 tree functype
= fntype
->get_tree();
3031 if (functype
!= error_mark_node
)
3033 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3034 functype
= TREE_TYPE(functype
);
3036 tree id
= get_identifier_from_string(name
);
3037 if (functype
== error_mark_node
|| id
== error_mark_node
)
3038 return this->error_function();
3040 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3041 if (! asm_name
.empty())
3042 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3044 TREE_PUBLIC(decl
) = 1;
3046 DECL_EXTERNAL(decl
) = 1;
3049 tree restype
= TREE_TYPE(functype
);
3051 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3052 DECL_ARTIFICIAL(resdecl
) = 1;
3053 DECL_IGNORED_P(resdecl
) = 1;
3054 DECL_CONTEXT(resdecl
) = decl
;
3055 DECL_RESULT(decl
) = resdecl
;
3058 DECL_UNINLINABLE(decl
) = 1;
3059 if (disable_split_stack
)
3061 tree attr
= get_identifier ("no_split_stack");
3062 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3064 if (does_not_return
)
3065 TREE_THIS_VOLATILE(decl
) = 1;
3066 if (in_unique_section
)
3067 resolve_unique_section(decl
, 0, 1);
3069 go_preserve_from_gc(decl
);
3070 return new Bfunction(decl
);
3073 // Create a statement that runs all deferred calls for FUNCTION. This should
3074 // be a statement that looks like this in C++:
3076 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3079 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3080 Bexpression
* defer
, Location location
)
3082 tree undefer_tree
= undefer
->get_tree();
3083 tree defer_tree
= defer
->get_tree();
3084 tree fntree
= function
->get_tree();
3086 if (undefer_tree
== error_mark_node
3087 || defer_tree
== error_mark_node
3088 || fntree
== error_mark_node
)
3089 return this->error_statement();
3091 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3092 push_struct_function(fntree
);
3094 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3096 tree stmt_list
= NULL
;
3097 Blabel
* blabel
= this->label(function
, "", location
);
3098 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3099 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3101 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3102 tree jump
= jump_stmt
->get_tree();
3103 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3104 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3106 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3107 append_to_statement_list(try_catch
, &stmt_list
);
3110 return this->make_statement(stmt_list
);
3113 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3114 // This will only be called for a function definition.
3117 Gcc_backend::function_set_parameters(Bfunction
* function
,
3118 const std::vector
<Bvariable
*>& param_vars
)
3120 tree func_tree
= function
->get_tree();
3121 if (func_tree
== error_mark_node
)
3124 tree params
= NULL_TREE
;
3126 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3127 pv
!= param_vars
.end();
3130 *pp
= (*pv
)->get_decl();
3131 gcc_assert(*pp
!= error_mark_node
);
3132 pp
= &DECL_CHAIN(*pp
);
3135 DECL_ARGUMENTS(func_tree
) = params
;
3139 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3142 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3144 tree func_tree
= function
->get_tree();
3145 tree code
= code_stmt
->get_tree();
3147 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3149 DECL_SAVED_TREE(func_tree
) = code
;
3153 // Look up a named built-in function in the current backend implementation.
3154 // Returns NULL if no built-in function by that name exists.
3157 Gcc_backend::lookup_builtin(const std::string
& name
)
3159 if (this->builtin_functions_
.count(name
) != 0)
3160 return this->builtin_functions_
[name
];
3164 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3165 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3166 // emit early debugging information.
3169 Gcc_backend::write_global_definitions(
3170 const std::vector
<Btype
*>& type_decls
,
3171 const std::vector
<Bexpression
*>& constant_decls
,
3172 const std::vector
<Bfunction
*>& function_decls
,
3173 const std::vector
<Bvariable
*>& variable_decls
)
3175 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3176 + function_decls
.size() + variable_decls
.size();
3178 tree
* defs
= new tree
[count_definitions
];
3180 // Convert all non-erroneous declarations into Gimple form.
3182 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3183 p
!= variable_decls
.end();
3186 tree v
= (*p
)->get_decl();
3187 if (v
!= error_mark_node
)
3190 go_preserve_from_gc(defs
[i
]);
3195 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3196 p
!= type_decls
.end();
3199 tree type_tree
= (*p
)->get_tree();
3200 if (type_tree
!= error_mark_node
3201 && IS_TYPE_OR_DECL_P(type_tree
))
3203 defs
[i
] = TYPE_NAME(type_tree
);
3204 gcc_assert(defs
[i
] != NULL
);
3205 go_preserve_from_gc(defs
[i
]);
3209 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3210 p
!= constant_decls
.end();
3213 if ((*p
)->get_tree() != error_mark_node
)
3215 defs
[i
] = (*p
)->get_tree();
3216 go_preserve_from_gc(defs
[i
]);
3220 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3221 p
!= function_decls
.end();
3224 tree decl
= (*p
)->get_tree();
3225 if (decl
!= error_mark_node
)
3227 go_preserve_from_gc(decl
);
3228 gimplify_function_tree(decl
);
3229 cgraph_node::finalize_function(decl
, true);
3236 // Pass everything back to the middle-end.
3238 wrapup_global_declarations(defs
, i
);
3244 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3246 go_write_export_data(bytes
, size
);
3250 // Define a builtin function. BCODE is the builtin function code
3251 // defined by builtins.def. NAME is the name of the builtin function.
3252 // LIBNAME is the name of the corresponding library function, and is
3253 // NULL if there isn't one. FNTYPE is the type of the function.
3254 // CONST_P is true if the function has the const attribute.
3255 // NORETURN_P is true if the function has the noreturn attribute.
3258 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3259 const char* libname
, tree fntype
, bool const_p
,
3262 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3263 libname
, NULL_TREE
);
3265 TREE_READONLY(decl
) = 1;
3267 TREE_THIS_VOLATILE(decl
) = 1;
3268 set_builtin_decl(bcode
, decl
, true);
3269 this->builtin_functions_
[name
] = this->make_function(decl
);
3270 if (libname
!= NULL
)
3272 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3275 TREE_READONLY(decl
) = 1;
3277 TREE_THIS_VOLATILE(decl
) = 1;
3278 this->builtin_functions_
[libname
] = this->make_function(decl
);
3282 // Return the backend generator.
3287 return new Gcc_backend();