1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2023 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.
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
33 #include "tree-iterator.h"
38 #include "gimple-expr.h"
40 #include "langhooks.h"
52 // A class wrapping a tree.
73 // In gcc, types, expressions, and statements are all trees.
74 class Btype
: public Gcc_tree
82 class Bexpression
: public Gcc_tree
90 class Bstatement
: public Gcc_tree
98 class Bfunction
: public Gcc_tree
106 class Bblock
: public Gcc_tree
114 class Blabel
: public Gcc_tree
122 // Bvariable is a bit more complicated, because of zero-sized types.
123 // The GNU linker does not permit dynamic variables with zero size.
124 // When we see such a variable, we generate a version of the type with
125 // non-zero size. However, when referring to the global variable, we
126 // want an expression of zero size; otherwise, if, say, the global
127 // variable is passed to a function, we will be passing a
128 // non-zero-sized value to a zero-sized value, which can lead to a
135 : t_(t
), orig_type_(NULL
)
138 Bvariable(tree t
, tree orig_type
)
139 : t_(t
), orig_type_(orig_type
)
142 // Get the tree for use as an expression.
144 get_tree(Location
) const;
146 // Get the actual decl;
156 // Get the tree of a variable for use as an expression. If this is a
157 // zero-sized global, create an expression that refers to the decl but
160 Bvariable::get_tree(Location location
) const
162 if (this->orig_type_
== NULL
163 || this->t_
== error_mark_node
164 || TREE_TYPE(this->t_
) == this->orig_type_
)
166 // Return *(orig_type*)&decl. */
167 tree t
= build_fold_addr_expr_loc(location
.gcc_location(), this->t_
);
168 t
= fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
169 build_pointer_type(this->orig_type_
), t
);
170 return build_fold_indirect_ref_loc(location
.gcc_location(), t
);
173 // This file implements the interface between the Go frontend proper
174 // and the gcc IR. This implements specific instantiations of
175 // abstract classes defined by the Go frontend proper. The Go
176 // frontend proper class methods of these classes to generate the
177 // backend representation.
179 class Gcc_backend
: public Backend
188 { return this->make_type(error_mark_node
); }
192 { return this->make_type(void_type_node
); }
196 { return this->make_type(boolean_type_node
); }
199 integer_type(bool, int);
208 pointer_type(Btype
*);
211 function_type(const Btyped_identifier
&,
212 const std::vector
<Btyped_identifier
>&,
213 const std::vector
<Btyped_identifier
>&,
218 struct_type(const std::vector
<Btyped_identifier
>&);
221 array_type(Btype
*, Bexpression
*);
224 placeholder_pointer_type(const std::string
&, Location
, bool);
227 set_placeholder_pointer_type(Btype
*, Btype
*);
230 set_placeholder_function_type(Btype
*, Btype
*);
233 placeholder_struct_type(const std::string
&, Location
);
236 set_placeholder_struct_type(Btype
* placeholder
,
237 const std::vector
<Btyped_identifier
>&);
240 placeholder_array_type(const std::string
&, Location
);
243 set_placeholder_array_type(Btype
*, Btype
*, Bexpression
*);
246 named_type(const std::string
&, Btype
*, Location
);
249 circular_pointer_type(Btype
*, bool);
252 is_circular_pointer_type(Btype
*);
258 type_alignment(Btype
*);
261 type_field_alignment(Btype
*);
264 type_field_offset(Btype
*, size_t index
);
269 zero_expression(Btype
*);
273 { return this->make_expression(error_mark_node
); }
276 nil_pointer_expression()
277 { return this->make_expression(null_pointer_node
); }
280 var_expression(Bvariable
* var
, Location
);
283 indirect_expression(Btype
*, Bexpression
* expr
, bool known_valid
, Location
);
286 named_constant_expression(Btype
* btype
, const std::string
& name
,
287 Bexpression
* val
, Location
);
290 integer_constant_expression(Btype
* btype
, mpz_t val
);
293 float_constant_expression(Btype
* btype
, mpfr_t val
);
296 complex_constant_expression(Btype
* btype
, mpc_t val
);
299 string_constant_expression(const std::string
& val
);
302 boolean_constant_expression(bool val
);
305 real_part_expression(Bexpression
* bcomplex
, Location
);
308 imag_part_expression(Bexpression
* bcomplex
, Location
);
311 complex_expression(Bexpression
* breal
, Bexpression
* bimag
, Location
);
314 convert_expression(Btype
* type
, Bexpression
* expr
, Location
);
317 function_code_expression(Bfunction
*, Location
);
320 address_expression(Bexpression
*, Location
);
323 struct_field_expression(Bexpression
*, size_t, Location
);
326 compound_expression(Bstatement
*, Bexpression
*, Location
);
329 conditional_expression(Bfunction
*, Btype
*, Bexpression
*, Bexpression
*,
330 Bexpression
*, Location
);
333 unary_expression(Operator
, Bexpression
*, Location
);
336 binary_expression(Operator
, Bexpression
*, Bexpression
*, Location
);
339 constructor_expression(Btype
*, const std::vector
<Bexpression
*>&, Location
);
342 array_constructor_expression(Btype
*, const std::vector
<unsigned long>&,
343 const std::vector
<Bexpression
*>&, Location
);
346 pointer_offset_expression(Bexpression
* base
, Bexpression
* offset
, Location
);
349 array_index_expression(Bexpression
* array
, Bexpression
* index
, Location
);
352 call_expression(Bfunction
* caller
, Bexpression
* fn
,
353 const std::vector
<Bexpression
*>& args
,
354 Bexpression
* static_chain
, Location
);
360 { return this->make_statement(error_mark_node
); }
363 expression_statement(Bfunction
*, Bexpression
*);
366 init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
);
369 assignment_statement(Bfunction
*, Bexpression
* lhs
, Bexpression
* rhs
,
373 return_statement(Bfunction
*, const std::vector
<Bexpression
*>&,
377 if_statement(Bfunction
*, Bexpression
* condition
, Bblock
* then_block
,
378 Bblock
* else_block
, Location
);
381 switch_statement(Bfunction
* function
, Bexpression
* value
,
382 const std::vector
<std::vector
<Bexpression
*> >& cases
,
383 const std::vector
<Bstatement
*>& statements
,
387 compound_statement(Bstatement
*, Bstatement
*);
390 statement_list(const std::vector
<Bstatement
*>&);
393 exception_handler_statement(Bstatement
* bstat
, Bstatement
* except_stmt
,
394 Bstatement
* finally_stmt
, Location
);
399 block(Bfunction
*, Bblock
*, const std::vector
<Bvariable
*>&,
403 block_add_statements(Bblock
*, const std::vector
<Bstatement
*>&);
406 block_statement(Bblock
*);
412 { return new Bvariable(error_mark_node
); }
415 global_variable(const std::string
& var_name
,
416 const std::string
& asm_name
,
422 global_variable_set_init(Bvariable
*, Bexpression
*);
425 local_variable(Bfunction
*, const std::string
&, Btype
*, Bvariable
*,
426 unsigned int, Location
);
429 parameter_variable(Bfunction
*, const std::string
&, Btype
*, unsigned int,
433 static_chain_variable(Bfunction
*, const std::string
&, Btype
*, unsigned int,
437 temporary_variable(Bfunction
*, Bblock
*, Btype
*, Bexpression
*, unsigned int,
438 Location
, Bstatement
**);
441 implicit_variable(const std::string
&, const std::string
&, Btype
*,
442 unsigned int, int64_t);
445 implicit_variable_set_init(Bvariable
*, const std::string
&, Btype
*,
446 unsigned int, Bexpression
*);
449 implicit_variable_reference(const std::string
&, const std::string
&, Btype
*);
452 immutable_struct(const std::string
&, const std::string
&,
453 unsigned int, Btype
*, Location
);
456 immutable_struct_set_init(Bvariable
*, const std::string
&, unsigned int,
457 Btype
*, Location
, Bexpression
*);
460 immutable_struct_reference(const std::string
&, const std::string
&,
466 label(Bfunction
*, const std::string
& name
, Location
);
469 label_definition_statement(Blabel
*);
472 goto_statement(Blabel
*, Location
);
475 label_address(Blabel
*, Location
);
481 { return this->make_function(error_mark_node
); }
484 function(Btype
* fntype
, const std::string
& name
, const std::string
& asm_name
,
485 unsigned int flags
, Location
);
488 function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
489 Bexpression
* defer
, Location
);
492 function_set_parameters(Bfunction
* function
, const std::vector
<Bvariable
*>&);
495 function_set_body(Bfunction
* function
, Bstatement
* code_stmt
);
498 lookup_builtin(const std::string
&);
501 write_global_definitions(const std::vector
<Btype
*>&,
502 const std::vector
<Bexpression
*>&,
503 const std::vector
<Bfunction
*>&,
504 const std::vector
<Bvariable
*>&);
507 write_export_data(const char* bytes
, unsigned int size
);
511 // Make a Bexpression from a tree.
513 make_expression(tree t
)
514 { return new Bexpression(t
); }
516 // Make a Bstatement from a tree.
518 make_statement(tree t
)
519 { return new Bstatement(t
); }
521 // Make a Btype from a tree.
524 { return new Btype(t
); }
527 make_function(tree t
)
528 { return new Bfunction(t
); }
531 fill_in_struct(Btype
*, const std::vector
<Btyped_identifier
>&);
534 fill_in_array(Btype
*, Btype
*, Bexpression
*);
537 non_zero_size_type(tree
);
540 convert_tree(tree
, tree
, Location
);
543 static const int builtin_const
= 1 << 0;
544 static const int builtin_noreturn
= 1 << 1;
545 static const int builtin_novops
= 1 << 2;
546 static const int builtin_pure
= 1 << 3;
547 static const int builtin_nothrow
= 1 << 4;
550 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
551 tree fntype
, int flags
);
553 // A mapping of the GCC built-ins exposed to GCCGo.
554 std::map
<std::string
, Bfunction
*> builtin_functions_
;
557 // A helper function to create a GCC identifier from a C++ string.
560 get_identifier_from_string(const std::string
& str
)
562 return get_identifier_with_length(str
.data(), str
.length());
565 // Define the built-in functions that are exposed to GCCGo.
567 Gcc_backend::Gcc_backend()
569 /* We need to define the fetch_and_add functions, since we use them
571 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
572 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
573 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
574 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
576 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
577 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
578 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
579 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
581 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
582 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
583 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
584 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
586 t
= this->integer_type(true, BITS_PER_UNIT
* 8)->get_tree();
587 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
588 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
589 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
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
,
606 builtin_pure
| builtin_nothrow
);
608 // We use __builtin_memmove for copying data.
609 this->define_builtin(BUILT_IN_MEMMOVE
, "__builtin_memmove", "memmove",
610 build_function_type_list(void_type_node
,
617 // We use __builtin_memset for zeroing data.
618 this->define_builtin(BUILT_IN_MEMSET
, "__builtin_memset", "memset",
619 build_function_type_list(void_type_node
,
626 // Used by runtime/internal/sys and math/bits.
627 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
628 build_function_type_list(integer_type_node
,
632 this->define_builtin(BUILT_IN_CTZL
, "__builtin_ctzl", "ctzl",
633 build_function_type_list(integer_type_node
,
634 long_unsigned_type_node
,
637 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
638 build_function_type_list(integer_type_node
,
639 long_long_unsigned_type_node
,
642 this->define_builtin(BUILT_IN_CLZ
, "__builtin_clz", "clz",
643 build_function_type_list(integer_type_node
,
647 this->define_builtin(BUILT_IN_CLZL
, "__builtin_clzl", "clzl",
648 build_function_type_list(integer_type_node
,
649 long_unsigned_type_node
,
652 this->define_builtin(BUILT_IN_CLZLL
, "__builtin_clzll", "clzll",
653 build_function_type_list(integer_type_node
,
654 long_long_unsigned_type_node
,
657 this->define_builtin(BUILT_IN_POPCOUNT
, "__builtin_popcount", "popcount",
658 build_function_type_list(integer_type_node
,
662 this->define_builtin(BUILT_IN_POPCOUNTLL
, "__builtin_popcountll", "popcountll",
663 build_function_type_list(integer_type_node
,
664 long_long_unsigned_type_node
,
667 this->define_builtin(BUILT_IN_BSWAP16
, "__builtin_bswap16", "bswap16",
668 build_function_type_list(uint16_type_node
,
672 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
673 build_function_type_list(uint32_type_node
,
677 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
678 build_function_type_list(uint64_type_node
,
683 // We provide some functions for the math library.
684 tree math_function_type
= build_function_type_list(double_type_node
,
687 tree math_function_type_long
=
688 build_function_type_list(long_double_type_node
, long_double_type_node
,
690 tree math_function_type_two
= build_function_type_list(double_type_node
,
694 tree math_function_type_long_two
=
695 build_function_type_list(long_double_type_node
, long_double_type_node
,
696 long_double_type_node
, NULL_TREE
);
697 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
698 math_function_type
, builtin_const
);
699 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
700 math_function_type_long
, builtin_const
);
701 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
702 math_function_type
, builtin_const
);
703 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
704 math_function_type_long
, builtin_const
);
705 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
706 math_function_type
, builtin_const
);
707 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
708 math_function_type_long
, builtin_const
);
709 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
710 math_function_type_two
, builtin_const
);
711 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
712 math_function_type_long_two
, builtin_const
);
713 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
714 math_function_type
, builtin_const
);
715 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
716 math_function_type_long
, builtin_const
);
717 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
718 math_function_type
, builtin_const
);
719 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
720 math_function_type_long
, builtin_const
);
721 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
722 math_function_type
, builtin_const
);
723 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
724 math_function_type_long
, builtin_const
);
725 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
726 math_function_type
, builtin_const
);
727 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
728 math_function_type_long
, builtin_const
);
729 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
730 math_function_type
, builtin_const
);
731 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
732 math_function_type_long
, builtin_const
);
733 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
734 math_function_type
, builtin_const
);
735 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
736 math_function_type_long
, builtin_const
);
737 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
738 math_function_type_two
, builtin_const
);
739 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
740 math_function_type_long_two
, builtin_const
);
741 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
742 build_function_type_list(double_type_node
,
747 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
748 build_function_type_list(long_double_type_node
,
749 long_double_type_node
,
753 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
754 math_function_type
, builtin_const
);
755 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
756 math_function_type_long
, builtin_const
);
757 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
758 math_function_type
, builtin_const
);
759 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
760 math_function_type_long
, builtin_const
);
761 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
762 math_function_type
, builtin_const
);
763 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
764 math_function_type_long
, builtin_const
);
765 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
766 math_function_type
, builtin_const
);
767 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
768 math_function_type_long
, builtin_const
);
769 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
770 math_function_type
, builtin_const
);
771 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
772 math_function_type_long
, builtin_const
);
773 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
774 math_function_type
, builtin_const
);
775 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
776 math_function_type_long
, builtin_const
);
777 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
778 math_function_type
, builtin_const
);
779 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
780 math_function_type_long
, builtin_const
);
781 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
782 math_function_type
, builtin_const
);
783 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
784 math_function_type_long
, builtin_const
);
786 // We use __builtin_return_address in the thunk we build for
787 // functions which call recover, and for runtime.getcallerpc.
788 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
789 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
792 // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
793 t
= build_function_type_list(ptr_type_node
, NULL_TREE
);
794 this->define_builtin(BUILT_IN_DWARF_CFA
, "__builtin_dwarf_cfa",
797 // The runtime calls __builtin_extract_return_addr when recording
798 // the address to which a function returns.
799 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
800 "__builtin_extract_return_addr", NULL
,
801 build_function_type_list(ptr_type_node
,
806 // The compiler uses __builtin_trap for some exception handling
808 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
809 build_function_type(void_type_node
, void_list_node
),
812 // The runtime uses __builtin_prefetch.
813 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
814 build_varargs_function_type_list(void_type_node
,
819 // The compiler uses __builtin_unreachable for cases that cannot
821 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
822 build_function_type(void_type_node
, void_list_node
),
823 builtin_const
| builtin_noreturn
);
825 // We provide some atomic functions.
826 t
= build_function_type_list(uint32_type_node
,
830 this->define_builtin(BUILT_IN_ATOMIC_LOAD_4
, "__atomic_load_4", NULL
,
833 t
= build_function_type_list(uint64_type_node
,
837 this->define_builtin(BUILT_IN_ATOMIC_LOAD_8
, "__atomic_load_8", NULL
,
840 t
= build_function_type_list(void_type_node
,
845 this->define_builtin(BUILT_IN_ATOMIC_STORE_4
, "__atomic_store_4", NULL
,
848 t
= build_function_type_list(void_type_node
,
853 this->define_builtin(BUILT_IN_ATOMIC_STORE_8
, "__atomic_store_8", NULL
,
856 t
= build_function_type_list(uint32_type_node
,
861 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_4
, "__atomic_exchange_4", NULL
,
864 t
= build_function_type_list(uint64_type_node
,
869 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_8
, "__atomic_exchange_8", NULL
,
872 t
= build_function_type_list(boolean_type_node
,
880 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
,
881 "__atomic_compare_exchange_4", NULL
,
884 t
= build_function_type_list(boolean_type_node
,
892 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
,
893 "__atomic_compare_exchange_8", NULL
,
896 t
= build_function_type_list(uint32_type_node
,
901 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_4
, "__atomic_add_fetch_4",
903 this->define_builtin(BUILT_IN_ATOMIC_FETCH_ADD_4
, "__atomic_fetch_add_4",
906 t
= build_function_type_list(uint64_type_node
,
911 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_8
, "__atomic_add_fetch_8",
913 this->define_builtin(BUILT_IN_ATOMIC_FETCH_ADD_8
, "__atomic_fetch_add_8",
916 t
= build_function_type_list(unsigned_char_type_node
,
920 this->define_builtin(BUILT_IN_ATOMIC_LOAD_1
, "__atomic_load_1", NULL
, t
, 0);
922 t
= build_function_type_list(void_type_node
,
924 unsigned_char_type_node
,
927 this->define_builtin(BUILT_IN_ATOMIC_STORE_1
, "__atomic_store_1", NULL
,
930 t
= build_function_type_list(unsigned_char_type_node
,
932 unsigned_char_type_node
,
935 this->define_builtin(BUILT_IN_ATOMIC_AND_FETCH_1
, "__atomic_and_fetch_1", NULL
,
937 this->define_builtin(BUILT_IN_ATOMIC_FETCH_AND_1
, "__atomic_fetch_and_1", NULL
,
940 t
= build_function_type_list(unsigned_char_type_node
,
942 unsigned_char_type_node
,
945 this->define_builtin(BUILT_IN_ATOMIC_OR_FETCH_1
, "__atomic_or_fetch_1", NULL
,
947 this->define_builtin(BUILT_IN_ATOMIC_FETCH_OR_1
, "__atomic_fetch_or_1", NULL
,
951 // Get an unnamed integer type.
954 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
959 if (bits
== INT_TYPE_SIZE
)
960 type
= unsigned_type_node
;
961 else if (bits
== CHAR_TYPE_SIZE
)
962 type
= unsigned_char_type_node
;
963 else if (bits
== SHORT_TYPE_SIZE
)
964 type
= short_unsigned_type_node
;
965 else if (bits
== LONG_TYPE_SIZE
)
966 type
= long_unsigned_type_node
;
967 else if (bits
== LONG_LONG_TYPE_SIZE
)
968 type
= long_long_unsigned_type_node
;
970 type
= make_unsigned_type(bits
);
974 if (bits
== INT_TYPE_SIZE
)
975 type
= integer_type_node
;
976 else if (bits
== CHAR_TYPE_SIZE
)
977 type
= signed_char_type_node
;
978 else if (bits
== SHORT_TYPE_SIZE
)
979 type
= short_integer_type_node
;
980 else if (bits
== LONG_TYPE_SIZE
)
981 type
= long_integer_type_node
;
982 else if (bits
== LONG_LONG_TYPE_SIZE
)
983 type
= long_long_integer_type_node
;
985 type
= make_signed_type(bits
);
987 return this->make_type(type
);
990 // Get an unnamed float type.
993 Gcc_backend::float_type(int bits
)
996 if (bits
== FLOAT_TYPE_SIZE
)
997 type
= float_type_node
;
998 else if (bits
== DOUBLE_TYPE_SIZE
)
999 type
= double_type_node
;
1000 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
1001 type
= long_double_type_node
;
1004 type
= make_node(REAL_TYPE
);
1005 TYPE_PRECISION(type
) = bits
;
1008 return this->make_type(type
);
1011 // Get an unnamed complex type.
1014 Gcc_backend::complex_type(int bits
)
1017 if (bits
== FLOAT_TYPE_SIZE
* 2)
1018 type
= complex_float_type_node
;
1019 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
1020 type
= complex_double_type_node
;
1021 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
1022 type
= complex_long_double_type_node
;
1025 type
= make_node(REAL_TYPE
);
1026 TYPE_PRECISION(type
) = bits
/ 2;
1028 type
= build_complex_type(type
);
1030 return this->make_type(type
);
1033 // Get a pointer type.
1036 Gcc_backend::pointer_type(Btype
* to_type
)
1038 tree to_type_tree
= to_type
->get_tree();
1039 if (to_type_tree
== error_mark_node
)
1040 return this->error_type();
1041 tree type
= build_pointer_type(to_type_tree
);
1042 return this->make_type(type
);
1045 // Make a function type.
1048 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
1049 const std::vector
<Btyped_identifier
>& parameters
,
1050 const std::vector
<Btyped_identifier
>& results
,
1051 Btype
* result_struct
,
1054 tree args
= NULL_TREE
;
1056 if (receiver
.btype
!= NULL
)
1058 tree t
= receiver
.btype
->get_tree();
1059 if (t
== error_mark_node
)
1060 return this->error_type();
1061 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1062 pp
= &TREE_CHAIN(*pp
);
1065 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
1066 p
!= parameters
.end();
1069 tree t
= p
->btype
->get_tree();
1070 if (t
== error_mark_node
)
1071 return this->error_type();
1072 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1073 pp
= &TREE_CHAIN(*pp
);
1076 // Varargs is handled entirely at the Go level. When converted to
1077 // GENERIC functions are not varargs.
1078 *pp
= void_list_node
;
1081 if (results
.empty())
1082 result
= void_type_node
;
1083 else if (results
.size() == 1)
1084 result
= results
.front().btype
->get_tree();
1087 gcc_assert(result_struct
!= NULL
);
1088 result
= result_struct
->get_tree();
1090 if (result
== error_mark_node
)
1091 return this->error_type();
1093 // The libffi library cannot represent a zero-sized object. To
1094 // avoid causing confusion on 32-bit SPARC, we treat a function that
1095 // returns a zero-sized value as returning void. That should do no
1096 // harm since there is no actual value to be returned. See
1097 // https://gcc.gnu.org/PR72814 for details.
1098 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
1099 result
= void_type_node
;
1101 tree fntype
= build_function_type(result
, args
);
1102 if (fntype
== error_mark_node
)
1103 return this->error_type();
1105 return this->make_type(build_pointer_type(fntype
));
1108 // Make a struct type.
1111 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
1113 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
1116 // Fill in the fields of a struct type.
1119 Gcc_backend::fill_in_struct(Btype
* fill
,
1120 const std::vector
<Btyped_identifier
>& fields
)
1122 tree fill_tree
= fill
->get_tree();
1123 tree field_trees
= NULL_TREE
;
1124 tree
* pp
= &field_trees
;
1125 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
1129 tree name_tree
= get_identifier_from_string(p
->name
);
1130 tree type_tree
= p
->btype
->get_tree();
1131 if (type_tree
== error_mark_node
)
1132 return this->error_type();
1133 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
1135 DECL_CONTEXT(field
) = fill_tree
;
1137 pp
= &DECL_CHAIN(field
);
1139 TYPE_FIELDS(fill_tree
) = field_trees
;
1140 layout_type(fill_tree
);
1142 // Because Go permits converting between named struct types and
1143 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
1144 // because we don't try to maintain TYPE_CANONICAL for struct types,
1145 // we need to tell the middle-end to use structural equality.
1146 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1151 // Make an array type.
1154 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
1156 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
1157 element_btype
, length
);
1160 // Fill in an array type.
1163 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
1164 Bexpression
* length
)
1166 tree element_type_tree
= element_type
->get_tree();
1167 tree length_tree
= length
->get_tree();
1168 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
1169 return this->error_type();
1171 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
1173 length_tree
= fold_convert(sizetype
, length_tree
);
1175 // build_index_type takes the maximum index, which is one less than
1177 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1181 tree fill_tree
= fill
->get_tree();
1182 TREE_TYPE(fill_tree
) = element_type_tree
;
1183 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1184 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1185 layout_type(fill_tree
);
1187 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1188 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1189 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1190 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1191 TYPE_CANONICAL(fill_tree
) =
1192 build_array_type(TYPE_CANONICAL(element_type_tree
),
1193 TYPE_CANONICAL(index_type_tree
));
1198 // Create a placeholder for a pointer type.
1201 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1202 Location location
, bool)
1204 tree ret
= build_distinct_type_copy(ptr_type_node
);
1207 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1208 get_identifier_from_string(name
),
1210 TYPE_NAME(ret
) = decl
;
1212 return this->make_type(ret
);
1215 // Set the real target type for a placeholder pointer type.
1218 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1221 tree pt
= placeholder
->get_tree();
1222 if (pt
== error_mark_node
)
1224 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1225 tree tt
= to_type
->get_tree();
1226 if (tt
== error_mark_node
)
1228 placeholder
->set_tree(error_mark_node
);
1231 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1232 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1233 TYPE_CANONICAL(pt
) = TYPE_CANONICAL(tt
);
1234 if (TYPE_NAME(pt
) != NULL_TREE
)
1236 // Build the data structure gcc wants to see for a typedef.
1237 tree copy
= build_variant_type_copy(pt
);
1238 TYPE_NAME(copy
) = NULL_TREE
;
1239 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1244 // Set the real values for a placeholder function type.
1247 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1249 return this->set_placeholder_pointer_type(placeholder
, ft
);
1252 // Create a placeholder for a struct type.
1255 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1258 tree ret
= make_node(RECORD_TYPE
);
1261 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1262 get_identifier_from_string(name
),
1264 TYPE_NAME(ret
) = decl
;
1266 // The struct type that eventually replaces this placeholder will require
1267 // structural equality. The placeholder must too, so that the requirement
1268 // for structural equality propagates to references that are constructed
1269 // before the replacement occurs.
1270 SET_TYPE_STRUCTURAL_EQUALITY(ret
);
1272 return this->make_type(ret
);
1275 // Fill in the fields of a placeholder struct type.
1278 Gcc_backend::set_placeholder_struct_type(
1280 const std::vector
<Btyped_identifier
>& fields
)
1282 tree t
= placeholder
->get_tree();
1283 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1284 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1286 if (TYPE_NAME(t
) != NULL_TREE
)
1288 // Build the data structure gcc wants to see for a typedef.
1289 tree copy
= build_distinct_type_copy(t
);
1290 TYPE_NAME(copy
) = NULL_TREE
;
1291 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1292 TYPE_SIZE(copy
) = NULL_TREE
;
1293 Btype
* bc
= this->make_type(copy
);
1294 this->fill_in_struct(bc
, fields
);
1298 return r
->get_tree() != error_mark_node
;
1301 // Create a placeholder for an array type.
1304 Gcc_backend::placeholder_array_type(const std::string
& name
,
1307 tree ret
= make_node(ARRAY_TYPE
);
1308 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1309 get_identifier_from_string(name
),
1311 TYPE_NAME(ret
) = decl
;
1312 return this->make_type(ret
);
1315 // Fill in the fields of a placeholder array type.
1318 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1319 Btype
* element_btype
,
1320 Bexpression
* length
)
1322 tree t
= placeholder
->get_tree();
1323 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1324 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1326 // Build the data structure gcc wants to see for a typedef.
1327 tree copy
= build_distinct_type_copy(t
);
1328 TYPE_NAME(copy
) = NULL_TREE
;
1329 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1331 return r
->get_tree() != error_mark_node
;
1334 // Return a named version of a type.
1337 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1340 tree type
= btype
->get_tree();
1341 if (type
== error_mark_node
)
1342 return this->error_type();
1344 // The middle-end expects a basic type to have a name. In Go every
1345 // basic type will have a name. The first time we see a basic type,
1346 // give it whatever Go name we have at this point.
1347 if (TYPE_NAME(type
) == NULL_TREE
1348 && location
.gcc_location() == BUILTINS_LOCATION
1349 && (TREE_CODE(type
) == INTEGER_TYPE
1350 || TREE_CODE(type
) == REAL_TYPE
1351 || TREE_CODE(type
) == COMPLEX_TYPE
1352 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1354 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1355 get_identifier_from_string(name
),
1357 TYPE_NAME(type
) = decl
;
1358 return this->make_type(type
);
1361 tree copy
= build_variant_type_copy(type
);
1362 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1363 get_identifier_from_string(name
),
1365 DECL_ORIGINAL_TYPE(decl
) = type
;
1366 TYPE_NAME(copy
) = decl
;
1367 return this->make_type(copy
);
1370 // Return a pointer type used as a marker for a circular type.
1373 Gcc_backend::circular_pointer_type(Btype
*, bool)
1375 return this->make_type(ptr_type_node
);
1378 // Return whether we might be looking at a circular type.
1381 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1383 return btype
->get_tree() == ptr_type_node
;
1386 // Return the size of a type.
1389 Gcc_backend::type_size(Btype
* btype
)
1391 tree t
= btype
->get_tree();
1392 if (t
== error_mark_node
)
1394 if (t
== void_type_node
)
1396 t
= TYPE_SIZE_UNIT(t
);
1397 gcc_assert(tree_fits_uhwi_p (t
));
1398 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1399 int64_t ret
= static_cast<int64_t>(val_wide
);
1400 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1405 // Return the alignment of a type.
1408 Gcc_backend::type_alignment(Btype
* btype
)
1410 tree t
= btype
->get_tree();
1411 if (t
== error_mark_node
)
1413 return TYPE_ALIGN_UNIT(t
);
1416 // Return the alignment of a struct field of type BTYPE.
1419 Gcc_backend::type_field_alignment(Btype
* btype
)
1421 tree t
= btype
->get_tree();
1422 if (t
== error_mark_node
)
1424 return go_field_alignment(t
);
1427 // Return the offset of a field in a struct.
1430 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1432 tree struct_tree
= btype
->get_tree();
1433 if (struct_tree
== error_mark_node
)
1435 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1436 tree field
= TYPE_FIELDS(struct_tree
);
1437 for (; index
> 0; --index
)
1439 field
= DECL_CHAIN(field
);
1440 gcc_assert(field
!= NULL_TREE
);
1442 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1443 int64_t ret
= static_cast<int64_t>(offset_wide
);
1444 gcc_assert(ret
== offset_wide
);
1448 // Return the zero value for a type.
1451 Gcc_backend::zero_expression(Btype
* btype
)
1453 tree t
= btype
->get_tree();
1455 if (t
== error_mark_node
)
1456 ret
= error_mark_node
;
1458 ret
= build_zero_cst(t
);
1459 return this->make_expression(ret
);
1462 // An expression that references a variable.
1465 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1467 tree ret
= var
->get_tree(location
);
1468 if (ret
== error_mark_node
)
1469 return this->error_expression();
1470 return this->make_expression(ret
);
1473 // An expression that indirectly references an expression.
1476 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1477 bool known_valid
, Location location
)
1479 tree expr_tree
= expr
->get_tree();
1480 tree type_tree
= btype
->get_tree();
1481 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1482 return this->error_expression();
1484 // If the type of EXPR is a recursive pointer type, then we
1485 // need to insert a cast before indirecting.
1486 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1487 if (VOID_TYPE_P(target_type_tree
))
1488 expr_tree
= fold_convert_loc(location
.gcc_location(),
1489 build_pointer_type(type_tree
), expr_tree
);
1491 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1494 TREE_THIS_NOTRAP(ret
) = 1;
1495 return this->make_expression(ret
);
1498 // Return an expression that declares a constant named NAME with the
1499 // constant value VAL in BTYPE.
1502 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1503 Bexpression
* val
, Location location
)
1505 tree type_tree
= btype
->get_tree();
1506 tree const_val
= val
->get_tree();
1507 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1508 return this->error_expression();
1510 tree name_tree
= get_identifier_from_string(name
);
1511 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1513 DECL_INITIAL(decl
) = const_val
;
1514 TREE_CONSTANT(decl
) = 1;
1515 TREE_READONLY(decl
) = 1;
1517 go_preserve_from_gc(decl
);
1518 return this->make_expression(decl
);
1521 // Return a typed value as a constant integer.
1524 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1526 tree t
= btype
->get_tree();
1527 if (t
== error_mark_node
)
1528 return this->error_expression();
1530 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1531 return this->make_expression(ret
);
1534 // Return a typed value as a constant floating-point number.
1537 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1539 tree t
= btype
->get_tree();
1541 if (t
== error_mark_node
)
1542 return this->error_expression();
1545 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1547 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1548 ret
= build_real(t
, r2
);
1549 return this->make_expression(ret
);
1552 // Return a typed real and imaginary value as a constant complex number.
1555 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1557 tree t
= btype
->get_tree();
1559 if (t
== error_mark_node
)
1560 return this->error_expression();
1563 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1565 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1568 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1570 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1572 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1573 build_real(TREE_TYPE(t
), r4
));
1574 return this->make_expression(ret
);
1577 // Make a constant string expression.
1580 Gcc_backend::string_constant_expression(const std::string
& val
)
1582 tree index_type
= build_index_type(size_int(val
.length()));
1583 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1585 tree string_type
= build_array_type(const_char_type
, index_type
);
1586 TYPE_STRING_FLAG(string_type
) = 1;
1587 tree string_val
= build_string(val
.length(), val
.data());
1588 TREE_TYPE(string_val
) = string_type
;
1590 return this->make_expression(string_val
);
1593 // Make a constant boolean expression.
1596 Gcc_backend::boolean_constant_expression(bool val
)
1598 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1599 return this->make_expression(bool_cst
);
1602 // Return the real part of a complex expression.
1605 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1607 tree complex_tree
= bcomplex
->get_tree();
1608 if (complex_tree
== error_mark_node
)
1609 return this->error_expression();
1610 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1611 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1612 TREE_TYPE(TREE_TYPE(complex_tree
)),
1614 return this->make_expression(ret
);
1617 // Return the imaginary part of a complex expression.
1620 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1622 tree complex_tree
= bcomplex
->get_tree();
1623 if (complex_tree
== error_mark_node
)
1624 return this->error_expression();
1625 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1626 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1627 TREE_TYPE(TREE_TYPE(complex_tree
)),
1629 return this->make_expression(ret
);
1632 // Make a complex expression given its real and imaginary parts.
1635 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1638 tree real_tree
= breal
->get_tree();
1639 tree imag_tree
= bimag
->get_tree();
1640 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1641 return this->error_expression();
1642 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1643 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1644 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1645 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1646 build_complex_type(TREE_TYPE(real_tree
)),
1647 real_tree
, imag_tree
);
1648 return this->make_expression(ret
);
1651 // An expression that converts an expression to a different type.
1654 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1657 tree type_tree
= type
->get_tree();
1658 tree expr_tree
= expr
->get_tree();
1659 if (type_tree
== error_mark_node
1660 || expr_tree
== error_mark_node
1661 || TREE_TYPE(expr_tree
) == error_mark_node
)
1662 return this->error_expression();
1665 if (this->type_size(type
) == 0
1666 || TREE_TYPE(expr_tree
) == void_type_node
)
1668 // Do not convert zero-sized types.
1671 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1672 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1673 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1674 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1675 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1676 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1677 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1678 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1679 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1680 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1681 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1682 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1683 type_tree
, expr_tree
);
1685 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1687 return this->make_expression(ret
);
1690 // Get the address of a function.
1693 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1695 tree func
= bfunc
->get_tree();
1696 if (func
== error_mark_node
)
1697 return this->error_expression();
1699 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1700 return this->make_expression(ret
);
1703 // Get the address of an expression.
1706 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1708 tree expr
= bexpr
->get_tree();
1709 if (expr
== error_mark_node
)
1710 return this->error_expression();
1712 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1713 return this->make_expression(ret
);
1716 // Return an expression for the field at INDEX in BSTRUCT.
1719 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1722 tree struct_tree
= bstruct
->get_tree();
1723 if (struct_tree
== error_mark_node
1724 || TREE_TYPE(struct_tree
) == error_mark_node
)
1725 return this->error_expression();
1727 // A function call that returns a zero-sized object will have been
1728 // changed to return void. A zero-sized object can have a
1729 // (zero-sized) field, so support that case.
1730 if (TREE_TYPE(struct_tree
) == void_type_node
)
1733 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1734 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1735 if (field
== NULL_TREE
)
1737 // This can happen for a type which refers to itself indirectly
1738 // and then turns out to be erroneous.
1739 return this->error_expression();
1741 for (unsigned int i
= index
; i
> 0; --i
)
1743 field
= DECL_CHAIN(field
);
1744 gcc_assert(field
!= NULL_TREE
);
1746 if (TREE_TYPE(field
) == error_mark_node
)
1747 return this->error_expression();
1748 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1749 TREE_TYPE(field
), struct_tree
, field
,
1751 if (TREE_CONSTANT(struct_tree
))
1752 TREE_CONSTANT(ret
) = 1;
1753 return this->make_expression(ret
);
1756 // Return an expression that executes BSTAT before BEXPR.
1759 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1762 tree stat
= bstat
->get_tree();
1763 tree expr
= bexpr
->get_tree();
1764 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1765 return this->error_expression();
1766 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1767 TREE_TYPE(expr
), stat
, expr
);
1768 return this->make_expression(ret
);
1771 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1772 // ELSE_EXPR otherwise.
1775 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1776 Bexpression
* condition
,
1777 Bexpression
* then_expr
,
1778 Bexpression
* else_expr
, Location location
)
1780 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1781 tree cond_tree
= condition
->get_tree();
1782 tree then_tree
= then_expr
->get_tree();
1783 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1784 if (type_tree
== error_mark_node
1785 || cond_tree
== error_mark_node
1786 || then_tree
== error_mark_node
1787 || else_tree
== error_mark_node
)
1788 return this->error_expression();
1789 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1790 cond_tree
, then_tree
, else_tree
);
1791 return this->make_expression(ret
);
1794 // Return an expression for the unary operation OP EXPR.
1797 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1799 tree expr_tree
= expr
->get_tree();
1800 if (expr_tree
== error_mark_node
1801 || TREE_TYPE(expr_tree
) == error_mark_node
)
1802 return this->error_expression();
1804 tree type_tree
= TREE_TYPE(expr_tree
);
1805 enum tree_code code
;
1808 case OPERATOR_MINUS
:
1810 tree computed_type
= excess_precision_type(type_tree
);
1811 if (computed_type
!= NULL_TREE
)
1813 expr_tree
= convert(computed_type
, expr_tree
);
1814 type_tree
= computed_type
;
1820 code
= TRUTH_NOT_EXPR
;
1823 code
= BIT_NOT_EXPR
;
1830 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1832 return this->make_expression(ret
);
1835 // Convert a gofrontend operator to an equivalent tree_code.
1837 static enum tree_code
1838 operator_to_tree_code(Operator op
, tree type
)
1840 enum tree_code code
;
1846 case OPERATOR_NOTEQ
:
1862 code
= TRUTH_ORIF_EXPR
;
1864 case OPERATOR_ANDAND
:
1865 code
= TRUTH_ANDIF_EXPR
;
1870 case OPERATOR_MINUS
:
1874 code
= BIT_IOR_EXPR
;
1877 code
= BIT_XOR_EXPR
;
1883 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1886 code
= TRUNC_DIV_EXPR
;
1889 code
= TRUNC_MOD_EXPR
;
1891 case OPERATOR_LSHIFT
:
1894 case OPERATOR_RSHIFT
:
1898 code
= BIT_AND_EXPR
;
1900 case OPERATOR_BITCLEAR
:
1901 code
= BIT_AND_EXPR
;
1910 // Return an expression for the binary operation LEFT OP RIGHT.
1913 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1914 Bexpression
* right
, Location location
)
1916 tree left_tree
= left
->get_tree();
1917 tree right_tree
= right
->get_tree();
1918 if (left_tree
== error_mark_node
1919 || right_tree
== error_mark_node
)
1920 return this->error_expression();
1921 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1923 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1924 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1925 tree computed_type
= excess_precision_type(type_tree
);
1926 if (computed_type
!= NULL_TREE
)
1928 left_tree
= convert(computed_type
, left_tree
);
1929 right_tree
= convert(computed_type
, right_tree
);
1930 type_tree
= computed_type
;
1933 // For comparison operators, the resulting type should be boolean.
1937 case OPERATOR_NOTEQ
:
1942 type_tree
= boolean_type_node
;
1948 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1949 left_tree
, right_tree
);
1950 return this->make_expression(ret
);
1953 // Return an expression that constructs BTYPE with VALS.
1956 Gcc_backend::constructor_expression(Btype
* btype
,
1957 const std::vector
<Bexpression
*>& vals
,
1960 tree type_tree
= btype
->get_tree();
1961 if (type_tree
== error_mark_node
)
1962 return this->error_expression();
1964 vec
<constructor_elt
, va_gc
> *init
;
1965 vec_alloc(init
, vals
.size());
1967 tree sink
= NULL_TREE
;
1968 bool is_constant
= true;
1969 tree field
= TYPE_FIELDS(type_tree
);
1970 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1972 ++p
, field
= DECL_CHAIN(field
))
1974 gcc_assert(field
!= NULL_TREE
);
1975 tree val
= (*p
)->get_tree();
1976 if (TREE_TYPE(field
) == error_mark_node
1977 || val
== error_mark_node
1978 || TREE_TYPE(val
) == error_mark_node
)
1979 return this->error_expression();
1981 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1983 // GIMPLE cannot represent indices of zero-sized types so
1984 // trying to construct a map with zero-sized keys might lead
1985 // to errors. Instead, we evaluate each expression that
1986 // would have been added as a map element for its
1987 // side-effects and construct an empty map.
1988 append_to_statement_list(val
, &sink
);
1992 constructor_elt empty
= {NULL
, NULL
};
1993 constructor_elt
* elt
= init
->quick_push(empty
);
1995 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1996 if (!TREE_CONSTANT(elt
->value
))
1997 is_constant
= false;
1999 gcc_assert(field
== NULL_TREE
);
2000 tree ret
= build_constructor(type_tree
, init
);
2002 TREE_CONSTANT(ret
) = 1;
2003 if (sink
!= NULL_TREE
)
2004 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2005 type_tree
, sink
, ret
);
2006 return this->make_expression(ret
);
2010 Gcc_backend::array_constructor_expression(
2011 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
2012 const std::vector
<Bexpression
*>& vals
, Location location
)
2014 tree type_tree
= array_btype
->get_tree();
2015 if (type_tree
== error_mark_node
)
2016 return this->error_expression();
2018 gcc_assert(indexes
.size() == vals
.size());
2020 tree element_type
= TREE_TYPE(type_tree
);
2021 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
2022 vec
<constructor_elt
, va_gc
> *init
;
2023 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
2025 tree sink
= NULL_TREE
;
2026 bool is_constant
= true;
2027 for (size_t i
= 0; i
< vals
.size(); ++i
)
2029 tree index
= size_int(indexes
[i
]);
2030 tree val
= (vals
[i
])->get_tree();
2032 if (index
== error_mark_node
2033 || val
== error_mark_node
)
2034 return this->error_expression();
2036 if (element_size
== 0)
2038 // GIMPLE cannot represent arrays of zero-sized types so trying
2039 // to construct an array of zero-sized values might lead to errors.
2040 // Instead, we evaluate each expression that would have been added as
2041 // an array value for its side-effects and construct an empty array.
2042 append_to_statement_list(val
, &sink
);
2046 if (!TREE_CONSTANT(val
))
2047 is_constant
= false;
2049 constructor_elt empty
= {NULL
, NULL
};
2050 constructor_elt
* elt
= init
->quick_push(empty
);
2055 tree ret
= build_constructor(type_tree
, init
);
2057 TREE_CONSTANT(ret
) = 1;
2058 if (sink
!= NULL_TREE
)
2059 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2060 type_tree
, sink
, ret
);
2061 return this->make_expression(ret
);
2064 // Return an expression for the address of BASE[INDEX].
2067 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
2070 tree base_tree
= base
->get_tree();
2071 tree index_tree
= index
->get_tree();
2072 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
2073 if (base_tree
== error_mark_node
2074 || TREE_TYPE(base_tree
) == error_mark_node
2075 || index_tree
== error_mark_node
2076 || element_type_tree
== error_mark_node
)
2077 return this->error_expression();
2079 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
2080 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
2081 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
2082 index_tree
, element_size
);
2083 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
2084 TREE_TYPE(base_tree
), base_tree
, offset
);
2085 return this->make_expression(ptr
);
2088 // Return an expression representing ARRAY[INDEX]
2091 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
2094 tree array_tree
= array
->get_tree();
2095 tree index_tree
= index
->get_tree();
2096 if (array_tree
== error_mark_node
2097 || TREE_TYPE(array_tree
) == error_mark_node
2098 || index_tree
== error_mark_node
)
2099 return this->error_expression();
2101 // A function call that returns a zero sized object will have been
2102 // changed to return void. If we see void here, assume we are
2103 // dealing with a zero sized type and just evaluate the operands.
2105 if (TREE_TYPE(array_tree
) != void_type_node
)
2106 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
2107 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
2108 index_tree
, NULL_TREE
, NULL_TREE
);
2110 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2111 void_type_node
, array_tree
, index_tree
);
2113 return this->make_expression(ret
);
2116 // Create an expression for a call to FN_EXPR with FN_ARGS.
2118 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
2119 Bexpression
* fn_expr
,
2120 const std::vector
<Bexpression
*>& fn_args
,
2121 Bexpression
* chain_expr
,
2124 tree fn
= fn_expr
->get_tree();
2125 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
2126 return this->error_expression();
2128 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
2129 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
2131 size_t nargs
= fn_args
.size();
2132 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
2133 for (size_t i
= 0; i
< nargs
; ++i
)
2135 args
[i
] = fn_args
.at(i
)->get_tree();
2136 if (args
[i
] == error_mark_node
)
2137 return this->error_expression();
2138 if (TREE_TYPE(args
[i
]) == void_type_node
)
2140 // This can happen for a case like f(g()) where g returns a
2141 // zero-sized type, because in that case we've changed g to
2143 tree t
= TYPE_ARG_TYPES(TREE_TYPE(TREE_TYPE(fn
)));
2144 for (size_t j
= 0; j
< i
; ++j
)
2146 tree arg_type
= TREE_TYPE(TREE_VALUE(t
));
2147 args
[i
] = fold_build2_loc(EXPR_LOCATION(args
[i
]), COMPOUND_EXPR
,
2149 build_zero_cst(arg_type
));
2154 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
2155 fndecl
= TREE_OPERAND(fndecl
, 0);
2157 // This is to support builtin math functions when using 80387 math.
2158 tree excess_type
= NULL_TREE
;
2160 && TREE_CODE(fndecl
) == FUNCTION_DECL
2161 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
2162 && DECL_IS_UNDECLARED_BUILTIN (fndecl
)
2164 && ((SCALAR_FLOAT_TYPE_P(rettype
)
2165 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
2166 || (COMPLEX_FLOAT_TYPE_P(rettype
)
2167 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
2169 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
2170 if (excess_type
!= NULL_TREE
)
2172 tree excess_fndecl
= mathfn_built_in(excess_type
,
2173 DECL_FUNCTION_CODE(fndecl
));
2174 if (excess_fndecl
== NULL_TREE
)
2175 excess_type
= NULL_TREE
;
2178 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
2180 for (size_t i
= 0; i
< nargs
; ++i
)
2182 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
2183 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
2184 args
[i
] = ::convert(excess_type
, args
[i
]);
2191 build_call_array_loc(location
.gcc_location(),
2192 excess_type
!= NULL_TREE
? excess_type
: rettype
,
2196 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
2198 if (excess_type
!= NULL_TREE
)
2200 // Calling convert here can undo our excess precision change.
2201 // That may or may not be a bug in convert_to_real.
2202 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
2206 return this->make_expression(ret
);
2209 // An expression as a statement.
2212 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2214 return this->make_statement(expr
->get_tree());
2217 // Variable initialization.
2220 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2222 tree var_tree
= var
->get_decl();
2223 tree init_tree
= init
->get_tree();
2224 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2225 return this->error_statement();
2226 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2228 // To avoid problems with GNU ld, we don't make zero-sized
2229 // externally visible variables. That might lead us to doing an
2230 // initialization of a zero-sized expression to a non-zero sized
2231 // variable, or vice-versa. Avoid crashes by omitting the
2232 // initializer. Such initializations don't mean anything anyhow.
2233 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2234 && init_tree
!= NULL_TREE
2235 && TREE_TYPE(init_tree
) != void_type_node
2236 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2238 DECL_INITIAL(var_tree
) = init_tree
;
2239 init_tree
= NULL_TREE
;
2242 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2243 void_type_node
, var_tree
);
2244 if (init_tree
!= NULL_TREE
)
2245 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2246 void_type_node
, init_tree
, ret
);
2248 return this->make_statement(ret
);
2254 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2255 Bexpression
* rhs
, Location location
)
2257 tree lhs_tree
= lhs
->get_tree();
2258 tree rhs_tree
= rhs
->get_tree();
2259 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2260 return this->error_statement();
2262 // To avoid problems with GNU ld, we don't make zero-sized
2263 // externally visible variables. That might lead us to doing an
2264 // assignment of a zero-sized expression to a non-zero sized
2265 // expression; avoid crashes here by avoiding assignments of
2266 // zero-sized expressions. Such assignments don't really mean
2268 if (TREE_TYPE(lhs_tree
) == void_type_node
2269 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2270 || TREE_TYPE(rhs_tree
) == void_type_node
2271 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2272 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2273 this->expression_statement(bfn
, rhs
));
2275 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2277 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2280 lhs_tree
, rhs_tree
));
2286 Gcc_backend::return_statement(Bfunction
* bfunction
,
2287 const std::vector
<Bexpression
*>& vals
,
2290 tree fntree
= bfunction
->get_tree();
2291 if (fntree
== error_mark_node
)
2292 return this->error_statement();
2293 tree result
= DECL_RESULT(fntree
);
2294 if (result
== error_mark_node
)
2295 return this->error_statement();
2297 // If the result size is zero bytes, we have set the function type
2298 // to have a result type of void, so don't return anything.
2299 // See the function_type method.
2300 tree res_type
= TREE_TYPE(result
);
2301 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2303 tree stmt_list
= NULL_TREE
;
2304 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2308 tree val
= (*p
)->get_tree();
2309 if (val
== error_mark_node
)
2310 return this->error_statement();
2311 append_to_statement_list(val
, &stmt_list
);
2313 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2314 void_type_node
, NULL_TREE
);
2315 append_to_statement_list(ret
, &stmt_list
);
2316 return this->make_statement(stmt_list
);
2321 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2323 else if (vals
.size() == 1)
2325 tree val
= vals
.front()->get_tree();
2326 if (val
== error_mark_node
)
2327 return this->error_statement();
2328 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2329 void_type_node
, result
,
2330 vals
.front()->get_tree());
2331 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2332 void_type_node
, set
);
2336 // To return multiple values, copy the values into a temporary
2337 // variable of the right structure type, and then assign the
2338 // temporary variable to the DECL_RESULT in the return
2340 tree stmt_list
= NULL_TREE
;
2341 tree rettype
= TREE_TYPE(result
);
2343 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2344 push_struct_function(fntree
);
2346 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2347 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2350 tree field
= TYPE_FIELDS(rettype
);
2351 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2353 p
++, field
= DECL_CHAIN(field
))
2355 gcc_assert(field
!= NULL_TREE
);
2356 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2357 TREE_TYPE(field
), rettmp
, field
,
2359 tree val
= (*p
)->get_tree();
2360 if (val
== error_mark_node
)
2361 return this->error_statement();
2362 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2364 ref
, (*p
)->get_tree());
2365 append_to_statement_list(set
, &stmt_list
);
2367 gcc_assert(field
== NULL_TREE
);
2368 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2371 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2372 void_type_node
, set
);
2373 append_to_statement_list(ret_expr
, &stmt_list
);
2376 return this->make_statement(ret
);
2379 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2380 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2381 // NULL, it will always be executed. This is used for handling defers in Go
2382 // functions. In C++, the resulting code is of this form:
2383 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2386 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2387 Bstatement
* except_stmt
,
2388 Bstatement
* finally_stmt
,
2391 tree stat_tree
= bstat
->get_tree();
2392 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2393 tree finally_tree
= finally_stmt
== NULL
2395 : finally_stmt
->get_tree();
2397 if (stat_tree
== error_mark_node
2398 || except_tree
== error_mark_node
2399 || finally_tree
== error_mark_node
)
2400 return this->error_statement();
2402 if (except_tree
!= NULL_TREE
)
2403 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2404 void_type_node
, stat_tree
,
2405 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2406 void_type_node
, NULL
, except_tree
));
2407 if (finally_tree
!= NULL_TREE
)
2408 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2409 void_type_node
, stat_tree
, finally_tree
);
2410 return this->make_statement(stat_tree
);
2416 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2417 Bblock
* then_block
, Bblock
* else_block
,
2420 tree cond_tree
= condition
->get_tree();
2421 tree then_tree
= then_block
->get_tree();
2422 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2423 if (cond_tree
== error_mark_node
2424 || then_tree
== error_mark_node
2425 || else_tree
== error_mark_node
)
2426 return this->error_statement();
2427 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2428 cond_tree
, then_tree
, else_tree
);
2429 return this->make_statement(ret
);
2435 Gcc_backend::switch_statement(
2436 Bfunction
* function
,
2438 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2439 const std::vector
<Bstatement
*>& statements
,
2440 Location switch_location
)
2442 gcc_assert(cases
.size() == statements
.size());
2444 tree decl
= function
->get_tree();
2445 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2446 push_struct_function(decl
);
2448 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2450 tree stmt_list
= NULL_TREE
;
2451 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2452 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2453 ps
!= statements
.end();
2458 location_t loc
= (*ps
!= NULL
2459 ? EXPR_LOCATION((*ps
)->get_tree())
2460 : UNKNOWN_LOCATION
);
2461 tree label
= create_artificial_label(loc
);
2462 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2463 append_to_statement_list(c
, &stmt_list
);
2467 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2471 tree t
= (*pcv
)->get_tree();
2472 if (t
== error_mark_node
)
2473 return this->error_statement();
2474 location_t loc
= EXPR_LOCATION(t
);
2475 tree label
= create_artificial_label(loc
);
2476 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2477 append_to_statement_list(c
, &stmt_list
);
2483 tree t
= (*ps
)->get_tree();
2484 if (t
== error_mark_node
)
2485 return this->error_statement();
2486 append_to_statement_list(t
, &stmt_list
);
2491 tree tv
= value
->get_tree();
2492 if (tv
== error_mark_node
)
2493 return this->error_statement();
2494 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2495 NULL_TREE
, tv
, stmt_list
);
2496 return this->make_statement(t
);
2499 // Pair of statements.
2502 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2504 tree stmt_list
= NULL_TREE
;
2505 tree t
= s1
->get_tree();
2506 if (t
== error_mark_node
)
2507 return this->error_statement();
2508 append_to_statement_list(t
, &stmt_list
);
2510 if (t
== error_mark_node
)
2511 return this->error_statement();
2512 append_to_statement_list(t
, &stmt_list
);
2514 // If neither statement has any side effects, stmt_list can be NULL
2516 if (stmt_list
== NULL_TREE
)
2517 stmt_list
= integer_zero_node
;
2519 return this->make_statement(stmt_list
);
2522 // List of statements.
2525 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2527 tree stmt_list
= NULL_TREE
;
2528 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2529 p
!= statements
.end();
2532 tree t
= (*p
)->get_tree();
2533 if (t
== error_mark_node
)
2534 return this->error_statement();
2535 append_to_statement_list(t
, &stmt_list
);
2537 return this->make_statement(stmt_list
);
2540 // Make a block. For some reason gcc uses a dual structure for
2541 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2542 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2546 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2547 const std::vector
<Bvariable
*>& vars
,
2548 Location start_location
,
2551 tree block_tree
= make_node(BLOCK
);
2552 if (enclosing
== NULL
)
2554 tree fndecl
= function
->get_tree();
2555 gcc_assert(fndecl
!= NULL_TREE
);
2557 // We may have already created a block for local variables when
2558 // we take the address of a parameter.
2559 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2561 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2562 DECL_INITIAL(fndecl
) = block_tree
;
2566 tree superblock_tree
= DECL_INITIAL(fndecl
);
2567 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2569 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2571 pp
= &BLOCK_CHAIN(*pp
))
2578 tree superbind_tree
= enclosing
->get_tree();
2579 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2580 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2582 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2584 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2586 pp
= &BLOCK_CHAIN(*pp
))
2591 tree
* pp
= &BLOCK_VARS(block_tree
);
2592 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2596 *pp
= (*pv
)->get_decl();
2597 if (*pp
!= error_mark_node
)
2598 pp
= &DECL_CHAIN(*pp
);
2602 TREE_USED(block_tree
) = 1;
2604 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2605 void_type_node
, BLOCK_VARS(block_tree
),
2606 NULL_TREE
, block_tree
);
2607 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2608 return new Bblock(bind_tree
);
2611 // Add statements to a block.
2614 Gcc_backend::block_add_statements(Bblock
* bblock
,
2615 const std::vector
<Bstatement
*>& statements
)
2617 tree stmt_list
= NULL_TREE
;
2618 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2619 p
!= statements
.end();
2622 tree s
= (*p
)->get_tree();
2623 if (s
!= error_mark_node
)
2624 append_to_statement_list(s
, &stmt_list
);
2627 tree bind_tree
= bblock
->get_tree();
2628 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2629 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2632 // Return a block as a statement.
2635 Gcc_backend::block_statement(Bblock
* bblock
)
2637 tree bind_tree
= bblock
->get_tree();
2638 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2639 return this->make_statement(bind_tree
);
2642 // This is not static because we declare it with GTY(()) in go-c.h.
2643 tree go_non_zero_struct
;
2645 // Return a type corresponding to TYPE with non-zero size.
2648 Gcc_backend::non_zero_size_type(tree type
)
2650 if (int_size_in_bytes(type
) != 0)
2653 switch (TREE_CODE(type
))
2656 if (TYPE_FIELDS(type
) != NULL_TREE
)
2658 tree ns
= make_node(RECORD_TYPE
);
2659 tree field_trees
= NULL_TREE
;
2660 tree
*pp
= &field_trees
;
2661 for (tree field
= TYPE_FIELDS(type
);
2663 field
= DECL_CHAIN(field
))
2665 tree ft
= TREE_TYPE(field
);
2666 if (field
== TYPE_FIELDS(type
))
2667 ft
= non_zero_size_type(ft
);
2668 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2669 DECL_NAME(field
), ft
);
2670 DECL_CONTEXT(f
) = ns
;
2672 pp
= &DECL_CHAIN(f
);
2674 TYPE_FIELDS(ns
) = field_trees
;
2679 if (go_non_zero_struct
== NULL_TREE
)
2681 type
= make_node(RECORD_TYPE
);
2682 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2683 get_identifier("dummy"),
2685 DECL_CONTEXT(field
) = type
;
2686 TYPE_FIELDS(type
) = field
;
2688 go_non_zero_struct
= type
;
2690 return go_non_zero_struct
;
2694 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2695 return build_array_type_nelts(element_type
, 1);
2705 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2706 // can be created multiple times and thus have multiple tree
2707 // representations. Make sure this does not confuse the middle-end.
2710 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2712 if (type_tree
== TREE_TYPE(expr_tree
))
2715 if (type_tree
== error_mark_node
2716 || expr_tree
== error_mark_node
2717 || TREE_TYPE(expr_tree
) == error_mark_node
)
2718 return error_mark_node
;
2720 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2721 if (POINTER_TYPE_P(type_tree
)
2722 || INTEGRAL_TYPE_P(type_tree
)
2723 || SCALAR_FLOAT_TYPE_P(type_tree
)
2724 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2725 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2726 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2727 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2729 gcc_assert(int_size_in_bytes(type_tree
)
2730 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2731 if (TYPE_MAIN_VARIANT(type_tree
)
2732 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2733 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2734 type_tree
, expr_tree
);
2735 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2736 type_tree
, expr_tree
);
2742 // Make a global variable.
2745 Gcc_backend::global_variable(const std::string
& var_name
,
2746 const std::string
& asm_name
,
2751 tree type_tree
= btype
->get_tree();
2752 if (type_tree
== error_mark_node
)
2753 return this->error_variable();
2755 // The GNU linker does not like dynamic variables with zero size.
2756 tree orig_type_tree
= type_tree
;
2757 bool is_external
= (flags
& variable_is_external
) != 0;
2758 bool is_hidden
= (flags
& variable_is_hidden
) != 0;
2759 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2760 type_tree
= this->non_zero_size_type(type_tree
);
2762 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2763 get_identifier_from_string(var_name
),
2765 if ((flags
& variable_is_external
) != 0)
2767 DECL_EXTERNAL(decl
) = 1;
2768 flags
&=~ variable_is_external
;
2771 TREE_STATIC(decl
) = 1;
2773 if ((flags
& variable_is_hidden
) == 0)
2774 TREE_PUBLIC(decl
) = 1;
2776 flags
&=~ variable_is_hidden
;
2778 if ((flags
& variable_address_is_taken
) != 0)
2780 TREE_ADDRESSABLE(decl
) = 1;
2781 flags
&=~ variable_address_is_taken
;
2784 // We take the address in Bvariable::get_tree if orig_type_tree is
2785 // different from type_tree.
2786 if (orig_type_tree
!= type_tree
)
2787 TREE_ADDRESSABLE(decl
) = 1;
2789 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2791 TREE_USED(decl
) = 1;
2793 if ((flags
& variable_in_unique_section
) != 0)
2795 resolve_unique_section (decl
, 0, 1);
2796 flags
&=~ variable_in_unique_section
;
2799 gcc_assert(flags
== 0);
2801 go_preserve_from_gc(decl
);
2803 return new Bvariable(decl
, orig_type_tree
);
2806 // Set the initial value of a global variable.
2809 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2811 tree expr_tree
= expr
->get_tree();
2812 if (expr_tree
== error_mark_node
)
2814 gcc_assert(TREE_CONSTANT(expr_tree
));
2815 tree var_decl
= var
->get_decl();
2816 if (var_decl
== error_mark_node
)
2818 DECL_INITIAL(var_decl
) = expr_tree
;
2820 // If this variable goes in a unique section, it may need to go into
2821 // a different one now that DECL_INITIAL is set.
2822 if (symtab_node::get(var_decl
)
2823 && symtab_node::get(var_decl
)->implicit_section
)
2825 set_decl_section_name (var_decl
, (const char *) NULL
);
2826 resolve_unique_section (var_decl
,
2827 compute_reloc_for_constant (expr_tree
),
2832 // Make a local variable.
2835 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2836 Btype
* btype
, Bvariable
* decl_var
,
2837 unsigned int flags
, Location location
)
2839 tree type_tree
= btype
->get_tree();
2840 if (type_tree
== error_mark_node
)
2841 return this->error_variable();
2842 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2843 get_identifier_from_string(name
),
2845 DECL_CONTEXT(decl
) = function
->get_tree();
2846 TREE_USED(decl
) = 1;
2847 if ((flags
& variable_address_is_taken
) != 0)
2849 TREE_ADDRESSABLE(decl
) = 1;
2850 flags
&=~ variable_address_is_taken
;
2852 if (decl_var
!= NULL
)
2854 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2855 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2857 go_assert(flags
== 0);
2858 go_preserve_from_gc(decl
);
2859 return new Bvariable(decl
);
2862 // Make a function parameter variable.
2865 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2866 Btype
* btype
, unsigned int flags
,
2869 tree type_tree
= btype
->get_tree();
2870 if (type_tree
== error_mark_node
)
2871 return this->error_variable();
2872 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2873 get_identifier_from_string(name
),
2875 DECL_CONTEXT(decl
) = function
->get_tree();
2876 DECL_ARG_TYPE(decl
) = type_tree
;
2877 TREE_USED(decl
) = 1;
2878 if ((flags
& variable_address_is_taken
) != 0)
2880 TREE_ADDRESSABLE(decl
) = 1;
2881 flags
&=~ variable_address_is_taken
;
2883 go_assert(flags
== 0);
2884 go_preserve_from_gc(decl
);
2885 return new Bvariable(decl
);
2888 // Make a static chain variable.
2891 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2892 Btype
* btype
, unsigned int flags
,
2895 tree type_tree
= btype
->get_tree();
2896 if (type_tree
== error_mark_node
)
2897 return this->error_variable();
2898 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2899 get_identifier_from_string(name
), type_tree
);
2900 tree fndecl
= function
->get_tree();
2901 DECL_CONTEXT(decl
) = fndecl
;
2902 DECL_ARG_TYPE(decl
) = type_tree
;
2903 TREE_USED(decl
) = 1;
2904 DECL_ARTIFICIAL(decl
) = 1;
2905 DECL_IGNORED_P(decl
) = 1;
2906 DECL_NAMELESS(decl
) = 1;
2907 TREE_READONLY(decl
) = 1;
2909 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2912 push_struct_function(fndecl
);
2914 f
= DECL_STRUCT_FUNCTION(fndecl
);
2916 gcc_assert(f
->static_chain_decl
== NULL
);
2917 f
->static_chain_decl
= decl
;
2918 DECL_STATIC_CHAIN(fndecl
) = 1;
2919 go_assert(flags
== 0);
2921 go_preserve_from_gc(decl
);
2922 return new Bvariable(decl
);
2925 // Make a temporary variable.
2928 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2929 Btype
* btype
, Bexpression
* binit
,
2932 Bstatement
** pstatement
)
2934 gcc_assert(function
!= NULL
);
2935 tree decl
= function
->get_tree();
2936 tree type_tree
= btype
->get_tree();
2937 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2938 if (type_tree
== error_mark_node
2939 || init_tree
== error_mark_node
2940 || decl
== error_mark_node
)
2942 *pstatement
= this->error_statement();
2943 return this->error_variable();
2947 // We can only use create_tmp_var if the type is not addressable.
2948 if (!TREE_ADDRESSABLE(type_tree
))
2950 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2951 push_struct_function(decl
);
2953 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2955 var
= create_tmp_var(type_tree
, "GOTMP");
2960 gcc_assert(bblock
!= NULL
);
2961 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2962 create_tmp_var_name("GOTMP"),
2964 DECL_ARTIFICIAL(var
) = 1;
2965 DECL_IGNORED_P(var
) = 1;
2966 DECL_NAMELESS(var
) = 1;
2968 DECL_CONTEXT(var
) = decl
;
2970 // We have to add this variable to the BLOCK and the BIND_EXPR.
2971 tree bind_tree
= bblock
->get_tree();
2972 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2973 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2974 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2975 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2976 BLOCK_VARS(block_tree
) = var
;
2977 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2980 if (this->type_size(btype
) != 0
2981 && init_tree
!= NULL_TREE
2982 && TREE_TYPE(init_tree
) != void_type_node
)
2983 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2985 if ((flags
& variable_address_is_taken
) != 0)
2987 TREE_ADDRESSABLE(var
) = 1;
2988 flags
&=~ variable_address_is_taken
;
2991 gcc_assert(flags
== 0);
2993 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2995 void_type_node
, var
));
2997 // For a zero sized type, don't initialize VAR with BINIT, but still
2998 // evaluate BINIT for its side effects.
2999 if (init_tree
!= NULL_TREE
3000 && (this->type_size(btype
) == 0
3001 || TREE_TYPE(init_tree
) == void_type_node
))
3003 this->compound_statement(this->expression_statement(function
, binit
),
3006 return new Bvariable(var
);
3009 // Create an implicit variable that is compiler-defined. This is used when
3010 // generating GC root variables and storing the values of a slice initializer.
3013 Gcc_backend::implicit_variable(const std::string
& name
,
3014 const std::string
& asm_name
,
3015 Btype
* type
, unsigned int flags
,
3018 tree type_tree
= type
->get_tree();
3019 if (type_tree
== error_mark_node
)
3020 return this->error_variable();
3022 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
3023 get_identifier_from_string(name
), type_tree
);
3024 DECL_EXTERNAL(decl
) = 0;
3025 if ((flags
& variable_is_hidden
) != 0)
3026 flags
&=~ variable_is_hidden
;
3028 TREE_PUBLIC(decl
) = 1;
3029 TREE_STATIC(decl
) = 1;
3030 TREE_USED(decl
) = 1;
3031 DECL_ARTIFICIAL(decl
) = 1;
3032 if ((flags
& variable_is_common
) != 0)
3034 DECL_COMMON(decl
) = 1;
3036 // When the initializer for one implicit_variable refers to another,
3037 // it needs to know the visibility of the referenced struct so that
3038 // compute_reloc_for_constant will return the right value. On many
3039 // systems calling make_decl_one_only will mark the decl as weak,
3040 // which will change the return value of compute_reloc_for_constant.
3041 // We can't reliably call make_decl_one_only yet, because we don't
3042 // yet know the initializer. This issue doesn't arise in C because
3043 // Go initializers, unlike C initializers, can be indirectly
3044 // recursive. To ensure that compute_reloc_for_constant computes
3045 // the right value if some other initializer refers to this one, we
3046 // mark this symbol as weak here. We undo that below in
3047 // immutable_struct_set_init before calling mark_decl_one_only.
3048 DECL_WEAK(decl
) = 1;
3050 flags
&=~ variable_is_common
;
3052 if ((flags
& variable_is_constant
) != 0)
3054 TREE_READONLY(decl
) = 1;
3055 TREE_CONSTANT(decl
) = 1;
3056 flags
&=~ variable_is_constant
;
3058 if ((flags
& variable_address_is_taken
) != 0)
3060 TREE_ADDRESSABLE(decl
) = 1;
3061 flags
&=~ variable_address_is_taken
;
3065 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
3066 DECL_USER_ALIGN(decl
) = 1;
3068 if (! asm_name
.empty())
3069 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3070 gcc_assert(flags
== 0);
3072 go_preserve_from_gc(decl
);
3073 return new Bvariable(decl
);
3076 // Set the initalizer for a variable created by implicit_variable.
3077 // This is where we finish compiling the variable.
3080 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
3081 Btype
*, unsigned int flags
,
3084 tree decl
= var
->get_decl();
3087 init_tree
= NULL_TREE
;
3089 init_tree
= init
->get_tree();
3090 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3093 DECL_INITIAL(decl
) = init_tree
;
3095 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3096 // See the comment where DECL_WEAK is set in implicit_variable.
3097 if ((flags
& variable_is_common
) != 0)
3099 DECL_WEAK(decl
) = 0;
3100 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3103 resolve_unique_section(decl
, 2, 1);
3105 rest_of_decl_compilation(decl
, 1, 0);
3108 // Return a reference to an implicit variable defined in another package.
3111 Gcc_backend::implicit_variable_reference(const std::string
& name
,
3112 const std::string
& asm_name
,
3115 tree type_tree
= btype
->get_tree();
3116 if (type_tree
== error_mark_node
)
3117 return this->error_variable();
3119 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
3120 get_identifier_from_string(name
), type_tree
);
3121 DECL_EXTERNAL(decl
) = 1;
3122 TREE_PUBLIC(decl
) = 1;
3123 TREE_STATIC(decl
) = 0;
3124 DECL_ARTIFICIAL(decl
) = 1;
3125 if (! asm_name
.empty())
3126 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3127 go_preserve_from_gc(decl
);
3128 return new Bvariable(decl
);
3131 // Create a named immutable initialized data structure.
3134 Gcc_backend::immutable_struct(const std::string
& name
,
3135 const std::string
& asm_name
,
3136 unsigned int flags
, Btype
* btype
,
3139 tree type_tree
= btype
->get_tree();
3140 if (type_tree
== error_mark_node
)
3141 return this->error_variable();
3142 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3143 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3144 get_identifier_from_string(name
),
3145 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3146 TREE_STATIC(decl
) = 1;
3147 TREE_USED(decl
) = 1;
3148 TREE_READONLY(decl
) = 1;
3149 TREE_CONSTANT(decl
) = 1;
3150 DECL_ARTIFICIAL(decl
) = 1;
3151 if ((flags
& variable_is_hidden
) != 0)
3152 flags
&=~ variable_is_hidden
;
3154 TREE_PUBLIC(decl
) = 1;
3155 if (! asm_name
.empty())
3156 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3157 if ((flags
& variable_address_is_taken
) != 0)
3159 TREE_ADDRESSABLE(decl
) = 1;
3160 flags
&=~ variable_address_is_taken
;
3163 // When the initializer for one immutable_struct refers to another,
3164 // it needs to know the visibility of the referenced struct so that
3165 // compute_reloc_for_constant will return the right value. On many
3166 // systems calling make_decl_one_only will mark the decl as weak,
3167 // which will change the return value of compute_reloc_for_constant.
3168 // We can't reliably call make_decl_one_only yet, because we don't
3169 // yet know the initializer. This issue doesn't arise in C because
3170 // Go initializers, unlike C initializers, can be indirectly
3171 // recursive. To ensure that compute_reloc_for_constant computes
3172 // the right value if some other initializer refers to this one, we
3173 // mark this symbol as weak here. We undo that below in
3174 // immutable_struct_set_init before calling mark_decl_one_only.
3175 if ((flags
& variable_is_common
) != 0)
3177 DECL_WEAK(decl
) = 1;
3178 flags
&=~ variable_is_common
;
3181 gcc_assert(flags
== 0);
3183 // We don't call rest_of_decl_compilation until we have the
3186 go_preserve_from_gc(decl
);
3187 return new Bvariable(decl
);
3190 // Set the initializer for a variable created by immutable_struct.
3191 // This is where we finish compiling the variable.
3194 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
3195 unsigned int flags
, Btype
*, Location
,
3196 Bexpression
* initializer
)
3198 tree decl
= var
->get_decl();
3199 tree init_tree
= initializer
->get_tree();
3200 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3203 DECL_INITIAL(decl
) = init_tree
;
3205 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3206 // See the comment where DECL_WEAK is set in immutable_struct.
3207 if ((flags
& variable_is_common
) != 0)
3209 DECL_WEAK(decl
) = 0;
3210 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3213 // These variables are often unneeded in the final program, so put
3214 // them in their own section so that linker GC can discard them.
3215 resolve_unique_section(decl
,
3216 compute_reloc_for_constant (init_tree
),
3219 rest_of_decl_compilation(decl
, 1, 0);
3222 // Return a reference to an immutable initialized data structure
3223 // defined in another package.
3226 Gcc_backend::immutable_struct_reference(const std::string
& name
,
3227 const std::string
& asm_name
,
3231 tree type_tree
= btype
->get_tree();
3232 if (type_tree
== error_mark_node
)
3233 return this->error_variable();
3234 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3235 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3236 get_identifier_from_string(name
),
3237 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3238 TREE_READONLY(decl
) = 1;
3239 TREE_CONSTANT(decl
) = 1;
3240 DECL_ARTIFICIAL(decl
) = 1;
3241 TREE_PUBLIC(decl
) = 1;
3242 DECL_EXTERNAL(decl
) = 1;
3243 if (! asm_name
.empty())
3244 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3245 go_preserve_from_gc(decl
);
3246 return new Bvariable(decl
);
3252 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
3258 tree func_tree
= function
->get_tree();
3259 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
3260 push_struct_function(func_tree
);
3262 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
3264 decl
= create_artificial_label(location
.gcc_location());
3270 tree id
= get_identifier_from_string(name
);
3271 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3273 DECL_CONTEXT(decl
) = function
->get_tree();
3275 return new Blabel(decl
);
3278 // Make a statement which defines a label.
3281 Gcc_backend::label_definition_statement(Blabel
* label
)
3283 tree lab
= label
->get_tree();
3284 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3285 void_type_node
, lab
);
3286 return this->make_statement(ret
);
3289 // Make a goto statement.
3292 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3294 tree lab
= label
->get_tree();
3295 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3297 return this->make_statement(ret
);
3300 // Get the address of a label.
3303 Gcc_backend::label_address(Blabel
* label
, Location location
)
3305 tree lab
= label
->get_tree();
3307 TREE_ADDRESSABLE(lab
) = 1;
3308 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3309 build_fold_addr_expr_loc(location
.gcc_location(),
3311 return this->make_expression(ret
);
3314 // Declare or define a new function.
3317 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3318 const std::string
& asm_name
, unsigned int flags
,
3321 tree functype
= fntype
->get_tree();
3322 if (functype
!= error_mark_node
)
3324 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3325 functype
= TREE_TYPE(functype
);
3327 tree id
= get_identifier_from_string(name
);
3328 if (functype
== error_mark_node
|| id
== error_mark_node
)
3329 return this->error_function();
3331 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3332 if (! asm_name
.empty())
3333 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3334 if ((flags
& function_is_visible
) != 0)
3335 TREE_PUBLIC(decl
) = 1;
3336 if ((flags
& function_is_declaration
) != 0)
3337 DECL_EXTERNAL(decl
) = 1;
3340 tree restype
= TREE_TYPE(functype
);
3342 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3343 DECL_ARTIFICIAL(resdecl
) = 1;
3344 DECL_IGNORED_P(resdecl
) = 1;
3345 DECL_NAMELESS(resdecl
) = 1;
3346 DECL_CONTEXT(resdecl
) = decl
;
3347 DECL_RESULT(decl
) = resdecl
;
3349 if ((flags
& function_is_inlinable
) == 0)
3350 DECL_UNINLINABLE(decl
) = 1;
3351 if ((flags
& function_no_split_stack
) != 0)
3353 tree attr
= get_identifier ("no_split_stack");
3354 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3356 if ((flags
& function_does_not_return
) != 0)
3357 TREE_THIS_VOLATILE(decl
) = 1;
3358 if ((flags
& function_in_unique_section
) != 0)
3359 resolve_unique_section(decl
, 0, 1);
3360 if ((flags
& function_only_inline
) != 0)
3362 TREE_PUBLIC (decl
) = 1;
3363 DECL_EXTERNAL(decl
) = 1;
3364 DECL_DECLARED_INLINE_P(decl
) = 1;
3367 // Optimize thunk functions for size. A thunk created for a defer
3368 // statement that may call recover looks like:
3369 // if runtime.setdeferretaddr(L1) {
3374 // The idea is that L1 should be the address to which realfn
3375 // returns. This only works if this little function is not over
3376 // optimized. At some point GCC started duplicating the epilogue in
3377 // the basic-block reordering pass, breaking this assumption.
3378 // Optimizing the function for size avoids duplicating the epilogue.
3379 // This optimization shouldn't matter for any thunk since all thunks
3381 size_t pos
= name
.find("..thunk");
3382 if (pos
!= std::string::npos
)
3384 for (pos
+= 7; pos
< name
.length(); ++pos
)
3386 if (name
[pos
] < '0' || name
[pos
] > '9')
3389 if (pos
== name
.length())
3391 struct cl_optimization cur_opts
;
3392 cl_optimization_save(&cur_opts
, &global_options
,
3393 &global_options_set
);
3394 global_options
.x_optimize_size
= 1;
3395 global_options
.x_optimize_fast
= 0;
3396 global_options
.x_optimize_debug
= 0;
3397 DECL_FUNCTION_SPECIFIC_OPTIMIZATION(decl
) =
3398 build_optimization_node(&global_options
, &global_options_set
);
3399 cl_optimization_restore(&global_options
, &global_options_set
,
3404 go_preserve_from_gc(decl
);
3405 return new Bfunction(decl
);
3408 // Create a statement that runs all deferred calls for FUNCTION. This should
3409 // be a statement that looks like this in C++:
3411 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3414 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3415 Bexpression
* defer
, Location location
)
3417 tree undefer_tree
= undefer
->get_tree();
3418 tree defer_tree
= defer
->get_tree();
3419 tree fntree
= function
->get_tree();
3421 if (undefer_tree
== error_mark_node
3422 || defer_tree
== error_mark_node
3423 || fntree
== error_mark_node
)
3424 return this->error_statement();
3426 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3427 push_struct_function(fntree
);
3429 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3431 tree stmt_list
= NULL
;
3432 Blabel
* blabel
= this->label(function
, "", location
);
3433 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3434 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3436 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3437 tree jump
= jump_stmt
->get_tree();
3438 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3439 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3441 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3442 append_to_statement_list(try_catch
, &stmt_list
);
3445 return this->make_statement(stmt_list
);
3448 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3449 // This will only be called for a function definition.
3452 Gcc_backend::function_set_parameters(Bfunction
* function
,
3453 const std::vector
<Bvariable
*>& param_vars
)
3455 tree func_tree
= function
->get_tree();
3456 if (func_tree
== error_mark_node
)
3459 tree params
= NULL_TREE
;
3461 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3462 pv
!= param_vars
.end();
3465 *pp
= (*pv
)->get_decl();
3466 gcc_assert(*pp
!= error_mark_node
);
3467 pp
= &DECL_CHAIN(*pp
);
3470 DECL_ARGUMENTS(func_tree
) = params
;
3474 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3477 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3479 tree func_tree
= function
->get_tree();
3480 tree code
= code_stmt
->get_tree();
3482 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3484 DECL_SAVED_TREE(func_tree
) = code
;
3488 // Look up a named built-in function in the current backend implementation.
3489 // Returns NULL if no built-in function by that name exists.
3492 Gcc_backend::lookup_builtin(const std::string
& name
)
3494 if (this->builtin_functions_
.count(name
) != 0)
3495 return this->builtin_functions_
[name
];
3499 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3500 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3501 // emit early debugging information.
3504 Gcc_backend::write_global_definitions(
3505 const std::vector
<Btype
*>& type_decls
,
3506 const std::vector
<Bexpression
*>& constant_decls
,
3507 const std::vector
<Bfunction
*>& function_decls
,
3508 const std::vector
<Bvariable
*>& variable_decls
)
3510 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3511 + function_decls
.size() + variable_decls
.size();
3513 tree
* defs
= new tree
[count_definitions
];
3515 // Convert all non-erroneous declarations into Gimple form.
3517 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3518 p
!= variable_decls
.end();
3521 tree v
= (*p
)->get_decl();
3522 if (v
!= error_mark_node
)
3525 go_preserve_from_gc(defs
[i
]);
3530 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3531 p
!= type_decls
.end();
3534 tree type_tree
= (*p
)->get_tree();
3535 if (type_tree
!= error_mark_node
3536 && IS_TYPE_OR_DECL_P(type_tree
))
3538 defs
[i
] = TYPE_NAME(type_tree
);
3539 gcc_assert(defs
[i
] != NULL
);
3540 go_preserve_from_gc(defs
[i
]);
3544 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3545 p
!= constant_decls
.end();
3548 if ((*p
)->get_tree() != error_mark_node
)
3550 defs
[i
] = (*p
)->get_tree();
3551 go_preserve_from_gc(defs
[i
]);
3555 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3556 p
!= function_decls
.end();
3559 tree decl
= (*p
)->get_tree();
3560 if (decl
!= error_mark_node
)
3562 go_preserve_from_gc(decl
);
3563 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
3564 allocate_struct_function(decl
, false);
3565 cgraph_node::finalize_function(decl
, true);
3572 // Pass everything back to the middle-end.
3574 wrapup_global_declarations(defs
, i
);
3580 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3582 go_write_export_data(bytes
, size
);
3586 // Define a builtin function. BCODE is the builtin function code
3587 // defined by builtins.def. NAME is the name of the builtin function.
3588 // LIBNAME is the name of the corresponding library function, and is
3589 // NULL if there isn't one. FNTYPE is the type of the function.
3590 // CONST_P is true if the function has the const attribute.
3591 // NORETURN_P is true if the function has the noreturn attribute.
3594 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3595 const char* libname
, tree fntype
, int flags
)
3597 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3598 libname
, NULL_TREE
);
3599 if ((flags
& builtin_const
) != 0)
3600 TREE_READONLY(decl
) = 1;
3601 if ((flags
& builtin_pure
) != 0)
3602 DECL_PURE_P(decl
) = 1;
3603 if ((flags
& builtin_nothrow
) != 0)
3604 TREE_NOTHROW (decl
) = 1;
3605 if ((flags
& builtin_noreturn
) != 0)
3606 TREE_THIS_VOLATILE(decl
) = 1;
3607 if ((flags
& builtin_novops
) != 0)
3608 DECL_IS_NOVOPS(decl
) = 1;
3609 set_builtin_decl(bcode
, decl
, true);
3610 this->builtin_functions_
[name
] = this->make_function(decl
);
3611 if (libname
!= NULL
)
3613 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3615 if ((flags
& builtin_const
) != 0)
3616 TREE_READONLY(decl
) = 1;
3617 if ((flags
& builtin_pure
) != 0)
3618 DECL_PURE_P(decl
) = 1;
3619 if ((flags
& builtin_nothrow
) != 0)
3620 TREE_NOTHROW (decl
) = 1;
3621 if ((flags
& builtin_noreturn
) != 0)
3622 TREE_THIS_VOLATILE(decl
) = 1;
3623 if ((flags
& builtin_novops
) != 0)
3624 DECL_IS_NOVOPS(decl
) = 1;
3625 this->builtin_functions_
[libname
] = this->make_function(decl
);
3629 // Return the backend generator.
3634 return new Gcc_backend();