1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2022 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;
548 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
549 tree fntype
, int flags
);
551 // A mapping of the GCC built-ins exposed to GCCGo.
552 std::map
<std::string
, Bfunction
*> builtin_functions_
;
555 // A helper function to create a GCC identifier from a C++ string.
558 get_identifier_from_string(const std::string
& str
)
560 return get_identifier_with_length(str
.data(), str
.length());
563 // Define the built-in functions that are exposed to GCCGo.
565 Gcc_backend::Gcc_backend()
567 /* We need to define the fetch_and_add functions, since we use them
569 tree t
= this->integer_type(true, BITS_PER_UNIT
)->get_tree();
570 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
571 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1",
572 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
574 t
= this->integer_type(true, BITS_PER_UNIT
* 2)->get_tree();
575 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
576 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2",
577 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
579 t
= this->integer_type(true, BITS_PER_UNIT
* 4)->get_tree();
580 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
581 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4",
582 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
584 t
= this->integer_type(true, BITS_PER_UNIT
* 8)->get_tree();
585 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
586 this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8",
587 NULL
, build_function_type_list(t
, p
, t
, NULL_TREE
), 0);
589 // We use __builtin_expect for magic import functions.
590 this->define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
591 build_function_type_list(long_integer_type_node
,
592 long_integer_type_node
,
593 long_integer_type_node
,
597 // We use __builtin_memcmp for struct comparisons.
598 this->define_builtin(BUILT_IN_MEMCMP
, "__builtin_memcmp", "memcmp",
599 build_function_type_list(integer_type_node
,
606 // We use __builtin_memmove for copying data.
607 this->define_builtin(BUILT_IN_MEMMOVE
, "__builtin_memmove", "memmove",
608 build_function_type_list(void_type_node
,
615 // We use __builtin_memset for zeroing data.
616 this->define_builtin(BUILT_IN_MEMSET
, "__builtin_memset", "memset",
617 build_function_type_list(void_type_node
,
624 // Used by runtime/internal/sys and math/bits.
625 this->define_builtin(BUILT_IN_CTZ
, "__builtin_ctz", "ctz",
626 build_function_type_list(integer_type_node
,
630 this->define_builtin(BUILT_IN_CTZLL
, "__builtin_ctzll", "ctzll",
631 build_function_type_list(integer_type_node
,
632 long_long_unsigned_type_node
,
635 this->define_builtin(BUILT_IN_CLZ
, "__builtin_clz", "clz",
636 build_function_type_list(integer_type_node
,
640 this->define_builtin(BUILT_IN_CLZLL
, "__builtin_clzll", "clzll",
641 build_function_type_list(integer_type_node
,
642 long_long_unsigned_type_node
,
645 this->define_builtin(BUILT_IN_POPCOUNT
, "__builtin_popcount", "popcount",
646 build_function_type_list(integer_type_node
,
650 this->define_builtin(BUILT_IN_POPCOUNTLL
, "__builtin_popcountll", "popcountll",
651 build_function_type_list(integer_type_node
,
652 long_long_unsigned_type_node
,
655 this->define_builtin(BUILT_IN_BSWAP16
, "__builtin_bswap16", "bswap16",
656 build_function_type_list(uint16_type_node
,
660 this->define_builtin(BUILT_IN_BSWAP32
, "__builtin_bswap32", "bswap32",
661 build_function_type_list(uint32_type_node
,
665 this->define_builtin(BUILT_IN_BSWAP64
, "__builtin_bswap64", "bswap64",
666 build_function_type_list(uint64_type_node
,
671 // We provide some functions for the math library.
672 tree math_function_type
= build_function_type_list(double_type_node
,
675 tree math_function_type_long
=
676 build_function_type_list(long_double_type_node
, long_double_type_node
,
678 tree math_function_type_two
= build_function_type_list(double_type_node
,
682 tree math_function_type_long_two
=
683 build_function_type_list(long_double_type_node
, long_double_type_node
,
684 long_double_type_node
, NULL_TREE
);
685 this->define_builtin(BUILT_IN_ACOS
, "__builtin_acos", "acos",
686 math_function_type
, builtin_const
);
687 this->define_builtin(BUILT_IN_ACOSL
, "__builtin_acosl", "acosl",
688 math_function_type_long
, builtin_const
);
689 this->define_builtin(BUILT_IN_ASIN
, "__builtin_asin", "asin",
690 math_function_type
, builtin_const
);
691 this->define_builtin(BUILT_IN_ASINL
, "__builtin_asinl", "asinl",
692 math_function_type_long
, builtin_const
);
693 this->define_builtin(BUILT_IN_ATAN
, "__builtin_atan", "atan",
694 math_function_type
, builtin_const
);
695 this->define_builtin(BUILT_IN_ATANL
, "__builtin_atanl", "atanl",
696 math_function_type_long
, builtin_const
);
697 this->define_builtin(BUILT_IN_ATAN2
, "__builtin_atan2", "atan2",
698 math_function_type_two
, builtin_const
);
699 this->define_builtin(BUILT_IN_ATAN2L
, "__builtin_atan2l", "atan2l",
700 math_function_type_long_two
, builtin_const
);
701 this->define_builtin(BUILT_IN_CEIL
, "__builtin_ceil", "ceil",
702 math_function_type
, builtin_const
);
703 this->define_builtin(BUILT_IN_CEILL
, "__builtin_ceill", "ceill",
704 math_function_type_long
, builtin_const
);
705 this->define_builtin(BUILT_IN_COS
, "__builtin_cos", "cos",
706 math_function_type
, builtin_const
);
707 this->define_builtin(BUILT_IN_COSL
, "__builtin_cosl", "cosl",
708 math_function_type_long
, builtin_const
);
709 this->define_builtin(BUILT_IN_EXP
, "__builtin_exp", "exp",
710 math_function_type
, builtin_const
);
711 this->define_builtin(BUILT_IN_EXPL
, "__builtin_expl", "expl",
712 math_function_type_long
, builtin_const
);
713 this->define_builtin(BUILT_IN_EXPM1
, "__builtin_expm1", "expm1",
714 math_function_type
, builtin_const
);
715 this->define_builtin(BUILT_IN_EXPM1L
, "__builtin_expm1l", "expm1l",
716 math_function_type_long
, builtin_const
);
717 this->define_builtin(BUILT_IN_FABS
, "__builtin_fabs", "fabs",
718 math_function_type
, builtin_const
);
719 this->define_builtin(BUILT_IN_FABSL
, "__builtin_fabsl", "fabsl",
720 math_function_type_long
, builtin_const
);
721 this->define_builtin(BUILT_IN_FLOOR
, "__builtin_floor", "floor",
722 math_function_type
, builtin_const
);
723 this->define_builtin(BUILT_IN_FLOORL
, "__builtin_floorl", "floorl",
724 math_function_type_long
, builtin_const
);
725 this->define_builtin(BUILT_IN_FMOD
, "__builtin_fmod", "fmod",
726 math_function_type_two
, builtin_const
);
727 this->define_builtin(BUILT_IN_FMODL
, "__builtin_fmodl", "fmodl",
728 math_function_type_long_two
, builtin_const
);
729 this->define_builtin(BUILT_IN_LDEXP
, "__builtin_ldexp", "ldexp",
730 build_function_type_list(double_type_node
,
735 this->define_builtin(BUILT_IN_LDEXPL
, "__builtin_ldexpl", "ldexpl",
736 build_function_type_list(long_double_type_node
,
737 long_double_type_node
,
741 this->define_builtin(BUILT_IN_LOG
, "__builtin_log", "log",
742 math_function_type
, builtin_const
);
743 this->define_builtin(BUILT_IN_LOGL
, "__builtin_logl", "logl",
744 math_function_type_long
, builtin_const
);
745 this->define_builtin(BUILT_IN_LOG1P
, "__builtin_log1p", "log1p",
746 math_function_type
, builtin_const
);
747 this->define_builtin(BUILT_IN_LOG1PL
, "__builtin_log1pl", "log1pl",
748 math_function_type_long
, builtin_const
);
749 this->define_builtin(BUILT_IN_LOG10
, "__builtin_log10", "log10",
750 math_function_type
, builtin_const
);
751 this->define_builtin(BUILT_IN_LOG10L
, "__builtin_log10l", "log10l",
752 math_function_type_long
, builtin_const
);
753 this->define_builtin(BUILT_IN_LOG2
, "__builtin_log2", "log2",
754 math_function_type
, builtin_const
);
755 this->define_builtin(BUILT_IN_LOG2L
, "__builtin_log2l", "log2l",
756 math_function_type_long
, builtin_const
);
757 this->define_builtin(BUILT_IN_SIN
, "__builtin_sin", "sin",
758 math_function_type
, builtin_const
);
759 this->define_builtin(BUILT_IN_SINL
, "__builtin_sinl", "sinl",
760 math_function_type_long
, builtin_const
);
761 this->define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
762 math_function_type
, builtin_const
);
763 this->define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
764 math_function_type_long
, builtin_const
);
765 this->define_builtin(BUILT_IN_TAN
, "__builtin_tan", "tan",
766 math_function_type
, builtin_const
);
767 this->define_builtin(BUILT_IN_TANL
, "__builtin_tanl", "tanl",
768 math_function_type_long
, builtin_const
);
769 this->define_builtin(BUILT_IN_TRUNC
, "__builtin_trunc", "trunc",
770 math_function_type
, builtin_const
);
771 this->define_builtin(BUILT_IN_TRUNCL
, "__builtin_truncl", "truncl",
772 math_function_type_long
, builtin_const
);
774 // We use __builtin_return_address in the thunk we build for
775 // functions which call recover, and for runtime.getcallerpc.
776 t
= build_function_type_list(ptr_type_node
, unsigned_type_node
, NULL_TREE
);
777 this->define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address",
780 // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
781 t
= build_function_type_list(ptr_type_node
, NULL_TREE
);
782 this->define_builtin(BUILT_IN_DWARF_CFA
, "__builtin_dwarf_cfa",
785 // The runtime calls __builtin_extract_return_addr when recording
786 // the address to which a function returns.
787 this->define_builtin(BUILT_IN_EXTRACT_RETURN_ADDR
,
788 "__builtin_extract_return_addr", NULL
,
789 build_function_type_list(ptr_type_node
,
794 // The compiler uses __builtin_trap for some exception handling
796 this->define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
797 build_function_type(void_type_node
, void_list_node
),
800 // The runtime uses __builtin_prefetch.
801 this->define_builtin(BUILT_IN_PREFETCH
, "__builtin_prefetch", NULL
,
802 build_varargs_function_type_list(void_type_node
,
807 // The compiler uses __builtin_unreachable for cases that cannot
809 this->define_builtin(BUILT_IN_UNREACHABLE
, "__builtin_unreachable", NULL
,
810 build_function_type(void_type_node
, void_list_node
),
811 builtin_const
| builtin_noreturn
);
813 // We provide some atomic functions.
814 t
= build_function_type_list(uint32_type_node
,
818 this->define_builtin(BUILT_IN_ATOMIC_LOAD_4
, "__atomic_load_4", NULL
,
821 t
= build_function_type_list(uint64_type_node
,
825 this->define_builtin(BUILT_IN_ATOMIC_LOAD_8
, "__atomic_load_8", NULL
,
828 t
= build_function_type_list(void_type_node
,
833 this->define_builtin(BUILT_IN_ATOMIC_STORE_4
, "__atomic_store_4", NULL
,
836 t
= build_function_type_list(void_type_node
,
841 this->define_builtin(BUILT_IN_ATOMIC_STORE_8
, "__atomic_store_8", NULL
,
844 t
= build_function_type_list(uint32_type_node
,
849 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_4
, "__atomic_exchange_4", NULL
,
852 t
= build_function_type_list(uint64_type_node
,
857 this->define_builtin(BUILT_IN_ATOMIC_EXCHANGE_8
, "__atomic_exchange_8", NULL
,
860 t
= build_function_type_list(boolean_type_node
,
868 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4
,
869 "__atomic_compare_exchange_4", NULL
,
872 t
= build_function_type_list(boolean_type_node
,
880 this->define_builtin(BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8
,
881 "__atomic_compare_exchange_8", NULL
,
884 t
= build_function_type_list(uint32_type_node
,
889 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_4
, "__atomic_add_fetch_4", NULL
,
892 t
= build_function_type_list(uint64_type_node
,
897 this->define_builtin(BUILT_IN_ATOMIC_ADD_FETCH_8
, "__atomic_add_fetch_8", NULL
,
900 t
= build_function_type_list(unsigned_char_type_node
,
904 this->define_builtin(BUILT_IN_ATOMIC_LOAD_1
, "__atomic_load_1", NULL
, t
, 0);
906 t
= build_function_type_list(void_type_node
,
908 unsigned_char_type_node
,
911 this->define_builtin(BUILT_IN_ATOMIC_STORE_1
, "__atomic_store_1", NULL
,
914 t
= build_function_type_list(unsigned_char_type_node
,
916 unsigned_char_type_node
,
919 this->define_builtin(BUILT_IN_ATOMIC_AND_FETCH_1
, "__atomic_and_fetch_1", NULL
,
921 this->define_builtin(BUILT_IN_ATOMIC_FETCH_AND_1
, "__atomic_fetch_and_1", NULL
,
924 t
= build_function_type_list(unsigned_char_type_node
,
926 unsigned_char_type_node
,
929 this->define_builtin(BUILT_IN_ATOMIC_OR_FETCH_1
, "__atomic_or_fetch_1", NULL
,
931 this->define_builtin(BUILT_IN_ATOMIC_FETCH_OR_1
, "__atomic_fetch_or_1", NULL
,
935 // Get an unnamed integer type.
938 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
943 if (bits
== INT_TYPE_SIZE
)
944 type
= unsigned_type_node
;
945 else if (bits
== CHAR_TYPE_SIZE
)
946 type
= unsigned_char_type_node
;
947 else if (bits
== SHORT_TYPE_SIZE
)
948 type
= short_unsigned_type_node
;
949 else if (bits
== LONG_TYPE_SIZE
)
950 type
= long_unsigned_type_node
;
951 else if (bits
== LONG_LONG_TYPE_SIZE
)
952 type
= long_long_unsigned_type_node
;
954 type
= make_unsigned_type(bits
);
958 if (bits
== INT_TYPE_SIZE
)
959 type
= integer_type_node
;
960 else if (bits
== CHAR_TYPE_SIZE
)
961 type
= signed_char_type_node
;
962 else if (bits
== SHORT_TYPE_SIZE
)
963 type
= short_integer_type_node
;
964 else if (bits
== LONG_TYPE_SIZE
)
965 type
= long_integer_type_node
;
966 else if (bits
== LONG_LONG_TYPE_SIZE
)
967 type
= long_long_integer_type_node
;
969 type
= make_signed_type(bits
);
971 return this->make_type(type
);
974 // Get an unnamed float type.
977 Gcc_backend::float_type(int bits
)
980 if (bits
== FLOAT_TYPE_SIZE
)
981 type
= float_type_node
;
982 else if (bits
== DOUBLE_TYPE_SIZE
)
983 type
= double_type_node
;
984 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
985 type
= long_double_type_node
;
988 type
= make_node(REAL_TYPE
);
989 TYPE_PRECISION(type
) = bits
;
992 return this->make_type(type
);
995 // Get an unnamed complex type.
998 Gcc_backend::complex_type(int bits
)
1001 if (bits
== FLOAT_TYPE_SIZE
* 2)
1002 type
= complex_float_type_node
;
1003 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
1004 type
= complex_double_type_node
;
1005 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
1006 type
= complex_long_double_type_node
;
1009 type
= make_node(REAL_TYPE
);
1010 TYPE_PRECISION(type
) = bits
/ 2;
1012 type
= build_complex_type(type
);
1014 return this->make_type(type
);
1017 // Get a pointer type.
1020 Gcc_backend::pointer_type(Btype
* to_type
)
1022 tree to_type_tree
= to_type
->get_tree();
1023 if (to_type_tree
== error_mark_node
)
1024 return this->error_type();
1025 tree type
= build_pointer_type(to_type_tree
);
1026 return this->make_type(type
);
1029 // Make a function type.
1032 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
1033 const std::vector
<Btyped_identifier
>& parameters
,
1034 const std::vector
<Btyped_identifier
>& results
,
1035 Btype
* result_struct
,
1038 tree args
= NULL_TREE
;
1040 if (receiver
.btype
!= NULL
)
1042 tree t
= receiver
.btype
->get_tree();
1043 if (t
== error_mark_node
)
1044 return this->error_type();
1045 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1046 pp
= &TREE_CHAIN(*pp
);
1049 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
1050 p
!= parameters
.end();
1053 tree t
= p
->btype
->get_tree();
1054 if (t
== error_mark_node
)
1055 return this->error_type();
1056 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1057 pp
= &TREE_CHAIN(*pp
);
1060 // Varargs is handled entirely at the Go level. When converted to
1061 // GENERIC functions are not varargs.
1062 *pp
= void_list_node
;
1065 if (results
.empty())
1066 result
= void_type_node
;
1067 else if (results
.size() == 1)
1068 result
= results
.front().btype
->get_tree();
1071 gcc_assert(result_struct
!= NULL
);
1072 result
= result_struct
->get_tree();
1074 if (result
== error_mark_node
)
1075 return this->error_type();
1077 // The libffi library cannot represent a zero-sized object. To
1078 // avoid causing confusion on 32-bit SPARC, we treat a function that
1079 // returns a zero-sized value as returning void. That should do no
1080 // harm since there is no actual value to be returned. See
1081 // https://gcc.gnu.org/PR72814 for details.
1082 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
1083 result
= void_type_node
;
1085 tree fntype
= build_function_type(result
, args
);
1086 if (fntype
== error_mark_node
)
1087 return this->error_type();
1089 return this->make_type(build_pointer_type(fntype
));
1092 // Make a struct type.
1095 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
1097 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
1100 // Fill in the fields of a struct type.
1103 Gcc_backend::fill_in_struct(Btype
* fill
,
1104 const std::vector
<Btyped_identifier
>& fields
)
1106 tree fill_tree
= fill
->get_tree();
1107 tree field_trees
= NULL_TREE
;
1108 tree
* pp
= &field_trees
;
1109 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
1113 tree name_tree
= get_identifier_from_string(p
->name
);
1114 tree type_tree
= p
->btype
->get_tree();
1115 if (type_tree
== error_mark_node
)
1116 return this->error_type();
1117 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
1119 DECL_CONTEXT(field
) = fill_tree
;
1121 pp
= &DECL_CHAIN(field
);
1123 TYPE_FIELDS(fill_tree
) = field_trees
;
1124 layout_type(fill_tree
);
1126 // Because Go permits converting between named struct types and
1127 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
1128 // because we don't try to maintain TYPE_CANONICAL for struct types,
1129 // we need to tell the middle-end to use structural equality.
1130 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1135 // Make an array type.
1138 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
1140 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
1141 element_btype
, length
);
1144 // Fill in an array type.
1147 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
1148 Bexpression
* length
)
1150 tree element_type_tree
= element_type
->get_tree();
1151 tree length_tree
= length
->get_tree();
1152 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
1153 return this->error_type();
1155 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
1157 length_tree
= fold_convert(sizetype
, length_tree
);
1159 // build_index_type takes the maximum index, which is one less than
1161 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1165 tree fill_tree
= fill
->get_tree();
1166 TREE_TYPE(fill_tree
) = element_type_tree
;
1167 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1168 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1169 layout_type(fill_tree
);
1171 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1172 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1173 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1174 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1175 TYPE_CANONICAL(fill_tree
) =
1176 build_array_type(TYPE_CANONICAL(element_type_tree
),
1177 TYPE_CANONICAL(index_type_tree
));
1182 // Create a placeholder for a pointer type.
1185 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1186 Location location
, bool)
1188 tree ret
= build_distinct_type_copy(ptr_type_node
);
1191 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1192 get_identifier_from_string(name
),
1194 TYPE_NAME(ret
) = decl
;
1196 return this->make_type(ret
);
1199 // Set the real target type for a placeholder pointer type.
1202 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1205 tree pt
= placeholder
->get_tree();
1206 if (pt
== error_mark_node
)
1208 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1209 tree tt
= to_type
->get_tree();
1210 if (tt
== error_mark_node
)
1212 placeholder
->set_tree(error_mark_node
);
1215 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1216 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1217 TYPE_CANONICAL(pt
) = TYPE_CANONICAL(tt
);
1218 if (TYPE_NAME(pt
) != NULL_TREE
)
1220 // Build the data structure gcc wants to see for a typedef.
1221 tree copy
= build_variant_type_copy(pt
);
1222 TYPE_NAME(copy
) = NULL_TREE
;
1223 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1228 // Set the real values for a placeholder function type.
1231 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1233 return this->set_placeholder_pointer_type(placeholder
, ft
);
1236 // Create a placeholder for a struct type.
1239 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1242 tree ret
= make_node(RECORD_TYPE
);
1245 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1246 get_identifier_from_string(name
),
1248 TYPE_NAME(ret
) = decl
;
1250 // The struct type that eventually replaces this placeholder will require
1251 // structural equality. The placeholder must too, so that the requirement
1252 // for structural equality propagates to references that are constructed
1253 // before the replacement occurs.
1254 SET_TYPE_STRUCTURAL_EQUALITY(ret
);
1256 return this->make_type(ret
);
1259 // Fill in the fields of a placeholder struct type.
1262 Gcc_backend::set_placeholder_struct_type(
1264 const std::vector
<Btyped_identifier
>& fields
)
1266 tree t
= placeholder
->get_tree();
1267 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1268 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1270 if (TYPE_NAME(t
) != NULL_TREE
)
1272 // Build the data structure gcc wants to see for a typedef.
1273 tree copy
= build_distinct_type_copy(t
);
1274 TYPE_NAME(copy
) = NULL_TREE
;
1275 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1276 TYPE_SIZE(copy
) = NULL_TREE
;
1277 Btype
* bc
= this->make_type(copy
);
1278 this->fill_in_struct(bc
, fields
);
1282 return r
->get_tree() != error_mark_node
;
1285 // Create a placeholder for an array type.
1288 Gcc_backend::placeholder_array_type(const std::string
& name
,
1291 tree ret
= make_node(ARRAY_TYPE
);
1292 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1293 get_identifier_from_string(name
),
1295 TYPE_NAME(ret
) = decl
;
1296 return this->make_type(ret
);
1299 // Fill in the fields of a placeholder array type.
1302 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1303 Btype
* element_btype
,
1304 Bexpression
* length
)
1306 tree t
= placeholder
->get_tree();
1307 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1308 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1310 // Build the data structure gcc wants to see for a typedef.
1311 tree copy
= build_distinct_type_copy(t
);
1312 TYPE_NAME(copy
) = NULL_TREE
;
1313 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1315 return r
->get_tree() != error_mark_node
;
1318 // Return a named version of a type.
1321 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1324 tree type
= btype
->get_tree();
1325 if (type
== error_mark_node
)
1326 return this->error_type();
1328 // The middle-end expects a basic type to have a name. In Go every
1329 // basic type will have a name. The first time we see a basic type,
1330 // give it whatever Go name we have at this point.
1331 if (TYPE_NAME(type
) == NULL_TREE
1332 && location
.gcc_location() == BUILTINS_LOCATION
1333 && (TREE_CODE(type
) == INTEGER_TYPE
1334 || TREE_CODE(type
) == REAL_TYPE
1335 || TREE_CODE(type
) == COMPLEX_TYPE
1336 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1338 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1339 get_identifier_from_string(name
),
1341 TYPE_NAME(type
) = decl
;
1342 return this->make_type(type
);
1345 tree copy
= build_variant_type_copy(type
);
1346 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1347 get_identifier_from_string(name
),
1349 DECL_ORIGINAL_TYPE(decl
) = type
;
1350 TYPE_NAME(copy
) = decl
;
1351 return this->make_type(copy
);
1354 // Return a pointer type used as a marker for a circular type.
1357 Gcc_backend::circular_pointer_type(Btype
*, bool)
1359 return this->make_type(ptr_type_node
);
1362 // Return whether we might be looking at a circular type.
1365 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1367 return btype
->get_tree() == ptr_type_node
;
1370 // Return the size of a type.
1373 Gcc_backend::type_size(Btype
* btype
)
1375 tree t
= btype
->get_tree();
1376 if (t
== error_mark_node
)
1378 if (t
== void_type_node
)
1380 t
= TYPE_SIZE_UNIT(t
);
1381 gcc_assert(tree_fits_uhwi_p (t
));
1382 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1383 int64_t ret
= static_cast<int64_t>(val_wide
);
1384 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1389 // Return the alignment of a type.
1392 Gcc_backend::type_alignment(Btype
* btype
)
1394 tree t
= btype
->get_tree();
1395 if (t
== error_mark_node
)
1397 return TYPE_ALIGN_UNIT(t
);
1400 // Return the alignment of a struct field of type BTYPE.
1403 Gcc_backend::type_field_alignment(Btype
* btype
)
1405 tree t
= btype
->get_tree();
1406 if (t
== error_mark_node
)
1408 return go_field_alignment(t
);
1411 // Return the offset of a field in a struct.
1414 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1416 tree struct_tree
= btype
->get_tree();
1417 if (struct_tree
== error_mark_node
)
1419 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1420 tree field
= TYPE_FIELDS(struct_tree
);
1421 for (; index
> 0; --index
)
1423 field
= DECL_CHAIN(field
);
1424 gcc_assert(field
!= NULL_TREE
);
1426 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1427 int64_t ret
= static_cast<int64_t>(offset_wide
);
1428 gcc_assert(ret
== offset_wide
);
1432 // Return the zero value for a type.
1435 Gcc_backend::zero_expression(Btype
* btype
)
1437 tree t
= btype
->get_tree();
1439 if (t
== error_mark_node
)
1440 ret
= error_mark_node
;
1442 ret
= build_zero_cst(t
);
1443 return this->make_expression(ret
);
1446 // An expression that references a variable.
1449 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1451 tree ret
= var
->get_tree(location
);
1452 if (ret
== error_mark_node
)
1453 return this->error_expression();
1454 return this->make_expression(ret
);
1457 // An expression that indirectly references an expression.
1460 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1461 bool known_valid
, Location location
)
1463 tree expr_tree
= expr
->get_tree();
1464 tree type_tree
= btype
->get_tree();
1465 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1466 return this->error_expression();
1468 // If the type of EXPR is a recursive pointer type, then we
1469 // need to insert a cast before indirecting.
1470 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1471 if (VOID_TYPE_P(target_type_tree
))
1472 expr_tree
= fold_convert_loc(location
.gcc_location(),
1473 build_pointer_type(type_tree
), expr_tree
);
1475 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1478 TREE_THIS_NOTRAP(ret
) = 1;
1479 return this->make_expression(ret
);
1482 // Return an expression that declares a constant named NAME with the
1483 // constant value VAL in BTYPE.
1486 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1487 Bexpression
* val
, Location location
)
1489 tree type_tree
= btype
->get_tree();
1490 tree const_val
= val
->get_tree();
1491 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1492 return this->error_expression();
1494 tree name_tree
= get_identifier_from_string(name
);
1495 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1497 DECL_INITIAL(decl
) = const_val
;
1498 TREE_CONSTANT(decl
) = 1;
1499 TREE_READONLY(decl
) = 1;
1501 go_preserve_from_gc(decl
);
1502 return this->make_expression(decl
);
1505 // Return a typed value as a constant integer.
1508 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1510 tree t
= btype
->get_tree();
1511 if (t
== error_mark_node
)
1512 return this->error_expression();
1514 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1515 return this->make_expression(ret
);
1518 // Return a typed value as a constant floating-point number.
1521 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1523 tree t
= btype
->get_tree();
1525 if (t
== error_mark_node
)
1526 return this->error_expression();
1529 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1531 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1532 ret
= build_real(t
, r2
);
1533 return this->make_expression(ret
);
1536 // Return a typed real and imaginary value as a constant complex number.
1539 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1541 tree t
= btype
->get_tree();
1543 if (t
== error_mark_node
)
1544 return this->error_expression();
1547 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1549 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1552 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1554 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1556 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1557 build_real(TREE_TYPE(t
), r4
));
1558 return this->make_expression(ret
);
1561 // Make a constant string expression.
1564 Gcc_backend::string_constant_expression(const std::string
& val
)
1566 tree index_type
= build_index_type(size_int(val
.length()));
1567 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1569 tree string_type
= build_array_type(const_char_type
, index_type
);
1570 TYPE_STRING_FLAG(string_type
) = 1;
1571 tree string_val
= build_string(val
.length(), val
.data());
1572 TREE_TYPE(string_val
) = string_type
;
1574 return this->make_expression(string_val
);
1577 // Make a constant boolean expression.
1580 Gcc_backend::boolean_constant_expression(bool val
)
1582 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1583 return this->make_expression(bool_cst
);
1586 // Return the real part of a complex expression.
1589 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1591 tree complex_tree
= bcomplex
->get_tree();
1592 if (complex_tree
== error_mark_node
)
1593 return this->error_expression();
1594 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1595 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1596 TREE_TYPE(TREE_TYPE(complex_tree
)),
1598 return this->make_expression(ret
);
1601 // Return the imaginary part of a complex expression.
1604 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1606 tree complex_tree
= bcomplex
->get_tree();
1607 if (complex_tree
== error_mark_node
)
1608 return this->error_expression();
1609 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1610 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1611 TREE_TYPE(TREE_TYPE(complex_tree
)),
1613 return this->make_expression(ret
);
1616 // Make a complex expression given its real and imaginary parts.
1619 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1622 tree real_tree
= breal
->get_tree();
1623 tree imag_tree
= bimag
->get_tree();
1624 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1625 return this->error_expression();
1626 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1627 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1628 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1629 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1630 build_complex_type(TREE_TYPE(real_tree
)),
1631 real_tree
, imag_tree
);
1632 return this->make_expression(ret
);
1635 // An expression that converts an expression to a different type.
1638 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1641 tree type_tree
= type
->get_tree();
1642 tree expr_tree
= expr
->get_tree();
1643 if (type_tree
== error_mark_node
1644 || expr_tree
== error_mark_node
1645 || TREE_TYPE(expr_tree
) == error_mark_node
)
1646 return this->error_expression();
1649 if (this->type_size(type
) == 0
1650 || TREE_TYPE(expr_tree
) == void_type_node
)
1652 // Do not convert zero-sized types.
1655 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1656 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1657 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1658 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1659 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1660 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1661 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1662 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1663 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1664 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1665 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1666 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1667 type_tree
, expr_tree
);
1669 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1671 return this->make_expression(ret
);
1674 // Get the address of a function.
1677 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1679 tree func
= bfunc
->get_tree();
1680 if (func
== error_mark_node
)
1681 return this->error_expression();
1683 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1684 return this->make_expression(ret
);
1687 // Get the address of an expression.
1690 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1692 tree expr
= bexpr
->get_tree();
1693 if (expr
== error_mark_node
)
1694 return this->error_expression();
1696 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1697 return this->make_expression(ret
);
1700 // Return an expression for the field at INDEX in BSTRUCT.
1703 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1706 tree struct_tree
= bstruct
->get_tree();
1707 if (struct_tree
== error_mark_node
1708 || TREE_TYPE(struct_tree
) == error_mark_node
)
1709 return this->error_expression();
1710 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1711 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1712 if (field
== NULL_TREE
)
1714 // This can happen for a type which refers to itself indirectly
1715 // and then turns out to be erroneous.
1716 return this->error_expression();
1718 for (unsigned int i
= index
; i
> 0; --i
)
1720 field
= DECL_CHAIN(field
);
1721 gcc_assert(field
!= NULL_TREE
);
1723 if (TREE_TYPE(field
) == error_mark_node
)
1724 return this->error_expression();
1725 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1726 TREE_TYPE(field
), struct_tree
, field
,
1728 if (TREE_CONSTANT(struct_tree
))
1729 TREE_CONSTANT(ret
) = 1;
1730 return this->make_expression(ret
);
1733 // Return an expression that executes BSTAT before BEXPR.
1736 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1739 tree stat
= bstat
->get_tree();
1740 tree expr
= bexpr
->get_tree();
1741 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1742 return this->error_expression();
1743 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1744 TREE_TYPE(expr
), stat
, expr
);
1745 return this->make_expression(ret
);
1748 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1749 // ELSE_EXPR otherwise.
1752 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1753 Bexpression
* condition
,
1754 Bexpression
* then_expr
,
1755 Bexpression
* else_expr
, Location location
)
1757 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1758 tree cond_tree
= condition
->get_tree();
1759 tree then_tree
= then_expr
->get_tree();
1760 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1761 if (type_tree
== error_mark_node
1762 || cond_tree
== error_mark_node
1763 || then_tree
== error_mark_node
1764 || else_tree
== error_mark_node
)
1765 return this->error_expression();
1766 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1767 cond_tree
, then_tree
, else_tree
);
1768 return this->make_expression(ret
);
1771 // Return an expression for the unary operation OP EXPR.
1774 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1776 tree expr_tree
= expr
->get_tree();
1777 if (expr_tree
== error_mark_node
1778 || TREE_TYPE(expr_tree
) == error_mark_node
)
1779 return this->error_expression();
1781 tree type_tree
= TREE_TYPE(expr_tree
);
1782 enum tree_code code
;
1785 case OPERATOR_MINUS
:
1787 tree computed_type
= excess_precision_type(type_tree
);
1788 if (computed_type
!= NULL_TREE
)
1790 expr_tree
= convert(computed_type
, expr_tree
);
1791 type_tree
= computed_type
;
1797 code
= TRUTH_NOT_EXPR
;
1800 code
= BIT_NOT_EXPR
;
1807 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1809 return this->make_expression(ret
);
1812 // Convert a gofrontend operator to an equivalent tree_code.
1814 static enum tree_code
1815 operator_to_tree_code(Operator op
, tree type
)
1817 enum tree_code code
;
1823 case OPERATOR_NOTEQ
:
1839 code
= TRUTH_ORIF_EXPR
;
1841 case OPERATOR_ANDAND
:
1842 code
= TRUTH_ANDIF_EXPR
;
1847 case OPERATOR_MINUS
:
1851 code
= BIT_IOR_EXPR
;
1854 code
= BIT_XOR_EXPR
;
1860 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1863 code
= TRUNC_DIV_EXPR
;
1866 code
= TRUNC_MOD_EXPR
;
1868 case OPERATOR_LSHIFT
:
1871 case OPERATOR_RSHIFT
:
1875 code
= BIT_AND_EXPR
;
1877 case OPERATOR_BITCLEAR
:
1878 code
= BIT_AND_EXPR
;
1887 // Return an expression for the binary operation LEFT OP RIGHT.
1890 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1891 Bexpression
* right
, Location location
)
1893 tree left_tree
= left
->get_tree();
1894 tree right_tree
= right
->get_tree();
1895 if (left_tree
== error_mark_node
1896 || right_tree
== error_mark_node
)
1897 return this->error_expression();
1898 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1900 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1901 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1902 tree computed_type
= excess_precision_type(type_tree
);
1903 if (computed_type
!= NULL_TREE
)
1905 left_tree
= convert(computed_type
, left_tree
);
1906 right_tree
= convert(computed_type
, right_tree
);
1907 type_tree
= computed_type
;
1910 // For comparison operators, the resulting type should be boolean.
1914 case OPERATOR_NOTEQ
:
1919 type_tree
= boolean_type_node
;
1925 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1926 left_tree
, right_tree
);
1927 return this->make_expression(ret
);
1930 // Return an expression that constructs BTYPE with VALS.
1933 Gcc_backend::constructor_expression(Btype
* btype
,
1934 const std::vector
<Bexpression
*>& vals
,
1937 tree type_tree
= btype
->get_tree();
1938 if (type_tree
== error_mark_node
)
1939 return this->error_expression();
1941 vec
<constructor_elt
, va_gc
> *init
;
1942 vec_alloc(init
, vals
.size());
1944 tree sink
= NULL_TREE
;
1945 bool is_constant
= true;
1946 tree field
= TYPE_FIELDS(type_tree
);
1947 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1949 ++p
, field
= DECL_CHAIN(field
))
1951 gcc_assert(field
!= NULL_TREE
);
1952 tree val
= (*p
)->get_tree();
1953 if (TREE_TYPE(field
) == error_mark_node
1954 || val
== error_mark_node
1955 || TREE_TYPE(val
) == error_mark_node
)
1956 return this->error_expression();
1958 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1960 // GIMPLE cannot represent indices of zero-sized types so
1961 // trying to construct a map with zero-sized keys might lead
1962 // to errors. Instead, we evaluate each expression that
1963 // would have been added as a map element for its
1964 // side-effects and construct an empty map.
1965 append_to_statement_list(val
, &sink
);
1969 constructor_elt empty
= {NULL
, NULL
};
1970 constructor_elt
* elt
= init
->quick_push(empty
);
1972 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1973 if (!TREE_CONSTANT(elt
->value
))
1974 is_constant
= false;
1976 gcc_assert(field
== NULL_TREE
);
1977 tree ret
= build_constructor(type_tree
, init
);
1979 TREE_CONSTANT(ret
) = 1;
1980 if (sink
!= NULL_TREE
)
1981 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1982 type_tree
, sink
, ret
);
1983 return this->make_expression(ret
);
1987 Gcc_backend::array_constructor_expression(
1988 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1989 const std::vector
<Bexpression
*>& vals
, Location location
)
1991 tree type_tree
= array_btype
->get_tree();
1992 if (type_tree
== error_mark_node
)
1993 return this->error_expression();
1995 gcc_assert(indexes
.size() == vals
.size());
1997 tree element_type
= TREE_TYPE(type_tree
);
1998 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1999 vec
<constructor_elt
, va_gc
> *init
;
2000 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
2002 tree sink
= NULL_TREE
;
2003 bool is_constant
= true;
2004 for (size_t i
= 0; i
< vals
.size(); ++i
)
2006 tree index
= size_int(indexes
[i
]);
2007 tree val
= (vals
[i
])->get_tree();
2009 if (index
== error_mark_node
2010 || val
== error_mark_node
)
2011 return this->error_expression();
2013 if (element_size
== 0)
2015 // GIMPLE cannot represent arrays of zero-sized types so trying
2016 // to construct an array of zero-sized values might lead to errors.
2017 // Instead, we evaluate each expression that would have been added as
2018 // an array value for its side-effects and construct an empty array.
2019 append_to_statement_list(val
, &sink
);
2023 if (!TREE_CONSTANT(val
))
2024 is_constant
= false;
2026 constructor_elt empty
= {NULL
, NULL
};
2027 constructor_elt
* elt
= init
->quick_push(empty
);
2032 tree ret
= build_constructor(type_tree
, init
);
2034 TREE_CONSTANT(ret
) = 1;
2035 if (sink
!= NULL_TREE
)
2036 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2037 type_tree
, sink
, ret
);
2038 return this->make_expression(ret
);
2041 // Return an expression for the address of BASE[INDEX].
2044 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
2047 tree base_tree
= base
->get_tree();
2048 tree index_tree
= index
->get_tree();
2049 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
2050 if (base_tree
== error_mark_node
2051 || TREE_TYPE(base_tree
) == error_mark_node
2052 || index_tree
== error_mark_node
2053 || element_type_tree
== error_mark_node
)
2054 return this->error_expression();
2056 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
2057 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
2058 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
2059 index_tree
, element_size
);
2060 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
2061 TREE_TYPE(base_tree
), base_tree
, offset
);
2062 return this->make_expression(ptr
);
2065 // Return an expression representing ARRAY[INDEX]
2068 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
2071 tree array_tree
= array
->get_tree();
2072 tree index_tree
= index
->get_tree();
2073 if (array_tree
== error_mark_node
2074 || TREE_TYPE(array_tree
) == error_mark_node
2075 || index_tree
== error_mark_node
)
2076 return this->error_expression();
2078 // A function call that returns a zero sized object will have been
2079 // changed to return void. If we see void here, assume we are
2080 // dealing with a zero sized type and just evaluate the operands.
2082 if (TREE_TYPE(array_tree
) != void_type_node
)
2083 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
2084 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
2085 index_tree
, NULL_TREE
, NULL_TREE
);
2087 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2088 void_type_node
, array_tree
, index_tree
);
2090 return this->make_expression(ret
);
2093 // Create an expression for a call to FN_EXPR with FN_ARGS.
2095 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
2096 Bexpression
* fn_expr
,
2097 const std::vector
<Bexpression
*>& fn_args
,
2098 Bexpression
* chain_expr
,
2101 tree fn
= fn_expr
->get_tree();
2102 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
2103 return this->error_expression();
2105 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
2106 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
2108 size_t nargs
= fn_args
.size();
2109 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
2110 for (size_t i
= 0; i
< nargs
; ++i
)
2112 args
[i
] = fn_args
.at(i
)->get_tree();
2113 if (args
[i
] == error_mark_node
)
2114 return this->error_expression();
2118 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
2119 fndecl
= TREE_OPERAND(fndecl
, 0);
2121 // This is to support builtin math functions when using 80387 math.
2122 tree excess_type
= NULL_TREE
;
2124 && TREE_CODE(fndecl
) == FUNCTION_DECL
2125 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
2126 && DECL_IS_UNDECLARED_BUILTIN (fndecl
)
2128 && ((SCALAR_FLOAT_TYPE_P(rettype
)
2129 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
2130 || (COMPLEX_FLOAT_TYPE_P(rettype
)
2131 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
2133 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
2134 if (excess_type
!= NULL_TREE
)
2136 tree excess_fndecl
= mathfn_built_in(excess_type
,
2137 DECL_FUNCTION_CODE(fndecl
));
2138 if (excess_fndecl
== NULL_TREE
)
2139 excess_type
= NULL_TREE
;
2142 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
2144 for (size_t i
= 0; i
< nargs
; ++i
)
2146 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
2147 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
2148 args
[i
] = ::convert(excess_type
, args
[i
]);
2155 build_call_array_loc(location
.gcc_location(),
2156 excess_type
!= NULL_TREE
? excess_type
: rettype
,
2160 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
2162 if (excess_type
!= NULL_TREE
)
2164 // Calling convert here can undo our excess precision change.
2165 // That may or may not be a bug in convert_to_real.
2166 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
2170 return this->make_expression(ret
);
2173 // An expression as a statement.
2176 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2178 return this->make_statement(expr
->get_tree());
2181 // Variable initialization.
2184 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2186 tree var_tree
= var
->get_decl();
2187 tree init_tree
= init
->get_tree();
2188 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2189 return this->error_statement();
2190 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2192 // To avoid problems with GNU ld, we don't make zero-sized
2193 // externally visible variables. That might lead us to doing an
2194 // initialization of a zero-sized expression to a non-zero sized
2195 // variable, or vice-versa. Avoid crashes by omitting the
2196 // initializer. Such initializations don't mean anything anyhow.
2197 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2198 && init_tree
!= NULL_TREE
2199 && TREE_TYPE(init_tree
) != void_type_node
2200 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2202 DECL_INITIAL(var_tree
) = init_tree
;
2203 init_tree
= NULL_TREE
;
2206 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2207 void_type_node
, var_tree
);
2208 if (init_tree
!= NULL_TREE
)
2209 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2210 void_type_node
, init_tree
, ret
);
2212 return this->make_statement(ret
);
2218 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2219 Bexpression
* rhs
, Location location
)
2221 tree lhs_tree
= lhs
->get_tree();
2222 tree rhs_tree
= rhs
->get_tree();
2223 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2224 return this->error_statement();
2226 // To avoid problems with GNU ld, we don't make zero-sized
2227 // externally visible variables. That might lead us to doing an
2228 // assignment of a zero-sized expression to a non-zero sized
2229 // expression; avoid crashes here by avoiding assignments of
2230 // zero-sized expressions. Such assignments don't really mean
2232 if (TREE_TYPE(lhs_tree
) == void_type_node
2233 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2234 || TREE_TYPE(rhs_tree
) == void_type_node
2235 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2236 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2237 this->expression_statement(bfn
, rhs
));
2239 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2241 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2244 lhs_tree
, rhs_tree
));
2250 Gcc_backend::return_statement(Bfunction
* bfunction
,
2251 const std::vector
<Bexpression
*>& vals
,
2254 tree fntree
= bfunction
->get_tree();
2255 if (fntree
== error_mark_node
)
2256 return this->error_statement();
2257 tree result
= DECL_RESULT(fntree
);
2258 if (result
== error_mark_node
)
2259 return this->error_statement();
2261 // If the result size is zero bytes, we have set the function type
2262 // to have a result type of void, so don't return anything.
2263 // See the function_type method.
2264 tree res_type
= TREE_TYPE(result
);
2265 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2267 tree stmt_list
= NULL_TREE
;
2268 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2272 tree val
= (*p
)->get_tree();
2273 if (val
== error_mark_node
)
2274 return this->error_statement();
2275 append_to_statement_list(val
, &stmt_list
);
2277 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2278 void_type_node
, NULL_TREE
);
2279 append_to_statement_list(ret
, &stmt_list
);
2280 return this->make_statement(stmt_list
);
2285 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2287 else if (vals
.size() == 1)
2289 tree val
= vals
.front()->get_tree();
2290 if (val
== error_mark_node
)
2291 return this->error_statement();
2292 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2293 void_type_node
, result
,
2294 vals
.front()->get_tree());
2295 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2296 void_type_node
, set
);
2300 // To return multiple values, copy the values into a temporary
2301 // variable of the right structure type, and then assign the
2302 // temporary variable to the DECL_RESULT in the return
2304 tree stmt_list
= NULL_TREE
;
2305 tree rettype
= TREE_TYPE(result
);
2307 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2308 push_struct_function(fntree
);
2310 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2311 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2314 tree field
= TYPE_FIELDS(rettype
);
2315 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2317 p
++, field
= DECL_CHAIN(field
))
2319 gcc_assert(field
!= NULL_TREE
);
2320 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2321 TREE_TYPE(field
), rettmp
, field
,
2323 tree val
= (*p
)->get_tree();
2324 if (val
== error_mark_node
)
2325 return this->error_statement();
2326 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2328 ref
, (*p
)->get_tree());
2329 append_to_statement_list(set
, &stmt_list
);
2331 gcc_assert(field
== NULL_TREE
);
2332 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2335 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2336 void_type_node
, set
);
2337 append_to_statement_list(ret_expr
, &stmt_list
);
2340 return this->make_statement(ret
);
2343 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2344 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2345 // NULL, it will always be executed. This is used for handling defers in Go
2346 // functions. In C++, the resulting code is of this form:
2347 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2350 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2351 Bstatement
* except_stmt
,
2352 Bstatement
* finally_stmt
,
2355 tree stat_tree
= bstat
->get_tree();
2356 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2357 tree finally_tree
= finally_stmt
== NULL
2359 : finally_stmt
->get_tree();
2361 if (stat_tree
== error_mark_node
2362 || except_tree
== error_mark_node
2363 || finally_tree
== error_mark_node
)
2364 return this->error_statement();
2366 if (except_tree
!= NULL_TREE
)
2367 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2368 void_type_node
, stat_tree
,
2369 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2370 void_type_node
, NULL
, except_tree
));
2371 if (finally_tree
!= NULL_TREE
)
2372 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2373 void_type_node
, stat_tree
, finally_tree
);
2374 return this->make_statement(stat_tree
);
2380 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2381 Bblock
* then_block
, Bblock
* else_block
,
2384 tree cond_tree
= condition
->get_tree();
2385 tree then_tree
= then_block
->get_tree();
2386 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2387 if (cond_tree
== error_mark_node
2388 || then_tree
== error_mark_node
2389 || else_tree
== error_mark_node
)
2390 return this->error_statement();
2391 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2392 cond_tree
, then_tree
, else_tree
);
2393 return this->make_statement(ret
);
2399 Gcc_backend::switch_statement(
2400 Bfunction
* function
,
2402 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2403 const std::vector
<Bstatement
*>& statements
,
2404 Location switch_location
)
2406 gcc_assert(cases
.size() == statements
.size());
2408 tree decl
= function
->get_tree();
2409 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2410 push_struct_function(decl
);
2412 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2414 tree stmt_list
= NULL_TREE
;
2415 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2416 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2417 ps
!= statements
.end();
2422 location_t loc
= (*ps
!= NULL
2423 ? EXPR_LOCATION((*ps
)->get_tree())
2424 : UNKNOWN_LOCATION
);
2425 tree label
= create_artificial_label(loc
);
2426 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2427 append_to_statement_list(c
, &stmt_list
);
2431 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2435 tree t
= (*pcv
)->get_tree();
2436 if (t
== error_mark_node
)
2437 return this->error_statement();
2438 location_t loc
= EXPR_LOCATION(t
);
2439 tree label
= create_artificial_label(loc
);
2440 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2441 append_to_statement_list(c
, &stmt_list
);
2447 tree t
= (*ps
)->get_tree();
2448 if (t
== error_mark_node
)
2449 return this->error_statement();
2450 append_to_statement_list(t
, &stmt_list
);
2455 tree tv
= value
->get_tree();
2456 if (tv
== error_mark_node
)
2457 return this->error_statement();
2458 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2459 NULL_TREE
, tv
, stmt_list
);
2460 return this->make_statement(t
);
2463 // Pair of statements.
2466 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2468 tree stmt_list
= NULL_TREE
;
2469 tree t
= s1
->get_tree();
2470 if (t
== error_mark_node
)
2471 return this->error_statement();
2472 append_to_statement_list(t
, &stmt_list
);
2474 if (t
== error_mark_node
)
2475 return this->error_statement();
2476 append_to_statement_list(t
, &stmt_list
);
2478 // If neither statement has any side effects, stmt_list can be NULL
2480 if (stmt_list
== NULL_TREE
)
2481 stmt_list
= integer_zero_node
;
2483 return this->make_statement(stmt_list
);
2486 // List of statements.
2489 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2491 tree stmt_list
= NULL_TREE
;
2492 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2493 p
!= statements
.end();
2496 tree t
= (*p
)->get_tree();
2497 if (t
== error_mark_node
)
2498 return this->error_statement();
2499 append_to_statement_list(t
, &stmt_list
);
2501 return this->make_statement(stmt_list
);
2504 // Make a block. For some reason gcc uses a dual structure for
2505 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2506 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2510 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2511 const std::vector
<Bvariable
*>& vars
,
2512 Location start_location
,
2515 tree block_tree
= make_node(BLOCK
);
2516 if (enclosing
== NULL
)
2518 tree fndecl
= function
->get_tree();
2519 gcc_assert(fndecl
!= NULL_TREE
);
2521 // We may have already created a block for local variables when
2522 // we take the address of a parameter.
2523 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2525 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2526 DECL_INITIAL(fndecl
) = block_tree
;
2530 tree superblock_tree
= DECL_INITIAL(fndecl
);
2531 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2533 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2535 pp
= &BLOCK_CHAIN(*pp
))
2542 tree superbind_tree
= enclosing
->get_tree();
2543 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2544 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2546 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2548 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2550 pp
= &BLOCK_CHAIN(*pp
))
2555 tree
* pp
= &BLOCK_VARS(block_tree
);
2556 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2560 *pp
= (*pv
)->get_decl();
2561 if (*pp
!= error_mark_node
)
2562 pp
= &DECL_CHAIN(*pp
);
2566 TREE_USED(block_tree
) = 1;
2568 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2569 void_type_node
, BLOCK_VARS(block_tree
),
2570 NULL_TREE
, block_tree
);
2571 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2572 return new Bblock(bind_tree
);
2575 // Add statements to a block.
2578 Gcc_backend::block_add_statements(Bblock
* bblock
,
2579 const std::vector
<Bstatement
*>& statements
)
2581 tree stmt_list
= NULL_TREE
;
2582 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2583 p
!= statements
.end();
2586 tree s
= (*p
)->get_tree();
2587 if (s
!= error_mark_node
)
2588 append_to_statement_list(s
, &stmt_list
);
2591 tree bind_tree
= bblock
->get_tree();
2592 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2593 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2596 // Return a block as a statement.
2599 Gcc_backend::block_statement(Bblock
* bblock
)
2601 tree bind_tree
= bblock
->get_tree();
2602 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2603 return this->make_statement(bind_tree
);
2606 // This is not static because we declare it with GTY(()) in go-c.h.
2607 tree go_non_zero_struct
;
2609 // Return a type corresponding to TYPE with non-zero size.
2612 Gcc_backend::non_zero_size_type(tree type
)
2614 if (int_size_in_bytes(type
) != 0)
2617 switch (TREE_CODE(type
))
2620 if (TYPE_FIELDS(type
) != NULL_TREE
)
2622 tree ns
= make_node(RECORD_TYPE
);
2623 tree field_trees
= NULL_TREE
;
2624 tree
*pp
= &field_trees
;
2625 for (tree field
= TYPE_FIELDS(type
);
2627 field
= DECL_CHAIN(field
))
2629 tree ft
= TREE_TYPE(field
);
2630 if (field
== TYPE_FIELDS(type
))
2631 ft
= non_zero_size_type(ft
);
2632 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2633 DECL_NAME(field
), ft
);
2634 DECL_CONTEXT(f
) = ns
;
2636 pp
= &DECL_CHAIN(f
);
2638 TYPE_FIELDS(ns
) = field_trees
;
2643 if (go_non_zero_struct
== NULL_TREE
)
2645 type
= make_node(RECORD_TYPE
);
2646 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2647 get_identifier("dummy"),
2649 DECL_CONTEXT(field
) = type
;
2650 TYPE_FIELDS(type
) = field
;
2652 go_non_zero_struct
= type
;
2654 return go_non_zero_struct
;
2658 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2659 return build_array_type_nelts(element_type
, 1);
2669 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2670 // can be created multiple times and thus have multiple tree
2671 // representations. Make sure this does not confuse the middle-end.
2674 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2676 if (type_tree
== TREE_TYPE(expr_tree
))
2679 if (type_tree
== error_mark_node
2680 || expr_tree
== error_mark_node
2681 || TREE_TYPE(expr_tree
) == error_mark_node
)
2682 return error_mark_node
;
2684 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2685 if (POINTER_TYPE_P(type_tree
)
2686 || INTEGRAL_TYPE_P(type_tree
)
2687 || SCALAR_FLOAT_TYPE_P(type_tree
)
2688 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2689 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2690 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2691 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2693 gcc_assert(int_size_in_bytes(type_tree
)
2694 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2695 if (TYPE_MAIN_VARIANT(type_tree
)
2696 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2697 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2698 type_tree
, expr_tree
);
2699 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2700 type_tree
, expr_tree
);
2706 // Make a global variable.
2709 Gcc_backend::global_variable(const std::string
& var_name
,
2710 const std::string
& asm_name
,
2715 tree type_tree
= btype
->get_tree();
2716 if (type_tree
== error_mark_node
)
2717 return this->error_variable();
2719 // The GNU linker does not like dynamic variables with zero size.
2720 tree orig_type_tree
= type_tree
;
2721 bool is_external
= (flags
& variable_is_external
) != 0;
2722 bool is_hidden
= (flags
& variable_is_hidden
) != 0;
2723 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2724 type_tree
= this->non_zero_size_type(type_tree
);
2726 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2727 get_identifier_from_string(var_name
),
2729 if ((flags
& variable_is_external
) != 0)
2731 DECL_EXTERNAL(decl
) = 1;
2732 flags
&=~ variable_is_external
;
2735 TREE_STATIC(decl
) = 1;
2737 if ((flags
& variable_is_hidden
) == 0)
2738 TREE_PUBLIC(decl
) = 1;
2740 flags
&=~ variable_is_hidden
;
2742 if ((flags
& variable_address_is_taken
) != 0)
2744 TREE_ADDRESSABLE(decl
) = 1;
2745 flags
&=~ variable_address_is_taken
;
2748 // We take the address in Bvariable::get_tree if orig_type_tree is
2749 // different from type_tree.
2750 if (orig_type_tree
!= type_tree
)
2751 TREE_ADDRESSABLE(decl
) = 1;
2753 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2755 TREE_USED(decl
) = 1;
2757 if ((flags
& variable_in_unique_section
) != 0)
2759 resolve_unique_section (decl
, 0, 1);
2760 flags
&=~ variable_in_unique_section
;
2763 gcc_assert(flags
== 0);
2765 go_preserve_from_gc(decl
);
2767 return new Bvariable(decl
, orig_type_tree
);
2770 // Set the initial value of a global variable.
2773 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2775 tree expr_tree
= expr
->get_tree();
2776 if (expr_tree
== error_mark_node
)
2778 gcc_assert(TREE_CONSTANT(expr_tree
));
2779 tree var_decl
= var
->get_decl();
2780 if (var_decl
== error_mark_node
)
2782 DECL_INITIAL(var_decl
) = expr_tree
;
2784 // If this variable goes in a unique section, it may need to go into
2785 // a different one now that DECL_INITIAL is set.
2786 if (symtab_node::get(var_decl
)
2787 && symtab_node::get(var_decl
)->implicit_section
)
2789 set_decl_section_name (var_decl
, (const char *) NULL
);
2790 resolve_unique_section (var_decl
,
2791 compute_reloc_for_constant (expr_tree
),
2796 // Make a local variable.
2799 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2800 Btype
* btype
, Bvariable
* decl_var
,
2801 unsigned int flags
, Location location
)
2803 tree type_tree
= btype
->get_tree();
2804 if (type_tree
== error_mark_node
)
2805 return this->error_variable();
2806 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2807 get_identifier_from_string(name
),
2809 DECL_CONTEXT(decl
) = function
->get_tree();
2810 TREE_USED(decl
) = 1;
2811 if ((flags
& variable_address_is_taken
) != 0)
2813 TREE_ADDRESSABLE(decl
) = 1;
2814 flags
&=~ variable_address_is_taken
;
2816 if (decl_var
!= NULL
)
2818 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2819 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2821 go_assert(flags
== 0);
2822 go_preserve_from_gc(decl
);
2823 return new Bvariable(decl
);
2826 // Make a function parameter variable.
2829 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2830 Btype
* btype
, unsigned int flags
,
2833 tree type_tree
= btype
->get_tree();
2834 if (type_tree
== error_mark_node
)
2835 return this->error_variable();
2836 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2837 get_identifier_from_string(name
),
2839 DECL_CONTEXT(decl
) = function
->get_tree();
2840 DECL_ARG_TYPE(decl
) = type_tree
;
2841 TREE_USED(decl
) = 1;
2842 if ((flags
& variable_address_is_taken
) != 0)
2844 TREE_ADDRESSABLE(decl
) = 1;
2845 flags
&=~ variable_address_is_taken
;
2847 go_assert(flags
== 0);
2848 go_preserve_from_gc(decl
);
2849 return new Bvariable(decl
);
2852 // Make a static chain variable.
2855 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2856 Btype
* btype
, unsigned int flags
,
2859 tree type_tree
= btype
->get_tree();
2860 if (type_tree
== error_mark_node
)
2861 return this->error_variable();
2862 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2863 get_identifier_from_string(name
), type_tree
);
2864 tree fndecl
= function
->get_tree();
2865 DECL_CONTEXT(decl
) = fndecl
;
2866 DECL_ARG_TYPE(decl
) = type_tree
;
2867 TREE_USED(decl
) = 1;
2868 DECL_ARTIFICIAL(decl
) = 1;
2869 DECL_IGNORED_P(decl
) = 1;
2870 DECL_NAMELESS(decl
) = 1;
2871 TREE_READONLY(decl
) = 1;
2873 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2876 push_struct_function(fndecl
);
2878 f
= DECL_STRUCT_FUNCTION(fndecl
);
2880 gcc_assert(f
->static_chain_decl
== NULL
);
2881 f
->static_chain_decl
= decl
;
2882 DECL_STATIC_CHAIN(fndecl
) = 1;
2883 go_assert(flags
== 0);
2885 go_preserve_from_gc(decl
);
2886 return new Bvariable(decl
);
2889 // Make a temporary variable.
2892 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2893 Btype
* btype
, Bexpression
* binit
,
2896 Bstatement
** pstatement
)
2898 gcc_assert(function
!= NULL
);
2899 tree decl
= function
->get_tree();
2900 tree type_tree
= btype
->get_tree();
2901 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2902 if (type_tree
== error_mark_node
2903 || init_tree
== error_mark_node
2904 || decl
== error_mark_node
)
2906 *pstatement
= this->error_statement();
2907 return this->error_variable();
2911 // We can only use create_tmp_var if the type is not addressable.
2912 if (!TREE_ADDRESSABLE(type_tree
))
2914 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2915 push_struct_function(decl
);
2917 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2919 var
= create_tmp_var(type_tree
, "GOTMP");
2924 gcc_assert(bblock
!= NULL
);
2925 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2926 create_tmp_var_name("GOTMP"),
2928 DECL_ARTIFICIAL(var
) = 1;
2929 DECL_IGNORED_P(var
) = 1;
2930 DECL_NAMELESS(var
) = 1;
2932 DECL_CONTEXT(var
) = decl
;
2934 // We have to add this variable to the BLOCK and the BIND_EXPR.
2935 tree bind_tree
= bblock
->get_tree();
2936 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2937 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2938 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2939 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2940 BLOCK_VARS(block_tree
) = var
;
2941 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2944 if (this->type_size(btype
) != 0
2945 && init_tree
!= NULL_TREE
2946 && TREE_TYPE(init_tree
) != void_type_node
)
2947 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2949 if ((flags
& variable_address_is_taken
) != 0)
2951 TREE_ADDRESSABLE(var
) = 1;
2952 flags
&=~ variable_address_is_taken
;
2955 gcc_assert(flags
== 0);
2957 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2959 void_type_node
, var
));
2961 // For a zero sized type, don't initialize VAR with BINIT, but still
2962 // evaluate BINIT for its side effects.
2963 if (init_tree
!= NULL_TREE
2964 && (this->type_size(btype
) == 0
2965 || TREE_TYPE(init_tree
) == void_type_node
))
2967 this->compound_statement(this->expression_statement(function
, binit
),
2970 return new Bvariable(var
);
2973 // Create an implicit variable that is compiler-defined. This is used when
2974 // generating GC root variables and storing the values of a slice initializer.
2977 Gcc_backend::implicit_variable(const std::string
& name
,
2978 const std::string
& asm_name
,
2979 Btype
* type
, unsigned int flags
,
2982 tree type_tree
= type
->get_tree();
2983 if (type_tree
== error_mark_node
)
2984 return this->error_variable();
2986 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2987 get_identifier_from_string(name
), type_tree
);
2988 DECL_EXTERNAL(decl
) = 0;
2989 if ((flags
& variable_is_hidden
) != 0)
2990 flags
&=~ variable_is_hidden
;
2992 TREE_PUBLIC(decl
) = 1;
2993 TREE_STATIC(decl
) = 1;
2994 TREE_USED(decl
) = 1;
2995 DECL_ARTIFICIAL(decl
) = 1;
2996 if ((flags
& variable_is_common
) != 0)
2998 DECL_COMMON(decl
) = 1;
3000 // When the initializer for one implicit_variable refers to another,
3001 // it needs to know the visibility of the referenced struct so that
3002 // compute_reloc_for_constant will return the right value. On many
3003 // systems calling make_decl_one_only will mark the decl as weak,
3004 // which will change the return value of compute_reloc_for_constant.
3005 // We can't reliably call make_decl_one_only yet, because we don't
3006 // yet know the initializer. This issue doesn't arise in C because
3007 // Go initializers, unlike C initializers, can be indirectly
3008 // recursive. To ensure that compute_reloc_for_constant computes
3009 // the right value if some other initializer refers to this one, we
3010 // mark this symbol as weak here. We undo that below in
3011 // immutable_struct_set_init before calling mark_decl_one_only.
3012 DECL_WEAK(decl
) = 1;
3014 flags
&=~ variable_is_common
;
3016 if ((flags
& variable_is_constant
) != 0)
3018 TREE_READONLY(decl
) = 1;
3019 TREE_CONSTANT(decl
) = 1;
3020 flags
&=~ variable_is_constant
;
3022 if ((flags
& variable_address_is_taken
) != 0)
3024 TREE_ADDRESSABLE(decl
) = 1;
3025 flags
&=~ variable_address_is_taken
;
3029 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
3030 DECL_USER_ALIGN(decl
) = 1;
3032 if (! asm_name
.empty())
3033 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3034 gcc_assert(flags
== 0);
3036 go_preserve_from_gc(decl
);
3037 return new Bvariable(decl
);
3040 // Set the initalizer for a variable created by implicit_variable.
3041 // This is where we finish compiling the variable.
3044 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
3045 Btype
*, unsigned int flags
,
3048 tree decl
= var
->get_decl();
3051 init_tree
= NULL_TREE
;
3053 init_tree
= init
->get_tree();
3054 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3057 DECL_INITIAL(decl
) = init_tree
;
3059 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3060 // See the comment where DECL_WEAK is set in implicit_variable.
3061 if ((flags
& variable_is_common
) != 0)
3063 DECL_WEAK(decl
) = 0;
3064 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3067 resolve_unique_section(decl
, 2, 1);
3069 rest_of_decl_compilation(decl
, 1, 0);
3072 // Return a reference to an implicit variable defined in another package.
3075 Gcc_backend::implicit_variable_reference(const std::string
& name
,
3076 const std::string
& asm_name
,
3079 tree type_tree
= btype
->get_tree();
3080 if (type_tree
== error_mark_node
)
3081 return this->error_variable();
3083 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
3084 get_identifier_from_string(name
), type_tree
);
3085 DECL_EXTERNAL(decl
) = 1;
3086 TREE_PUBLIC(decl
) = 1;
3087 TREE_STATIC(decl
) = 0;
3088 DECL_ARTIFICIAL(decl
) = 1;
3089 if (! asm_name
.empty())
3090 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3091 go_preserve_from_gc(decl
);
3092 return new Bvariable(decl
);
3095 // Create a named immutable initialized data structure.
3098 Gcc_backend::immutable_struct(const std::string
& name
,
3099 const std::string
& asm_name
,
3100 unsigned int flags
, Btype
* btype
,
3103 tree type_tree
= btype
->get_tree();
3104 if (type_tree
== error_mark_node
)
3105 return this->error_variable();
3106 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3107 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3108 get_identifier_from_string(name
),
3109 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3110 TREE_STATIC(decl
) = 1;
3111 TREE_USED(decl
) = 1;
3112 TREE_READONLY(decl
) = 1;
3113 TREE_CONSTANT(decl
) = 1;
3114 DECL_ARTIFICIAL(decl
) = 1;
3115 if ((flags
& variable_is_hidden
) != 0)
3116 flags
&=~ variable_is_hidden
;
3118 TREE_PUBLIC(decl
) = 1;
3119 if (! asm_name
.empty())
3120 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3121 if ((flags
& variable_address_is_taken
) != 0)
3123 TREE_ADDRESSABLE(decl
) = 1;
3124 flags
&=~ variable_address_is_taken
;
3127 // When the initializer for one immutable_struct refers to another,
3128 // it needs to know the visibility of the referenced struct so that
3129 // compute_reloc_for_constant will return the right value. On many
3130 // systems calling make_decl_one_only will mark the decl as weak,
3131 // which will change the return value of compute_reloc_for_constant.
3132 // We can't reliably call make_decl_one_only yet, because we don't
3133 // yet know the initializer. This issue doesn't arise in C because
3134 // Go initializers, unlike C initializers, can be indirectly
3135 // recursive. To ensure that compute_reloc_for_constant computes
3136 // the right value if some other initializer refers to this one, we
3137 // mark this symbol as weak here. We undo that below in
3138 // immutable_struct_set_init before calling mark_decl_one_only.
3139 if ((flags
& variable_is_common
) != 0)
3141 DECL_WEAK(decl
) = 1;
3142 flags
&=~ variable_is_common
;
3145 gcc_assert(flags
== 0);
3147 // We don't call rest_of_decl_compilation until we have the
3150 go_preserve_from_gc(decl
);
3151 return new Bvariable(decl
);
3154 // Set the initializer for a variable created by immutable_struct.
3155 // This is where we finish compiling the variable.
3158 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
3159 unsigned int flags
, Btype
*, Location
,
3160 Bexpression
* initializer
)
3162 tree decl
= var
->get_decl();
3163 tree init_tree
= initializer
->get_tree();
3164 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3167 DECL_INITIAL(decl
) = init_tree
;
3169 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3170 // See the comment where DECL_WEAK is set in immutable_struct.
3171 if ((flags
& variable_is_common
) != 0)
3173 DECL_WEAK(decl
) = 0;
3174 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3177 // These variables are often unneeded in the final program, so put
3178 // them in their own section so that linker GC can discard them.
3179 resolve_unique_section(decl
,
3180 compute_reloc_for_constant (init_tree
),
3183 rest_of_decl_compilation(decl
, 1, 0);
3186 // Return a reference to an immutable initialized data structure
3187 // defined in another package.
3190 Gcc_backend::immutable_struct_reference(const std::string
& name
,
3191 const std::string
& asm_name
,
3195 tree type_tree
= btype
->get_tree();
3196 if (type_tree
== error_mark_node
)
3197 return this->error_variable();
3198 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3199 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3200 get_identifier_from_string(name
),
3201 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3202 TREE_READONLY(decl
) = 1;
3203 TREE_CONSTANT(decl
) = 1;
3204 DECL_ARTIFICIAL(decl
) = 1;
3205 TREE_PUBLIC(decl
) = 1;
3206 DECL_EXTERNAL(decl
) = 1;
3207 if (! asm_name
.empty())
3208 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3209 go_preserve_from_gc(decl
);
3210 return new Bvariable(decl
);
3216 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
3222 tree func_tree
= function
->get_tree();
3223 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
3224 push_struct_function(func_tree
);
3226 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
3228 decl
= create_artificial_label(location
.gcc_location());
3234 tree id
= get_identifier_from_string(name
);
3235 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3237 DECL_CONTEXT(decl
) = function
->get_tree();
3239 return new Blabel(decl
);
3242 // Make a statement which defines a label.
3245 Gcc_backend::label_definition_statement(Blabel
* label
)
3247 tree lab
= label
->get_tree();
3248 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3249 void_type_node
, lab
);
3250 return this->make_statement(ret
);
3253 // Make a goto statement.
3256 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3258 tree lab
= label
->get_tree();
3259 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3261 return this->make_statement(ret
);
3264 // Get the address of a label.
3267 Gcc_backend::label_address(Blabel
* label
, Location location
)
3269 tree lab
= label
->get_tree();
3271 TREE_ADDRESSABLE(lab
) = 1;
3272 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3273 build_fold_addr_expr_loc(location
.gcc_location(),
3275 return this->make_expression(ret
);
3278 // Declare or define a new function.
3281 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3282 const std::string
& asm_name
, unsigned int flags
,
3285 tree functype
= fntype
->get_tree();
3286 if (functype
!= error_mark_node
)
3288 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3289 functype
= TREE_TYPE(functype
);
3291 tree id
= get_identifier_from_string(name
);
3292 if (functype
== error_mark_node
|| id
== error_mark_node
)
3293 return this->error_function();
3295 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3296 if (! asm_name
.empty())
3297 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3298 if ((flags
& function_is_visible
) != 0)
3299 TREE_PUBLIC(decl
) = 1;
3300 if ((flags
& function_is_declaration
) != 0)
3301 DECL_EXTERNAL(decl
) = 1;
3304 tree restype
= TREE_TYPE(functype
);
3306 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3307 DECL_ARTIFICIAL(resdecl
) = 1;
3308 DECL_IGNORED_P(resdecl
) = 1;
3309 DECL_NAMELESS(resdecl
) = 1;
3310 DECL_CONTEXT(resdecl
) = decl
;
3311 DECL_RESULT(decl
) = resdecl
;
3313 if ((flags
& function_is_inlinable
) == 0)
3314 DECL_UNINLINABLE(decl
) = 1;
3315 if ((flags
& function_no_split_stack
) != 0)
3317 tree attr
= get_identifier ("no_split_stack");
3318 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3320 if ((flags
& function_does_not_return
) != 0)
3321 TREE_THIS_VOLATILE(decl
) = 1;
3322 if ((flags
& function_in_unique_section
) != 0)
3323 resolve_unique_section(decl
, 0, 1);
3324 if ((flags
& function_only_inline
) != 0)
3326 TREE_PUBLIC (decl
) = 1;
3327 DECL_EXTERNAL(decl
) = 1;
3328 DECL_DECLARED_INLINE_P(decl
) = 1;
3331 // Optimize thunk functions for size. A thunk created for a defer
3332 // statement that may call recover looks like:
3333 // if runtime.setdeferretaddr(L1) {
3338 // The idea is that L1 should be the address to which realfn
3339 // returns. This only works if this little function is not over
3340 // optimized. At some point GCC started duplicating the epilogue in
3341 // the basic-block reordering pass, breaking this assumption.
3342 // Optimizing the function for size avoids duplicating the epilogue.
3343 // This optimization shouldn't matter for any thunk since all thunks
3345 size_t pos
= name
.find("..thunk");
3346 if (pos
!= std::string::npos
)
3348 for (pos
+= 7; pos
< name
.length(); ++pos
)
3350 if (name
[pos
] < '0' || name
[pos
] > '9')
3353 if (pos
== name
.length())
3355 struct cl_optimization cur_opts
;
3356 cl_optimization_save(&cur_opts
, &global_options
,
3357 &global_options_set
);
3358 global_options
.x_optimize_size
= 1;
3359 global_options
.x_optimize_fast
= 0;
3360 global_options
.x_optimize_debug
= 0;
3361 DECL_FUNCTION_SPECIFIC_OPTIMIZATION(decl
) =
3362 build_optimization_node(&global_options
, &global_options_set
);
3363 cl_optimization_restore(&global_options
, &global_options_set
,
3368 go_preserve_from_gc(decl
);
3369 return new Bfunction(decl
);
3372 // Create a statement that runs all deferred calls for FUNCTION. This should
3373 // be a statement that looks like this in C++:
3375 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3378 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3379 Bexpression
* defer
, Location location
)
3381 tree undefer_tree
= undefer
->get_tree();
3382 tree defer_tree
= defer
->get_tree();
3383 tree fntree
= function
->get_tree();
3385 if (undefer_tree
== error_mark_node
3386 || defer_tree
== error_mark_node
3387 || fntree
== error_mark_node
)
3388 return this->error_statement();
3390 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3391 push_struct_function(fntree
);
3393 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3395 tree stmt_list
= NULL
;
3396 Blabel
* blabel
= this->label(function
, "", location
);
3397 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3398 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3400 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3401 tree jump
= jump_stmt
->get_tree();
3402 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3403 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3405 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3406 append_to_statement_list(try_catch
, &stmt_list
);
3409 return this->make_statement(stmt_list
);
3412 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3413 // This will only be called for a function definition.
3416 Gcc_backend::function_set_parameters(Bfunction
* function
,
3417 const std::vector
<Bvariable
*>& param_vars
)
3419 tree func_tree
= function
->get_tree();
3420 if (func_tree
== error_mark_node
)
3423 tree params
= NULL_TREE
;
3425 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3426 pv
!= param_vars
.end();
3429 *pp
= (*pv
)->get_decl();
3430 gcc_assert(*pp
!= error_mark_node
);
3431 pp
= &DECL_CHAIN(*pp
);
3434 DECL_ARGUMENTS(func_tree
) = params
;
3438 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3441 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3443 tree func_tree
= function
->get_tree();
3444 tree code
= code_stmt
->get_tree();
3446 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3448 DECL_SAVED_TREE(func_tree
) = code
;
3452 // Look up a named built-in function in the current backend implementation.
3453 // Returns NULL if no built-in function by that name exists.
3456 Gcc_backend::lookup_builtin(const std::string
& name
)
3458 if (this->builtin_functions_
.count(name
) != 0)
3459 return this->builtin_functions_
[name
];
3463 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3464 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3465 // emit early debugging information.
3468 Gcc_backend::write_global_definitions(
3469 const std::vector
<Btype
*>& type_decls
,
3470 const std::vector
<Bexpression
*>& constant_decls
,
3471 const std::vector
<Bfunction
*>& function_decls
,
3472 const std::vector
<Bvariable
*>& variable_decls
)
3474 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3475 + function_decls
.size() + variable_decls
.size();
3477 tree
* defs
= new tree
[count_definitions
];
3479 // Convert all non-erroneous declarations into Gimple form.
3481 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3482 p
!= variable_decls
.end();
3485 tree v
= (*p
)->get_decl();
3486 if (v
!= error_mark_node
)
3489 go_preserve_from_gc(defs
[i
]);
3494 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3495 p
!= type_decls
.end();
3498 tree type_tree
= (*p
)->get_tree();
3499 if (type_tree
!= error_mark_node
3500 && IS_TYPE_OR_DECL_P(type_tree
))
3502 defs
[i
] = TYPE_NAME(type_tree
);
3503 gcc_assert(defs
[i
] != NULL
);
3504 go_preserve_from_gc(defs
[i
]);
3508 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3509 p
!= constant_decls
.end();
3512 if ((*p
)->get_tree() != error_mark_node
)
3514 defs
[i
] = (*p
)->get_tree();
3515 go_preserve_from_gc(defs
[i
]);
3519 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3520 p
!= function_decls
.end();
3523 tree decl
= (*p
)->get_tree();
3524 if (decl
!= error_mark_node
)
3526 go_preserve_from_gc(decl
);
3527 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
3528 allocate_struct_function(decl
, false);
3529 cgraph_node::finalize_function(decl
, true);
3536 // Pass everything back to the middle-end.
3538 wrapup_global_declarations(defs
, i
);
3544 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3546 go_write_export_data(bytes
, size
);
3550 // Define a builtin function. BCODE is the builtin function code
3551 // defined by builtins.def. NAME is the name of the builtin function.
3552 // LIBNAME is the name of the corresponding library function, and is
3553 // NULL if there isn't one. FNTYPE is the type of the function.
3554 // CONST_P is true if the function has the const attribute.
3555 // NORETURN_P is true if the function has the noreturn attribute.
3558 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3559 const char* libname
, tree fntype
, int flags
)
3561 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3562 libname
, NULL_TREE
);
3563 if ((flags
& builtin_const
) != 0)
3564 TREE_READONLY(decl
) = 1;
3565 if ((flags
& builtin_noreturn
) != 0)
3566 TREE_THIS_VOLATILE(decl
) = 1;
3567 if ((flags
& builtin_novops
) != 0)
3568 DECL_IS_NOVOPS(decl
) = 1;
3569 set_builtin_decl(bcode
, decl
, true);
3570 this->builtin_functions_
[name
] = this->make_function(decl
);
3571 if (libname
!= NULL
)
3573 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3575 if ((flags
& builtin_const
) != 0)
3576 TREE_READONLY(decl
) = 1;
3577 if ((flags
& builtin_noreturn
) != 0)
3578 TREE_THIS_VOLATILE(decl
) = 1;
3579 if ((flags
& builtin_novops
) != 0)
3580 DECL_IS_NOVOPS(decl
) = 1;
3581 this->builtin_functions_
[libname
] = this->make_function(decl
);
3585 // Return the backend generator.
3590 return new Gcc_backend();