1 // go-gcc.cc -- Go frontend to gcc IR.
2 // Copyright (C) 2011-2021 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
,
902 unsigned_char_type_node
,
905 this->define_builtin(BUILT_IN_ATOMIC_AND_FETCH_1
, "__atomic_and_fetch_1", NULL
,
907 this->define_builtin(BUILT_IN_ATOMIC_FETCH_AND_1
, "__atomic_fetch_and_1", NULL
,
910 t
= build_function_type_list(unsigned_char_type_node
,
912 unsigned_char_type_node
,
915 this->define_builtin(BUILT_IN_ATOMIC_OR_FETCH_1
, "__atomic_or_fetch_1", NULL
,
917 this->define_builtin(BUILT_IN_ATOMIC_FETCH_OR_1
, "__atomic_fetch_or_1", NULL
,
921 // Get an unnamed integer type.
924 Gcc_backend::integer_type(bool is_unsigned
, int bits
)
929 if (bits
== INT_TYPE_SIZE
)
930 type
= unsigned_type_node
;
931 else if (bits
== CHAR_TYPE_SIZE
)
932 type
= unsigned_char_type_node
;
933 else if (bits
== SHORT_TYPE_SIZE
)
934 type
= short_unsigned_type_node
;
935 else if (bits
== LONG_TYPE_SIZE
)
936 type
= long_unsigned_type_node
;
937 else if (bits
== LONG_LONG_TYPE_SIZE
)
938 type
= long_long_unsigned_type_node
;
940 type
= make_unsigned_type(bits
);
944 if (bits
== INT_TYPE_SIZE
)
945 type
= integer_type_node
;
946 else if (bits
== CHAR_TYPE_SIZE
)
947 type
= signed_char_type_node
;
948 else if (bits
== SHORT_TYPE_SIZE
)
949 type
= short_integer_type_node
;
950 else if (bits
== LONG_TYPE_SIZE
)
951 type
= long_integer_type_node
;
952 else if (bits
== LONG_LONG_TYPE_SIZE
)
953 type
= long_long_integer_type_node
;
955 type
= make_signed_type(bits
);
957 return this->make_type(type
);
960 // Get an unnamed float type.
963 Gcc_backend::float_type(int bits
)
966 if (bits
== FLOAT_TYPE_SIZE
)
967 type
= float_type_node
;
968 else if (bits
== DOUBLE_TYPE_SIZE
)
969 type
= double_type_node
;
970 else if (bits
== LONG_DOUBLE_TYPE_SIZE
)
971 type
= long_double_type_node
;
974 type
= make_node(REAL_TYPE
);
975 TYPE_PRECISION(type
) = bits
;
978 return this->make_type(type
);
981 // Get an unnamed complex type.
984 Gcc_backend::complex_type(int bits
)
987 if (bits
== FLOAT_TYPE_SIZE
* 2)
988 type
= complex_float_type_node
;
989 else if (bits
== DOUBLE_TYPE_SIZE
* 2)
990 type
= complex_double_type_node
;
991 else if (bits
== LONG_DOUBLE_TYPE_SIZE
* 2)
992 type
= complex_long_double_type_node
;
995 type
= make_node(REAL_TYPE
);
996 TYPE_PRECISION(type
) = bits
/ 2;
998 type
= build_complex_type(type
);
1000 return this->make_type(type
);
1003 // Get a pointer type.
1006 Gcc_backend::pointer_type(Btype
* to_type
)
1008 tree to_type_tree
= to_type
->get_tree();
1009 if (to_type_tree
== error_mark_node
)
1010 return this->error_type();
1011 tree type
= build_pointer_type(to_type_tree
);
1012 return this->make_type(type
);
1015 // Make a function type.
1018 Gcc_backend::function_type(const Btyped_identifier
& receiver
,
1019 const std::vector
<Btyped_identifier
>& parameters
,
1020 const std::vector
<Btyped_identifier
>& results
,
1021 Btype
* result_struct
,
1024 tree args
= NULL_TREE
;
1026 if (receiver
.btype
!= NULL
)
1028 tree t
= receiver
.btype
->get_tree();
1029 if (t
== error_mark_node
)
1030 return this->error_type();
1031 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1032 pp
= &TREE_CHAIN(*pp
);
1035 for (std::vector
<Btyped_identifier
>::const_iterator p
= parameters
.begin();
1036 p
!= parameters
.end();
1039 tree t
= p
->btype
->get_tree();
1040 if (t
== error_mark_node
)
1041 return this->error_type();
1042 *pp
= tree_cons(NULL_TREE
, t
, NULL_TREE
);
1043 pp
= &TREE_CHAIN(*pp
);
1046 // Varargs is handled entirely at the Go level. When converted to
1047 // GENERIC functions are not varargs.
1048 *pp
= void_list_node
;
1051 if (results
.empty())
1052 result
= void_type_node
;
1053 else if (results
.size() == 1)
1054 result
= results
.front().btype
->get_tree();
1057 gcc_assert(result_struct
!= NULL
);
1058 result
= result_struct
->get_tree();
1060 if (result
== error_mark_node
)
1061 return this->error_type();
1063 // The libffi library cannot represent a zero-sized object. To
1064 // avoid causing confusion on 32-bit SPARC, we treat a function that
1065 // returns a zero-sized value as returning void. That should do no
1066 // harm since there is no actual value to be returned. See
1067 // https://gcc.gnu.org/PR72814 for details.
1068 if (result
!= void_type_node
&& int_size_in_bytes(result
) == 0)
1069 result
= void_type_node
;
1071 tree fntype
= build_function_type(result
, args
);
1072 if (fntype
== error_mark_node
)
1073 return this->error_type();
1075 return this->make_type(build_pointer_type(fntype
));
1078 // Make a struct type.
1081 Gcc_backend::struct_type(const std::vector
<Btyped_identifier
>& fields
)
1083 return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE
)), fields
);
1086 // Fill in the fields of a struct type.
1089 Gcc_backend::fill_in_struct(Btype
* fill
,
1090 const std::vector
<Btyped_identifier
>& fields
)
1092 tree fill_tree
= fill
->get_tree();
1093 tree field_trees
= NULL_TREE
;
1094 tree
* pp
= &field_trees
;
1095 for (std::vector
<Btyped_identifier
>::const_iterator p
= fields
.begin();
1099 tree name_tree
= get_identifier_from_string(p
->name
);
1100 tree type_tree
= p
->btype
->get_tree();
1101 if (type_tree
== error_mark_node
)
1102 return this->error_type();
1103 tree field
= build_decl(p
->location
.gcc_location(), FIELD_DECL
, name_tree
,
1105 DECL_CONTEXT(field
) = fill_tree
;
1107 pp
= &DECL_CHAIN(field
);
1109 TYPE_FIELDS(fill_tree
) = field_trees
;
1110 layout_type(fill_tree
);
1112 // Because Go permits converting between named struct types and
1113 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
1114 // because we don't try to maintain TYPE_CANONICAL for struct types,
1115 // we need to tell the middle-end to use structural equality.
1116 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1121 // Make an array type.
1124 Gcc_backend::array_type(Btype
* element_btype
, Bexpression
* length
)
1126 return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE
)),
1127 element_btype
, length
);
1130 // Fill in an array type.
1133 Gcc_backend::fill_in_array(Btype
* fill
, Btype
* element_type
,
1134 Bexpression
* length
)
1136 tree element_type_tree
= element_type
->get_tree();
1137 tree length_tree
= length
->get_tree();
1138 if (element_type_tree
== error_mark_node
|| length_tree
== error_mark_node
)
1139 return this->error_type();
1141 gcc_assert(TYPE_SIZE(element_type_tree
) != NULL_TREE
);
1143 length_tree
= fold_convert(sizetype
, length_tree
);
1145 // build_index_type takes the maximum index, which is one less than
1147 tree index_type_tree
= build_index_type(fold_build2(MINUS_EXPR
, sizetype
,
1151 tree fill_tree
= fill
->get_tree();
1152 TREE_TYPE(fill_tree
) = element_type_tree
;
1153 TYPE_DOMAIN(fill_tree
) = index_type_tree
;
1154 TYPE_ADDR_SPACE(fill_tree
) = TYPE_ADDR_SPACE(element_type_tree
);
1155 layout_type(fill_tree
);
1157 if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree
))
1158 SET_TYPE_STRUCTURAL_EQUALITY(fill_tree
);
1159 else if (TYPE_CANONICAL(element_type_tree
) != element_type_tree
1160 || TYPE_CANONICAL(index_type_tree
) != index_type_tree
)
1161 TYPE_CANONICAL(fill_tree
) =
1162 build_array_type(TYPE_CANONICAL(element_type_tree
),
1163 TYPE_CANONICAL(index_type_tree
));
1168 // Create a placeholder for a pointer type.
1171 Gcc_backend::placeholder_pointer_type(const std::string
& name
,
1172 Location location
, bool)
1174 tree ret
= build_distinct_type_copy(ptr_type_node
);
1177 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1178 get_identifier_from_string(name
),
1180 TYPE_NAME(ret
) = decl
;
1182 return this->make_type(ret
);
1185 // Set the real target type for a placeholder pointer type.
1188 Gcc_backend::set_placeholder_pointer_type(Btype
* placeholder
,
1191 tree pt
= placeholder
->get_tree();
1192 if (pt
== error_mark_node
)
1194 gcc_assert(TREE_CODE(pt
) == POINTER_TYPE
);
1195 tree tt
= to_type
->get_tree();
1196 if (tt
== error_mark_node
)
1198 placeholder
->set_tree(error_mark_node
);
1201 gcc_assert(TREE_CODE(tt
) == POINTER_TYPE
);
1202 TREE_TYPE(pt
) = TREE_TYPE(tt
);
1203 TYPE_CANONICAL(pt
) = TYPE_CANONICAL(tt
);
1204 if (TYPE_NAME(pt
) != NULL_TREE
)
1206 // Build the data structure gcc wants to see for a typedef.
1207 tree copy
= build_variant_type_copy(pt
);
1208 TYPE_NAME(copy
) = NULL_TREE
;
1209 DECL_ORIGINAL_TYPE(TYPE_NAME(pt
)) = copy
;
1214 // Set the real values for a placeholder function type.
1217 Gcc_backend::set_placeholder_function_type(Btype
* placeholder
, Btype
* ft
)
1219 return this->set_placeholder_pointer_type(placeholder
, ft
);
1222 // Create a placeholder for a struct type.
1225 Gcc_backend::placeholder_struct_type(const std::string
& name
,
1228 tree ret
= make_node(RECORD_TYPE
);
1231 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1232 get_identifier_from_string(name
),
1234 TYPE_NAME(ret
) = decl
;
1236 // The struct type that eventually replaces this placeholder will require
1237 // structural equality. The placeholder must too, so that the requirement
1238 // for structural equality propagates to references that are constructed
1239 // before the replacement occurs.
1240 SET_TYPE_STRUCTURAL_EQUALITY(ret
);
1242 return this->make_type(ret
);
1245 // Fill in the fields of a placeholder struct type.
1248 Gcc_backend::set_placeholder_struct_type(
1250 const std::vector
<Btyped_identifier
>& fields
)
1252 tree t
= placeholder
->get_tree();
1253 gcc_assert(TREE_CODE(t
) == RECORD_TYPE
&& TYPE_FIELDS(t
) == NULL_TREE
);
1254 Btype
* r
= this->fill_in_struct(placeholder
, fields
);
1256 if (TYPE_NAME(t
) != NULL_TREE
)
1258 // Build the data structure gcc wants to see for a typedef.
1259 tree copy
= build_distinct_type_copy(t
);
1260 TYPE_NAME(copy
) = NULL_TREE
;
1261 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1262 TYPE_SIZE(copy
) = NULL_TREE
;
1263 Btype
* bc
= this->make_type(copy
);
1264 this->fill_in_struct(bc
, fields
);
1268 return r
->get_tree() != error_mark_node
;
1271 // Create a placeholder for an array type.
1274 Gcc_backend::placeholder_array_type(const std::string
& name
,
1277 tree ret
= make_node(ARRAY_TYPE
);
1278 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1279 get_identifier_from_string(name
),
1281 TYPE_NAME(ret
) = decl
;
1282 return this->make_type(ret
);
1285 // Fill in the fields of a placeholder array type.
1288 Gcc_backend::set_placeholder_array_type(Btype
* placeholder
,
1289 Btype
* element_btype
,
1290 Bexpression
* length
)
1292 tree t
= placeholder
->get_tree();
1293 gcc_assert(TREE_CODE(t
) == ARRAY_TYPE
&& TREE_TYPE(t
) == NULL_TREE
);
1294 Btype
* r
= this->fill_in_array(placeholder
, element_btype
, length
);
1296 // Build the data structure gcc wants to see for a typedef.
1297 tree copy
= build_distinct_type_copy(t
);
1298 TYPE_NAME(copy
) = NULL_TREE
;
1299 DECL_ORIGINAL_TYPE(TYPE_NAME(t
)) = copy
;
1301 return r
->get_tree() != error_mark_node
;
1304 // Return a named version of a type.
1307 Gcc_backend::named_type(const std::string
& name
, Btype
* btype
,
1310 tree type
= btype
->get_tree();
1311 if (type
== error_mark_node
)
1312 return this->error_type();
1314 // The middle-end expects a basic type to have a name. In Go every
1315 // basic type will have a name. The first time we see a basic type,
1316 // give it whatever Go name we have at this point.
1317 if (TYPE_NAME(type
) == NULL_TREE
1318 && location
.gcc_location() == BUILTINS_LOCATION
1319 && (TREE_CODE(type
) == INTEGER_TYPE
1320 || TREE_CODE(type
) == REAL_TYPE
1321 || TREE_CODE(type
) == COMPLEX_TYPE
1322 || TREE_CODE(type
) == BOOLEAN_TYPE
))
1324 tree decl
= build_decl(BUILTINS_LOCATION
, TYPE_DECL
,
1325 get_identifier_from_string(name
),
1327 TYPE_NAME(type
) = decl
;
1328 return this->make_type(type
);
1331 tree copy
= build_variant_type_copy(type
);
1332 tree decl
= build_decl(location
.gcc_location(), TYPE_DECL
,
1333 get_identifier_from_string(name
),
1335 DECL_ORIGINAL_TYPE(decl
) = type
;
1336 TYPE_NAME(copy
) = decl
;
1337 return this->make_type(copy
);
1340 // Return a pointer type used as a marker for a circular type.
1343 Gcc_backend::circular_pointer_type(Btype
*, bool)
1345 return this->make_type(ptr_type_node
);
1348 // Return whether we might be looking at a circular type.
1351 Gcc_backend::is_circular_pointer_type(Btype
* btype
)
1353 return btype
->get_tree() == ptr_type_node
;
1356 // Return the size of a type.
1359 Gcc_backend::type_size(Btype
* btype
)
1361 tree t
= btype
->get_tree();
1362 if (t
== error_mark_node
)
1364 if (t
== void_type_node
)
1366 t
= TYPE_SIZE_UNIT(t
);
1367 gcc_assert(tree_fits_uhwi_p (t
));
1368 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(t
);
1369 int64_t ret
= static_cast<int64_t>(val_wide
);
1370 if (ret
< 0 || static_cast<unsigned HOST_WIDE_INT
>(ret
) != val_wide
)
1375 // Return the alignment of a type.
1378 Gcc_backend::type_alignment(Btype
* btype
)
1380 tree t
= btype
->get_tree();
1381 if (t
== error_mark_node
)
1383 return TYPE_ALIGN_UNIT(t
);
1386 // Return the alignment of a struct field of type BTYPE.
1389 Gcc_backend::type_field_alignment(Btype
* btype
)
1391 tree t
= btype
->get_tree();
1392 if (t
== error_mark_node
)
1394 return go_field_alignment(t
);
1397 // Return the offset of a field in a struct.
1400 Gcc_backend::type_field_offset(Btype
* btype
, size_t index
)
1402 tree struct_tree
= btype
->get_tree();
1403 if (struct_tree
== error_mark_node
)
1405 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
1406 tree field
= TYPE_FIELDS(struct_tree
);
1407 for (; index
> 0; --index
)
1409 field
= DECL_CHAIN(field
);
1410 gcc_assert(field
!= NULL_TREE
);
1412 HOST_WIDE_INT offset_wide
= int_byte_position(field
);
1413 int64_t ret
= static_cast<int64_t>(offset_wide
);
1414 gcc_assert(ret
== offset_wide
);
1418 // Return the zero value for a type.
1421 Gcc_backend::zero_expression(Btype
* btype
)
1423 tree t
= btype
->get_tree();
1425 if (t
== error_mark_node
)
1426 ret
= error_mark_node
;
1428 ret
= build_zero_cst(t
);
1429 return this->make_expression(ret
);
1432 // An expression that references a variable.
1435 Gcc_backend::var_expression(Bvariable
* var
, Location location
)
1437 tree ret
= var
->get_tree(location
);
1438 if (ret
== error_mark_node
)
1439 return this->error_expression();
1440 return this->make_expression(ret
);
1443 // An expression that indirectly references an expression.
1446 Gcc_backend::indirect_expression(Btype
* btype
, Bexpression
* expr
,
1447 bool known_valid
, Location location
)
1449 tree expr_tree
= expr
->get_tree();
1450 tree type_tree
= btype
->get_tree();
1451 if (expr_tree
== error_mark_node
|| type_tree
== error_mark_node
)
1452 return this->error_expression();
1454 // If the type of EXPR is a recursive pointer type, then we
1455 // need to insert a cast before indirecting.
1456 tree target_type_tree
= TREE_TYPE(TREE_TYPE(expr_tree
));
1457 if (VOID_TYPE_P(target_type_tree
))
1458 expr_tree
= fold_convert_loc(location
.gcc_location(),
1459 build_pointer_type(type_tree
), expr_tree
);
1461 tree ret
= build_fold_indirect_ref_loc(location
.gcc_location(),
1464 TREE_THIS_NOTRAP(ret
) = 1;
1465 return this->make_expression(ret
);
1468 // Return an expression that declares a constant named NAME with the
1469 // constant value VAL in BTYPE.
1472 Gcc_backend::named_constant_expression(Btype
* btype
, const std::string
& name
,
1473 Bexpression
* val
, Location location
)
1475 tree type_tree
= btype
->get_tree();
1476 tree const_val
= val
->get_tree();
1477 if (type_tree
== error_mark_node
|| const_val
== error_mark_node
)
1478 return this->error_expression();
1480 tree name_tree
= get_identifier_from_string(name
);
1481 tree decl
= build_decl(location
.gcc_location(), CONST_DECL
, name_tree
,
1483 DECL_INITIAL(decl
) = const_val
;
1484 TREE_CONSTANT(decl
) = 1;
1485 TREE_READONLY(decl
) = 1;
1487 go_preserve_from_gc(decl
);
1488 return this->make_expression(decl
);
1491 // Return a typed value as a constant integer.
1494 Gcc_backend::integer_constant_expression(Btype
* btype
, mpz_t val
)
1496 tree t
= btype
->get_tree();
1497 if (t
== error_mark_node
)
1498 return this->error_expression();
1500 tree ret
= double_int_to_tree(t
, mpz_get_double_int(t
, val
, true));
1501 return this->make_expression(ret
);
1504 // Return a typed value as a constant floating-point number.
1507 Gcc_backend::float_constant_expression(Btype
* btype
, mpfr_t val
)
1509 tree t
= btype
->get_tree();
1511 if (t
== error_mark_node
)
1512 return this->error_expression();
1515 real_from_mpfr(&r1
, val
, t
, GMP_RNDN
);
1517 real_convert(&r2
, TYPE_MODE(t
), &r1
);
1518 ret
= build_real(t
, r2
);
1519 return this->make_expression(ret
);
1522 // Return a typed real and imaginary value as a constant complex number.
1525 Gcc_backend::complex_constant_expression(Btype
* btype
, mpc_t val
)
1527 tree t
= btype
->get_tree();
1529 if (t
== error_mark_node
)
1530 return this->error_expression();
1533 real_from_mpfr(&r1
, mpc_realref(val
), TREE_TYPE(t
), GMP_RNDN
);
1535 real_convert(&r2
, TYPE_MODE(TREE_TYPE(t
)), &r1
);
1538 real_from_mpfr(&r3
, mpc_imagref(val
), TREE_TYPE(t
), GMP_RNDN
);
1540 real_convert(&r4
, TYPE_MODE(TREE_TYPE(t
)), &r3
);
1542 ret
= build_complex(t
, build_real(TREE_TYPE(t
), r2
),
1543 build_real(TREE_TYPE(t
), r4
));
1544 return this->make_expression(ret
);
1547 // Make a constant string expression.
1550 Gcc_backend::string_constant_expression(const std::string
& val
)
1552 tree index_type
= build_index_type(size_int(val
.length()));
1553 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
1555 tree string_type
= build_array_type(const_char_type
, index_type
);
1556 TYPE_STRING_FLAG(string_type
) = 1;
1557 tree string_val
= build_string(val
.length(), val
.data());
1558 TREE_TYPE(string_val
) = string_type
;
1560 return this->make_expression(string_val
);
1563 // Make a constant boolean expression.
1566 Gcc_backend::boolean_constant_expression(bool val
)
1568 tree bool_cst
= val
? boolean_true_node
: boolean_false_node
;
1569 return this->make_expression(bool_cst
);
1572 // Return the real part of a complex expression.
1575 Gcc_backend::real_part_expression(Bexpression
* bcomplex
, Location location
)
1577 tree complex_tree
= bcomplex
->get_tree();
1578 if (complex_tree
== error_mark_node
)
1579 return this->error_expression();
1580 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1581 tree ret
= fold_build1_loc(location
.gcc_location(), REALPART_EXPR
,
1582 TREE_TYPE(TREE_TYPE(complex_tree
)),
1584 return this->make_expression(ret
);
1587 // Return the imaginary part of a complex expression.
1590 Gcc_backend::imag_part_expression(Bexpression
* bcomplex
, Location location
)
1592 tree complex_tree
= bcomplex
->get_tree();
1593 if (complex_tree
== error_mark_node
)
1594 return this->error_expression();
1595 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree
)));
1596 tree ret
= fold_build1_loc(location
.gcc_location(), IMAGPART_EXPR
,
1597 TREE_TYPE(TREE_TYPE(complex_tree
)),
1599 return this->make_expression(ret
);
1602 // Make a complex expression given its real and imaginary parts.
1605 Gcc_backend::complex_expression(Bexpression
* breal
, Bexpression
* bimag
,
1608 tree real_tree
= breal
->get_tree();
1609 tree imag_tree
= bimag
->get_tree();
1610 if (real_tree
== error_mark_node
|| imag_tree
== error_mark_node
)
1611 return this->error_expression();
1612 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree
))
1613 == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree
)));
1614 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree
)));
1615 tree ret
= fold_build2_loc(location
.gcc_location(), COMPLEX_EXPR
,
1616 build_complex_type(TREE_TYPE(real_tree
)),
1617 real_tree
, imag_tree
);
1618 return this->make_expression(ret
);
1621 // An expression that converts an expression to a different type.
1624 Gcc_backend::convert_expression(Btype
* type
, Bexpression
* expr
,
1627 tree type_tree
= type
->get_tree();
1628 tree expr_tree
= expr
->get_tree();
1629 if (type_tree
== error_mark_node
1630 || expr_tree
== error_mark_node
1631 || TREE_TYPE(expr_tree
) == error_mark_node
)
1632 return this->error_expression();
1635 if (this->type_size(type
) == 0
1636 || TREE_TYPE(expr_tree
) == void_type_node
)
1638 // Do not convert zero-sized types.
1641 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
1642 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
1643 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
1644 ret
= fold(convert_to_real(type_tree
, expr_tree
));
1645 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
1646 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
1647 else if (TREE_CODE(type_tree
) == POINTER_TYPE
1648 && TREE_CODE(TREE_TYPE(expr_tree
)) == INTEGER_TYPE
)
1649 ret
= fold(convert_to_pointer(type_tree
, expr_tree
));
1650 else if (TREE_CODE(type_tree
) == RECORD_TYPE
1651 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
1652 ret
= fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
1653 type_tree
, expr_tree
);
1655 ret
= fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
1657 return this->make_expression(ret
);
1660 // Get the address of a function.
1663 Gcc_backend::function_code_expression(Bfunction
* bfunc
, Location location
)
1665 tree func
= bfunc
->get_tree();
1666 if (func
== error_mark_node
)
1667 return this->error_expression();
1669 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), func
);
1670 return this->make_expression(ret
);
1673 // Get the address of an expression.
1676 Gcc_backend::address_expression(Bexpression
* bexpr
, Location location
)
1678 tree expr
= bexpr
->get_tree();
1679 if (expr
== error_mark_node
)
1680 return this->error_expression();
1682 tree ret
= build_fold_addr_expr_loc(location
.gcc_location(), expr
);
1683 return this->make_expression(ret
);
1686 // Return an expression for the field at INDEX in BSTRUCT.
1689 Gcc_backend::struct_field_expression(Bexpression
* bstruct
, size_t index
,
1692 tree struct_tree
= bstruct
->get_tree();
1693 if (struct_tree
== error_mark_node
1694 || TREE_TYPE(struct_tree
) == error_mark_node
)
1695 return this->error_expression();
1696 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
1697 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
1698 if (field
== NULL_TREE
)
1700 // This can happen for a type which refers to itself indirectly
1701 // and then turns out to be erroneous.
1702 return this->error_expression();
1704 for (unsigned int i
= index
; i
> 0; --i
)
1706 field
= DECL_CHAIN(field
);
1707 gcc_assert(field
!= NULL_TREE
);
1709 if (TREE_TYPE(field
) == error_mark_node
)
1710 return this->error_expression();
1711 tree ret
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
1712 TREE_TYPE(field
), struct_tree
, field
,
1714 if (TREE_CONSTANT(struct_tree
))
1715 TREE_CONSTANT(ret
) = 1;
1716 return this->make_expression(ret
);
1719 // Return an expression that executes BSTAT before BEXPR.
1722 Gcc_backend::compound_expression(Bstatement
* bstat
, Bexpression
* bexpr
,
1725 tree stat
= bstat
->get_tree();
1726 tree expr
= bexpr
->get_tree();
1727 if (stat
== error_mark_node
|| expr
== error_mark_node
)
1728 return this->error_expression();
1729 tree ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1730 TREE_TYPE(expr
), stat
, expr
);
1731 return this->make_expression(ret
);
1734 // Return an expression that executes THEN_EXPR if CONDITION is true, or
1735 // ELSE_EXPR otherwise.
1738 Gcc_backend::conditional_expression(Bfunction
*, Btype
* btype
,
1739 Bexpression
* condition
,
1740 Bexpression
* then_expr
,
1741 Bexpression
* else_expr
, Location location
)
1743 tree type_tree
= btype
== NULL
? void_type_node
: btype
->get_tree();
1744 tree cond_tree
= condition
->get_tree();
1745 tree then_tree
= then_expr
->get_tree();
1746 tree else_tree
= else_expr
== NULL
? NULL_TREE
: else_expr
->get_tree();
1747 if (type_tree
== error_mark_node
1748 || cond_tree
== error_mark_node
1749 || then_tree
== error_mark_node
1750 || else_tree
== error_mark_node
)
1751 return this->error_expression();
1752 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, type_tree
,
1753 cond_tree
, then_tree
, else_tree
);
1754 return this->make_expression(ret
);
1757 // Return an expression for the unary operation OP EXPR.
1760 Gcc_backend::unary_expression(Operator op
, Bexpression
* expr
, Location location
)
1762 tree expr_tree
= expr
->get_tree();
1763 if (expr_tree
== error_mark_node
1764 || TREE_TYPE(expr_tree
) == error_mark_node
)
1765 return this->error_expression();
1767 tree type_tree
= TREE_TYPE(expr_tree
);
1768 enum tree_code code
;
1771 case OPERATOR_MINUS
:
1773 tree computed_type
= excess_precision_type(type_tree
);
1774 if (computed_type
!= NULL_TREE
)
1776 expr_tree
= convert(computed_type
, expr_tree
);
1777 type_tree
= computed_type
;
1783 code
= TRUTH_NOT_EXPR
;
1786 code
= BIT_NOT_EXPR
;
1793 tree ret
= fold_build1_loc(location
.gcc_location(), code
, type_tree
,
1795 return this->make_expression(ret
);
1798 // Convert a gofrontend operator to an equivalent tree_code.
1800 static enum tree_code
1801 operator_to_tree_code(Operator op
, tree type
)
1803 enum tree_code code
;
1809 case OPERATOR_NOTEQ
:
1825 code
= TRUTH_ORIF_EXPR
;
1827 case OPERATOR_ANDAND
:
1828 code
= TRUTH_ANDIF_EXPR
;
1833 case OPERATOR_MINUS
:
1837 code
= BIT_IOR_EXPR
;
1840 code
= BIT_XOR_EXPR
;
1846 if (TREE_CODE(type
) == REAL_TYPE
|| TREE_CODE(type
) == COMPLEX_TYPE
)
1849 code
= TRUNC_DIV_EXPR
;
1852 code
= TRUNC_MOD_EXPR
;
1854 case OPERATOR_LSHIFT
:
1857 case OPERATOR_RSHIFT
:
1861 code
= BIT_AND_EXPR
;
1863 case OPERATOR_BITCLEAR
:
1864 code
= BIT_AND_EXPR
;
1873 // Return an expression for the binary operation LEFT OP RIGHT.
1876 Gcc_backend::binary_expression(Operator op
, Bexpression
* left
,
1877 Bexpression
* right
, Location location
)
1879 tree left_tree
= left
->get_tree();
1880 tree right_tree
= right
->get_tree();
1881 if (left_tree
== error_mark_node
1882 || right_tree
== error_mark_node
)
1883 return this->error_expression();
1884 enum tree_code code
= operator_to_tree_code(op
, TREE_TYPE(left_tree
));
1886 bool use_left_type
= op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
;
1887 tree type_tree
= use_left_type
? TREE_TYPE(left_tree
) : TREE_TYPE(right_tree
);
1888 tree computed_type
= excess_precision_type(type_tree
);
1889 if (computed_type
!= NULL_TREE
)
1891 left_tree
= convert(computed_type
, left_tree
);
1892 right_tree
= convert(computed_type
, right_tree
);
1893 type_tree
= computed_type
;
1896 // For comparison operators, the resulting type should be boolean.
1900 case OPERATOR_NOTEQ
:
1905 type_tree
= boolean_type_node
;
1911 tree ret
= fold_build2_loc(location
.gcc_location(), code
, type_tree
,
1912 left_tree
, right_tree
);
1913 return this->make_expression(ret
);
1916 // Return an expression that constructs BTYPE with VALS.
1919 Gcc_backend::constructor_expression(Btype
* btype
,
1920 const std::vector
<Bexpression
*>& vals
,
1923 tree type_tree
= btype
->get_tree();
1924 if (type_tree
== error_mark_node
)
1925 return this->error_expression();
1927 vec
<constructor_elt
, va_gc
> *init
;
1928 vec_alloc(init
, vals
.size());
1930 tree sink
= NULL_TREE
;
1931 bool is_constant
= true;
1932 tree field
= TYPE_FIELDS(type_tree
);
1933 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
1935 ++p
, field
= DECL_CHAIN(field
))
1937 gcc_assert(field
!= NULL_TREE
);
1938 tree val
= (*p
)->get_tree();
1939 if (TREE_TYPE(field
) == error_mark_node
1940 || val
== error_mark_node
1941 || TREE_TYPE(val
) == error_mark_node
)
1942 return this->error_expression();
1944 if (int_size_in_bytes(TREE_TYPE(field
)) == 0)
1946 // GIMPLE cannot represent indices of zero-sized types so
1947 // trying to construct a map with zero-sized keys might lead
1948 // to errors. Instead, we evaluate each expression that
1949 // would have been added as a map element for its
1950 // side-effects and construct an empty map.
1951 append_to_statement_list(val
, &sink
);
1955 constructor_elt empty
= {NULL
, NULL
};
1956 constructor_elt
* elt
= init
->quick_push(empty
);
1958 elt
->value
= this->convert_tree(TREE_TYPE(field
), val
, location
);
1959 if (!TREE_CONSTANT(elt
->value
))
1960 is_constant
= false;
1962 gcc_assert(field
== NULL_TREE
);
1963 tree ret
= build_constructor(type_tree
, init
);
1965 TREE_CONSTANT(ret
) = 1;
1966 if (sink
!= NULL_TREE
)
1967 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
1968 type_tree
, sink
, ret
);
1969 return this->make_expression(ret
);
1973 Gcc_backend::array_constructor_expression(
1974 Btype
* array_btype
, const std::vector
<unsigned long>& indexes
,
1975 const std::vector
<Bexpression
*>& vals
, Location location
)
1977 tree type_tree
= array_btype
->get_tree();
1978 if (type_tree
== error_mark_node
)
1979 return this->error_expression();
1981 gcc_assert(indexes
.size() == vals
.size());
1983 tree element_type
= TREE_TYPE(type_tree
);
1984 HOST_WIDE_INT element_size
= int_size_in_bytes(element_type
);
1985 vec
<constructor_elt
, va_gc
> *init
;
1986 vec_alloc(init
, element_size
== 0 ? 0 : vals
.size());
1988 tree sink
= NULL_TREE
;
1989 bool is_constant
= true;
1990 for (size_t i
= 0; i
< vals
.size(); ++i
)
1992 tree index
= size_int(indexes
[i
]);
1993 tree val
= (vals
[i
])->get_tree();
1995 if (index
== error_mark_node
1996 || val
== error_mark_node
)
1997 return this->error_expression();
1999 if (element_size
== 0)
2001 // GIMPLE cannot represent arrays of zero-sized types so trying
2002 // to construct an array of zero-sized values might lead to errors.
2003 // Instead, we evaluate each expression that would have been added as
2004 // an array value for its side-effects and construct an empty array.
2005 append_to_statement_list(val
, &sink
);
2009 if (!TREE_CONSTANT(val
))
2010 is_constant
= false;
2012 constructor_elt empty
= {NULL
, NULL
};
2013 constructor_elt
* elt
= init
->quick_push(empty
);
2018 tree ret
= build_constructor(type_tree
, init
);
2020 TREE_CONSTANT(ret
) = 1;
2021 if (sink
!= NULL_TREE
)
2022 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2023 type_tree
, sink
, ret
);
2024 return this->make_expression(ret
);
2027 // Return an expression for the address of BASE[INDEX].
2030 Gcc_backend::pointer_offset_expression(Bexpression
* base
, Bexpression
* index
,
2033 tree base_tree
= base
->get_tree();
2034 tree index_tree
= index
->get_tree();
2035 tree element_type_tree
= TREE_TYPE(TREE_TYPE(base_tree
));
2036 if (base_tree
== error_mark_node
2037 || TREE_TYPE(base_tree
) == error_mark_node
2038 || index_tree
== error_mark_node
2039 || element_type_tree
== error_mark_node
)
2040 return this->error_expression();
2042 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
2043 index_tree
= fold_convert_loc(location
.gcc_location(), sizetype
, index_tree
);
2044 tree offset
= fold_build2_loc(location
.gcc_location(), MULT_EXPR
, sizetype
,
2045 index_tree
, element_size
);
2046 tree ptr
= fold_build2_loc(location
.gcc_location(), POINTER_PLUS_EXPR
,
2047 TREE_TYPE(base_tree
), base_tree
, offset
);
2048 return this->make_expression(ptr
);
2051 // Return an expression representing ARRAY[INDEX]
2054 Gcc_backend::array_index_expression(Bexpression
* array
, Bexpression
* index
,
2057 tree array_tree
= array
->get_tree();
2058 tree index_tree
= index
->get_tree();
2059 if (array_tree
== error_mark_node
2060 || TREE_TYPE(array_tree
) == error_mark_node
2061 || index_tree
== error_mark_node
)
2062 return this->error_expression();
2064 // A function call that returns a zero sized object will have been
2065 // changed to return void. If we see void here, assume we are
2066 // dealing with a zero sized type and just evaluate the operands.
2068 if (TREE_TYPE(array_tree
) != void_type_node
)
2069 ret
= build4_loc(location
.gcc_location(), ARRAY_REF
,
2070 TREE_TYPE(TREE_TYPE(array_tree
)), array_tree
,
2071 index_tree
, NULL_TREE
, NULL_TREE
);
2073 ret
= fold_build2_loc(location
.gcc_location(), COMPOUND_EXPR
,
2074 void_type_node
, array_tree
, index_tree
);
2076 return this->make_expression(ret
);
2079 // Create an expression for a call to FN_EXPR with FN_ARGS.
2081 Gcc_backend::call_expression(Bfunction
*, // containing fcn for call
2082 Bexpression
* fn_expr
,
2083 const std::vector
<Bexpression
*>& fn_args
,
2084 Bexpression
* chain_expr
,
2087 tree fn
= fn_expr
->get_tree();
2088 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
2089 return this->error_expression();
2091 gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn
)));
2092 tree rettype
= TREE_TYPE(TREE_TYPE(TREE_TYPE(fn
)));
2094 size_t nargs
= fn_args
.size();
2095 tree
* args
= nargs
== 0 ? NULL
: new tree
[nargs
];
2096 for (size_t i
= 0; i
< nargs
; ++i
)
2098 args
[i
] = fn_args
.at(i
)->get_tree();
2099 if (args
[i
] == error_mark_node
)
2100 return this->error_expression();
2104 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
2105 fndecl
= TREE_OPERAND(fndecl
, 0);
2107 // This is to support builtin math functions when using 80387 math.
2108 tree excess_type
= NULL_TREE
;
2110 && TREE_CODE(fndecl
) == FUNCTION_DECL
2111 && fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
)
2112 && DECL_IS_UNDECLARED_BUILTIN (fndecl
)
2114 && ((SCALAR_FLOAT_TYPE_P(rettype
)
2115 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
2116 || (COMPLEX_FLOAT_TYPE_P(rettype
)
2117 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
2119 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
2120 if (excess_type
!= NULL_TREE
)
2122 tree excess_fndecl
= mathfn_built_in(excess_type
,
2123 DECL_FUNCTION_CODE(fndecl
));
2124 if (excess_fndecl
== NULL_TREE
)
2125 excess_type
= NULL_TREE
;
2128 fn
= build_fold_addr_expr_loc(location
.gcc_location(),
2130 for (size_t i
= 0; i
< nargs
; ++i
)
2132 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[i
]))
2133 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[i
])))
2134 args
[i
] = ::convert(excess_type
, args
[i
]);
2141 build_call_array_loc(location
.gcc_location(),
2142 excess_type
!= NULL_TREE
? excess_type
: rettype
,
2146 CALL_EXPR_STATIC_CHAIN (ret
) = chain_expr
->get_tree();
2148 if (excess_type
!= NULL_TREE
)
2150 // Calling convert here can undo our excess precision change.
2151 // That may or may not be a bug in convert_to_real.
2152 ret
= build1_loc(location
.gcc_location(), NOP_EXPR
, rettype
, ret
);
2156 return this->make_expression(ret
);
2159 // An expression as a statement.
2162 Gcc_backend::expression_statement(Bfunction
*, Bexpression
* expr
)
2164 return this->make_statement(expr
->get_tree());
2167 // Variable initialization.
2170 Gcc_backend::init_statement(Bfunction
*, Bvariable
* var
, Bexpression
* init
)
2172 tree var_tree
= var
->get_decl();
2173 tree init_tree
= init
->get_tree();
2174 if (var_tree
== error_mark_node
|| init_tree
== error_mark_node
)
2175 return this->error_statement();
2176 gcc_assert(TREE_CODE(var_tree
) == VAR_DECL
);
2178 // To avoid problems with GNU ld, we don't make zero-sized
2179 // externally visible variables. That might lead us to doing an
2180 // initialization of a zero-sized expression to a non-zero sized
2181 // variable, or vice-versa. Avoid crashes by omitting the
2182 // initializer. Such initializations don't mean anything anyhow.
2183 if (int_size_in_bytes(TREE_TYPE(var_tree
)) != 0
2184 && init_tree
!= NULL_TREE
2185 && TREE_TYPE(init_tree
) != void_type_node
2186 && int_size_in_bytes(TREE_TYPE(init_tree
)) != 0)
2188 DECL_INITIAL(var_tree
) = init_tree
;
2189 init_tree
= NULL_TREE
;
2192 tree ret
= build1_loc(DECL_SOURCE_LOCATION(var_tree
), DECL_EXPR
,
2193 void_type_node
, var_tree
);
2194 if (init_tree
!= NULL_TREE
)
2195 ret
= build2_loc(DECL_SOURCE_LOCATION(var_tree
), COMPOUND_EXPR
,
2196 void_type_node
, init_tree
, ret
);
2198 return this->make_statement(ret
);
2204 Gcc_backend::assignment_statement(Bfunction
* bfn
, Bexpression
* lhs
,
2205 Bexpression
* rhs
, Location location
)
2207 tree lhs_tree
= lhs
->get_tree();
2208 tree rhs_tree
= rhs
->get_tree();
2209 if (lhs_tree
== error_mark_node
|| rhs_tree
== error_mark_node
)
2210 return this->error_statement();
2212 // To avoid problems with GNU ld, we don't make zero-sized
2213 // externally visible variables. That might lead us to doing an
2214 // assignment of a zero-sized expression to a non-zero sized
2215 // expression; avoid crashes here by avoiding assignments of
2216 // zero-sized expressions. Such assignments don't really mean
2218 if (TREE_TYPE(lhs_tree
) == void_type_node
2219 || int_size_in_bytes(TREE_TYPE(lhs_tree
)) == 0
2220 || TREE_TYPE(rhs_tree
) == void_type_node
2221 || int_size_in_bytes(TREE_TYPE(rhs_tree
)) == 0)
2222 return this->compound_statement(this->expression_statement(bfn
, lhs
),
2223 this->expression_statement(bfn
, rhs
));
2225 rhs_tree
= this->convert_tree(TREE_TYPE(lhs_tree
), rhs_tree
, location
);
2227 return this->make_statement(fold_build2_loc(location
.gcc_location(),
2230 lhs_tree
, rhs_tree
));
2236 Gcc_backend::return_statement(Bfunction
* bfunction
,
2237 const std::vector
<Bexpression
*>& vals
,
2240 tree fntree
= bfunction
->get_tree();
2241 if (fntree
== error_mark_node
)
2242 return this->error_statement();
2243 tree result
= DECL_RESULT(fntree
);
2244 if (result
== error_mark_node
)
2245 return this->error_statement();
2247 // If the result size is zero bytes, we have set the function type
2248 // to have a result type of void, so don't return anything.
2249 // See the function_type method.
2250 tree res_type
= TREE_TYPE(result
);
2251 if (res_type
== void_type_node
|| int_size_in_bytes(res_type
) == 0)
2253 tree stmt_list
= NULL_TREE
;
2254 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2258 tree val
= (*p
)->get_tree();
2259 if (val
== error_mark_node
)
2260 return this->error_statement();
2261 append_to_statement_list(val
, &stmt_list
);
2263 tree ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2264 void_type_node
, NULL_TREE
);
2265 append_to_statement_list(ret
, &stmt_list
);
2266 return this->make_statement(stmt_list
);
2271 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
, void_type_node
,
2273 else if (vals
.size() == 1)
2275 tree val
= vals
.front()->get_tree();
2276 if (val
== error_mark_node
)
2277 return this->error_statement();
2278 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2279 void_type_node
, result
,
2280 vals
.front()->get_tree());
2281 ret
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2282 void_type_node
, set
);
2286 // To return multiple values, copy the values into a temporary
2287 // variable of the right structure type, and then assign the
2288 // temporary variable to the DECL_RESULT in the return
2290 tree stmt_list
= NULL_TREE
;
2291 tree rettype
= TREE_TYPE(result
);
2293 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
2294 push_struct_function(fntree
);
2296 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
2297 tree rettmp
= create_tmp_var(rettype
, "RESULT");
2300 tree field
= TYPE_FIELDS(rettype
);
2301 for (std::vector
<Bexpression
*>::const_iterator p
= vals
.begin();
2303 p
++, field
= DECL_CHAIN(field
))
2305 gcc_assert(field
!= NULL_TREE
);
2306 tree ref
= fold_build3_loc(location
.gcc_location(), COMPONENT_REF
,
2307 TREE_TYPE(field
), rettmp
, field
,
2309 tree val
= (*p
)->get_tree();
2310 if (val
== error_mark_node
)
2311 return this->error_statement();
2312 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2314 ref
, (*p
)->get_tree());
2315 append_to_statement_list(set
, &stmt_list
);
2317 gcc_assert(field
== NULL_TREE
);
2318 tree set
= fold_build2_loc(location
.gcc_location(), MODIFY_EXPR
,
2321 tree ret_expr
= fold_build1_loc(location
.gcc_location(), RETURN_EXPR
,
2322 void_type_node
, set
);
2323 append_to_statement_list(ret_expr
, &stmt_list
);
2326 return this->make_statement(ret
);
2329 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
2330 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
2331 // NULL, it will always be executed. This is used for handling defers in Go
2332 // functions. In C++, the resulting code is of this form:
2333 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
2336 Gcc_backend::exception_handler_statement(Bstatement
* bstat
,
2337 Bstatement
* except_stmt
,
2338 Bstatement
* finally_stmt
,
2341 tree stat_tree
= bstat
->get_tree();
2342 tree except_tree
= except_stmt
== NULL
? NULL_TREE
: except_stmt
->get_tree();
2343 tree finally_tree
= finally_stmt
== NULL
2345 : finally_stmt
->get_tree();
2347 if (stat_tree
== error_mark_node
2348 || except_tree
== error_mark_node
2349 || finally_tree
== error_mark_node
)
2350 return this->error_statement();
2352 if (except_tree
!= NULL_TREE
)
2353 stat_tree
= build2_loc(location
.gcc_location(), TRY_CATCH_EXPR
,
2354 void_type_node
, stat_tree
,
2355 build2_loc(location
.gcc_location(), CATCH_EXPR
,
2356 void_type_node
, NULL
, except_tree
));
2357 if (finally_tree
!= NULL_TREE
)
2358 stat_tree
= build2_loc(location
.gcc_location(), TRY_FINALLY_EXPR
,
2359 void_type_node
, stat_tree
, finally_tree
);
2360 return this->make_statement(stat_tree
);
2366 Gcc_backend::if_statement(Bfunction
*, Bexpression
* condition
,
2367 Bblock
* then_block
, Bblock
* else_block
,
2370 tree cond_tree
= condition
->get_tree();
2371 tree then_tree
= then_block
->get_tree();
2372 tree else_tree
= else_block
== NULL
? NULL_TREE
: else_block
->get_tree();
2373 if (cond_tree
== error_mark_node
2374 || then_tree
== error_mark_node
2375 || else_tree
== error_mark_node
)
2376 return this->error_statement();
2377 tree ret
= build3_loc(location
.gcc_location(), COND_EXPR
, void_type_node
,
2378 cond_tree
, then_tree
, else_tree
);
2379 return this->make_statement(ret
);
2385 Gcc_backend::switch_statement(
2386 Bfunction
* function
,
2388 const std::vector
<std::vector
<Bexpression
*> >& cases
,
2389 const std::vector
<Bstatement
*>& statements
,
2390 Location switch_location
)
2392 gcc_assert(cases
.size() == statements
.size());
2394 tree decl
= function
->get_tree();
2395 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2396 push_struct_function(decl
);
2398 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2400 tree stmt_list
= NULL_TREE
;
2401 std::vector
<std::vector
<Bexpression
*> >::const_iterator pc
= cases
.begin();
2402 for (std::vector
<Bstatement
*>::const_iterator ps
= statements
.begin();
2403 ps
!= statements
.end();
2408 location_t loc
= (*ps
!= NULL
2409 ? EXPR_LOCATION((*ps
)->get_tree())
2410 : UNKNOWN_LOCATION
);
2411 tree label
= create_artificial_label(loc
);
2412 tree c
= build_case_label(NULL_TREE
, NULL_TREE
, label
);
2413 append_to_statement_list(c
, &stmt_list
);
2417 for (std::vector
<Bexpression
*>::const_iterator pcv
= pc
->begin();
2421 tree t
= (*pcv
)->get_tree();
2422 if (t
== error_mark_node
)
2423 return this->error_statement();
2424 location_t loc
= EXPR_LOCATION(t
);
2425 tree label
= create_artificial_label(loc
);
2426 tree c
= build_case_label((*pcv
)->get_tree(), NULL_TREE
, label
);
2427 append_to_statement_list(c
, &stmt_list
);
2433 tree t
= (*ps
)->get_tree();
2434 if (t
== error_mark_node
)
2435 return this->error_statement();
2436 append_to_statement_list(t
, &stmt_list
);
2441 tree tv
= value
->get_tree();
2442 if (tv
== error_mark_node
)
2443 return this->error_statement();
2444 tree t
= build2_loc(switch_location
.gcc_location(), SWITCH_EXPR
,
2445 NULL_TREE
, tv
, stmt_list
);
2446 return this->make_statement(t
);
2449 // Pair of statements.
2452 Gcc_backend::compound_statement(Bstatement
* s1
, Bstatement
* s2
)
2454 tree stmt_list
= NULL_TREE
;
2455 tree t
= s1
->get_tree();
2456 if (t
== error_mark_node
)
2457 return this->error_statement();
2458 append_to_statement_list(t
, &stmt_list
);
2460 if (t
== error_mark_node
)
2461 return this->error_statement();
2462 append_to_statement_list(t
, &stmt_list
);
2464 // If neither statement has any side effects, stmt_list can be NULL
2466 if (stmt_list
== NULL_TREE
)
2467 stmt_list
= integer_zero_node
;
2469 return this->make_statement(stmt_list
);
2472 // List of statements.
2475 Gcc_backend::statement_list(const std::vector
<Bstatement
*>& statements
)
2477 tree stmt_list
= NULL_TREE
;
2478 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2479 p
!= statements
.end();
2482 tree t
= (*p
)->get_tree();
2483 if (t
== error_mark_node
)
2484 return this->error_statement();
2485 append_to_statement_list(t
, &stmt_list
);
2487 return this->make_statement(stmt_list
);
2490 // Make a block. For some reason gcc uses a dual structure for
2491 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
2492 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
2496 Gcc_backend::block(Bfunction
* function
, Bblock
* enclosing
,
2497 const std::vector
<Bvariable
*>& vars
,
2498 Location start_location
,
2501 tree block_tree
= make_node(BLOCK
);
2502 if (enclosing
== NULL
)
2504 tree fndecl
= function
->get_tree();
2505 gcc_assert(fndecl
!= NULL_TREE
);
2507 // We may have already created a block for local variables when
2508 // we take the address of a parameter.
2509 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
2511 BLOCK_SUPERCONTEXT(block_tree
) = fndecl
;
2512 DECL_INITIAL(fndecl
) = block_tree
;
2516 tree superblock_tree
= DECL_INITIAL(fndecl
);
2517 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2519 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2521 pp
= &BLOCK_CHAIN(*pp
))
2528 tree superbind_tree
= enclosing
->get_tree();
2529 tree superblock_tree
= BIND_EXPR_BLOCK(superbind_tree
);
2530 gcc_assert(TREE_CODE(superblock_tree
) == BLOCK
);
2532 BLOCK_SUPERCONTEXT(block_tree
) = superblock_tree
;
2534 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
2536 pp
= &BLOCK_CHAIN(*pp
))
2541 tree
* pp
= &BLOCK_VARS(block_tree
);
2542 for (std::vector
<Bvariable
*>::const_iterator pv
= vars
.begin();
2546 *pp
= (*pv
)->get_decl();
2547 if (*pp
!= error_mark_node
)
2548 pp
= &DECL_CHAIN(*pp
);
2552 TREE_USED(block_tree
) = 1;
2554 tree bind_tree
= build3_loc(start_location
.gcc_location(), BIND_EXPR
,
2555 void_type_node
, BLOCK_VARS(block_tree
),
2556 NULL_TREE
, block_tree
);
2557 TREE_SIDE_EFFECTS(bind_tree
) = 1;
2558 return new Bblock(bind_tree
);
2561 // Add statements to a block.
2564 Gcc_backend::block_add_statements(Bblock
* bblock
,
2565 const std::vector
<Bstatement
*>& statements
)
2567 tree stmt_list
= NULL_TREE
;
2568 for (std::vector
<Bstatement
*>::const_iterator p
= statements
.begin();
2569 p
!= statements
.end();
2572 tree s
= (*p
)->get_tree();
2573 if (s
!= error_mark_node
)
2574 append_to_statement_list(s
, &stmt_list
);
2577 tree bind_tree
= bblock
->get_tree();
2578 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2579 BIND_EXPR_BODY(bind_tree
) = stmt_list
;
2582 // Return a block as a statement.
2585 Gcc_backend::block_statement(Bblock
* bblock
)
2587 tree bind_tree
= bblock
->get_tree();
2588 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2589 return this->make_statement(bind_tree
);
2592 // This is not static because we declare it with GTY(()) in go-c.h.
2593 tree go_non_zero_struct
;
2595 // Return a type corresponding to TYPE with non-zero size.
2598 Gcc_backend::non_zero_size_type(tree type
)
2600 if (int_size_in_bytes(type
) != 0)
2603 switch (TREE_CODE(type
))
2606 if (TYPE_FIELDS(type
) != NULL_TREE
)
2608 tree ns
= make_node(RECORD_TYPE
);
2609 tree field_trees
= NULL_TREE
;
2610 tree
*pp
= &field_trees
;
2611 for (tree field
= TYPE_FIELDS(type
);
2613 field
= DECL_CHAIN(field
))
2615 tree ft
= TREE_TYPE(field
);
2616 if (field
== TYPE_FIELDS(type
))
2617 ft
= non_zero_size_type(ft
);
2618 tree f
= build_decl(DECL_SOURCE_LOCATION(field
), FIELD_DECL
,
2619 DECL_NAME(field
), ft
);
2620 DECL_CONTEXT(f
) = ns
;
2622 pp
= &DECL_CHAIN(f
);
2624 TYPE_FIELDS(ns
) = field_trees
;
2629 if (go_non_zero_struct
== NULL_TREE
)
2631 type
= make_node(RECORD_TYPE
);
2632 tree field
= build_decl(UNKNOWN_LOCATION
, FIELD_DECL
,
2633 get_identifier("dummy"),
2635 DECL_CONTEXT(field
) = type
;
2636 TYPE_FIELDS(type
) = field
;
2638 go_non_zero_struct
= type
;
2640 return go_non_zero_struct
;
2644 tree element_type
= non_zero_size_type(TREE_TYPE(type
));
2645 return build_array_type_nelts(element_type
, 1);
2655 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Go type
2656 // can be created multiple times and thus have multiple tree
2657 // representations. Make sure this does not confuse the middle-end.
2660 Gcc_backend::convert_tree(tree type_tree
, tree expr_tree
, Location location
)
2662 if (type_tree
== TREE_TYPE(expr_tree
))
2665 if (type_tree
== error_mark_node
2666 || expr_tree
== error_mark_node
2667 || TREE_TYPE(expr_tree
) == error_mark_node
)
2668 return error_mark_node
;
2670 gcc_assert(TREE_CODE(type_tree
) == TREE_CODE(TREE_TYPE(expr_tree
)));
2671 if (POINTER_TYPE_P(type_tree
)
2672 || INTEGRAL_TYPE_P(type_tree
)
2673 || SCALAR_FLOAT_TYPE_P(type_tree
)
2674 || COMPLEX_FLOAT_TYPE_P(type_tree
))
2675 return fold_convert_loc(location
.gcc_location(), type_tree
, expr_tree
);
2676 else if (TREE_CODE(type_tree
) == RECORD_TYPE
2677 || TREE_CODE(type_tree
) == ARRAY_TYPE
)
2679 gcc_assert(int_size_in_bytes(type_tree
)
2680 == int_size_in_bytes(TREE_TYPE(expr_tree
)));
2681 if (TYPE_MAIN_VARIANT(type_tree
)
2682 == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
2683 return fold_build1_loc(location
.gcc_location(), NOP_EXPR
,
2684 type_tree
, expr_tree
);
2685 return fold_build1_loc(location
.gcc_location(), VIEW_CONVERT_EXPR
,
2686 type_tree
, expr_tree
);
2692 // Make a global variable.
2695 Gcc_backend::global_variable(const std::string
& var_name
,
2696 const std::string
& asm_name
,
2701 tree type_tree
= btype
->get_tree();
2702 if (type_tree
== error_mark_node
)
2703 return this->error_variable();
2705 // The GNU linker does not like dynamic variables with zero size.
2706 tree orig_type_tree
= type_tree
;
2707 bool is_external
= (flags
& variable_is_external
) != 0;
2708 bool is_hidden
= (flags
& variable_is_hidden
) != 0;
2709 if ((is_external
|| !is_hidden
) && int_size_in_bytes(type_tree
) == 0)
2710 type_tree
= this->non_zero_size_type(type_tree
);
2712 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2713 get_identifier_from_string(var_name
),
2715 if ((flags
& variable_is_external
) != 0)
2717 DECL_EXTERNAL(decl
) = 1;
2718 flags
&=~ variable_is_external
;
2721 TREE_STATIC(decl
) = 1;
2723 if ((flags
& variable_is_hidden
) == 0)
2724 TREE_PUBLIC(decl
) = 1;
2726 flags
&=~ variable_is_hidden
;
2728 if ((flags
& variable_address_is_taken
) != 0)
2730 TREE_ADDRESSABLE(decl
) = 1;
2731 flags
&=~ variable_address_is_taken
;
2734 // We take the address in Bvariable::get_tree if orig_type_tree is
2735 // different from type_tree.
2736 if (orig_type_tree
!= type_tree
)
2737 TREE_ADDRESSABLE(decl
) = 1;
2739 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
2741 TREE_USED(decl
) = 1;
2743 if ((flags
& variable_in_unique_section
) != 0)
2745 resolve_unique_section (decl
, 0, 1);
2746 flags
&=~ variable_in_unique_section
;
2749 gcc_assert(flags
== 0);
2751 go_preserve_from_gc(decl
);
2753 return new Bvariable(decl
, orig_type_tree
);
2756 // Set the initial value of a global variable.
2759 Gcc_backend::global_variable_set_init(Bvariable
* var
, Bexpression
* expr
)
2761 tree expr_tree
= expr
->get_tree();
2762 if (expr_tree
== error_mark_node
)
2764 gcc_assert(TREE_CONSTANT(expr_tree
));
2765 tree var_decl
= var
->get_decl();
2766 if (var_decl
== error_mark_node
)
2768 DECL_INITIAL(var_decl
) = expr_tree
;
2770 // If this variable goes in a unique section, it may need to go into
2771 // a different one now that DECL_INITIAL is set.
2772 if (symtab_node::get(var_decl
)
2773 && symtab_node::get(var_decl
)->implicit_section
)
2775 set_decl_section_name (var_decl
, (const char *) NULL
);
2776 resolve_unique_section (var_decl
,
2777 compute_reloc_for_constant (expr_tree
),
2782 // Make a local variable.
2785 Gcc_backend::local_variable(Bfunction
* function
, const std::string
& name
,
2786 Btype
* btype
, Bvariable
* decl_var
,
2787 unsigned int flags
, Location location
)
2789 tree type_tree
= btype
->get_tree();
2790 if (type_tree
== error_mark_node
)
2791 return this->error_variable();
2792 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
2793 get_identifier_from_string(name
),
2795 DECL_CONTEXT(decl
) = function
->get_tree();
2796 TREE_USED(decl
) = 1;
2797 if ((flags
& variable_address_is_taken
) != 0)
2799 TREE_ADDRESSABLE(decl
) = 1;
2800 flags
&=~ variable_address_is_taken
;
2802 if (decl_var
!= NULL
)
2804 DECL_HAS_VALUE_EXPR_P(decl
) = 1;
2805 SET_DECL_VALUE_EXPR(decl
, decl_var
->get_decl());
2807 go_assert(flags
== 0);
2808 go_preserve_from_gc(decl
);
2809 return new Bvariable(decl
);
2812 // Make a function parameter variable.
2815 Gcc_backend::parameter_variable(Bfunction
* function
, const std::string
& name
,
2816 Btype
* btype
, unsigned int flags
,
2819 tree type_tree
= btype
->get_tree();
2820 if (type_tree
== error_mark_node
)
2821 return this->error_variable();
2822 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2823 get_identifier_from_string(name
),
2825 DECL_CONTEXT(decl
) = function
->get_tree();
2826 DECL_ARG_TYPE(decl
) = type_tree
;
2827 TREE_USED(decl
) = 1;
2828 if ((flags
& variable_address_is_taken
) != 0)
2830 TREE_ADDRESSABLE(decl
) = 1;
2831 flags
&=~ variable_address_is_taken
;
2833 go_assert(flags
== 0);
2834 go_preserve_from_gc(decl
);
2835 return new Bvariable(decl
);
2838 // Make a static chain variable.
2841 Gcc_backend::static_chain_variable(Bfunction
* function
, const std::string
& name
,
2842 Btype
* btype
, unsigned int flags
,
2845 tree type_tree
= btype
->get_tree();
2846 if (type_tree
== error_mark_node
)
2847 return this->error_variable();
2848 tree decl
= build_decl(location
.gcc_location(), PARM_DECL
,
2849 get_identifier_from_string(name
), type_tree
);
2850 tree fndecl
= function
->get_tree();
2851 DECL_CONTEXT(decl
) = fndecl
;
2852 DECL_ARG_TYPE(decl
) = type_tree
;
2853 TREE_USED(decl
) = 1;
2854 DECL_ARTIFICIAL(decl
) = 1;
2855 DECL_IGNORED_P(decl
) = 1;
2856 DECL_NAMELESS(decl
) = 1;
2857 TREE_READONLY(decl
) = 1;
2859 struct function
*f
= DECL_STRUCT_FUNCTION(fndecl
);
2862 push_struct_function(fndecl
);
2864 f
= DECL_STRUCT_FUNCTION(fndecl
);
2866 gcc_assert(f
->static_chain_decl
== NULL
);
2867 f
->static_chain_decl
= decl
;
2868 DECL_STATIC_CHAIN(fndecl
) = 1;
2869 go_assert(flags
== 0);
2871 go_preserve_from_gc(decl
);
2872 return new Bvariable(decl
);
2875 // Make a temporary variable.
2878 Gcc_backend::temporary_variable(Bfunction
* function
, Bblock
* bblock
,
2879 Btype
* btype
, Bexpression
* binit
,
2882 Bstatement
** pstatement
)
2884 gcc_assert(function
!= NULL
);
2885 tree decl
= function
->get_tree();
2886 tree type_tree
= btype
->get_tree();
2887 tree init_tree
= binit
== NULL
? NULL_TREE
: binit
->get_tree();
2888 if (type_tree
== error_mark_node
2889 || init_tree
== error_mark_node
2890 || decl
== error_mark_node
)
2892 *pstatement
= this->error_statement();
2893 return this->error_variable();
2897 // We can only use create_tmp_var if the type is not addressable.
2898 if (!TREE_ADDRESSABLE(type_tree
))
2900 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
2901 push_struct_function(decl
);
2903 push_cfun(DECL_STRUCT_FUNCTION(decl
));
2905 var
= create_tmp_var(type_tree
, "GOTMP");
2910 gcc_assert(bblock
!= NULL
);
2911 var
= build_decl(location
.gcc_location(), VAR_DECL
,
2912 create_tmp_var_name("GOTMP"),
2914 DECL_ARTIFICIAL(var
) = 1;
2915 DECL_IGNORED_P(var
) = 1;
2916 DECL_NAMELESS(var
) = 1;
2918 DECL_CONTEXT(var
) = decl
;
2920 // We have to add this variable to the BLOCK and the BIND_EXPR.
2921 tree bind_tree
= bblock
->get_tree();
2922 gcc_assert(TREE_CODE(bind_tree
) == BIND_EXPR
);
2923 tree block_tree
= BIND_EXPR_BLOCK(bind_tree
);
2924 gcc_assert(TREE_CODE(block_tree
) == BLOCK
);
2925 DECL_CHAIN(var
) = BLOCK_VARS(block_tree
);
2926 BLOCK_VARS(block_tree
) = var
;
2927 BIND_EXPR_VARS(bind_tree
) = BLOCK_VARS(block_tree
);
2930 if (this->type_size(btype
) != 0
2931 && init_tree
!= NULL_TREE
2932 && TREE_TYPE(init_tree
) != void_type_node
)
2933 DECL_INITIAL(var
) = this->convert_tree(type_tree
, init_tree
, location
);
2935 if ((flags
& variable_address_is_taken
) != 0)
2937 TREE_ADDRESSABLE(var
) = 1;
2938 flags
&=~ variable_address_is_taken
;
2941 gcc_assert(flags
== 0);
2943 *pstatement
= this->make_statement(build1_loc(location
.gcc_location(),
2945 void_type_node
, var
));
2947 // For a zero sized type, don't initialize VAR with BINIT, but still
2948 // evaluate BINIT for its side effects.
2949 if (init_tree
!= NULL_TREE
2950 && (this->type_size(btype
) == 0
2951 || TREE_TYPE(init_tree
) == void_type_node
))
2953 this->compound_statement(this->expression_statement(function
, binit
),
2956 return new Bvariable(var
);
2959 // Create an implicit variable that is compiler-defined. This is used when
2960 // generating GC root variables and storing the values of a slice initializer.
2963 Gcc_backend::implicit_variable(const std::string
& name
,
2964 const std::string
& asm_name
,
2965 Btype
* type
, unsigned int flags
,
2968 tree type_tree
= type
->get_tree();
2969 if (type_tree
== error_mark_node
)
2970 return this->error_variable();
2972 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
2973 get_identifier_from_string(name
), type_tree
);
2974 DECL_EXTERNAL(decl
) = 0;
2975 if ((flags
& variable_is_hidden
) != 0)
2976 flags
&=~ variable_is_hidden
;
2978 TREE_PUBLIC(decl
) = 1;
2979 TREE_STATIC(decl
) = 1;
2980 TREE_USED(decl
) = 1;
2981 DECL_ARTIFICIAL(decl
) = 1;
2982 if ((flags
& variable_is_common
) != 0)
2984 DECL_COMMON(decl
) = 1;
2986 // When the initializer for one implicit_variable refers to another,
2987 // it needs to know the visibility of the referenced struct so that
2988 // compute_reloc_for_constant will return the right value. On many
2989 // systems calling make_decl_one_only will mark the decl as weak,
2990 // which will change the return value of compute_reloc_for_constant.
2991 // We can't reliably call make_decl_one_only yet, because we don't
2992 // yet know the initializer. This issue doesn't arise in C because
2993 // Go initializers, unlike C initializers, can be indirectly
2994 // recursive. To ensure that compute_reloc_for_constant computes
2995 // the right value if some other initializer refers to this one, we
2996 // mark this symbol as weak here. We undo that below in
2997 // immutable_struct_set_init before calling mark_decl_one_only.
2998 DECL_WEAK(decl
) = 1;
3000 flags
&=~ variable_is_common
;
3002 if ((flags
& variable_is_constant
) != 0)
3004 TREE_READONLY(decl
) = 1;
3005 TREE_CONSTANT(decl
) = 1;
3006 flags
&=~ variable_is_constant
;
3008 if ((flags
& variable_address_is_taken
) != 0)
3010 TREE_ADDRESSABLE(decl
) = 1;
3011 flags
&=~ variable_address_is_taken
;
3015 SET_DECL_ALIGN(decl
, alignment
* BITS_PER_UNIT
);
3016 DECL_USER_ALIGN(decl
) = 1;
3018 if (! asm_name
.empty())
3019 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3020 gcc_assert(flags
== 0);
3022 go_preserve_from_gc(decl
);
3023 return new Bvariable(decl
);
3026 // Set the initalizer for a variable created by implicit_variable.
3027 // This is where we finish compiling the variable.
3030 Gcc_backend::implicit_variable_set_init(Bvariable
* var
, const std::string
&,
3031 Btype
*, unsigned int flags
,
3034 tree decl
= var
->get_decl();
3037 init_tree
= NULL_TREE
;
3039 init_tree
= init
->get_tree();
3040 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3043 DECL_INITIAL(decl
) = init_tree
;
3045 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3046 // See the comment where DECL_WEAK is set in implicit_variable.
3047 if ((flags
& variable_is_common
) != 0)
3049 DECL_WEAK(decl
) = 0;
3050 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3053 resolve_unique_section(decl
, 2, 1);
3055 rest_of_decl_compilation(decl
, 1, 0);
3058 // Return a reference to an implicit variable defined in another package.
3061 Gcc_backend::implicit_variable_reference(const std::string
& name
,
3062 const std::string
& asm_name
,
3065 tree type_tree
= btype
->get_tree();
3066 if (type_tree
== error_mark_node
)
3067 return this->error_variable();
3069 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
3070 get_identifier_from_string(name
), type_tree
);
3071 DECL_EXTERNAL(decl
) = 1;
3072 TREE_PUBLIC(decl
) = 1;
3073 TREE_STATIC(decl
) = 0;
3074 DECL_ARTIFICIAL(decl
) = 1;
3075 if (! asm_name
.empty())
3076 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3077 go_preserve_from_gc(decl
);
3078 return new Bvariable(decl
);
3081 // Create a named immutable initialized data structure.
3084 Gcc_backend::immutable_struct(const std::string
& name
,
3085 const std::string
& asm_name
,
3086 unsigned int flags
, Btype
* btype
,
3089 tree type_tree
= btype
->get_tree();
3090 if (type_tree
== error_mark_node
)
3091 return this->error_variable();
3092 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3093 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3094 get_identifier_from_string(name
),
3095 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3096 TREE_STATIC(decl
) = 1;
3097 TREE_USED(decl
) = 1;
3098 TREE_READONLY(decl
) = 1;
3099 TREE_CONSTANT(decl
) = 1;
3100 DECL_ARTIFICIAL(decl
) = 1;
3101 if ((flags
& variable_is_hidden
) != 0)
3102 flags
&=~ variable_is_hidden
;
3104 TREE_PUBLIC(decl
) = 1;
3105 if (! asm_name
.empty())
3106 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3107 if ((flags
& variable_address_is_taken
) != 0)
3109 TREE_ADDRESSABLE(decl
) = 1;
3110 flags
&=~ variable_address_is_taken
;
3113 // When the initializer for one immutable_struct refers to another,
3114 // it needs to know the visibility of the referenced struct so that
3115 // compute_reloc_for_constant will return the right value. On many
3116 // systems calling make_decl_one_only will mark the decl as weak,
3117 // which will change the return value of compute_reloc_for_constant.
3118 // We can't reliably call make_decl_one_only yet, because we don't
3119 // yet know the initializer. This issue doesn't arise in C because
3120 // Go initializers, unlike C initializers, can be indirectly
3121 // recursive. To ensure that compute_reloc_for_constant computes
3122 // the right value if some other initializer refers to this one, we
3123 // mark this symbol as weak here. We undo that below in
3124 // immutable_struct_set_init before calling mark_decl_one_only.
3125 if ((flags
& variable_is_common
) != 0)
3127 DECL_WEAK(decl
) = 1;
3128 flags
&=~ variable_is_common
;
3131 gcc_assert(flags
== 0);
3133 // We don't call rest_of_decl_compilation until we have the
3136 go_preserve_from_gc(decl
);
3137 return new Bvariable(decl
);
3140 // Set the initializer for a variable created by immutable_struct.
3141 // This is where we finish compiling the variable.
3144 Gcc_backend::immutable_struct_set_init(Bvariable
* var
, const std::string
&,
3145 unsigned int flags
, Btype
*, Location
,
3146 Bexpression
* initializer
)
3148 tree decl
= var
->get_decl();
3149 tree init_tree
= initializer
->get_tree();
3150 if (decl
== error_mark_node
|| init_tree
== error_mark_node
)
3153 DECL_INITIAL(decl
) = init_tree
;
3155 // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
3156 // See the comment where DECL_WEAK is set in immutable_struct.
3157 if ((flags
& variable_is_common
) != 0)
3159 DECL_WEAK(decl
) = 0;
3160 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
3163 // These variables are often unneeded in the final program, so put
3164 // them in their own section so that linker GC can discard them.
3165 resolve_unique_section(decl
,
3166 compute_reloc_for_constant (init_tree
),
3169 rest_of_decl_compilation(decl
, 1, 0);
3172 // Return a reference to an immutable initialized data structure
3173 // defined in another package.
3176 Gcc_backend::immutable_struct_reference(const std::string
& name
,
3177 const std::string
& asm_name
,
3181 tree type_tree
= btype
->get_tree();
3182 if (type_tree
== error_mark_node
)
3183 return this->error_variable();
3184 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
3185 tree decl
= build_decl(location
.gcc_location(), VAR_DECL
,
3186 get_identifier_from_string(name
),
3187 build_qualified_type(type_tree
, TYPE_QUAL_CONST
));
3188 TREE_READONLY(decl
) = 1;
3189 TREE_CONSTANT(decl
) = 1;
3190 DECL_ARTIFICIAL(decl
) = 1;
3191 TREE_PUBLIC(decl
) = 1;
3192 DECL_EXTERNAL(decl
) = 1;
3193 if (! asm_name
.empty())
3194 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3195 go_preserve_from_gc(decl
);
3196 return new Bvariable(decl
);
3202 Gcc_backend::label(Bfunction
* function
, const std::string
& name
,
3208 tree func_tree
= function
->get_tree();
3209 if (DECL_STRUCT_FUNCTION(func_tree
) == NULL
)
3210 push_struct_function(func_tree
);
3212 push_cfun(DECL_STRUCT_FUNCTION(func_tree
));
3214 decl
= create_artificial_label(location
.gcc_location());
3220 tree id
= get_identifier_from_string(name
);
3221 decl
= build_decl(location
.gcc_location(), LABEL_DECL
, id
,
3223 DECL_CONTEXT(decl
) = function
->get_tree();
3225 return new Blabel(decl
);
3228 // Make a statement which defines a label.
3231 Gcc_backend::label_definition_statement(Blabel
* label
)
3233 tree lab
= label
->get_tree();
3234 tree ret
= fold_build1_loc(DECL_SOURCE_LOCATION(lab
), LABEL_EXPR
,
3235 void_type_node
, lab
);
3236 return this->make_statement(ret
);
3239 // Make a goto statement.
3242 Gcc_backend::goto_statement(Blabel
* label
, Location location
)
3244 tree lab
= label
->get_tree();
3245 tree ret
= fold_build1_loc(location
.gcc_location(), GOTO_EXPR
, void_type_node
,
3247 return this->make_statement(ret
);
3250 // Get the address of a label.
3253 Gcc_backend::label_address(Blabel
* label
, Location location
)
3255 tree lab
= label
->get_tree();
3257 TREE_ADDRESSABLE(lab
) = 1;
3258 tree ret
= fold_convert_loc(location
.gcc_location(), ptr_type_node
,
3259 build_fold_addr_expr_loc(location
.gcc_location(),
3261 return this->make_expression(ret
);
3264 // Declare or define a new function.
3267 Gcc_backend::function(Btype
* fntype
, const std::string
& name
,
3268 const std::string
& asm_name
, unsigned int flags
,
3271 tree functype
= fntype
->get_tree();
3272 if (functype
!= error_mark_node
)
3274 gcc_assert(FUNCTION_POINTER_TYPE_P(functype
));
3275 functype
= TREE_TYPE(functype
);
3277 tree id
= get_identifier_from_string(name
);
3278 if (functype
== error_mark_node
|| id
== error_mark_node
)
3279 return this->error_function();
3281 tree decl
= build_decl(location
.gcc_location(), FUNCTION_DECL
, id
, functype
);
3282 if (! asm_name
.empty())
3283 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(asm_name
));
3284 if ((flags
& function_is_visible
) != 0)
3285 TREE_PUBLIC(decl
) = 1;
3286 if ((flags
& function_is_declaration
) != 0)
3287 DECL_EXTERNAL(decl
) = 1;
3290 tree restype
= TREE_TYPE(functype
);
3292 build_decl(location
.gcc_location(), RESULT_DECL
, NULL_TREE
, restype
);
3293 DECL_ARTIFICIAL(resdecl
) = 1;
3294 DECL_IGNORED_P(resdecl
) = 1;
3295 DECL_NAMELESS(resdecl
) = 1;
3296 DECL_CONTEXT(resdecl
) = decl
;
3297 DECL_RESULT(decl
) = resdecl
;
3299 if ((flags
& function_is_inlinable
) == 0)
3300 DECL_UNINLINABLE(decl
) = 1;
3301 if ((flags
& function_no_split_stack
) != 0)
3303 tree attr
= get_identifier ("no_split_stack");
3304 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
3306 if ((flags
& function_does_not_return
) != 0)
3307 TREE_THIS_VOLATILE(decl
) = 1;
3308 if ((flags
& function_in_unique_section
) != 0)
3309 resolve_unique_section(decl
, 0, 1);
3310 if ((flags
& function_only_inline
) != 0)
3312 TREE_PUBLIC (decl
) = 1;
3313 DECL_EXTERNAL(decl
) = 1;
3314 DECL_DECLARED_INLINE_P(decl
) = 1;
3317 // Optimize thunk functions for size. A thunk created for a defer
3318 // statement that may call recover looks like:
3319 // if runtime.setdeferretaddr(L1) {
3324 // The idea is that L1 should be the address to which realfn
3325 // returns. This only works if this little function is not over
3326 // optimized. At some point GCC started duplicating the epilogue in
3327 // the basic-block reordering pass, breaking this assumption.
3328 // Optimizing the function for size avoids duplicating the epilogue.
3329 // This optimization shouldn't matter for any thunk since all thunks
3331 size_t pos
= name
.find("..thunk");
3332 if (pos
!= std::string::npos
)
3334 for (pos
+= 7; pos
< name
.length(); ++pos
)
3336 if (name
[pos
] < '0' || name
[pos
] > '9')
3339 if (pos
== name
.length())
3341 struct cl_optimization cur_opts
;
3342 cl_optimization_save(&cur_opts
, &global_options
,
3343 &global_options_set
);
3344 global_options
.x_optimize_size
= 1;
3345 global_options
.x_optimize_fast
= 0;
3346 global_options
.x_optimize_debug
= 0;
3347 DECL_FUNCTION_SPECIFIC_OPTIMIZATION(decl
) =
3348 build_optimization_node(&global_options
, &global_options_set
);
3349 cl_optimization_restore(&global_options
, &global_options_set
,
3354 go_preserve_from_gc(decl
);
3355 return new Bfunction(decl
);
3358 // Create a statement that runs all deferred calls for FUNCTION. This should
3359 // be a statement that looks like this in C++:
3361 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
3364 Gcc_backend::function_defer_statement(Bfunction
* function
, Bexpression
* undefer
,
3365 Bexpression
* defer
, Location location
)
3367 tree undefer_tree
= undefer
->get_tree();
3368 tree defer_tree
= defer
->get_tree();
3369 tree fntree
= function
->get_tree();
3371 if (undefer_tree
== error_mark_node
3372 || defer_tree
== error_mark_node
3373 || fntree
== error_mark_node
)
3374 return this->error_statement();
3376 if (DECL_STRUCT_FUNCTION(fntree
) == NULL
)
3377 push_struct_function(fntree
);
3379 push_cfun(DECL_STRUCT_FUNCTION(fntree
));
3381 tree stmt_list
= NULL
;
3382 Blabel
* blabel
= this->label(function
, "", location
);
3383 Bstatement
* label_def
= this->label_definition_statement(blabel
);
3384 append_to_statement_list(label_def
->get_tree(), &stmt_list
);
3386 Bstatement
* jump_stmt
= this->goto_statement(blabel
, location
);
3387 tree jump
= jump_stmt
->get_tree();
3388 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer_tree
, jump
);
3389 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
3391 build2(TRY_CATCH_EXPR
, void_type_node
, undefer_tree
, catch_body
);
3392 append_to_statement_list(try_catch
, &stmt_list
);
3395 return this->make_statement(stmt_list
);
3398 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
3399 // This will only be called for a function definition.
3402 Gcc_backend::function_set_parameters(Bfunction
* function
,
3403 const std::vector
<Bvariable
*>& param_vars
)
3405 tree func_tree
= function
->get_tree();
3406 if (func_tree
== error_mark_node
)
3409 tree params
= NULL_TREE
;
3411 for (std::vector
<Bvariable
*>::const_iterator pv
= param_vars
.begin();
3412 pv
!= param_vars
.end();
3415 *pp
= (*pv
)->get_decl();
3416 gcc_assert(*pp
!= error_mark_node
);
3417 pp
= &DECL_CHAIN(*pp
);
3420 DECL_ARGUMENTS(func_tree
) = params
;
3424 // Set the function body for FUNCTION using the code in CODE_BLOCK.
3427 Gcc_backend::function_set_body(Bfunction
* function
, Bstatement
* code_stmt
)
3429 tree func_tree
= function
->get_tree();
3430 tree code
= code_stmt
->get_tree();
3432 if (func_tree
== error_mark_node
|| code
== error_mark_node
)
3434 DECL_SAVED_TREE(func_tree
) = code
;
3438 // Look up a named built-in function in the current backend implementation.
3439 // Returns NULL if no built-in function by that name exists.
3442 Gcc_backend::lookup_builtin(const std::string
& name
)
3444 if (this->builtin_functions_
.count(name
) != 0)
3445 return this->builtin_functions_
[name
];
3449 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
3450 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
3451 // emit early debugging information.
3454 Gcc_backend::write_global_definitions(
3455 const std::vector
<Btype
*>& type_decls
,
3456 const std::vector
<Bexpression
*>& constant_decls
,
3457 const std::vector
<Bfunction
*>& function_decls
,
3458 const std::vector
<Bvariable
*>& variable_decls
)
3460 size_t count_definitions
= type_decls
.size() + constant_decls
.size()
3461 + function_decls
.size() + variable_decls
.size();
3463 tree
* defs
= new tree
[count_definitions
];
3465 // Convert all non-erroneous declarations into Gimple form.
3467 for (std::vector
<Bvariable
*>::const_iterator p
= variable_decls
.begin();
3468 p
!= variable_decls
.end();
3471 tree v
= (*p
)->get_decl();
3472 if (v
!= error_mark_node
)
3475 go_preserve_from_gc(defs
[i
]);
3480 for (std::vector
<Btype
*>::const_iterator p
= type_decls
.begin();
3481 p
!= type_decls
.end();
3484 tree type_tree
= (*p
)->get_tree();
3485 if (type_tree
!= error_mark_node
3486 && IS_TYPE_OR_DECL_P(type_tree
))
3488 defs
[i
] = TYPE_NAME(type_tree
);
3489 gcc_assert(defs
[i
] != NULL
);
3490 go_preserve_from_gc(defs
[i
]);
3494 for (std::vector
<Bexpression
*>::const_iterator p
= constant_decls
.begin();
3495 p
!= constant_decls
.end();
3498 if ((*p
)->get_tree() != error_mark_node
)
3500 defs
[i
] = (*p
)->get_tree();
3501 go_preserve_from_gc(defs
[i
]);
3505 for (std::vector
<Bfunction
*>::const_iterator p
= function_decls
.begin();
3506 p
!= function_decls
.end();
3509 tree decl
= (*p
)->get_tree();
3510 if (decl
!= error_mark_node
)
3512 go_preserve_from_gc(decl
);
3513 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
3514 allocate_struct_function(decl
, false);
3515 cgraph_node::finalize_function(decl
, true);
3522 // Pass everything back to the middle-end.
3524 wrapup_global_declarations(defs
, i
);
3530 Gcc_backend::write_export_data(const char* bytes
, unsigned int size
)
3532 go_write_export_data(bytes
, size
);
3536 // Define a builtin function. BCODE is the builtin function code
3537 // defined by builtins.def. NAME is the name of the builtin function.
3538 // LIBNAME is the name of the corresponding library function, and is
3539 // NULL if there isn't one. FNTYPE is the type of the function.
3540 // CONST_P is true if the function has the const attribute.
3541 // NORETURN_P is true if the function has the noreturn attribute.
3544 Gcc_backend::define_builtin(built_in_function bcode
, const char* name
,
3545 const char* libname
, tree fntype
, int flags
)
3547 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
3548 libname
, NULL_TREE
);
3549 if ((flags
& builtin_const
) != 0)
3550 TREE_READONLY(decl
) = 1;
3551 if ((flags
& builtin_noreturn
) != 0)
3552 TREE_THIS_VOLATILE(decl
) = 1;
3553 if ((flags
& builtin_novops
) != 0)
3554 DECL_IS_NOVOPS(decl
) = 1;
3555 set_builtin_decl(bcode
, decl
, true);
3556 this->builtin_functions_
[name
] = this->make_function(decl
);
3557 if (libname
!= NULL
)
3559 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
3561 if ((flags
& builtin_const
) != 0)
3562 TREE_READONLY(decl
) = 1;
3563 if ((flags
& builtin_noreturn
) != 0)
3564 TREE_THIS_VOLATILE(decl
) = 1;
3565 if ((flags
& builtin_novops
) != 0)
3566 DECL_IS_NOVOPS(decl
) = 1;
3567 this->builtin_functions_
[libname
] = this->make_function(decl
);
3571 // Return the backend generator.
3576 return new Gcc_backend();